Update modeling_super_linear.py
Browse files3 unmodified lines456789552 unmodified lines56256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960020 unmodified lines62162262362462562662715 unmodified lines64364464564664764864915 unmodified lines6656666673 unmodified linesfrom transformers import (PreTrainedModel,GenerationMixin,AutoConfig,AutoModelForCausalLM,)from transformers.modeling_outputs import CausalLMOutputWithCrossAttentionsfrom .configuration_super_linear import SuperLinearConfigimport datetime552 unmodified linesself.revin_layer = RevIN(num_features = None, affine=False, norm_type = None, subtract_last = False)self.post_init()# ------------------------------------------------------------------# Forward pass expected by AutoModelForCausalLM# ------------------------------------------------------------------def upsample_dim1(self, x, target_len: int = 512, mode: str = "bicubic"):# -------- bring the dim-1 axis to the PyTorch 1-D “length” position --------orig_shape = x.shapendim = x.ndim# Reshape to (N, C, L) where L is the axis we want to scaleif ndim == 1: # (L,)x_ = x.unsqueeze(0).unsqueeze(0) # (1,1,L)unstack = lambda t: t.squeeze(0).squeeze(0)elif ndim == 2: # (L,C) or (C,L)if orig_shape[0] == 48: # assume (L,C)x_ = x.permute(1, 0).unsqueeze(0) # (1,C,L)unstack = lambda t: t.squeeze(0).permute(1, 0)else: # assume (C,L)x_ = x.unsqueeze(0) # (1,C,L)unstack = lambda t: t.squeeze(0)else: # ≥3 dims, assume (B,L,C, …) with L at dim-1x_ = x.transpose(1, 2) # (B,C,L,...)new_order = list(range(ndim))new_order[1], new_order[2] = 2, 1 # swap back laterunstack = lambda t: t.permute(*new_order)# ------------------ actual interpolation in length dimension --------------y = F.interpolate(x_, size=target_len, mode=mode, align_corners=False)# ------------------ restore original dimension ordering -------------------return unstack(y)def fourier_interp_dim1(self,x, target_len: int = 512):L = x.size(1)20 unmodified linesreturn y15 unmodified linesif x_enc.shape[1] < 512:#x_enc = self.revin_layer(x_enc, 'norm')#x_enc = self.fourier_interp_dim1(x_enc)pass15 unmodified linesdef _reorder_cache(self, past, beam_idx, **kwargs):return past # backbone keeps no KV cache3 unmodified lines45678910552 unmodified lines56356456556656756856957020 unmodified lines59159259359459559659759859960060160215 unmodified lines61861962062162262362415 unmodified lines6406416426433 unmodified linesfrom transformers import (PreTrainedModel,GenerationMixin,AutoConfig,AutoModelForCausalLM,)from transformers.modeling_outputs import CausalLMOutputWithCrossAttentionsfrom .configuration_super_linear import SuperLinearConfigfrom torch.nn.functional import interpolateimport datetime552 unmodified linesself.revin_layer = RevIN(num_features = None, affine=False, norm_type = None, subtract_last = False)self.post_init()def fourier_interp_dim1(self,x, target_len: int = 512):L = x.size(1)20 unmodified linesreturn ydef upsample_interpolate(self,x, target_len: int = 512):scale_factor = 512/x.shape[1]upsample = interpolate(x, scale_factor=scale_factor, mode='linear').permute(0,2,1)[:, -500:, :]print(upsample.shape)15 unmodified linesif x_enc.shape[1] < 512:#x_enc = self.revin_layer(x_enc, 'norm')x_enc = self.upsample_interpolate(x_enc)pass15 unmodified linesdef _reorder_cache(self, past, beam_idx, **kwargs):return past # backbone keeps no KV cache
modeling_super_linear.py+9-33