Rex Cheng commited on
Commit ·
dbac20f
1
Parent(s): f2786fb
initial commit
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- LICENSEAdded+21
- README.md+152-14
- app.pyAdded+149
- demo.pyAdded+135
- docs/images/icon.pngAdded
- docs/index.htmlAdded+147
- docs/style.cssAdded+78
- docs/style_videos.cssAdded+52
- docs/video_gen.htmlAdded+254
- docs/video_main.htmlAdded+98
- docs/video_vgg.htmlAdded+452
- mmaudio/__init__.pyAdded
- mmaudio/eval_utils.pyAdded+245
- mmaudio/ext/__init__.pyAdded+1
- mmaudio/ext/autoencoder/__init__.pyAdded+1
- mmaudio/ext/autoencoder/autoencoder.pyAdded+48
- mmaudio/ext/autoencoder/edm2_utils.pyAdded+168
- mmaudio/ext/autoencoder/vae.pyAdded+369
- mmaudio/ext/autoencoder/vae_modules.pyAdded+117
- mmaudio/ext/bigvgan/LICENSEAdded+21
- mmaudio/ext/bigvgan/__init__.pyAdded+1
- mmaudio/ext/bigvgan/activations.pyAdded+120
- mmaudio/ext/bigvgan/alias_free_torch/__init__.pyAdded+6
- mmaudio/ext/bigvgan/alias_free_torch/act.pyAdded+28
- mmaudio/ext/bigvgan/alias_free_torch/filter.pyAdded+95
- mmaudio/ext/bigvgan/alias_free_torch/resample.pyAdded+49
- mmaudio/ext/bigvgan/bigvgan.pyAdded+32
- mmaudio/ext/bigvgan/bigvgan_vocoder.ymlAdded+63
- mmaudio/ext/bigvgan/env.pyAdded+18
- mmaudio/ext/bigvgan/incl_licenses/LICENSE_1Added+21
- mmaudio/ext/bigvgan/incl_licenses/LICENSE_2Added+21
- mmaudio/ext/bigvgan/incl_licenses/LICENSE_3Added+201
- mmaudio/ext/bigvgan/incl_licenses/LICENSE_4Added+29
- mmaudio/ext/bigvgan/incl_licenses/LICENSE_5Added+16
- mmaudio/ext/bigvgan/models.pyAdded+255
- mmaudio/ext/bigvgan/utils.pyAdded+31
- mmaudio/ext/bigvgan_v2/LICENSEAdded+21
- mmaudio/ext/bigvgan_v2/__init__.pyAdded
- mmaudio/ext/bigvgan_v2/activations.pyAdded+126
- mmaudio/ext/bigvgan_v2/alias_free_activation/cuda/__init__.pyAdded
- mmaudio/ext/bigvgan_v2/alias_free_activation/cuda/activation1d.pyAdded+77
- mmaudio/ext/bigvgan_v2/alias_free_activation/cuda/anti_alias_activation.cppAdded+23
- mmaudio/ext/bigvgan_v2/alias_free_activation/cuda/anti_alias_activation_cuda.cuAdded+246
- mmaudio/ext/bigvgan_v2/alias_free_activation/cuda/compat.hAdded+29
- mmaudio/ext/bigvgan_v2/alias_free_activation/cuda/load.pyAdded+86
- mmaudio/ext/bigvgan_v2/alias_free_activation/cuda/type_shim.hAdded+92
- mmaudio/ext/bigvgan_v2/alias_free_activation/torch/__init__.pyAdded+6
- mmaudio/ext/bigvgan_v2/alias_free_activation/torch/act.pyAdded+32
- mmaudio/ext/bigvgan_v2/alias_free_activation/torch/filter.pyAdded+101
- mmaudio/ext/bigvgan_v2/alias_free_activation/torch/resample.pyAdded+54
123456789101112131415161718192021MIT License
Copyright (c) 2024 Ho Kei Cheng
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.1234567891011121314123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152---title: MMAudioemoji: 🐨colorFrom: yellowcolorTo: graysdk: gradiosdk_version: 5.8.0app_file: app.pypinned: falselicense: mitshort_description: Generating synchronizated audio given video/text inputs.---
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference# [Taming Multimodal Joint Training for High-Quality Video-to-Audio Synthesis](https://hkchengrex.github.io/MMAudio)
[Ho Kei Cheng](https://hkchengrex.github.io/), [Masato Ishii](https://scholar.google.co.jp/citations?user=RRIO1CcAAAAJ), [Akio Hayakawa](https://scholar.google.com/citations?user=sXAjHFIAAAAJ), [Takashi Shibuya](https://scholar.google.com/citations?user=XCRO260AAAAJ), [Alexander Schwing](https://www.alexander-schwing.de/), [Yuki Mitsufuji](https://www.yukimitsufuji.com/)
University of Illinois Urbana-Champaign, Sony AI, and Sony Group Corporation
[[Paper (being prepared)]](https://hkchengrex.github.io/MMAudio) [[Project Page]](https://hkchengrex.github.io/MMAudio)
**Note: This repository is still under construction. Single-example inference should work as expected. The training code will be added. Code is subject to non-backward-compatible changes.**
## Highlight
MMAudio generates synchronized audio given video and/or text inputs.Our key innovation is multimodal joint training which allows training on a wide range of audio-visual and audio-text datasets.Moreover, a synchronization module aligns the generated audio with the video frames.
## Results
(All audio from our algorithm MMAudio)
Videos from Sora:
https://github.com/user-attachments/assets/82afd192-0cee-48a1-86ca-bd39b8c8f330
Videos from MovieGen/Hunyuan Video/VGGSound:
https://github.com/user-attachments/assets/29230d4e-21c1-4cf8-a221-c28f2af6d0ca
For more results, visit https://hkchengrex.com/MMAudio/video_main.html.
## Installation
We have only tested this on Ubuntu.
### Prerequisites
We recommend using a [miniforge](https://github.com/conda-forge/miniforge) environment.
- Python 3.8+- PyTorch **2.5.1+** and corresponding torchvision/torchaudio (pick your CUDA version https://pytorch.org/)- ffmpeg<7 ([this is required by torchaudio](https://pytorch.org/audio/master/installation.html#optional-dependencies), you can install it in a miniforge environment with `conda install -c conda-forge 'ffmpeg<7'`)
**Clone our repository:**
```bashgit clone https://github.com/hkchengrex/MMAudio.git```
**Install with pip:**
```bashcd MMAudiopip install -e .```
(If you encounter the File "setup.py" not found error, upgrade your pip with pip install --upgrade pip)
**Pretrained models:**
The models will be downloaded automatically when you run the demo script. MD5 checksums are provided in `mmaudio/utils/download_utils.py`
| Model | Download link | File size || -------- | ------- | ------- || Flow prediction network, small 16kHz | <a href="https://databank.illinois.edu/datafiles/k6jve/download" download="mmaudio_small_16k.pth">mmaudio_small_16k.pth</a> | 601M || Flow prediction network, small 44.1kHz | <a href="https://databank.illinois.edu/datafiles/864ya/download" download="mmaudio_small_44k.pth">mmaudio_small_44k.pth</a> | 601M || Flow prediction network, medium 44.1kHz | <a href="https://databank.illinois.edu/datafiles/pa94t/download" download="mmaudio_medium_44k.pth">mmaudio_medium_44k.pth</a> | 2.4G || Flow prediction network, large 44.1kHz **(recommended)** | <a href="https://databank.illinois.edu/datafiles/4jx76/download" download="mmaudio_large_44k.pth">mmaudio_large_44k.pth</a> | 3.9G || 16kHz VAE | <a href="https://github.com/hkchengrex/MMAudio/releases/download/v0.1/v1-16.pth">v1-16.pth</a> | 655M || 16kHz BigVGAN vocoder |<a href="https://github.com/hkchengrex/MMAudio/releases/download/v0.1/best_netG.pt">best_netG.pt</a> | 429M || 44.1kHz VAE |<a href="https://github.com/hkchengrex/MMAudio/releases/download/v0.1/v1-44.pth">v1-44.pth</a> | 1.2G | | Synchformer visual encoder |<a href="https://github.com/hkchengrex/MMAudio/releases/download/v0.1/synchformer_state_dict.pth">synchformer_state_dict.pth</a> | 907M |
The 44.1kHz vocoder will be downloaded automatically.
The expected directory structure (full):
```bashMMAudio├── ext_weights│ ├── best_netG.pt│ ├── synchformer_state_dict.pth│ ├── v1-16.pth│ └── v1-44.pth├── weights│ ├── mmaudio_small_16k.pth│ ├── mmaudio_small_44k.pth│ ├── mmaudio_medium_44k.pth│ └── mmaudio_large_44k.pth└── ...```
The expected directory structure (minimal, for the recommended model only):
```bashMMAudio├── ext_weights│ ├── synchformer_state_dict.pth│ └── v1-44.pth├── weights│ └── mmaudio_large_44k.pth└── ...```
## Demo
By default, these scripts use the `large_44k` model. In our experiments, inference only takes around 6GB of GPU memory (in 16-bit mode) which should fit in most modern GPUs.
### Command-line interface
With `demo.py````bashpython demo.py --duration=8 --video=<path to video> --prompt "your prompt" ```The output (audio in `.flac` format, and video in `.mp4` format) will be saved in `./output`.See the file for more options.Simply omit the `--video` option for text-to-audio synthesis.The default output (and training) duration is 8 seconds. Longer/shorter durations could also work, but a large deviation from the training duration may result in a lower quality.
### Gradio interface
Supports video-to-audio and text-to-audio synthesis.
```python gradio_demo.py```
### Known limitations
1. The model sometimes generates undesired unintelligible human speech-like sounds2. The model sometimes generates undesired background music3. The model struggles with unfamiliar concepts, e.g., it can generate "gunfires" but not "RPG firing".
We believe all of these three limitations can be addressed with more high-quality training data.
## TrainingWork in progress.
## EvaluationWork in progress.
## AcknowledgementMany thanks to:- [Make-An-Audio 2](https://github.com/bytedance/Make-An-Audio-2) for the 16kHz BigVGAN pretrained model- [BigVGAN](https://github.com/NVIDIA/BigVGAN)- [Synchformer](https://github.com/v-iashin/Synchformer)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149import loggingfrom datetime import datetimefrom pathlib import Path
import gradio as grimport torchimport torchaudio
from mmaudio.eval_utils import (ModelConfig, all_model_cfg, generate, load_video, make_video, setup_eval_logging)from mmaudio.model.flow_matching import FlowMatchingfrom mmaudio.model.networks import MMAudio, get_my_mmaudiofrom mmaudio.model.sequence_config import SequenceConfigfrom mmaudio.model.utils.features_utils import FeaturesUtils
torch.backends.cuda.matmul.allow_tf32 = Truetorch.backends.cudnn.allow_tf32 = True
log = logging.getLogger()
device = 'cuda'dtype = torch.bfloat16
model: ModelConfig = all_model_cfg['large_44k_v2']model.download_if_needed()output_dir = Path('./output/gradio')
setup_eval_logging()
def get_model() -> tuple[MMAudio, FeaturesUtils, SequenceConfig]: seq_cfg = model.seq_cfg
net: MMAudio = get_my_mmaudio(model.model_name).to(device, dtype).eval() net.load_weights(torch.load(model.model_path, map_location=device, weights_only=True)) log.info(f'Loaded weights from {model.model_path}')
feature_utils = FeaturesUtils(tod_vae_ckpt=model.vae_path, synchformer_ckpt=model.synchformer_ckpt, enable_conditions=True, mode=model.mode, bigvgan_vocoder_ckpt=model.bigvgan_16k_path) feature_utils = feature_utils.to(device, dtype).eval()
return net, feature_utils, seq_cfg
net, feature_utils, seq_cfg = get_model()
@torch.inference_mode()def video_to_audio(video: gr.Video, prompt: str, negative_prompt: str, seed: int, num_steps: int, cfg_strength: float, duration: float):
rng = torch.Generator(device=device) rng.manual_seed(seed) fm = FlowMatching(min_sigma=0, inference_mode='euler', num_steps=num_steps)
clip_frames, sync_frames, duration = load_video(video, duration) clip_frames = clip_frames.unsqueeze(0) sync_frames = sync_frames.unsqueeze(0) seq_cfg.duration = duration net.update_seq_lengths(seq_cfg.latent_seq_len, seq_cfg.clip_seq_len, seq_cfg.sync_seq_len)
audios = generate(clip_frames, sync_frames, [prompt], negative_text=[negative_prompt], feature_utils=feature_utils, net=net, fm=fm, rng=rng, cfg_strength=cfg_strength) audio = audios.float().cpu()[0]
current_time_string = datetime.now().strftime('%Y%m%d_%H%M%S') output_dir.mkdir(exist_ok=True, parents=True) video_save_path = output_dir / f'{current_time_string}.mp4' make_video(video, video_save_path, audio, sampling_rate=seq_cfg.sampling_rate, duration_sec=seq_cfg.duration) return video_save_path
@torch.inference_mode()def text_to_audio(prompt: str, negative_prompt: str, seed: int, num_steps: int, cfg_strength: float, duration: float):
rng = torch.Generator(device=device) rng.manual_seed(seed) fm = FlowMatching(min_sigma=0, inference_mode='euler', num_steps=num_steps)
clip_frames = sync_frames = None seq_cfg.duration = duration net.update_seq_lengths(seq_cfg.latent_seq_len, seq_cfg.clip_seq_len, seq_cfg.sync_seq_len)
audios = generate(clip_frames, sync_frames, [prompt], negative_text=[negative_prompt], feature_utils=feature_utils, net=net, fm=fm, rng=rng, cfg_strength=cfg_strength) audio = audios.float().cpu()[0]
current_time_string = datetime.now().strftime('%Y%m%d_%H%M%S') output_dir.mkdir(exist_ok=True, parents=True) audio_save_path = output_dir / f'{current_time_string}.flac' torchaudio.save(audio_save_path, audio, seq_cfg.sampling_rate) return audio_save_path
video_to_audio_tab = gr.Interface( fn=video_to_audio, inputs=[ gr.Video(), gr.Text(label='Prompt'), gr.Text(label='Negative prompt', value='music'), gr.Number(label='Seed', value=0, precision=0, minimum=0), gr.Number(label='Num steps', value=25, precision=0, minimum=1), gr.Number(label='Guidance Strength', value=4.5, minimum=1), gr.Number(label='Duration (sec)', value=8, minimum=1), ], outputs='playable_video', cache_examples=False, title='MMAudio — Video-to-Audio Synthesis',)
text_to_audio_tab = gr.Interface( fn=text_to_audio, inputs=[ gr.Text(label='Prompt'), gr.Text(label='Negative prompt'), gr.Number(label='Seed', value=0, precision=0, minimum=0), gr.Number(label='Num steps', value=25, precision=0, minimum=1), gr.Number(label='Guidance Strength', value=4.5, minimum=1), gr.Number(label='Duration (sec)', value=8, minimum=1), ], outputs='audio', cache_examples=False, title='MMAudio — Text-to-Audio Synthesis',)
if __name__ == "__main__": gr.TabbedInterface([video_to_audio_tab, text_to_audio_tab], ['Video-to-Audio', 'Text-to-Audio']).launch(server_port=17888, allowed_paths=[output_dir])123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135import loggingfrom argparse import ArgumentParserfrom pathlib import Path
import torchimport torchaudio
from mmaudio.eval_utils import (ModelConfig, all_model_cfg, generate, load_video, make_video, setup_eval_logging)from mmaudio.model.flow_matching import FlowMatchingfrom mmaudio.model.networks import MMAudio, get_my_mmaudiofrom mmaudio.model.utils.features_utils import FeaturesUtils
torch.backends.cuda.matmul.allow_tf32 = Truetorch.backends.cudnn.allow_tf32 = True
log = logging.getLogger()
@torch.inference_mode()def main(): setup_eval_logging()
parser = ArgumentParser() parser.add_argument('--variant', type=str, default='large_44k_v2', help='small_16k, small_44k, medium_44k, large_44k, large_44k_v2') parser.add_argument('--video', type=Path, help='Path to the video file') parser.add_argument('--prompt', type=str, help='Input prompt', default='') parser.add_argument('--negative_prompt', type=str, help='Negative prompt', default='') parser.add_argument('--duration', type=float, default=8.0) parser.add_argument('--cfg_strength', type=float, default=4.5) parser.add_argument('--num_steps', type=int, default=25)
parser.add_argument('--mask_away_clip', action='store_true')
parser.add_argument('--output', type=Path, help='Output directory', default='./output') parser.add_argument('--seed', type=int, help='Random seed', default=42) parser.add_argument('--skip_video_composite', action='store_true') parser.add_argument('--full_precision', action='store_true')
args = parser.parse_args()
if args.variant not in all_model_cfg: raise ValueError(f'Unknown model variant: {args.variant}') model: ModelConfig = all_model_cfg[args.variant] model.download_if_needed() seq_cfg = model.seq_cfg
if args.video: video_path: Path = Path(args.video).expanduser() else: video_path = None prompt: str = args.prompt negative_prompt: str = args.negative_prompt output_dir: str = args.output.expanduser() seed: int = args.seed num_steps: int = args.num_steps duration: float = args.duration cfg_strength: float = args.cfg_strength skip_video_composite: bool = args.skip_video_composite mask_away_clip: bool = args.mask_away_clip
device = 'cuda' dtype = torch.float32 if args.full_precision else torch.bfloat16
output_dir.mkdir(parents=True, exist_ok=True)
# load a pretrained model net: MMAudio = get_my_mmaudio(model.model_name).to(device, dtype).eval() net.load_weights(torch.load(model.model_path, map_location=device, weights_only=True)) log.info(f'Loaded weights from {model.model_path}')
# misc setup rng = torch.Generator(device=device) rng.manual_seed(seed) fm = FlowMatching(min_sigma=0, inference_mode='euler', num_steps=num_steps)
feature_utils = FeaturesUtils(tod_vae_ckpt=model.vae_path, synchformer_ckpt=model.synchformer_ckpt, enable_conditions=True, mode=model.mode, bigvgan_vocoder_ckpt=model.bigvgan_16k_path) feature_utils = feature_utils.to(device, dtype).eval()
if video_path is not None: log.info(f'Using video {video_path}') clip_frames, sync_frames, duration = load_video(video_path, duration) if mask_away_clip: clip_frames = None else: clip_frames = clip_frames.unsqueeze(0) sync_frames = sync_frames.unsqueeze(0) else: log.info('No video provided -- text-to-audio mode') clip_frames = sync_frames = None
seq_cfg.duration = duration net.update_seq_lengths(seq_cfg.latent_seq_len, seq_cfg.clip_seq_len, seq_cfg.sync_seq_len)
log.info(f'Prompt: {prompt}') log.info(f'Negative prompt: {negative_prompt}')
audios = generate(clip_frames, sync_frames, [prompt], negative_text=[negative_prompt], feature_utils=feature_utils, net=net, fm=fm, rng=rng, cfg_strength=cfg_strength) audio = audios.float().cpu()[0] if video_path is not None: save_path = output_dir / f'{video_path.stem}.flac' else: safe_filename = prompt.replace(' ', '_').replace('/', '_').replace('.', '') save_path = output_dir / f'{safe_filename}.flac' torchaudio.save(save_path, audio, seq_cfg.sampling_rate)
log.info(f'Audio saved to {save_path}') if video_path is not None and not skip_video_composite: video_save_path = output_dir / f'{video_path.stem}.mp4' make_video(video_path, video_save_path, audio, sampling_rate=seq_cfg.sampling_rate, duration_sec=seq_cfg.duration) log.info(f'Video saved to {output_dir / video_save_path}')
log.info('Memory usage: %.2f GB', torch.cuda.max_memory_allocated() / (2**30))
if __name__ == '__main__': main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147<!DOCTYPE html><html lang="en"><head> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-0JKBJ3WRJZ"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-0JKBJ3WRJZ'); </script>
<link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+3&display=swap" rel="stylesheet"> <meta charset="UTF-8"> <title>MMAudio</title>
<link rel="icon" type="image/png" href="images/icon.png">
<meta name="viewport" content="width=device-width, initial-scale=1"> <!-- CSS only --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="style.css"></head><body>
<body> <br><br><br><br> <div class="container"> <div class="row text-center" style="font-size:38px"> <div class="col strong"> Taming Multimodal Joint Training for High-Quality <br>Video-to-Audio Synthesis </div> </div> <br> <div class="row text-center" style="font-size:28px"> <div class="col"> arXiv 2024 </div> </div> <br> <div class="h-100 row text-center heavy justify-content-md-center" style="font-size:22px;"> <div class="col-sm-auto px-lg-2"> <a href="https://hkchengrex.github.io/">Ho Kei Cheng<sup>1</sup></a> </div> <div class="col-sm-auto px-lg-2"> <nobr><a href="https://scholar.google.co.jp/citations?user=RRIO1CcAAAAJ">Masato Ishii<sup>2</sup></a></nobr> </div> <div class="col-sm-auto px-lg-2"> <nobr><a href="https://scholar.google.com/citations?user=sXAjHFIAAAAJ">Akio Hayakawa<sup>2</sup></a></nobr> </div> <div class="col-sm-auto px-lg-2"> <nobr><a href="https://scholar.google.com/citations?user=XCRO260AAAAJ">Takashi Shibuya<sup>2</sup></a></nobr> </div> <div class="col-sm-auto px-lg-2"> <nobr><a href="https://www.alexander-schwing.de/">Alexander Schwing<sup>1</sup></a></nobr> </div> <div class="col-sm-auto px-lg-2" > <nobr><a href="https://www.yukimitsufuji.com/">Yuki Mitsufuji<sup>2,3</sup></a></nobr> </div> </div>
<div class="h-100 row text-center heavy justify-content-md-center" style="font-size:22px;"> <div class="col-sm-auto px-lg-2"> <sup>1</sup>University of Illinois Urbana-Champaign </div> <div class="col-sm-auto px-lg-2"> <sup>2</sup>Sony AI </div> <div class="col-sm-auto px-lg-2"> <sup>3</sup>Sony Group Corporation </div> </div> <br> <br> <div class="h-100 row text-center justify-content-md-center" style="font-size:20px;"> <!-- <div class="col-sm-2"> <a href="https://arxiv.org/abs/2310.12982">[arXiv]</a> </div> --> <div class="col-sm-3"> <a href="">[Paper (being prepared)]</a> </div> <div class="col-sm-3"> <a href="https://github.com/hkchengrex/MMAudio">[Code]</a> </div> <!-- <div class="col-sm-2"> <a href="https://colab.research.google.com/drive/1yo43XTbjxuWA7XgCUO9qxAi7wBI6HzvP?usp=sharing">[Colab]</a> </div> --> </div> <br> <hr> <div class="row" style="font-size:32px"> <div class="col strong"> TL;DR </div> </div> <br> <div class="row"> <div class="col"> <p class="light" style="text-align: left;"> MMAudio generates synchronized audio given video and/or text inputs. </p> </div> </div> <br> <hr> <br> <div class="row" style="font-size:32px"> <div class="col strong"> Demo </div> </div> <br> <div class="row" style="font-size:48px"> <div class="col strong text-center"> <a href="video_main.html" style="text-decoration: underline;"><More results></a> </div> </div> <br> <div class="video-container" style="text-align: center;"> <iframe src="https://youtube.com/embed/YElewUT2M4M"></iframe> </div>
<br> <br><br> <br><br> </div>
</body></html>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778body { font-family: 'Source Sans 3', sans-serif; font-size: 18px; margin-left: auto; margin-right: auto; font-weight: 400; height: 100%; max-width: 1000px;}
table { width: 100%; border-collapse: collapse;}th, td { border: 1px solid #ddd; padding: 8px; text-align: center;}th { background-color: #f2f2f2;}video { width: 100%; height: auto;}p { font-size: 28px;}h2 { font-size: 36px;}
.strong { font-weight: 700;}
.light { font-weight: 100;}
.heavy { font-weight: 900;}
.column { float: left;}
a:link,a:visited { color: #05538f; text-decoration: none;}
a:hover { color: #63cbdd;}
hr { border: 0; height: 1px; background-image: linear-gradient(to right, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0));}
.video-container { position: relative; padding-bottom: 56.25%; /* 16:9 */ height: 0; } .video-container iframe { position: absolute; top: 0; left: 0; width: 100%; height: 100%;}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152body { font-family: 'Source Sans 3', sans-serif; font-size: 1.5vh; font-weight: 400;}
table { width: 100%; border-collapse: collapse;}th, td { border: 1px solid #ddd; padding: 8px; text-align: center;}th { background-color: #f2f2f2;}video { width: 100%; height: auto;}p { font-size: 1.5vh; font-weight: bold;}h2 { font-size: 2vh; font-weight: bold;}
.video-container { position: relative; padding-bottom: 56.25%; /* 16:9 */ height: 0; } .video-container iframe { position: absolute; top: 0; left: 0; width: 100%; height: 100%;}
.video-header { background-color: #f2f2f2; text-align: center; font-size: 1.5vh; font-weight: bold; padding: 8px;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254<!DOCTYPE html><html lang="en"><head> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-0JKBJ3WRJZ"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-0JKBJ3WRJZ'); </script>
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro' rel='stylesheet' type='text/css'> <meta charset="UTF-8"> <title>MMAudio</title>
<link rel="icon" type="image/png" href="images/icon.png">
<meta name="viewport" content="width=device-width, initial-scale=1"> <!-- CSS only --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<link rel="stylesheet" href="style_videos.css"></head><body>
<div id="moviegen_all"> <h2 id="moviegen" style="text-align: center;">Comparisons with Movie Gen Audio on Videos Generated by MovieGen</h2> <p id="moviegen1" style="overflow: hidden;"> Example 1: Ice cracking with sharp snapping sound, and metal tool scraping against the ice surface. <span style="float: right;"><a href="#index">Back to index</a></span> </p>
<div class="row g-1"> <div class="col-sm-6"> <div class="video-header">Movie Gen Audio</div> <div class="video-container"> <iframe src="https://youtube.com/embed/d7Lb0ihtGcE"></iframe> </div> </div> <div class="col-sm-6"> <div class="video-header">Ours</div> <div class="video-container"> <iframe src="https://youtube.com/embed/F4JoJ2r2m8U"></iframe> </div> </div> </div> <br>
<!-- <p id="moviegen2">Example 2: Rhythmic splashing and lapping of water. <span style="float:right;"><a href="#index">Back to index</a></span> </p>
<table> <thead> <tr> <th>Movie Gen Audio</th> <th>Ours</th> </tr> </thead> <tbody> <tr> <td width="50%"> <div class="video-container"> <iframe src="https://youtube.com/embed/5gQNPK99CIk"></iframe> </div> </td> <td width="50%"> <div class="video-container"> <iframe src="https://youtube.com/embed/AbwnTzG-BpA"></iframe> </div> </td> </tr> </tbody> </table> -->
<p id="moviegen2" style="overflow: hidden;"> Example 2: Rhythmic splashing and lapping of water. <span style="float:right;"><a href="#index">Back to index</a></span> </p> <div class="row g-1"> <div class="col-sm-6"> <div class="video-header">Movie Gen Audio</div> <div class="video-container"> <iframe src="https://youtube.com/embed/5gQNPK99CIk"></iframe> </div> </div> <div class="col-sm-6"> <div class="video-header">Ours</div> <div class="video-container"> <iframe src="https://youtube.com/embed/AbwnTzG-BpA"></iframe> </div> </div> </div> <br>
<p id="moviegen3" style="overflow: hidden;"> Example 3: Shovel scrapes against dry earth. <span style="float:right;"><a href="#index">Back to index</a></span> </p> <div class="row g-1"> <div class="col-sm-6"> <div class="video-header">Movie Gen Audio</div> <div class="video-container"> <iframe src="https://youtube.com/embed/PUKGyEve7XQ"></iframe> </div> </div> <div class="col-sm-6"> <div class="video-header">Ours</div> <div class="video-container"> <iframe src="https://youtube.com/embed/CNn7i8VNkdc"></iframe> </div> </div> </div> <br>
<p id="moviegen4" style="overflow: hidden;"> (Failure case) Example 4: Creamy sound of mashed potatoes being scooped. <span style="float:right;"><a href="#index">Back to index</a></span> </p> <div class="row g-1"> <div class="col-sm-6"> <div class="video-header">Movie Gen Audio</div> <div class="video-container"> <iframe src="https://youtube.com/embed/PJv1zxR9JjQ"></iframe> </div> </div> <div class="col-sm-6"> <div class="video-header">Ours</div> <div class="video-container"> <iframe src="https://youtube.com/embed/c3-LJ1lNsPQ"></iframe> </div> </div> </div> <br>
</div>
<div id="hunyuan_sora_all">
<h2 id="hunyuan" style="text-align: center;">Results on Videos Generated by Hunyuan</h2> <p style="overflow: hidden;"> <span style="float:right;"><a href="#index">Back to index</a></span> </p> <div class="row g-1"> <div class="col-sm-6"> <div class="video-header">Typing</div> <div class="video-container"> <iframe src="https://youtube.com/embed/8ln_9hhH_nk"></iframe> </div> </div> <div class="col-sm-6"> <div class="video-header">Water is rushing down a stream and pouring</div> <div class="video-container"> <iframe src="https://youtube.com/embed/5df1FZFQj30"></iframe> </div> </div> </div> <div class="row g-1"> <div class="col-sm-6"> <div class="video-header">Waves on beach</div> <div class="video-container"> <iframe src="https://youtube.com/embed/7wQ9D5WgpFc"></iframe> </div> </div> <div class="col-sm-6"> <div class="video-header">Water droplet</div> <div class="video-container"> <iframe src="https://youtube.com/embed/q7M2nsalGjM"></iframe> </div> </div> </div> <br>
<h2 id="sora" style="text-align: center;">Results on Videos Generated by Sora</h2> <p style="overflow: hidden;"> <span style="float:right;"><a href="#index">Back to index</a></span> </p> <div class="row g-1"> <div class="col-sm-6"> <div class="video-header">Ships riding waves</div> <div class="video-container"> <iframe src="https://youtube.com/embed/JbgQzHHytk8"></iframe> </div> </div> <div class="col-sm-6"> <div class="video-header">Train (no text prompt given)</div> <div class="video-container"> <iframe src="https://youtube.com/embed/xOW7zrjpWC8"></iframe> </div> </div> </div> <div class="row g-1"> <div class="col-sm-6"> <div class="video-header">Seashore (no text prompt given)</div> <div class="video-container"> <iframe src="https://youtube.com/embed/fIuw5Y8ZZ9E"></iframe> </div> </div> <div class="col-sm-6"> <div class="video-header">Surfing (failure: unprompted music)</div> <div class="video-container"> <iframe src="https://youtube.com/embed/UcSTk-v0M_s"></iframe> </div> </div> </div> <br>
<div id="mochi_ltx_all"> <h2 id="mochi" style="text-align: center;">Results on Videos Generated by Mochi 1</h2> <p style="overflow: hidden;"> <span style="float:right;"><a href="#index">Back to index</a></span> </p> <div class="row g-1"> <div class="col-sm-6"> <div class="video-header">Magical fire and lightning (no text prompt given)</div> <div class="video-container"> <iframe src="https://youtube.com/embed/tTlRZaSMNwY"></iframe> </div> </div> <div class="col-sm-6"> <div class="video-header">Storm (no text prompt given)</div> <div class="video-container"> <iframe src="https://youtube.com/embed/4hrZTMJUy3w"></iframe> </div> </div> </div> <br>
<h2 id="ltx" style="text-align: center;">Results on Videos Generated by LTX-Video</h2> <p style="overflow: hidden;"> <span style="float:right;"><a href="#index">Back to index</a></span> </p> <div class="row g-1"> <div class="col-sm-6"> <div class="video-header">Firewood burning and cracking</div> <div class="video-container"> <iframe src="https://youtube.com/embed/P7_DDpgev0g"></iframe> </div> </div> <div class="col-sm-6"> <div class="video-header">Waterfall, water splashing</div> <div class="video-container"> <iframe src="https://youtube.com/embed/4MvjceYnIO0"></iframe> </div> </div> </div> <br>
</div>
</body></html>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798<!DOCTYPE html><html lang="en"><head> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-0JKBJ3WRJZ"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-0JKBJ3WRJZ'); </script>
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro' rel='stylesheet' type='text/css'> <meta charset="UTF-8"> <title>MMAudio</title>
<link rel="icon" type="image/png" href="images/icon.png">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <!-- CSS only --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<link rel="stylesheet" href="style_videos.css">
<script type="text/javascript"> $(document).ready(function(){ $("#content").load("video_gen.html #moviegen_all"); $("#load_moveigen").click(function(){ $("#content").load("video_gen.html #moviegen_all"); }); $("#load_hunyuan_sora").click(function(){ $("#content").load("video_gen.html #hunyuan_sora_all"); }); $("#load_mochi_ltx").click(function(){ $("#content").load("video_gen.html #mochi_ltx_all"); }); $("#load_vgg1").click(function(){ $("#content").load("video_vgg.html #vgg1"); }); $("#load_vgg2").click(function(){ $("#content").load("video_vgg.html #vgg2"); }); $("#load_vgg3").click(function(){ $("#content").load("video_vgg.html #vgg3"); }); $("#load_vgg4").click(function(){ $("#content").load("video_vgg.html #vgg4"); }); $("#load_vgg5").click(function(){ $("#content").load("video_vgg.html #vgg5"); }); $("#load_vgg6").click(function(){ $("#content").load("video_vgg.html #vgg6"); }); $("#load_vgg_extra").click(function(){ $("#content").load("video_vgg.html #vgg_extra"); }); }); </script></head><body> <h1 id="index" style="text-align: center;">Index</h1> <p><b>(Click on the links to load the corresponding videos)</b> <span style="float:right;"><a href="index.html">Back to project page</a></span></p>
<ol> <li> <a href="#" id="load_moveigen">Comparisons with Movie Gen Audio on Videos Generated by MovieGen</a> </li> <li> <a href="#" id="load_hunyuan_sora">Results on Videos Generated by Hunyuan and Sora</a> </li> <li> <a href="#" id="load_mochi_ltx">Results on Videos Generated by Mochi 1 and LTX-Video</a> </li> <li> On VGGSound <ol> <li><a id='load_vgg1' href="#">Example 1: Wolf howling</a></li> <li><a id='load_vgg2' href="#">Example 2: Striking a golf ball</a></li> <li><a id='load_vgg3' href="#">Example 3: Hitting a drum</a></li> <li><a id='load_vgg4' href="#">Example 4: Dog barking</a></li> <li><a id='load_vgg5' href="#">Example 5: Playing a string instrument</a></li> <li><a id='load_vgg6' href="#">Example 6: A group of people playing tambourines</a></li> <li><a id='load_vgg_extra' href="#">Extra results & failure cases</a></li> </ol> </li> </ol>
<div id="content" class="container-fluid">
</div> <br> <br>
</body></html>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452<!DOCTYPE html><html lang="en"><head> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-0JKBJ3WRJZ"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-0JKBJ3WRJZ'); </script>
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro' rel='stylesheet' type='text/css'> <meta charset="UTF-8"> <title>MMAudio</title>
<meta name="viewport" content="width=device-width, initial-scale=1"> <!-- CSS only --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="style_videos.css"></head><body>
<div id="vgg1"> <h2 style="text-align: center;">Comparisons with state-of-the-art methods in VGGSound</h2> <p style="overflow: hidden;"> Example 1: Wolf howling. <span style="float:right;"><a href="#index">Back to index</a></span> </p> <div class="row g-1"> <div class="col-sm-3"> <div class="video-header">Ground-truth</div> <div class="video-container"> <iframe src="https://youtube.com/embed/9J_V74gqMUA"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">Ours</div> <div class="video-container"> <iframe src="https://youtube.com/embed/P6O8IpjErPc"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">V2A-Mapper</div> <div class="video-container"> <iframe src="https://youtube.com/embed/w-5eyqepvTk"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">FoleyCrafter</div> <div class="video-container"> <iframe src="https://youtube.com/embed/VOLfoZlRkzo"></iframe> </div> </div> </div> <div class="row g-1"> <div class="col-sm-3"> <div class="video-header">Frieren</div> <div class="video-container"> <iframe src="https://youtube.com/embed/49owKyA5Pa8"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">VATT</div> <div class="video-container"> <iframe src="https://youtube.com/embed/QVtrFgbeGDM"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">V-AURA</div> <div class="video-container"> <iframe src="https://youtube.com/embed/8r0uEfSNjvI"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">Seeing and Hearing</div> <div class="video-container"> <iframe src="https://youtube.com/embed/bn-sLg2qulk"></iframe> </div> </div> </div> </div>
<div id="vgg2"> <h2 style="text-align: center;">Comparisons with state-of-the-art methods in VGGSound</h2> <p style="overflow: hidden;"> Example 2: Striking a golf ball. <span style="float:right;"><a href="#index">Back to index</a></span> </p>
<div class="row g-1"> <div class="col-sm-3"> <div class="video-header">Ground-truth</div> <div class="video-container"> <iframe src="https://youtube.com/embed/1hwSu42kkho"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">Ours</div> <div class="video-container"> <iframe src="https://youtube.com/embed/kZibDoDCNxI"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">V2A-Mapper</div> <div class="video-container"> <iframe src="https://youtube.com/embed/jgKfLBLhh7Y"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">FoleyCrafter</div> <div class="video-container"> <iframe src="https://youtube.com/embed/Lfsx8mOPcJo"></iframe> </div> </div> </div> <div class="row g-1"> <div class="col-sm-3"> <div class="video-header">Frieren</div> <div class="video-container"> <iframe src="https://youtube.com/embed/tz-LpbB0MBc"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">VATT</div> <div class="video-container"> <iframe src="https://youtube.com/embed/RTDUHMi08n4"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">V-AURA</div> <div class="video-container"> <iframe src="https://youtube.com/embed/N-3TDOsPnZQ"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">Seeing and Hearing</div> <div class="video-container"> <iframe src="https://youtube.com/embed/QnsHnLn4gB0"></iframe> </div> </div> </div> </div>
<div id="vgg3"> <h2 style="text-align: center;">Comparisons with state-of-the-art methods in VGGSound</h2> <p style="overflow: hidden;"> Example 3: Hitting a drum. <span style="float:right;"><a href="#index">Back to index</a></span> </p>
<div class="row g-1"> <div class="col-sm-3"> <div class="video-header">Ground-truth</div> <div class="video-container"> <iframe src="https://youtube.com/embed/0oeIwq77w0Q"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">Ours</div> <div class="video-container"> <iframe src="https://youtube.com/embed/-UtPV9ohuIM"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">V2A-Mapper</div> <div class="video-container"> <iframe src="https://youtube.com/embed/9yivkgN-zwc"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">FoleyCrafter</div> <div class="video-container"> <iframe src="https://youtube.com/embed/kkCsXPOlBvY"></iframe> </div> </div> </div> <div class="row g-1"> <div class="col-sm-3"> <div class="video-header">Frieren</div> <div class="video-container"> <iframe src="https://youtube.com/embed/MbNKsVsuvig"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">VATT</div> <div class="video-container"> <iframe src="https://youtube.com/embed/2yYviBjrpBw"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">V-AURA</div> <div class="video-container"> <iframe src="https://youtube.com/embed/9yivkgN-zwc"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">Seeing and Hearing</div> <div class="video-container"> <iframe src="https://youtube.com/embed/6dnyQt4Fuhs"></iframe> </div> </div> </div> </div> </div>
<div id="vgg4"> <h2 style="text-align: center;">Comparisons with state-of-the-art methods in VGGSound</h2> <p style="overflow: hidden;"> Example 4: Dog barking. <span style="float:right;"><a href="#index">Back to index</a></span> </p>
<div class="row g-1"> <div class="col-sm-3"> <div class="video-header">Ground-truth</div> <div class="video-container"> <iframe src="https://youtube.com/embed/ckaqvTyMYAw"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">Ours</div> <div class="video-container"> <iframe src="https://youtube.com/embed/_aRndFZzZ-I"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">V2A-Mapper</div> <div class="video-container"> <iframe src="https://youtube.com/embed/mNCISP3LBl0"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">FoleyCrafter</div> <div class="video-container"> <iframe src="https://youtube.com/embed/phZBQ3L7foE"></iframe> </div> </div> </div> <div class="row g-1"> <div class="col-sm-3"> <div class="video-header">Frieren</div> <div class="video-container"> <iframe src="https://youtube.com/embed/Sb5Mg1-ORao"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">VATT</div> <div class="video-container"> <iframe src="https://youtube.com/embed/eHmAGOmtDDg"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">V-AURA</div> <div class="video-container"> <iframe src="https://youtube.com/embed/NEGa3krBrm0"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">Seeing and Hearing</div> <div class="video-container"> <iframe src="https://youtube.com/embed/aO0EAXlwE7A"></iframe> </div> </div> </div> </div> <div id="vgg5"> <h2 style="text-align: center;">Comparisons with state-of-the-art methods in VGGSound</h2> <p style="overflow: hidden;"> Example 5: Playing a string instrument. <span style="float:right;"><a href="#index">Back to index</a></span> </p>
<div class="row g-1"> <div class="col-sm-3"> <div class="video-header">Ground-truth</div> <div class="video-container"> <iframe src="https://youtube.com/embed/KP1QhWauIOc"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">Ours</div> <div class="video-container"> <iframe src="https://youtube.com/embed/ovaJhWSquYE"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">V2A-Mapper</div> <div class="video-container"> <iframe src="https://youtube.com/embed/N723FS9lcy8"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">FoleyCrafter</div> <div class="video-container"> <iframe src="https://youtube.com/embed/t0N4ZAAXo58"></iframe> </div> </div> </div> <div class="row g-1"> <div class="col-sm-3"> <div class="video-header">Frieren</div> <div class="video-container"> <iframe src="https://youtube.com/embed/8YSRs03QNNA"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">VATT</div> <div class="video-container"> <iframe src="https://youtube.com/embed/vOpMz55J1kY"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">V-AURA</div> <div class="video-container"> <iframe src="https://youtube.com/embed/9JHC75vr9h0"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">Seeing and Hearing</div> <div class="video-container"> <iframe src="https://youtube.com/embed/9w0JckNzXmY"></iframe> </div> </div> </div> </div> <div id="vgg6"> <h2 style="text-align: center;">Comparisons with state-of-the-art methods in VGGSound</h2> <p style="overflow: hidden;"> Example 6: A group of people playing tambourines. <span style="float:right;"><a href="#index">Back to index</a></span> </p>
<div class="row g-1"> <div class="col-sm-3"> <div class="video-header">Ground-truth</div> <div class="video-container"> <iframe src="https://youtube.com/embed/mx6JLxzUkRc"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">Ours</div> <div class="video-container"> <iframe src="https://youtube.com/embed/oLirHhP9Su8"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">V2A-Mapper</div> <div class="video-container"> <iframe src="https://youtube.com/embed/HkLkHMqptv0"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">FoleyCrafter</div> <div class="video-container"> <iframe src="https://youtube.com/embed/rpHiiODjmNU"></iframe> </div> </div> </div> <div class="row g-1"> <div class="col-sm-3"> <div class="video-header">Frieren</div> <div class="video-container"> <iframe src="https://youtube.com/embed/1mVD3fJ0LpM"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">VATT</div> <div class="video-container"> <iframe src="https://youtube.com/embed/yjVFnJiEJlw"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">V-AURA</div> <div class="video-container"> <iframe src="https://youtube.com/embed/neVeMSWtRkU"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">Seeing and Hearing</div> <div class="video-container"> <iframe src="https://youtube.com/embed/EUE7YwyVWz8"></iframe> </div> </div> </div> </div> <div id="vgg_extra"> <h2 style="text-align: center;">Comparisons with state-of-the-art methods in VGGSound</h2> <p style="overflow: hidden;"> <span style="float:right;"><a href="#index">Back to index</a></span> </p>
<div class="row g-1"> <div class="col-sm-3"> <div class="video-header">Moving train</div> <div class="video-container"> <iframe src="https://youtube.com/embed/Ta6H45rBzJc"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">Water splashing</div> <div class="video-container"> <iframe src="https://youtube.com/embed/hl6AtgHXpb4"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">Skateboarding</div> <div class="video-container"> <iframe src="https://youtube.com/embed/n4sCNi_9buI"></iframe> </div> </div> <div class="col-sm-3"> <div class="video-header">Synchronized clapping</div> <div class="video-container"> <iframe src="https://youtube.com/embed/oxexfpLn7FE"></iframe> </div> </div> </div>
<br><br> <div id="extra-failure"> <h2 style="text-align: center;">Failure cases</h2> <p style="overflow: hidden;"> <span style="float:right;"><a href="#index">Back to index</a></span> </p>
<div class="row g-1"> <div class="col-sm-6"> <div class="video-header">Human speech</div> <div class="video-container"> <iframe src="https://youtube.com/embed/nx0CyrDu70Y"></iframe> </div> </div> <div class="col-sm-6"> <div class="video-header">Unfamiliar vision input</div> <div class="video-container"> <iframe src="https://youtube.com/embed/hfnAqmK3X7w"></iframe> </div> </div> </div> </div> </div>
</body></html>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245import dataclassesimport loggingfrom pathlib import Pathfrom typing import Optional
import torchfrom colorlog import ColoredFormatterfrom torchvision.transforms import v2from torio.io import StreamingMediaDecoder, StreamingMediaEncoder
from mmaudio.model.flow_matching import FlowMatchingfrom mmaudio.model.networks import MMAudiofrom mmaudio.model.sequence_config import (CONFIG_16K, CONFIG_44K, SequenceConfig)from mmaudio.model.utils.features_utils import FeaturesUtilsfrom mmaudio.utils.download_utils import download_model_if_needed
log = logging.getLogger()
@dataclasses.dataclassclass ModelConfig: model_name: str model_path: Path vae_path: Path bigvgan_16k_path: Optional[Path] mode: str synchformer_ckpt: Path = Path('./ext_weights/synchformer_state_dict.pth')
@property def seq_cfg(self) -> SequenceConfig: if self.mode == '16k': return CONFIG_16K elif self.mode == '44k': return CONFIG_44K
def download_if_needed(self): download_model_if_needed(self.model_path) download_model_if_needed(self.vae_path) if self.bigvgan_16k_path is not None: download_model_if_needed(self.bigvgan_16k_path) download_model_if_needed(self.synchformer_ckpt)
small_16k = ModelConfig(model_name='small_16k', model_path=Path('./weights/mmaudio_small_16k.pth'), vae_path=Path('./ext_weights/v1-16.pth'), bigvgan_16k_path=Path('./ext_weights/best_netG.pt'), mode='16k')small_44k = ModelConfig(model_name='small_44k', model_path=Path('./weights/mmaudio_small_44k.pth'), vae_path=Path('./ext_weights/v1-44.pth'), bigvgan_16k_path=None, mode='44k')medium_44k = ModelConfig(model_name='medium_44k', model_path=Path('./weights/mmaudio_medium_44k.pth'), vae_path=Path('./ext_weights/v1-44.pth'), bigvgan_16k_path=None, mode='44k')large_44k = ModelConfig(model_name='large_44k', model_path=Path('./weights/mmaudio_large_44k.pth'), vae_path=Path('./ext_weights/v1-44.pth'), bigvgan_16k_path=None, mode='44k')large_44k_v2 = ModelConfig(model_name='large_44k_v2', model_path=Path('./weights/mmaudio_large_44k_v2.pth'), vae_path=Path('./ext_weights/v1-44.pth'), bigvgan_16k_path=None, mode='44k')all_model_cfg: dict[str, ModelConfig] = { 'small_16k': small_16k, 'small_44k': small_44k, 'medium_44k': medium_44k, 'large_44k': large_44k, 'large_44k_v2': large_44k_v2,}
def generate(clip_video: Optional[torch.Tensor], sync_video: Optional[torch.Tensor], text: Optional[list[str]], *, negative_text: Optional[list[str]] = None, feature_utils: FeaturesUtils, net: MMAudio, fm: FlowMatching, rng: torch.Generator, cfg_strength: float): device = feature_utils.device dtype = feature_utils.dtype
bs = len(text) if clip_video is not None: clip_video = clip_video.to(device, dtype, non_blocking=True) clip_features = feature_utils.encode_video_with_clip(clip_video, batch_size=bs) else: clip_features = net.get_empty_clip_sequence(bs)
if sync_video is not None: sync_video = sync_video.to(device, dtype, non_blocking=True) sync_features = feature_utils.encode_video_with_sync(sync_video, batch_size=bs) else: sync_features = net.get_empty_sync_sequence(bs)
if text is not None: text_features = feature_utils.encode_text(text) else: text_features = net.get_empty_string_sequence(bs)
if negative_text is not None: assert len(negative_text) == bs negative_text_features = feature_utils.encode_text(negative_text) else: negative_text_features = net.get_empty_string_sequence(bs)
x0 = torch.randn(bs, net.latent_seq_len, net.latent_dim, device=device, dtype=dtype, generator=rng) preprocessed_conditions = net.preprocess_conditions(clip_features, sync_features, text_features) empty_conditions = net.get_empty_conditions( bs, negative_text_features=negative_text_features if negative_text is not None else None)
cfg_ode_wrapper = lambda t, x: net.ode_wrapper(t, x, preprocessed_conditions, empty_conditions, cfg_strength) x1 = fm.to_data(cfg_ode_wrapper, x0) x1 = net.unnormalize(x1) spec = feature_utils.decode(x1) audio = feature_utils.vocode(spec) return audio
LOGFORMAT = " %(log_color)s%(levelname)-8s%(reset)s | %(log_color)s%(message)s%(reset)s"
def setup_eval_logging(log_level: int = logging.INFO): logging.root.setLevel(log_level) formatter = ColoredFormatter(LOGFORMAT) stream = logging.StreamHandler() stream.setLevel(log_level) stream.setFormatter(formatter) log = logging.getLogger() log.setLevel(log_level) log.addHandler(stream)
def load_video(video_path: Path, duration_sec: float) -> tuple[torch.Tensor, torch.Tensor, float]: _CLIP_SIZE = 384 _CLIP_FPS = 8.0
_SYNC_SIZE = 224 _SYNC_FPS = 25.0
clip_transform = v2.Compose([ v2.Resize((_CLIP_SIZE, _CLIP_SIZE), interpolation=v2.InterpolationMode.BICUBIC), v2.ToImage(), v2.ToDtype(torch.float32, scale=True), ])
sync_transform = v2.Compose([ v2.Resize(_SYNC_SIZE, interpolation=v2.InterpolationMode.BICUBIC), v2.CenterCrop(_SYNC_SIZE), v2.ToImage(), v2.ToDtype(torch.float32, scale=True), v2.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), ])
reader = StreamingMediaDecoder(video_path) reader.add_basic_video_stream( frames_per_chunk=int(_CLIP_FPS * duration_sec), frame_rate=_CLIP_FPS, format='rgb24', ) reader.add_basic_video_stream( frames_per_chunk=int(_SYNC_FPS * duration_sec), frame_rate=_SYNC_FPS, format='rgb24', )
reader.fill_buffer() data_chunk = reader.pop_chunks() clip_chunk = data_chunk[0] sync_chunk = data_chunk[1] assert clip_chunk is not None assert sync_chunk is not None
clip_frames = clip_transform(clip_chunk) sync_frames = sync_transform(sync_chunk)
clip_length_sec = clip_frames.shape[0] / _CLIP_FPS sync_length_sec = sync_frames.shape[0] / _SYNC_FPS
if clip_length_sec < duration_sec: log.warning(f'Clip video is too short: {clip_length_sec:.2f} < {duration_sec:.2f}') log.warning(f'Truncating to {clip_length_sec:.2f} sec') duration_sec = clip_length_sec
if sync_length_sec < duration_sec: log.warning(f'Sync video is too short: {sync_length_sec:.2f} < {duration_sec:.2f}') log.warning(f'Truncating to {sync_length_sec:.2f} sec') duration_sec = sync_length_sec
clip_frames = clip_frames[:int(_CLIP_FPS * duration_sec)] sync_frames = sync_frames[:int(_SYNC_FPS * duration_sec)]
return clip_frames, sync_frames, duration_sec
def make_video(video_path: Path, output_path: Path, audio: torch.Tensor, sampling_rate: int, duration_sec: float):
approx_max_length = int(duration_sec * 60) reader = StreamingMediaDecoder(video_path) reader.add_basic_video_stream( frames_per_chunk=approx_max_length, format='rgb24', ) reader.fill_buffer() video_chunk = reader.pop_chunks()[0] assert video_chunk is not None
fps = int(reader.get_out_stream_info(0).frame_rate) if fps > 60: log.warning(f'This code supports only up to 60 fps, but the video has {fps} fps') log.warning(f'Just change the *60 above me')
h, w = video_chunk.shape[-2:] video_chunk = video_chunk[:int(fps * duration_sec)]
writer = StreamingMediaEncoder(output_path) writer.add_audio_stream( sample_rate=sampling_rate, num_channels=audio.shape[0], encoder='aac', # 'flac' does not work for some reason? ) writer.add_video_stream(frame_rate=fps, width=w, height=h, format='rgb24', encoder='libx264', encoder_format='yuv420p') with writer.open(): writer.write_audio_chunk(0, audio.float().transpose(0, 1)) writer.write_video_chunk(1, video_chunk)1
1from .autoencoder import AutoEncoderModule123456789101112131415161718192021222324252627282930313233343536373839404142434445464748from typing import Literal, Optional
import torchimport torch.nn as nn
from mmaudio.ext.autoencoder.vae import VAE, get_my_vaefrom mmaudio.ext.bigvgan import BigVGANfrom mmaudio.ext.bigvgan_v2.bigvgan import BigVGAN as BigVGANv2from mmaudio.model.utils.distributions import DiagonalGaussianDistribution
class AutoEncoderModule(nn.Module):
def __init__(self, *, vae_ckpt_path, vocoder_ckpt_path: Optional[str] = None, mode: Literal['16k', '44k']): super().__init__() self.vae: VAE = get_my_vae(mode).eval() vae_state_dict = torch.load(vae_ckpt_path, weights_only=True, map_location='cpu') self.vae.load_state_dict(vae_state_dict) self.vae.remove_weight_norm()
if mode == '16k': 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) self.vocoder.remove_weight_norm() else: raise ValueError(f'Unknown mode: {mode}')
for param in self.parameters(): param.requires_grad = False
@torch.inference_mode() def encode(self, x: torch.Tensor) -> DiagonalGaussianDistribution: return self.vae.encode(x)
@torch.inference_mode() def decode(self, z: torch.Tensor) -> torch.Tensor: return self.vae.decode(z)
@torch.inference_mode() def vocode(self, spec: torch.Tensor) -> torch.Tensor: return self.vocoder(spec)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168# Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.## This work is licensed under a Creative Commons# Attribution-NonCommercial-ShareAlike 4.0 International License.# You should have received a copy of the license along with this# work. If not, see http://creativecommons.org/licenses/by-nc-sa/4.0/"""Improved diffusion model architecture proposed in the paper"Analyzing and Improving the Training Dynamics of Diffusion Models"."""
import numpy as npimport torch
#----------------------------------------------------------------------------# Variant of constant() that inherits dtype and device from the given# reference tensor by default.
_constant_cache = dict()
def constant(value, shape=None, dtype=None, device=None, memory_format=None): value = np.asarray(value) if shape is not None: shape = tuple(shape) if dtype is None: dtype = torch.get_default_dtype() if device is None: device = torch.device('cpu') if memory_format is None: memory_format = torch.contiguous_format
key = (value.shape, value.dtype, value.tobytes(), shape, dtype, device, memory_format) tensor = _constant_cache.get(key, None) if tensor is None: tensor = torch.as_tensor(value.copy(), dtype=dtype, device=device) if shape is not None: tensor, _ = torch.broadcast_tensors(tensor, torch.empty(shape)) tensor = tensor.contiguous(memory_format=memory_format) _constant_cache[key] = tensor return tensor
def const_like(ref, value, shape=None, dtype=None, device=None, memory_format=None): if dtype is None: dtype = ref.dtype if device is None: device = ref.device return constant(value, shape=shape, dtype=dtype, device=device, memory_format=memory_format)
#----------------------------------------------------------------------------# Normalize given tensor to unit magnitude with respect to the given# dimensions. Default = all dimensions except the first.
def normalize(x, dim=None, eps=1e-4): if dim is None: dim = list(range(1, x.ndim)) norm = torch.linalg.vector_norm(x, dim=dim, keepdim=True, dtype=torch.float32) norm = torch.add(eps, norm, alpha=np.sqrt(norm.numel() / x.numel())) return x / norm.to(x.dtype)
class Normalize(torch.nn.Module):
def __init__(self, dim=None, eps=1e-4): super().__init__() self.dim = dim self.eps = eps
def forward(self, x): return normalize(x, dim=self.dim, eps=self.eps)
#----------------------------------------------------------------------------# Upsample or downsample the given tensor with the given filter,# or keep it as is.
def resample(x, f=[1, 1], mode='keep'): if mode == 'keep': return x f = np.float32(f) assert f.ndim == 1 and len(f) % 2 == 0 pad = (len(f) - 1) // 2 f = f / f.sum() f = np.outer(f, f)[np.newaxis, np.newaxis, :, :] f = const_like(x, f) c = x.shape[1] if mode == 'down': return torch.nn.functional.conv2d(x, f.tile([c, 1, 1, 1]), groups=c, stride=2, padding=(pad, )) assert mode == 'up' return torch.nn.functional.conv_transpose2d(x, (f * 4).tile([c, 1, 1, 1]), groups=c, stride=2, padding=(pad, ))
#----------------------------------------------------------------------------# Magnitude-preserving SiLU (Equation 81).
def mp_silu(x): return torch.nn.functional.silu(x) / 0.596
class MPSiLU(torch.nn.Module):
def forward(self, x): return mp_silu(x)
#----------------------------------------------------------------------------# Magnitude-preserving sum (Equation 88).
def mp_sum(a, b, t=0.5): return a.lerp(b, t) / np.sqrt((1 - t)**2 + t**2)
#----------------------------------------------------------------------------# Magnitude-preserving concatenation (Equation 103).
def mp_cat(a, b, dim=1, t=0.5): Na = a.shape[dim] Nb = b.shape[dim] C = np.sqrt((Na + Nb) / ((1 - t)**2 + t**2)) wa = C / np.sqrt(Na) * (1 - t) wb = C / np.sqrt(Nb) * t return torch.cat([wa * a, wb * b], dim=dim)
#----------------------------------------------------------------------------# Magnitude-preserving convolution or fully-connected layer (Equation 47)# with force weight normalization (Equation 66).
class MPConv1D(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size): super().__init__() self.out_channels = out_channels self.weight = torch.nn.Parameter(torch.randn(out_channels, in_channels, kernel_size))
self.weight_norm_removed = False
def forward(self, x, gain=1): assert self.weight_norm_removed, 'call remove_weight_norm() before inference'
w = self.weight * gain if w.ndim == 2: return x @ w.t() assert w.ndim == 3 return torch.nn.functional.conv1d(x, w, padding=(w.shape[-1] // 2, ))
def remove_weight_norm(self): w = self.weight.to(torch.float32) w = normalize(w) # traditional weight normalization w = w / np.sqrt(w[0].numel()) w = w.to(self.weight.dtype) self.weight.data.copy_(w)
self.weight_norm_removed = True return self123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369import loggingfrom typing import Optional
import torchimport torch.nn as nn
from mmaudio.ext.autoencoder.edm2_utils import MPConv1Dfrom mmaudio.ext.autoencoder.vae_modules import (AttnBlock1D, Downsample1D, ResnetBlock1D, Upsample1D, nonlinearity)from mmaudio.model.utils.distributions import DiagonalGaussianDistribution
log = logging.getLogger()
DATA_MEAN_80D = [ -1.6058, -1.3676, -1.2520, -1.2453, -1.2078, -1.2224, -1.2419, -1.2439, -1.2922, -1.2927, -1.3170, -1.3543, -1.3401, -1.3836, -1.3907, -1.3912, -1.4313, -1.4152, -1.4527, -1.4728, -1.4568, -1.5101, -1.5051, -1.5172, -1.5623, -1.5373, -1.5746, -1.5687, -1.6032, -1.6131, -1.6081, -1.6331, -1.6489, -1.6489, -1.6700, -1.6738, -1.6953, -1.6969, -1.7048, -1.7280, -1.7361, -1.7495, -1.7658, -1.7814, -1.7889, -1.8064, -1.8221, -1.8377, -1.8417, -1.8643, -1.8857, -1.8929, -1.9173, -1.9379, -1.9531, -1.9673, -1.9824, -2.0042, -2.0215, -2.0436, -2.0766, -2.1064, -2.1418, -2.1855, -2.2319, -2.2767, -2.3161, -2.3572, -2.3954, -2.4282, -2.4659, -2.5072, -2.5552, -2.6074, -2.6584, -2.7107, -2.7634, -2.8266, -2.8981, -2.9673]
DATA_STD_80D = [ 1.0291, 1.0411, 1.0043, 0.9820, 0.9677, 0.9543, 0.9450, 0.9392, 0.9343, 0.9297, 0.9276, 0.9263, 0.9242, 0.9254, 0.9232, 0.9281, 0.9263, 0.9315, 0.9274, 0.9247, 0.9277, 0.9199, 0.9188, 0.9194, 0.9160, 0.9161, 0.9146, 0.9161, 0.9100, 0.9095, 0.9145, 0.9076, 0.9066, 0.9095, 0.9032, 0.9043, 0.9038, 0.9011, 0.9019, 0.9010, 0.8984, 0.8983, 0.8986, 0.8961, 0.8962, 0.8978, 0.8962, 0.8973, 0.8993, 0.8976, 0.8995, 0.9016, 0.8982, 0.8972, 0.8974, 0.8949, 0.8940, 0.8947, 0.8936, 0.8939, 0.8951, 0.8956, 0.9017, 0.9167, 0.9436, 0.9690, 1.0003, 1.0225, 1.0381, 1.0491, 1.0545, 1.0604, 1.0761, 1.0929, 1.1089, 1.1196, 1.1176, 1.1156, 1.1117, 1.1070]
DATA_MEAN_128D = [ -3.3462, -2.6723, -2.4893, -2.3143, -2.2664, -2.3317, -2.1802, -2.4006, -2.2357, -2.4597, -2.3717, -2.4690, -2.5142, -2.4919, -2.6610, -2.5047, -2.7483, -2.5926, -2.7462, -2.7033, -2.7386, -2.8112, -2.7502, -2.9594, -2.7473, -3.0035, -2.8891, -2.9922, -2.9856, -3.0157, -3.1191, -2.9893, -3.1718, -3.0745, -3.1879, -3.2310, -3.1424, -3.2296, -3.2791, -3.2782, -3.2756, -3.3134, -3.3509, -3.3750, -3.3951, -3.3698, -3.4505, -3.4509, -3.5089, -3.4647, -3.5536, -3.5788, -3.5867, -3.6036, -3.6400, -3.6747, -3.7072, -3.7279, -3.7283, -3.7795, -3.8259, -3.8447, -3.8663, -3.9182, -3.9605, -3.9861, -4.0105, -4.0373, -4.0762, -4.1121, -4.1488, -4.1874, -4.2461, -4.3170, -4.3639, -4.4452, -4.5282, -4.6297, -4.7019, -4.7960, -4.8700, -4.9507, -5.0303, -5.0866, -5.1634, -5.2342, -5.3242, -5.4053, -5.4927, -5.5712, -5.6464, -5.7052, -5.7619, -5.8410, -5.9188, -6.0103, -6.0955, -6.1673, -6.2362, -6.3120, -6.3926, -6.4797, -6.5565, -6.6511, -6.8130, -6.9961, -7.1275, -7.2457, -7.3576, -7.4663, -7.6136, -7.7469, -7.8815, -8.0132, -8.1515, -8.3071, -8.4722, -8.7418, -9.3975, -9.6628, -9.7671, -9.8863, -9.9992, -10.0860, -10.1709, -10.5418, -11.2795, -11.3861]
DATA_STD_128D = [ 2.3804, 2.4368, 2.3772, 2.3145, 2.2803, 2.2510, 2.2316, 2.2083, 2.1996, 2.1835, 2.1769, 2.1659, 2.1631, 2.1618, 2.1540, 2.1606, 2.1571, 2.1567, 2.1612, 2.1579, 2.1679, 2.1683, 2.1634, 2.1557, 2.1668, 2.1518, 2.1415, 2.1449, 2.1406, 2.1350, 2.1313, 2.1415, 2.1281, 2.1352, 2.1219, 2.1182, 2.1327, 2.1195, 2.1137, 2.1080, 2.1179, 2.1036, 2.1087, 2.1036, 2.1015, 2.1068, 2.0975, 2.0991, 2.0902, 2.1015, 2.0857, 2.0920, 2.0893, 2.0897, 2.0910, 2.0881, 2.0925, 2.0873, 2.0960, 2.0900, 2.0957, 2.0958, 2.0978, 2.0936, 2.0886, 2.0905, 2.0845, 2.0855, 2.0796, 2.0840, 2.0813, 2.0817, 2.0838, 2.0840, 2.0917, 2.1061, 2.1431, 2.1976, 2.2482, 2.3055, 2.3700, 2.4088, 2.4372, 2.4609, 2.4731, 2.4847, 2.5072, 2.5451, 2.5772, 2.6147, 2.6529, 2.6596, 2.6645, 2.6726, 2.6803, 2.6812, 2.6899, 2.6916, 2.6931, 2.6998, 2.7062, 2.7262, 2.7222, 2.7158, 2.7041, 2.7485, 2.7491, 2.7451, 2.7485, 2.7233, 2.7297, 2.7233, 2.7145, 2.6958, 2.6788, 2.6439, 2.6007, 2.4786, 2.2469, 2.1877, 2.1392, 2.0717, 2.0107, 1.9676, 1.9140, 1.7102, 0.9101, 0.7164]
class VAE(nn.Module):
def __init__( self, *, data_dim: int, embed_dim: int, hidden_dim: int, ): super().__init__()
if data_dim == 80: self.data_mean = nn.Buffer(torch.tensor(DATA_MEAN_80D, dtype=torch.float32).cuda()) self.data_std = nn.Buffer(torch.tensor(DATA_STD_80D, dtype=torch.float32).cuda()) elif data_dim == 128: self.data_mean = nn.Buffer(torch.tensor(DATA_MEAN_128D, dtype=torch.float32).cuda()) self.data_std = nn.Buffer(torch.tensor(DATA_STD_128D, dtype=torch.float32).cuda())
self.data_mean = self.data_mean.view(1, -1, 1) self.data_std = self.data_std.view(1, -1, 1)
self.encoder = Encoder1D( dim=hidden_dim, ch_mult=(1, 2, 4), num_res_blocks=2, attn_layers=[3], down_layers=[0], in_dim=data_dim, embed_dim=embed_dim, ) self.decoder = Decoder1D( dim=hidden_dim, ch_mult=(1, 2, 4), num_res_blocks=2, attn_layers=[3], down_layers=[0], in_dim=data_dim, out_dim=data_dim, embed_dim=embed_dim, )
self.embed_dim = embed_dim # self.quant_conv = nn.Conv1d(2 * embed_dim, 2 * embed_dim, 1) # self.post_quant_conv = nn.Conv1d(embed_dim, embed_dim, 1)
self.initialize_weights()
def initialize_weights(self): pass
def encode(self, x: torch.Tensor, normalize: bool = True) -> DiagonalGaussianDistribution: if normalize: x = self.normalize(x) moments = self.encoder(x) posterior = DiagonalGaussianDistribution(moments) return posterior
def decode(self, z: torch.Tensor, unnormalize: bool = True) -> torch.Tensor: dec = self.decoder(z) if unnormalize: dec = self.unnormalize(dec) return dec
def normalize(self, x: torch.Tensor) -> torch.Tensor: return (x - self.data_mean) / self.data_std
def unnormalize(self, x: torch.Tensor) -> torch.Tensor: return x * self.data_std + self.data_mean
def forward( self, x: torch.Tensor, sample_posterior: bool = True, rng: Optional[torch.Generator] = None, normalize: bool = True, unnormalize: bool = True, ) -> tuple[torch.Tensor, DiagonalGaussianDistribution]:
posterior = self.encode(x, normalize=normalize) if sample_posterior: z = posterior.sample(rng) else: z = posterior.mode() dec = self.decode(z, unnormalize=unnormalize) return dec, posterior
def load_weights(self, src_dict) -> None: self.load_state_dict(src_dict, strict=True)
@property def device(self) -> torch.device: return next(self.parameters()).device
def get_last_layer(self): return self.decoder.conv_out.weight
def remove_weight_norm(self): for name, m in self.named_modules(): if isinstance(m, MPConv1D): m.remove_weight_norm() log.debug(f"Removed weight norm from {name}") return self
class Encoder1D(nn.Module):
def __init__(self, *, dim: int, ch_mult: tuple[int] = (1, 2, 4, 8), num_res_blocks: int, attn_layers: list[int] = [], down_layers: list[int] = [], resamp_with_conv: bool = True, in_dim: int, embed_dim: int, double_z: bool = True, kernel_size: int = 3, clip_act: float = 256.0): super().__init__() self.dim = dim self.num_layers = len(ch_mult) self.num_res_blocks = num_res_blocks self.in_channels = in_dim self.clip_act = clip_act self.down_layers = down_layers self.attn_layers = attn_layers self.conv_in = MPConv1D(in_dim, self.dim, kernel_size=kernel_size)
in_ch_mult = (1, ) + tuple(ch_mult) self.in_ch_mult = in_ch_mult # downsampling self.down = nn.ModuleList() for i_level in range(self.num_layers): block = nn.ModuleList() attn = nn.ModuleList() block_in = dim * in_ch_mult[i_level] block_out = dim * ch_mult[i_level] for i_block in range(self.num_res_blocks): block.append( ResnetBlock1D(in_dim=block_in, out_dim=block_out, kernel_size=kernel_size, use_norm=True)) block_in = block_out if i_level in attn_layers: attn.append(AttnBlock1D(block_in)) down = nn.Module() down.block = block down.attn = attn if i_level in down_layers: down.downsample = Downsample1D(block_in, resamp_with_conv) self.down.append(down)
# middle self.mid = nn.Module() self.mid.block_1 = ResnetBlock1D(in_dim=block_in, out_dim=block_in, kernel_size=kernel_size, use_norm=True) self.mid.attn_1 = AttnBlock1D(block_in) self.mid.block_2 = ResnetBlock1D(in_dim=block_in, out_dim=block_in, kernel_size=kernel_size, use_norm=True)
# end self.conv_out = MPConv1D(block_in, 2 * embed_dim if double_z else embed_dim, kernel_size=kernel_size)
self.learnable_gain = nn.Parameter(torch.zeros([]))
def forward(self, x):
# downsampling hs = [self.conv_in(x)] for i_level in range(self.num_layers): for i_block in range(self.num_res_blocks): h = self.down[i_level].block[i_block](hs[-1]) if len(self.down[i_level].attn) > 0: h = self.down[i_level].attn[i_block](h) h = h.clamp(-self.clip_act, self.clip_act) hs.append(h) if i_level in self.down_layers: hs.append(self.down[i_level].downsample(hs[-1]))
# middle h = hs[-1] h = self.mid.block_1(h) h = self.mid.attn_1(h) h = self.mid.block_2(h) h = h.clamp(-self.clip_act, self.clip_act)
# end h = nonlinearity(h) h = self.conv_out(h, gain=(self.learnable_gain + 1)) return h
class Decoder1D(nn.Module):
def __init__(self, *, dim: int, out_dim: int, ch_mult: tuple[int] = (1, 2, 4, 8), num_res_blocks: int, attn_layers: list[int] = [], down_layers: list[int] = [], kernel_size: int = 3, resamp_with_conv: bool = True, in_dim: int, embed_dim: int, clip_act: float = 256.0): super().__init__() self.ch = dim self.num_layers = len(ch_mult) self.num_res_blocks = num_res_blocks self.in_channels = in_dim self.clip_act = clip_act self.down_layers = [i + 1 for i in down_layers] # each downlayer add one
# compute in_ch_mult, block_in and curr_res at lowest res block_in = dim * ch_mult[self.num_layers - 1]
# z to block_in self.conv_in = MPConv1D(embed_dim, block_in, kernel_size=kernel_size)
# middle self.mid = nn.Module() self.mid.block_1 = ResnetBlock1D(in_dim=block_in, out_dim=block_in, use_norm=True) self.mid.attn_1 = AttnBlock1D(block_in) self.mid.block_2 = ResnetBlock1D(in_dim=block_in, out_dim=block_in, use_norm=True)
# upsampling self.up = nn.ModuleList() for i_level in reversed(range(self.num_layers)): block = nn.ModuleList() attn = nn.ModuleList() block_out = dim * ch_mult[i_level] for i_block in range(self.num_res_blocks + 1): block.append(ResnetBlock1D(in_dim=block_in, out_dim=block_out, use_norm=True)) block_in = block_out if i_level in attn_layers: attn.append(AttnBlock1D(block_in)) up = nn.Module() up.block = block up.attn = attn if i_level in self.down_layers: up.upsample = Upsample1D(block_in, resamp_with_conv) self.up.insert(0, up) # prepend to get consistent order
# end self.conv_out = MPConv1D(block_in, out_dim, kernel_size=kernel_size) self.learnable_gain = nn.Parameter(torch.zeros([]))
def forward(self, z): # z to block_in h = self.conv_in(z)
# middle h = self.mid.block_1(h) h = self.mid.attn_1(h) h = self.mid.block_2(h) h = h.clamp(-self.clip_act, self.clip_act)
# upsampling for i_level in reversed(range(self.num_layers)): for i_block in range(self.num_res_blocks + 1): h = self.up[i_level].block[i_block](h) if len(self.up[i_level].attn) > 0: h = self.up[i_level].attn[i_block](h) h = h.clamp(-self.clip_act, self.clip_act) if i_level in self.down_layers: h = self.up[i_level].upsample(h)
h = nonlinearity(h) h = self.conv_out(h, gain=(self.learnable_gain + 1)) return h
def VAE_16k(**kwargs) -> VAE: return VAE(data_dim=80, embed_dim=20, hidden_dim=384, **kwargs)
def VAE_44k(**kwargs) -> VAE: return VAE(data_dim=128, embed_dim=40, hidden_dim=512, **kwargs)
def get_my_vae(name: str, **kwargs) -> VAE: if name == '16k': return VAE_16k(**kwargs) if name == '44k': return VAE_44k(**kwargs) raise ValueError(f'Unknown model: {name}')
if __name__ == '__main__': network = get_my_vae('standard')
# print the number of parameters in terms of millions num_params = sum(p.numel() for p in network.parameters()) / 1e6 print(f'Number of parameters: {num_params:.2f}M')123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117import torchimport torch.nn as nnimport torch.nn.functional as Ffrom einops import rearrange
from mmaudio.ext.autoencoder.edm2_utils import (MPConv1D, mp_silu, mp_sum, normalize)
def nonlinearity(x): # swish return mp_silu(x)
class ResnetBlock1D(nn.Module):
def __init__(self, *, in_dim, out_dim=None, conv_shortcut=False, kernel_size=3, use_norm=True): super().__init__() self.in_dim = in_dim out_dim = in_dim if out_dim is None else out_dim self.out_dim = out_dim self.use_conv_shortcut = conv_shortcut self.use_norm = use_norm
self.conv1 = MPConv1D(in_dim, out_dim, kernel_size=kernel_size) self.conv2 = MPConv1D(out_dim, out_dim, kernel_size=kernel_size) if self.in_dim != self.out_dim: if self.use_conv_shortcut: self.conv_shortcut = MPConv1D(in_dim, out_dim, kernel_size=kernel_size) else: self.nin_shortcut = MPConv1D(in_dim, out_dim, kernel_size=1)
def forward(self, x: torch.Tensor) -> torch.Tensor:
# pixel norm if self.use_norm: x = normalize(x, dim=1)
h = x h = nonlinearity(h) h = self.conv1(h)
h = nonlinearity(h) h = self.conv2(h)
if self.in_dim != self.out_dim: if self.use_conv_shortcut: x = self.conv_shortcut(x) else: x = self.nin_shortcut(x)
return mp_sum(x, h, t=0.3)
class AttnBlock1D(nn.Module):
def __init__(self, in_channels, num_heads=1): super().__init__() self.in_channels = in_channels
self.num_heads = num_heads self.qkv = MPConv1D(in_channels, in_channels * 3, kernel_size=1) self.proj_out = MPConv1D(in_channels, in_channels, kernel_size=1)
def forward(self, x): h = x y = self.qkv(h) y = y.reshape(y.shape[0], self.num_heads, -1, 3, y.shape[-1]) q, k, v = normalize(y, dim=2).unbind(3)
q = rearrange(q, 'b h c l -> b h l c') k = rearrange(k, 'b h c l -> b h l c') v = rearrange(v, 'b h c l -> b h l c')
h = F.scaled_dot_product_attention(q, k, v) h = rearrange(h, 'b h l c -> b (h c) l')
h = self.proj_out(h)
return mp_sum(x, h, t=0.3)
class Upsample1D(nn.Module):
def __init__(self, in_channels, with_conv): super().__init__() self.with_conv = with_conv if self.with_conv: self.conv = MPConv1D(in_channels, in_channels, kernel_size=3)
def forward(self, x): x = F.interpolate(x, scale_factor=2.0, mode='nearest-exact') # support 3D tensor(B,C,T) if self.with_conv: x = self.conv(x) return x
class Downsample1D(nn.Module):
def __init__(self, in_channels, with_conv): super().__init__() self.with_conv = with_conv if self.with_conv: # no asymmetric padding in torch conv, must do it ourselves self.conv1 = MPConv1D(in_channels, in_channels, kernel_size=1) self.conv2 = MPConv1D(in_channels, in_channels, kernel_size=1)
def forward(self, x):
if self.with_conv: x = self.conv1(x)
x = F.avg_pool1d(x, kernel_size=2, stride=2)
if self.with_conv: x = self.conv2(x)
return x123456789101112131415161718192021MIT License
Copyright (c) 2022 NVIDIA CORPORATION.
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.1from .bigvgan import BigVGAN123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120# Implementation adapted from https://github.com/EdwardDixon/snake under the MIT license.# LICENSE is in incl_licenses directory.
import torchfrom torch import nn, sin, powfrom torch.nn import Parameter
class Snake(nn.Module): ''' Implementation of a sine-based periodic activation function Shape: - Input: (B, C, T) - Output: (B, C, T), same shape as the input Parameters: - alpha - trainable parameter References: - This activation function is from this paper by Liu Ziyin, Tilman Hartwig, Masahito Ueda: https://arxiv.org/abs/2006.08195 Examples: >>> a1 = snake(256) >>> x = torch.randn(256) >>> x = a1(x) ''' def __init__(self, in_features, alpha=1.0, alpha_trainable=True, alpha_logscale=False): ''' Initialization. INPUT: - in_features: shape of the input - alpha: trainable parameter alpha is initialized to 1 by default, higher values = higher-frequency. alpha will be trained along with the rest of your model. ''' super(Snake, self).__init__() self.in_features = in_features
# initialize alpha self.alpha_logscale = alpha_logscale if self.alpha_logscale: # log scale alphas initialized to zeros self.alpha = Parameter(torch.zeros(in_features) * alpha) else: # linear scale alphas initialized to ones self.alpha = Parameter(torch.ones(in_features) * alpha)
self.alpha.requires_grad = alpha_trainable
self.no_div_by_zero = 0.000000001
def forward(self, x): ''' Forward pass of the function. Applies the function to the input elementwise. Snake ∶= x + 1/a * sin^2 (xa) ''' alpha = self.alpha.unsqueeze(0).unsqueeze(-1) # line up with x to [B, C, T] if self.alpha_logscale: alpha = torch.exp(alpha) x = x + (1.0 / (alpha + self.no_div_by_zero)) * pow(sin(x * alpha), 2)
return x
class SnakeBeta(nn.Module): ''' A modified Snake function which uses separate parameters for the magnitude of the periodic components Shape: - Input: (B, C, T) - Output: (B, C, T), same shape as the input Parameters: - alpha - trainable parameter that controls frequency - beta - trainable parameter that controls magnitude References: - This activation function is a modified version based on this paper by Liu Ziyin, Tilman Hartwig, Masahito Ueda: https://arxiv.org/abs/2006.08195 Examples: >>> a1 = snakebeta(256) >>> x = torch.randn(256) >>> x = a1(x) ''' def __init__(self, in_features, alpha=1.0, alpha_trainable=True, alpha_logscale=False): ''' Initialization. INPUT: - in_features: shape of the input - alpha - trainable parameter that controls frequency - beta - trainable parameter that controls magnitude alpha is initialized to 1 by default, higher values = higher-frequency. beta is initialized to 1 by default, higher values = higher-magnitude. alpha will be trained along with the rest of your model. ''' super(SnakeBeta, self).__init__() self.in_features = in_features
# initialize alpha self.alpha_logscale = alpha_logscale if self.alpha_logscale: # log scale alphas initialized to zeros self.alpha = Parameter(torch.zeros(in_features) * alpha) self.beta = Parameter(torch.zeros(in_features) * alpha) else: # linear scale alphas initialized to ones self.alpha = Parameter(torch.ones(in_features) * alpha) self.beta = Parameter(torch.ones(in_features) * alpha)
self.alpha.requires_grad = alpha_trainable self.beta.requires_grad = alpha_trainable
self.no_div_by_zero = 0.000000001
def forward(self, x): ''' Forward pass of the function. Applies the function to the input elementwise. SnakeBeta ∶= x + 1/b * sin^2 (xa) ''' alpha = self.alpha.unsqueeze(0).unsqueeze(-1) # line up with x to [B, C, T] beta = self.beta.unsqueeze(0).unsqueeze(-1) if self.alpha_logscale: alpha = torch.exp(alpha) beta = torch.exp(beta) x = x + (1.0 / (beta + self.no_div_by_zero)) * pow(sin(x * alpha), 2)
return x123456# Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0# LICENSE is in incl_licenses directory.
from .filter import *from .resample import *from .act import *12345678910111213141516171819202122232425262728# Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0# LICENSE is in incl_licenses directory.
import torch.nn as nnfrom .resample import UpSample1d, DownSample1d
class Activation1d(nn.Module): def __init__(self, activation, up_ratio: int = 2, down_ratio: int = 2, up_kernel_size: int = 12, down_kernel_size: int = 12): super().__init__() self.up_ratio = up_ratio self.down_ratio = down_ratio self.act = activation self.upsample = UpSample1d(up_ratio, up_kernel_size) self.downsample = DownSample1d(down_ratio, down_kernel_size)
# x: [B,C,T] def forward(self, x): x = self.upsample(x) x = self.act(x) x = self.downsample(x)
return x1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495# Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0# LICENSE is in incl_licenses directory.
import torchimport torch.nn as nnimport torch.nn.functional as Fimport math
if 'sinc' in dir(torch): sinc = torch.sincelse: # This code is adopted from adefossez's julius.core.sinc under the MIT License # https://adefossez.github.io/julius/julius/core.html # LICENSE is in incl_licenses directory. def sinc(x: torch.Tensor): """ Implementation of sinc, i.e. sin(pi * x) / (pi * x) __Warning__: Different to julius.sinc, the input is multiplied by `pi`! """ return torch.where(x == 0, torch.tensor(1., device=x.device, dtype=x.dtype), torch.sin(math.pi * x) / math.pi / x)
# This code is adopted from adefossez's julius.lowpass.LowPassFilters under the MIT License# https://adefossez.github.io/julius/julius/lowpass.html# LICENSE is in incl_licenses directory.def kaiser_sinc_filter1d(cutoff, half_width, kernel_size): # return filter [1,1,kernel_size] even = (kernel_size % 2 == 0) half_size = kernel_size // 2
#For kaiser window delta_f = 4 * half_width A = 2.285 * (half_size - 1) * math.pi * delta_f + 7.95 if A > 50.: beta = 0.1102 * (A - 8.7) elif A >= 21.: beta = 0.5842 * (A - 21)**0.4 + 0.07886 * (A - 21.) else: beta = 0. window = torch.kaiser_window(kernel_size, beta=beta, periodic=False)
# ratio = 0.5/cutoff -> 2 * cutoff = 1 / ratio if even: time = (torch.arange(-half_size, half_size) + 0.5) else: time = torch.arange(kernel_size) - half_size if cutoff == 0: filter_ = torch.zeros_like(time) else: filter_ = 2 * cutoff * window * sinc(2 * cutoff * time) # Normalize filter to have sum = 1, otherwise we will have a small leakage # of the constant component in the input signal. filter_ /= filter_.sum() filter = filter_.view(1, 1, kernel_size)
return filter
class LowPassFilter1d(nn.Module): def __init__(self, cutoff=0.5, half_width=0.6, stride: int = 1, padding: bool = True, padding_mode: str = 'replicate', kernel_size: int = 12): # kernel_size should be even number for stylegan3 setup, # in this implementation, odd number is also possible. super().__init__() if cutoff < -0.: raise ValueError("Minimum cutoff must be larger than zero.") if cutoff > 0.5: raise ValueError("A cutoff above 0.5 does not make sense.") self.kernel_size = kernel_size self.even = (kernel_size % 2 == 0) self.pad_left = kernel_size // 2 - int(self.even) self.pad_right = kernel_size // 2 self.stride = stride self.padding = padding self.padding_mode = padding_mode filter = kaiser_sinc_filter1d(cutoff, half_width, kernel_size) self.register_buffer("filter", filter)
#input [B, C, T] def forward(self, x): _, C, _ = x.shape
if self.padding: x = F.pad(x, (self.pad_left, self.pad_right), mode=self.padding_mode) out = F.conv1d(x, self.filter.expand(C, -1, -1), stride=self.stride, groups=C)
return out12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849# Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0# LICENSE is in incl_licenses directory.
import torch.nn as nnfrom torch.nn import functional as Ffrom .filter import LowPassFilter1dfrom .filter import kaiser_sinc_filter1d
class UpSample1d(nn.Module): def __init__(self, ratio=2, kernel_size=None): super().__init__() self.ratio = ratio self.kernel_size = int(6 * ratio // 2) * 2 if kernel_size is None else kernel_size self.stride = ratio self.pad = self.kernel_size // ratio - 1 self.pad_left = self.pad * self.stride + (self.kernel_size - self.stride) // 2 self.pad_right = self.pad * self.stride + (self.kernel_size - self.stride + 1) // 2 filter = kaiser_sinc_filter1d(cutoff=0.5 / ratio, half_width=0.6 / ratio, kernel_size=self.kernel_size) self.register_buffer("filter", filter)
# x: [B, C, T] def forward(self, x): _, C, _ = x.shape
x = F.pad(x, (self.pad, self.pad), mode='replicate') x = self.ratio * F.conv_transpose1d( x, self.filter.expand(C, -1, -1), stride=self.stride, groups=C) x = x[..., self.pad_left:-self.pad_right]
return x
class DownSample1d(nn.Module): def __init__(self, ratio=2, kernel_size=None): super().__init__() self.ratio = ratio self.kernel_size = int(6 * ratio // 2) * 2 if kernel_size is None else kernel_size self.lowpass = LowPassFilter1d(cutoff=0.5 / ratio, half_width=0.6 / ratio, stride=ratio, kernel_size=self.kernel_size)
def forward(self, x): xx = self.lowpass(x)
return xx1234567891011121314151617181920212223242526272829303132from pathlib import Path
import torchimport torch.nn as nnfrom omegaconf import OmegaConf
from mmaudio.ext.bigvgan.models import BigVGANVocoder
_bigvgan_vocoder_path = Path(__file__).parent / 'bigvgan_vocoder.yml'
class BigVGAN(nn.Module):
def __init__(self, ckpt_path, config_path=_bigvgan_vocoder_path): super().__init__() vocoder_cfg = OmegaConf.load(config_path) self.vocoder = BigVGANVocoder(vocoder_cfg).eval() vocoder_ckpt = torch.load(ckpt_path, map_location='cpu', weights_only=True)['generator'] self.vocoder.load_state_dict(vocoder_ckpt)
self.weight_norm_removed = False self.remove_weight_norm()
@torch.inference_mode() def forward(self, x): assert self.weight_norm_removed, 'call remove_weight_norm() before inference' return self.vocoder(x)
def remove_weight_norm(self): self.vocoder.remove_weight_norm() self.weight_norm_removed = True return self123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263resblock: '1'num_gpus: 0batch_size: 64num_mels: 80learning_rate: 0.0001adam_b1: 0.8adam_b2: 0.99lr_decay: 0.999seed: 1234upsample_rates:- 4- 4- 2- 2- 2- 2upsample_kernel_sizes:- 8- 8- 4- 4- 4- 4upsample_initial_channel: 1536resblock_kernel_sizes:- 3- 7- 11resblock_dilation_sizes:- - 1 - 3 - 5- - 1 - 3 - 5- - 1 - 3 - 5activation: snakebetasnake_logscale: trueresolutions:- - 1024 - 120 - 600- - 2048 - 240 - 1200- - 512 - 50 - 240mpd_reshapes:- 2- 3- 5- 7- 11use_spectral_norm: falsediscriminator_channel_mult: 1num_workers: 4dist_config: dist_backend: nccl dist_url: tcp://localhost:54341 world_size: 1123456789101112131415161718# Adapted from https://github.com/jik876/hifi-gan under the MIT license.# LICENSE is in incl_licenses directory.
import osimport shutil
class AttrDict(dict): def __init__(self, *args, **kwargs): super(AttrDict, self).__init__(*args, **kwargs) self.__dict__ = self
def build_env(config, config_name, path): t_path = os.path.join(path, config_name) if config != t_path: os.makedirs(path, exist_ok=True) shutil.copyfile(config, os.path.join(path, config_name))123456789101112131415161718192021MIT License
Copyright (c) 2020 Jungil Kong
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.123456789101112131415161718192021MIT License
Copyright (c) 2020 Edward Dixon
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.1234567891011121314151617181920212223242526272829BSD 3-Clause License
Copyright (c) 2019, Seungwon Park 박승원All rights reserved.
Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLEFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIALDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.12345678910111213141516Copyright 2020 Alexandre Défossez
Permission is hereby granted, free of charge, to any person obtaining a copy of this software andassociated documentation files (the "Software"), to deal in the Software without restriction,including without limitation the rights to use, copy, modify, merge, publish, distribute,sublicense, and/or sell copies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies orsubstantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUTNOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE ANDNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255# Copyright (c) 2022 NVIDIA CORPORATION.# Licensed under the MIT license.
# Adapted from https://github.com/jik876/hifi-gan under the MIT license.# LICENSE is in incl_licenses directory.
import torchimport torch.nn as nnfrom torch.nn import Conv1d, ConvTranspose1dfrom torch.nn.utils.parametrizations import weight_normfrom torch.nn.utils.parametrize import remove_parametrizations
from mmaudio.ext.bigvgan import activationsfrom mmaudio.ext.bigvgan.alias_free_torch import *from mmaudio.ext.bigvgan.utils import get_padding, init_weights
LRELU_SLOPE = 0.1
class AMPBlock1(torch.nn.Module):
def __init__(self, h, channels, kernel_size=3, dilation=(1, 3, 5), activation=None): super(AMPBlock1, self).__init__() self.h = h
self.convs1 = nn.ModuleList([ weight_norm( Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0], padding=get_padding(kernel_size, dilation[0]))), weight_norm( Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1], padding=get_padding(kernel_size, dilation[1]))), weight_norm( Conv1d(channels, channels, kernel_size, 1, dilation=dilation[2], padding=get_padding(kernel_size, dilation[2]))) ]) self.convs1.apply(init_weights)
self.convs2 = nn.ModuleList([ weight_norm( Conv1d(channels, channels, kernel_size, 1, dilation=1, padding=get_padding(kernel_size, 1))), weight_norm( Conv1d(channels, channels, kernel_size, 1, dilation=1, padding=get_padding(kernel_size, 1))), weight_norm( Conv1d(channels, channels, kernel_size, 1, dilation=1, padding=get_padding(kernel_size, 1))) ]) self.convs2.apply(init_weights)
self.num_layers = len(self.convs1) + len(self.convs2) # total number of conv layers
if activation == 'snake': # periodic nonlinearity with snake function and anti-aliasing self.activations = nn.ModuleList([ Activation1d( activation=activations.Snake(channels, alpha_logscale=h.snake_logscale)) for _ in range(self.num_layers) ]) elif activation == 'snakebeta': # periodic nonlinearity with snakebeta function and anti-aliasing self.activations = nn.ModuleList([ Activation1d( activation=activations.SnakeBeta(channels, alpha_logscale=h.snake_logscale)) for _ in range(self.num_layers) ]) else: raise NotImplementedError( "activation incorrectly specified. check the config file and look for 'activation'." )
def forward(self, x): acts1, acts2 = self.activations[::2], self.activations[1::2] for c1, c2, a1, a2 in zip(self.convs1, self.convs2, acts1, acts2): xt = a1(x) xt = c1(xt) xt = a2(xt) xt = c2(xt) x = xt + x
return x
def remove_weight_norm(self): for l in self.convs1: remove_parametrizations(l, 'weight') for l in self.convs2: remove_parametrizations(l, 'weight')
class AMPBlock2(torch.nn.Module):
def __init__(self, h, channels, kernel_size=3, dilation=(1, 3), activation=None): super(AMPBlock2, self).__init__() self.h = h
self.convs = nn.ModuleList([ weight_norm( Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0], padding=get_padding(kernel_size, dilation[0]))), weight_norm( Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1], padding=get_padding(kernel_size, dilation[1]))) ]) self.convs.apply(init_weights)
self.num_layers = len(self.convs) # total number of conv layers
if activation == 'snake': # periodic nonlinearity with snake function and anti-aliasing self.activations = nn.ModuleList([ Activation1d( activation=activations.Snake(channels, alpha_logscale=h.snake_logscale)) for _ in range(self.num_layers) ]) elif activation == 'snakebeta': # periodic nonlinearity with snakebeta function and anti-aliasing self.activations = nn.ModuleList([ Activation1d( activation=activations.SnakeBeta(channels, alpha_logscale=h.snake_logscale)) for _ in range(self.num_layers) ]) else: raise NotImplementedError( "activation incorrectly specified. check the config file and look for 'activation'." )
def forward(self, x): for c, a in zip(self.convs, self.activations): xt = a(x) xt = c(xt) x = xt + x
return x
def remove_weight_norm(self): for l in self.convs: remove_parametrizations(l, 'weight')
class BigVGANVocoder(torch.nn.Module): # this is our main BigVGAN model. Applies anti-aliased periodic activation for resblocks. def __init__(self, h): super().__init__() self.h = h
self.num_kernels = len(h.resblock_kernel_sizes) self.num_upsamples = len(h.upsample_rates)
# pre conv self.conv_pre = weight_norm(Conv1d(h.num_mels, h.upsample_initial_channel, 7, 1, padding=3))
# define which AMPBlock to use. BigVGAN uses AMPBlock1 as default resblock = AMPBlock1 if h.resblock == '1' else AMPBlock2
# transposed conv-based upsamplers. does not apply anti-aliasing self.ups = nn.ModuleList() for i, (u, k) in enumerate(zip(h.upsample_rates, h.upsample_kernel_sizes)): self.ups.append( nn.ModuleList([ weight_norm( ConvTranspose1d(h.upsample_initial_channel // (2**i), h.upsample_initial_channel // (2**(i + 1)), k, u, padding=(k - u) // 2)) ]))
# residual blocks using anti-aliased multi-periodicity composition modules (AMP) self.resblocks = nn.ModuleList() for i in range(len(self.ups)): ch = h.upsample_initial_channel // (2**(i + 1)) for j, (k, d) in enumerate(zip(h.resblock_kernel_sizes, h.resblock_dilation_sizes)): self.resblocks.append(resblock(h, ch, k, d, activation=h.activation))
# post conv if h.activation == "snake": # periodic nonlinearity with snake function and anti-aliasing activation_post = activations.Snake(ch, alpha_logscale=h.snake_logscale) self.activation_post = Activation1d(activation=activation_post) elif h.activation == "snakebeta": # periodic nonlinearity with snakebeta function and anti-aliasing activation_post = activations.SnakeBeta(ch, alpha_logscale=h.snake_logscale) self.activation_post = Activation1d(activation=activation_post) else: raise NotImplementedError( "activation incorrectly specified. check the config file and look for 'activation'." )
self.conv_post = weight_norm(Conv1d(ch, 1, 7, 1, padding=3))
# weight initialization for i in range(len(self.ups)): self.ups[i].apply(init_weights) self.conv_post.apply(init_weights)
def forward(self, x): # pre conv x = self.conv_pre(x)
for i in range(self.num_upsamples): # upsampling for i_up in range(len(self.ups[i])): x = self.ups[i][i_up](x) # AMP blocks xs = None for j in range(self.num_kernels): if xs is None: xs = self.resblocks[i * self.num_kernels + j](x) else: xs += self.resblocks[i * self.num_kernels + j](x) x = xs / self.num_kernels
# post conv x = self.activation_post(x) x = self.conv_post(x) x = torch.tanh(x)
return x
def remove_weight_norm(self): print('Removing weight norm...') for l in self.ups: for l_i in l: remove_parametrizations(l_i, 'weight') for l in self.resblocks: l.remove_weight_norm() remove_parametrizations(self.conv_pre, 'weight') remove_parametrizations(self.conv_post, 'weight')12345678910111213141516171819202122232425262728293031# Adapted from https://github.com/jik876/hifi-gan under the MIT license.# LICENSE is in incl_licenses directory.
import os
import torchfrom torch.nn.utils.parametrizations import weight_norm
def init_weights(m, mean=0.0, std=0.01): classname = m.__class__.__name__ if classname.find("Conv") != -1: m.weight.data.normal_(mean, std)
def apply_weight_norm(m): classname = m.__class__.__name__ if classname.find("Conv") != -1: weight_norm(m)
def get_padding(kernel_size, dilation=1): return int((kernel_size * dilation - dilation) / 2)
def load_checkpoint(filepath, device): assert os.path.isfile(filepath) print("Loading '{}'".format(filepath)) checkpoint_dict = torch.load(filepath, map_location=device) print("Complete.") return checkpoint_dict123456789101112131415161718192021MIT License
Copyright (c) 2024 NVIDIA CORPORATION.
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126# Implementation adapted from https://github.com/EdwardDixon/snake under the MIT license.# LICENSE is in incl_licenses directory.
import torchfrom torch import nn, sin, powfrom torch.nn import Parameter
class Snake(nn.Module): """ Implementation of a sine-based periodic activation function Shape: - Input: (B, C, T) - Output: (B, C, T), same shape as the input Parameters: - alpha - trainable parameter References: - This activation function is from this paper by Liu Ziyin, Tilman Hartwig, Masahito Ueda: https://arxiv.org/abs/2006.08195 Examples: >>> a1 = snake(256) >>> x = torch.randn(256) >>> x = a1(x) """
def __init__( self, in_features, alpha=1.0, alpha_trainable=True, alpha_logscale=False ): """ Initialization. INPUT: - in_features: shape of the input - alpha: trainable parameter alpha is initialized to 1 by default, higher values = higher-frequency. alpha will be trained along with the rest of your model. """ super(Snake, self).__init__() self.in_features = in_features
# Initialize alpha self.alpha_logscale = alpha_logscale if self.alpha_logscale: # Log scale alphas initialized to zeros self.alpha = Parameter(torch.zeros(in_features) * alpha) else: # Linear scale alphas initialized to ones self.alpha = Parameter(torch.ones(in_features) * alpha)
self.alpha.requires_grad = alpha_trainable
self.no_div_by_zero = 0.000000001
def forward(self, x): """ Forward pass of the function. Applies the function to the input elementwise. Snake ∶= x + 1/a * sin^2 (xa) """ alpha = self.alpha.unsqueeze(0).unsqueeze(-1) # Line up with x to [B, C, T] if self.alpha_logscale: alpha = torch.exp(alpha) x = x + (1.0 / (alpha + self.no_div_by_zero)) * pow(sin(x * alpha), 2)
return x
class SnakeBeta(nn.Module): """ A modified Snake function which uses separate parameters for the magnitude of the periodic components Shape: - Input: (B, C, T) - Output: (B, C, T), same shape as the input Parameters: - alpha - trainable parameter that controls frequency - beta - trainable parameter that controls magnitude References: - This activation function is a modified version based on this paper by Liu Ziyin, Tilman Hartwig, Masahito Ueda: https://arxiv.org/abs/2006.08195 Examples: >>> a1 = snakebeta(256) >>> x = torch.randn(256) >>> x = a1(x) """
def __init__( self, in_features, alpha=1.0, alpha_trainable=True, alpha_logscale=False ): """ Initialization. INPUT: - in_features: shape of the input - alpha - trainable parameter that controls frequency - beta - trainable parameter that controls magnitude alpha is initialized to 1 by default, higher values = higher-frequency. beta is initialized to 1 by default, higher values = higher-magnitude. alpha will be trained along with the rest of your model. """ super(SnakeBeta, self).__init__() self.in_features = in_features
# Initialize alpha self.alpha_logscale = alpha_logscale if self.alpha_logscale: # Log scale alphas initialized to zeros self.alpha = Parameter(torch.zeros(in_features) * alpha) self.beta = Parameter(torch.zeros(in_features) * alpha) else: # Linear scale alphas initialized to ones self.alpha = Parameter(torch.ones(in_features) * alpha) self.beta = Parameter(torch.ones(in_features) * alpha)
self.alpha.requires_grad = alpha_trainable self.beta.requires_grad = alpha_trainable
self.no_div_by_zero = 0.000000001
def forward(self, x): """ Forward pass of the function. Applies the function to the input elementwise. SnakeBeta ∶= x + 1/b * sin^2 (xa) """ alpha = self.alpha.unsqueeze(0).unsqueeze(-1) # Line up with x to [B, C, T] beta = self.beta.unsqueeze(0).unsqueeze(-1) if self.alpha_logscale: alpha = torch.exp(alpha) beta = torch.exp(beta) x = x + (1.0 / (beta + self.no_div_by_zero)) * pow(sin(x * alpha), 2)
return x1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677# Copyright (c) 2024 NVIDIA CORPORATION.# Licensed under the MIT license.
import torchimport torch.nn as nnfrom alias_free_activation.torch.resample import UpSample1d, DownSample1d
# load fused CUDA kernel: this enables importing anti_alias_activation_cudafrom alias_free_activation.cuda import load
anti_alias_activation_cuda = load.load()
class FusedAntiAliasActivation(torch.autograd.Function): """ Assumes filter size 12, replication padding on upsampling/downsampling, and logscale alpha/beta parameters as inputs. The hyperparameters are hard-coded in the kernel to maximize speed. NOTE: The fused kenrel is incorrect for Activation1d with different hyperparameters. """
@staticmethod def forward(ctx, inputs, up_ftr, down_ftr, alpha, beta): activation_results = anti_alias_activation_cuda.forward( inputs, up_ftr, down_ftr, alpha, beta )
return activation_results
@staticmethod def backward(ctx, output_grads): raise NotImplementedError return output_grads, None, None
class Activation1d(nn.Module): def __init__( self, activation, up_ratio: int = 2, down_ratio: int = 2, up_kernel_size: int = 12, down_kernel_size: int = 12, fused: bool = True, ): super().__init__() self.up_ratio = up_ratio self.down_ratio = down_ratio self.act = activation self.upsample = UpSample1d(up_ratio, up_kernel_size) self.downsample = DownSample1d(down_ratio, down_kernel_size)
self.fused = fused # Whether to use fused CUDA kernel or not
def forward(self, x): if not self.fused: x = self.upsample(x) x = self.act(x) x = self.downsample(x) return x else: if self.act.__class__.__name__ == "Snake": beta = self.act.alpha.data # Snake uses same params for alpha and beta else: beta = ( self.act.beta.data ) # Snakebeta uses different params for alpha and beta alpha = self.act.alpha.data if ( not self.act.alpha_logscale ): # Exp baked into cuda kernel, cancel it out with a log alpha = torch.log(alpha) beta = torch.log(beta)
x = FusedAntiAliasActivation.apply( x, self.upsample.filter, self.downsample.lowpass.filter, alpha, beta ) return x1234567891011121314151617181920212223/* coding=utf-8 * Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
#include <torch/extension.h>
extern "C" torch::Tensor fwd_cuda(torch::Tensor const &input, torch::Tensor const &up_filter, torch::Tensor const &down_filter, torch::Tensor const &alpha, torch::Tensor const &beta);
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { m.def("forward", &fwd_cuda, "Anti-Alias Activation forward (CUDA)");}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246/* coding=utf-8 * Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
#include <ATen/ATen.h>#include <cuda.h>#include <cuda_runtime.h>#include <cuda_fp16.h>#include <cuda_profiler_api.h>#include <ATen/cuda/CUDAContext.h>#include <torch/extension.h>#include "type_shim.h"#include <assert.h>#include <cfloat>#include <limits>#include <stdint.h>#include <c10/macros/Macros.h>
namespace{ // Hard-coded hyperparameters // WARP_SIZE and WARP_BATCH must match the return values batches_per_warp and constexpr int ELEMENTS_PER_LDG_STG = 1; //(WARP_ITERATIONS < 4) ? 1 : 4; constexpr int BUFFER_SIZE = 32; constexpr int FILTER_SIZE = 12; constexpr int HALF_FILTER_SIZE = 6; constexpr int UPSAMPLE_REPLICATION_PAD = 5; // 5 on each side, matching torch impl constexpr int DOWNSAMPLE_REPLICATION_PAD_LEFT = 5; // matching torch impl constexpr int DOWNSAMPLE_REPLICATION_PAD_RIGHT = 6; // matching torch impl
template <typename input_t, typename output_t, typename acc_t> __global__ void anti_alias_activation_forward( output_t *dst, const input_t *src, const input_t *up_ftr, const input_t *down_ftr, const input_t *alpha, const input_t *beta, int batch_size, int channels, int seq_len) { // Up and downsample filters input_t up_filter[FILTER_SIZE]; input_t down_filter[FILTER_SIZE];
// Load data from global memory including extra indices reserved for replication paddings input_t elements[2 * FILTER_SIZE + 2 * BUFFER_SIZE + 2 * UPSAMPLE_REPLICATION_PAD] = {0}; input_t intermediates[2 * FILTER_SIZE + 2 * BUFFER_SIZE + DOWNSAMPLE_REPLICATION_PAD_LEFT + DOWNSAMPLE_REPLICATION_PAD_RIGHT] = {0};
// Output stores downsampled output before writing to dst output_t output[BUFFER_SIZE];
// blockDim/threadIdx = (128, 1, 1) // gridDim/blockIdx = (seq_blocks, channels, batches) int block_offset = (blockIdx.x * 128 * BUFFER_SIZE + seq_len * (blockIdx.y + gridDim.y * blockIdx.z)); int local_offset = threadIdx.x * BUFFER_SIZE; int seq_offset = blockIdx.x * 128 * BUFFER_SIZE + local_offset;
// intermediate have double the seq_len int intermediate_local_offset = threadIdx.x * BUFFER_SIZE * 2; int intermediate_seq_offset = blockIdx.x * 128 * BUFFER_SIZE * 2 + intermediate_local_offset;
// Get values needed for replication padding before moving pointer const input_t *right_most_pntr = src + (seq_len * (blockIdx.y + gridDim.y * blockIdx.z)); input_t seq_left_most_value = right_most_pntr[0]; input_t seq_right_most_value = right_most_pntr[seq_len - 1];
// Move src and dst pointers src += block_offset + local_offset; dst += block_offset + local_offset;
// Alpha and beta values for snake activatons. Applies exp by default alpha = alpha + blockIdx.y; input_t alpha_val = expf(alpha[0]); beta = beta + blockIdx.y; input_t beta_val = expf(beta[0]);
#pragma unroll for (int it = 0; it < FILTER_SIZE; it += 1) { up_filter[it] = up_ftr[it]; down_filter[it] = down_ftr[it]; }
// Apply replication padding for upsampling, matching torch impl #pragma unroll for (int it = -HALF_FILTER_SIZE; it < BUFFER_SIZE + HALF_FILTER_SIZE; it += 1) { int element_index = seq_offset + it; // index for element if ((element_index < 0) && (element_index >= -UPSAMPLE_REPLICATION_PAD)) { elements[2 * (HALF_FILTER_SIZE + it)] = 2 * seq_left_most_value; } if ((element_index >= seq_len) && (element_index < seq_len + UPSAMPLE_REPLICATION_PAD)) { elements[2 * (HALF_FILTER_SIZE + it)] = 2 * seq_right_most_value; } if ((element_index >= 0) && (element_index < seq_len)) { elements[2 * (HALF_FILTER_SIZE + it)] = 2 * src[it]; } }
// Apply upsampling strided convolution and write to intermediates. It reserves DOWNSAMPLE_REPLICATION_PAD_LEFT for replication padding of the downsampilng conv later #pragma unroll for (int it = 0; it < (2 * BUFFER_SIZE + 2 * FILTER_SIZE); it += 1) { input_t acc = 0.0; int element_index = intermediate_seq_offset + it; // index for intermediate #pragma unroll for (int f_idx = 0; f_idx < FILTER_SIZE; f_idx += 1) { if ((element_index + f_idx) >= 0) { acc += up_filter[f_idx] * elements[it + f_idx]; } } intermediates[it + DOWNSAMPLE_REPLICATION_PAD_LEFT] = acc; }
// Apply activation function. It reserves DOWNSAMPLE_REPLICATION_PAD_LEFT and DOWNSAMPLE_REPLICATION_PAD_RIGHT for replication padding of the downsampilng conv later double no_div_by_zero = 0.000000001; #pragma unroll for (int it = 0; it < 2 * BUFFER_SIZE + 2 * FILTER_SIZE; it += 1) { intermediates[it + DOWNSAMPLE_REPLICATION_PAD_LEFT] += (1.0 / (beta_val + no_div_by_zero)) * sinf(intermediates[it + DOWNSAMPLE_REPLICATION_PAD_LEFT] * alpha_val) * sinf(intermediates[it + DOWNSAMPLE_REPLICATION_PAD_LEFT] * alpha_val); }
// Apply replication padding before downsampling conv from intermediates #pragma unroll for (int it = 0; it < DOWNSAMPLE_REPLICATION_PAD_LEFT; it += 1) { intermediates[it] = intermediates[DOWNSAMPLE_REPLICATION_PAD_LEFT]; } #pragma unroll for (int it = DOWNSAMPLE_REPLICATION_PAD_LEFT + 2 * BUFFER_SIZE + 2 * FILTER_SIZE; it < DOWNSAMPLE_REPLICATION_PAD_LEFT + 2 * BUFFER_SIZE + 2 * FILTER_SIZE + DOWNSAMPLE_REPLICATION_PAD_RIGHT; it += 1) { intermediates[it] = intermediates[DOWNSAMPLE_REPLICATION_PAD_LEFT + 2 * BUFFER_SIZE + 2 * FILTER_SIZE - 1]; }
// Apply downsample strided convolution (assuming stride=2) from intermediates #pragma unroll for (int it = 0; it < BUFFER_SIZE; it += 1) { input_t acc = 0.0; #pragma unroll for (int f_idx = 0; f_idx < FILTER_SIZE; f_idx += 1) { // Add constant DOWNSAMPLE_REPLICATION_PAD_RIGHT to match torch implementation acc += down_filter[f_idx] * intermediates[it * 2 + f_idx + DOWNSAMPLE_REPLICATION_PAD_RIGHT]; } output[it] = acc; }
// Write output to dst #pragma unroll for (int it = 0; it < BUFFER_SIZE; it += ELEMENTS_PER_LDG_STG) { int element_index = seq_offset + it; if (element_index < seq_len) { dst[it] = output[it]; } }
}
template <typename input_t, typename output_t, typename acc_t> void dispatch_anti_alias_activation_forward( output_t *dst, const input_t *src, const input_t *up_ftr, const input_t *down_ftr, const input_t *alpha, const input_t *beta, int batch_size, int channels, int seq_len) { if (seq_len == 0) { return; } else { // Use 128 threads per block to maximimize gpu utilization constexpr int threads_per_block = 128; constexpr int seq_len_per_block = 4096; int blocks_per_seq_len = (seq_len + seq_len_per_block - 1) / seq_len_per_block; dim3 blocks(blocks_per_seq_len, channels, batch_size); dim3 threads(threads_per_block, 1, 1);
anti_alias_activation_forward<input_t, output_t, acc_t> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, up_ftr, down_ftr, alpha, beta, batch_size, channels, seq_len); } }}
extern "C" torch::Tensor fwd_cuda(torch::Tensor const &input, torch::Tensor const &up_filter, torch::Tensor const &down_filter, torch::Tensor const &alpha, torch::Tensor const &beta){ // Input is a 3d tensor with dimensions [batches, channels, seq_len] const int batches = input.size(0); const int channels = input.size(1); const int seq_len = input.size(2);
// Output auto act_options = input.options().requires_grad(false);
torch::Tensor anti_alias_activation_results = torch::empty({batches, channels, seq_len}, act_options);
void *input_ptr = static_cast<void *>(input.data_ptr()); void *up_filter_ptr = static_cast<void *>(up_filter.data_ptr()); void *down_filter_ptr = static_cast<void *>(down_filter.data_ptr()); void *alpha_ptr = static_cast<void *>(alpha.data_ptr()); void *beta_ptr = static_cast<void *>(beta.data_ptr()); void *anti_alias_activation_results_ptr = static_cast<void *>(anti_alias_activation_results.data_ptr());
DISPATCH_FLOAT_HALF_AND_BFLOAT( input.scalar_type(), "dispatch anti alias activation_forward", dispatch_anti_alias_activation_forward<scalar_t, scalar_t, float>( reinterpret_cast<scalar_t *>(anti_alias_activation_results_ptr), reinterpret_cast<const scalar_t *>(input_ptr), reinterpret_cast<const scalar_t *>(up_filter_ptr), reinterpret_cast<const scalar_t *>(down_filter_ptr), reinterpret_cast<const scalar_t *>(alpha_ptr), reinterpret_cast<const scalar_t *>(beta_ptr), batches, channels, seq_len);); return anti_alias_activation_results;}1234567891011121314151617181920212223242526272829/* coding=utf-8 * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
/*This code is copied fron NVIDIA apex: * https://github.com/NVIDIA/apex * with minor changes. */
#ifndef TORCH_CHECK#define TORCH_CHECK AT_CHECK#endif
#ifdef VERSION_GE_1_3#define DATA_PTR data_ptr#else#define DATA_PTR data#endif1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586# Copyright (c) 2024 NVIDIA CORPORATION.# Licensed under the MIT license.
import osimport pathlibimport subprocess
from torch.utils import cpp_extension
"""Setting this param to a list has a problem of generating different compilation commands (with diferent order of architectures) and leading to recompilation of fused kernels. Set it to empty stringo avoid recompilation and assign arch flags explicity in extra_cuda_cflags below"""os.environ["TORCH_CUDA_ARCH_LIST"] = ""
def load(): # Check if cuda 11 is installed for compute capability 8.0 cc_flag = [] _, bare_metal_major, _ = _get_cuda_bare_metal_version(cpp_extension.CUDA_HOME) if int(bare_metal_major) >= 11: cc_flag.append("-gencode") cc_flag.append("arch=compute_80,code=sm_80")
# Build path srcpath = pathlib.Path(__file__).parent.absolute() buildpath = srcpath / "build" _create_build_dir(buildpath)
# Helper function to build the kernels. def _cpp_extention_load_helper(name, sources, extra_cuda_flags): return cpp_extension.load( name=name, sources=sources, build_directory=buildpath, extra_cflags=[ "-O3", ], extra_cuda_cflags=[ "-O3", "-gencode", "arch=compute_70,code=sm_70", "--use_fast_math", ] + extra_cuda_flags + cc_flag, verbose=True, )
extra_cuda_flags = [ "-U__CUDA_NO_HALF_OPERATORS__", "-U__CUDA_NO_HALF_CONVERSIONS__", "--expt-relaxed-constexpr", "--expt-extended-lambda", ]
sources = [ srcpath / "anti_alias_activation.cpp", srcpath / "anti_alias_activation_cuda.cu", ] anti_alias_activation_cuda = _cpp_extention_load_helper( "anti_alias_activation_cuda", sources, extra_cuda_flags )
return anti_alias_activation_cuda
def _get_cuda_bare_metal_version(cuda_dir): raw_output = subprocess.check_output( [cuda_dir + "/bin/nvcc", "-V"], universal_newlines=True ) output = raw_output.split() release_idx = output.index("release") + 1 release = output[release_idx].split(".") bare_metal_major = release[0] bare_metal_minor = release[1][0]
return raw_output, bare_metal_major, bare_metal_minor
def _create_build_dir(buildpath): try: os.mkdir(buildpath) except OSError: if not os.path.isdir(buildpath): print(f"Creation of the build directory {buildpath} failed")1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192/* coding=utf-8 * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
#include <ATen/ATen.h>#include "compat.h"
#define DISPATCH_FLOAT_HALF_AND_BFLOAT(TYPE, NAME, ...) \ switch (TYPE) \ { \ case at::ScalarType::Float: \ { \ using scalar_t = float; \ __VA_ARGS__; \ break; \ } \ case at::ScalarType::Half: \ { \ using scalar_t = at::Half; \ __VA_ARGS__; \ break; \ } \ case at::ScalarType::BFloat16: \ { \ using scalar_t = at::BFloat16; \ __VA_ARGS__; \ break; \ } \ default: \ AT_ERROR(#NAME, " not implemented for '", toString(TYPE), "'"); \ }
#define DISPATCH_FLOAT_HALF_AND_BFLOAT_INOUT_TYPES(TYPEIN, TYPEOUT, NAME, ...) \ switch (TYPEIN) \ { \ case at::ScalarType::Float: \ { \ using scalar_t_in = float; \ switch (TYPEOUT) \ { \ case at::ScalarType::Float: \ { \ using scalar_t_out = float; \ __VA_ARGS__; \ break; \ } \ case at::ScalarType::Half: \ { \ using scalar_t_out = at::Half; \ __VA_ARGS__; \ break; \ } \ case at::ScalarType::BFloat16: \ { \ using scalar_t_out = at::BFloat16; \ __VA_ARGS__; \ break; \ } \ default: \ AT_ERROR(#NAME, " not implemented for '", toString(TYPEOUT), "'"); \ } \ break; \ } \ case at::ScalarType::Half: \ { \ using scalar_t_in = at::Half; \ using scalar_t_out = at::Half; \ __VA_ARGS__; \ break; \ } \ case at::ScalarType::BFloat16: \ { \ using scalar_t_in = at::BFloat16; \ using scalar_t_out = at::BFloat16; \ __VA_ARGS__; \ break; \ } \ default: \ AT_ERROR(#NAME, " not implemented for '", toString(TYPEIN), "'"); \ }123456# Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0# LICENSE is in incl_licenses directory.
from .filter import *from .resample import *from .act import *1234567891011121314151617181920212223242526272829303132# Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0# LICENSE is in incl_licenses directory.
import torch.nn as nn
from mmaudio.ext.bigvgan_v2.alias_free_activation.torch.resample import (DownSample1d, UpSample1d)
class Activation1d(nn.Module):
def __init__( self, activation, up_ratio: int = 2, down_ratio: int = 2, up_kernel_size: int = 12, down_kernel_size: int = 12, ): super().__init__() self.up_ratio = up_ratio self.down_ratio = down_ratio self.act = activation self.upsample = UpSample1d(up_ratio, up_kernel_size) self.downsample = DownSample1d(down_ratio, down_kernel_size)
# x: [B,C,T] def forward(self, x): x = self.upsample(x) x = self.act(x) x = self.downsample(x)
return x123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101# Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0# LICENSE is in incl_licenses directory.
import torchimport torch.nn as nnimport torch.nn.functional as Fimport math
if "sinc" in dir(torch): sinc = torch.sincelse: # This code is adopted from adefossez's julius.core.sinc under the MIT License # https://adefossez.github.io/julius/julius/core.html # LICENSE is in incl_licenses directory. def sinc(x: torch.Tensor): """ Implementation of sinc, i.e. sin(pi * x) / (pi * x) __Warning__: Different to julius.sinc, the input is multiplied by `pi`! """ return torch.where( x == 0, torch.tensor(1.0, device=x.device, dtype=x.dtype), torch.sin(math.pi * x) / math.pi / x, )
# This code is adopted from adefossez's julius.lowpass.LowPassFilters under the MIT License# https://adefossez.github.io/julius/julius/lowpass.html# LICENSE is in incl_licenses directory.def kaiser_sinc_filter1d( cutoff, half_width, kernel_size): # return filter [1,1,kernel_size] even = kernel_size % 2 == 0 half_size = kernel_size // 2
# For kaiser window delta_f = 4 * half_width A = 2.285 * (half_size - 1) * math.pi * delta_f + 7.95 if A > 50.0: beta = 0.1102 * (A - 8.7) elif A >= 21.0: beta = 0.5842 * (A - 21) ** 0.4 + 0.07886 * (A - 21.0) else: beta = 0.0 window = torch.kaiser_window(kernel_size, beta=beta, periodic=False)
# ratio = 0.5/cutoff -> 2 * cutoff = 1 / ratio if even: time = torch.arange(-half_size, half_size) + 0.5 else: time = torch.arange(kernel_size) - half_size if cutoff == 0: filter_ = torch.zeros_like(time) else: filter_ = 2 * cutoff * window * sinc(2 * cutoff * time) """ Normalize filter to have sum = 1, otherwise we will have a small leakage of the constant component in the input signal. """ filter_ /= filter_.sum() filter = filter_.view(1, 1, kernel_size)
return filter
class LowPassFilter1d(nn.Module): def __init__( self, cutoff=0.5, half_width=0.6, stride: int = 1, padding: bool = True, padding_mode: str = "replicate", kernel_size: int = 12, ): """ kernel_size should be even number for stylegan3 setup, in this implementation, odd number is also possible. """ super().__init__() if cutoff < -0.0: raise ValueError("Minimum cutoff must be larger than zero.") if cutoff > 0.5: raise ValueError("A cutoff above 0.5 does not make sense.") self.kernel_size = kernel_size self.even = kernel_size % 2 == 0 self.pad_left = kernel_size // 2 - int(self.even) self.pad_right = kernel_size // 2 self.stride = stride self.padding = padding self.padding_mode = padding_mode filter = kaiser_sinc_filter1d(cutoff, half_width, kernel_size) self.register_buffer("filter", filter)
# Input [B, C, T] def forward(self, x): _, C, _ = x.shape
if self.padding: x = F.pad(x, (self.pad_left, self.pad_right), mode=self.padding_mode) out = F.conv1d(x, self.filter.expand(C, -1, -1), stride=self.stride, groups=C)
return out123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354# Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0# LICENSE is in incl_licenses directory.
import torch.nn as nnfrom torch.nn import functional as F
from mmaudio.ext.bigvgan_v2.alias_free_activation.torch.filter import (LowPassFilter1d, kaiser_sinc_filter1d)
class UpSample1d(nn.Module):
def __init__(self, ratio=2, kernel_size=None): super().__init__() self.ratio = ratio self.kernel_size = (int(6 * ratio // 2) * 2 if kernel_size is None else kernel_size) self.stride = ratio self.pad = self.kernel_size // ratio - 1 self.pad_left = self.pad * self.stride + (self.kernel_size - self.stride) // 2 self.pad_right = (self.pad * self.stride + (self.kernel_size - self.stride + 1) // 2) filter = kaiser_sinc_filter1d(cutoff=0.5 / ratio, half_width=0.6 / ratio, kernel_size=self.kernel_size) self.register_buffer("filter", filter)
# x: [B, C, T] def forward(self, x): _, C, _ = x.shape
x = F.pad(x, (self.pad, self.pad), mode="replicate") x = self.ratio * F.conv_transpose1d( x, self.filter.expand(C, -1, -1), stride=self.stride, groups=C) x = x[..., self.pad_left:-self.pad_right]
return x
class DownSample1d(nn.Module):
def __init__(self, ratio=2, kernel_size=None): super().__init__() self.ratio = ratio self.kernel_size = (int(6 * ratio // 2) * 2 if kernel_size is None else kernel_size) self.lowpass = LowPassFilter1d( cutoff=0.5 / ratio, half_width=0.6 / ratio, stride=ratio, kernel_size=self.kernel_size, )
def forward(self, x): xx = self.lowpass(x)
return xxLICENSEAdded+21
README.md+152-14
app.pyAdded+149
demo.pyAdded+135
docs/images/icon.pngAdded
docs/index.htmlAdded+147
docs/style.cssAdded+78
docs/style_videos.cssAdded+52
docs/video_gen.htmlAdded+254
docs/video_main.htmlAdded+98
docs/video_vgg.htmlAdded+452
mmaudio/__init__.pyAdded
mmaudio/eval_utils.pyAdded+245
mmaudio/ext/__init__.pyAdded+1
mmaudio/ext/autoencoder/__init__.pyAdded+1
mmaudio/ext/autoencoder/autoencoder.pyAdded+48
mmaudio/ext/autoencoder/edm2_utils.pyAdded+168
mmaudio/ext/autoencoder/vae.pyAdded+369
mmaudio/ext/autoencoder/vae_modules.pyAdded+117
mmaudio/ext/bigvgan/LICENSEAdded+21
mmaudio/ext/bigvgan/__init__.pyAdded+1
mmaudio/ext/bigvgan/activations.pyAdded+120
mmaudio/ext/bigvgan/alias_free_torch/__init__.pyAdded+6
mmaudio/ext/bigvgan/alias_free_torch/act.pyAdded+28
mmaudio/ext/bigvgan/alias_free_torch/filter.pyAdded+95
mmaudio/ext/bigvgan/alias_free_torch/resample.pyAdded+49
mmaudio/ext/bigvgan/bigvgan.pyAdded+32
mmaudio/ext/bigvgan/bigvgan_vocoder.ymlAdded+63
mmaudio/ext/bigvgan/env.pyAdded+18
mmaudio/ext/bigvgan/incl_licenses/LICENSE_1Added+21
mmaudio/ext/bigvgan/incl_licenses/LICENSE_2Added+21
mmaudio/ext/bigvgan/incl_licenses/LICENSE_3Added+201
mmaudio/ext/bigvgan/incl_licenses/LICENSE_4Added+29
mmaudio/ext/bigvgan/incl_licenses/LICENSE_5Added+16
mmaudio/ext/bigvgan/models.pyAdded+255
mmaudio/ext/bigvgan/utils.pyAdded+31
mmaudio/ext/bigvgan_v2/LICENSEAdded+21
mmaudio/ext/bigvgan_v2/__init__.pyAdded
mmaudio/ext/bigvgan_v2/activations.pyAdded+126
mmaudio/ext/bigvgan_v2/alias_free_activation/cuda/__init__.pyAdded
mmaudio/ext/bigvgan_v2/alias_free_activation/cuda/activation1d.pyAdded+77
mmaudio/ext/bigvgan_v2/alias_free_activation/cuda/anti_alias_activation.cppAdded+23
mmaudio/ext/bigvgan_v2/alias_free_activation/cuda/anti_alias_activation_cuda.cuAdded+246
mmaudio/ext/bigvgan_v2/alias_free_activation/cuda/compat.hAdded+29
mmaudio/ext/bigvgan_v2/alias_free_activation/cuda/load.pyAdded+86
mmaudio/ext/bigvgan_v2/alias_free_activation/cuda/type_shim.hAdded+92
mmaudio/ext/bigvgan_v2/alias_free_activation/torch/__init__.pyAdded+6
mmaudio/ext/bigvgan_v2/alias_free_activation/torch/act.pyAdded+32
mmaudio/ext/bigvgan_v2/alias_free_activation/torch/filter.pyAdded+101
mmaudio/ext/bigvgan_v2/alias_free_activation/torch/resample.pyAdded+54