Update modeling_super_linear.py
Browse files192 unmodified lines193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274192 unmodified linesclass RLinear(nn.Module):"""Resizable linear projection from variable input length L to fixed horizon.Each channel is projected independently (no mixing across channels)."""def __init__(self, input_len: int, output_len: int):super().__init__()self.seq_len = input_len # βdesignβ lengthself.horizon = output_len# plain weight + bias β we will *interpolate* weight when L β seq_lenself.linear = nn.Linear(input_len, output_len, bias=True)# RevIN β your own implementation; keep it stateless for safetyself.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 thatweight.shape 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 interpolatew4d = weight.unsqueeze(0).unsqueeze(0)w_resized = F.interpolate(w4d, size=(self.horizon, new_in), mode="bilinear",align_corners=False)[0, 0] # back to (out, new_in)return w_resizeddef forward(self, x: torch.Tensor) -> torch.Tensor:"""x: (B, L, C) or (B, L) β (B, horizon, C) or (B, horizon)"""# make sure x is 3-Dsqueeze_last_dim = Falseif x.dim() == 2: # (B,L)x = x.unsqueeze(-1) # (B,L,1)squeeze_last_dim = TrueB, 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 independentlyx = x.permute(0, 2, 1) # (B,C,L)x = self.linear(x) # (B,C,horizon)x = x.permute(0, 2, 1) # (B,horizon,C)else:# slow path β resize the weight to match L# freeze current weight & biasW = self.linear.weight.detach() # (out,in)b = self.linear.bias.detach() # (out)W_resized = self._resize_weight(W, L) # (out,L)# apply per channel# x: (B,L,C) β (B,C,L) so that last dim is Lx = x.permute(0, 2, 1)# out = x @ W_resized.T + bout = torch.einsum("bcl,ol->bco", x, W_resized) + bx = out.permute(0, 2, 1) # back to (B,horizon,C)# RevIN denormx = self.revin(x, "denorm")if squeeze_last_dim:x = x.squeeze(-1) # (B,horizon)return x"-------------------------------------------------------------------------------------------------------------------"class SparseNoisyMoE(nn.Module):def __init__(self, configs, experts=None):192 unmodified lines193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253192 unmodified linesclass RLinear(nn.Module):"""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_lenself.horizon = output_len# β bias removed β bias=Falseself.linear = nn.Linear(input_len, output_len)self.revin = RevIN(num_features=None, affine=False,norm_type=None, subtract_last=False)@staticmethoddef _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 weightw4d = weight.unsqueeze(0).unsqueeze(0) # (1,1,out,in)w_resized = F.interpolate(w4d, size=(horizon, new_in), mode="bilinear",align_corners=False)[0, 0] # (out,new_in)return w_resizeddef forward(self, x: torch.Tensor) -> torch.Tensor:"""x: (B,L,C) or (B,L) β (B,horizon,C) or (B,horizon)"""squeeze_last = Falseif x.dim() == 2: # (B,L)x = x.unsqueeze(-1) # (B,L,1)squeeze_last = TrueB, L, C = x.shapex = self.revin(x, "norm")if L == self.seq_len: # fast pathx = self.linear(x.permute(0, 2, 1)) # (B,C,horizon)x = x.permute(0, 2, 1) # (B,horizon,C)else: # resize pathW = self.linear.weight.detach() # (out,in)W_resized = self._resize_weight(W, L, self.horizon)# β 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)x = self.revin(x, "denorm")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