|
| 1 | +import torch |
| 2 | +import torch.functional as F |
| 3 | +import torch.distributed as dist |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from lightllm.models.llama.layer_infer.transformer_layer_infer import LlamaTransformerLayerInfer |
| 7 | + |
| 8 | +import torch.nn as nn |
| 9 | +from functools import partial |
| 10 | + |
| 11 | + |
| 12 | +def rotate_half(x): |
| 13 | + x1 = x[..., : x.shape[-1] // 2] |
| 14 | + x2 = x[..., x.shape[-1] // 2 :] |
| 15 | + return torch.cat((-x2, x1), dim=-1) |
| 16 | + |
| 17 | + |
| 18 | +def apply_multimodal_rotary_pos_emb(q, k, cos, sin, mrope_section, unsqueeze_dim=1): |
| 19 | + mrope_section = mrope_section * 2 |
| 20 | + cos = torch.cat([m[i % 3] for i, m in enumerate(cos.split(mrope_section, dim=-1))], dim=-1).unsqueeze(unsqueeze_dim) |
| 21 | + sin = torch.cat([m[i % 3] for i, m in enumerate(sin.split(mrope_section, dim=-1))], dim=-1).unsqueeze(unsqueeze_dim) |
| 22 | + |
| 23 | + q_embed = (q * cos) + (rotate_half(q) * sin) |
| 24 | + k_embed = (k * cos) + (rotate_half(k) * sin) |
| 25 | + |
| 26 | + return q_embed, k_embed |
| 27 | + |
| 28 | + |
| 29 | +class Qwen2RMSNorm(nn.Module): |
| 30 | + def __init__(self, hidden_size, device, eps=1e-6): |
| 31 | + super().__init__() |
| 32 | + self.variance_epsilon = eps |
| 33 | + |
| 34 | + def forward(self, hidden_states, weight): |
| 35 | + input_dtype = hidden_states.dtype |
| 36 | + hidden_states = hidden_states.to(torch.float32) |
| 37 | + variance = hidden_states.pow(2).mean(-1, keepdim=True) |
| 38 | + hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) |
| 39 | + |
| 40 | + return (weight * hidden_states).to(input_dtype) |
| 41 | + |
| 42 | + |
| 43 | +class Qwen2VLTransformerLayerInfer(LlamaTransformerLayerInfer): |
| 44 | + def __init__(self, layer_num, network_config, mode=[]): |
| 45 | + super().__init__(layer_num, network_config, mode) |
| 46 | + self.mrope_section = network_config["rope_scaling"]["mrope_section"] |
| 47 | + self.norm_fwd = Qwen2RMSNorm( |
| 48 | + network_config["hidden_size"], device="cuda", eps=network_config.get("rms_norm_eps", 1e-06) |
| 49 | + ) |
| 50 | + |
| 51 | + def _bind_norm(self): |
| 52 | + self._ffn_norm = partial(LlamaTransformerLayerInfer._ffn_norm, self) |
| 53 | + |
| 54 | + def _att_norm(self, input_embedding, infer_state, layer_weight) -> torch.Tensor: |
| 55 | + return self.norm_fwd(input_embedding, weight=layer_weight.att_norm_weight_.weight) |
| 56 | + |
| 57 | + def _get_qkv(self, input, cache_kv, infer_state, layer_weight): |
| 58 | + q = layer_weight.q_proj.mm(input) |
| 59 | + cache_kv = layer_weight.kv_proj.mm( |
| 60 | + input, out=cache_kv.view(-1, (self.tp_k_head_num_ + self.tp_v_head_num_) * self.head_dim_) |
| 61 | + ).view(-1, (self.tp_k_head_num_ + self.tp_v_head_num_), self.head_dim_) |
| 62 | + seq_len, _ = q.shape |
| 63 | + q = q.view(1, seq_len, -1, self.head_dim_).transpose(1, 2) |
| 64 | + k = cache_kv[:, : self.tp_k_head_num_, :].view(1, seq_len, -1, self.head_dim_).transpose(1, 2) |
| 65 | + new_q, new_k = apply_multimodal_rotary_pos_emb( |
| 66 | + q, k, infer_state.position_cos, infer_state.position_sin, self.mrope_section |
| 67 | + ) |
| 68 | + new_q = new_q.transpose(1, 2).reshape(1, seq_len, -1) |
| 69 | + cache_kv[:, : self.tp_k_head_num_, :] = new_k.squeeze(0).permute(1, 0, 2) |
| 70 | + |
| 71 | + return new_q, cache_kv |
0 commit comments