Upload model.py
Browse files- code/model.py +4 -3
code/model.py
CHANGED
|
@@ -2,6 +2,7 @@ import math
|
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
| 4 |
import torch.nn.functional as F
|
|
|
|
| 5 |
|
| 6 |
class RMSNorm(nn.Module):
|
| 7 |
def __init__(self, dim, eps=1e-5):
|
|
@@ -33,7 +34,7 @@ class ResonanceLayerKaggle(nn.Module):
|
|
| 33 |
phase = torch.tanh(self.to_phase(h)) * math.pi
|
| 34 |
|
| 35 |
# Distance - causal ke liye
|
| 36 |
-
pos = torch.arange(T, device=x.device
|
| 37 |
chunk = 512 # safe for Kaggle T4
|
| 38 |
out = torch.zeros(B, T, C, device=x.device, dtype=x.dtype)
|
| 39 |
|
|
@@ -49,7 +50,7 @@ class ResonanceLayerKaggle(nn.Module):
|
|
| 49 |
angle = (phase_i - phase_j) + freq_i * dist * 0.05
|
| 50 |
score = torch.cos(angle) / math.sqrt(self.head_dim) # Normalize to prevent explosion
|
| 51 |
|
| 52 |
-
causal_mask = (torch.arange(i, end, device=x.device)[:,None] >= pos[None,:]).
|
| 53 |
score = score * causal_mask
|
| 54 |
|
| 55 |
amp = A.norm(dim=-1).permute(0,2,1) # B,H,T
|
|
@@ -86,7 +87,7 @@ class ViuResonance100M(nn.Module):
|
|
| 86 |
def forward(self, idx, targets=None):
|
| 87 |
x = self.emb(idx)
|
| 88 |
for layer in self.layers:
|
| 89 |
-
x = x + self.resid_dropout(
|
| 90 |
logits = self.head(self.norm(x))
|
| 91 |
loss = None
|
| 92 |
if targets is not None:
|
|
|
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
| 4 |
import torch.nn.functional as F
|
| 5 |
+
from torch.utils.checkpoint import checkpoint
|
| 6 |
|
| 7 |
class RMSNorm(nn.Module):
|
| 8 |
def __init__(self, dim, eps=1e-5):
|
|
|
|
| 34 |
phase = torch.tanh(self.to_phase(h)) * math.pi
|
| 35 |
|
| 36 |
# Distance - causal ke liye
|
| 37 |
+
pos = torch.arange(T, device=x.device, dtype=x.dtype)
|
| 38 |
chunk = 512 # safe for Kaggle T4
|
| 39 |
out = torch.zeros(B, T, C, device=x.device, dtype=x.dtype)
|
| 40 |
|
|
|
|
| 50 |
angle = (phase_i - phase_j) + freq_i * dist * 0.05
|
| 51 |
score = torch.cos(angle) / math.sqrt(self.head_dim) # Normalize to prevent explosion
|
| 52 |
|
| 53 |
+
causal_mask = (torch.arange(i, end, device=x.device)[:,None] >= pos[None,:]).to(x.dtype).view(1,1,end-i,T)
|
| 54 |
score = score * causal_mask
|
| 55 |
|
| 56 |
amp = A.norm(dim=-1).permute(0,2,1) # B,H,T
|
|
|
|
| 87 |
def forward(self, idx, targets=None):
|
| 88 |
x = self.emb(idx)
|
| 89 |
for layer in self.layers:
|
| 90 |
+
x = x + self.resid_dropout(checkpoint(layer, x, use_reentrant=False))
|
| 91 |
logits = self.head(self.norm(x))
|
| 92 |
loss = None
|
| 93 |
if targets is not None:
|