optimize internlm2 xcomposer agin (#11124)
This commit is contained in:
		
							parent
							
								
									9372ce87ce
								
							
						
					
					
						commit
						1db9d9a63b
					
				
					 2 changed files with 14 additions and 6 deletions
				
			
		| 
						 | 
					@ -45,8 +45,9 @@ from torch import nn
 | 
				
			||||||
from ipex_llm.utils.common import invalidInputError
 | 
					from ipex_llm.utils.common import invalidInputError
 | 
				
			||||||
from ipex_llm.transformers.models.utils import init_kv_cache, extend_kv_cache, \
 | 
					from ipex_llm.transformers.models.utils import init_kv_cache, extend_kv_cache, \
 | 
				
			||||||
    append_kv_cache, is_enough_kv_cache_room_4_31
 | 
					    append_kv_cache, is_enough_kv_cache_room_4_31
 | 
				
			||||||
from ipex_llm.transformers.models.utils import apply_rotary_pos_emb
 | 
					from ipex_llm.transformers.models.utils import should_use_fuse_rope, apply_rotary_pos_emb
 | 
				
			||||||
from ipex_llm.transformers.models.utils import apply_rotary_pos_emb_no_cache_xpu
 | 
					from ipex_llm.transformers.models.utils import apply_rotary_pos_emb_no_cache_xpu
 | 
				
			||||||
 | 
					from ipex_llm.transformers.models.utils import apply_rotary_pos_emb_cache_freq_xpu
 | 
				
			||||||
from ipex_llm.transformers.models.utils import use_quantize_kv_cache, restore_fp8_kv_cache
 | 
					from ipex_llm.transformers.models.utils import use_quantize_kv_cache, restore_fp8_kv_cache
 | 
				
			||||||
from ipex_llm.transformers.models.utils import update_past_key_value
 | 
					from ipex_llm.transformers.models.utils import update_past_key_value
 | 
				
			||||||
from ipex_llm.transformers.models.utils import use_sdp, use_sdp_causal
 | 
					from ipex_llm.transformers.models.utils import use_sdp, use_sdp_causal
 | 
				
			||||||
| 
						 | 
					@ -83,7 +84,7 @@ def internlm_attention_forward(
 | 
				
			||||||
    if past_key_value is not None:
 | 
					    if past_key_value is not None:
 | 
				
			||||||
        enough_kv_room = is_enough_kv_cache_room_4_31(past_key_value, seq_len=kv_seq_len)
 | 
					        enough_kv_room = is_enough_kv_cache_room_4_31(past_key_value, seq_len=kv_seq_len)
 | 
				
			||||||
        kv_seq_len += past_key_value[0].shape[-2]
 | 
					        kv_seq_len += past_key_value[0].shape[-2]
 | 
				
			||||||
    if query_states.device.type == "xpu" and not (self.training and query_states.requires_grad):
 | 
					    if should_use_fuse_rope(hidden_states, position_ids, self.training):
 | 
				
			||||||
        query_states, key_states = apply_rotary_pos_emb_no_cache_xpu(query_states,
 | 
					        query_states, key_states = apply_rotary_pos_emb_no_cache_xpu(query_states,
 | 
				
			||||||
                                                                     key_states,
 | 
					                                                                     key_states,
 | 
				
			||||||
                                                                     position_ids,
 | 
					                                                                     position_ids,
 | 
				
			||||||
| 
						 | 
					@ -228,7 +229,7 @@ def internlm2_attention_forward(
 | 
				
			||||||
    kv_seq_len = key_states.shape[-2]
 | 
					    kv_seq_len = key_states.shape[-2]
 | 
				
			||||||
    if past_key_value is not None:
 | 
					    if past_key_value is not None:
 | 
				
			||||||
        kv_seq_len += past_key_value[0].shape[-2]
 | 
					        kv_seq_len += past_key_value[0].shape[-2]
 | 
				
			||||||
    if query_states.device.type == "xpu" and not (self.training and query_states.requires_grad):
 | 
					    if should_use_fuse_rope(hidden_states, position_ids, self.training):
 | 
				
			||||||
        query_states, key_states = apply_rotary_pos_emb_no_cache_xpu(query_states,
 | 
					        query_states, key_states = apply_rotary_pos_emb_no_cache_xpu(query_states,
 | 
				
			||||||
                                                                     key_states,
 | 
					                                                                     key_states,
 | 
				
			||||||
                                                                     position_ids,
 | 
					                                                                     position_ids,
 | 
				
			||||||
| 
						 | 
					@ -376,9 +377,16 @@ def internlm_xcomposser2_attention_forward(
 | 
				
			||||||
    kv_seq_len = key_states.shape[-2]
 | 
					    kv_seq_len = key_states.shape[-2]
 | 
				
			||||||
    if past_key_value is not None:
 | 
					    if past_key_value is not None:
 | 
				
			||||||
        kv_seq_len += past_key_value[0].shape[-2]
 | 
					        kv_seq_len += past_key_value[0].shape[-2]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # IPEX-LLM OPT: fuse rope
 | 
				
			||||||
    cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
 | 
					    cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
 | 
				
			||||||
    query_states, key_states = apply_rotary_pos_emb(
 | 
					    if should_use_fuse_rope(hidden_states, position_ids, self.training):
 | 
				
			||||||
        query_states, key_states, cos, sin, position_ids, "internlm")
 | 
					        query_states, key_states = apply_rotary_pos_emb_cache_freq_xpu(
 | 
				
			||||||
 | 
					            query_states, key_states, sin, cos, "internlm", position_ids
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        query_states, key_states = apply_rotary_pos_emb(
 | 
				
			||||||
 | 
					            query_states, key_states, cos, sin, position_ids, "internlm")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # IPEX-LLM OPT: kv cache and quantzie kv cache
 | 
					    # IPEX-LLM OPT: kv cache and quantzie kv cache
 | 
				
			||||||
    use_quantize_kv = use_quantize_kv_cache(self.wqkv, hidden_states)
 | 
					    use_quantize_kv = use_quantize_kv_cache(self.wqkv, hidden_states)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -233,7 +233,7 @@ def apply_rotary_pos_emb_cache_freq_xpu(q, k, sin, cos, model_family, position_i
 | 
				
			||||||
    k_embed = torch.empty(k.shape, dtype=k.dtype, device=k.device)
 | 
					    k_embed = torch.empty(k.shape, dtype=k.dtype, device=k.device)
 | 
				
			||||||
    if model_family in ["qwen", "mixtral"]:
 | 
					    if model_family in ["qwen", "mixtral"]:
 | 
				
			||||||
        linear_q4_0.apply_rotary_embedding_half_q_and_k_cache_freq(q, k, sin, cos, q_embed, k_embed)
 | 
					        linear_q4_0.apply_rotary_embedding_half_q_and_k_cache_freq(q, k, sin, cos, q_embed, k_embed)
 | 
				
			||||||
    elif model_family in ["qwen2", "yuan", "stablelm", "qwen2_moe"]:
 | 
					    elif model_family in ["qwen2", "yuan", "stablelm", "qwen2_moe", "internlm"]:
 | 
				
			||||||
        cos = cos.to(q.dtype)
 | 
					        cos = cos.to(q.dtype)
 | 
				
			||||||
        sin = sin.to(q.dtype)
 | 
					        sin = sin.to(q.dtype)
 | 
				
			||||||
        cos = cos.squeeze(1).squeeze(0)  # [seq_len, dim]
 | 
					        cos = cos.squeeze(1).squeeze(0)  # [seq_len, dim]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue