Update modeling_super_linear.py
Browse files192 unmodified lines193194195196197196197198199200201202203204201202203204205206206207208209210211212213214210211212213216217218219214215216221217218223219220221222223228224225230231232233234226227228229230231237238232233241242243244245246234235236237238239240248249250251252241242243244245254255256257258259260261262263246265266267247248268249250271251252253192 unmodified lines class RLinear(nn.Module): """ Resizable linear projection from variable input length L to fixed horizon. Each channel is projected independently (no mixing across channels). Resizable linear projection from variable input length L to fixed horizon, applied independently to every channel, **without bias**. """ def __init__(self, input_len: int, output_len: int): super().__init__() self.seq_len = input_len # “design” length self.horizon = output_len # plain weight + bias – we will *interpolate* weight when L ≠ seq_len self.linear = nn.Linear(input_len, output_len, bias=True) self.seq_len = input_len self.horizon = output_len
# ★ bias removed → bias=False self.linear = nn.Linear(input_len, output_len)
# RevIN — your own implementation; keep it stateless for safety self.revin = RevIN(num_features=None, affine=False, norm_type=None, subtract_last=False)
def _resize_weight(self, weight: torch.Tensor, new_in: int) -> torch.Tensor: """ Bilinearly interpolate the *columns* of `weight` so that weight.shape becomes (horizon, new_in). """ @staticmethod def _resize_weight(weight: torch.Tensor, new_in: int, horizon: int) -> torch.Tensor: """Interpolate columns so weight becomes (horizon, new_in).""" if new_in == weight.shape[1]: return weight # no resizing needed
# weight: (out, in) → (1,1,out,in) so we can use interpolate w4d = weight.unsqueeze(0).unsqueeze(0) return weight w4d = weight.unsqueeze(0).unsqueeze(0) # (1,1,out,in) w_resized = F.interpolate( w4d, size=(self.horizon, new_in), mode="bilinear", w4d, size=(horizon, new_in), mode="bilinear", align_corners=False )[0, 0] # back to (out, new_in) )[0, 0] # (out,new_in) return w_resized
def forward(self, x: torch.Tensor) -> torch.Tensor: """ x: (B, L, C) or (B, L) ⇒ (B, horizon, C) or (B, horizon) x: (B,L,C) or (B,L) ➜ (B,horizon,C) or (B,horizon) """ # make sure x is 3-D squeeze_last_dim = False if x.dim() == 2: # (B,L) x = x.unsqueeze(-1) # (B,L,1) squeeze_last_dim = True squeeze_last = False if x.dim() == 2: # (B,L) x = x.unsqueeze(-1) # (B,L,1) squeeze_last = True
B, L, C = x.shape
# RevIN normalisation (over time dimension) x = self.revin(x, "norm")
if L == self.seq_len: # fast path — built-in linear works # reshape so that each channel is treated independently x = x.permute(0, 2, 1) # (B,C,L) x = self.linear(x) # (B,C,horizon) x = x.permute(0, 2, 1) # (B,horizon,C) if L == self.seq_len: # fast path x = self.linear(x.permute(0, 2, 1)) # (B,C,horizon) x = x.permute(0, 2, 1) # (B,horizon,C) else: # resize path W = self.linear.weight.detach() # (out,in) W_resized = self._resize_weight(W, L, self.horizon)
else: # slow path — resize the weight to match L # freeze current weight & bias W = self.linear.weight.detach() # (out,in) b = self.linear.bias.detach() # (out) # ★ bias removed → no "+ b" x = x.permute(0, 2, 1) # (B,C,L) x = torch.einsum("bcl,ol->bco", x, W_resized) # (B,C,out) x = x.permute(0, 2, 1) # (B,horizon,C)
W_resized = self._resize_weight(W, L) # (out,L)
# apply per channel # x: (B,L,C) → (B,C,L) so that last dim is L x = x.permute(0, 2, 1) # out = x @ W_resized.T + b out = torch.einsum("bcl,ol->bco", x, W_resized) + b x = out.permute(0, 2, 1) # back to (B,horizon,C)
# RevIN denorm x = self.revin(x, "denorm")
if squeeze_last_dim: x = x.squeeze(-1) # (B,horizon) if squeeze_last: x = x.squeeze(-1)
return x
"-------------------------------------------------------------------------------------------------------------------"class SparseNoisyMoE(nn.Module): def __init__(self, configs, experts=None):modeling_super_linear.py+31-52