Upload folder using huggingface_hub
Browse files- modeling_tiny_mixtral.py +2 -9
modeling_tiny_mixtral.py
CHANGED
|
@@ -179,7 +179,6 @@ def apply_rotary_embeddings(x:torch.Tensor,freq_complex:torch.Tensor,device:str)
|
|
| 179 |
Returns:
|
| 180 |
torch.Tensor: The tensor after applying Rotary Position Embeddings.
|
| 181 |
"""
|
| 182 |
-
freq_complex = freq_complex.to(x.device)
|
| 183 |
x_complex=torch.view_as_complex(x.float().reshape(*x.shape[:-1],-1,2)) #N,seq_len,h,head_dim/2,2
|
| 184 |
|
| 185 |
freq_complex=freq_complex.unsqueeze(0).unsqueeze(2) # 1,seq_len,1,head_dim/2
|
|
@@ -188,7 +187,7 @@ def apply_rotary_embeddings(x:torch.Tensor,freq_complex:torch.Tensor,device:str)
|
|
| 188 |
x_out=torch.view_as_real(x_rotated) #(N,seq_len,h,head_dim/2,2)
|
| 189 |
x_out=x_out.reshape(*x.shape)
|
| 190 |
|
| 191 |
-
return x_out.type_as(x).to(
|
| 192 |
|
| 193 |
|
| 194 |
|
|
@@ -307,10 +306,6 @@ class SimpleMultiHeadAttention(nn.Module):
|
|
| 307 |
|
| 308 |
q = q_rotary.transpose(1, 2) # Back to (batch_size, num_heads, seq_len, head_dim)
|
| 309 |
k = k_rotary.transpose(1, 2) # Back to (batch_size, num_heads, seq_len, head_dim)
|
| 310 |
-
|
| 311 |
-
q = q.to(self.device)
|
| 312 |
-
k = k.to(self.device)
|
| 313 |
-
v = v.to(self.device)
|
| 314 |
|
| 315 |
# Compute attention
|
| 316 |
if self.flash:
|
|
@@ -340,7 +335,6 @@ class SimpleMultiHeadAttention(nn.Module):
|
|
| 340 |
|
| 341 |
# Reshape back to (batch_size, seq_len, dim)
|
| 342 |
y = y.transpose(1, 2).contiguous().view(batch_size, seq_len, self.dim)
|
| 343 |
-
y = y.to(self.device)
|
| 344 |
|
| 345 |
# Output projection
|
| 346 |
y = self.resid_dropout(self.c_proj(y))
|
|
@@ -519,7 +513,7 @@ class layer(nn.Module):
|
|
| 519 |
out=h+ffn_output
|
| 520 |
|
| 521 |
|
| 522 |
-
return out
|
| 523 |
|
| 524 |
|
| 525 |
class tiny_mixtral(nn.Module):
|
|
@@ -584,7 +578,6 @@ class TinyMixtralForCausalLM(PreTrainedModel, GenerationMixin):
|
|
| 584 |
self.config = config
|
| 585 |
self.post_init()
|
| 586 |
|
| 587 |
-
|
| 588 |
def forward(self, input_ids, attention_mask=None, labels=None, **kwargs):
|
| 589 |
|
| 590 |
outputs, load_balancing_loss = self.model(input_ids, start_pos=0)
|
|
|
|
| 179 |
Returns:
|
| 180 |
torch.Tensor: The tensor after applying Rotary Position Embeddings.
|
| 181 |
"""
|
|
|
|
| 182 |
x_complex=torch.view_as_complex(x.float().reshape(*x.shape[:-1],-1,2)) #N,seq_len,h,head_dim/2,2
|
| 183 |
|
| 184 |
freq_complex=freq_complex.unsqueeze(0).unsqueeze(2) # 1,seq_len,1,head_dim/2
|
|
|
|
| 187 |
x_out=torch.view_as_real(x_rotated) #(N,seq_len,h,head_dim/2,2)
|
| 188 |
x_out=x_out.reshape(*x.shape)
|
| 189 |
|
| 190 |
+
return x_out.type_as(x).to(device)
|
| 191 |
|
| 192 |
|
| 193 |
|
|
|
|
| 306 |
|
| 307 |
q = q_rotary.transpose(1, 2) # Back to (batch_size, num_heads, seq_len, head_dim)
|
| 308 |
k = k_rotary.transpose(1, 2) # Back to (batch_size, num_heads, seq_len, head_dim)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
# Compute attention
|
| 311 |
if self.flash:
|
|
|
|
| 335 |
|
| 336 |
# Reshape back to (batch_size, seq_len, dim)
|
| 337 |
y = y.transpose(1, 2).contiguous().view(batch_size, seq_len, self.dim)
|
|
|
|
| 338 |
|
| 339 |
# Output projection
|
| 340 |
y = self.resid_dropout(self.c_proj(y))
|
|
|
|
| 513 |
out=h+ffn_output
|
| 514 |
|
| 515 |
|
| 516 |
+
return out, router_loss
|
| 517 |
|
| 518 |
|
| 519 |
class tiny_mixtral(nn.Module):
|
|
|
|
| 578 |
self.config = config
|
| 579 |
self.post_init()
|
| 580 |
|
|
|
|
| 581 |
def forward(self, input_ids, attention_mask=None, labels=None, **kwargs):
|
| 582 |
|
| 583 |
outputs, load_balancing_loss = self.model(input_ids, start_pos=0)
|