razmars commited on
Commit
1683834
·
verified ·
1 Parent(s): 42d5df0

Update modeling_super_linear.py

Browse files
Files changed (1) hide show
  1. 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
- Linear projection from a variable-length input (L) to a fixed horizon,
198
- applied *independently per channel* and wrapped with RevIN.
199
- """
200
- def __init__(self, input_len: int, output_len: int):
201
- super().__init__()
202
- self.seq_len = input_len # “design” length
203
- self.horizon = output_len
204
-
205
- # bias=False because you asked to drop the bias
206
- self.linear = nn.Linear(input_len, output_len)
207
-
208
- # your RevIN layer (must be defined elsewhere)
209
- self.revin = RevIN(num_features=None, affine=False,
210
- norm_type=None, subtract_last=False)
211
-
212
- # ------------------------------------------------------------------ helpers
213
- def _resize_weight(self, weight: torch.Tensor, new_in: int) -> torch.Tensor:
214
- """
215
- Bilinearly interpolate columns so the weight becomes (horizon, new_in).
216
- """
217
- if new_in == weight.shape[1]:
218
- return weight # nothing to do
219
- w4d = weight.unsqueeze(0).unsqueeze(0) # (1,1,out,in)
220
- w_resized = F.interpolate(
221
- w4d,
222
- size=(self.horizon, new_in), # always ≥ 0, so no crash
223
- mode="bilinear",
224
- align_corners=False
225
- )[0, 0] # back to (out,new_in)
226
- return w_resized
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
- # project each channel separately: (B,C,L) @ (L,out)ᵀ → (B,C,out)
251
- x = x.permute(0, 2, 1) # (B,C,L)
252
- x = torch.matmul(x, W.t()) # (B,C,out)
253
- x = x.permute(0, 2, 1) # (B,horizon,C)
 
 
 
 
 
 
 
 
 
 
 
254
 
255
- # ---------- RevIN denormalisation -------------------------------------
256
- x = self.revin(x, "denorm")
 
257
 
258
- if squeeze_last:
259
- x = x.squeeze(-1) # (B,horizon)
 
 
260
 
261
- return x
 
 
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):