Skip to content
Python

fixed offload mmaudio

DeepBeepMeep6a5022fb2474
diff --git a/models/wan/ovi/modules/mmaudio/ext/autoencoder/autoencoder.py b/models/wan/ovi/modules/mmaudio/ext/autoencoder/autoencoder.py
index ceb57ec..cbc6c02 100644
--- a/models/wan/ovi/modules/mmaudio/ext/autoencoder/autoencoder.py
+++ b/models/wan/ovi/modules/mmaudio/ext/autoencoder/autoencoder.py
@@ -1,14 +1,40 @@
+import os
from typing import Literal, Optional
import torch
import torch.nn as nn
import numpy as np
+from mmgp import offload
+from shared.utils import files_locator as fl
from .vae import VAE, get_my_vae
from .distributions import DiagonalGaussianDistribution
from ..bigvgan import BigVGAN
from ..bigvgan_v2.bigvgan import BigVGAN as BigVGANv2
+_BIGVGAN_V2_FOLDER = os.path.join("bigvgan_v2_44khz_128band_512x")
+
+
+def _resolve_bigvgan_v2_files():
+ weights_path = fl.locate_file(
+ os.path.join(_BIGVGAN_V2_FOLDER, "bigvgan_generator.pt"), error_if_none=False
+ )
+ config_path = fl.locate_file(
+ os.path.join(_BIGVGAN_V2_FOLDER, "config.json"), error_if_none=False
+ )
+ if weights_path is None or config_path is None:
+ raise FileNotFoundError(
+ f"Missing BigVGANv2 files in '{_BIGVGAN_V2_FOLDER}'. "
+ "Expected 'config.json' and 'bigvgan_generator.pt'."
+ )
+ return weights_path, config_path
+
+
+def _preprocess_bigvgan_v2_state_dict(state_dict, quantization_map=None, tied_weights_map=None):
+ if isinstance(state_dict, dict) and isinstance(state_dict.get("generator"), dict):
+ state_dict = state_dict["generator"]
+ return state_dict, quantization_map, tied_weights_map
+
@@ -30,9 +56,18 @@ class AutoEncoderModule(nn.Module):
assert vocoder_ckpt_path is not None
self.vocoder = BigVGAN(vocoder_ckpt_path).eval()
elif mode == '44k':
- self.vocoder = BigVGANv2.from_pretrained('nvidia/bigvgan_v2_44khz_128band_512x',
- use_cuda_kernel=False)
+ vocoder_ckpt_path, vocoder_config_path = _resolve_bigvgan_v2_files()
+ self.vocoder = offload.fast_load_transformers_model(
+ vocoder_ckpt_path,
+ modelClass=BigVGANv2,
+ forcedConfigPath=vocoder_config_path,
+ preprocess_sd=_preprocess_bigvgan_v2_state_dict,
+ configKwargs={"use_cuda_kernel": False},
+ writable_tensors=False,
+ default_dtype=torch.float32,
+ )
self.vocoder.remove_weight_norm()
+ self.vocoder.eval()
else:
raise ValueError(f'Unknown mode: {mode}')
diff --git a/models/wan/ovi/modules/mmaudio/ext/bigvgan_v2/bigvgan.py b/models/wan/ovi/modules/mmaudio/ext/bigvgan_v2/bigvgan.py
index cbcb21f..2bcbbf1 100644
--- a/models/wan/ovi/modules/mmaudio/ext/bigvgan_v2/bigvgan.py
+++ b/models/wan/ovi/modules/mmaudio/ext/bigvgan_v2/bigvgan.py
@@ -346,6 +346,12 @@ class BigVGAN(
print("[INFO] Model already removed weight norm. Skipping!")
pass
+ @classmethod
+ def from_config(cls, config: Dict):
+ config_data = dict(config)
+ use_cuda_kernel = config_data.pop("use_cuda_kernel", False)
+ return cls(AttrDict(config_data), use_cuda_kernel=use_cuda_kernel)
+
# Additional methods for huggingface_hub support
def _save_pretrained(self, save_directory: Path) -> None:
"""Save weights and config.json from a Pytorch model to a local directory."""
diff --git a/models/wan/ovi_handler.py b/models/wan/ovi_handler.py
index e47a8f9..039b911 100644
--- a/models/wan/ovi_handler.py
+++ b/models/wan/ovi_handler.py
@@ -73,10 +73,11 @@ class family_handler:
download_def = family_handler.query_model_files(computeList, "ti2v_2_2", model_def)
if not isinstance(download_def, list):
download_def = [download_def]
+ bigvgan_v2_files = ["config.json", "bigvgan_generator.pt"]
download_def += [{
"repoId" : "DeepBeepMeep/Wan2.1",
- "sourceFolderList" : ["mmaudio", ],
- "fileList" : [ [ "v1-16.pth", "best_netG.pt"]]
+ "sourceFolderList" : ["mmaudio", "bigvgan_v2_44khz_128band_512x"],
+ "fileList" : [ [ "v1-16.pth", "best_netG.pt"], bigvgan_v2_files]
}]
return download_def
diff --git a/postprocessing/mmaudio/ext/autoencoder/autoencoder.py b/postprocessing/mmaudio/ext/autoencoder/autoencoder.py
index e40f3fc..d1b79e8 100644
--- a/postprocessing/mmaudio/ext/autoencoder/autoencoder.py
+++ b/postprocessing/mmaudio/ext/autoencoder/autoencoder.py
@@ -1,13 +1,39 @@
+import os
from typing import Literal, Optional
import torch
import torch.nn as nn
+from mmgp import offload
+from shared.utils import files_locator as fl
from ..autoencoder.vae import VAE, get_my_vae
from ..bigvgan import BigVGAN
from ..bigvgan_v2.bigvgan import BigVGAN as BigVGANv2
from ...model.utils.distributions import DiagonalGaussianDistribution
+_BIGVGAN_V2_FOLDER = "bigvgan_v2_44khz_128band_512x"
+
+
+def _resolve_bigvgan_v2_files():
+ weights_path = fl.locate_file(
+ os.path.join(_BIGVGAN_V2_FOLDER, "bigvgan_generator.pt"), error_if_none=False
+ )
+ config_path = fl.locate_file(
+ os.path.join(_BIGVGAN_V2_FOLDER, "config.json"), error_if_none=False
+ )
+ if weights_path is None or config_path is None:
+ raise FileNotFoundError(
+ f"Missing BigVGANv2 files in '{_BIGVGAN_V2_FOLDER}'. "
+ "Expected 'config.json' and 'bigvgan_generator.pt'."
+ )
+ return weights_path, config_path
+
+
+def _preprocess_bigvgan_v2_state_dict(state_dict, quantization_map=None, tied_weights_map=None):
+ if isinstance(state_dict, dict) and isinstance(state_dict.get("generator"), dict):
+ state_dict = state_dict["generator"]
+ return state_dict, quantization_map, tied_weights_map
+
class AutoEncoderModule(nn.Module):
@@ -27,9 +53,18 @@ class AutoEncoderModule(nn.Module):
assert vocoder_ckpt_path is not None
self.vocoder = BigVGAN(vocoder_ckpt_path).eval()
elif mode == '44k':
- self.vocoder = BigVGANv2.from_pretrained('nvidia/bigvgan_v2_44khz_128band_512x',
- use_cuda_kernel=False)
+ vocoder_ckpt_path, vocoder_config_path = _resolve_bigvgan_v2_files()
+ self.vocoder = offload.fast_load_transformers_model(
+ vocoder_ckpt_path,
+ modelClass=BigVGANv2,
+ forcedConfigPath=vocoder_config_path,
+ preprocess_sd=_preprocess_bigvgan_v2_state_dict,
+ configKwargs={"use_cuda_kernel": False},
+ writable_tensors=False,
+ default_dtype=torch.float32,
+ )
self.vocoder.remove_weight_norm()
+ self.vocoder.eval()
else:
raise ValueError(f'Unknown mode: {mode}')
diff --git a/postprocessing/mmaudio/ext/bigvgan_v2/bigvgan.py b/postprocessing/mmaudio/ext/bigvgan_v2/bigvgan.py
index 96b87c2..4432583 100644
--- a/postprocessing/mmaudio/ext/bigvgan_v2/bigvgan.py
+++ b/postprocessing/mmaudio/ext/bigvgan_v2/bigvgan.py
@@ -346,6 +346,12 @@ class BigVGAN(
print("[INFO] Model already removed weight norm. Skipping!")
pass
+ @classmethod
+ def from_config(cls, config: Dict):
+ config_data = dict(config)
+ use_cuda_kernel = config_data.pop("use_cuda_kernel", False)
+ return cls(AttrDict(config_data), use_cuda_kernel=use_cuda_kernel)
+
# Additional methods for huggingface_hub support
def _save_pretrained(self, save_directory: Path) -> None:
"""Save weights and config.json from a Pytorch model to a local directory."""
diff --git a/wgp.py b/wgp.py
index 7ae0430..16dc396 100644
--- a/wgp.py
+++ b/wgp.py
@@ -3013,10 +3013,11 @@ def download_mmaudio():
mmaudio_enabled, mmaudio_mode, _, _, _ = get_mmaudio_settings(server_config)
if mmaudio_enabled:
mmaudio_files = ["synchformer_state_dict.pth", "v1-44.pth", MMAUDIO_STANDARD if mmaudio_mode == MMAUDIO_MODE_V2 else MMAUDIO_ALTERNATE]
+ bigvgan_v2_files = ["config.json", "bigvgan_generator.pt"]
enhancer_def = {
"repoId" : "DeepBeepMeep/Wan2.1",
- "sourceFolderList" : [ "mmaudio", "DFN5B-CLIP-ViT-H-14-378" ],
- "fileList" : [ mmaudio_files, ["open_clip_config.json", "open_clip_pytorch_model.bin"]]
+ "sourceFolderList" : [ "mmaudio", "DFN5B-CLIP-ViT-H-14-378", "bigvgan_v2_44khz_128band_512x" ],
+ "fileList" : [ mmaudio_files, ["open_clip_config.json", "open_clip_pytorch_model.bin"], bigvgan_v2_files]
}
process_files_def(**enhancer_def)