zifei9 commited on
Commit
65687d0
·
verified ·
1 Parent(s): 8579907

Update modeling_llama.py

Browse files
Files changed (1) hide show
  1. modeling_llama.py +1 -34
modeling_llama.py CHANGED
@@ -52,6 +52,7 @@ from transformers.utils import (
52
  replace_return_docstrings,
53
  )
54
  from .configuration_llama import LlamaConfig
 
55
 
56
 
57
  logger = logging.get_logger(__name__)
@@ -174,40 +175,6 @@ class LlamaDynamicNTKScalingRotaryEmbedding(LlamaRotaryEmbedding):
174
  super().__init__(*args, **kwargs)
175
 
176
 
177
- def rotate_half(x):
178
- """Rotates half the hidden dims of the input."""
179
- x1 = x[..., : x.shape[-1] // 2]
180
- x2 = x[..., x.shape[-1] // 2 :]
181
- return torch.cat((-x2, x1), dim=-1)
182
-
183
-
184
- def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1):
185
- """Applies Rotary Position Embedding to the query and key tensors.
186
-
187
- Args:
188
- q (`torch.Tensor`): The query tensor.
189
- k (`torch.Tensor`): The key tensor.
190
- cos (`torch.Tensor`): The cosine part of the rotary embedding.
191
- sin (`torch.Tensor`): The sine part of the rotary embedding.
192
- position_ids (`torch.Tensor`, *optional*):
193
- Deprecated and unused.
194
- unsqueeze_dim (`int`, *optional*, defaults to 1):
195
- The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
196
- sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
197
- that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
198
- k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
199
- cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
200
- the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
201
- Returns:
202
- `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
203
- """
204
- cos = cos.unsqueeze(unsqueeze_dim)
205
- sin = sin.unsqueeze(unsqueeze_dim)
206
- q_embed = (q * cos) + (rotate_half(q) * sin)
207
- k_embed = (k * cos) + (rotate_half(k) * sin)
208
- return q_embed, k_embed
209
-
210
-
211
  class LlamaMLP(nn.Module):
212
  def __init__(self, config):
213
  super().__init__()
 
52
  replace_return_docstrings,
53
  )
54
  from .configuration_llama import LlamaConfig
55
+ from transformers.models.llama.modeling_llama import apply_rotary_pos_emb
56
 
57
 
58
  logger = logging.get_logger(__name__)
 
175
  super().__init__(*args, **kwargs)
176
 
177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  class LlamaMLP(nn.Module):
179
  def __init__(self, config):
180
  super().__init__()