optimize phi3 1st token performance (#10981)
This commit is contained in:
parent
cfed76b2ed
commit
ad96f32ce0
2 changed files with 26 additions and 13 deletions
|
|
@ -42,6 +42,7 @@ from ipex_llm.transformers.models.utils import (
|
||||||
from ipex_llm.transformers.models.utils import mlp_fusion_check, SILU
|
from ipex_llm.transformers.models.utils import mlp_fusion_check, SILU
|
||||||
from ipex_llm.transformers.models.utils import use_new_esimd_sdp_fp16, use_quantize_kv_cache
|
from ipex_llm.transformers.models.utils import use_new_esimd_sdp_fp16, use_quantize_kv_cache
|
||||||
from ipex_llm.transformers.models.utils import use_sdp_fp8, restore_fp8_kv_cache
|
from ipex_llm.transformers.models.utils import use_sdp_fp8, restore_fp8_kv_cache
|
||||||
|
from ipex_llm.transformers.models.utils import use_sdp_causal
|
||||||
from ipex_llm.transformers.kv import DynamicNormalCache, DynamicFp8Cache
|
from ipex_llm.transformers.kv import DynamicNormalCache, DynamicFp8Cache
|
||||||
|
|
||||||
from typing import Optional, Tuple, List
|
from typing import Optional, Tuple, List
|
||||||
|
|
@ -148,6 +149,10 @@ def attention_forward(
|
||||||
if isinstance(past_key_value, DynamicFp8Cache):
|
if isinstance(past_key_value, DynamicFp8Cache):
|
||||||
key_states, value_states = restore_fp8_kv_cache(key_states, value_states,
|
key_states, value_states = restore_fp8_kv_cache(key_states, value_states,
|
||||||
query_states.dtype)
|
query_states.dtype)
|
||||||
|
if use_sdp_causal(q_len, kv_seq_len, query_states, self.training):
|
||||||
|
import linear_q4_0
|
||||||
|
attn_output = linear_q4_0.sdp_causal(query_states, key_states, value_states)
|
||||||
|
else:
|
||||||
# repeat k/v heads if n_kv_heads < n_heads
|
# repeat k/v heads if n_kv_heads < n_heads
|
||||||
key_states = repeat_kv(key_states, self.num_key_value_groups)
|
key_states = repeat_kv(key_states, self.num_key_value_groups)
|
||||||
value_states = repeat_kv(value_states, self.num_key_value_groups)
|
value_states = repeat_kv(value_states, self.num_key_value_groups)
|
||||||
|
|
|
||||||
|
|
@ -384,6 +384,14 @@ def use_sdp_fp8(q_len, k_len, query_states):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def use_sdp_causal(q_len, kv_len, query_states, training):
|
||||||
|
return (
|
||||||
|
q_len == kv_len # first token
|
||||||
|
and query_states.device.type == "xpu" # GPU
|
||||||
|
and not query_states.requires_grad and not training # no training
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def mlp_fusion_check(x, qtype, training):
|
def mlp_fusion_check(x, qtype, training):
|
||||||
invalidInputError(x.dim() == 2,
|
invalidInputError(x.dim() == 2,
|
||||||
"Here input x's dim should be 2.")
|
"Here input x's dim should be 2.")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue