Skip to content

Commit de1b2aa

Browse files
committed
feat: default to old PyTorch weight_norm for RVC fork compat + fix FAISS AVX2
- Add arvc/engine/models/weight_norm.py wrapper module: - Default: torch.nn.utils.weight_norm (old-style, .weight_g/.weight_v) compatible with virtually all RVC forks - New mode: torch.nn.utils.parametrizations.weight_norm (PyTorch 2.0+) Enable by setting new_pytorch_weight_norm: true in config.json - Update 20+ model files to use weight_norm wrapper instead of direct torch.nn.utils.parametrizations imports - Make checkpoint key conversion conditional: - Old mode: no conversion needed (model uses .weight_g/.weight_v directly) - New mode: convert between old checkpoint format and new parametrizations format on load/save (same behavior as before) - Fix ModuleNotFoundError for faiss.swigfaiss_avx2: Add fallback import in models/utils.py and create_index.py that catches the AVX2 module error and falls back to non-AVX2 faiss
1 parent bd94b41 commit de1b2aa

30 files changed

Lines changed: 256 additions & 96 deletions

arvc/configs/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,5 +614,6 @@
614614
"pretrained_v2_url": "https://huggingface.co/buckets/R-Kentaren/Ultimate-RVC-Models/resolve/pretrained_v2/",
615615
"pretrained_v1_fallback_url": "https://huggingface.co/buckets/R-Kentaren/Ultimate-RVC-Models/resolve/pretrained_v1/",
616616
"pretrained_v2_fallback_url": "https://huggingface.co/buckets/R-Kentaren/Ultimate-RVC-Models/resolve/pretrained_v2/",
617-
"search_api_url": "https://voice-models.com/fetch_data.php"
617+
"search_api_url": "https://voice-models.com/fetch_data.php",
618+
"new_pytorch_weight_norm": false
618619
}

arvc/engine/models/algorithms/discriminators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import torch.nn.functional as F
33

44
from torch.utils.checkpoint import checkpoint
5-
from torch.nn.utils.parametrizations import spectral_norm, weight_norm
5+
from torch.nn.utils.parametrizations import spectral_norm
6+
from arvc.engine.models.weight_norm import weight_norm
67

78
from arvc.engine.models.algorithms.commons import get_padding
89
from arvc.engine.models.algorithms.residuals import LRELU_SLOPE

arvc/engine/models/algorithms/generators/pcph_gan.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
from torch import Tensor
88
from torch.nn import Conv1d, ConvTranspose1d
99
import torch.nn.utils.parametrize as parametrize
10-
from torch.nn.utils import remove_weight_norm
11-
from torch.nn.utils.parametrizations import weight_norm
10+
from arvc.engine.models.weight_norm import weight_norm, remove_weight_norm
1211
from torch.amp import autocast
1312
from torch.utils.checkpoint import checkpoint
1413

arvc/engine/models/algorithms/generators/ringformer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import torch.nn as nn
55
import torch.nn.functional as F
66
import torch.nn.utils.parametrize as parametrize
7-
from torch.nn.utils import remove_weight_norm
8-
from torch.nn.utils.parametrizations import weight_norm
7+
from arvc.engine.models.weight_norm import weight_norm, remove_weight_norm
98
from torch.nn import Conv1d, ConvTranspose1d
109
from torch.amp import autocast
1110
from torch.utils.checkpoint import checkpoint

arvc/engine/models/algorithms/modules.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import torch.nn.utils.parametrize as parametrize
44

5+
from arvc.engine.models.weight_norm import weight_norm, remove_weight_norm
56
from .commons import fused_add_tanh_sigmoid_multiply
67

