Update modeling_super_linear.py
Browse files- modeling_super_linear.py +57 -62
modeling_super_linear.py
CHANGED
|
@@ -193,72 +193,67 @@ class NLinear(nn.Module):
|
|
| 193 |
|
| 194 |
|
| 195 |
class RLinear(nn.Module):
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
self.
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
# ------------------------------------------------------------------ forward
|
| 229 |
-
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 230 |
-
"""
|
| 231 |
-
x: (B,L,C) or (B,L) → (B,horizon,C) or (B,horizon)
|
| 232 |
-
"""
|
| 233 |
-
squeeze_last = False
|
| 234 |
-
if x.dim() == 2: # (B,L)
|
| 235 |
-
x = x.unsqueeze(-1) # (B,L,1)
|
| 236 |
-
squeeze_last = True
|
| 237 |
-
|
| 238 |
-
B, L, C = x.shape
|
| 239 |
-
|
| 240 |
-
# ---------- RevIN normalisation ---------------------------------------
|
| 241 |
-
x = self.revin(x, "norm")
|
| 242 |
-
|
| 243 |
-
if L == self.seq_len: # fast path – no resizing
|
| 244 |
-
x = self.linear(x.permute(0, 2, 1)) # (B,C,horizon)
|
| 245 |
-
x = x.permute(0, 2, 1) # (B,horizon,C)
|
| 246 |
-
|
| 247 |
-
else: # resize the weight once
|
| 248 |
-
W = self._resize_weight(self.linear.weight.detach(), L) # (out,L)
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
-
|
| 256 |
-
|
|
|
|
| 257 |
|
| 258 |
-
|
| 259 |
-
|
|
|
|
|
|
|
| 260 |
|
| 261 |
-
|
|
|
|
|
|
|
| 262 |
|
| 263 |
"-------------------------------------------------------------------------------------------------------------------"
|
| 264 |
class SparseNoisyMoE(nn.Module):
|
|
|
|
| 193 |
|
| 194 |
|
| 195 |
class RLinear(nn.Module):
|
| 196 |
+
def __init__(self, input_len, output_len):
|
| 197 |
+
super(RLinear, self).__init__()
|
| 198 |
+
self.Linear = nn.Linear(input_len, output_len)
|
| 199 |
+
self.seq_len = input_len
|
| 200 |
+
self.horizon = output_len
|
| 201 |
+
self.revin_layer = RevIN(num_features = None, affine=False, norm_type = None, subtract_last = False)
|
| 202 |
+
self.zero_shot_Linear = None
|
| 203 |
+
|
| 204 |
+
def transform_model(self,new_lookback,mode):
|
| 205 |
+
if mode == 1:
|
| 206 |
+
W = self.Linear.weight.detach()
|
| 207 |
+
new_W = W[:, -new_lookback:]
|
| 208 |
+
original_norm = torch.norm(W, p=2)
|
| 209 |
+
new_norm = torch.norm(new_W, p=2)
|
| 210 |
+
final_scaling = original_norm / new_norm if new_norm.item() != 0 else 1.0
|
| 211 |
+
new_W = new_W * final_scaling
|
| 212 |
+
|
| 213 |
+
self.zero_shot_Linear = new_W
|
| 214 |
+
else:
|
| 215 |
+
W = self.Linear.weight.detach()
|
| 216 |
+
W4d = W.unsqueeze(0).unsqueeze(0) # (1, 1, out, in)
|
| 217 |
+
|
| 218 |
+
# resize H → self.horizon and W → new_lookback
|
| 219 |
+
new_W = F.interpolate(
|
| 220 |
+
W4d,
|
| 221 |
+
size=(self.horizon, new_lookback), # (H_out, W_out)
|
| 222 |
+
mode='bilinear',
|
| 223 |
+
align_corners=False
|
| 224 |
+
)[0, 0] # drop the two singleton dims
|
| 225 |
+
|
| 226 |
+
self.zero_shot_Linear = new_W # shape (self.horizon, new_lookback)
|
| 227 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
+
def forward(self, x):
|
| 230 |
+
# x: [Batch, Input length,Channel]
|
| 231 |
+
x_shape = x.shape
|
| 232 |
+
if x.shape[1] < self.seq_len:
|
| 233 |
+
#if self.zero_shot_Linear is None:
|
| 234 |
+
#print(F"new Lookkback : {x.shape[1]}")
|
| 235 |
+
|
| 236 |
+
self.transform_model(x.shape[1],3)
|
| 237 |
+
x = x.clone()
|
| 238 |
+
#x = x * (x.shape[1]/512)
|
| 239 |
+
x = self.revin_layer(x, 'norm')
|
| 240 |
+
x = F.linear(x, self.zero_shot_Linear)
|
| 241 |
+
x = self.revin_layer(x, 'denorm')
|
| 242 |
+
#x = x * (512/x.shape[1])
|
| 243 |
+
return x
|
| 244 |
|
| 245 |
+
|
| 246 |
+
if len(x_shape) == 2:
|
| 247 |
+
x = x.unsqueeze(-1)
|
| 248 |
|
| 249 |
+
x = x.clone()
|
| 250 |
+
x = self.revin_layer(x, 'norm')
|
| 251 |
+
x = self.Linear(x.permute(0,2,1)).permute(0,2,1).clone()
|
| 252 |
+
x = self.revin_layer(x, 'denorm')
|
| 253 |
|
| 254 |
+
if len(x_shape) == 2:
|
| 255 |
+
x = x.squeeze(-1)
|
| 256 |
+
return x # to [Batch, Output length, Channel]
|
| 257 |
|
| 258 |
"-------------------------------------------------------------------------------------------------------------------"
|
| 259 |
class SparseNoisyMoE(nn.Module):
|