Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/transformers/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4079,6 +4079,10 @@ def _fix_key(key):
return key.replace("beta", "bias")
if "gamma" in key:
return key.replace("gamma", "weight")
if "weight_g" in key:
return key.replace("weight_g", "parametrizations.weight.original0")
if "weight_v" in key:
return key.replace("weight_v", "parametrizations.weight.original1")
return key

original_loaded_keys = loaded_keys
Expand Down
12 changes: 10 additions & 2 deletions src/transformers/models/encodec/modeling_encodec.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ def __init__(

self.conv = nn.Conv1d(in_channels, out_channels, kernel_size, stride, dilation=dilation)
if self.norm_type == "weight_norm":
self.conv = nn.utils.weight_norm(self.conv)
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

self.conv = weight_norm(self.conv)
elif self.norm_type == "time_group_norm":
self.norm = nn.GroupNorm(1, out_channels)

Expand Down Expand Up @@ -187,7 +191,11 @@ def __init__(self, config, in_channels: int, out_channels: int, kernel_size: int

self.conv = nn.ConvTranspose1d(in_channels, out_channels, kernel_size, stride)
if config.norm_type == "weight_norm":
self.conv = nn.utils.weight_norm(self.conv)
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

self.conv = weight_norm(self.conv)
elif config.norm_type == "time_group_norm":
self.norm = nn.GroupNorm(1, out_channels)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1416,10 +1416,14 @@ def get_padding(self, kernel_size, dilation=1):
return (kernel_size * dilation - dilation) // 2

def apply_weight_norm(self):
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

for layer in self.convs1:
nn.utils.weight_norm(layer)
weight_norm(layer)
for layer in self.convs2:
nn.utils.weight_norm(layer)
weight_norm(layer)

def remove_weight_norm(self):
for layer in self.convs1:
Expand Down Expand Up @@ -1493,12 +1497,16 @@ def _init_weights(self, module):
module.bias.data.zero_()

def apply_weight_norm(self):
nn.utils.weight_norm(self.conv_pre)
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

weight_norm(self.conv_pre)
for layer in self.upsampler:
nn.utils.weight_norm(layer)
weight_norm(layer)
for layer in self.resblocks:
layer.apply_weight_norm()
nn.utils.weight_norm(self.conv_post)
weight_norm(self.conv_post)

def remove_weight_norm(self):
nn.utils.remove_weight_norm(self.conv_pre)
Expand Down
18 changes: 13 additions & 5 deletions src/transformers/models/seamless_m4t/modeling_seamless_m4t.py
Original file line number Diff line number Diff line change
Expand Up @@ -2361,10 +2361,14 @@ def get_padding(self, kernel_size, dilation=1):
return (kernel_size * dilation - dilation) // 2

def apply_weight_norm(self):
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

for layer in self.convs1:
nn.utils.weight_norm(layer)
weight_norm(layer)
for layer in self.convs2:
nn.utils.weight_norm(layer)
weight_norm(layer)

def remove_weight_norm(self):
for layer in self.convs1:
Expand Down Expand Up @@ -2633,12 +2637,16 @@ def _init_weights(self, module):
module.weight.data[module.padding_idx].zero_()

def apply_weight_norm(self):
nn.utils.weight_norm(self.hifi_gan.conv_pre)
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

weight_norm(self.hifi_gan.conv_pre)
for layer in self.hifi_gan.upsampler:
nn.utils.weight_norm(layer)
weight_norm(layer)
for layer in self.hifi_gan.resblocks:
layer.apply_weight_norm()
nn.utils.weight_norm(self.hifi_gan.conv_post)
weight_norm(self.hifi_gan.conv_post)

def remove_weight_norm(self):
nn.utils.remove_weight_norm(self.hifi_gan.conv_pre)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2608,10 +2608,14 @@ def get_padding(self, kernel_size, dilation=1):
return (kernel_size * dilation - dilation) // 2

def apply_weight_norm(self):
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

for layer in self.convs1:
nn.utils.weight_norm(layer)
weight_norm(layer)
for layer in self.convs2:
nn.utils.weight_norm(layer)
weight_norm(layer)

def remove_weight_norm(self):
for layer in self.convs1:
Expand Down Expand Up @@ -2889,12 +2893,16 @@ def _init_weights(self, module):

# Copied from transformers.models.seamless_m4t.modeling_seamless_m4t.SeamlessM4TCodeHifiGan.apply_weight_norm
def apply_weight_norm(self):
nn.utils.weight_norm(self.hifi_gan.conv_pre)
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

weight_norm(self.hifi_gan.conv_pre)
for layer in self.hifi_gan.upsampler:
nn.utils.weight_norm(layer)
weight_norm(layer)
for layer in self.hifi_gan.resblocks:
layer.apply_weight_norm()
nn.utils.weight_norm(self.hifi_gan.conv_post)
weight_norm(self.hifi_gan.conv_post)

# Copied from transformers.models.seamless_m4t.modeling_seamless_m4t.SeamlessM4TCodeHifiGan.remove_weight_norm
def remove_weight_norm(self):
Expand Down
8 changes: 6 additions & 2 deletions src/transformers/models/sew/modeling_sew.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,15 @@ def __init__(self, config):
stride=config.squeeze_factor,
)

weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

if is_deepspeed_zero3_enabled():
import deepspeed

with deepspeed.zero.GatheredParameters(self.conv.weight, modifier_rank=0):
self.conv = nn.utils.weight_norm(self.conv, name="weight", dim=2)
self.conv = weight_norm(self.conv, name="weight", dim=2)
if hasattr(self.conv, "parametrizations"):
weight_g = self.conv.parametrizations.weight.original0
weight_v = self.conv.parametrizations.weight.original1
Expand All @@ -288,7 +292,7 @@ def __init__(self, config):
deepspeed.zero.register_external_parameter(self, weight_v)
deepspeed.zero.register_external_parameter(self, weight_g)
else:
self.conv = nn.utils.weight_norm(self.conv, name="weight", dim=2)
self.conv = weight_norm(self.conv, name="weight", dim=2)

self.padding = SEWSamePadLayer(config.num_conv_pos_embeddings)
self.activation = ACT2FN[config.feat_extract_activation]
Expand Down
8 changes: 6 additions & 2 deletions src/transformers/models/sew_d/modeling_sew_d.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,15 @@ def __init__(self, config):
stride=config.squeeze_factor,
)

weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

if is_deepspeed_zero3_enabled():
import deepspeed

with deepspeed.zero.GatheredParameters(self.conv.weight, modifier_rank=0):
self.conv = nn.utils.weight_norm(self.conv, name="weight", dim=2)
self.conv = weight_norm(self.conv, name="weight", dim=2)
if hasattr(self.conv, "parametrizations"):
weight_g = self.conv.parametrizations.weight.original0
weight_v = self.conv.parametrizations.weight.original1
Expand All @@ -363,7 +367,7 @@ def __init__(self, config):
deepspeed.zero.register_external_parameter(self, weight_v)
deepspeed.zero.register_external_parameter(self, weight_g)
else:
self.conv = nn.utils.weight_norm(self.conv, name="weight", dim=2)
self.conv = weight_norm(self.conv, name="weight", dim=2)

self.padding = SEWDSamePadLayer(config.num_conv_pos_embeddings)
self.activation = ACT2FN[config.feat_extract_activation]
Expand Down
18 changes: 13 additions & 5 deletions src/transformers/models/speecht5/modeling_speecht5.py
Original file line number Diff line number Diff line change
Expand Up @@ -3234,10 +3234,14 @@ def get_padding(self, kernel_size, dilation=1):
return (kernel_size * dilation - dilation) // 2

def apply_weight_norm(self):
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

for layer in self.convs1:
nn.utils.weight_norm(layer)
weight_norm(layer)
for layer in self.convs2:
nn.utils.weight_norm(layer)
weight_norm(layer)