78
class WaveNet(torch.nn.Module):
@@ -28,7 +29,7 @@ def __init__(
2829
self.drop = torch.nn.Dropout(p_dropout)
2930

3031
if gin_channels:
31-
self.cond_layer = torch.nn.utils.parametrizations.weight_norm(
32+
self.cond_layer = weight_norm(
3233
torch.nn.Conv1d(
3334
gin_channels,
3435
2 * hidden_channels * n_layers,
@@ -42,7 +43,7 @@ def __init__(
4243

4344
for i in range(n_layers):
4445
self.in_layers.append(
45-
torch.nn.utils.parametrizations.weight_norm(
46+
weight_norm(
4647
torch.nn.Conv1d(
4748
hidden_channels,
4849
2 * hidden_channels,
@@ -57,7 +58,7 @@ def __init__(
5758
res_skip_channels = (hidden_channels if i == n_layers - 1 else 2 * hidden_channels)
5859

5960
self.res_skip_layers.append(
60-
torch.nn.utils.parametrizations.weight_norm(
61+
weight_norm(
6162
torch.nn.Conv1d(
6263
hidden_channels,
6364
res_skip_channels,
@@ -86,12 +87,12 @@ def forward(self, x, x_mask, g=None):
8687
def remove_weight_norm(self):
8788
if self.gin_channels != 0:
8889
if hasattr(self.cond_layer, "parametrizations") and "weight" in self.cond_layer.parametrizations: parametrize.remove_parametrizations(self.cond_layer, "weight", leave_parametrized=True)
89-
else: torch.nn.utils.remove_weight_norm(self.cond_layer)
90+
else: remove_weight_norm(self.cond_layer)
9091

9192
for l in self.in_layers:
9293
if hasattr(l, "parametrizations") and "weight" in l.parametrizations: parametrize.remove_parametrizations(l, "weight", leave_parametrized=True)
93-
else: torch.nn.utils.remove_weight_norm(l)
94+
else: remove_weight_norm(l)
9495

9596
for l in self.res_skip_layers:
9697
if hasattr(l, "parametrizations") and "weight" in l.parametrizations: parametrize.remove_parametrizations(l, "weight", leave_parametrized=True)
97-
else: torch.nn.utils.remove_weight_norm(l)
98+
else: remove_weight_norm(l)

arvc/engine/models/algorithms/normalization.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import torch.nn.functional as F
77

88
import torch.nn.utils.parametrize as parametrize
9-
from torch.nn.utils import remove_weight_norm
10-
from torch.nn.utils.parametrizations import weight_norm
9+
from arvc.engine.models.weight_norm import weight_norm, remove_weight_norm
1110

1211

1312
class LayerNorm(torch.nn.Module):

arvc/engine/models/algorithms/normalizing_flows.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import torch.nn as nn
66

77
import torch.nn.utils.parametrize as parametrize
8-
from torch.nn.utils import remove_weight_norm
9-
from torch.nn.utils.parametrizations import weight_norm
8+
from arvc.engine.models.weight_norm import weight_norm, remove_weight_norm
109

1110
from arvc.engine.models.algorithms.modules import WaveNet
1211
from arvc.engine.models.algorithms.encoders import Encoder

arvc/engine/models/algorithms/residuals.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import torch.nn.utils.parametrize as parametrize
44

55
from itertools import chain
6-
from torch.nn.utils import remove_weight_norm
7-
from torch.nn.utils.parametrizations import weight_norm
6+
from arvc.engine.models.weight_norm import weight_norm, remove_weight_norm
87

98
from .modules import WaveNet
109
from .commons import get_padding, init_weights

arvc/engine/models/algorithms/wavenet.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import torch.nn.utils.parametrize as parametrize
44

5+
from arvc.engine.models.weight_norm import weight_norm, remove_weight_norm
56
from .commons import fused_add_tanh_sigmoid_multiply
67

78
class WaveNet(torch.nn.Module):
@@ -28,7 +29,7 @@ def __init__(
2829
self.drop = torch.nn.Dropout(p_dropout)
2930

3031
if gin_channels:
31-
self.cond_layer = torch.nn.utils.parametrizations.weight_norm(
32+
self.cond_layer = weight_norm(
3233
torch.nn.Conv1d(
3334
gin_channels,
3435
2 * hidden_channels * n_layers,
@@ -42,7 +43,7 @@ def __init__(
4243

4344
for i in range(n_layers):
4445
self.in_layers.append(
45-
torch.nn.utils.parametrizations.weight_norm(
46+
weight_norm(
4647
torch.nn.Conv1d(
4748
hidden_channels,
4849
2 * hidden_channels,
@@ -57,7 +58,7 @@ def __init__(
5758
res_skip_channels = (hidden_channels if i == n_layers - 1 else 2 * hidden_channels)
5859

5960
self.res_skip_layers.append(
60-
torch.nn.utils.parametrizations.weight_norm(
61+
weight_norm(
6162
torch.nn.Conv1d(
6263
hidden_channels,
6364
res_skip_channels,
@@ -86,12 +87,12 @@ def forward(self, x, x_mask, g=None):
8687
def remove_weight_norm(self):
8788
if self.gin_channels != 0:
8889
if hasattr(self.cond_layer, "parametrizations") and "weight" in self.cond_layer.parametrizations: parametrize.remove_parametrizations(self.cond_layer, "weight", leave_parametrized=True)
89-
else: torch.nn.utils.remove_weight_norm(self.cond_layer)
90+
else: remove_weight_norm(self.cond_layer)
9091

9192
for l in self.in_layers:
9293
if hasattr(l, "parametrizations") and "weight" in l.parametrizations: parametrize.remove_parametrizations(l, "weight", leave_parametrized=True)
93-
else: torch.nn.utils.remove_weight_norm(l)
94+
else: remove_weight_norm(l)
9495

9596
for l in self.res_skip_layers:
9697
if hasattr(l, "parametrizations") and "weight" in l.parametrizations: parametrize.remove_parametrizations(l, "weight", leave_parametrized=True)
97-
else: torch.nn.utils.remove_weight_norm(l)
98+
else: remove_weight_norm(l)

arvc/engine/models/embedders/fairseq.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import uuid
55
import torch
66
import types
7+
8+
from arvc.engine.models.weight_norm import weight_norm as _weight_norm, remove_weight_norm as _remove_weight_norm
79
import contextlib
810

911
import numpy as np
@@ -558,7 +560,7 @@ def make_conv_pos(e, k, g):
558560
dropout = 0
559561
nn.init.normal_(pos_conv.weight, mean=0, std=math.sqrt((4 * (1.0 - dropout)) / (k * e)))
560562
nn.init.constant_(pos_conv.bias, 0)
561-
return nn.Sequential(nn.utils.parametrizations.weight_norm(pos_conv, name="weight", dim=2), SamePad(k), nn.GELU())
563+
return nn.Sequential(_weight_norm(pos_conv, name="weight", dim=2), SamePad(k), nn.GELU())
562564

563565
def is_xla_tensor(tensor):
564566
return torch.is_tensor(tensor) and tensor.device.type == "xla"
@@ -1183,7 +1185,7 @@ def make_generation_fast_(self, **kwargs):
11831185

11841186
def apply_remove_weight_norm(module):
11851187
try:
1186-
nn.utils.remove_weight_norm(module)
1188+
_remove_weight_norm(module)
11871189
except (AttributeError, ValueError):
11881190
return
11891191

0 commit comments

Comments
 (0)