File size: 9,982 Bytes
574a515 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
import sys
import torch
from torch import nn
import numpy as np
from vq.module import WNConv1d, EncoderBlock, ResLSTM
from vq.alias_free_torch import *
from vq import activations
from vq.bs_roformer5 import TransformerBlock
from torchtune.modules import RotaryPositionalEmbeddings
import vq.blocks as blocks
from torch.nn import utils
def init_weights(m):
if isinstance(m, nn.Conv1d):
nn.init.trunc_normal_(m.weight, std=0.02)
nn.init.constant_(m.bias, 0)
class CodecEncoder(nn.Module):
def __init__(self,
ngf=48,
use_rnn=True,
rnn_bidirectional=False,
rnn_num_layers=2,
up_ratios=(2, 2, 4, 4, 5),
dilations=(1, 3, 9),
out_channels=1024):
super().__init__()
self.hop_length = np.prod(up_ratios)
self.ngf = ngf
self.up_ratios = up_ratios
# Create first convolution
d_model = ngf
self.block = [WNConv1d(1, d_model, kernel_size=7, padding=3)]
# Create EncoderBlocks that double channels as they downsample by `stride`
for i, stride in enumerate(up_ratios):
d_model *= 2
self.block += [EncoderBlock(d_model, stride=stride, dilations=dilations)]
# RNN
if use_rnn:
self.block += [
ResLSTM(d_model,
num_layers=rnn_num_layers,
bidirectional=rnn_bidirectional
)
]
# Create last convolution
self.block += [
Activation1d(activation=activations.SnakeBeta(d_model, alpha_logscale=True)),
WNConv1d(d_model, out_channels, kernel_size=3, padding=1),
]
# Wrap black into nn.Sequential
self.block = nn.Sequential(*self.block)
self.enc_dim = d_model
self.reset_parameters()
def forward(self, x):
out = self.block(x)
return out
def inference(self, x):
return self.block(x)
def remove_weight_norm(self):
"""Remove weight normalization module from all of the layers."""
def _remove_weight_norm(m):
try:
torch.nn.utils.remove_weight_norm(m)
except ValueError: # this module didn't have weight norm
return
self.apply(_remove_weight_norm)
def apply_weight_norm(self):
"""Apply weight normalization module from all of the layers."""
def _apply_weight_norm(m):
if isinstance(m, nn.Conv1d):
torch.nn.utils.weight_norm(m)
self.apply(_apply_weight_norm)
def reset_parameters(self):
self.apply(init_weights)
class Transpose(nn.Module):
def __init__(self, dim1, dim2):
super(Transpose, self).__init__()
self.dim1 = dim1
self.dim2 = dim2
def forward(self, x):
return x.transpose(self.dim1, self.dim2)
class CodecEncoder_Transformer(nn.Module):
def __init__(self,
ngf=48,
up_ratios=[2, 2, 4, 4, 5],
dilations=(1, 3, 9),
hidden_dim=1024,
depth=12,
heads=12,
pos_meb_dim=64,
):
super().__init__()
self.hop_length = np.prod(up_ratios)
self.ngf =ngf
self.up_ratios = up_ratios
d_model = ngf
self.conv_blocks = [WNConv1d(1, d_model, kernel_size=7, padding=3)]
for i, stride in enumerate(up_ratios):
d_model *= 2
self.conv_blocks += [EncoderBlock(d_model, stride=stride, dilations=dilations)]
self.conv_blocks = nn.Sequential(*self.conv_blocks)
# time_rotary_embed = RotaryPositionalEmbeddings(dim=pos_meb_dim)
# transformer_blocks = [
# TransformerBlock(dim=hidden_dim, n_heads=heads, rotary_embed=time_rotary_embed)
# for _ in range(depth)
# ]
# self.transformers = nn.Sequential(*transformer_blocks)
# self.final_layer_norm = nn.LayerNorm(hidden_dim, eps=1e-6)
self.conv_final_block = [
Activation1d(activation=activations.SnakeBeta(d_model, alpha_logscale=True)),
WNConv1d(d_model, hidden_dim, kernel_size=3, padding=1),
]
self.conv_final_block = nn.Sequential(*self.conv_final_block)
self.reset_parameters()
def forward(self, x):
x = self.conv_blocks(x)
# x = x.permute(0, 2, 1)
# x= self.transformers(x)
# x = self.final_layer_norm(x)
# x = x.permute(0, 2, 1)
x = self.conv_final_block (x)
x = x.permute(0, 2, 1)
return x
def inference(self, x):
return self.block(x)
def remove_weight_norm(self):
"""Remove weight normalization module from all of the layers."""
def _remove_weight_norm(m):
try:
torch.nn.utils.remove_weight_norm(m)
except ValueError: # this module didn't have weight norm
return
self.apply(_remove_weight_norm)
def apply_weight_norm(self):
"""Apply weight normalization module from all of the layers."""
def _apply_weight_norm(m):
if isinstance(m, nn.Conv1d):
torch.nn.utils.weight_norm(m)
self.apply(_apply_weight_norm)
def reset_parameters(self):
self.apply(init_weights)
class Codec_oobleck_Transformer(nn.Module):
def __init__(self,
ngf=32,
up_ratios=(2, 2,4,4, 5),
dilations=(1, 3, 9),
hidden_dim=1024,
depth=12,
heads=16,
pos_meb_dim=64,
):
super().__init__()
self.hop_length = np.prod(up_ratios)
self.ngf =ngf
self.up_ratios = up_ratios
self.hidden_dim = hidden_dim
self.conv_blocks = blocks.DilatedResidualEncoder(
capacity=ngf,
dilated_unit=self.dilated_unit,
downsampling_unit=self.downsampling_unit,
ratios=up_ratios,
dilations=dilations,
pre_network_conv=self.pre_conv,
post_network_conv=self.post_conv,
)
time_rotary_embed = RotaryPositionalEmbeddings(dim=pos_meb_dim)
transformer_blocks = [
TransformerBlock(dim=hidden_dim, n_heads=heads, rotary_embed=time_rotary_embed)
for _ in range(depth)
]
self.transformers = nn.Sequential(*transformer_blocks)
self.final_layer_norm = nn.LayerNorm(hidden_dim, eps=1e-6)
self.reset_parameters()
def forward(self, x):
x = self.conv_blocks(x)
x = x.permute(0, 2, 1)
x= self.transformers(x)
x = self.final_layer_norm(x)
return x
def inference(self, x):
return self.block(x)
def remove_weight_norm(self):
"""Remove weight normalization module from all of the layers."""
def _remove_weight_norm(m):
try:
torch.nn.utils.remove_weight_norm(m)
except ValueError: # this module didn't have weight norm
return
self.apply(_remove_weight_norm)
def apply_weight_norm(self):
"""Apply weight normalization module from all of the layers."""
def _apply_weight_norm(m):
if isinstance(m, nn.Conv1d):
torch.nn.utils.weight_norm(m)
self.apply(_apply_weight_norm)
def reset_parameters(self):
self.apply(init_weights)
def dilated_unit(self,hidden_dim, dilation):
return blocks.DilatedConvolutionalUnit(hidden_dim,
dilation,
kernel_size=3,
activation=nn.ReLU,
normalization=utils.weight_norm)
def downsampling_unit(self, input_dim: int, output_dim: int, stride: int):
return blocks.DownsamplingUnit(input_dim,
output_dim,
stride,
nn.ReLU,
normalization=utils.weight_norm)
def pre_conv(self,out_channels):
return nn.Conv1d(1, out_channels, 1)
def post_conv(self,in_channels):
return nn.Conv1d(in_channels, self.hidden_dim, 1)
class CodecEncoder_only_Transformer(nn.Module):
def __init__(self,hidden_dim=1024,depth=12,heads=16,pos_meb_dim=64):
super().__init__()
# self.embed = nn.Linear(input_dim, hidden_dim )input_dim=300,
depth = depth
time_rotary_embed = RotaryPositionalEmbeddings(dim=pos_meb_dim)
transformer_blocks = [
TransformerBlock(dim=hidden_dim, n_heads=heads, rotary_embed=time_rotary_embed)
for _ in range(depth)
]
self.transformers = nn.Sequential(*transformer_blocks)
self.final_layer_norm = nn.LayerNorm(hidden_dim, eps=1e-6)
def forward(self, x: torch.Tensor ) -> torch.Tensor:
# x = self.embed(x)
x= self.transformers(x)
x = self.final_layer_norm(x)
return x
def get_model_size(model):
# 计算总参数数
total_params = sum(p.numel() for p in model.parameters())
# 假设每个参数都是32位浮点数,计算模型大小(以字节为单位)
model_size_bytes = total_params # 每个参数4字节
# 转换为更易读的单位(例如,MB)
model_size_mb = model_size_bytes / (1024 ** 2)
return total_params, model_size_mb
if __name__ == '__main__':
model = Codec_oobleck_Transformer()
x = torch.randn(1, 1, 16000) # example input tensor
output = model(x)
print("Output shape:", output.shape)
|