PRIYANSHUDHAKED commited on
Commit
fd3bc53
·
verified ·
1 Parent(s): 9c9409d

Delete src/miocodec/pupu/modules/vocoder_blocks/norm2d.py with huggingface_hub

Browse files
src/miocodec/pupu/modules/vocoder_blocks/norm2d.py DELETED
@@ -1,84 +0,0 @@
1
- import torch.nn as nn
2
- import einops
3
- from torch.nn.utils import spectral_norm, weight_norm
4
-
5
- CONV_NORMALIZATIONS = frozenset(
6
- [
7
- "none",
8
- "weight_norm",
9
- "spectral_norm",
10
- "time_layer_norm",
11
- "layer_norm",
12
- "time_group_norm",
13
- ]
14
- )
15
-
16
-
17
- class ConvLayerNorm(nn.LayerNorm):
18
- """
19
- Convolution-friendly LayerNorm that moves channels to last dimensions
20
- before running the normalization and moves them back to original position right after.
21
- """
22
-
23
- def __init__(self, normalized_shape, **kwargs):
24
- super().__init__(normalized_shape, **kwargs)
25
-
26
- def forward(self, x):
27
- x = einops.rearrange(x, "b ... t -> b t ...")
28
- x = super().forward(x)
29
- x = einops.rearrange(x, "b t ... -> b ... t")
30
- return
31
-
32
-
33
- def apply_parametrization_norm(module: nn.Module, norm: str = "none") -> nn.Module:
34
- assert norm in CONV_NORMALIZATIONS
35
- if norm == "weight_norm":
36
- return weight_norm(module)
37
- elif norm == "spectral_norm":
38
- return spectral_norm(module)
39
- else:
40
- # We already check was in CONV_NORMALIZATION, so any other choice
41
- # doesn't need reparametrization.
42
- return module
43
-
44
-
45
- def get_norm_module(
46
- module: nn.Module, causal: bool = False, norm: str = "none", **norm_kwargs
47
- ) -> nn.Module:
48
- """Return the proper normalization module. If causal is True, this will ensure the returned
49
- module is causal, or return an error if the normalization doesn't support causal evaluation.
50
- """
51
- assert norm in CONV_NORMALIZATIONS
52
- if norm == "layer_norm":
53
- assert isinstance(module, nn.modules.conv._ConvNd)
54
- return ConvLayerNorm(module.out_channels, **norm_kwargs)
55
- elif norm == "time_group_norm":
56
- if causal:
57
- raise ValueError("GroupNorm doesn't support causal evaluation.")
58
- assert isinstance(module, nn.modules.conv._ConvNd)
59
- return nn.GroupNorm(1, module.out_channels, **norm_kwargs)
60
- else:
61
- return nn.Identity()
62
-
63
-
64
- class NormConv2d(nn.Module):
65
- """Wrapper around Conv2d and normalization applied to this conv
66
- to provide a uniform interface across normalization approaches.
67
- """
68
-
69
- def __init__(
70
- self,
71
- *args,
72
- norm: str = "none",
73
- norm_kwargs={},
74
- **kwargs,
75
- ):
76
- super().__init__()
77
- self.conv = apply_parametrization_norm(nn.Conv2d(*args, **kwargs), norm)
78
- self.norm = get_norm_module(self.conv, causal=False, norm=norm, **norm_kwargs)
79
- self.norm_type = norm
80
-
81
- def forward(self, x):
82
- x = self.conv(x)
83
- x = self.norm(x)
84
- return x