def remove_weight_norm(self):
for layer in self.convs1:
Expand Down Expand Up @@ -3310,12 +3314,16 @@ def _init_weights(self, module):
module.bias.data.zero_()

def apply_weight_norm(self):
nn.utils.weight_norm(self.conv_pre)
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

weight_norm(self.conv_pre)
for layer in self.upsampler:
nn.utils.weight_norm(layer)
weight_norm(layer)
for layer in self.resblocks:
layer.apply_weight_norm()
nn.utils.weight_norm(self.conv_post)
weight_norm(self.conv_post)

def remove_weight_norm(self):
nn.utils.remove_weight_norm(self.conv_pre)
Expand Down
38 changes: 29 additions & 9 deletions src/transformers/models/univnet/modeling_univnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ def forward(self, hidden_states: torch.FloatTensor):
return hidden_states + residual

def apply_weight_norm(self):
nn.utils.weight_norm(self.conv1)
nn.utils.weight_norm(self.conv2)
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

weight_norm(self.conv1)
weight_norm(self.conv2)

def remove_weight_norm(self):
nn.utils.remove_weight_norm(self.conv1)
Expand Down Expand Up @@ -197,11 +201,15 @@ def forward(self, spectrogram: torch.FloatTensor):
return kernels, biases

def apply_weight_norm(self):
nn.utils.weight_norm(self.input_conv)
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

weight_norm(self.input_conv)
for layer in self.resblocks:
layer.apply_weight_norm()
nn.utils.weight_norm(self.kernel_conv)
nn.utils.weight_norm(self.bias_conv)
weight_norm(self.kernel_conv)
weight_norm(self.bias_conv)

def remove_weight_norm(self):
nn.utils.remove_weight_norm(self.input_conv)
Expand Down Expand Up @@ -328,7 +336,11 @@ def location_variable_convolution(
return output_hidden_states

def apply_weight_norm(self):
nn.utils.weight_norm(self.conv)
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

weight_norm(self.conv)

def remove_weight_norm(self):
nn.utils.remove_weight_norm(self.conv)
Expand Down Expand Up @@ -398,7 +410,11 @@ def forward(self, hidden_states: torch.FloatTensor, spectrogram: torch.FloatTens
return hidden_states

def apply_weight_norm(self):
nn.utils.weight_norm(self.convt_pre)
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

weight_norm(self.convt_pre)
self.kernel_predictor.apply_weight_norm()
for layer in self.resblocks:
layer.apply_weight_norm()
Expand Down Expand Up @@ -619,10 +635,14 @@ def _init_weights(self, module):
module.bias.data.zero_()

def apply_weight_norm(self):
nn.utils.weight_norm(self.conv_pre)
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

weight_norm(self.conv_pre)
for layer in self.resblocks:
layer.apply_weight_norm()
nn.utils.weight_norm(self.conv_post)
weight_norm(self.conv_post)

def remove_weight_norm(self):
nn.utils.remove_weight_norm(self.conv_pre)
Expand Down
8 changes: 6 additions & 2 deletions src/transformers/models/vits/modeling_vits.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,14 @@ def get_padding(self, kernel_size, dilation=1):
return (kernel_size * dilation - dilation) // 2

def apply_weight_norm(self):
weight_norm = nn.utils.weight_norm
if hasattr(nn.utils.parametrizations, "weight_norm"):
weight_norm = nn.utils.parametrizations.weight_norm

for layer in self.convs1:
nn.utils.weight_norm(layer)
weight_norm(layer)
for layer in self.convs2:
nn.utils.weight_norm(layer)
weight_norm(layer)

def remove_weight_norm(self):
for layer in self.convs1:
Expand Down
1 change: 1 addition & 0 deletions tests/models/sew/test_modeling_sew.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ def test_initialization(self):
model = model_class(config=configs_no_init)
for name, param in model.named_parameters():
uniform_init_parms = [
"conv.parametrizations.weight",
"conv.weight",
"masked_spec_embed",
"quantizer.weight_proj.weight",
Expand Down