rotary forward
Browse files- modeling_myolmoe.py +15 -0
modeling_myolmoe.py
CHANGED
|
@@ -215,6 +215,21 @@ class OlmoeRotaryEmbedding(nn.Module):
|
|
| 215 |
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
| 216 |
self.original_inv_freq = self.inv_freq
|
| 217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
class MyOLMoESparseMoeBlock(nn.Module):
|
| 219 |
"""Modified sparse MoE block with custom routing."""
|
| 220 |
|
|
|
|
| 215 |
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
| 216 |
self.original_inv_freq = self.inv_freq
|
| 217 |
|
| 218 |
+
@torch.no_grad()
|
| 219 |
+
@dynamic_rope_update # power user: used with advanced RoPE types (e.g. dynamic rope)
|
| 220 |
+
def forward(self, x, position_ids):
|
| 221 |
+
inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1).to(x.device)
|
| 222 |
+
position_ids_expanded = position_ids[:, None, :].float()
|
| 223 |
+
|
| 224 |
+
device_type = x.device.type if isinstance(x.device.type, str) and x.device.type != "mps" else "cpu"
|
| 225 |
+
with torch.autocast(device_type=device_type, enabled=False): # Force float32
|
| 226 |
+
freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)
|
| 227 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
| 228 |
+
cos = emb.cos() * self.attention_scaling
|
| 229 |
+
sin = emb.sin() * self.attention_scaling
|
| 230 |
+
|
| 231 |
+
return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)
|
| 232 |
+
|
| 233 |
class MyOLMoESparseMoeBlock(nn.Module):
|
| 234 |
"""Modified sparse MoE block with custom routing."""
|
| 235 |
|