Spaces:
Runtime error
Runtime error
Major Update
Browse files-metadata
-new models
-background and style
-readme
-requirements
-gradio extension
This view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- .gitattributes+2
- CHANGELOG.md+61-12
- README.md+48-7
- app.py+270-202
- assets/KuritaSurnLogox64.pngAdded
- assets/Vermilion-Musical-Notes-Typography-No-Background.svgAdded
- assets/a_duck_quacking_as_birds_chirp_and_a_pigeon_cooing.mp3Added
- assets/icon_delete.pngAdded
- assets/icon_download.pngAdded
- assets/icon_refresh.pngAdded
- assets/logo_animation_256.gifAdded
- assets/screenshot.pngAdded
- assets/sirens_and_a_humming_engine_approach_and_pass.mp3Added
- audiocraft/__init__.py+1-1
- audiocraft/data/__init__.py+1-1
- audiocraft/data/audio.py+86-1
- audiocraft/data/audio_dataset.py+93-31
- audiocraft/data/audio_utils.py+9-6
- audiocraft/data/info_audio_dataset.pyAdded+110
- audiocraft/data/zip.py+8-6
- audiocraft/environment.pyAdded+176
- audiocraft/models/__init__.py+7
- audiocraft/models/builders.py+89-49
- audiocraft/models/encodec.py+267-63
- audiocraft/models/lm.py+35-29
- audiocraft/models/loaders.py+146-5
- audiocraft/models/musicgen.py+108-34
- audiocraft/models/unet.pyAdded+214
- audiocraft/modules/__init__.py+1
- audiocraft/modules/chroma.pyAdded+66
- audiocraft/modules/codebooks_patterns.py+17-12
- audiocraft/modules/conditioners.py+724-298
- audiocraft/modules/conv.py+1-1
- audiocraft/modules/diffusion_schedule.pyAdded+272
- audiocraft/modules/rope.py+20-19
- audiocraft/modules/transformer.py+36-38
- audiocraft/quantization/core_vq.py+8-3
- audiocraft/utils/cache.pyAdded+324
- audiocraft/utils/cluster.pyAdded+75
- audiocraft/utils/export.py+50-27
- audiocraft/utils/export_legacy.pyAdded+56
- audiocraft/utils/extend.py+5-4
- audiocraft/utils/utils.py+68-2
- modules/file_utils.pyAdded+91
- modules/gradio.pyAdded+272
- modules/user_history.pyAdded+598
- modules/version_info.pyAdded+123
- pre-requirements.txt+1-1
- requirements.txt+28-12
- style_20250331.cssAdded+215
31 unmodified lines32333431 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text31 unmodified lines323334353631 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textassets/logo_animation_256.gif filter=lfs diff=lfs merge=lfs -textassets/screenshot.png filter=lfs diff=lfs merge=lfs -text
1234567891011121310 unmodified lines24252627282930313233## [0.0.2a2] - 2023-07-20Music Generation set to a max of 720 seconds (12 minutes) to avoid memory issues.Video editing options (thanks @Surn and @oncorporation).Music Conditioning segment options## [0.0.2a] - TBDImproved demo, fixed top p (thanks @jnordberg).10 unmodified lines## [0.0.1] - 2023-06-09Initial release, with model evaluation only.# ChangelogAll notable changes to this project will be documented in this file.The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686910 unmodified lines808182# ChangelogAll notable changes to this project will be documented in this file.## [1.2.Surn] - 2025-04-02Implemented Unlimited Music Generation (UMG) with the [hf checkpoints](https://huggingface.co/facebook/unlimited-music-generation).## [1.4.0a2] - 2025-01-14Add training and inference code for JASCO (https://arxiv.org/abs/2406.10970) along with the [hf checkpoints](https://huggingface.co/facebook/jasco-chords-drums-melody-1B).## [1.4.0a1] - 2024-06-03Adding new metric PesqMetric ([Perceptual Evaluation of Speech Quality](https://doi.org/10.5281/zenodo.6549559))Adding multiple audio augmentation functions: generating pink noises, up-/downsampling, low-/highpass filtering, banpass filtering, smoothing, duck masking, boosting. All are wrapped in the `audiocraft.utils.audio_effects.AudioEffects` and can be called with the API `audiocraft.utils.audio_effects.select_audio_effects`.Add training code for AudioSeal (https://arxiv.org/abs/2401.17264) along with the [hf checkpoints]( https://huggingface.co/facebook/audioseal).## [1.3.0] - 2024-05-02Adding the MAGNeT model (https://arxiv.org/abs/2401.04577) along with hf checkpoints and a gradio demo app.Typo fixes.Fixing setup.py to install only audiocraft, not the unit tests and scripts.Fix FSDP support with PyTorch 2.1.0.## [1.2.0] - 2024-01-11Adding stereo models.Fixed the commitment loss, which was until now only applied to the first RVQ layer.Removed compression model state from the LM checkpoints, for consistency, itshould always be loaded from the original `compression_model_checkpoint`.## [1.1.0] - 2023-11-06Not using torchaudio anymore when writing audio files, relying instead directly on the commandline ffmpeg. Also not using it anymore for reading audio files, for similar reasons.Fixed DAC support with non default number of codebooks.Fixed bug when `two_step_cfg` was overriden when calling `generate()`.Fixed samples being always prompted with audio, rather than having both prompted and unprompted.**Backward incompatible change:** A `torch.no_grad` around the computation of the conditioning made its way in the public release.The released models were trained without this. Those impact linear layers applied to the output of the T5 or melody conditioners.We removed it, so you might need to retrain models.**Backward incompatible change:** Fixing wrong sample rate in CLAP (WARNING if you trained model with CLAP before).**Backward incompatible change:** Renamed VALLEPattern to CoarseFirstPattern, as it was wrongly named. Probably no oneretrained a model with this pattern, so hopefully this won't impact you!## [1.0.0] - 2023-09-07Major revision, added training code for EnCodec, AudioGen, MusicGen, and MultiBandDiffusion.Added pretrained model for AudioGen and MultiBandDiffusion.## [0.0.2] - 2023-08-01Improved demo, fixed top p (thanks @jnordberg).10 unmodified lines## [0.0.1] - 2023-06-09Initial release, with model evaluation only.
3 unmodified lines456789101112131415161 unmodified line1819202122232421 unmodified lines4647484950515253545556572 unmodified lines60616263646566111 unmodified lines17817918018118218328 unmodified lines2122132142153 unmodified linescolorFrom: graycolorTo: redsdk: gradiosdk_version: 3.38.0app_file: app.pypinned: falselicense: creativeml-openrail-mtags:- musicgen- unlimited---[arxiv]: https://arxiv.org/abs/2306.052841 unmodified lineCheck out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference# UnlimitedMusicGenThis is my modification of the Audiocraft project to enable unlimited Audio generation. I have added a few features to the original project to enable this. I have also added a few features to the gradio interface to make it easier to use.# Audiocraft21 unmodified linesWe use 20K hours of licensed music to train MusicGen. Specifically, we rely on an internal dataset of 10K high-quality music tracks, and on the ShutterStock and Pond5 music data.## InstallationAudiocraft requires Python 3.9, PyTorch 2.0.0, and a GPU with at least 16 GB of memory (for the medium-sized model). To install Audiocraft, you can run the following:```shell# Best to make sure you have torch installed first, in particular before installing xformers.# Don't run this if you already have PyTorch installed.pip install 'torch>=2.0'# Then proceed to one of the followingpip install -U audiocraft # stable releasepip install -U git+https://git@github.com/facebookresearch/audiocraft#egg=audiocraft # bleeding edge2 unmodified lines## UsageWe offer a number of way to interact with MusicGen:1. A demo is also available on the [`facebook/MusicGen` HuggingFace Space](https://huggingface.co/spaces/facebook/MusicGen) (huge thanks to all the HF team for their support).2. You can run the Gradio demo in Colab: [colab notebook](https://colab.research.google.com/drive/1-Xe9NCdIs2sCUbiSmwHXozK6AAhMm7_i?usp=sharing).3. You can use the gradio demo locally by running `python app.py`.4. You can play with MusicGen by running the jupyter notebook at [`demo.ipynb`](./demo.ipynb) locally (if you have a GPU).111 unmodified lines[MusicGen docs](https://huggingface.co/docs/transformers/main/en/model_doc/musicgen) or the hands-on[Google Colab](https://colab.research.google.com/github/sanchit-gandhi/notebooks/blob/main/MusicGen.ipynb).## Model Card28 unmodified lines* The code in this repository is released under the MIT license as found in the [LICENSE file](LICENSE).* The weights in this repository are released under the CC-BY-NC 4.0 license as found in the [LICENSE_weights file](LICENSE_weights).[arxiv]: https://arxiv.org/abs/2306.05284[musicgen_samples]: https://ai.honu.io/papers/musicgen/3 unmodified lines4567891011121314151617181920212223241 unmodified line26272829303132333435363738394041424321 unmodified lines6566676869707172737475762 unmodified lines79808182838485111 unmodified lines19719819920020120220320420520620720820921021121221321421521621721821922022128 unmodified lines2502512522532542552563 unmodified linescolorFrom: graycolorTo: redsdk: gradiosdk_version: 5.23.3python_version: 3.12.8app_file: app.pypinned: truelicense: creativeml-openrail-mtags:- musicgen- unlimited- user history- metadatahf_oauth: truedisable_embedding: trueshort_description: 'unlimited Audio generation with a few added features 'thumbnail: >-https://cdn-uploads.huggingface.co/production/uploads/6346595c9e5f0fe83fc60444/Z8E8OaKV84zuVAvvGpMDJ.png---[arxiv]: https://arxiv.org/abs/2306.052841 unmodified lineCheck out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference# UnlimitedMusicGenCharles Fettinger's modification of the Audiocraft project to enable unlimited Audio generation. I have added a few features to the original project to enable this. I have also added a few features to the gradio interface to make it easier to use.Please review my other AI relalated spaces at https://huggingface.co/SurnCheck your video's generative metadata with https://mediaarea.net/en/MediaInfoAlso note that I wrote an extension to Gradio for the waveform in the video after v4.48.0 removed it.The key update here is in the extend utility. We segment melody input and then condition the next segment with current tensors and tensors from the current time in the conditioning melody file.This allows us to follow the same arraingement of the original melody.**Thank you Huggingface for the community grant to run this project**!!# Audiocraft21 unmodified linesWe use 20K hours of licensed music to train MusicGen. Specifically, we rely on an internal dataset of 10K high-quality music tracks, and on the ShutterStock and Pond5 music data.## InstallationAudiocraft requires Python 3.9, PyTorch 2.1.0, and a GPU with at least 16 GB of memory (for the medium-sized model). To install Audiocraft, you can run the following:```shell# Best to make sure you have torch installed first, in particular before installing xformers.# Don't run this if you already have PyTorch installed.pip install 'torch>=2.1'# Then proceed to one of the followingpip install -U audiocraft # stable releasepip install -U git+https://git@github.com/facebookresearch/audiocraft#egg=audiocraft # bleeding edge2 unmodified lines## UsageWe offer a number of way to interact with MusicGen:1. A demo is also available on the [`facebook/MusicGen` HuggingFace Space](https://huggingface.co/spaces/Surn/UnlimitedMusicGen) (huge thanks to all the HF team for their support).2. You can run the Gradio demo in Colab: [colab notebook](https://colab.research.google.com/drive/1-Xe9NCdIs2sCUbiSmwHXozK6AAhMm7_i?usp=sharing).3. You can use the gradio demo locally by running `python app.py`.4. You can play with MusicGen by running the jupyter notebook at [`demo.ipynb`](./demo.ipynb) locally (if you have a GPU).111 unmodified lines[MusicGen docs](https://huggingface.co/docs/transformers/main/en/model_doc/musicgen) or the hands-on[Google Colab](https://colab.research.google.com/github/sanchit-gandhi/notebooks/blob/main/MusicGen.ipynb).## User HistoryUser History is a plugin that you can add to your Spaces to cache generated images for your users.Key features:- 🤗 Sign in with Hugging Face- Save generated image, video, audio and document files with their metadata: prompts, timestamp, hyper-parameters, etc.- Export your history as zip.- Delete your history to respect privacy.- Compatible with Persistent Storage for long-term storage.- Admin panel to check configuration and disk usage .Useful links:- Demo: https://huggingface.co/spaces/Wauplin/gradio-user-history- README: https://huggingface.co/spaces/Wauplin/gradio-user-history/blob/main/README.md- Source file: https://huggingface.co/spaces/Wauplin/gradio-user-history/blob/main/user_history.py- Discussions: https://huggingface.co/spaces/Wauplin/gradio-user-history/discussions## Model Card28 unmodified lines* The code in this repository is released under the MIT license as found in the [LICENSE file](LICENSE).* The weights in this repository are released under the CC-BY-NC 4.0 license as found in the [LICENSE_weights file](LICENSE_weights).[arxiv]: https://arxiv.org/abs/2306.05284[arxiv]: https://arxiv.org/abs/2306.05284[musicgen_samples]: https://ai.honu.io/papers/musicgen/Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
20 unmodified lines21222324252627282930313 unmodified lines3536373839404130 unmodified lines727374757677781 unmodified line80818283848516 unmodified lines1021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321336 unmodified lines14014114214314414514614714814915015115215315415515615715815916016116216316416516616719 unmodified lines1871881891901911921931941951961971982 unmodified lines20120220320420520620720820921021121221321421521621733 unmodified lines2512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842851 unmodified line28728828929029129219 unmodified lines3123133143153163173182 unmodified lines3213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624637 unmodified lines47147247347447547647747847948037 unmodified lines5185195205215225235243 unmodified lines5285295305315325335343 unmodified lines53853954054154254320 unmodified linesfrom audiocraft.data.audio import audio_writefrom audiocraft.data.audio_utils import apply_fade, apply_tafade, apply_splice_effectfrom audiocraft.utils.extend import generate_music_segments, add_settings_to_image, INTERRUPTINGimport numpy as npimport random#from pathlib import Path#from typing import List, Unionimport librosaMODEL = NoneMODELS = None3 unmodified linesMOVE_TO_CPU = FalseMAX_PROMPT_INDEX = 0git = os.environ.get('GIT', "git")os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"def interrupt_callback():return INTERRUPTED30 unmodified lineselse:return gr.update(source="upload", value=None, label="File")def make_waveform(*args, **kwargs):# Further remove some warnings.be = time.time()with warnings.catch_warnings():1 unmodified lineout = gr.make_waveform(*args, **kwargs)print("Make a video took", time.time() - be)return outdef load_model(version):global MODEL, MODELS, UNLOAD_MODEL16 unmodified linesprint("Cached model loaded in %.2fs" % (time.monotonic() - t1))return resultdef get_filename(file):# extract filename from file objectfilename = Noneif file is not None:filename = file.namereturn filenamedef get_filename_from_filepath(filepath):file_name = os.path.basename(filepath)file_base, file_extension = os.path.splitext(file_name)return file_base, file_extensiondef get_melody(melody_filepath):audio_data= list(librosa.load(melody_filepath, sr=None))audio_data[0], audio_data[1] = audio_data[1], audio_data[0]melody = tuple(audio_data)return melodydef commit_hash():try:return subprocess.check_output([git, "rev-parse", "HEAD"], shell=False, encoding='utf8').strip()except Exception:return "<none>"def git_tag():try:return subprocess.check_output([git, "describe", "--tags"], shell=False, encoding='utf8').strip()6 unmodified linesexcept Exception:return "<none>"def versions_html():import torchpython_version = ".".join([str(x) for x in sys.version_info[0:3]])commit = commit_hash()#tag = git_tag()import xformersxformers_version = xformers.__version__return f"""version: <a href="https://github.com/Oncorporation/audiocraft/commit/{"huggingface" if commit == "<none>" else commit}" target="_blank">{"huggingface" if commit == "<none>" else commit}</a> • python: <span title="{sys.version}">{python_version}</span> • torch: {getattr(torch, '__long_version__',torch.__version__)} • xformers: {xformers_version} • gradio: {gr.__version__}"""def load_melody_filepath(melody_filepath, title):# get melody filename#$Union[str, os.PathLike]19 unmodified linesprint(f"Melody length: {len(melody_data)}, Melody segments: {total_melodys}\n")MAX_PROMPT_INDEX = total_melodysreturn gr.Textbox.update(value=melody_name), gr.update(maximum=MAX_PROMPT_INDEX, value=0), gr.update(value="melody-large", interactive=True)def predict(model, text, melody_filepath, duration, dimension, topk, topp, temperature, cfg_coef, background, title, settings_font, settings_font_color, seed, overlap=1, prompt_index = 0, include_title = True, include_settings = True, harmony_only = False):global MODEL, INTERRUPTED, INTERRUPTING, MOVE_TO_CPUoutput_segments = Nonemelody_name = "Not Used"melody = Noneif melody_filepath:melody_name, melody_extension = get_filename_from_filepath(melody_filepath)2 unmodified linesINTERRUPTED = FalseINTERRUPTING = Falseif temperature < 0:raise gr.Error("Temperature must be >= 0.")if topk < 0:raise gr.Error("Topk must be non-negative.")if topp < 0:raise gr.Error("Topp must be non-negative.")if MODEL is None or MODEL.name != model:MODEL = load_model(model)else:if MOVE_TO_CPU:MODEL.to('cuda')# prevent hackingduration = min(duration, 720)33 unmodified linesrep_penalty=0.5)if melody:# todo return excess duration, load next model and continue in loop structure building up output_segmentsif duration > MODEL.lm.cfg.dataset.segment_duration:output_segments, duration = generate_music_segments(text, melody, seed, MODEL, duration, overlap, MODEL.lm.cfg.dataset.segment_duration, prompt_index, harmony_only=False)else:# pure original codesr, melody = melody[0], torch.from_numpy(melody[1]).to(MODEL.device).float().t().unsqueeze(0)print(melody.shape)if melody.dim() == 2:melody = melody[None]melody = melody[..., :int(sr * MODEL.lm.cfg.dataset.segment_duration)]output = MODEL.generate_with_chroma(descriptions=[text],melody_wavs=melody,melody_sample_rate=sr,progress=False)# All output_segments are populated, so we can break the loop or set duration to 0breakelse:#output = MODEL.generate(descriptions=[text], progress=False)if not output_segments:next_segment = MODEL.generate(descriptions=[text], progress=False)duration -= segment_durationelse:last_chunk = output_segments[-1][:, :, -overlap*MODEL.sample_rate:]next_segment = MODEL.generate_continuation(last_chunk, MODEL.sample_rate, descriptions=[text], progress=False)duration -= segment_duration - overlapoutput_segments.append(next_segment)if INTERRUPTING:INTERRUPTED = True1 unmodified lineprint("Function execution interrupted!")raise gr.Error("Interrupted.")if output_segments:try:# Combine the output segments into one long audio file or stack tracks19 unmodified lines##overlapping_output = torch.cat([output[:, :, -overlap_samples:], output_segments[i][:, :, :overlap_samples]], dim=1) #stack tracks##print(f" overlap size stack:{overlapping_output.size()}\n output: {output.size()}\n segment: {output_segments[i].size()}")#overlapping_output = torch.cat([output[:, :, -overlap_samples:], output_segments[i][:, :, :overlap_samples]], dim=2) #stack tracks#print(f" overlap size cat:{overlapping_output.size()}\n output: {output.size()}\n segment: {output_segments[i].size()}")output = torch.cat([output[:, :, :-overlap_samples], overlapping_output, output_segments[i][:, :, overlap_samples:]], dim=dimension)else:output = torch.cat([output, output_segments[i]], dim=dimension)2 unmodified linesprint(f"Error combining segments: {e}. Using the first segment only.")output = output_segments[0].detach().cpu().float()[0]else:output = output.detach().cpu().float()[0]with NamedTemporaryFile("wb", suffix=".wav", delete=False) as file:video_description = f"{text}\n Duration: {str(initial_duration)} Dimension: {dimension}\n Top-k:{topk} Top-p:{topp}\n Randomness:{temperature}\n cfg:{cfg_coef} overlap: {overlap}\n Seed: {seed}\n Model: {model}\n Melody Condition:{melody_name}\n Sample Segment: {prompt_index}"if include_settings or include_title:background = add_settings_to_image(title if include_title else "", video_description if include_settings else "", background_path=background, font=settings_font, font_color=settings_font_color)audio_write(file.name, output, MODEL.sample_rate, strategy="loudness",loudness_headroom_db=18, loudness_compressor=True, add_suffix=False, channels=2)waveform_video = make_waveform(file.name,bg_image=background, bar_count=45)if MOVE_TO_CPU:MODEL.to('cpu')if UNLOAD_MODEL:MODEL = Nonetorch.cuda.empty_cache()torch.cuda.ipc_collect()return waveform_video, file.name, seeddef ui(**kwargs):css="""#col-container {max-width: 910px; margin-left: auto; margin-right: auto;}a {text-decoration-line: underline; font-weight: 600;}#btn-generate {background-image:linear-gradient(to right bottom, rgb(157, 255, 157), rgb(229, 255, 235));}#btn-generate:hover {background-image:linear-gradient(to right bottom, rgb(229, 255, 229), rgb(255, 255, 255));}#btn-generate:active {background-image:linear-gradient(to right bottom, rgb(229, 255, 235), rgb(157, 255, 157));}#versions {margin-top: 1em; width:100%; text-align:center;}.small-btn {max-width:75px;}"""with gr.Blocks(title="UnlimitedMusicGen", css=css) as demo:gr.Markdown("""# UnlimitedMusicGenThis is your private demo for [UnlimitedMusicGen](https://github.com/Oncorporation/audiocraft), a simple and controllable model for music generationpresented at: ["Simple and Controllable Music Generation"](https://huggingface.co/papers/2306.05284)Disclaimer: This won't run on CPU only. Clone this App and run on GPU instance!Todo: Working on improved Interrupt and new Models.""")if IS_SHARED_SPACE and not torch.cuda.is_available():gr.Markdown("""⚠ This Space doesn't work in this shared UI ⚠<a href="https://huggingface.co/spaces/musicgen/MusicGen?duplicate=true" style="display: inline-block;margin-top: .5em;margin-right: .25em;" target="_blank"><img style="margin-bottom: 0em;display: inline;margin-top: -.25em;" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>to use it privately, or use the <a href="https://huggingface.co/spaces/facebook/MusicGen">public demo</a>""")with gr.Row():with gr.Column():with gr.Row():text = gr.Text(label="Describe your music", interactive=True, value="4/4 100bpm 320kbps 48khz, Industrial/Electronic Soundtrack, Dark, Intense, Sci-Fi")with gr.Column():duration = gr.Slider(minimum=1, maximum=720, value=10, label="Duration (s)", interactive=True)model = gr.Radio(["melody", "medium", "small", "large", "melody-large", "stereo-melody", "stereo-medium", "stereo-small", "stereo-large", "stereo-melody-large"], label="AI Model", value="melody-large", interactive=True)with gr.Row():submit = gr.Button("Generate", elem_id="btn-generate")# Adapted from https://github.com/rkfg/audiocraft/blob/long/app.py, MIT license._ = gr.Button("Interrupt", elem_id="btn-interrupt").click(fn=interrupt, queue=False)with gr.Row():with gr.Column():radio = gr.Radio(["file", "mic"], value="file", label="Condition on a melody (optional) File or Mic")melody_filepath = gr.Audio(source="upload", type="filepath", label="Melody Condition (optional)", interactive=True, elem_id="melody-input")with gr.Column():harmony_only = gr.Radio(label="Use Harmony Only",choices=["No", "Yes"], value="No", interactive=True, info="Remove Drums?")prompt_index = gr.Slider(label="Melody Condition Sample Segment", minimum=-1, maximum=MAX_PROMPT_INDEX, step=1, value=0, interactive=True, info="Which 30 second segment to condition with, - 1 condition each segment independantly")with gr.Accordion("Video", open=False):with gr.Row():background= gr.Image(value="./assets/background.png", source="upload", label="Background", shape=(768,512), type="filepath", interactive=True)with gr.Column():include_title = gr.Checkbox(label="Add Title", value=True, interactive=True)include_settings = gr.Checkbox(label="Add Settings to background", value=True, interactive=True)with gr.Row():title = gr.Textbox(label="Title", value="UnlimitedMusicGen", interactive=True)settings_font = gr.Text(label="Settings Font", value="./assets/arial.ttf", interactive=True)settings_font_color = gr.ColorPicker(label="Settings Font Color", value="#c87f05", interactive=True)with gr.Accordion("Expert", open=False):with gr.Row():overlap = gr.Slider(minimum=0, maximum=15, value=2, step=1, label="Verse Overlap", interactive=True)dimension = gr.Slider(minimum=-2, maximum=2, value=2, step=1, label="Dimension", info="determines which direction to add new segements of audio. (1 = stack tracks, 2 = lengthen, -2..0 = ?)", interactive=True)with gr.Row():topk = gr.Number(label="Top-k", value=280, precision=0, interactive=True)topp = gr.Number(label="Top-p", value=1150, precision=0, interactive=True)temperature = gr.Number(label="Randomness Temperature", value=0.7, precision=None, interactive=True)cfg_coef = gr.Number(label="Classifier Free Guidance", value=8.5, precision=None, interactive=True)with gr.Row():seed = gr.Number(label="Seed", value=-1, precision=0, interactive=True)gr.Button('\U0001f3b2\ufe0f', elem_classes="small-btn").click(fn=lambda: -1, outputs=[seed], queue=False)reuse_seed = gr.Button('\u267b\ufe0f', elem_classes="small-btn")with gr.Column() as c:output = gr.Video(label="Generated Music")wave_file = gr.File(label=".wav file", elem_id="output_wavefile", interactive=True)seed_used = gr.Number(label='Seed used', value=-1, interactive=False)radio.change(toggle_audio_src, radio, [melody_filepath], queue=False, show_progress=False)melody_filepath.change(load_melody_filepath, inputs=[melody_filepath, title], outputs=[title, prompt_index , model], api_name="melody_filepath_change", queue=False)reuse_seed.click(fn=lambda x: x, inputs=[seed_used], outputs=[seed], queue=False, api_name="reuse_seed")submit.click(predict, inputs=[model, text,melody_filepath, duration, dimension, topk, topp, temperature, cfg_coef, background, title, settings_font, settings_font_color, seed, overlap, prompt_index, include_title, include_settings, harmony_only], outputs=[output, wave_file, seed_used], api_name="submit")gr.Examples(fn=predict,examples=[["4/4 120bpm 320kbps 48khz, An 80s driving pop song with heavy drums and synth pads in the background","./assets/bach.mp3","melody","80s Pop Synth"],["4/4 120bpm 320kbps 48khz, A cheerful country song with acoustic guitars","./assets/bolero_ravel.mp3","melody","Country Guitar"],["4/4 120bpm 320kbps 48khz, 90s rock song with electric guitar and heavy drums",None,"medium","90s Rock Guitar"],["4/4 120bpm 320kbps 48khz, a light and cheerly EDM track, with syncopated drums, aery pads, and strong emotions","./assets/bach.mp3","melody","EDM my Bach"],["4/4 320kbps 48khz, lofi slow bpm electro chill with organic samples",None,"medium","LoFi Chill"],],inputs=[text, melody_filepath, model, title],outputs=[output])gr.HTML(value=versions_html(), visible=True, elem_id="versions")# Show the interfacelaunch_kwargs = {}share = kwargs.get('share', False)7 unmodified linesif share:launch_kwargs['share'] = sharelaunch_kwargs['favicon_path']= "./assets/favicon.ico"demo.queue(max_size=10, concurrency_count=1, api_open=False).launch(**launch_kwargs)if __name__ == "__main__":parser = argparse.ArgumentParser()37 unmodified linesargs = parser.parse_args()launch_kwargs = {}launch_kwargs['server_name'] = args.listenif args.username and args.password:launch_kwargs['auth'] = (args.username, args.password)3 unmodified lineslaunch_kwargs['inbrowser'] = args.inbrowserif args.share:launch_kwargs['share'] = args.sharelaunch_kwargs['favicon_path']= "./assets/favicon.ico"UNLOAD_MODEL = args.unload_model3 unmodified linesui(unload_to_cpu = MOVE_TO_CPU,share=args.share)20 unmodified lines21222324252627282930313233343536373 unmodified lines41424344454647484950515230 unmodified lines838485868788891 unmodified line9192939495969716 unmodified lines1141151161171181191201211221231241256 unmodified lines13213313413513613719 unmodified lines1571581591601611621631641651661671681692 unmodified lines17217317417517617717817918018118218318418518618718818919019119219319433 unmodified lines2282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672681 unmodified line27027127227327427527619 unmodified lines2962972982993003013022 unmodified lines3053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305317 unmodified lines53954054154254354454554654754837 unmodified lines5865875885895905915923 unmodified lines5965975985996006016023 unmodified lines60660760860961061120 unmodified linesfrom audiocraft.data.audio import audio_writefrom audiocraft.data.audio_utils import apply_fade, apply_tafade, apply_splice_effectfrom audiocraft.utils.extend import generate_music_segments, add_settings_to_image, INTERRUPTINGfrom audiocraft.utils import utilsimport numpy as npimport randomimport shutilfrom mutagen.mp4 import MP4#from typing import List, Unionimport librosaimport modules.user_historyfrom modules.version_info import versions_html, commit_hash, get_xformers_versionfrom modules.gradio import *from modules.file_utils import get_file_parts, get_filename_from_filepath, convert_title_to_filename, get_filename, delete_fileMODEL = NoneMODELS = None3 unmodified linesMOVE_TO_CPU = FalseMAX_PROMPT_INDEX = 0git = os.environ.get('GIT', "git")#s.environ["CUDA_LAUNCH_BLOCKING"] = "1"os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128,expandable_segments:True"os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'os.environ['CUDA_MODULE_LOADING']='LAZY'os.environ['USE_FLASH_ATTENTION'] = '1'os.environ['XFORMERS_FORCE_DISABLE_TRITON']= '1'def interrupt_callback():return INTERRUPTED30 unmodified lineselse:return gr.update(source="upload", value=None, label="File")def get_waveform(*args, **kwargs):# Further remove some warnings.be = time.time()with warnings.catch_warnings():1 unmodified lineout = gr.make_waveform(*args, **kwargs)print("Make a video took", time.time() - be)return outdef load_model(version):global MODEL, MODELS, UNLOAD_MODEL16 unmodified linesprint("Cached model loaded in %.2fs" % (time.monotonic() - t1))return resultdef get_melody(melody_filepath):audio_data= list(librosa.load(melody_filepath, sr=None))audio_data[0], audio_data[1] = audio_data[1], audio_data[0]melody = tuple(audio_data)return melodydef git_tag():try:return subprocess.check_output([git, "describe", "--tags"], shell=False, encoding='utf8').strip()6 unmodified linesexcept Exception:return "<none>"def load_melody_filepath(melody_filepath, title):# get melody filename#$Union[str, os.PathLike]19 unmodified linesprint(f"Melody length: {len(melody_data)}, Melody segments: {total_melodys}\n")MAX_PROMPT_INDEX = total_melodysreturn gr.update(value=melody_name), gr.update(maximum=MAX_PROMPT_INDEX, value=0), gr.update(value="melody", interactive=True)def predict(model, text, melody_filepath, duration, dimension, topk, topp, temperature, cfg_coef, background, title, settings_font, settings_font_color, seed, overlap=1, prompt_index = 0, include_title = True, include_settings = True, harmony_only = False):global MODEL, INTERRUPTED, INTERRUPTING, MOVE_TO_CPUoutput_segments = Nonemelody_name = "Not Used"melody_extension = "Not Used"melody = Noneif melody_filepath:melody_name, melody_extension = get_filename_from_filepath(melody_filepath)2 unmodified linesINTERRUPTED = FalseINTERRUPTING = Falseif temperature < 0:temperature -0raise gr.Error("Temperature must be >= 0.")if topk < 0:topk = 1raise gr.Error("Topk must be non-negative.")if topp < 0:topp =1raise gr.Error("Topp must be non-negative.")try:if MODEL is None or MODEL.name != model:MODEL = load_model(model)else:if MOVE_TO_CPU:MODEL.to('cuda')except Exception as e:raise gr.Error(f"Error loading model '{model}': {str(e)}. Try a different model.")# prevent hackingduration = min(duration, 720)33 unmodified linesrep_penalty=0.5)try:if melody:# return excess duration, load next model and continue in loop structure building up output_segmentsif duration > MODEL.lm.cfg.dataset.segment_duration:output_segments, duration = generate_music_segments(text, melody, seed, MODEL, duration, overlap, MODEL.lm.cfg.dataset.segment_duration, prompt_index, harmony_only=False)else:# pure original codesr, melody = melody[0], torch.from_numpy(melody[1]).to(MODEL.device).float().t().unsqueeze(0)print(melody.shape)if melody.dim() == 2:melody = melody[None]melody = melody[..., :int(sr * MODEL.lm.cfg.dataset.segment_duration)]output = MODEL.generate_with_chroma(descriptions=[text],melody_wavs=melody,melody_sample_rate=sr,progress=True)# All output_segments are populated, so we can break the loop or set duration to 0breakelse:#output = MODEL.generate(descriptions=[text], progress=False)if not output_segments:next_segment = MODEL.generate(descriptions=[text], progress=True)duration -= segment_durationelse:last_chunk = output_segments[-1][:, :, -overlap*MODEL.sample_rate:]next_segment = MODEL.generate_continuation(last_chunk, MODEL.sample_rate, descriptions=[text], progress=True)duration -= segment_duration - overlapif next_segment != None:output_segments.append(next_segment)except Exception as e:print(f"Error generating audio: {e}")gr.Error(f"Error generating audio: {e}")return None, None, seedif INTERRUPTING:INTERRUPTED = True1 unmodified lineprint("Function execution interrupted!")raise gr.Error("Interrupted.")print(f"\nOutput segments: {len(output_segments)}\n")if output_segments:try:# Combine the output segments into one long audio file or stack tracks19 unmodified lines##overlapping_output = torch.cat([output[:, :, -overlap_samples:], output_segments[i][:, :, :overlap_samples]], dim=1) #stack tracks##print(f" overlap size stack:{overlapping_output.size()}\n output: {output.size()}\n segment: {output_segments[i].size()}")#overlapping_output = torch.cat([output[:, :, -overlap_samples:], output_segments[i][:, :, :overlap_samples]], dim=2) #stack tracks#print(f" overlap size cat:{overlapping_output.size()}\n output: {output.size()}\n segment: {output_segments[i].size()}")output = torch.cat([output[:, :, :-overlap_samples], overlapping_output, output_segments[i][:, :, overlap_samples:]], dim=dimension)else:output = torch.cat([output, output_segments[i]], dim=dimension)2 unmodified linesprint(f"Error combining segments: {e}. Using the first segment only.")output = output_segments[0].detach().cpu().float()[0]else:if (output is None) or (output.dim() == 0):return None, None, seedelse:output = output.detach().cpu().float()[0]profile: gr.OAuthProfile | None = Nonetitle_file_name = convert_title_to_filename(title)with NamedTemporaryFile("wb", suffix=".wav", delete=False, prefix = title_file_name) as file:video_description = f"{text}\n Duration: {str(initial_duration)} Dimension: {dimension}\n Top-k:{topk} Top-p:{topp}\n Randomness:{temperature}\n cfg:{cfg_coef} overlap: {overlap}\n Seed: {seed}\n Model: {model}\n Melody Condition:{melody_name}\n Sample Segment: {prompt_index}"if include_settings or include_title:background = add_settings_to_image(title if include_title else "", video_description if include_settings else "", background_path=background, font=settings_font, font_color=settings_font_color)audio_write(file.name, output, MODEL.sample_rate, strategy="loudness",loudness_headroom_db=18, loudness_compressor=True, add_suffix=False, channels=2)waveform_video_path = get_waveform(file.name,bg_image=background, bar_count=45, name = title_file_name)# Remove the extension from file.namefile_name_without_extension = os.path.splitext(file.name)[0]# Get the directory, filename, name, extension, and new extension of the waveform video pathvideo_dir, video_name, video_name, video_ext, video_new_ext = get_file_parts(waveform_video_path)new_video_path = os.path.join(video_dir, title_file_name + video_new_ext)mp4 = MP4(waveform_video_path)mp4["©nam"] = title_file_name # Title tagmp4["desc"] = f"{text}\n Duration: {str(initial_duration)}" # Description tagcommit = commit_hash()metadata={"prompt": text,"negative_prompt": "","Seed": seed,"steps": 1,"width": "768px","height":"512px","Dimension": dimension,"Top-k": topk,"Top-p":topp,"Randomness": temperature,"cfg":cfg_coef,"overlap": overlap,"Melody Condition": melody_name,"Sample Segment": prompt_index,"Duration": initial_duration,"Audio": file.name,"font": settings_font,"font_color": settings_font_color,"harmony_only": harmony_only,"background": background,"include_title": include_title,"include_settings": include_settings,"profile": profile,"commit": commit_hash(),"tag": git_tag(),"version": gr.__version__,"model_version": MODEL.version,"model_name": MODEL.name,"model_description": f"{MODEL.audio_channels} channels, {MODEL.sample_rate} Hz","melody_name" : melody_name if melody_name else "","melody_extension" : melody_extension if melody_extension else "","hostname": "https://huggingface.co/spaces/Surn/UnlimitedMusicGen","version" : f"""https://huggingface.co/spaces/Surn/UnlimitedMusicGen/commit/{"huggingface" if commit == "<none>" else commit}""","python" : sys.version,"torch" : getattr(torch, '__long_version__',torch.__version__),"xformers": get_xformers_version(),"gradio": gr.__version__,"huggingface_space": os.environ.get('SPACE_ID', ''),"CUDA": f"""{"CUDA is available. device: " + torch.cuda.get_device_name(0) + " version: " + torch.version.cuda if torch.cuda.is_available() else "CUDA is not available."}""",}# Add additional metadata from the metadata dictionary (if it exists)for key, value in metadata.items():mp4[key] = str(value) # Convert values to strings as required by mutagen# Save the metadata changes to the filemp4.save()try:if os.path.exists(new_video_path):delete_file(new_video_path)# Open the original MP4 file in binary read mode and the new file in binary write modewith open(waveform_video_path, "rb") as src, open(new_video_path, "wb") as dst:if os.path.exists(waveform_video_path):# Copy the contents from the source file to the destination fileshutil.copyfileobj(src, dst)waveform_video_path = new_video_pathexcept Exception as e:print(f"Error copying file: {e}")if waveform_video_path:modules.user_history.save_file(profile=profile,image=background,audio=file,video=waveform_video_path,label=text,metadata=metadata,)if MOVE_TO_CPU:MODEL.to('cpu')if UNLOAD_MODEL:MODEL = Nonetorch.cuda.empty_cache()torch.cuda.ipc_collect()return waveform_video_path, file.name, seedgr.set_static_paths(paths=["fonts/","assets/"])def ui(**kwargs):with gr.Blocks(title="UnlimitedMusicGen",css_paths="style_20250331.css", theme='Surn/beeuty') as interface:with gr.Tab("UnlimitedMusicGen"):gr.Markdown("""# UnlimitedMusicGenThis is your private demo for [UnlimitedMusicGen](https://github.com/Oncorporation/audiocraft), a simple and controllable model for music generationpresented at: ["Simple and Controllable Music Generation"](https://huggingface.co/papers/2306.05284)Disclaimer: This won't run on CPU only. Clone this App and run on GPU instance!Todo: Working on improved Interrupt.Theme Available at ["Surn/Beeuty"](https://huggingface.co/spaces/Surn/Beeuty)""")if IS_SHARED_SPACE and not torch.cuda.is_available():gr.Markdown("""⚠ This Space doesn't work in this shared UI ⚠<a href="https://huggingface.co/spaces/musicgen/MusicGen?duplicate=true" style="display: inline-block;margin-top: .5em;margin-right: .25em;" target="_blank"><img style="margin-bottom: 0em;display: inline;margin-top: -.25em;" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>to use it privately, or use the <a href="https://huggingface.co/spaces/facebook/MusicGen">public demo</a>""")with gr.Row():with gr.Column():with gr.Row():text = gr.Text(label="Describe your music", interactive=True, value="4/4 100bpm 320kbps 48khz, Industrial/Electronic Soundtrack, Dark, Intense, Sci-Fi")with gr.Column():duration = gr.Slider(minimum=1, maximum=720, value=10, label="Duration (s)", interactive=True)model = gr.Radio(["melody", "medium", "small", "large", "melody-large", "stereo-small", "stereo-medium", "stereo-large", "stereo-melody", "stereo-melody-large"], label="AI Model", value="melody", interactive=True)with gr.Row():submit = gr.Button("Generate", elem_id="btn-generate")# Adapted from https://github.com/rkfg/audiocraft/blob/long/app.py, MIT license._ = gr.Button("Interrupt", elem_id="btn-interrupt").click(fn=interrupt, queue=False)with gr.Row():with gr.Column():radio = gr.Radio(["file", "mic"], value="file", label="Condition on a melody (optional) File or Mic")melody_filepath = gr.Audio(sources=["upload"], type="filepath", label="Melody Condition (optional)", interactive=True, elem_id="melody-input")with gr.Column():harmony_only = gr.Radio(label="Use Harmony Only",choices=["No", "Yes"], value="No", interactive=True, info="Remove Drums?")prompt_index = gr.Slider(label="Melody Condition Sample Segment", minimum=-1, maximum=MAX_PROMPT_INDEX, step=1, value=0, interactive=True, info="Which 30 second segment to condition with, - 1 condition each segment independantly")with gr.Accordion("Video", open=False):with gr.Row():background= gr.Image(value="./assets/background.png", sources=["upload"], label="Background", width=768, height=512, type="filepath", interactive=True)with gr.Column():include_title = gr.Checkbox(label="Add Title", value=True, interactive=True)include_settings = gr.Checkbox(label="Add Settings to background", value=True, interactive=True)with gr.Row():title = gr.Textbox(label="Title", value="UnlimitedMusicGen", interactive=True)settings_font = gr.Text(label="Settings Font", value="./assets/arial.ttf", interactive=True)settings_font_color = gr.ColorPicker(label="Settings Font Color", value="#c87f05", interactive=True)with gr.Accordion("Expert", open=False):with gr.Row():overlap = gr.Slider(minimum=0, maximum=15, value=2, step=1, label="Verse Overlap", interactive=True)dimension = gr.Slider(minimum=-2, maximum=2, value=2, step=1, label="Dimension", info="determines which direction to add new segements of audio. (1 = stack tracks, 2 = lengthen, -2..0 = ?)", interactive=True)with gr.Row():topk = gr.Number(label="Top-k", value=280, precision=0, interactive=True)topp = gr.Number(label="Top-p", value=1150, precision=0, interactive=True)temperature = gr.Number(label="Randomness Temperature", value=0.7, precision=None, interactive=True)cfg_coef = gr.Number(label="Classifier Free Guidance", value=8.5, precision=None, interactive=True)with gr.Row():seed = gr.Number(label="Seed", value=-1, precision=0, interactive=True)gr.Button('\U0001f3b2\ufe0f', elem_classes="small-btn").click(fn=lambda: -1, outputs=[seed], queue=False)reuse_seed = gr.Button('\u267b\ufe0f', elem_classes="small-btn")with gr.Column() as c:output = gr.Video(label="Generated Music")wave_file = gr.File(label=".wav file", elem_id="output_wavefile", interactive=True)seed_used = gr.Number(label='Seed used', value=-1, interactive=False)radio.change(toggle_audio_src, radio, [melody_filepath], queue=False, show_progress=False)melody_filepath.change(load_melody_filepath, inputs=[melody_filepath, title], outputs=[title, prompt_index , model], api_name="melody_filepath_change", queue=False)reuse_seed.click(fn=lambda x: x, inputs=[seed_used], outputs=[seed], queue=False, api_name="reuse_seed")submit.click(predict, inputs=[model, text,melody_filepath, duration, dimension, topk, topp, temperature, cfg_coef, background, title, settings_font, settings_font_color, seed, overlap, prompt_index, include_title, include_settings, harmony_only], outputs=[output, wave_file, seed_used], api_name="submit")gr.Examples(fn=predict,examples=[["4/4 120bpm 320kbps 48khz, An 80s driving pop song with heavy drums and synth pads in the background","./assets/bach.mp3","stereo-melody-large","80s Pop Synth"],["4/4 120bpm 320kbps 48khz, A cheerful country song with acoustic guitars","./assets/bolero_ravel.mp3","melody","Country Guitar"],["4/4 120bpm 320kbps 48khz, 90s rock song with electric guitar and heavy drums",None,"stereo-medium","90s Rock Guitar"],["4/4 120bpm 320kbps 48khz, a light and cheerly EDM track, with syncopated drums, aery pads, and strong emotions","./assets/bach.mp3","melody-large","EDM my Bach"],["4/4 320kbps 48khz, lofi slow bpm electro chill with organic samples",None,"medium","LoFi Chill"],],inputs=[text, melody_filepath, model, title],outputs=[output])gr.HTML(value=versions_html(), visible=True, elem_id="versions")with gr.Tab("User History") as history_tab:modules.user_history.render()# Show the interfacelaunch_kwargs = {}share = kwargs.get('share', False)7 unmodified linesif share:launch_kwargs['share'] = sharelaunch_kwargs['favicon_path']= "./assets/favicon.ico"interface.queue(max_size=10, api_open=False).launch(**launch_kwargs)if __name__ == "__main__":parser = argparse.ArgumentParser()37 unmodified linesargs = parser.parse_args()launch_kwargs = {}launch_kwargs['listen'] = args.listenif args.username and args.password:launch_kwargs['auth'] = (args.username, args.password)3 unmodified lineslaunch_kwargs['inbrowser'] = args.inbrowserif args.share:launch_kwargs['share'] = args.sharelaunch_kwargs['favicon_path']= "./assets/favicon.ico"UNLOAD_MODEL = args.unload_model3 unmodified linesui(unload_to_cpu = MOVE_TO_CPU,share=args.share,**launch_kwargs,)
6 unmodified lines789106 unmodified lines# flake8: noqafrom . import data, modules, models__version__ = '0.0.2a2'6 unmodified lines789106 unmodified lines# flake8: noqafrom . import data, modules, models__version__ = '1.4.Surn'
4 unmodified lines56784 unmodified lines# LICENSE file in the root directory of this source tree.# flake8: noqafrom . import audio, audio_dataset4 unmodified lines56784 unmodified lines# LICENSE file in the root directory of this source tree.# flake8: noqafrom . import audio, audio_dataset, info_audio_dataset
20 unmodified lines212223242526122 unmodified lines14915015115215315415559 unmodified lines21521621720 unmodified linesimport torchaudio as taimport avfrom .audio_utils import f32_pcm, i16_pcm, normalize_audio, convert_audio122 unmodified lineswav = F.pad(wav, (0, expected_frames - wav.shape[-1]))return wav, srdef audio_write(stem_name: tp.Union[str, Path],wav: torch.Tensor, sample_rate: int,format: str = 'wav', mp3_rate: int = 320, normalize: bool = True,59 unmodified linespath.unlink()raisereturn path20 unmodified lines21222324252627122 unmodified lines15015115215315415515615715815916016116216316416516659 unmodified lines22622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130220 unmodified linesimport torchaudio as taimport avimport subprocess as spfrom .audio_utils import f32_pcm, i16_pcm, normalize_audio, convert_audio122 unmodified lineswav = F.pad(wav, (0, expected_frames - wav.shape[-1]))return wav, srdef _piping_to_ffmpeg(out_path: tp.Union[str, Path], wav: torch.Tensor, sample_rate: int, flags: tp.List[str]):# ffmpeg is always installed and torchaudio is a bit unstable lately, so let's bypass it entirely.assert wav.dim() == 2, wav.shapecommand = ['ffmpeg','-loglevel', 'error','-y', '-f', 'f32le', '-ar', str(sample_rate), '-ac', str(wav.shape[0]),'-i', '-'] + flags + [str(out_path)]input_ = f32_pcm(wav).t().detach().cpu().numpy().tobytes()sp.run(command, input=input_, check=True)def audio_write(stem_name: tp.Union[str, Path],wav: torch.Tensor, sample_rate: int,format: str = 'wav', mp3_rate: int = 320, normalize: bool = True,59 unmodified linespath.unlink()raisereturn pathdef audio_write2(stem_name: tp.Union[str, Path],wav: torch.Tensor, sample_rate: int,format: str = 'wav', mp3_rate: int = 320, ogg_rate: tp.Optional[int] = None,normalize: bool = True, strategy: str = 'peak', peak_clip_headroom_db: float = 1,rms_headroom_db: float = 18, loudness_headroom_db: float = 14,loudness_compressor: bool = False,log_clipping: bool = True, make_parent_dir: bool = True,add_suffix: bool = True) -> Path:"""Convenience function for saving audio to disk. Returns the filename the audio was written to.Args:stem_name (str or Path): Filename without extension which will be added automatically.wav (torch.Tensor): Audio data to save.sample_rate (int): Sample rate of audio data.format (str): Either "wav", "mp3", "ogg", or "flac".mp3_rate (int): kbps when using mp3s.ogg_rate (int): kbps when using ogg/vorbis. If not provided, let ffmpeg decide for itself.normalize (bool): if `True` (default), normalizes according to the prescribedstrategy (see after). If `False`, the strategy is only used in case clippingwould happen.strategy (str): Can be either 'clip', 'peak', or 'rms'. Default is 'peak',i.e. audio is normalized by its largest value. RMS normalizes by root-mean-squarewith extra headroom to avoid clipping. 'clip' just clips.peak_clip_headroom_db (float): Headroom in dB when doing 'peak' or 'clip' strategy.rms_headroom_db (float): Headroom in dB when doing 'rms' strategy. This must be much largerthan the `peak_clip` one to avoid further clipping.loudness_headroom_db (float): Target loudness for loudness normalization.loudness_compressor (bool): Uses tanh for soft clipping when strategy is 'loudness'.when strategy is 'loudness' log_clipping (bool): If True, basic logging on stderr when clipping stilloccurs despite strategy (only for 'rms').make_parent_dir (bool): Make parent directory if it doesn't exist.Returns:Path: Path of the saved audio."""assert wav.dtype.is_floating_point, "wav is not floating point"if wav.dim() == 1:wav = wav[None]elif wav.dim() > 2:raise ValueError("Input wav should be at most 2 dimension.")assert wav.isfinite().all()wav = normalize_audio(wav, normalize, strategy, peak_clip_headroom_db,rms_headroom_db, loudness_headroom_db, loudness_compressor,log_clipping=log_clipping, sample_rate=sample_rate,stem_name=str(stem_name))if format == 'mp3':suffix = '.mp3'flags = ['-f', 'mp3', '-c:a', 'libmp3lame', '-b:a', f'{mp3_rate}k']elif format == 'wav':suffix = '.wav'flags = ['-f', 'wav', '-c:a', 'pcm_s16le']elif format == 'ogg':suffix = '.ogg'flags = ['-f', 'ogg', '-c:a', 'libvorbis']if ogg_rate is not None:flags += ['-b:a', f'{ogg_rate}k']elif format == 'flac':suffix = '.flac'flags = ['-f', 'flac']else:raise RuntimeError(f"Invalid format {format}. Only wav or mp3 are supported.")if not add_suffix:suffix = ''path = Path(str(stem_name) + suffix)if make_parent_dir:path.parent.mkdir(exist_ok=True, parents=True)try:_piping_to_ffmpeg(path, wav, sample_rate, flags)except Exception:if path.exists():# we do not want to leave half written files around.path.unlink()raisereturn path
2 unmodified lines3456789101112131466 unmodified lines81828384858687888924 unmodified lines11411511611711811912012129 unmodified lines15115215315415515615745 unmodified lines20320420520620720820940 unmodified lines2502512522532542552562572587 unmodified lines2662672682692702712722732742759 unmodified lines28528628728828929029129229329429529629729829930016 unmodified lines3173183193203213223233243253263273283297 unmodified lines3373383393403413423433443453463473481 unmodified line3503513523533543551 unmodified line3573583593603613623633643653663673683693703713723733746 unmodified lines38138238338438538638735 unmodified lines4234244254264274284296 unmodified lines4364374384394404414424434442 unmodified lines## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.import argparseimport copyfrom concurrent.futures import ThreadPoolExecutor, Futurefrom dataclasses import dataclass, fieldsfrom contextlib import ExitStackimport gzipimport jsonimport logging66 unmodified linesclass SegmentInfo(BaseInfo):meta: AudioMetaseek_time: floatn_frames: int # actual number of frames without paddingtotal_frames: int # total number of frames, padding includedsample_rate: int # actual sample rateDEFAULT_EXTS = ['.wav', '.mp3', '.flac', '.ogg', '.m4a']24 unmodified linesArgs:m (AudioMeta): Audio meta to resolve.fast (bool): If True, uses a really fast check for determining if a file is already absolute or not.Only valid on Linux/Mac.Returns:AudioMeta: Audio meta with resolved path."""29 unmodified linesprogress (bool): Whether to log progress on audio files collection.workers (int): number of parallel workers, if 0, use only the current thread.Returns:List[AudioMeta]: List of audio file path and its metadata."""audio_files = []futures: tp.List[Future] = []45 unmodified linesresolve (bool): Whether to resolve the path from AudioMeta (default=True).fast (bool): activates some tricks to make things faster.Returns:List[AudioMeta]: List of audio file path and its total duration."""open_fn = gzip.open if str(path).lower().endswith('.gz') else openwith open_fn(path, 'rb') as fp: # type: ignore40 unmodified linesallows to return a tuple containing the torch Tensor and additional metadata on the segment and theoriginal audio meta.Args:meta (tp.List[AudioMeta]): List of audio files metadata.segment_duration (float): Optional segment duration of audio to load.If not specified, the dataset will load the full audio segment from the file.shuffle (bool): Set to `True` to have the data reshuffled at every epoch.sample_rate (int): Target sample rate of the loaded audio samples.7 unmodified linesis shorter than the desired segment.max_read_retry (int): Maximum number of retries to sample an audio segment from the dataset.return_info (bool): Whether to return the wav only or return wav along with segment info and metadata.min_audio_duration (tp.Optional[float], optional): Minimum audio file duration, in seconds, if providedaudio shorter than this will be filtered out.max_audio_duration (tp.Optional[float], optional): Maximal audio file duration in seconds, if providedaudio longer than this will be filtered out."""def __init__(self,meta: tp.List[AudioMeta],9 unmodified linesmax_read_retry: int = 10,return_info: bool = False,min_audio_duration: tp.Optional[float] = None,max_audio_duration: tp.Optional[float] = None):assert len(meta) > 0, 'No audio meta provided to AudioDataset. Please check loading of audio meta.'assert segment_duration is None or segment_duration > 0assert segment_duration is None or min_segment_ratio >= 0logging.debug(f'sample_on_duration: {sample_on_duration}')logging.debug(f'sample_on_weight: {sample_on_weight}')logging.debug(f'pad: {pad}')logging.debug(f'min_segment_ratio: {min_segment_ratio}')self.segment_duration = segment_durationself.min_segment_ratio = min_segment_ratioself.max_audio_duration = max_audio_duration16 unmodified linesself.sampling_probabilities = self._get_sampling_probabilities()self.max_read_retry = max_read_retryself.return_info = return_infodef __len__(self):return self.num_samplesdef _get_sampling_probabilities(self, normalized: bool = True):"""Return the sampling probabilities for each file inside `self.meta`."""scores: tp.List[float] = []for file_meta in self.meta:score = 1.7 unmodified linesprobabilities /= probabilities.sum()return probabilitiesdef sample_file(self, rng: torch.Generator) -> AudioMeta:"""Sample a given file from `self.meta`. Can be overriden in subclasses.This is only called if `segment_duration` is not None.You must use the provided random number generator `rng` for reproducibility."""if not self.sample_on_weight and not self.sample_on_duration:file_index = int(torch.randint(len(self.sampling_probabilities), (1,), generator=rng).item())else:1 unmodified linereturn self.meta[file_index]def __getitem__(self, index: int) -> tp.Union[torch.Tensor, tp.Tuple[torch.Tensor, SegmentInfo]]:if self.segment_duration is None:file_meta = self.meta[index]1 unmodified lineout = convert_audio(out, sr, self.sample_rate, self.channels)n_frames = out.shape[-1]segment_info = SegmentInfo(file_meta, seek_time=0., n_frames=n_frames, total_frames=n_frames,sample_rate=self.sample_rate)else:rng = torch.Generator()if self.shuffle:# We use index, plus extra randomnessrng.manual_seed(index + self.num_samples * random.randint(0, 2**24))else:# We only use indexrng.manual_seed(index)for retry in range(self.max_read_retry):file_meta = self.sample_file(rng)# We add some variance in the file position even if audio file is smaller than segment# without ending up with empty segmentsmax_seek = max(0, file_meta.duration - self.segment_duration * self.min_segment_ratio)6 unmodified linesif self.pad:out = F.pad(out, (0, target_frames - n_frames))segment_info = SegmentInfo(file_meta, seek_time, n_frames=n_frames, total_frames=target_frames,sample_rate=self.sample_rate)except Exception as exc:logger.warning("Error opening file %s: %r", file_meta.path, exc)if retry == self.max_read_retry - 1:35 unmodified linesif to_pad:# Each wav could be of a different duration as they are not segmented.for i in range(len(samples)):# Determines the total legth of the signal with padding, so we update here as we pad.segment_infos[i].total_frames = max_lenwavs[i] = _pad_wav(wavs[i])6 unmodified linesreturn torch.stack(samples)def _filter_duration(self, meta: tp.List[AudioMeta]) -> tp.List[AudioMeta]:"""Filters out audio files with short durations.Removes from meta files that have durations that will not allow to samples examples from them."""orig_len = len(meta)# Filter data that is too short.2 unmodified lines345678910111213141516171866 unmodified lines85868788899091929394959624 unmodified lines12112212312412512612712829 unmodified lines15815916016116216316445 unmodified lines21021121221321421521640 unmodified lines2572582592602612622632642652662672682692707 unmodified lines2782792802812822832842852862872882892902912922932942952969 unmodified lines30630730830931031131231331431531631731831916 unmodified lines3363373383393403413423433443453463473483493503513523533543553563573583593607 unmodified lines3683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983991 unmodified line4014024034044054064074084094104114124134144151 unmodified line4174184194204214224234244254264274284294304314324334344354364374386 unmodified lines44544644744844945045135 unmodified lines4874884894904914924936 unmodified lines5005015025035045055062 unmodified lines## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree."""AudioDataset support. In order to handle a larger number of fileswithout having to scan again the folders, we precompute some metadata(filename, sample rate, duration), and use that to efficiently sample audio segments."""import argparseimport copyfrom concurrent.futures import ThreadPoolExecutor, Futurefrom dataclasses import dataclass, fieldsfrom contextlib import ExitStackfrom functools import lru_cacheimport gzipimport jsonimport logging66 unmodified linesclass SegmentInfo(BaseInfo):meta: AudioMetaseek_time: float# The following values are given once the audio is processed, e.g.# at the target sample rate and target number of channels.n_frames: int # actual number of frames without paddingtotal_frames: int # total number of frames, padding includedsample_rate: int # actual sample ratechannels: int # number of audio channels.DEFAULT_EXTS = ['.wav', '.mp3', '.flac', '.ogg', '.m4a']24 unmodified linesArgs:m (AudioMeta): Audio meta to resolve.fast (bool): If True, uses a really fast check for determining if a fileis already absolute or not. Only valid on Linux/Mac.Returns:AudioMeta: Audio meta with resolved path."""29 unmodified linesprogress (bool): Whether to log progress on audio files collection.workers (int): number of parallel workers, if 0, use only the current thread.Returns:list of AudioMeta: List of audio file path and its metadata."""audio_files = []futures: tp.List[Future] = []45 unmodified linesresolve (bool): Whether to resolve the path from AudioMeta (default=True).fast (bool): activates some tricks to make things faster.Returns:list of AudioMeta: List of audio file path and its total duration."""open_fn = gzip.open if str(path).lower().endswith('.gz') else openwith open_fn(path, 'rb') as fp: # type: ignore40 unmodified linesallows to return a tuple containing the torch Tensor and additional metadata on the segment and theoriginal audio meta.Note that you can call `start_epoch(epoch)` in order to geta deterministic "randomization" for `shuffle=True`.For a given epoch and dataset index, this will always return the same extract.You can get back some diversity by setting the `shuffle_seed` param.Args:meta (list of AudioMeta): List of audio files metadata.segment_duration (float, optional): Optional segment duration of audio to load.If not specified, the dataset will load the full audio segment from the file.shuffle (bool): Set to `True` to have the data reshuffled at every epoch.sample_rate (int): Target sample rate of the loaded audio samples.7 unmodified linesis shorter than the desired segment.max_read_retry (int): Maximum number of retries to sample an audio segment from the dataset.return_info (bool): Whether to return the wav only or return wav along with segment info and metadata.min_audio_duration (float, optional): Minimum audio file duration, in seconds, if providedaudio shorter than this will be filtered out.max_audio_duration (float, optional): Maximal audio file duration in seconds, if providedaudio longer than this will be filtered out.shuffle_seed (int): can be used to further randomizeload_wav (bool): if False, skip loading the wav but returns a tensor of 0with the expected segment_duration (which must be provided if load_wav is False).permutation_on_files (bool): only if `sample_on_weight` and `sample_on_duration`are False. Will ensure a permutation on files when going through the dataset.In that case the epoch number must be provided in order for the modelto continue the permutation across epochs. In that case, it is assumedthat `num_samples = total_batch_size * num_updates_per_epoch`, with`total_batch_size` the overall batch size accounting for all gpus."""def __init__(self,meta: tp.List[AudioMeta],9 unmodified linesmax_read_retry: int = 10,return_info: bool = False,min_audio_duration: tp.Optional[float] = None,max_audio_duration: tp.Optional[float] = None,shuffle_seed: int = 0,load_wav: bool = True,permutation_on_files: bool = False,):assert len(meta) > 0, "No audio meta provided to AudioDataset. Please check loading of audio meta."assert segment_duration is None or segment_duration > 0assert segment_duration is None or min_segment_ratio >= 0self.segment_duration = segment_durationself.min_segment_ratio = min_segment_ratioself.max_audio_duration = max_audio_duration16 unmodified linesself.sampling_probabilities = self._get_sampling_probabilities()self.max_read_retry = max_read_retryself.return_info = return_infoself.shuffle_seed = shuffle_seedself.current_epoch: tp.Optional[int] = Noneself.load_wav = load_wavif not load_wav:assert segment_duration is not Noneself.permutation_on_files = permutation_on_filesif permutation_on_files:assert not self.sample_on_durationassert not self.sample_on_weightassert self.shuffledef start_epoch(self, epoch: int):self.current_epoch = epochdef __len__(self):return self.num_samplesdef _get_sampling_probabilities(self, normalized: bool = True):"""Return the sampling probabilities for each file inside `self.meta`."""scores: tp.List[float] = []for file_meta in self.meta:score = 1.7 unmodified linesprobabilities /= probabilities.sum()return probabilities@staticmethod@lru_cache(16)def _get_file_permutation(num_files: int, permutation_index: int, base_seed: int):# Used to keep the most recent files permutation in memory implicitely.# will work unless someone is using a lot of Datasets in parallel.rng = torch.Generator()rng.manual_seed(base_seed + permutation_index)return torch.randperm(num_files, generator=rng)def sample_file(self, index: int, rng: torch.Generator) -> AudioMeta:"""Sample a given file from `self.meta`. Can be overridden in subclasses.This is only called if `segment_duration` is not None.You must use the provided random number generator `rng` for reproducibility.You can further make use of the index accessed."""if self.permutation_on_files:assert self.current_epoch is not Nonetotal_index = self.current_epoch * len(self) + indexpermutation_index = total_index // len(self.meta)relative_index = total_index % len(self.meta)permutation = AudioDataset._get_file_permutation(len(self.meta), permutation_index, self.shuffle_seed)file_index = permutation[relative_index]return self.meta[file_index]if not self.sample_on_weight and not self.sample_on_duration:file_index = int(torch.randint(len(self.sampling_probabilities), (1,), generator=rng).item())else:1 unmodified linereturn self.meta[file_index]def _audio_read(self, path: str, seek_time: float = 0, duration: float = -1):# Override this method in subclass if needed.if self.load_wav:return audio_read(path, seek_time, duration, pad=False)else:assert self.segment_duration is not Nonen_frames = int(self.sample_rate * self.segment_duration)return torch.zeros(self.channels, n_frames), self.sample_ratedef __getitem__(self, index: int) -> tp.Union[torch.Tensor, tp.Tuple[torch.Tensor, SegmentInfo]]:if self.segment_duration is None:file_meta = self.meta[index]1 unmodified lineout = convert_audio(out, sr, self.sample_rate, self.channels)n_frames = out.shape[-1]segment_info = SegmentInfo(file_meta, seek_time=0., n_frames=n_frames, total_frames=n_frames,sample_rate=self.sample_rate, channels=out.shape[0])else:rng = torch.Generator()if self.shuffle:# We use index, plus extra randomness, either totally random if we don't know the epoch.# otherwise we make use of the epoch number and optional shuffle_seed.if self.current_epoch is None:rng.manual_seed(index + self.num_samples * random.randint(0, 2**24))else:rng.manual_seed(index + self.num_samples * (self.current_epoch + self.shuffle_seed))else:# We only use indexrng.manual_seed(index)for retry in range(self.max_read_retry):file_meta = self.sample_file(index, rng)# We add some variance in the file position even if audio file is smaller than segment# without ending up with empty segmentsmax_seek = max(0, file_meta.duration - self.segment_duration * self.min_segment_ratio)6 unmodified linesif self.pad:out = F.pad(out, (0, target_frames - n_frames))segment_info = SegmentInfo(file_meta, seek_time, n_frames=n_frames, total_frames=target_frames,sample_rate=self.sample_rate, channels=out.shape[0])except Exception as exc:logger.warning("Error opening file %s: %r", file_meta.path, exc)if retry == self.max_read_retry - 1:35 unmodified linesif to_pad:# Each wav could be of a different duration as they are not segmented.for i in range(len(samples)):# Determines the total length of the signal with padding, so we update here as we pad.segment_infos[i].total_frames = max_lenwavs[i] = _pad_wav(wavs[i])6 unmodified linesreturn torch.stack(samples)def _filter_duration(self, meta: tp.List[AudioMeta]) -> tp.List[AudioMeta]:"""Filters out audio files with audio durations that will not allow to sample examples from them."""orig_len = len(meta)# Filter data that is too short.
2 unmodified lines3456789140 unmodified lines1501511521531541551561571581591601611621631641651662 unmodified lines## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.import sysimport typing as tp140 unmodified lines"""if wav.dtype.is_floating_point:return wavelse:assert wav.dtype == torch.int16return wav.float() / 2**15def i16_pcm(wav: torch.Tensor) -> torch.Tensor:"""Convert audio to int 16 bits PCM format...Warning:: There exist many formula for doing this convertion. None are perfectdue to the asymetry of the int16 range. One either have possible clipping, DC offset,or inconsistancies with f32_pcm. If the given wav doesn't have enough headroom,it is possible that `i16_pcm(f32_pcm)) != Identity`."""if wav.dtype.is_floating_point:2 unmodified lines345678910140 unmodified lines1511521531541551561571581591601611621631641651661671681692 unmodified lines## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree."""Various utilities for audio convertion (pcm format, sample rate and channels),and volume normalization."""import sysimport typing as tp140 unmodified lines"""if wav.dtype.is_floating_point:return wavelif wav.dtype == torch.int16:return wav.float() / 2**15elif wav.dtype == torch.int32:return wav.float() / 2**31raise ValueError(f"Unsupported wav dtype: {wav.dtype}")def i16_pcm(wav: torch.Tensor) -> torch.Tensor:"""Convert audio to int 16 bits PCM format...Warning:: There exist many formula for doing this conversion. None are perfectdue to the asymmetry of the int16 range. One either have possible clipping, DC offset,or inconsistencies with f32_pcm. If the given wav doesn't have enough headroom,it is possible that `i16_pcm(f32_pcm)) != Identity`."""if wav.dtype.is_floating_point:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110# Copyright (c) Meta Platforms, Inc. and affiliates.# All rights reserved.## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree."""Base classes for the datasets that also provide non-audio metadata,e.g. description, text transcription etc."""from dataclasses import dataclassimport loggingimport mathimport reimport typing as tp
import torch
from .audio_dataset import AudioDataset, AudioMetafrom ..environment import AudioCraftEnvironmentfrom ..modules.conditioners import SegmentWithAttributes, ConditioningAttributes
logger = logging.getLogger(__name__)
def _clusterify_meta(meta: AudioMeta) -> AudioMeta: """Monkey-patch meta to match cluster specificities.""" meta.path = AudioCraftEnvironment.apply_dataset_mappers(meta.path) if meta.info_path is not None: meta.info_path.zip_path = AudioCraftEnvironment.apply_dataset_mappers(meta.info_path.zip_path) return meta
def clusterify_all_meta(meta: tp.List[AudioMeta]) -> tp.List[AudioMeta]: """Monkey-patch all meta to match cluster specificities.""" return [_clusterify_meta(m) for m in meta]
@dataclassclass AudioInfo(SegmentWithAttributes): """Dummy SegmentInfo with empty attributes.
The InfoAudioDataset is expected to return metadata that inherits from SegmentWithAttributes class and can return conditioning attributes.
This basically guarantees all datasets will be compatible with current solver that contain conditioners requiring this. """ audio_tokens: tp.Optional[torch.Tensor] = None # populated when using cached batch for training a LM.
def to_condition_attributes(self) -> ConditioningAttributes: return ConditioningAttributes()
class InfoAudioDataset(AudioDataset): """AudioDataset that always returns metadata as SegmentWithAttributes along with the audio waveform.
See `audiocraft.data.audio_dataset.AudioDataset` for initialization arguments. """ def __init__(self, meta: tp.List[AudioMeta], **kwargs): super().__init__(clusterify_all_meta(meta), **kwargs)
def __getitem__(self, index: int) -> tp.Union[torch.Tensor, tp.Tuple[torch.Tensor, SegmentWithAttributes]]: if not self.return_info: wav = super().__getitem__(index) assert isinstance(wav, torch.Tensor) return wav wav, meta = super().__getitem__(index) return wav, AudioInfo(**meta.to_dict())
def get_keyword_or_keyword_list(value: tp.Optional[str]) -> tp.Union[tp.Optional[str], tp.Optional[tp.List[str]]]: """Preprocess a single keyword or possible a list of keywords.""" if isinstance(value, list): return get_keyword_list(value) else: return get_keyword(value)
def get_string(value: tp.Optional[str]) -> tp.Optional[str]: """Preprocess a single keyword.""" if value is None or (not isinstance(value, str)) or len(value) == 0 or value == 'None': return None else: return value.strip()
def get_keyword(value: tp.Optional[str]) -> tp.Optional[str]: """Preprocess a single keyword.""" if value is None or (not isinstance(value, str)) or len(value) == 0 or value == 'None': return None else: return value.strip().lower()
def get_keyword_list(values: tp.Union[str, tp.List[str]]) -> tp.Optional[tp.List[str]]: """Preprocess a list of keywords.""" if isinstance(values, str): values = [v.strip() for v in re.split(r'[,\s]', values)] elif isinstance(values, float) and math.isnan(values): values = [] if not isinstance(values, list): logger.debug(f"Unexpected keyword list {values}") values = [str(values)]
kws = [get_keyword(v) for v in values] kw_list = [k for k in kws if k is not None] if len(kw_list) == 0: return None else: return kw_list2 unmodified lines3456789 unmodified lines1819202122232425262728293024 unmodified lines555657585960613 unmodified lines65666768697071722 unmodified lines## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.import typingimport zipfile9 unmodified lines@dataclass(order=True)class PathInZip:"""Class for holding a path of file within a zip file.Args:path: The convention is <path_to_zip>:<relative_path_inside_zip>Let's assume there is a zip file /some/location/foo.zipand inside of it is a json file located at /data/file1.json,Then we expect path = "/some/location/foo.zip:/data/file1.json""""INFO_PATH_SEP = ':'24 unmodified lines"""Sets the maximal LRU caching for zip file opening.Args:max_size: the maximal LRU cache."""global _cached_open_zip_cached_open_zip = lru_cache(max_size)(_open_zip)3 unmodified lines"""Opens a file stored inside a zip and returns a file-like object.Args:path_in_zip: A PathInZip object representing the file to return a file-like object of.mode: The mode in which to open the file with.Returns:A file-like object for PathInZip."""2 unmodified lines3456789109 unmodified lines2021222324252627282930313224 unmodified lines575859606162633 unmodified lines67686970717273742 unmodified lines## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree."""Utility for reading some info from inside a zip file."""import typingimport zipfile9 unmodified lines@dataclass(order=True)class PathInZip:"""Hold a path of file within a zip file.Args:path (str): The convention is <path_to_zip>:<relative_path_inside_zip>.Let's assume there is a zip file /some/location/foo.zipand inside of it is a json file located at /data/file1.json,Then we expect path = "/some/location/foo.zip:/data/file1.json"."""INFO_PATH_SEP = ':'24 unmodified lines"""Sets the maximal LRU caching for zip file opening.Args:max_size (int): the maximal LRU cache."""global _cached_open_zip_cached_open_zip = lru_cache(max_size)(_open_zip)3 unmodified lines"""Opens a file stored inside a zip and returns a file-like object.Args:path_in_zip (PathInZip): A PathInZip object representing the file to return a file-like object of.mode (str): The mode in which to open the file with.Returns:A file-like object for PathInZip."""
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176# Copyright (c) Meta Platforms, Inc. and affiliates.# All rights reserved.## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.
"""Provides cluster and tools configuration across clusters (slurm, dora, utilities)."""
import loggingimport osfrom pathlib import Pathimport reimport typing as tp
import omegaconf
from .utils.cluster import _guess_cluster_type
logger = logging.getLogger(__name__)
class AudioCraftEnvironment: """Environment configuration for teams and clusters.
AudioCraftEnvironment picks compute cluster settings (slurm, dora) from the current running environment or declared variable and the loaded team configuration. Additionally, the AudioCraftEnvironment provides pointers to a reference folder resolved automatically across clusters that is shared across team members, allowing to share sigs or other files to run jobs. Finally, it provides dataset mappers to automatically map dataset file paths to new locations across clusters, allowing to use the same manifest of files across cluters.
The cluster type is identified automatically and base configuration file is read from config/teams.yaml. Use the following environment variables to specify the cluster, team or configuration:
AUDIOCRAFT_CLUSTER (optional): Cluster type to enforce. Useful if the cluster type cannot be inferred automatically. AUDIOCRAFT_CONFIG (optional): Path to yaml config holding the teams configuration. If not set, configuration is read from config/teams.yaml. AUDIOCRAFT_TEAM (optional): Name of the team. Recommended to set to your own team. Cluster configuration are shared across teams to match compute allocation, specify your cluster configuration in the configuration file under a key mapping your team name. """ _instance = None DEFAULT_TEAM = "default"
def __init__(self) -> None: """Loads configuration.""" self.team: str = os.getenv("AUDIOCRAFT_TEAM", self.DEFAULT_TEAM) cluster_type = _guess_cluster_type() cluster = os.getenv( "AUDIOCRAFT_CLUSTER", cluster_type.value ) logger.info("Detecting cluster type %s", cluster_type)
self.cluster: str = cluster
config_path = os.getenv( "AUDIOCRAFT_CONFIG", Path(__file__) .parent.parent.joinpath("config/teams", self.team) .with_suffix(".yaml"), ) self.config = omegaconf.OmegaConf.load(config_path) self._dataset_mappers = [] cluster_config = self._get_cluster_config() if "dataset_mappers" in cluster_config: for pattern, repl in cluster_config["dataset_mappers"].items(): regex = re.compile(pattern) self._dataset_mappers.append((regex, repl))
def _get_cluster_config(self) -> omegaconf.DictConfig: assert isinstance(self.config, omegaconf.DictConfig) return self.config[self.cluster]
@classmethod def instance(cls): if cls._instance is None: cls._instance = cls() return cls._instance
@classmethod def reset(cls): """Clears the environment and forces a reload on next invocation.""" cls._instance = None
@classmethod def get_team(cls) -> str: """Gets the selected team as dictated by the AUDIOCRAFT_TEAM env var. If not defined, defaults to "labs". """ return cls.instance().team
@classmethod def get_cluster(cls) -> str: """Gets the detected cluster. This value can be overridden by the AUDIOCRAFT_CLUSTER env var. """ return cls.instance().cluster
@classmethod def get_dora_dir(cls) -> Path: """Gets the path to the dora directory for the current team and cluster. Value is overridden by the AUDIOCRAFT_DORA_DIR env var. """ cluster_config = cls.instance()._get_cluster_config() dora_dir = os.getenv("AUDIOCRAFT_DORA_DIR", cluster_config["dora_dir"]) logger.warning(f"Dora directory: {dora_dir}") return Path(dora_dir)
@classmethod def get_reference_dir(cls) -> Path: """Gets the path to the reference directory for the current team and cluster. Value is overridden by the AUDIOCRAFT_REFERENCE_DIR env var. """ cluster_config = cls.instance()._get_cluster_config() return Path(os.getenv("AUDIOCRAFT_REFERENCE_DIR", cluster_config["reference_dir"]))
@classmethod def get_slurm_exclude(cls) -> tp.Optional[str]: """Get the list of nodes to exclude for that cluster.""" cluster_config = cls.instance()._get_cluster_config() return cluster_config.get("slurm_exclude")
@classmethod def get_slurm_partitions(cls, partition_types: tp.Optional[tp.List[str]] = None) -> str: """Gets the requested partitions for the current team and cluster as a comma-separated string.
Args: partition_types (list[str], optional): partition types to retrieve. Values must be from ['global', 'team']. If not provided, the global partition is returned. """ if not partition_types: partition_types = ["global"]
cluster_config = cls.instance()._get_cluster_config() partitions = [ cluster_config["partitions"][partition_type] for partition_type in partition_types ] return ",".join(partitions)
@classmethod def resolve_reference_path(cls, path: tp.Union[str, Path]) -> Path: """Converts reference placeholder in path with configured reference dir to resolve paths.
Args: path (str or Path): Path to resolve. Returns: Path: Resolved path. """ path = str(path)
if path.startswith("//reference"): reference_dir = cls.get_reference_dir() logger.warn(f"Reference directory: {reference_dir}") assert ( reference_dir.exists() and reference_dir.is_dir() ), f"Reference directory does not exist: {reference_dir}." path = re.sub("^//reference", str(reference_dir), path)
return Path(path)
@classmethod def apply_dataset_mappers(cls, path: str) -> str: """Applies dataset mapping regex rules as defined in the configuration. If no rules are defined, the path is returned as-is. """ instance = cls.instance()
for pattern, repl in instance._dataset_mappers: path = pattern.sub(repl, path)
return path3 unmodified lines456789103 unmodified lines# This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.# flake8: noqafrom .musicgen import MusicGenfrom .lm import LMModelfrom .encodec import CompressionModel, EncodecModel3 unmodified lines45678910111213141516173 unmodified lines# This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree."""Models for EnCodec, AudioGen, MusicGen, as well as the generic LMModel."""# flake8: noqafrom . import builders, loadersfrom .encodec import (CompressionModel, EncodecModel, DAC,HFEncodecModel, HFEncodecCompressionModel)from .musicgen import MusicGenfrom .lm import LMModelfrom .encodec import CompressionModel, EncodecModel
9 unmodified lines101112131415161718192021222324252627282930313233343536373839404118 unmodified lines6061626364656667686970711 unmodified line73747576777879808182838485868788899091921 unmodified line94959697989910010110210310410510610710 unmodified lines1181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621 unmodified line1641651661671681691701711721731741751762 unmodified lines1791801811821831841851861871881891901911922 unmodified lines1951961971981992002012022032042052062078 unmodified lines2162172189 unmodified lines"""import typing as tpimport warningsimport audiocraftimport omegaconfimport torchfrom .encodec import CompressionModel, EncodecModel, FlattenedCompressionModel # noqafrom .lm import LMModelfrom ..modules.codebooks_patterns import (CodebooksPatternProvider,DelayedPatternProvider,ParallelPatternProvider,UnrolledPatternProvider,VALLEPattern,MusicLMPattern,)from ..modules.conditioners import (BaseConditioner,ConditioningProvider,LUTConditioner,T5Conditioner,ConditionFuser,ChromaStemConditioner,)from .. import quantization as qtfrom ..utils.utils import dict_from_configdef get_quantizer(quantizer: str, cfg: omegaconf.DictConfig, dimension: int) -> qt.BaseQuantizer:18 unmodified linesdecoder = audiocraft.modules.SEANetDecoder(**decoder_kwargs)return encoder, decoderelse:raise KeyError(f'Unexpected compression model {cfg.compression_model}')def get_compression_model(cfg: omegaconf.DictConfig) -> CompressionModel:"""Instantiate a compression model."""if cfg.compression_model == 'encodec':kwargs = dict_from_config(getattr(cfg, 'encodec'))encoder_name = kwargs.pop('autoencoder')1 unmodified lineencoder, decoder = get_encodec_autoencoder(encoder_name, cfg)quantizer = get_quantizer(quantizer_name, cfg, encoder.dimension)frame_rate = kwargs['sample_rate'] // encoder.hop_lengthrenormalize = kwargs.pop('renormalize', None)renorm = kwargs.pop('renorm')if renormalize is None:renormalize = renorm is not Nonewarnings.warn("You are using a deprecated EnCodec model. Please migrate to new renormalization.")return EncodecModel(encoder, decoder, quantizer,frame_rate=frame_rate, renormalize=renormalize, **kwargs).to(cfg.device)else:raise KeyError(f'Unexpected compression model {cfg.compression_model}')def get_lm_model(cfg: omegaconf.DictConfig) -> LMModel:"""Instantiate a transformer LM."""if cfg.lm_model == 'transformer_lm':kwargs = dict_from_config(getattr(cfg, 'transformer_lm'))n_q = kwargs['n_q']1 unmodified linecodebooks_pattern_cfg = getattr(cfg, 'codebooks_pattern')attribute_dropout = dict_from_config(getattr(cfg, 'attribute_dropout'))cls_free_guidance = dict_from_config(getattr(cfg, 'classifier_free_guidance'))cfg_prob, cfg_coef = cls_free_guidance["training_dropout"], cls_free_guidance["inference_coef"]fuser = get_condition_fuser(cfg)condition_provider = get_conditioner_provider(kwargs["dim"], cfg).to(cfg.device)if len(fuser.fuse2cond['cross']) > 0: # enforce cross-att programaticallykwargs['cross_attention'] = Trueif codebooks_pattern_cfg.modeling is None:assert q_modeling is not None, \'LM model should either have a codebook pattern defined or transformer_lm.q_modeling'codebooks_pattern_cfg = omegaconf.OmegaConf.create({'modeling': q_modeling, 'delay': {'delays': list(range(n_q))}})10 unmodified lines**kwargs).to(cfg.device)else:raise KeyError(f'Unexpected LM model {cfg.lm_model}')def get_conditioner_provider(output_dim: int, cfg: omegaconf.DictConfig) -> ConditioningProvider:"""Instantiate a conditioning model."""device = cfg.deviceduration = cfg.dataset.segment_durationcfg = getattr(cfg, "conditioners")cfg = omegaconf.OmegaConf.create({}) if cfg is None else cfgconditioners: tp.Dict[str, BaseConditioner] = {}with omegaconf.open_dict(cfg):condition_provider_args = cfg.pop('args', {})for cond, cond_cfg in cfg.items():model_type = cond_cfg["model"]model_args = cond_cfg[model_type]if model_type == "t5":conditioners[str(cond)] = T5Conditioner(output_dim=output_dim, device=device, **model_args)elif model_type == "lut":conditioners[str(cond)] = LUTConditioner(output_dim=output_dim, **model_args)elif model_type == "chroma_stem":model_args.pop('cache_path', None)conditioners[str(cond)] = ChromaStemConditioner(output_dim=output_dim,duration=duration,device=device,**model_args)else:raise ValueError(f"unrecognized conditioning model: {model_type}")conditioner = ConditioningProvider(conditioners, device=device, **condition_provider_args)return conditionerdef get_condition_fuser(cfg: omegaconf.DictConfig) -> ConditionFuser:"""Instantiate a condition fuser object."""fuser_cfg = getattr(cfg, "fuser")fuser_methods = ["sum", "cross", "prepend", "input_interpolate"]fuse2cond = {k: fuser_cfg[k] for k in fuser_methods}kwargs = {k: v for k, v in fuser_cfg.items() if k not in fuser_methods}fuser = ConditionFuser(fuse2cond=fuse2cond, **kwargs)1 unmodified linedef get_codebooks_pattern_provider(n_q: int, cfg: omegaconf.DictConfig) -> CodebooksPatternProvider:"""Instantiate a codebooks pattern provider object."""pattern_providers = {'parallel': ParallelPatternProvider,'delay': DelayedPatternProvider,'unroll': UnrolledPatternProvider,'valle': VALLEPattern,'musiclm': MusicLMPattern,}name = cfg.modeling2 unmodified linesreturn klass(n_q, **kwargs)def get_debug_compression_model(device='cpu'):"""Instantiate a debug compression model to be used for unit tests."""seanet_kwargs = {'n_filters': 4,'n_residual_layers': 1,'dimension': 32,'ratios': [10, 8, 16] # 25 Hz at 32kHz}encoder = audiocraft.modules.SEANetEncoder(**seanet_kwargs)decoder = audiocraft.modules.SEANetDecoder(**seanet_kwargs)2 unmodified linesquantizer(init_x, 1) # initialize kmeans etc.compression_model = EncodecModel(encoder, decoder, quantizer,frame_rate=25, sample_rate=32000, channels=1).to(device)return compression_model.eval()def get_debug_lm_model(device='cpu'):"""Instantiate a debug LM to be used for unit tests."""pattern = DelayedPatternProvider(n_q=4)dim = 16providers = {8 unmodified linesn_q=4, card=400, dim=dim, num_heads=4, custom=True, num_layers=2,cross_attention=True, causal=True)return lm.to(device).eval()9 unmodified lines1011121314151617181920212223242526272829303132333435363738394041424318 unmodified lines62636465666768697071721 unmodified line74757677787980818283848586878889901 unmodified line929394959697989910010110210310410510 unmodified lines1161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651 unmodified line1671681691701711721731741751761771782 unmodified lines1811821831841851861871881891901911921931941951961971981992002 unmodified lines2032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322338 unmodified lines2422432442452462472482492502512522532542552562572589 unmodified lines"""import typing as tpimport audiocraftimport omegaconfimport torchfrom .encodec import CompressionModel, EncodecModel, InterleaveStereoCompressionModelfrom .lm import LMModelfrom ..modules.codebooks_patterns import (CodebooksPatternProvider,DelayedPatternProvider,MusicLMPattern,ParallelPatternProvider,UnrolledPatternProvider,CoarseFirstPattern,)from ..modules.conditioners import (BaseConditioner,ChromaStemConditioner,CLAPEmbeddingConditioner,ConditionFuser,ConditioningProvider,LUTConditioner,T5Conditioner,)from .unet import DiffusionUnetfrom .. import quantization as qtfrom ..utils.utils import dict_from_configfrom ..modules.diffusion_schedule import MultiBandProcessor, SampleProcessordef get_quantizer(quantizer: str, cfg: omegaconf.DictConfig, dimension: int) -> qt.BaseQuantizer:18 unmodified linesdecoder = audiocraft.modules.SEANetDecoder(**decoder_kwargs)return encoder, decoderelse:raise KeyError(f"Unexpected compression model {cfg.compression_model}")def get_compression_model(cfg: omegaconf.DictConfig) -> CompressionModel:"""Instantiate a compression model."""if cfg.compression_model == 'encodec':kwargs = dict_from_config(getattr(cfg, 'encodec'))encoder_name = kwargs.pop('autoencoder')1 unmodified lineencoder, decoder = get_encodec_autoencoder(encoder_name, cfg)quantizer = get_quantizer(quantizer_name, cfg, encoder.dimension)frame_rate = kwargs['sample_rate'] // encoder.hop_lengthrenormalize = kwargs.pop('renormalize', False)# deprecated paramskwargs.pop('renorm', None)return EncodecModel(encoder, decoder, quantizer,frame_rate=frame_rate, renormalize=renormalize, **kwargs).to(cfg.device)else:raise KeyError(f"Unexpected compression model {cfg.compression_model}")def get_lm_model(cfg: omegaconf.DictConfig) -> LMModel:"""Instantiate a transformer LM."""if cfg.lm_model == 'transformer_lm':kwargs = dict_from_config(getattr(cfg, 'transformer_lm'))n_q = kwargs['n_q']1 unmodified linecodebooks_pattern_cfg = getattr(cfg, 'codebooks_pattern')attribute_dropout = dict_from_config(getattr(cfg, 'attribute_dropout'))cls_free_guidance = dict_from_config(getattr(cfg, 'classifier_free_guidance'))cfg_prob, cfg_coef = cls_free_guidance['training_dropout'], cls_free_guidance['inference_coef']fuser = get_condition_fuser(cfg)condition_provider = get_conditioner_provider(kwargs["dim"], cfg).to(cfg.device)if len(fuser.fuse2cond['cross']) > 0: # enforce cross-att programmaticallykwargs['cross_attention'] = Trueif codebooks_pattern_cfg.modeling is None:assert q_modeling is not None, \"LM model should either have a codebook pattern defined or transformer_lm.q_modeling"codebooks_pattern_cfg = omegaconf.OmegaConf.create({'modeling': q_modeling, 'delay': {'delays': list(range(n_q))}})10 unmodified lines**kwargs).to(cfg.device)else:raise KeyError(f"Unexpected LM model {cfg.lm_model}")def get_conditioner_provider(output_dim: int, cfg: omegaconf.DictConfig) -> ConditioningProvider:"""Instantiate a conditioning model."""device = cfg.deviceduration = cfg.dataset.segment_durationcfg = getattr(cfg, 'conditioners')dict_cfg = {} if cfg is None else dict_from_config(cfg)conditioners: tp.Dict[str, BaseConditioner] = {}condition_provider_args = dict_cfg.pop('args', {})condition_provider_args.pop('merge_text_conditions_p', None)condition_provider_args.pop('drop_desc_p', None)for cond, cond_cfg in dict_cfg.items():model_type = cond_cfg['model']model_args = cond_cfg[model_type]if model_type == 't5':conditioners[str(cond)] = T5Conditioner(output_dim=output_dim, device=device, **model_args)elif model_type == 'lut':conditioners[str(cond)] = LUTConditioner(output_dim=output_dim, **model_args)elif model_type == 'chroma_stem':conditioners[str(cond)] = ChromaStemConditioner(output_dim=output_dim,duration=duration,device=device,**model_args)elif model_type == 'clap':conditioners[str(cond)] = CLAPEmbeddingConditioner(output_dim=output_dim,device=device,**model_args)else:raise ValueError(f"Unrecognized conditioning model: {model_type}")conditioner = ConditioningProvider(conditioners, device=device, **condition_provider_args)return conditionerdef get_condition_fuser(cfg: omegaconf.DictConfig) -> ConditionFuser:"""Instantiate a condition fuser object."""fuser_cfg = getattr(cfg, 'fuser')fuser_methods = ['sum', 'cross', 'prepend', 'input_interpolate']fuse2cond = {k: fuser_cfg[k] for k in fuser_methods}kwargs = {k: v for k, v in fuser_cfg.items() if k not in fuser_methods}fuser = ConditionFuser(fuse2cond=fuse2cond, **kwargs)1 unmodified linedef get_codebooks_pattern_provider(n_q: int, cfg: omegaconf.DictConfig) -> CodebooksPatternProvider:"""Instantiate a codebooks pattern provider object."""pattern_providers = {'parallel': ParallelPatternProvider,'delay': DelayedPatternProvider,'unroll': UnrolledPatternProvider,'coarse_first': CoarseFirstPattern,'musiclm': MusicLMPattern,}name = cfg.modeling2 unmodified linesreturn klass(n_q, **kwargs)def get_debug_compression_model(device='cpu', sample_rate: int = 32000):"""Instantiate a debug compression model to be used for unit tests."""assert sample_rate in [16000, 32000], "unsupported sample rate for debug compression model"model_ratios = {16000: [10, 8, 8], # 25 Hz at 16kHz32000: [10, 8, 16] # 25 Hz at 32kHz}ratios: tp.List[int] = model_ratios[sample_rate]frame_rate = 25seanet_kwargs: dict = {'n_filters': 4,'n_residual_layers': 1,'dimension': 32,'ratios': ratios,}encoder = audiocraft.modules.SEANetEncoder(**seanet_kwargs)decoder = audiocraft.modules.SEANetDecoder(**seanet_kwargs)2 unmodified linesquantizer(init_x, 1) # initialize kmeans etc.compression_model = EncodecModel(encoder, decoder, quantizer,frame_rate=frame_rate, sample_rate=sample_rate, channels=1).to(device)return compression_model.eval()def get_diffusion_model(cfg: omegaconf.DictConfig):# TODO Find a way to infer the channels from dsetchannels = cfg.channelsnum_steps = cfg.schedule.num_stepsreturn DiffusionUnet(chin=channels, num_steps=num_steps, **cfg.diffusion_unet)def get_processor(cfg, sample_rate: int = 24000):sample_processor = SampleProcessor()if cfg.use:kw = dict(cfg)kw.pop('use')kw.pop('name')if cfg.name == "multi_band_processor":sample_processor = MultiBandProcessor(sample_rate=sample_rate, **kw)return sample_processordef get_debug_lm_model(device='cpu'):"""Instantiate a debug LM to be used for unit tests."""pattern = DelayedPatternProvider(n_q=4)dim = 16providers = {8 unmodified linesn_q=4, card=400, dim=dim, num_heads=4, custom=True, num_layers=2,cross_attention=True, causal=True)return lm.to(device).eval()def get_wrapped_compression_model(compression_model: CompressionModel,cfg: omegaconf.DictConfig) -> CompressionModel:if hasattr(cfg, 'interleave_stereo_codebooks'):if cfg.interleave_stereo_codebooks.use:kwargs = dict_from_config(cfg.interleave_stereo_codebooks)kwargs.pop('use')compression_model = InterleaveStereoCompressionModel(compression_model, **kwargs)if hasattr(cfg, 'compression_model_n_q'):if cfg.compression_model_n_q is not None:compression_model.set_num_codebooks(cfg.compression_model_n_q)return compression_model
2 unmodified lines345678910111213141516171819201 unmodified line2223242526272829303132333 unmodified lines3738394041424318 unmodified lines626364656667686970718 unmodified lines80818283848586878822 unmodified lines11111211311411511611711811912012112212312412512612712812913013113213313413540 unmodified lines1761771781791801811829 unmodified lines1921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312323 unmodified lines2362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652 unmodified lines2682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013022 unmodified lines## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.from abc import ABC, abstractmethodimport typing as tpfrom einops import rearrangeimport torchfrom torch import nnfrom .. import quantization as qtclass CompressionModel(ABC, nn.Module):@abstractmethoddef forward(self, x: torch.Tensor) -> qt.QuantizedResult:1 unmodified line@abstractmethoddef encode(self, x: torch.Tensor) -> tp.Tuple[torch.Tensor, tp.Optional[torch.Tensor]]:"""See `EncodecModel.encode`"""...@abstractmethoddef decode(self, codes: torch.Tensor, scale: tp.Optional[torch.Tensor] = None):"""See `EncodecModel.decode`"""...@property3 unmodified lines@property@abstractmethoddef frame_rate(self) -> int:...@property18 unmodified lines@abstractmethoddef set_num_codebooks(self, n: int):"""Set the active number of codebooks used by the quantizer."""...class EncodecModel(CompressionModel):"""Encodec model operating on the raw waveform.8 unmodified linescausal (bool): Whether to use a causal version of the model.renormalize (bool): Whether to renormalize the audio before running the model."""# we need assignement to override the property in the abstract class,# I couldn't find a better way...frame_rate: int = 0sample_rate: int = 0channels: int = 022 unmodified lines@propertydef total_codebooks(self):"""Total number of quantizer codebooks available."""return self.quantizer.total_codebooks@propertydef num_codebooks(self):"""Active number of codebooks used by the quantizer."""return self.quantizer.num_codebooksdef set_num_codebooks(self, n: int):"""Set the active number of codebooks used by the quantizer."""self.quantizer.set_num_codebooks(n)@propertydef cardinality(self):"""Cardinality of each codebook."""return self.quantizer.binsdef preprocess(self, x: torch.Tensor) -> tp.Tuple[torch.Tensor, tp.Optional[torch.Tensor]]:40 unmodified linesx (torch.Tensor): Float tensor of shape [B, C, T]Returns:codes, scale (tp.Tuple[torch.Tensor, torch.Tensor]): Tuple composed of:codes a float tensor of shape [B, K, T] with K the number of codebooks used and T the timestep.scale a float tensor containing the scale for audio renormalizealization."""9 unmodified linesArgs:codes (torch.Tensor): Int tensor of shape [B, K, T]scale (tp.Optional[torch.Tensor]): Float tensor containing the scale value.Returns:out (torch.Tensor): Float tensor of shape [B, C, T], the reconstructed audio."""emb = self.quantizer.decode(codes)out = self.decoder(emb)out = self.postprocess(out, scale)# out contains extra padding added by the encoder and decoderreturn outclass FlattenedCompressionModel(CompressionModel):"""Wraps a CompressionModel and flatten its codebooks, e.g.instead of returning [B, K, T], return [B, S, T * (K // S)] withS the number of codebooks per step, and `K // S` the number of 'virtual steps'for each real time step.Args:model (CompressionModel): compression model to wrap.codebooks_per_step (int): number of codebooks to keep per step,this must divide the number of codebooks provided by the wrapped model.extend_cardinality (bool): if True, and for instance if codebooks_per_step = 1,if each codebook has a cardinality N, then the first codebook willuse the range [0, N - 1], and the second [N, 2 N - 1] etc.On decoding, this can lead to potentially invalid sequences.Any invalid entry will be silently remapped to the proper rangewith a modulo."""def __init__(self, model: CompressionModel, codebooks_per_step: int = 1,extend_cardinality: bool = True):super().__init__()self.model = modelself.codebooks_per_step = codebooks_per_stepself.extend_cardinality = extend_cardinality@propertydef total_codebooks(self):3 unmodified linesdef num_codebooks(self):"""Active number of codebooks used by the quantizer...Warning:: this reports the number of codebooks after the flatteningof the codebooks!"""assert self.model.num_codebooks % self.codebooks_per_step == 0return self.codebooks_per_stepdef set_num_codebooks(self, n: int):"""Set the active number of codebooks used by the quantizer...Warning:: this sets the number of codebooks **before** the flatteningof the codebooks."""assert n % self.codebooks_per_step == 0self.model.set_num_codebooks(n)@propertydef num_virtual_steps(self) -> int:"""Return the number of virtual steps, e.g. one real stepwill be split into that many steps."""return self.model.num_codebooks // self.codebooks_per_step@propertydef frame_rate(self) -> int:return self.model.frame_rate * self.num_virtual_steps@property2 unmodified lines@propertydef channels(self) -> int:return self.model.channels@propertydef cardinality(self):"""Cardinality of each codebook."""if self.extend_cardinality:return self.model.cardinality * self.num_virtual_stepselse:return self.model.cardinalitydef forward(self, x: torch.Tensor) -> qt.QuantizedResult:raise NotImplementedError("Not supported, use encode and decode.")def encode(self, x: torch.Tensor) -> tp.Tuple[torch.Tensor, tp.Optional[torch.Tensor]]:indices, scales = self.model.encode(x)B, K, T = indices.shapeindices = rearrange(indices, 'b (k v) t -> b k t v', k=self.codebooks_per_step)if self.extend_cardinality:for virtual_step in range(1, self.num_virtual_steps):indices[..., virtual_step] += self.model.cardinality * virtual_stepindices = rearrange(indices, 'b k t v -> b k (t v)')return (indices, scales)def decode(self, codes: torch.Tensor, scale: tp.Optional[torch.Tensor] = None):B, K, T = codes.shapeassert T % self.num_virtual_steps == 0codes = rearrange(codes, 'b k (t v) -> b (k v) t', v=self.num_virtual_steps)# We silently ignore potential errors from the LM when# using extend_cardinality.codes = codes % self.model.cardinalityreturn self.model.decode(codes, scale)2 unmodified lines3456789101112131415161718192021222324252627282930313233341 unmodified line36373839404142434445464748495051523 unmodified lines5657585960616218 unmodified lines818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251268 unmodified lines13513613713813914014114214322 unmodified lines16616716816917017117217317417517617717817918018118218318418518640 unmodified lines2272282292302312322339 unmodified lines2432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154163 unmodified lines4204214224234244254264274284294304314324334344354364374384394404414424434444454462 unmodified lines4494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055062 unmodified lines## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree."""Compression models or wrapper around existing models.Also defines the main interface that a model must follow to be usable as an audio tokenizer."""from abc import ABC, abstractmethodimport loggingimport mathfrom pathlib import Pathimport typing as tpfrom einops import rearrangeimport numpy as npimport torchfrom torch import nnfrom transformers import EncodecModel as HFEncodecModelfrom .. import quantization as qtlogger = logging.getLogger()class CompressionModel(ABC, nn.Module):"""Base API for all compression model that aim at being used as audio tokenizerswith a language model."""@abstractmethoddef forward(self, x: torch.Tensor) -> qt.QuantizedResult:1 unmodified line@abstractmethoddef encode(self, x: torch.Tensor) -> tp.Tuple[torch.Tensor, tp.Optional[torch.Tensor]]:"""See `EncodecModel.encode`."""...@abstractmethoddef decode(self, codes: torch.Tensor, scale: tp.Optional[torch.Tensor] = None):"""See `EncodecModel.decode`."""...@abstractmethoddef decode_latent(self, codes: torch.Tensor):"""Decode from the discrete codes to continuous latent space."""...@property3 unmodified lines@property@abstractmethoddef frame_rate(self) -> float:...@property18 unmodified lines@abstractmethoddef set_num_codebooks(self, n: int):"""Set the active number of codebooks used by the quantizer."""...@staticmethoddef get_pretrained(name: str, device: tp.Union[torch.device, str] = 'cpu') -> 'CompressionModel':"""Instantiate a CompressionModel from a given pretrained model.Args:name (Path or str): name of the pretrained model. See after.device (torch.device or str): Device on which the model is loaded.Pretrained models:- dac_44khz (https://github.com/descriptinc/descript-audio-codec)- dac_24khz (same)- facebook/encodec_24khz (https://huggingface.co/facebook/encodec_24khz)- facebook/encodec_32khz (https://huggingface.co/facebook/encodec_32khz)- your own model on HugginFace. Export instructions to come..."""from . import builders, loadersmodel: CompressionModelif name in ['dac_44khz', 'dac_24khz']:model_type = name.split('_')[1]logger.info("Getting pretrained compression model from DAC %s", model_type)model = DAC(model_type)elif name in ['debug_compression_model']:logger.info("Getting pretrained compression model for debug")model = builders.get_debug_compression_model()elif Path(name).exists():# We assume here if the paths exist that it is in fact an AC checkpoint# that was exported using `audiocraft.utils.export` functions.model = loaders.load_compression_model(name, device=device)else:logger.info("Getting pretrained compression model from HF %s", name)hf_model = HFEncodecModel.from_pretrained(name)model = HFEncodecCompressionModel(hf_model).to(device)return model.to(device).eval()class EncodecModel(CompressionModel):"""Encodec model operating on the raw waveform.8 unmodified linescausal (bool): Whether to use a causal version of the model.renormalize (bool): Whether to renormalize the audio before running the model."""# we need assignment to override the property in the abstract class,# I couldn't find a better way...frame_rate: float = 0sample_rate: int = 0channels: int = 022 unmodified lines@propertydef total_codebooks(self):"""Total number of quantizer codebooks available."""return self.quantizer.total_codebooks@propertydef num_codebooks(self):"""Active number of codebooks used by the quantizer."""return self.quantizer.num_codebooksdef set_num_codebooks(self, n: int):"""Set the active number of codebooks used by the quantizer."""self.quantizer.set_num_codebooks(n)@propertydef cardinality(self):"""Cardinality of each codebook."""return self.quantizer.binsdef preprocess(self, x: torch.Tensor) -> tp.Tuple[torch.Tensor, tp.Optional[torch.Tensor]]:40 unmodified linesx (torch.Tensor): Float tensor of shape [B, C, T]Returns:codes, scale (tuple of torch.Tensor, torch.Tensor): Tuple composed of:codes a float tensor of shape [B, K, T] with K the number of codebooks used and T the timestep.scale a float tensor containing the scale for audio renormalizealization."""9 unmodified linesArgs:codes (torch.Tensor): Int tensor of shape [B, K, T]scale (torch.Tensor, optional): Float tensor containing the scale value.Returns:out (torch.Tensor): Float tensor of shape [B, C, T], the reconstructed audio."""emb = self.decode_latent(codes)out = self.decoder(emb)out = self.postprocess(out, scale)# out contains extra padding added by the encoder and decoderreturn outdef decode_latent(self, codes: torch.Tensor):"""Decode from the discrete codes to continuous latent space."""return self.quantizer.decode(codes)class DAC(CompressionModel):def __init__(self, model_type: str = "44khz"):super().__init__()try:import dac.utilsexcept ImportError:raise RuntimeError("Could not import dac, make sure it is installed, ""please run `pip install descript-audio-codec`")self.model = dac.utils.load_model(model_type=model_type)self.n_quantizers = self.total_codebooksself.model.eval()def forward(self, x: torch.Tensor) -> qt.QuantizedResult:# We don't support training with this.raise NotImplementedError("Forward and training with DAC not supported.")def encode(self, x: torch.Tensor) -> tp.Tuple[torch.Tensor, tp.Optional[torch.Tensor]]:codes = self.model.encode(x, self.n_quantizers)[1]return codes[:, :self.n_quantizers], Nonedef decode(self, codes: torch.Tensor, scale: tp.Optional[torch.Tensor] = None):assert scale is Nonez_q = self.decode_latent(codes)return self.model.decode(z_q)def decode_latent(self, codes: torch.Tensor):"""Decode from the discrete codes to continuous latent space."""return self.model.quantizer.from_codes(codes)[0]@propertydef channels(self) -> int:return 1@propertydef frame_rate(self) -> float:return self.model.sample_rate / self.model.hop_length@propertydef sample_rate(self) -> int:return self.model.sample_rate@propertydef cardinality(self) -> int:return self.model.codebook_size@propertydef num_codebooks(self) -> int:return self.n_quantizers@propertydef total_codebooks(self) -> int:return self.model.n_codebooksdef set_num_codebooks(self, n: int):"""Set the active number of codebooks used by the quantizer."""assert n >= 1assert n <= self.total_codebooksself.n_quantizers = nclass HFEncodecCompressionModel(CompressionModel):"""Wrapper around HuggingFace Encodec."""def __init__(self, model: HFEncodecModel):super().__init__()self.model = modelbws = self.model.config.target_bandwidthsnum_codebooks = [bw * 1000 / (self.frame_rate * math.log2(self.cardinality))for bw in bws]deltas = [nc - int(nc) for nc in num_codebooks]# Checking we didn't do some bad maths and we indeed have integers!assert all(deltas) <= 1e-3, deltasself.possible_num_codebooks = [int(nc) for nc in num_codebooks]self.set_num_codebooks(max(self.possible_num_codebooks))def forward(self, x: torch.Tensor) -> qt.QuantizedResult:# We don't support training with this.raise NotImplementedError("Forward and training with HF EncodecModel not supported.")def encode(self, x: torch.Tensor) -> tp.Tuple[torch.Tensor, tp.Optional[torch.Tensor]]:bandwidth_index = self.possible_num_codebooks.index(self.num_codebooks)bandwidth = self.model.config.target_bandwidths[bandwidth_index]res = self.model.encode(x, None, bandwidth)assert len(res[0]) == 1assert len(res[1]) == 1return res[0][0], res[1][0]def decode(self, codes: torch.Tensor, scale: tp.Optional[torch.Tensor] = None):if scale is None:scales = [None] # type: ignoreelse:scales = scale # type: ignoreres = self.model.decode(codes[None], scales)return res[0]def decode_latent(self, codes: torch.Tensor):"""Decode from the discrete codes to continuous latent space."""return self.model.quantizer.decode(codes.transpose(0, 1))@propertydef channels(self) -> int:return self.model.config.audio_channels@propertydef frame_rate(self) -> float:hop_length = int(np.prod(self.model.config.upsampling_ratios))return self.sample_rate / hop_length@propertydef sample_rate(self) -> int:return self.model.config.sampling_rate@propertydef cardinality(self) -> int:return self.model.config.codebook_size@propertydef num_codebooks(self) -> int:return self._num_codebooks@propertydef total_codebooks(self) -> int:return max(self.possible_num_codebooks)def set_num_codebooks(self, n: int):"""Set the active number of codebooks used by the quantizer."""if n not in self.possible_num_codebooks:raise ValueError(f"Allowed values for num codebooks: {self.possible_num_codebooks}")self._num_codebooks = nclass InterleaveStereoCompressionModel(CompressionModel):"""Wraps a CompressionModel to support stereo inputs. The wrapped modelwill be applied independently to the left and right channels, and both codebookswill be interleaved. If the wrapped model returns a representation `[B, K ,T]` perchannel, then the output will be `[B, K * 2, T]` or `[B, K, T * 2]` depending on`per_timestep`.Args:model (CompressionModel): Compression model to wrap.per_timestep (bool): Whether to interleave on the timestep dimensionor on the codebooks dimension."""def __init__(self, model: CompressionModel, per_timestep: bool = False):super().__init__()self.model = modelself.per_timestep = per_timestepassert self.model.channels == 1, "Wrapped model is expected to be for monophonic audio"@propertydef total_codebooks(self):3 unmodified linesdef num_codebooks(self):"""Active number of codebooks used by the quantizer...Warning:: this reports the number of codebooks after the interleavingof the codebooks!"""return self.model.num_codebooks if self.per_timestep else self.model.num_codebooks * 2def set_num_codebooks(self, n: int):"""Set the active number of codebooks used by the quantizer...Warning:: this sets the number of codebooks before the interleaving!"""self.model.set_num_codebooks(n)@propertydef num_virtual_steps(self) -> float:"""Return the number of virtual steps, e.g. one real stepwill be split into that many steps."""return 2 if self.per_timestep else 1@propertydef frame_rate(self) -> float:return self.model.frame_rate * self.num_virtual_steps@property2 unmodified lines@propertydef channels(self) -> int:return 2@propertydef cardinality(self):"""Cardinality of each codebook."""return self.model.cardinalitydef forward(self, x: torch.Tensor) -> qt.QuantizedResult:raise NotImplementedError("Not supported, use encode and decode.")def encode(self, x: torch.Tensor) -> tp.Tuple[torch.Tensor, tp.Optional[torch.Tensor]]:B, C, T = x.shapeassert C == self.channels, f"Expecting stereo audio but audio num channels is {C}"indices_c0, scales_c0 = self.model.encode(x[:, 0, ...].unsqueeze(1))indices_c1, scales_c1 = self.model.encode(x[:, 1, ...].unsqueeze(1))indices = torch.stack([indices_c0, indices_c1], dim=0)scales: tp.Optional[torch.Tensor] = Noneif scales_c0 is not None and scales_c1 is not None:scales = torch.stack([scales_c0, scales_c1], dim=1)if self.per_timestep:indices = rearrange(indices, 'c b k t -> b k (t c)', c=2)else:indices = rearrange(indices, 'c b k t -> b (k c) t', c=2)return (indices, scales)def get_left_right_codes(self, codes: torch.Tensor) -> tp.Tuple[torch.Tensor, torch.Tensor]:if self.per_timestep:codes = rearrange(codes, 'b k (t c) -> c b k t', c=2)else:codes = rearrange(codes, 'b (k c) t -> c b k t', c=2)return codes[0], codes[1]def decode(self, codes: torch.Tensor, scale: tp.Optional[torch.Tensor] = None):B, K, T = codes.shapeassert T % self.num_virtual_steps == 0, "Provided codes' number of timesteps does not match"assert K == self.num_codebooks, "Provided codes' number of codebooks does not match"scale_c0, scale_c1 = None, Noneif scale is not None:assert scale.size(0) == B and scale.size(1) == 2, f"Scale has unexpected shape: {scale.shape}"scale_c0 = scale[0, ...]scale_c1 = scale[1, ...]codes_c0, codes_c1 = self.get_left_right_codes(codes)audio_c0 = self.model.decode(codes_c0, scale_c0)audio_c1 = self.model.decode(codes_c1, scale_c1)return torch.cat([audio_c0, audio_c1], dim=1)def decode_latent(self, codes: torch.Tensor):"""Decode from the discrete codes to continuous latent space."""raise NotImplementedError("Not supported by interleaved stereo wrapped models.")
40 unmodified lines4142434445464722 unmodified lines7071727374757653 unmodified lines13013113213313413513613713813939 unmodified lines17918018118218318418518618718818935 unmodified lines22522622722822923023123223323423523623723823924024129 unmodified lines27127227327427527627727827928033 unmodified lines3143153163173183193201 unmodified line32232332432532632732832933033133233333433533633733833934034134245 unmodified lines3883893903913923933941 unmodified line3963973983994004014024034044054064074084094101 unmodified line4124134144154164174183 unmodified lines4224234244254264274283 unmodified lines43243343443543643743818 unmodified lines45745845946046146246346425 unmodified lines49049149249349449549640 unmodified linesmethod (str): Method name for init function. Valid options are:'gaussian', 'uniform'.input_dim (int): Input dimension of the initialized module.init_depth (Optional[int]): Optional init depth value used to rescalethe standard deviation if defined."""# Compute std22 unmodified linesArgs:m (nn.Module): Module to initialize.method (str): Method name for the init function.init_depth (Optional[int]): Optional init depth value used to rescalethe standard deviation if defined.zero_bias_init (bool): Whether to initialize the bias to 0 or not."""53 unmodified lineshidden_scale (int): Scale for hidden feed forward dimension of the transformer encoder.norm (str): Normalization method.norm_first (bool): Use pre-norm instead of post-norm.emb_lr (Optional[float]): Embedding-specific learning rate.bias_proj (bool): Use bias for output projections.weight_init (Optional[str]): Method for weight initialization.depthwise_init (Optional[str]): Method for depthwise weight initialization.zero_bias_init (bool): If true and bias in Linears, initialize bias to zeros.cfg_dropout (float): Classifier-free guidance dropout.cfg_coef (float): Classifier-free guidance coefficient.39 unmodified lines"""Initialization of the transformer module weights.Args:weight_init (Optional[str]): Weight initialization strategy. See ``get_init_fn`` for valid options.depthwise_init (Optional[str]): Depwthwise initialization strategy. The following options are valid:'current' where the depth corresponds to the current layer index or 'global' where the total numberof layer is used as depth. If not set, no depthwise initialization strategy is used.zero_bias_init (bool): Whether to initalize bias to zero or not."""assert depthwise_init is None or depthwise_init in ['current', 'global']assert depthwise_init is None or weight_init is not None, \35 unmodified linesS the sequence steps, return the logits with shape [B, card, K, S].Args:indices (torch.Tensor): indices of the codes to model.conditions (list[ConditioningAttributes]): conditionings to use when modelingthe given codes. Note that when evaluating multiple time with the same conditioningyou should pre-compute those and pass them as `condition_tensors`.condition_tensors (dict[str, ConditionType] or None): pre-computed conditioningtensors, see `conditions`.Returns:torch.Tensor: Logits."""B, K, S = sequence.shapeassert K == self.num_codebooks, 'Sequence shape must match the specified number of codebooks'input_ = sum([self.emb[k](sequence[:, k]) for k in range(K)])if condition_tensors is None:assert not self._is_streaming, "Conditions tensors should be precomputed when streaming."29 unmodified linesArgs:codes (torch.Tensor): Input codes of shape [B, K, T] with B the batch size,K the number of codebooks and T the number of timesteps.conditions (list[ConditioningAttributes]): conditionings to use when modelingthe given codes. Note that when evaluating multiple time with the same conditioningyou should pre-compute those and pass them as `condition_tensors`.condition_tensors (dict[str, ConditionType] or None): pre-computed conditioningtensors, see `conditions`.Returns:LMOutput: Language model outputs33 unmodified linestemp: float = 1.0,top_k: int = 0,top_p: float = 0.0,cfg_coef: tp.Optional[float] = None) -> torch.Tensor:"""Sample next token from the model given a sequence and a set of conditions. The model supportsmultiple sampling strategies (greedy sampling, softmax, top-k, top-p...).1 unmodified linesequence (torch.Tensor): Current sequence of shape [B, K, S]with K corresponding to the number of codebooks and S the number of sequence steps.S = 1 in streaming mode, except for the first step that contains a bigger prompt.condition_tensors (Dict[str, ConditionType): Set of conditions. If CFG is used,should be twice the batch size, being the concatenation of the conditions + null conditions.use_sampling (bool): Whether to use a sampling strategy or not.temp (float): Sampling temperature.top_k (int): K for "top-k" sampling.top_p (float): P for "top-p" sampling.cfg_coef (float): classifier free guidance coefficientReturns:next_token (torch.Tensor): Next token tensor of shape [B, K, 1]."""B = sequence.shape[0]cfg_coef = self.cfg_coef if cfg_coef is None else cfg_coefmodel = self if self._fsdp is None else self._fsdpif self.two_step_cfg and cfg_conditions != {}:assert isinstance(cfg_conditions, tuple)condition_tensors, null_condition_tensors = cfg_conditionscond_logits = model(sequence, conditions=[], condition_tensors=condition_tensors)state = self.get_streaming_state()45 unmodified linestop_k: int = 250,top_p: float = 0.0,cfg_coef: tp.Optional[float] = None,two_step_cfg: bool = False,remove_prompts: bool = False,check: bool = False,callback: tp.Optional[tp.Callable[[int, int], None]] = None) -> torch.Tensor:1 unmodified linebe perform in a greedy fashion or using sampling with top K and top P strategies.Args:prompt (Optional[torch.Tensor]): Prompt tokens of shape [B, K, T].conditions_tensors (Dict[str, torch.Tensor]): Set of conditions or None.num_samples (int or None): Number of samples to generate when no prompt and no conditions are given.max_gen_len (int): Maximum generation length.use_sampling (bool): Whether to use a sampling strategy or not.temp (float): Sampling temperature.top_k (int): K for "top-k" sampling.top_p (float): P for "top-p" sampling.remove_prompts (bool): Whether to remove prompts from generation or not.Returns:torch.Tensor: Generated tokens."""1 unmodified linefirst_param = next(iter(self.parameters()))device = first_param.device# Checking all input shapes are consistents.possible_num_samples = []if num_samples is not None:possible_num_samples.append(num_samples)3 unmodified linespossible_num_samples.append(len(conditions))else:possible_num_samples.append(1)assert [x == possible_num_samples[0] for x in possible_num_samples], "Inconsitent inputs shapes"num_samples = possible_num_samples[0]# below we create set of conditions: one conditional and one unconditional3 unmodified lines# 1. it is about x2 faster than doing 2 forward passes# 2. avoid the streaming API treating the 2 passes as part of different time steps# We also support doing two different passes, in particular to ensure that# the padding structure is exactly the same between train anf test.# With a batch size of 1, this can be slower though.cfg_conditions: CFGConditionstwo_step_cfg = self.two_step_cfg if two_step_cfg is None else two_step_cfg18 unmodified linesB, K, T = prompt.shapestart_offset = Tprint(f"start_offset: {start_offset} | max_gen_len: {max_gen_len}")assert start_offset <= max_gen_lenpattern = self.pattern_provider.get_pattern(max_gen_len)# this token is used as default value for codes that are not generated yetunknown_token = -125 unmodified lines# sample next token from the model, next token shape is [B, K, 1]next_token = self._sample_next_token(curr_sequence, cfg_conditions, unconditional_state, use_sampling, temp, top_k, top_p,cfg_coef=cfg_coef)# ensure the tokens that should be masked are properly set to special_token_id# as the model never output special_token_idvalid_mask = mask[..., offset:offset+1].expand(B, -1, -1)40 unmodified lines4142434445464722 unmodified lines7071727374757653 unmodified lines13013113213313413513613713813939 unmodified lines17918018118218318418518618718818935 unmodified lines22522622722822923023123223323423523623723823924024129 unmodified lines27127227327427527627727827928033 unmodified lines3143153163173183193203211 unmodified line32332432532632732832933033133233333433533633733833934034134234334445 unmodified lines3903913923933943953961 unmodified line3983994004014024034044054064074084094104114124134144154161 unmodified line4184194204214224234243 unmodified lines4284294304314324334343 unmodified lines43843944044144244344418 unmodified lines46346446546646746846947025 unmodified lines49649749849950050150240 unmodified linesmethod (str): Method name for init function. Valid options are:'gaussian', 'uniform'.input_dim (int): Input dimension of the initialized module.init_depth (int, optional): Optional init depth value used to rescalethe standard deviation if defined."""# Compute std22 unmodified linesArgs:m (nn.Module): Module to initialize.method (str): Method name for the init function.init_depth (int, optional): Optional init depth value used to rescalethe standard deviation if defined.zero_bias_init (bool): Whether to initialize the bias to 0 or not."""53 unmodified lineshidden_scale (int): Scale for hidden feed forward dimension of the transformer encoder.norm (str): Normalization method.norm_first (bool): Use pre-norm instead of post-norm.emb_lr (float, optional): Embedding-specific learning rate.bias_proj (bool): Use bias for output projections.weight_init (str, optional): Method for weight initialization.depthwise_init (str, optional): Method for depthwise weight initialization.zero_bias_init (bool): If true and bias in Linears, initialize bias to zeros.cfg_dropout (float): Classifier-free guidance dropout.cfg_coef (float): Classifier-free guidance coefficient.39 unmodified lines"""Initialization of the transformer module weights.Args:weight_init (str, optional): Weight initialization strategy. See ``get_init_fn`` for valid options.depthwise_init (str, optional): Depthwise initialization strategy. The following options are valid:'current' where the depth corresponds to the current layer index or 'global' where the total numberof layer is used as depth. If not set, no depthwise initialization strategy is used.zero_bias_init (bool): Whether to initialize bias to zero or not."""assert depthwise_init is None or depthwise_init in ['current', 'global']assert depthwise_init is None or weight_init is not None, \35 unmodified linesS the sequence steps, return the logits with shape [B, card, K, S].Args:indices (torch.Tensor): Indices of the codes to model.conditions (list of ConditioningAttributes): Conditions to use when modelingthe given codes. Note that when evaluating multiple time with the same conditioningyou should pre-compute those and pass them as `condition_tensors`.condition_tensors (dict[str, ConditionType], optional): Pre-computed conditioningtensors, see `conditions`.Returns:torch.Tensor: Logits."""B, K, S = sequence.shapeassert K == self.num_codebooks, "Sequence shape must match the specified number of codebooks"input_ = sum([self.emb[k](sequence[:, k]) for k in range(K)])if condition_tensors is None:assert not self._is_streaming, "Conditions tensors should be precomputed when streaming."29 unmodified linesArgs:codes (torch.Tensor): Input codes of shape [B, K, T] with B the batch size,K the number of codebooks and T the number of timesteps.conditions (list of ConditioningAttributes): conditionings to use when modelingthe given codes. Note that when evaluating multiple time with the same conditioningyou should pre-compute those and pass them as `condition_tensors`.condition_tensors (dict[str, ConditionType], optional): pre-computed conditioningtensors, see `conditions`.Returns:LMOutput: Language model outputs33 unmodified linestemp: float = 1.0,top_k: int = 0,top_p: float = 0.0,cfg_coef: tp.Optional[float] = None,two_step_cfg: tp.Optional[bool] = None) -> torch.Tensor:"""Sample next token from the model given a sequence and a set of conditions. The model supportsmultiple sampling strategies (greedy sampling, softmax, top-k, top-p...).1 unmodified linesequence (torch.Tensor): Current sequence of shape [B, K, S]with K corresponding to the number of codebooks and S the number of sequence steps.S = 1 in streaming mode, except for the first step that contains a bigger prompt.condition_tensors (dict[str, ConditionType): Set of conditions. If CFG is used,should be twice the batch size, being the concatenation of the conditions + null conditions.use_sampling (bool): Whether to use a sampling strategy or not.temp (float): Sampling temperature.top_k (int): K for "top-k" sampling.top_p (float): P for "top-p" sampling.cfg_coef (float, optional): classifier free guidance coefficientReturns:next_token (torch.Tensor): Next token tensor of shape [B, K, 1]."""B = sequence.shape[0]cfg_coef = self.cfg_coef if cfg_coef is None else cfg_coefmodel = self if self._fsdp is None else self._fsdptwo_step_cfg = self.two_step_cfg if two_step_cfg is None else two_step_cfgif two_step_cfg and cfg_conditions != {}:assert isinstance(cfg_conditions, tuple), type(cfg_conditions)condition_tensors, null_condition_tensors = cfg_conditionscond_logits = model(sequence, conditions=[], condition_tensors=condition_tensors)state = self.get_streaming_state()45 unmodified linestop_k: int = 250,top_p: float = 0.0,cfg_coef: tp.Optional[float] = None,two_step_cfg: tp.Optional[bool] = None,remove_prompts: bool = False,check: bool = False,callback: tp.Optional[tp.Callable[[int, int], None]] = None) -> torch.Tensor:1 unmodified linebe perform in a greedy fashion or using sampling with top K and top P strategies.Args:prompt (torch.Tensor, optional): Prompt tokens of shape [B, K, T].conditions_tensors (list of ConditioningAttributes, optional): List of conditions.num_samples (int, optional): Number of samples to generate when no prompt and no conditions are given.max_gen_len (int): Maximum generation length.use_sampling (bool): Whether to use a sampling strategy or not.temp (float): Sampling temperature.top_k (int): K for "top-k" sampling.top_p (float): P for "top-p" sampling.cfg_coeff (float, optional): Classifier-free guidance coefficient.two_step_cfg (bool, optional): Whether to perform classifier-free guidance with two steps generation.remove_prompts (bool): Whether to remove prompts from generation or not.check (bool): Whether to apply further checks on generated sequence.callback (Callback, optional): Callback function to report generation progress.Returns:torch.Tensor: Generated tokens."""1 unmodified linefirst_param = next(iter(self.parameters()))device = first_param.device# Checking all input shapes are consistent.possible_num_samples = []if num_samples is not None:possible_num_samples.append(num_samples)3 unmodified linespossible_num_samples.append(len(conditions))else:possible_num_samples.append(1)assert [x == possible_num_samples[0] for x in possible_num_samples], "Inconsistent inputs shapes"num_samples = possible_num_samples[0]# below we create set of conditions: one conditional and one unconditional3 unmodified lines# 1. it is about x2 faster than doing 2 forward passes# 2. avoid the streaming API treating the 2 passes as part of different time steps# We also support doing two different passes, in particular to ensure that# the padding structure is exactly the same between train and test.# With a batch size of 1, this can be slower though.cfg_conditions: CFGConditionstwo_step_cfg = self.two_step_cfg if two_step_cfg is None else two_step_cfg18 unmodified linesB, K, T = prompt.shapestart_offset = Tprint(f"start_offset: {start_offset} | max_gen_len: {max_gen_len}")assert start_offset < max_gen_lenpattern = self.pattern_provider.get_pattern(max_gen_len)# this token is used as default value for codes that are not generated yetunknown_token = -125 unmodified lines# sample next token from the model, next token shape is [B, K, 1]next_token = self._sample_next_token(curr_sequence, cfg_conditions, unconditional_state, use_sampling, temp, top_k, top_p,cfg_coef=cfg_coef, two_step_cfg=two_step_cfg)# ensure the tokens that should be masked are properly set to special_token_id# as the model never output special_token_idvalid_mask = mask[..., offset:offset+1].expand(B, -1, -1)
23 unmodified lines2425262728293031323316 unmodified lines50515253545516 unmodified lines7273747576777879808182838485868788899091922 unmodified lines959697989910010110223 unmodified linesimport typing as tpimport osfrom omegaconf import OmegaConfimport torchfrom . import buildersHF_MODEL_CHECKPOINTS_MAP = {16 unmodified linesdevice='cpu',cache_dir: tp.Optional[str] = None,):# Return the state dict either from a file or urlfile_or_url_or_id = str(file_or_url_or_id)assert isinstance(file_or_url_or_id, str)16 unmodified linesreturn torch.load(file, map_location=device)else:raise ValueError(f"{file_or_url_or_id} is not a valid name, path or link that can be loaded.")def load_compression_model(file_or_url_or_id: tp.Union[Path, str], device='cpu', cache_dir: tp.Optional[str] = None):pkg = _get_state_dict(file_or_url_or_id, filename="compression_state_dict.bin", cache_dir=cache_dir)cfg = OmegaConf.create(pkg['xp.cfg'])cfg.device = str(device)model = builders.get_compression_model(cfg)model.load_state_dict(pkg['best_state'])model.eval()return modeldef load_lm_model(file_or_url_or_id: tp.Union[Path, str], device='cpu', cache_dir: tp.Optional[str] = None):pkg = _get_state_dict(file_or_url_or_id, filename="state_dict.bin", cache_dir=cache_dir)cfg = OmegaConf.create(pkg['xp.cfg'])cfg.device = str(device)if cfg.device == 'cpu':2 unmodified linescfg.dtype = 'float32'else:cfg.dtype = 'float16'model = builders.get_lm_model(cfg)model.load_state_dict(pkg['best_state'])model.eval()model.cfg = cfgreturn model23 unmodified lines2425262728293031323334353637383916 unmodified lines565758596061626316 unmodified lines80818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992 unmodified lines20220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224323 unmodified linesimport typing as tpimport osfrom omegaconf import OmegaConf, DictConfigimport torchimport audiocraftfrom . import buildersfrom .encodec import CompressionModeldef get_audiocraft_cache_dir() -> tp.Optional[str]:return os.environ.get('AUDIOCRAFT_CACHE_DIR', None)HF_MODEL_CHECKPOINTS_MAP = {16 unmodified linesdevice='cpu',cache_dir: tp.Optional[str] = None,):if cache_dir is None:cache_dir = get_audiocraft_cache_dir()# Return the state dict either from a file or urlfile_or_url_or_id = str(file_or_url_or_id)assert isinstance(file_or_url_or_id, str)16 unmodified linesreturn torch.load(file, map_location=device)else:assert filename is not None, "filename needs to be defined if using HF checkpoints"file = hf_hub_download(repo_id=file_or_url_or_id, filename=filename, cache_dir=cache_dir,library_name="audiocraft", library_version=audiocraft.__version__)return torch.load(file, map_location=device)def create_melody_config(model_id: str, device: str) -> DictConfig:"""Create a fallback configuration for melody models.Args:model_id: The model identifierdevice: The device to useReturns:A compatible OmegaConf DictConfig"""base_cfg = {"device": str(device),"channels": 2 if "stereo" in model_id else 1,"sample_rate": 32000,"audio_channels": 2 if "stereo" in model_id else 1,"frame_rate": 50,"codec_name": "encodec","codec": {"dim": 128,"hidden_dim": 1024,"stride": 320,"n_q": 4,"codebook_size": 2048,"normalize": True,}}return OmegaConf.create(base_cfg)def create_default_config(model_id: str, device: str) -> DictConfig:"""Create a fallback configuration for standard models.Args:model_id: The model identifierdevice: The device to useReturns:A compatible OmegaConf DictConfig"""base_cfg = {"device": str(device),"channels": 2 if "stereo" in model_id else 1,"sample_rate": 32000,"audio_channels": 2 if "stereo" in model_id else 1,"frame_rate": 50,"codec_name": "encodec","codec": {"dim": 128,"hidden_dim": 1024,"stride": 320,"n_q": 4,"codebook_size": 1024,"normalize": True,}}return OmegaConf.create(base_cfg)def load_compression_model_ckpt(file_or_url_or_id: tp.Union[Path, str], cache_dir: tp.Optional[str] = None):return _get_state_dict(file_or_url_or_id, filename="compression_state_dict.bin", cache_dir=cache_dir)def load_compression_model(file_or_url_or_id: tp.Union[Path, str], device='cpu', cache_dir: tp.Optional[str] = None):pkg = load_compression_model_ckpt(file_or_url_or_id, cache_dir=cache_dir)if 'pretrained' in pkg:return CompressionModel.get_pretrained(pkg['pretrained'], device=device)# Handle newer model formats that might not have xp.cfgif 'xp.cfg' not in pkg:if file_or_url_or_id in ['melody-large', 'stereo-melody', 'stereo-medium','stereo-small', 'stereo-large', 'stereo-melody-large']:print(f"Using fallback configuration for {file_or_url_or_id}")# Create a default configuration based on the model type# This is where you'd need to add model-specific configurationsif 'melody' in file_or_url_or_id:cfg = create_melody_config(file_or_url_or_id, device)else:cfg = create_default_config(file_or_url_or_id, device)else:raise KeyError(f"Missing configuration for model {file_or_url_or_id}")else:cfg = OmegaConf.create(pkg['xp.cfg'])cfg.device = str(device)model = builders.get_compression_model(cfg)model.load_state_dict(pkg['best_state'])model.eval()return modeldef load_lm_model_ckpt(file_or_url_or_id: tp.Union[Path, str], cache_dir: tp.Optional[str] = None):return _get_state_dict(file_or_url_or_id, filename="state_dict.bin", cache_dir=cache_dir)def _delete_param(cfg: DictConfig, full_name: str):parts = full_name.split('.')for part in parts[:-1]:if part in cfg:cfg = cfg[part]else:returnOmegaConf.set_struct(cfg, False)if parts[-1] in cfg:del cfg[parts[-1]]OmegaConf.set_struct(cfg, True)def load_lm_model(file_or_url_or_id: tp.Union[Path, str], device='cpu', cache_dir: tp.Optional[str] = None):pkg = load_lm_model_ckpt(file_or_url_or_id, cache_dir=cache_dir)cfg = OmegaConf.create(pkg['xp.cfg'])cfg.device = str(device)if cfg.device == 'cpu':2 unmodified linescfg.dtype = 'float32'else:cfg.dtype = 'float16'_delete_param(cfg, 'conditioners.self_wav.chroma_stem.cache_path')_delete_param(cfg, 'conditioners.args.merge_text_conditions_p')_delete_param(cfg, 'conditioners.args.drop_desc_p')model = builders.get_lm_model(cfg)model.load_state_dict(pkg['best_state'])model.eval()model.cfg = cfgreturn modeldef load_mbd_ckpt(file_or_url_or_id: tp.Union[Path, str],filename: tp.Optional[str] = None,cache_dir: tp.Optional[str] = None):return _get_state_dict(file_or_url_or_id, filename=filename, cache_dir=cache_dir)def load_diffusion_models(file_or_url_or_id: tp.Union[Path, str],device='cpu',filename: tp.Optional[str] = None,cache_dir: tp.Optional[str] = None):pkg = load_mbd_ckpt(file_or_url_or_id, filename=filename, cache_dir=cache_dir)models = []processors = []cfgs = []sample_rate = pkg['sample_rate']for i in range(pkg['n_bands']):cfg = pkg[i]['cfg']model = builders.get_diffusion_model(cfg)model_dict = pkg[i]['model_state']model.load_state_dict(model_dict)model.to(device)processor = builders.get_processor(cfg=cfg.processor, sample_rate=sample_rate)processor_dict = pkg[i]['processor_state']processor.load_state_dict(processor_dict)processor.to(device)models.append(model)processors.append(processor)cfgs.append(cfg)return models, processors, cfgs
10 unmodified lines1112131415161718192021222324252627286 unmodified lines35363738394041424344457 unmodified lines5354555657585940 unmodified lines10010110210310410510610710810911011113 unmodified lines1251261271281291306 unmodified lines13713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218313 unmodified lines19719819920020120220320420520642 unmodified lines2492502512522532542552562572582592602612622632642652662674 unmodified lines2722732742752762772785 unmodified lines2842852862872882892902912923 unmodified lines2962972982993003013023033043053062 unmodified lines30931031131231331431531631731831932032174 unmodified lines3963973983994004014024037 unmodified lines41141241341441541641741841942042142242310 unmodified linesimport osimport typing as tpimport torchfrom .encodec import CompressionModelfrom .lm import LMModelfrom .builders import get_debug_compression_model, get_debug_lm_modelfrom .loaders import load_compression_model, load_lm_model, HF_MODEL_CHECKPOINTS_MAPfrom ..data.audio_utils import convert_audiofrom ..modules.conditioners import ConditioningAttributes, WavConditionfrom ..utils.autocast import TorchAutocastMelodyList = tp.List[tp.Optional[torch.Tensor]]MelodyType = tp.Union[torch.Tensor, MelodyList]6 unmodified linescompression_model (CompressionModel): Compression modelused to map audio to invertible discrete representations.lm (LMModel): Language model over discrete representations."""def __init__(self, name: str, compression_model: CompressionModel, lm: LMModel, max_duration: float = 30):self.name = nameself.compression_model = compression_modelself.lm = lmself.max_duration = max_durationself.duration = 15.0 # default durationself.device = next(iter(lm.parameters())).device7 unmodified linesenabled=True, device_type=self.device.type, dtype=torch.float16)@propertydef frame_rate(self) -> int:"""Roughly the number of AR steps per seconds."""return self.compression_model.frame_rate40 unmodified linesf"{name} is not a valid checkpoint name. "f"Choose one of {', '.join(HF_MODEL_CHECKPOINTS_MAP.keys())}")cache_dir = os.environ.get('MUSICGEN_ROOT', None)compression_model = load_compression_model(name, device=device, cache_dir=cache_dir)lm = load_lm_model(name, device=device, cache_dir=cache_dir)if name == 'melody':lm.condition_provider.conditioners['self_wav'].match_len_on_eval = Truereturn MusicGen(name, compression_model, lm)13 unmodified linestwo_step_cfg (bool, optional): If True, performs 2 forward for Classifier Free Guidance,instead of batching together the two. This has some impact on how thingsare padded but seems to have little impact in practice.rep_penalty (float, optional): If set, use repetition penalty during generation. Not Implemented."""assert extend_stride < self.max_duration, "Cannot stride by more than max generation duration."6 unmodified lines'top_k': top_k,'top_p': top_p,'cfg_coef': cfg_coef,'two_step_cfg': two_step_cfg,}def set_custom_progress_callback(self, progress_callback: tp.Optional[tp.Callable[[int, int], None]] = None):"""Override the default progress callback."""self._progress_callback = progress_callbackdef generate_unconditional(self, num_samples: int, progress: bool = False) -> torch.Tensor:"""Generate samples in an unconditional manner.Args:num_samples (int): Number of samples to be generated.progress (bool, optional): Flag to display progress of the generation process. Defaults to False."""descriptions: tp.List[tp.Optional[str]] = [None] * num_samplesattributes, prompt_tokens = self._prepare_tokens_and_attributes(descriptions, None)return self._generate_tokens(attributes, prompt_tokens, progress)def generate(self, descriptions: tp.List[str], progress: bool = False) -> torch.Tensor:"""Generate samples conditioned on text.Args:descriptions (tp.List[str]): A list of strings used as text conditioning.progress (bool, optional): Flag to display progress of the generation process. Defaults to False."""attributes, prompt_tokens = self._prepare_tokens_and_attributes(descriptions, None)assert prompt_tokens is Nonereturn self._generate_tokens(attributes, prompt_tokens, progress)def generate_with_chroma(self, descriptions: tp.List[str], melody_wavs: MelodyType,melody_sample_rate: int, progress: bool = False) -> torch.Tensor:"""Generate samples conditioned on text and melody.Args:descriptions (tp.List[str]): A list of strings used as text conditioning.melody_wavs: (torch.Tensor or list of Tensor): A batch of waveforms used asmelody conditioning. Should have shape [B, C, T] with B matching the description length,C=1 or 2. It can be [C, T] if there is a single description. It can also bea list of [C, T] tensors.melody_sample_rate: (int): Sample rate of the melody waveforms.progress (bool, optional): Flag to display progress of the generation process. Defaults to False."""if isinstance(melody_wavs, torch.Tensor):if melody_wavs.dim() == 2:13 unmodified linesattributes, prompt_tokens = self._prepare_tokens_and_attributes(descriptions=descriptions, prompt=None,melody_wavs=melody_wavs)assert prompt_tokens is Nonereturn self._generate_tokens(attributes, prompt_tokens, progress)def generate_with_all(self, descriptions: tp.List[str], melody_wavs: MelodyType,sample_rate: int, progress: bool = False, prompt: tp.Optional[torch.Tensor] = None) -> torch.Tensor:"""Generate samples conditioned on text and melody and audio prompts.Args:descriptions (tp.List[str]): A list of strings used as text conditioning.42 unmodified linesassert prompt_tokens is not Noneelse:assert prompt_tokens is Nonereturn self._generate_tokens(attributes, prompt_tokens, progress)def generate_continuation(self, prompt: torch.Tensor, prompt_sample_rate: int,descriptions: tp.Optional[tp.List[tp.Optional[str]]] = None,progress: bool = False) -> torch.Tensor:"""Generate samples conditioned on audio prompts.Args:prompt (torch.Tensor): A batch of waveforms used for continuation.Prompt should be [B, C, T], or [C, T] if only one sample is generated.prompt_sample_rate (int): Sampling rate of the given audio waveforms.descriptions (tp.List[str], optional): A list of strings used as text conditioning. Defaults to None.progress (bool, optional): Flag to display progress of the generation process. Defaults to False."""if prompt.dim() == 2:prompt = prompt[None]4 unmodified linesdescriptions = [None] * len(prompt)attributes, prompt_tokens = self._prepare_tokens_and_attributes(descriptions, prompt)assert prompt_tokens is not Nonereturn self._generate_tokens(attributes, prompt_tokens, progress)@torch.no_grad()def _prepare_tokens_and_attributes(5 unmodified lines"""Prepare model inputs.Args:descriptions (tp.List[str]): A list of strings used as text conditioning.prompt (torch.Tensor): A batch of waveforms used for continuation.melody_wavs (tp.Optional[torch.Tensor], optional): A batch of waveformsused as melody conditioning. Defaults to None."""attributes = [3 unmodified linesif melody_wavs is None:for attr in attributes:attr.wav['self_wav'] = WavCondition(torch.zeros((1, 1), device=self.device),torch.tensor([0], device=self.device),path='null_wav') # type: ignoreelse:if self.name != "melody":raise RuntimeError("This model doesn't support melody conditioning. ""Use the `melody` model.")assert len(melody_wavs) == len(descriptions), \2 unmodified linesfor attr, melody in zip(attributes, melody_wavs):if melody is None:attr.wav['self_wav'] = WavCondition(torch.zeros((1, 1), device=self.device),torch.tensor([0], device=self.device),path='null_wav') # type: ignoreelse:attr.wav['self_wav'] = WavCondition(melody.to(device=self.device),torch.tensor([melody.shape[-1]], device=self.device))if prompt is not None:if descriptions is not None:74 unmodified linespositions = torch.arange(initial_position,initial_position + wav_target_length, device=self.device)attr.wav['self_wav'] = WavCondition(ref_wav[0][:, positions % wav_length],torch.full_like(ref_wav[1], wav_target_length))with self.autocast:gen_tokens = self.lm.generate(prompt_tokens, attributes,7 unmodified linescurrent_gen_offset += stride_tokensgen_tokens = torch.cat(all_tokens, dim=-1)# generate audioassert gen_tokens.dim() == 3with torch.no_grad():gen_audio = self.compression_model.decode(gen_tokens, None)return gen_audio#def _generate_tokens(self, attributes: tp.List[ConditioningAttributes],# prompt_tokens: tp.Optional[torch.Tensor], progress: bool = False) -> torch.Tensor:# """Generate discrete audio tokens given audio prompt and/or conditions.10 unmodified lines111213141516171819202122232425262728296 unmodified lines36373839404142434445464748495051525354555657585960616263646566677 unmodified lines75767778798081828384858640 unmodified lines12712812913013113213313413513613713813914014113 unmodified lines1551561571581591601611621636 unmodified lines17017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923013 unmodified lines24424524624724824925025125225325425525625742 unmodified lines3003013023033043053063073083093103113123133143153163173183193203213223234 unmodified lines3283293303313323333343353363375 unmodified lines3433443453463473483493503513 unmodified lines3553563573583593603613623633643653662 unmodified lines36937037137237337437537637737837938038138238338438574 unmodified lines4604614624634644654664674684697 unmodified lines47747847948048148248348448548648748848949049149249349449549649710 unmodified linesimport osimport typing as tpimport warningsimport omegaconfimport torchfrom .encodec import CompressionModelfrom .lm import LMModelfrom .builders import get_debug_compression_model, get_debug_lm_model, get_wrapped_compression_modelfrom .loaders import load_compression_model, load_lm_model, HF_MODEL_CHECKPOINTS_MAPfrom ..data.audio_utils import convert_audiofrom ..modules.conditioners import ConditioningAttributes, WavConditionfrom ..utils.autocast import TorchAutocastMelodyList = tp.List[tp.Optional[torch.Tensor]]MelodyType = tp.Union[torch.Tensor, MelodyList]6 unmodified linescompression_model (CompressionModel): Compression modelused to map audio to invertible discrete representations.lm (LMModel): Language model over discrete representations.max_duration (float, optional): maximum duration the model can produce,otherwise, inferred from the training params."""def __init__(self, name: str, compression_model: CompressionModel, lm: LMModel, max_duration: tp.Optional[float] = 30):self.name = nameself.compression_model = compression_modelself.lm = lmself.cfg: tp.Optional[omegaconf.DictConfig] = None# Just to be safe, let's put everything in eval mode.self.compression_model.eval()self.lm.eval()if hasattr(lm, 'cfg'):cfg = lm.cfgassert isinstance(cfg, omegaconf.DictConfig)self.cfg = cfgif self.cfg is not None:self.compression_model = get_wrapped_compression_model(self.compression_model, self.cfg)if max_duration is None:if self.cfg is not None:max_duration = lm.cfg.dataset.segment_duration # type: ignoreelse:raise ValueError("You must provide max_duration when building directly MusicGen")assert max_duration is not Noneself.max_duration = max_durationself.duration = 15.0 # default durationself.device = next(iter(lm.parameters())).device7 unmodified linesenabled=True, device_type=self.device.type, dtype=torch.float16)@propertydef version(self) -> str:from audiocraft import __version__ as audiocraft_versionreturn audiocraft_version@propertydef frame_rate(self) -> float:"""Roughly the number of AR steps per seconds."""return self.compression_model.frame_rate40 unmodified linesf"{name} is not a valid checkpoint name. "f"Choose one of {', '.join(HF_MODEL_CHECKPOINTS_MAP.keys())}")else:name = HF_MODEL_CHECKPOINTS_MAP[name]cache_dir = os.environ.get('MUSICGEN_ROOT', None)compression_model = load_compression_model(name, device=device, cache_dir=cache_dir)lm = load_lm_model(name, device=device, cache_dir=cache_dir)if name.__contains__('melody') or 'self_wav' in lm.condition_provider.conditioners:lm.condition_provider.conditioners['self_wav'].match_len_on_eval = Truelm.condition_provider.conditioners['self_wav']._use_masking = Falsereturn MusicGen(name, compression_model, lm)13 unmodified linestwo_step_cfg (bool, optional): If True, performs 2 forward for Classifier Free Guidance,instead of batching together the two. This has some impact on how thingsare padded but seems to have little impact in practice.extend_stride: when doing extended generation (i.e. more than 30 seconds), by how muchshould we extend the audio each time. Larger values will mean less context ispreserved, and shorter value will require extra computations.rep_penalty (float, optional): If set, use repetition penalty during generation. Not Implemented."""assert extend_stride < self.max_duration, "Cannot stride by more than max generation duration."6 unmodified lines'top_k': top_k,'top_p': top_p,'cfg_coef': cfg_coef,'two_step_cfg': two_step_cfg,}def set_custom_progress_callback(self, progress_callback: tp.Optional[tp.Callable[[int, int], None]] = None):"""Override the default progress callback."""self._progress_callback = progress_callbackdef generate_unconditional(self, num_samples: int, progress: bool = False,return_tokens: bool = False) -> tp.Union[torch.Tensor,tp.Tuple[torch.Tensor, torch.Tensor]]:"""Generate samples in an unconditional manner.Args:num_samples (int): Number of samples to be generated.progress (bool, optional): Flag to display progress of the generation process. Defaults to False.return_tokens (bool, optional): If True, also return the generated tokens. Defaults to False."""descriptions: tp.List[tp.Optional[str]] = [None] * num_samplesattributes, prompt_tokens = self._prepare_tokens_and_attributes(descriptions, None)tokens = self._generate_tokens(attributes, prompt_tokens, progress)if return_tokens:return self.generate_audio(tokens), tokensreturn self.generate_audio(tokens)def generate(self, descriptions: tp.List[str], progress: bool = False, return_tokens: bool = False) \-> tp.Union[torch.Tensor, tp.Tuple[torch.Tensor, torch.Tensor]]:"""Generate samples conditioned on text.Args:descriptions (list of str): A list of strings used as text conditioning.progress (bool, optional): Flag to display progress of the generation process. Defaults to False.return_tokens (bool, optional): If True, also return the generated tokens. Defaults to False."""attributes, prompt_tokens = self._prepare_tokens_and_attributes(descriptions, None)assert prompt_tokens is Nonetokens = self._generate_tokens(attributes, prompt_tokens, progress)if return_tokens:return self.generate_audio(tokens), tokensreturn self.generate_audio(tokens)def generate_with_chroma(self, descriptions: tp.List[str], melody_wavs: MelodyType,melody_sample_rate: int, progress: bool = False,return_tokens: bool = False) -> tp.Union[torch.Tensor,tp.Tuple[torch.Tensor, torch.Tensor]]:"""Generate samples conditioned on text and melody.Args:descriptions (list of str): A list of strings used as text conditioning.melody_wavs: (torch.Tensor or list of Tensor): A batch of waveforms used asmelody conditioning. Should have shape [B, C, T] with B matching the description length,C=1 or 2. It can be [C, T] if there is a single description. It can also bea list of [C, T] tensors.melody_sample_rate: (int): Sample rate of the melody waveforms.progress (bool, optional): Flag to display progress of the generation process. Defaults to False.return_tokens (bool, optional): If True, also return the generated tokens. Defaults to False."""if isinstance(melody_wavs, torch.Tensor):if melody_wavs.dim() == 2:13 unmodified linesattributes, prompt_tokens = self._prepare_tokens_and_attributes(descriptions=descriptions, prompt=None,melody_wavs=melody_wavs)assert prompt_tokens is Nonetokens = self._generate_tokens(attributes, prompt_tokens, progress)if return_tokens:return self.generate_audio(tokens), tokensreturn self.generate_audio(tokens)def generate_with_all(self, descriptions: tp.List[str], melody_wavs: MelodyType,sample_rate: int, progress: bool = False, prompt: tp.Optional[torch.Tensor] = None, return_tokens: bool = False) \-> tp.Union[torch.Tensor, tp.Tuple[torch.Tensor, torch.Tensor]]:"""Generate samples conditioned on text and melody and audio prompts.Args:descriptions (tp.List[str]): A list of strings used as text conditioning.42 unmodified linesassert prompt_tokens is not Noneelse:assert prompt_tokens is Nonetokens = self._generate_tokens(attributes, prompt_tokens, progress)if return_tokens:return self.generate_audio(tokens), tokensreturn self.generate_audio(tokens)def generate_continuation(self, prompt: torch.Tensor, prompt_sample_rate: int,descriptions: tp.Optional[tp.List[tp.Optional[str]]] = None,progress: bool = False, return_tokens: bool = False) \-> tp.Union[torch.Tensor, tp.Tuple[torch.Tensor, torch.Tensor]]:"""Generate samples conditioned on audio prompts.Args:prompt (torch.Tensor): A batch of waveforms used for continuation.Prompt should be [B, C, T], or [C, T] if only one sample is generated.prompt_sample_rate (int): Sampling rate of the given audio waveforms.descriptions (list of str, optional): A list of strings used as text conditioning. Defaults to None.progress (bool, optional): Flag to display progress of the generation process. Defaults to False.return_tokens (bool, optional): If True, also return the generated tokens. Defaults to False."""if prompt.dim() == 2:prompt = prompt[None]4 unmodified linesdescriptions = [None] * len(prompt)attributes, prompt_tokens = self._prepare_tokens_and_attributes(descriptions, prompt)assert prompt_tokens is not Nonetokens = self._generate_tokens(attributes, prompt_tokens, progress)if return_tokens:return self.generate_audio(tokens), tokensreturn self.generate_audio(tokens)@torch.no_grad()def _prepare_tokens_and_attributes(5 unmodified lines"""Prepare model inputs.Args:descriptions (list of str): A list of strings used as text conditioning.prompt (torch.Tensor): A batch of waveforms used for continuation.melody_wavs (torch.Tensor, optional): A batch of waveformsused as melody conditioning. Defaults to None."""attributes = [3 unmodified linesif melody_wavs is None:for attr in attributes:attr.wav['self_wav'] = WavCondition(torch.zeros((1, 1, 1), device=self.device),torch.tensor([0], device=self.device),sample_rate=[self.sample_rate],path=[None]) # type: ignoreelse:if 'self_wav' not in self.lm.condition_provider.conditioners:raise RuntimeError("This model doesn't support melody conditioning. ""Use the `melody` model.")assert len(melody_wavs) == len(descriptions), \2 unmodified linesfor attr, melody in zip(attributes, melody_wavs):if melody is None:attr.wav['self_wav'] = WavCondition(torch.zeros((1, 1, 1), device=self.device),torch.tensor([0], device=self.device),sample_rate=[self.sample_rate],path=[None]) # type: ignoreelse:attr.wav['self_wav'] = WavCondition(melody[None].to(device=self.device),torch.tensor([melody.shape[-1]], device=self.device),sample_rate=[self.sample_rate],path=[None],)if prompt is not None:if descriptions is not None:74 unmodified linespositions = torch.arange(initial_position,initial_position + wav_target_length, device=self.device)attr.wav['self_wav'] = WavCondition(ref_wav[0][..., positions % wav_length],torch.full_like(ref_wav[1], wav_target_length),[self.sample_rate] * ref_wav[0].size(0),[None], [0.])with self.autocast:gen_tokens = self.lm.generate(prompt_tokens, attributes,7 unmodified linescurrent_gen_offset += stride_tokensgen_tokens = torch.cat(all_tokens, dim=-1)return gen_tokens# generate audiodef generate_audio(self, gen_tokens: torch.Tensor):try:"""Generate Audio from tokens"""assert gen_tokens.dim() == 3with torch.no_grad():gen_audio = self.compression_model.decode(gen_tokens, None)return gen_audioexcept Exception as e:print(f"Error generating audio: {e}")return None#def _generate_tokens(self, attributes: tp.List[ConditioningAttributes],# prompt_tokens: tp.Optional[torch.Tensor], progress: bool = False) -> torch.Tensor:# """Generate discrete audio tokens given audio prompt and/or conditions.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214# Copyright (c) Meta Platforms, Inc. and affiliates.# All rights reserved.## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.
"""Pytorch Unet Module used for diffusion."""
from dataclasses import dataclassimport typing as tp
import torchfrom torch import nnfrom torch.nn import functional as Ffrom audiocraft.modules.transformer import StreamingTransformer, create_sin_embedding
@dataclassclass Output: sample: torch.Tensor
def get_model(cfg, channels: int, side: int, num_steps: int): if cfg.model == 'unet': return DiffusionUnet( chin=channels, num_steps=num_steps, **cfg.diffusion_unet) else: raise RuntimeError('Not Implemented')
class ResBlock(nn.Module): def __init__(self, channels: int, kernel: int = 3, norm_groups: int = 4, dilation: int = 1, activation: tp.Type[nn.Module] = nn.ReLU, dropout: float = 0.): super().__init__() stride = 1 padding = dilation * (kernel - stride) // 2 Conv = nn.Conv1d Drop = nn.Dropout1d self.norm1 = nn.GroupNorm(norm_groups, channels) self.conv1 = Conv(channels, channels, kernel, 1, padding, dilation=dilation) self.activation1 = activation() self.dropout1 = Drop(dropout)
self.norm2 = nn.GroupNorm(norm_groups, channels) self.conv2 = Conv(channels, channels, kernel, 1, padding, dilation=dilation) self.activation2 = activation() self.dropout2 = Drop(dropout)
def forward(self, x): h = self.dropout1(self.conv1(self.activation1(self.norm1(x)))) h = self.dropout2(self.conv2(self.activation2(self.norm2(h)))) return x + h
class DecoderLayer(nn.Module): def __init__(self, chin: int, chout: int, kernel: int = 4, stride: int = 2, norm_groups: int = 4, res_blocks: int = 1, activation: tp.Type[nn.Module] = nn.ReLU, dropout: float = 0.): super().__init__() padding = (kernel - stride) // 2 self.res_blocks = nn.Sequential( *[ResBlock(chin, norm_groups=norm_groups, dilation=2**idx, dropout=dropout) for idx in range(res_blocks)]) self.norm = nn.GroupNorm(norm_groups, chin) ConvTr = nn.ConvTranspose1d self.convtr = ConvTr(chin, chout, kernel, stride, padding, bias=False) self.activation = activation()
def forward(self, x: torch.Tensor) -> torch.Tensor: x = self.res_blocks(x) x = self.norm(x) x = self.activation(x) x = self.convtr(x) return x
class EncoderLayer(nn.Module): def __init__(self, chin: int, chout: int, kernel: int = 4, stride: int = 2, norm_groups: int = 4, res_blocks: int = 1, activation: tp.Type[nn.Module] = nn.ReLU, dropout: float = 0.): super().__init__() padding = (kernel - stride) // 2 Conv = nn.Conv1d self.conv = Conv(chin, chout, kernel, stride, padding, bias=False) self.norm = nn.GroupNorm(norm_groups, chout) self.activation = activation() self.res_blocks = nn.Sequential( *[ResBlock(chout, norm_groups=norm_groups, dilation=2**idx, dropout=dropout) for idx in range(res_blocks)])
def forward(self, x: torch.Tensor) -> torch.Tensor: B, C, T = x.shape stride, = self.conv.stride pad = (stride - (T % stride)) % stride x = F.pad(x, (0, pad))
x = self.conv(x) x = self.norm(x) x = self.activation(x) x = self.res_blocks(x) return x
class BLSTM(nn.Module): """BiLSTM with same hidden units as input dim. """ def __init__(self, dim, layers=2): super().__init__() self.lstm = nn.LSTM(bidirectional=True, num_layers=layers, hidden_size=dim, input_size=dim) self.linear = nn.Linear(2 * dim, dim)
def forward(self, x): x = x.permute(2, 0, 1) x = self.lstm(x)[0] x = self.linear(x) x = x.permute(1, 2, 0) return x
class DiffusionUnet(nn.Module): def __init__(self, chin: int = 3, hidden: int = 24, depth: int = 3, growth: float = 2., max_channels: int = 10_000, num_steps: int = 1000, emb_all_layers=False, cross_attention: bool = False, bilstm: bool = False, transformer: bool = False, codec_dim: tp.Optional[int] = None, **kwargs): super().__init__() self.encoders = nn.ModuleList() self.decoders = nn.ModuleList() self.embeddings: tp.Optional[nn.ModuleList] = None self.embedding = nn.Embedding(num_steps, hidden) if emb_all_layers: self.embeddings = nn.ModuleList() self.condition_embedding: tp.Optional[nn.Module] = None for d in range(depth): encoder = EncoderLayer(chin, hidden, **kwargs) decoder = DecoderLayer(hidden, chin, **kwargs) self.encoders.append(encoder) self.decoders.insert(0, decoder) if emb_all_layers and d > 0: assert self.embeddings is not None self.embeddings.append(nn.Embedding(num_steps, hidden)) chin = hidden hidden = min(int(chin * growth), max_channels) self.bilstm: tp.Optional[nn.Module] if bilstm: self.bilstm = BLSTM(chin) else: self.bilstm = None self.use_transformer = transformer self.cross_attention = False if transformer: self.cross_attention = cross_attention self.transformer = StreamingTransformer(chin, 8, 6, bias_ff=False, bias_attn=False, cross_attention=cross_attention)
self.use_codec = False if codec_dim is not None: self.conv_codec = nn.Conv1d(codec_dim, chin, 1) self.use_codec = True
def forward(self, x: torch.Tensor, step: tp.Union[int, torch.Tensor], condition: tp.Optional[torch.Tensor] = None): skips = [] bs = x.size(0) z = x view_args = [1] if type(step) is torch.Tensor: step_tensor = step else: step_tensor = torch.tensor([step], device=x.device, dtype=torch.long).expand(bs)
for idx, encoder in enumerate(self.encoders): z = encoder(z) if idx == 0: z = z + self.embedding(step_tensor).view(bs, -1, *view_args).expand_as(z) elif self.embeddings is not None: z = z + self.embeddings[idx - 1](step_tensor).view(bs, -1, *view_args).expand_as(z)
skips.append(z)
if self.use_codec: # insert condition in the bottleneck assert condition is not None, "Model defined for conditionnal generation" condition_emb = self.conv_codec(condition) # reshape to the bottleneck dim assert condition_emb.size(-1) <= 2 * z.size(-1), \ f"You are downsampling the conditionning with factor >=2 : {condition_emb.size(-1)=} and {z.size(-1)=}" if not self.cross_attention:
condition_emb = torch.nn.functional.interpolate(condition_emb, z.size(-1)) assert z.size() == condition_emb.size() z += condition_emb cross_attention_src = None else: cross_attention_src = condition_emb.permute(0, 2, 1) # B, T, C B, T, C = cross_attention_src.shape positions = torch.arange(T, device=x.device).view(1, -1, 1) pos_emb = create_sin_embedding(positions, C, max_period=10_000, dtype=cross_attention_src.dtype) cross_attention_src = cross_attention_src + pos_emb if self.use_transformer: z = self.transformer(z.permute(0, 2, 1), cross_attention_src=cross_attention_src).permute(0, 2, 1) else: if self.bilstm is None: z = torch.zeros_like(z) else: z = self.bilstm(z)
for decoder in self.decoders: s = skips.pop(-1) z = z[:, :, :s.shape[2]] z = z + s z = decoder(z)
z = z[:, :, :x.shape[2]] return Output(z)17 unmodified lines18192017 unmodified lines)from .lstm import StreamableLSTMfrom .seanet import SEANetEncoder, SEANetDecoder17 unmodified lines1819202117 unmodified lines)from .lstm import StreamableLSTMfrom .seanet import SEANetEncoder, SEANetDecoderfrom .transformer import StreamingTransformer
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566# Copyright (c) Meta Platforms, Inc. and affiliates.# All rights reserved.## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.import typing as tp
from einops import rearrangefrom librosa import filtersimport torchfrom torch import nnimport torch.nn.functional as Fimport torchaudio
class ChromaExtractor(nn.Module): """Chroma extraction and quantization.
Args: sample_rate (int): Sample rate for the chroma extraction. n_chroma (int): Number of chroma bins for the chroma extraction. radix2_exp (int): Size of stft window for the chroma extraction (power of 2, e.g. 12 -> 2^12). nfft (int, optional): Number of FFT. winlen (int, optional): Window length. winhop (int, optional): Window hop size. argmax (bool, optional): Whether to use argmax. Defaults to False. norm (float, optional): Norm for chroma normalization. Defaults to inf. """ def __init__(self, sample_rate: int, n_chroma: int = 12, radix2_exp: int = 12, nfft: tp.Optional[int] = None, winlen: tp.Optional[int] = None, winhop: tp.Optional[int] = None, argmax: bool = False, norm: float = torch.inf): super().__init__() self.winlen = winlen or 2 ** radix2_exp self.nfft = nfft or self.winlen self.winhop = winhop or (self.winlen // 4) self.sample_rate = sample_rate self.n_chroma = n_chroma self.norm = norm self.argmax = argmax self.register_buffer('fbanks', torch.from_numpy(filters.chroma(sr=sample_rate, n_fft=self.nfft, tuning=0, n_chroma=self.n_chroma)), persistent=False) self.spec = torchaudio.transforms.Spectrogram(n_fft=self.nfft, win_length=self.winlen, hop_length=self.winhop, power=2, center=True, pad=0, normalized=True)
def forward(self, wav: torch.Tensor) -> torch.Tensor: T = wav.shape[-1] # in case we are getting a wav that was dropped out (nullified) # from the conditioner, make sure wav length is no less that nfft if T < self.nfft: pad = self.nfft - T r = 0 if pad % 2 == 0 else 1 wav = F.pad(wav, (pad // 2, pad // 2 + r), 'constant', 0) assert wav.shape[-1] == self.nfft, f"expected len {self.nfft} but got {wav.shape[-1]}"
spec = self.spec(wav).squeeze(1) raw_chroma = torch.einsum('cf,...ft->...ct', self.fbanks, spec) norm_chroma = torch.nn.functional.normalize(raw_chroma, p=self.norm, dim=-2, eps=1e-6) norm_chroma = rearrange(norm_chroma, 'b d t -> b t d')
if self.argmax: idx = norm_chroma.argmax(-1, keepdim=True) norm_chroma[:] = 0 norm_chroma.scatter_(dim=-1, index=idx, value=1)
return norm_chroma121 unmodified lines12212312412512612712860 unmodified lines18919019119219319419519619797 unmodified lines29529629729829930030116 unmodified lines31831932032132232332481 unmodified lines40640740840941041141241341441546 unmodified lines46246346446546646746817 unmodified lines486487488489490491492493494495496497498121 unmodified linesArgs:timesteps (int): Maximum number of timesteps steps to consider.keep_only_valid_steps (bool): Restrict the pattern layout to match only valid steps.device (Union[torch.device, str]): Device for created tensors.Returns:indexes (torch.Tensor): Indexes corresponding to the sequence, of shape [K, S].mask (torch.Tensor): Mask corresponding to indexes that matches valid indexes, of shape [K, S].60 unmodified lineskeep_only_valid_steps (bool): Build a sequence from the pattern up to valid (= fully defined) steps.Steps that are beyond valid steps will be replaced by the special_token in that case.is_model_output (bool): Whether to keep the sequence item corresponding to initial special token or not.device (Union[torch.device, str]): Device for created tensors.Returns:torch.Tensor: Indexes for reconstructing the output, of shape [K, T].mask (torch.Tensor): Mask corresponding to indexes that matches valid indexes of shape [K, T]."""ref_layout = self.valid_layout if keep_only_valid_steps else self.layout97 unmodified lines"""Builds pattern with specific interleaving between codebooks.Args:timesteps (int): Total numer of timesteps."""raise NotImplementedError()16 unmodified linesArgs:n_q (int): Number of codebooks.delays (Optional[List[int]]): Delay for each of the codebooks.If delays not defined, each codebook is delayed by 1 compared to the previous one.flatten_first (int): Flatten the first N timesteps.empty_initial (int): Prepend with N empty list of coordinates.81 unmodified linesArgs:n_q (int): Number of codebooks.flattening (Optional[List[int]]): Flattening schema over the codebooks. If not defined,the codebooks will be flattened to 1 codebook per step, meaning that the sequence willhave n_q extra steps for each timestep.delays (Optional[List[int]]): Delay for each of the codebooks. If not defined,no delay is added and therefore will default to [0] * ``n_q``.Note that two codebooks that will be flattened to the same inner stepshould have the same delay, otherwise the pattern is considered as invalid.46 unmodified lines"""Builds pattern for delay across codebooks.Args:timesteps (int): Total numer of timesteps."""# the PatternLayout is built as a tuple of sequence position and list of coordinates# so that it can be reordered properly given the required delay between codebooks of given timesteps17 unmodified linesreturn Pattern(out, n_q=self.n_q, timesteps=timesteps)class VALLEPattern(CodebooksPatternProvider):"""Almost VALL-E style pattern. We futher allow some delays for thecodebooks other than the first one.Args:n_q (int): Number of codebooks.delays (Optional[List[int]]): Delay for each of the codebooks.If delays not defined, each codebook is delayed by 1 compared to the previous one."""def __init__(self, n_q: int, delays: tp.Optional[tp.List[int]] = None):121 unmodified lines12212312412512612712860 unmodified lines18919019119219319419519619797 unmodified lines29529629729829930030116 unmodified lines31831932032132232332481 unmodified lines40640740840941041141241341441546 unmodified lines46246346446546646746817 unmodified lines486487488489490491492493494495496497498499500501502503121 unmodified linesArgs:timesteps (int): Maximum number of timesteps steps to consider.keep_only_valid_steps (bool): Restrict the pattern layout to match only valid steps.device (torch.device or str): Device for created tensors.Returns:indexes (torch.Tensor): Indexes corresponding to the sequence, of shape [K, S].mask (torch.Tensor): Mask corresponding to indexes that matches valid indexes, of shape [K, S].60 unmodified lineskeep_only_valid_steps (bool): Build a sequence from the pattern up to valid (= fully defined) steps.Steps that are beyond valid steps will be replaced by the special_token in that case.is_model_output (bool): Whether to keep the sequence item corresponding to initial special token or not.device (torch.device or str): Device for created tensors.Returns:indexes (torch.Tensor): Indexes for reconstructing the output, of shape [K, T].mask (torch.Tensor): Mask corresponding to indexes that matches valid indexes of shape [K, T]."""ref_layout = self.valid_layout if keep_only_valid_steps else self.layout97 unmodified lines"""Builds pattern with specific interleaving between codebooks.Args:timesteps (int): Total number of timesteps."""raise NotImplementedError()16 unmodified linesArgs:n_q (int): Number of codebooks.delays (list of int, optional): Delay for each of the codebooks.If delays not defined, each codebook is delayed by 1 compared to the previous one.flatten_first (int): Flatten the first N timesteps.empty_initial (int): Prepend with N empty list of coordinates.81 unmodified linesArgs:n_q (int): Number of codebooks.flattening (list of int, optional): Flattening schema over the codebooks. If not defined,the codebooks will be flattened to 1 codebook per step, meaning that the sequence willhave n_q extra steps for each timestep.delays (list of int, optional): Delay for each of the codebooks. If not defined,no delay is added and therefore will default to [0] * ``n_q``.Note that two codebooks that will be flattened to the same inner stepshould have the same delay, otherwise the pattern is considered as invalid.46 unmodified lines"""Builds pattern for delay across codebooks.Args:timesteps (int): Total number of timesteps."""# the PatternLayout is built as a tuple of sequence position and list of coordinates# so that it can be reordered properly given the required delay between codebooks of given timesteps17 unmodified linesreturn Pattern(out, n_q=self.n_q, timesteps=timesteps)class CoarseFirstPattern(CodebooksPatternProvider):"""First generates all the codebooks #1 (e.g. coarser), then the remaining ones,potentially with delays...Warning:: You must always generate the full training duration at test time, for instance,30 seconds, as otherwise, the fine codebooks will start being generated in an unexpectedlocation. This is due to the non causality of the remaining codebooks with respect tothe first ones.Args:n_q (int): Number of codebooks.delays (list of int, optional): Delay for each of the codebooks.If delays not defined, each codebook is delayed by 1 compared to the previous one."""def __init__(self, n_q: int, delays: tp.Optional[tp.List[int]] = None):
9 unmodified lines1011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495966 unmodified lines10310410510610710810911011111211311411511614 unmodified lines1311321331341351361371381391401414 unmodified lines1461471481491501511528 unmodified lines1611621631641651661671681691701711721731741751761771782 unmodified lines1811821831841851861874 unmodified lines19219319419519619719819920020120220320420520617 unmodified lines22422522622722822923010 unmodified lines24124224324424524624724824925025125225325425538 unmodified lines29429529629729829930030130243 unmodified lines34634734834935035135235335435535635735819 unmodified lines37837938038138238338413 unmodified lines39839940040140240340440540640740840941015 unmodified lines4264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286291 unmodified line6316326336346356366371 unmodified line63964064164264364464564664764816 unmodified lines6656666676686696706716726736746756766776786796806816826836846851 unmodified line6876886896906916926936946 unmodified lines7017027037047057067077087095 unmodified lines7157167177187197207217227237247257261 unmodified line72872973073173273373473573673773873974074174274374474574674774874975075175275375475575612 unmodified lines76977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080118 unmodified lines8208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698702 unmodified lines87387487587687787887988088188288388488588688788888989089189289389489589689789889920 unmodified lines9209219229239249259264 unmodified lines93193293393493593693793893994094194294394494594612 unmodified lines9599609619629639649659669679689699709719729739749 unmodified linesfrom itertools import chainimport loggingimport mathimport randomimport reimport typing as tpimport warningsfrom einops import rearrangefrom num2words import num2wordsimport spacyfrom transformers import T5EncoderModel, T5Tokenizer # type: ignoreimport torchaudioimport torchfrom torch import nnfrom torch import Tensorimport torch.nn.functional as Ffrom torch.nn.utils.rnn import pad_sequencefrom .streaming import StreamingModulefrom .transformer import create_sin_embeddingfrom ..data.audio_dataset import SegmentInfofrom ..utils.autocast import TorchAutocastfrom ..utils.utils import hash_trick, length_to_mask, collatelogger = logging.getLogger(__name__)TextCondition = tp.Optional[str] # a text condition can be a string or None (if doesn't exist)ConditionType = tp.Tuple[Tensor, Tensor] # condition, maskclass WavCondition(tp.NamedTuple):wav: Tensorlength: Tensorpath: tp.List[tp.Optional[str]] = []def nullify_condition(condition: ConditionType, dim: int = 1):"""This function transforms an input condition to a null condition.The way it is done by converting it to a single zero vector similarlyto how it is done inside WhiteSpaceTokenizer and NoopTokenizer.Args:condition (ConditionType): a tuple of condition and mask (tp.Tuple[Tensor, Tensor])dim (int): the dimension that will be truncated (should be the time dimension)WARNING!: dim should not be the batch dimension!Returns:ConditionType: a tuple of null condition and mask"""assert dim != 0, "dim cannot be the batch dimension!"assert type(condition) == tuple and \type(condition[0]) == Tensor and \type(condition[1]) == Tensor, "'nullify_condition' got an unexpected input type!"cond, mask = conditionB = cond.shape[0]last_dim = cond.dim() - 1out = cond.transpose(dim, last_dim)out = 0. * out[..., :1]out = out.transpose(dim, last_dim)mask = torch.zeros((B, 1), device=out.device).int()assert cond.dim() == out.dim()return out, maskdef nullify_wav(wav: Tensor) -> WavCondition:"""Create a nullified WavCondition from a wav tensor with appropriate shape.Args:wav (Tensor): tensor of shape [B, T]Returns:WavCondition: wav condition with nullified wav."""null_wav, _ = nullify_condition((wav, torch.zeros_like(wav)), dim=wav.dim() - 1)return WavCondition(wav=null_wav,length=torch.tensor([0] * wav.shape[0], device=wav.device),path=['null_wav'] * wav.shape[0])@dataclassclass ConditioningAttributes:text: tp.Dict[str, tp.Optional[str]] = field(default_factory=dict)wav: tp.Dict[str, WavCondition] = field(default_factory=dict)def __getitem__(self, item):return getattr(self, item)6 unmodified linesdef wav_attributes(self):return self.wav.keys()@propertydef attributes(self):return {"text": self.text_attributes, "wav": self.wav_attributes}def to_flat_dict(self):return {**{f"text.{k}": v for k, v in self.text.items()},**{f"wav.{k}": v for k, v in self.wav.items()},}@classmethod14 unmodified linesraise NotImplementedError()class Tokenizer:"""Base class for all tokenizers(in case we want to introduce more advances tokenizers in the future)."""def __call__(self, texts: tp.List[tp.Optional[str]]) -> tp.Tuple[Tensor, Tensor]:raise NotImplementedError()4 unmodified lines[[78, 62, 31, 4, 78, 25, 19, 34],[59, 77, 0, 0, 0, 0, 0, 0]]"""PUNCTUATIONS = "?:!.,;"def __init__(self, n_bins: int, pad_idx: int = 0, language: str = "en_core_web_sm",lemma: bool = True, stopwords: bool = True) -> None:8 unmodified linesself.nlp = spacy.load(language)@tp.no_type_checkdef __call__(self,texts: tp.List[tp.Optional[str]],return_text: bool = False) -> tp.Tuple[Tensor, Tensor]:"""Take a list of strings and convert them to a tensor of indices.Args:texts (tp.List[str]): List of strings.return_text (bool, optional): Whether to return text as additional tuple item. Defaults to False.Returns:tp.Tuple[Tensor, Tensor]:- Indices of words in the LUT.- And a mask indicating where the padding tokens are"""2 unmodified linesfor i, text in enumerate(texts):# if current sample doesn't have a certain attribute, replace with pad tokenif text is None:output.append(Tensor([self.pad_idx]))lengths.append(0)continue4 unmodified lines# remove stopwordsif self.stopwords:text = [w for w in text if not w.is_stop] # type: ignore# remove punctuationstext = [w for w in text if w.text not in self.PUNCTUATIONS] # type: ignore# lemmatize if neededtext = [getattr(t, "lemma_" if self.lemma else "text") for t in text] # type: ignoretexts[i] = " ".join(text)lengths.append(len(text))# convert to tensortokens = Tensor([hash_trick(w, self.n_bins) for w in text])output.append(tokens)mask = length_to_mask(torch.IntTensor(lengths)).int()17 unmodified linesself.n_bins = n_binsself.pad_idx = pad_idxdef __call__(self, texts: tp.List[tp.Optional[str]]) -> tp.Tuple[Tensor, Tensor]:output, lengths = [], []for text in texts:# if current sample doesn't have a certain attribute, replace with pad token10 unmodified linesclass BaseConditioner(nn.Module):"""Base model for all conditioner modules. We allow the output dim to be differentthan the hidden dim for two reasons: 1) keep our LUTs small when the vocab is large;2) make all condition dims consistent.Args:dim (int): Hidden dim of the model (text-encoder/LUT).output_dim (int): Output dim of the conditioner."""def __init__(self, dim, output_dim):super().__init__()self.dim = dimself.output_dim = output_dim38 unmodified linessuper().__init__(dim, output_dim)self.embed = nn.Embedding(n_bins, dim)self.tokenizer: Tokenizerif tokenizer == "whitespace":self.tokenizer = WhiteSpaceTokenizer(n_bins, pad_idx=pad_idx)elif tokenizer == "noop":self.tokenizer = NoopTokenizer(n_bins, pad_idx=pad_idx)else:raise ValueError(f"unrecognized tokenizer `{tokenizer}`.")43 unmodified linesdef __init__(self, name: str, output_dim: int, finetune: bool, device: str,autocast_dtype: tp.Optional[str] = 'float32', word_dropout: float = 0.,normalize_text: bool = False):assert name in self.MODELS, f"unrecognized t5 model name (should in {self.MODELS})"super().__init__(self.MODELS_DIMS[name], output_dim)self.device = deviceself.name = nameself.finetune = finetuneself.word_dropout = word_dropoutif autocast_dtype is None or self.device == 'cpu':self.autocast = TorchAutocast(enabled=False)if self.device != 'cpu':19 unmodified lineselse:# this makes sure that the t5 models is not part# of the saved checkpointself.__dict__["t5"] = t5.to(device)self.normalize_text = normalize_textif normalize_text:13 unmodified linesempty_idx = torch.LongTensor([i for i, xi in enumerate(entries) if xi == ""])inputs = self.t5_tokenizer(entries, return_tensors="pt", padding=True).to(self.device)mask = inputs["attention_mask"]mask[empty_idx, :] = 0 # zero-out index where the input is non-existantreturn inputsdef forward(self, inputs: tp.Dict[str, torch.Tensor]) -> ConditionType:mask = inputs["attention_mask"]with torch.set_grad_enabled(self.finetune), self.autocast:embeds = self.t5(**inputs).last_hidden_stateembeds = self.output_proj(embeds.to(self.output_proj.weight))15 unmodified linesdef __init__(self, dim: int, output_dim: int, device: tp.Union[torch.device, str]):super().__init__(dim, output_dim)self.device = devicedef tokenize(self, wav_length: WavCondition) -> WavCondition:wav, length, path = wav_lengthassert length is not Nonereturn WavCondition(wav.to(self.device), length.to(self.device), path)def _get_wav_embedding(self, wav: Tensor) -> Tensor:"""Gets as input a wav and returns a dense vector of conditions."""raise NotImplementedError()def _downsampling_factor(self):"""Returns the downsampling factor of the embedding model."""raise NotImplementedError()def forward(self, inputs: WavCondition) -> ConditionType:"""Args:input (WavCondition): Tuple of (waveform, lengths).Returns:ConditionType: Dense vector representing the conditioning along with its' mask."""wav, lengths, path = inputswith torch.no_grad():embeds = self._get_wav_embedding(wav)embeds = embeds.to(self.output_proj.weight)embeds = self.output_proj(embeds)if lengths is not None:lengths = lengths / self._downsampling_factor()mask = length_to_mask(lengths, max_len=embeds.shape[1]).int() # type: ignoreelse:mask = torch.ones_like(embeds)embeds = (embeds * mask.unsqueeze(2).to(self.device))return embeds, maskclass ChromaStemConditioner(WaveformConditioner):"""Chroma conditioner that uses DEMUCS to first filter out drums and bass. The is followed bythe insight the drums and bass often dominate the chroma, leading to the chroma not containing theinformation about melody.Args:output_dim (int): Output dimension for the conditioner.sample_rate (int): Sample rate for the chroma extractor.n_chroma (int): Number of chroma for the chroma extractor.radix2_exp (int): Radix2 exponent for the chroma extractor.duration (float): Duration used during training. This is later used for correct paddingin case we are using chroma as prefix.match_len_on_eval (bool, optional): If True then all chromas are padded to the trainingduration. Defaults to False.eval_wavs (str, optional): Path to a json egg with waveform, this waveforms are used asconditions during eval (for cases where we don't want to leak test conditions like MusicCaps).Defaults to None.n_eval_wavs (int, optional): Limits the number of waveforms used for conditioning. Defaults to 0.device (tp.Union[torch.device, str], optional): Device for the conditioner.**kwargs: Additional parameters for the chroma extractor."""def __init__(self, output_dim: int, sample_rate: int, n_chroma: int, radix2_exp: int,duration: float, match_len_on_eval: bool = True, eval_wavs: tp.Optional[str] = None,n_eval_wavs: int = 0, device: tp.Union[torch.device, str] = "cpu", **kwargs):from demucs import pretrainedsuper().__init__(dim=n_chroma, output_dim=output_dim, device=device)self.autocast = TorchAutocast(enabled=device != "cpu", device_type=self.device, dtype=torch.float32)self.sample_rate = sample_rateself.match_len_on_eval = match_len_on_evalself.duration = durationself.__dict__["demucs"] = pretrained.get_model('htdemucs').to(device)self.stem2idx = {'drums': 0, 'bass': 1, 'other': 2, 'vocal': 3}self.stem_idx = torch.LongTensor([self.stem2idx['vocal'], self.stem2idx['other']]).to(device)self.chroma = ChromaExtractor(sample_rate=sample_rate, n_chroma=n_chroma, radix2_exp=radix2_exp,device=device, **kwargs)self.chroma_len = self._get_chroma_len()def _downsampling_factor(self):return self.chroma.winhopdef _get_chroma_len(self):"""Get length of chroma during training"""dummy_wav = torch.zeros((1, self.sample_rate * self.duration), device=self.device)dummy_chr = self.chroma(dummy_wav)return dummy_chr.shape[1]@torch.no_grad()def _get_filtered_wav(self, wav):from demucs.apply import apply_modelfrom demucs.audio import convert_audiowith self.autocast:wav = convert_audio(wav, self.sample_rate, self.demucs.samplerate, self.demucs.audio_channels)stems = apply_model(self.demucs, wav, device=self.device)stems = stems[:, self.stem_idx] # extract stemstems = stems.sum(1) # merge extracted stemsstems = stems.mean(1, keepdim=True) # monostems = convert_audio(stems, self.demucs.samplerate, self.sample_rate, 1)return stems@torch.no_grad()def _get_wav_embedding(self, wav):# avoid 0-size tensors when we are working with null condsif wav.shape[-1] == 1:return self.chroma(wav)stems = self._get_filtered_wav(wav)chroma = self.chroma(stems)if self.match_len_on_eval:b, t, c = chroma.shapeif t > self.chroma_len:chroma = chroma[:, :self.chroma_len]logger.debug(f'chroma was truncated! ({t} -> {chroma.shape[1]})')elif t < self.chroma_len:# chroma = F.pad(chroma, (0, 0, 0, self.chroma_len - t))n_repeat = int(math.ceil(self.chroma_len / t))chroma = chroma.repeat(1, n_repeat, 1)chroma = chroma[:, :self.chroma_len]logger.debug(f'chroma was zero-padded! ({t} -> {chroma.shape[1]})')return chromaclass ChromaExtractor(nn.Module):"""Chroma extraction class, handles chroma extraction and quantization.Args:sample_rate (int): Sample rate.n_chroma (int): Number of chroma to consider.radix2_exp (int): Radix2 exponent.nfft (tp.Optional[int], optional): Number of FFT.winlen (tp.Optional[int], optional): Window length.winhop (tp.Optional[int], optional): Window hop size.argmax (bool, optional): Whether to use argmax. Defaults to False.norm (float, optional): Norm for chroma normalization. Defaults to inf.device (tp.Union[torch.device, str], optional): Device to use. Defaults to cpu."""def __init__(self, sample_rate: int, n_chroma: int = 12, radix2_exp: int = 12,nfft: tp.Optional[int] = None, winlen: tp.Optional[int] = None, winhop: tp.Optional[int] = None,argmax: bool = False, norm: float = torch.inf, device: tp.Union[torch.device, str] = "cpu"):super().__init__()from librosa import filtersself.device = deviceself.autocast = TorchAutocast(enabled=device != "cpu", device_type=self.device, dtype=torch.float32)self.winlen = winlen or 2 ** radix2_expself.nfft = nfft or self.winlenself.winhop = winhop or (self.winlen // 4)self.sr = sample_rateself.n_chroma = n_chromaself.norm = normself.argmax = argmaxself.window = torch.hann_window(self.winlen).to(device)self.fbanks = torch.from_numpy(filters.chroma(sr=sample_rate, n_fft=self.nfft, tuning=0,n_chroma=self.n_chroma)).to(device)self.spec = torchaudio.transforms.Spectrogram(n_fft=self.nfft, win_length=self.winlen,hop_length=self.winhop, power=2, center=True,pad=0, normalized=True).to(device)def forward(self, wav):with self.autocast:T = wav.shape[-1]# in case we are getting a wav that was dropped out (nullified)# make sure wav length is no less that nfftif T < self.nfft:pad = self.nfft - Tr = 0 if pad % 2 == 0 else 1wav = F.pad(wav, (pad // 2, pad // 2 + r), 'constant', 0)assert wav.shape[-1] == self.nfft, f'expected len {self.nfft} but got {wav.shape[-1]}'spec = self.spec(wav).squeeze(1)raw_chroma = torch.einsum("cf,...ft->...ct", self.fbanks, spec)norm_chroma = torch.nn.functional.normalize(raw_chroma, p=self.norm, dim=-2, eps=1e-6)norm_chroma = rearrange(norm_chroma, "b d t -> b t d")if self.argmax:idx = norm_chroma.argmax(-1, keepdims=True)norm_chroma[:] = 0norm_chroma.scatter_(dim=-1, index=idx, value=1)return norm_chromadef dropout_condition(sample: ConditioningAttributes, condition_type: str, condition: str):"""Utility function for nullifying an attribute inside an ConditioningAttributes object.If the condition is of type "wav", then nullify it using "nullify_condition".If the condition is of any other type, set its' value to None.Works in-place."""if condition_type not in ["text", "wav"]:raise ValueError("dropout_condition got an unexpected condition type!"f" expected 'wav' or 'text' but got '{condition_type}'")if condition not in getattr(sample, condition_type):raise ValueError("dropout_condition received an unexpected condition!"f" expected wav={sample.wav.keys()} and text={sample.text.keys()}"f"but got '{condition}' of type '{condition_type}'!")if condition_type == "wav":wav, length, path = sample.wav[condition]sample.wav[condition] = nullify_wav(wav)else:sample.text[condition] = None1 unmodified lineclass DropoutModule(nn.Module):"""Base class for all dropout modules."""def __init__(self, seed: int = 1234):super().__init__()self.rng = torch.Generator()1 unmodified lineclass AttributeDropout(DropoutModule):"""Applies dropout with a given probability per attribute. This is different from the behavior ofClassifierFreeGuidanceDropout as this allows for attributes to be dropped out separately. For example,"artist" can be dropped while "genre" remains. This is in contrast to ClassifierFreeGuidanceDropoutwhere if "artist" is dropped "genre" must also be dropped.Args:p (tp.Dict[str, float]): A dict mapping between attributes and dropout probability. For example:16 unmodified linesdef forward(self, samples: tp.List[ConditioningAttributes]) -> tp.List[ConditioningAttributes]:"""Args:samples (tp.List[ConditioningAttributes]): List of conditions.Returns:tp.List[ConditioningAttributes]: List of conditions after certain attributes were set to None."""if not self.training and not self.active_on_eval:return samplessamples = deepcopy(samples)for condition_type, ps in self.p.items(): # for condition types [text, wav]for condition, p in ps.items(): # for attributes of each type (e.g., [artist, genre])if torch.rand(1, generator=self.rng).item() < p:for sample in samples:dropout_condition(sample, condition_type, condition)return samplesdef __repr__(self):1 unmodified lineclass ClassifierFreeGuidanceDropout(DropoutModule):"""Applies Classifier Free Guidance dropout, meaning all attributesare dropped with the same probability.Args:p (float): Probability to apply condition dropout during training.6 unmodified linesdef forward(self, samples: tp.List[ConditioningAttributes]) -> tp.List[ConditioningAttributes]:"""Args:samples (tp.List[ConditioningAttributes]): List of conditions.Returns:tp.List[ConditioningAttributes]: List of conditions after all attributes were set to None."""if not self.training:return samples5 unmodified lines# nullify conditions of all attributessamples = deepcopy(samples)for condition_type in ["wav", "text"]:for sample in samples:for condition in sample.attributes[condition_type]:dropout_condition(sample, condition_type, condition)return samplesdef __repr__(self):1 unmodified lineclass ConditioningProvider(nn.Module):"""Main class to provide conditions given all the supported conditioners.Args:conditioners (dict): Dictionary of conditioners.merge_text_conditions_p (float, optional): Probability to merge all text sourcesinto a single text condition. Defaults to 0.drop_desc_p (float, optional): Probability to drop the original descriptionwhen merging all text sources into a single text condition. Defaults to 0.device (tp.Union[torch.device, str], optional): Device for conditioners and output condition types."""def __init__(self,conditioners: tp.Dict[str, BaseConditioner],merge_text_conditions_p: float = 0,drop_desc_p: float = 0,device: tp.Union[torch.device, str] = "cpu",):super().__init__()self.device = deviceself.merge_text_conditions_p = merge_text_conditions_pself.drop_desc_p = drop_desc_pself.conditioners = nn.ModuleDict(conditioners)@propertydef text_conditions(self):return [k for k, v in self.conditioners.items() if isinstance(v, TextConditioner)]12 unmodified linesThis will return a dict matching conditioner names to their arbitrary tokenized representations.Args:inputs (list[ConditioningAttribres]): List of ConditioningAttributes objects containingtext and wav conditions."""assert all([type(x) == ConditioningAttributes for x in inputs]), \"got unexpected types input for conditioner! should be tp.List[ConditioningAttributes]" \f" but types were {set([type(x) for x in inputs])}"output = {}text = self._collate_text(inputs)wavs = self._collate_wavs(inputs)assert set(text.keys() | wavs.keys()).issubset(set(self.conditioners.keys())), \f"got an unexpected attribute! Expected {self.conditioners.keys()}, got {text.keys(), wavs.keys()}"for attribute, batch in chain(text.items(), wavs.items()):output[attribute] = self.conditioners[attribute].tokenize(batch)return outputdef forward(self, tokenized: tp.Dict[str, tp.Any]) -> tp.Dict[str, ConditionType]:"""Compute pairs of `(embedding, mask)` using the configured conditionersand the tokenized representations. The output is for example:{"genre": (torch.Tensor([B, 1, D_genre]), torch.Tensor([B, 1])),"description": (torch.Tensor([B, T_desc, D_desc]), torch.Tensor([B, T_desc])),...}Args:tokenized (dict): Dict of tokenized representations as returned by `tokenize()`.18 unmodified lines"genre": ["Rock", "Hip-hop"],"description": ["A rock song with a guitar solo", "A hip-hop verse"]}"""batch_per_attribute: tp.Dict[str, tp.List[tp.Optional[str]]] = defaultdict(list)def _merge_conds(cond, merge_text_conditions_p=0, drop_desc_p=0):def is_valid(k, v):k_valid = k in ['key', 'bpm', 'genre', 'moods', 'instrument']v_valid = v is not None and isinstance(v, (int, float, str, list))return k_valid and v_validdef process_value(v):if isinstance(v, (int, float, str)):return vif isinstance(v, list):return ", ".join(v)else:RuntimeError(f"unknown type for text value! ({type(v), v})")desc = cond.text['description']meta_data = ""if random.uniform(0, 1) < merge_text_conditions_p:meta_pairs = [f'{k}: {process_value(v)}' for k, v in cond.text.items() if is_valid(k, v)]random.shuffle(meta_pairs)meta_data = ". ".join(meta_pairs)desc = desc if not random.uniform(0, 1) < drop_desc_p else Noneif desc is None:desc = meta_data if len(meta_data) > 1 else Noneelse:desc = desc.rstrip('.') + ". " + meta_datacond.text['description'] = desc.strip() if desc else Noneif self.training and self.merge_text_conditions_p:for sample in samples:_merge_conds(sample, self.merge_text_conditions_p, self.drop_desc_p)texts = [x.text for x in samples]for text in texts:for condition in self.text_conditions:batch_per_attribute[condition].append(text[condition])return batch_per_attributedef _collate_wavs(self, samples: tp.List[ConditioningAttributes]):"""Generate a dict where the keys are attributes by which we fetch similar wavs,and the values are Tensors of wavs according to said attribtues.*Note*: by the time the samples reach this function, each sample should have some waveforminside the "wav" attribute. It should be either:2 unmodified lines3. A null waveform due to it being dropped in a dropout module (nullified by dropout)Args:samples (tp.List[ConditioningAttributes]): List of ConditioningAttributes samples.Returns:dict: A dicionary mapping an attribute name to wavs."""wavs = defaultdict(list)lens = defaultdict(list)paths = defaultdict(list)out = {}for sample in samples:for attribute in self.wav_conditions:wav, length, path = sample.wav[attribute]wavs[attribute].append(wav.flatten())lens[attribute].append(length)paths[attribute].append(path)# stack all wavs to a single tensorfor attribute in self.wav_conditions:stacked_wav, _ = collate(wavs[attribute], dim=0)out[attribute] = WavCondition(stacked_wav.unsqueeze(1),torch.cat(lens['self_wav']), paths[attribute]) # type: ignorereturn out20 unmodified linessuper().__init__()assert all([k in self.FUSING_METHODS for k in fuse2cond.keys()]), f"got invalid fuse method, allowed methods: {self.FUSING_MEHTODS}"self.cross_attention_pos_emb = cross_attention_pos_embself.cross_attention_pos_emb_scale = cross_attention_pos_emb_scaleself.fuse2cond: tp.Dict[str, tp.List[str]] = fuse2cond4 unmodified linesdef forward(self,input: Tensor,conditions: tp.Dict[str, ConditionType]) -> tp.Tuple[Tensor, tp.Optional[Tensor]]:"""Fuse the conditions to the provided model input.Args:input (Tensor): Transformer input.conditions (tp.Dict[str, ConditionType]): Dict of conditions.Returns:tp.Tuple[Tensor, Tensor]: The first tensor is the transformer inputafter the conditions have been fused. The second output tensor is the tensorused for cross-attention or None if no cross attention inputs exist."""12 unmodified linescross_attention_output = Nonefor cond_type, (cond, cond_mask) in conditions.items():op = self.cond2fuse[cond_type]if op == "sum":input += condelif op == "input_interpolate":cond = rearrange(cond, "b t d -> b d t")cond = F.interpolate(cond, size=input.shape[1])input += rearrange(cond, "b d t -> b t d")elif op == "prepend":if first_step:input = torch.cat([cond, input], dim=1)elif op == "cross":if cross_attention_output is not None:cross_attention_output = torch.cat([cross_attention_output, cond], dim=1)else:9 unmodified lines101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869706 unmodified lines777879808182838485868788899091929394959697989914 unmodified lines1141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861874 unmodified lines1921931941951961971988 unmodified lines2072082092102112122132142152162172182192202212 unmodified lines2242252262272282292304 unmodified lines23523623723823924024124224324424524624724824917 unmodified lines26726826927027127227310 unmodified lines28428528628728828929029129229329429529629729829938 unmodified lines33833934034134234334434534643 unmodified lines39039139239339439539639739839940040119 unmodified lines42142242342442542642713 unmodified lines44144244344444544644744844945045145245315 unmodified lines4694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261 unmodified line10281029103010311032103310341 unmodified line1036103710381039104010411042104310441045104616 unmodified lines10631064106510661067106810691070107110721073107410751076107710781079108010811 unmodified line108310841085108610871088108910906 unmodified lines1097109810991100110111021103110411055 unmodified lines11111112111311141115111611171118111911201 unmodified line112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114612 unmodified lines11591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119418 unmodified lines12131214121512161217121812191220122112221223122412251226122712281229123012311232123312342 unmodified lines1237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132520 unmodified lines13461347134813491350135113524 unmodified lines135713581359136013611362136313641365136613671368136913701371137212 unmodified lines13851386138713881389139013911392139313941395139613971398139914009 unmodified linesfrom itertools import chainimport loggingimport mathfrom pathlib import Pathimport randomimport reimport typing as tpimport warningsimport einopsfrom num2words import num2wordsimport spacyfrom transformers import RobertaTokenizer, T5EncoderModel, T5Tokenizer # type: ignoreimport torchfrom torch import nnimport torch.nn.functional as Ffrom torch.nn.utils.rnn import pad_sequencefrom .chroma import ChromaExtractorfrom .streaming import StreamingModulefrom .transformer import create_sin_embeddingfrom ..data.audio import audio_readfrom ..data.audio_dataset import SegmentInfofrom ..data.audio_utils import convert_audiofrom ..environment import AudioCraftEnvironmentfrom ..quantization import ResidualVectorQuantizerfrom ..utils.autocast import TorchAutocastfrom ..utils.cache import EmbeddingCachefrom ..utils.utils import collate, hash_trick, length_to_mask, load_clap_state_dict, warn_oncelogger = logging.getLogger(__name__)TextCondition = tp.Optional[str] # a text condition can be a string or None (if doesn't exist)ConditionType = tp.Tuple[torch.Tensor, torch.Tensor] # condition, maskclass WavCondition(tp.NamedTuple):wav: torch.Tensorlength: torch.Tensorsample_rate: tp.List[int]path: tp.List[tp.Optional[str]] = []seek_time: tp.List[tp.Optional[float]] = []class JointEmbedCondition(tp.NamedTuple):wav: torch.Tensortext: tp.List[tp.Optional[str]]length: torch.Tensorsample_rate: tp.List[int]path: tp.List[tp.Optional[str]] = []seek_time: tp.List[tp.Optional[float]] = []@dataclassclass ConditioningAttributes:text: tp.Dict[str, tp.Optional[str]] = field(default_factory=dict)wav: tp.Dict[str, WavCondition] = field(default_factory=dict)joint_embed: tp.Dict[str, JointEmbedCondition] = field(default_factory=dict)def __getitem__(self, item):return getattr(self, item)6 unmodified linesdef wav_attributes(self):return self.wav.keys()@propertydef joint_embed_attributes(self):return self.joint_embed.keys()@propertydef attributes(self):return {"text": self.text_attributes,"wav": self.wav_attributes,"joint_embed": self.joint_embed_attributes,}def to_flat_dict(self):return {**{f"text.{k}": v for k, v in self.text.items()},**{f"wav.{k}": v for k, v in self.wav.items()},**{f"joint_embed.{k}": v for k, v in self.joint_embed.items()}}@classmethod14 unmodified linesraise NotImplementedError()def nullify_condition(condition: ConditionType, dim: int = 1):"""Transform an input condition to a null condition.The way it is done by converting it to a single zero vector similarlyto how it is done inside WhiteSpaceTokenizer and NoopTokenizer.Args:condition (ConditionType): A tuple of condition and mask (tuple[torch.Tensor, torch.Tensor])dim (int): The dimension that will be truncated (should be the time dimension)WARNING!: dim should not be the batch dimension!Returns:ConditionType: A tuple of null condition and mask"""assert dim != 0, "dim cannot be the batch dimension!"assert isinstance(condition, tuple) and \isinstance(condition[0], torch.Tensor) and \isinstance(condition[1], torch.Tensor), "'nullify_condition' got an unexpected input type!"cond, mask = conditionB = cond.shape[0]last_dim = cond.dim() - 1out = cond.transpose(dim, last_dim)out = 0. * out[..., :1]out = out.transpose(dim, last_dim)mask = torch.zeros((B, 1), device=out.device).int()assert cond.dim() == out.dim()return out, maskdef nullify_wav(cond: WavCondition) -> WavCondition:"""Transform a WavCondition to a nullified WavCondition.It replaces the wav by a null tensor, forces its length to 0, and replaces metadata by dummy attributes.Args:cond (WavCondition): Wav condition with wav, tensor of shape [B, T].Returns:WavCondition: Nullified wav condition."""null_wav, _ = nullify_condition((cond.wav, torch.zeros_like(cond.wav)), dim=cond.wav.dim() - 1)return WavCondition(wav=null_wav,length=torch.tensor([0] * cond.wav.shape[0], device=cond.wav.device),sample_rate=cond.sample_rate,path=[None] * cond.wav.shape[0],seek_time=[None] * cond.wav.shape[0],)def nullify_joint_embed(embed: JointEmbedCondition) -> JointEmbedCondition:"""Nullify the joint embedding condition by replacing it by a null tensor, forcing its length to 0,and replacing metadata by dummy attributes.Args:cond (JointEmbedCondition): Joint embedding condition with wav and text, wav tensor of shape [B, C, T]."""null_wav, _ = nullify_condition((embed.wav, torch.zeros_like(embed.wav)), dim=embed.wav.dim() - 1)return JointEmbedCondition(wav=null_wav, text=[None] * len(embed.text),length=torch.LongTensor([0]).to(embed.wav.device),sample_rate=embed.sample_rate,path=[None] * embed.wav.shape[0],seek_time=[0] * embed.wav.shape[0],)class Tokenizer:"""Base tokenizer implementation(in case we want to introduce more advances tokenizers in the future)."""def __call__(self, texts: tp.List[tp.Optional[str]]) -> tp.Tuple[torch.Tensor, torch.Tensor]:raise NotImplementedError()4 unmodified lines[[78, 62, 31, 4, 78, 25, 19, 34],[59, 77, 0, 0, 0, 0, 0, 0]]"""PUNCTUATION = "?:!.,;"def __init__(self, n_bins: int, pad_idx: int = 0, language: str = "en_core_web_sm",lemma: bool = True, stopwords: bool = True) -> None:8 unmodified linesself.nlp = spacy.load(language)@tp.no_type_checkdef __call__(self, texts: tp.List[tp.Optional[str]],return_text: bool = False) -> tp.Tuple[torch.Tensor, torch.Tensor]:"""Take a list of strings and convert them to a tensor of indices.Args:texts (list[str]): List of strings.return_text (bool, optional): Whether to return text as additional tuple item. Defaults to False.Returns:tuple[torch.Tensor, torch.Tensor]:- Indices of words in the LUT.- And a mask indicating where the padding tokens are"""2 unmodified linesfor i, text in enumerate(texts):# if current sample doesn't have a certain attribute, replace with pad tokenif text is None:output.append(torch.Tensor([self.pad_idx]))lengths.append(0)continue4 unmodified lines# remove stopwordsif self.stopwords:text = [w for w in text if not w.is_stop] # type: ignore# remove punctuationtext = [w for w in text if w.text not in self.PUNCTUATION] # type: ignore# lemmatize if neededtext = [getattr(t, "lemma_" if self.lemma else "text") for t in text] # type: ignoretexts[i] = " ".join(text)lengths.append(len(text))# convert to tensortokens = torch.Tensor([hash_trick(w, self.n_bins) for w in text])output.append(tokens)mask = length_to_mask(torch.IntTensor(lengths)).int()17 unmodified linesself.n_bins = n_binsself.pad_idx = pad_idxdef __call__(self, texts: tp.List[tp.Optional[str]]) -> tp.Tuple[torch.Tensor, torch.Tensor]:output, lengths = [], []for text in texts:# if current sample doesn't have a certain attribute, replace with pad token10 unmodified linesclass BaseConditioner(nn.Module):"""Base model for all conditioner modules.We allow the output dim to be different than the hidden dim for two reasons:1) keep our LUTs small when the vocab is large;2) make all condition dims consistent.Args:dim (int): Hidden dim of the model.output_dim (int): Output dim of the conditioner."""def __init__(self, dim: int, output_dim: int):super().__init__()self.dim = dimself.output_dim = output_dim38 unmodified linessuper().__init__(dim, output_dim)self.embed = nn.Embedding(n_bins, dim)self.tokenizer: Tokenizerif tokenizer == 'whitespace':self.tokenizer = WhiteSpaceTokenizer(n_bins, pad_idx=pad_idx)elif tokenizer == 'noop':self.tokenizer = NoopTokenizer(n_bins, pad_idx=pad_idx)else:raise ValueError(f"unrecognized tokenizer `{tokenizer}`.")43 unmodified linesdef __init__(self, name: str, output_dim: int, finetune: bool, device: str,autocast_dtype: tp.Optional[str] = 'float32', word_dropout: float = 0.,normalize_text: bool = False):assert name in self.MODELS, f"Unrecognized t5 model name (should in {self.MODELS})"super().__init__(self.MODELS_DIMS[name], output_dim)self.device = deviceself.name = nameself.finetune = finetuneself.word_dropout = word_dropoutif autocast_dtype is None or self.device == 'cpu':self.autocast = TorchAutocast(enabled=False)if self.device != 'cpu':19 unmodified lineselse:# this makes sure that the t5 models is not part# of the saved checkpointself.__dict__['t5'] = t5.to(device)self.normalize_text = normalize_textif normalize_text:13 unmodified linesempty_idx = torch.LongTensor([i for i, xi in enumerate(entries) if xi == ""])inputs = self.t5_tokenizer(entries, return_tensors='pt', padding=True).to(self.device)mask = inputs['attention_mask']mask[empty_idx, :] = 0 # zero-out index where the input is non-existantreturn inputsdef forward(self, inputs: tp.Dict[str, torch.Tensor]) -> ConditionType:mask = inputs['attention_mask']with torch.set_grad_enabled(self.finetune), self.autocast:embeds = self.t5(**inputs).last_hidden_stateembeds = self.output_proj(embeds.to(self.output_proj.weight))15 unmodified linesdef __init__(self, dim: int, output_dim: int, device: tp.Union[torch.device, str]):super().__init__(dim, output_dim)self.device = device# if False no masking is done, used in ChromaStemConditioner when completing by periodicity a sample.self._use_masking = Truedef tokenize(self, x: WavCondition) -> WavCondition:wav, length, sample_rate, path, seek_time = xassert length is not Nonereturn WavCondition(wav.to(self.device), length.to(self.device), sample_rate, path, seek_time)def _get_wav_embedding(self, x: WavCondition) -> torch.Tensor:"""Gets as input a WavCondition and returns a dense embedding."""raise NotImplementedError()def _downsampling_factor(self):"""Returns the downsampling factor of the embedding model."""raise NotImplementedError()def forward(self, x: WavCondition) -> ConditionType:"""Extract condition embedding and mask from a waveform and its metadata.Args:x (WavCondition): Waveform condition containing raw waveform and metadata.Returns:ConditionType: a dense vector representing the conditioning along with its mask"""wav, lengths, *_ = xwith torch.no_grad():embeds = self._get_wav_embedding(x)embeds = embeds.to(self.output_proj.weight)embeds = self.output_proj(embeds)if lengths is not None and self._use_masking:lengths = lengths / self._downsampling_factor()mask = length_to_mask(lengths, max_len=embeds.shape[1]).int() # type: ignoreelse:mask = torch.ones_like(embeds[..., 0])embeds = (embeds * mask.unsqueeze(-1))return embeds, maskclass ChromaStemConditioner(WaveformConditioner):"""Chroma conditioner based on stems.The ChromaStemConditioner uses DEMUCS to first filter out drums and bass, asthe drums and bass often dominate the chroma leading to the chroma featuresnot containing information about the melody.Args:output_dim (int): Output dimension for the conditioner.sample_rate (int): Sample rate for the chroma extractor.n_chroma (int): Number of chroma bins for the chroma extractor.radix2_exp (int): Size of stft window for the chroma extractor (power of 2, e.g. 12 -> 2^12).duration (int): duration used during training. This is later used for correct paddingin case we are using chroma as prefix.match_len_on_eval (bool, optional): if True then all chromas are padded to the trainingduration. Defaults to False.eval_wavs (str, optional): path to a dataset manifest with waveform, this waveforms are used asconditions during eval (for cases where we don't want to leak test conditions like MusicCaps).Defaults to None.n_eval_wavs (int, optional): limits the number of waveforms used for conditioning. Defaults to 0.device (tp.Union[torch.device, str], optional): Device for the conditioner.**kwargs: Additional parameters for the chroma extractor."""def __init__(self, output_dim: int, sample_rate: int, n_chroma: int, radix2_exp: int,duration: float, match_len_on_eval: bool = True, eval_wavs: tp.Optional[str] = None,n_eval_wavs: int = 0, cache_path: tp.Optional[tp.Union[str, Path]] = None,device: tp.Union[torch.device, str] = 'cpu', **kwargs):from demucs import pretrainedsuper().__init__(dim=n_chroma, output_dim=output_dim, device=device)self.autocast = TorchAutocast(enabled=device != 'cpu', device_type=self.device, dtype=torch.float32)self.sample_rate = sample_rateself.match_len_on_eval = match_len_on_evalif match_len_on_eval:self._use_masking = Falseself.duration = durationself.__dict__['demucs'] = pretrained.get_model('htdemucs').to(device)stem_sources: list = self.demucs.sources # type: ignoreself.stem_indices = torch.LongTensor([stem_sources.index('vocals'), stem_sources.index('other')]).to(device)self.chroma = ChromaExtractor(sample_rate=sample_rate, n_chroma=n_chroma,radix2_exp=radix2_exp, **kwargs).to(device)self.chroma_len = self._get_chroma_len()self.eval_wavs: tp.Optional[torch.Tensor] = self._load_eval_wavs(eval_wavs, n_eval_wavs)self.cache = Noneif cache_path is not None:self.cache = EmbeddingCache(Path(cache_path) / 'wav', self.device,compute_embed_fn=self._get_full_chroma_for_cache,extract_embed_fn=self._extract_chroma_chunk)def _downsampling_factor(self) -> int:return self.chroma.winhopdef _load_eval_wavs(self, path: tp.Optional[str], num_samples: int) -> tp.Optional[torch.Tensor]:"""Load pre-defined waveforms from a json.These waveforms will be used for chroma extraction during evaluation.This is done to make the evaluation on MusicCaps fair (we shouldn't see the chromas of MusicCaps)."""if path is None:return Nonelogger.info(f"Loading evaluation wavs from {path}")from audiocraft.data.audio_dataset import AudioDatasetdataset: AudioDataset = AudioDataset.from_meta(path, segment_duration=self.duration, min_audio_duration=self.duration,sample_rate=self.sample_rate, channels=1)if len(dataset) > 0:eval_wavs = dataset.collater([dataset[i] for i in range(num_samples)]).to(self.device)logger.info(f"Using {len(eval_wavs)} evaluation wavs for chroma-stem conditioner")return eval_wavselse:raise ValueError("Could not find evaluation wavs, check lengths of wavs")def reset_eval_wavs(self, eval_wavs: tp.Optional[torch.Tensor]) -> None:self.eval_wavs = eval_wavsdef has_eval_wavs(self) -> bool:return self.eval_wavs is not Nonedef _sample_eval_wavs(self, num_samples: int) -> torch.Tensor:"""Sample wavs from a predefined list."""assert self.eval_wavs is not None, "Cannot sample eval wavs as no eval wavs provided."total_eval_wavs = len(self.eval_wavs)out = self.eval_wavsif num_samples > total_eval_wavs:out = self.eval_wavs.repeat(num_samples // total_eval_wavs + 1, 1, 1)return out[torch.randperm(len(out))][:num_samples]def _get_chroma_len(self) -> int:"""Get length of chroma during training."""dummy_wav = torch.zeros((1, int(self.sample_rate * self.duration)), device=self.device)dummy_chr = self.chroma(dummy_wav)return dummy_chr.shape[1]@torch.no_grad()def _get_stemmed_wav(self, wav: torch.Tensor, sample_rate: int) -> torch.Tensor:"""Get parts of the wav that holds the melody, extracting the main stems from the wav."""from demucs.apply import apply_modelfrom demucs.audio import convert_audiowith self.autocast:wav = convert_audio(wav, sample_rate, self.demucs.samplerate, self.demucs.audio_channels) # type: ignorestems = apply_model(self.demucs, wav, device=self.device)stems = stems[:, self.stem_indices] # extract relevant stems for melody conditioningmix_wav = stems.sum(1) # merge extracted stems to single waveformmix_wav = convert_audio(mix_wav, self.demucs.samplerate, self.sample_rate, 1) # type: ignorereturn mix_wav@torch.no_grad()def _extract_chroma(self, wav: torch.Tensor) -> torch.Tensor:"""Extract chroma features from the waveform."""with self.autocast:return self.chroma(wav)@torch.no_grad()def _compute_wav_embedding(self, wav: torch.Tensor, sample_rate: int) -> torch.Tensor:"""Compute wav embedding, applying stem and chroma extraction."""# avoid 0-size tensors when we are working with null condsif wav.shape[-1] == 1:return self._extract_chroma(wav)stems = self._get_stemmed_wav(wav, sample_rate)chroma = self._extract_chroma(stems)return chroma@torch.no_grad()def _get_full_chroma_for_cache(self, path: tp.Union[str, Path], x: WavCondition, idx: int) -> torch.Tensor:"""Extract chroma from the whole audio waveform at the given path."""wav, sr = audio_read(path)wav = wav[None].to(self.device)wav = convert_audio(wav, sr, self.sample_rate, to_channels=1)chroma = self._compute_wav_embedding(wav, self.sample_rate)[0]return chromadef _extract_chroma_chunk(self, full_chroma: torch.Tensor, x: WavCondition, idx: int) -> torch.Tensor:"""Extract a chunk of chroma from the full chroma derived from the full waveform."""wav_length = x.wav.shape[-1]seek_time = x.seek_time[idx]assert seek_time is not None, ("WavCondition seek_time is required ""when extracting chroma chunks from pre-computed chroma.")full_chroma = full_chroma.float()frame_rate = self.sample_rate / self._downsampling_factor()target_length = int(frame_rate * wav_length / self.sample_rate)index = int(frame_rate * seek_time)out = full_chroma[index: index + target_length]out = F.pad(out[None], (0, 0, 0, target_length - out.shape[0]))[0]return out.to(self.device)@torch.no_grad()def _get_wav_embedding(self, x: WavCondition) -> torch.Tensor:"""Get the wav embedding from the WavCondition.The conditioner will either extract the embedding on-the-fly computing it from the condition wav directlyor will rely on the embedding cache to load the pre-computed embedding if relevant."""sampled_wav: tp.Optional[torch.Tensor] = Noneif not self.training and self.eval_wavs is not None:warn_once(logger, "Using precomputed evaluation wavs!")sampled_wav = self._sample_eval_wavs(len(x.wav))no_undefined_paths = all(p is not None for p in x.path)no_nullified_cond = x.wav.shape[-1] > 1if sampled_wav is not None:chroma = self._compute_wav_embedding(sampled_wav, self.sample_rate)elif self.cache is not None and no_undefined_paths and no_nullified_cond:paths = [Path(p) for p in x.path if p is not None]chroma = self.cache.get_embed_from_cache(paths, x)else:assert all(sr == x.sample_rate[0] for sr in x.sample_rate), "All sample rates in batch should be equal."chroma = self._compute_wav_embedding(x.wav, x.sample_rate[0])if self.match_len_on_eval:B, T, C = chroma.shapeif T > self.chroma_len:chroma = chroma[:, :self.chroma_len]logger.debug(f"Chroma was truncated to match length! ({T} -> {chroma.shape[1]})")elif T < self.chroma_len:n_repeat = int(math.ceil(self.chroma_len / T))chroma = chroma.repeat(1, n_repeat, 1)chroma = chroma[:, :self.chroma_len]logger.debug(f"Chroma was repeated to match length! ({T} -> {chroma.shape[1]})")return chromadef tokenize(self, x: WavCondition) -> WavCondition:"""Apply WavConditioner tokenization and populate cache if needed."""x = super().tokenize(x)no_undefined_paths = all(p is not None for p in x.path)if self.cache is not None and no_undefined_paths:paths = [Path(p) for p in x.path if p is not None]self.cache.populate_embed_cache(paths, x)return xclass JointEmbeddingConditioner(BaseConditioner):"""Joint embedding conditioning supporting both audio or text conditioning.Args:dim (int): Dimension.output_dim (int): Output dimension.device (str): Device.attribute (str): Attribute used by the conditioner.autocast_dtype (str): Autocast for the conditioner.quantize (bool): Whether to quantize the CLAP embedding.n_q (int): Number of residual quantizers (used if quantize is true).bins (int): Quantizers' codebooks size (used if quantize is true).kwargs: Additional parameters for residual vector quantizer."""def __init__(self, dim: int, output_dim: int, device: str, attribute: str,autocast_dtype: tp.Optional[str] = 'float32', quantize: bool = True,n_q: int = 12, bins: int = 1024, **kwargs):super().__init__(dim=dim, output_dim=output_dim)self.device = deviceself.attribute = attributeif autocast_dtype is None or device == 'cpu':self.autocast = TorchAutocast(enabled=False)logger.warning("JointEmbeddingConditioner has no autocast, this might lead to NaN.")else:dtype = getattr(torch, autocast_dtype)assert isinstance(dtype, torch.dtype)logger.info(f"JointEmbeddingConditioner will be evaluated with autocast as {autocast_dtype}.")self.autocast = TorchAutocast(enabled=True, device_type=self.device, dtype=dtype)# residual vector quantizer to discretize the conditioned embeddingself.quantizer: tp.Optional[ResidualVectorQuantizer] = Noneif quantize:self.quantizer = ResidualVectorQuantizer(dim, n_q=n_q, bins=bins, **kwargs)def _get_embed(self, x: JointEmbedCondition) -> tp.Tuple[torch.Tensor, torch.Tensor]:"""Get joint embedding in latent space from the inputs.Returns:tuple[torch.Tensor, torch.Tensor]: Tensor for the latent embeddingand corresponding empty indexes."""raise NotImplementedError()def forward(self, x: JointEmbedCondition) -> ConditionType:with self.autocast:embed, empty_idx = self._get_embed(x)if self.quantizer is not None:embed = embed.view(-1, self.dim, 1)q_res = self.quantizer(embed, frame_rate=1)out_embed = q_res.x.view(-1, self.dim)else:out_embed = embedout_embed = self.output_proj(out_embed).view(-1, 1, self.output_dim)mask = torch.ones(*out_embed.shape[:2], device=out_embed.device)mask[empty_idx, :] = 0 # zero-out index where the input is non-existantout_embed = (out_embed * mask.unsqueeze(-1))return out_embed, maskdef tokenize(self, x: JointEmbedCondition) -> JointEmbedCondition:return xclass CLAPEmbeddingConditioner(JointEmbeddingConditioner):"""Joint Embedding conditioner based on pre-trained CLAP model.This CLAP-based conditioner supports a caching mechanismover the computed embeddings for faster training.Args:dim (int): Dimension.output_dim (int): Output dimension.device (str): Device.attribute (str): Attribute used by the conditioner.quantize (bool): Whether to quantize the CLAP embedding.n_q (int): Number of residual quantizers (used if quantize is true).bins (int): Quantizers' codebooks size (used if quantize is true).checkpoint (str): Path to CLAP checkpoint.model_arch (str): CLAP model architecture.enable_fusion (bool): Enable fusion for CLAP model.sample_rate (int): Sample rate used by CLAP model.max_audio_length (float): Maximum audio length for CLAP model.audio_stride (float): Stride to use for getting a CLAP embedding on the full sequence.normalize (bool): Whether to normalize the CLAP embedding.text_p (float): Probability of using text representation instead of audio at train time.batch_size (Optional[int]): Batch size for CLAP embedding computation.autocast_dtype (str): Autocast for the conditioner.cache_path (Optional[str]): Path for pre-computed embeddings caching.kwargs: Additional parameters for residual vector quantizer."""def __init__(self, dim: int, output_dim: int, device: str, attribute: str,quantize: bool, n_q: int, bins: int, checkpoint: tp.Union[str, Path], model_arch: str,enable_fusion: bool, sample_rate: int, max_audio_length: int, audio_stride: int,normalize: bool, text_p: bool, batch_size: tp.Optional[int] = None,autocast_dtype: tp.Optional[str] = 'float32', cache_path: tp.Optional[str] = None, **kwargs):try:import laion_clap # type: ignoreexcept ImportError:raise ImportError("Please install CLAP to use the CLAPEmbeddingConditioner: 'pip install laion_clap'")warnings.warn("Sample rate for CLAP conditioner was fixed in version v1.1.0, (from 44.1 to 48 kHz). ""Please retrain all models.")checkpoint = AudioCraftEnvironment.resolve_reference_path(checkpoint)clap_tokenize = RobertaTokenizer.from_pretrained('roberta-base')clap_model = laion_clap.CLAP_Module(enable_fusion=enable_fusion, amodel=model_arch)load_clap_state_dict(clap_model, checkpoint)clap_model.eval()clap_model.to(device)super().__init__(dim=dim, output_dim=output_dim, device=device, attribute=attribute,autocast_dtype=autocast_dtype, quantize=quantize, n_q=n_q, bins=bins,**kwargs)self.checkpoint = checkpointself.enable_fusion = enable_fusionself.model_arch = model_archself.clap: laion_clap.CLAP_Moduleself.clap_tokenize: RobertaTokenizerself.clap_sample_rate = sample_rateself.clap_max_frames = int(self.clap_sample_rate * max_audio_length)self.clap_stride = int(self.clap_sample_rate * audio_stride)self.batch_size = batch_size or 1self.normalize = normalizeself.text_p = text_pself.__dict__['clap_tokenize'] = clap_tokenizeself.__dict__['clap'] = clap_modelself.wav_cache, self.text_cache = None, Noneif cache_path is not None:self.wav_cache = EmbeddingCache(Path(cache_path) / 'wav', self.device,compute_embed_fn=self._get_wav_embedding_for_cache,extract_embed_fn=self._extract_wav_embedding_chunk)self.text_cache = EmbeddingCache(Path(cache_path) / 'text', self.device,compute_embed_fn=self._get_text_embedding_for_cache)def _tokenizer(self, texts: tp.Union[str, tp.List[str]]) -> dict:# we use the default params from CLAP module here as wellreturn self.clap_tokenize(texts, padding="max_length", truncation=True, max_length=77, return_tensors="pt")def _compute_text_embedding(self, text: tp.List[str]) -> torch.Tensor:"""Compute text embedding from CLAP model on a given a batch of text.Args:text (list[str]): List of text for the batch, with B items.Returns:torch.Tensor: CLAP embedding derived from text, of shape [B, 1, D], with D the CLAP embedding dimension."""with torch.no_grad():embed = self.clap.get_text_embedding(text, tokenizer=self._tokenizer, use_tensor=True)return embed.view(embed.size(0), 1, embed.size(-1))def _get_text_embedding_for_cache(self, path: tp.Union[Path, str],x: JointEmbedCondition, idx: int) -> torch.Tensor:"""Get text embedding function for the cache."""text = x.text[idx]text = text if text is not None else ""return self._compute_text_embedding([text])[0]def _preprocess_wav(self, wav: torch.Tensor, length: torch.Tensor, sample_rates: tp.List[int]) -> torch.Tensor:"""Preprocess wav to expected format by CLAP model.Args:wav (torch.Tensor): Audio wav, of shape [B, C, T].length (torch.Tensor): Actual length of the audio for each item in the batch, of shape [B].sample_rates (list[int]): Sample rates for each sample in the batchReturns:torch.Tensor: Audio wav of shape [B, T]."""assert wav.dim() == 3, "Expecting wav to be [B, C, T]"if sample_rates is not None:_wav = []for i, audio in enumerate(wav):sr = sample_rates[i]audio = convert_audio(audio, from_rate=sr, to_rate=self.clap_sample_rate, to_channels=1)_wav.append(audio)wav = torch.stack(_wav, dim=0)wav = wav.mean(dim=1)return wavdef _compute_wav_embedding(self, wav: torch.Tensor, length: torch.Tensor,sample_rates: tp.List[int], reduce_mean: bool = False) -> torch.Tensor:"""Compute audio wave embedding from CLAP model.Since CLAP operates on a fixed sequence length audio inputs and we need to process longer audio sequences,we calculate the wav embeddings on `clap_max_frames` windows with `clap_stride`-second stride andaverage the resulting embeddings.Args:wav (torch.Tensor): Audio wav, of shape [B, C, T].length (torch.Tensor): Actual length of the audio for each item in the batch, of shape [B].sample_rates (list[int]): Sample rates for each sample in the batch.reduce_mean (bool): Whether to get the average tensor.Returns:torch.Tensor: Audio embedding of shape [B, F, D], F being the number of chunks, D the dimension."""with torch.no_grad():wav = self._preprocess_wav(wav, length, sample_rates)B, T = wav.shapeif T >= self.clap_max_frames:wav = wav.unfold(-1, self.clap_max_frames, self.clap_stride) # [B, F, T]else:wav = wav.view(-1, 1, T) # [B, F, T] with F=1wav = einops.rearrange(wav, 'b f t -> (b f) t')embed_list = []for i in range(0, wav.size(0), self.batch_size):_wav = wav[i:i+self.batch_size, ...]_embed = self.clap.get_audio_embedding_from_data(_wav, use_tensor=True)embed_list.append(_embed)embed = torch.cat(embed_list, dim=0)embed = einops.rearrange(embed, '(b f) d -> b f d', b=B)if reduce_mean:embed = embed.mean(dim=1, keepdim=True)return embed # [B, F, D] with F=1 if reduce_mean is Truedef _get_wav_embedding_for_cache(self, path: tp.Union[str, Path],x: JointEmbedCondition, idx: int) -> torch.Tensor:"""Compute audio wave embedding for the cache.The embedding is computed on a given audio read from file.Args:path (str or Path): Path to the full audio file.Returns:torch.Tensor: Single-item tensor of shape [F, D], F being the number of chunks, D the dimension."""wav, sr = audio_read(path) # [C, T]wav = wav.unsqueeze(0).to(self.device) # [1, C, T]wav_len = torch.LongTensor([wav.shape[-1]]).to(self.device)embed = self._compute_wav_embedding(wav, wav_len, [sr], reduce_mean=False) # [B, F, D]return embed.squeeze(0) # [F, D]def _extract_wav_embedding_chunk(self, full_embed: torch.Tensor, x: JointEmbedCondition, idx: int) -> torch.Tensor:"""Extract the chunk of embedding matching the seek_time and length from the full CLAP audio embedding.Args:full_embed (torch.Tensor): CLAP embedding computed on the full wave, of shape [F, D].x (JointEmbedCondition): Joint embedding condition for the full batch.idx (int): Index considered for the given embedding to extract.Returns:torch.Tensor: Wav embedding averaged on sliding window, of shape [1, D]."""sample_rate = x.sample_rate[idx]seek_time = x.seek_time[idx]seek_time = 0. if seek_time is None else seek_timeclap_stride = int(self.clap_stride / self.clap_sample_rate) * sample_rateend_seek_time = seek_time + self.clap_max_frames / self.clap_sample_ratestart_offset = int(seek_time * sample_rate // clap_stride)end_offset = int(end_seek_time * sample_rate // clap_stride)wav_embed = full_embed[start_offset:end_offset, ...]wav_embed = wav_embed.mean(dim=0, keepdim=True)return wav_embed.to(self.device) # [F, D]def _get_text_embedding(self, x: JointEmbedCondition) -> torch.Tensor:"""Get CLAP embedding from a batch of text descriptions."""no_nullified_cond = x.wav.shape[-1] > 1 # we don't want to read from cache when condition dropoutif self.text_cache is not None and no_nullified_cond:assert all(p is not None for p in x.path), "Cache requires all JointEmbedCondition paths to be provided"paths = [Path(p) for p in x.path if p is not None]embed = self.text_cache.get_embed_from_cache(paths, x)else:text = [xi if xi is not None else "" for xi in x.text]embed = self._compute_text_embedding(text)if self.normalize:embed = torch.nn.functional.normalize(embed, p=2.0, dim=-1)return embeddef _get_wav_embedding(self, x: JointEmbedCondition) -> torch.Tensor:"""Get CLAP embedding from a batch of audio tensors (and corresponding sample rates)."""no_undefined_paths = all(p is not None for p in x.path)no_nullified_cond = x.wav.shape[-1] > 1 # we don't want to read from cache when condition dropoutif self.wav_cache is not None and no_undefined_paths and no_nullified_cond:paths = [Path(p) for p in x.path if p is not None]embed = self.wav_cache.get_embed_from_cache(paths, x)else:embed = self._compute_wav_embedding(x.wav, x.length, x.sample_rate, reduce_mean=True)if self.normalize:embed = torch.nn.functional.normalize(embed, p=2.0, dim=-1)return embeddef tokenize(self, x: JointEmbedCondition) -> JointEmbedCondition:# Trying to limit as much as possible sync points when the cache is warm.no_undefined_paths = all(p is not None for p in x.path)if self.wav_cache is not None and no_undefined_paths:assert all([p is not None for p in x.path]), "Cache requires all JointEmbedCondition paths to be provided"paths = [Path(p) for p in x.path if p is not None]self.wav_cache.populate_embed_cache(paths, x)if self.text_cache is not None and no_undefined_paths:assert all([p is not None for p in x.path]), "Cache requires all JointEmbedCondition paths to be provided"paths = [Path(p) for p in x.path if p is not None]self.text_cache.populate_embed_cache(paths, x)return xdef _get_embed(self, x: JointEmbedCondition) -> tp.Tuple[torch.Tensor, torch.Tensor]:"""Extract shared latent representation from either the wav or the text using CLAP."""# decide whether to use text embedding at train time or notuse_text_embed = random.random() < self.text_pif self.training and not use_text_embed:embed = self._get_wav_embedding(x)empty_idx = torch.LongTensor([]) # we assume we always have the audio wavelse:embed = self._get_text_embedding(x)empty_idx = torch.LongTensor([i for i, xi in enumerate(x.text) if xi is None or xi == ""])return embed, empty_idxdef dropout_condition(sample: ConditioningAttributes, condition_type: str, condition: str) -> ConditioningAttributes:"""Utility function for nullifying an attribute inside an ConditioningAttributes object.If the condition is of type "wav", then nullify it using `nullify_condition` function.If the condition is of any other type, set its value to None.Works in-place."""if condition_type not in ['text', 'wav', 'joint_embed']:raise ValueError("dropout_condition got an unexpected condition type!"f" expected 'text', 'wav' or 'joint_embed' but got '{condition_type}'")if condition not in getattr(sample, condition_type):raise ValueError("dropout_condition received an unexpected condition!"f" expected wav={sample.wav.keys()} and text={sample.text.keys()}"f" but got '{condition}' of type '{condition_type}'!")if condition_type == 'wav':wav_cond = sample.wav[condition]sample.wav[condition] = nullify_wav(wav_cond)elif condition_type == 'joint_embed':embed = sample.joint_embed[condition]sample.joint_embed[condition] = nullify_joint_embed(embed)else:sample.text[condition] = None1 unmodified lineclass DropoutModule(nn.Module):"""Base module for all dropout modules."""def __init__(self, seed: int = 1234):super().__init__()self.rng = torch.Generator()1 unmodified lineclass AttributeDropout(DropoutModule):"""Dropout with a given probability per attribute.This is different from the behavior of ClassifierFreeGuidanceDropout as this allows for attributesto be dropped out separately. For example, "artist" can be dropped while "genre" remains.This is in contrast to ClassifierFreeGuidanceDropout where if "artist" is dropped "genre"must also be dropped.Args:p (tp.Dict[str, float]): A dict mapping between attributes and dropout probability. For example:16 unmodified linesdef forward(self, samples: tp.List[ConditioningAttributes]) -> tp.List[ConditioningAttributes]:"""Args:samples (list[ConditioningAttributes]): List of conditions.Returns:list[ConditioningAttributes]: List of conditions after certain attributes were set to None."""if not self.training and not self.active_on_eval:return samplessamples = deepcopy(samples)for condition_type, ps in self.p.items(): # for condition types [text, wav]for condition, p in ps.items(): # for attributes of each type (e.g., [artist, genre])if torch.rand(1, generator=self.rng).item() < p:for sample in samples:dropout_condition(sample, condition_type, condition)return samplesdef __repr__(self):1 unmodified lineclass ClassifierFreeGuidanceDropout(DropoutModule):"""Classifier Free Guidance dropout.All attributes are dropped with the same probability.Args:p (float): Probability to apply condition dropout during training.6 unmodified linesdef forward(self, samples: tp.List[ConditioningAttributes]) -> tp.List[ConditioningAttributes]:"""Args:samples (list[ConditioningAttributes]): List of conditions.Returns:list[ConditioningAttributes]: List of conditions after all attributes were set to None."""if not self.training:return samples5 unmodified lines# nullify conditions of all attributessamples = deepcopy(samples)for condition_type in ["wav", "text"]:for sample in samples:for condition in sample.attributes[condition_type]:dropout_condition(sample, condition_type, condition)return samplesdef __repr__(self):1 unmodified lineclass ConditioningProvider(nn.Module):"""Prepare and provide conditions given all the supported conditioners.Args:conditioners (dict): Dictionary of conditioners.device (torch.device or str, optional): Device for conditioners and output condition types."""def __init__(self, conditioners: tp.Dict[str, BaseConditioner], device: tp.Union[torch.device, str] = "cpu"):super().__init__()self.device = deviceself.conditioners = nn.ModuleDict(conditioners)@propertydef joint_embed_conditions(self):return [m.attribute for m in self.conditioners.values() if isinstance(m, JointEmbeddingConditioner)]@propertydef has_joint_embed_conditions(self):return len(self.joint_embed_conditions) > 0@propertydef text_conditions(self):return [k for k, v in self.conditioners.items() if isinstance(v, TextConditioner)]12 unmodified linesThis will return a dict matching conditioner names to their arbitrary tokenized representations.Args:inputs (list[ConditioningAttributes]): List of ConditioningAttributes objects containingtext and wav conditions."""assert all([isinstance(x, ConditioningAttributes) for x in inputs]), ("Got unexpected types input for conditioner! should be tp.List[ConditioningAttributes]",f" but types were {set([type(x) for x in inputs])}")output = {}text = self._collate_text(inputs)wavs = self._collate_wavs(inputs)joint_embeds = self._collate_joint_embeds(inputs)assert set(text.keys() | wavs.keys() | joint_embeds.keys()).issubset(set(self.conditioners.keys())), (f"Got an unexpected attribute! Expected {self.conditioners.keys()}, ",f"got {text.keys(), wavs.keys(), joint_embeds.keys()}")for attribute, batch in chain(text.items(), wavs.items(), joint_embeds.items()):output[attribute] = self.conditioners[attribute].tokenize(batch)return outputdef forward(self, tokenized: tp.Dict[str, tp.Any]) -> tp.Dict[str, ConditionType]:"""Compute pairs of `(embedding, mask)` using the configured conditioners and the tokenized representations.The output is for example:{"genre": (torch.Tensor([B, 1, D_genre]), torch.Tensor([B, 1])),"description": (torch.Tensor([B, T_desc, D_desc]), torch.Tensor([B, T_desc])),...}Args:tokenized (dict): Dict of tokenized representations as returned by `tokenize()`.18 unmodified lines"genre": ["Rock", "Hip-hop"],"description": ["A rock song with a guitar solo", "A hip-hop verse"]}Args:samples (list of ConditioningAttributes): List of ConditioningAttributes samples.Returns:dict[str, list[str, optional]]: A dictionary mapping an attribute name to text batch."""out: tp.Dict[str, tp.List[tp.Optional[str]]] = defaultdict(list)texts = [x.text for x in samples]for text in texts:for condition in self.text_conditions:out[condition].append(text[condition])return outdef _collate_wavs(self, samples: tp.List[ConditioningAttributes]) -> tp.Dict[str, WavCondition]:"""Generate a dict where the keys are attributes by which we fetch similar wavs,and the values are Tensors of wavs according to said attributes.*Note*: by the time the samples reach this function, each sample should have some waveforminside the "wav" attribute. It should be either:2 unmodified lines3. A null waveform due to it being dropped in a dropout module (nullified by dropout)Args:samples (list of ConditioningAttributes): List of ConditioningAttributes samples.Returns:dict[str, WavCondition]: A dictionary mapping an attribute name to wavs."""wavs = defaultdict(list)lengths = defaultdict(list)sample_rates = defaultdict(list)paths = defaultdict(list)seek_times = defaultdict(list)out: tp.Dict[str, WavCondition] = {}for sample in samples:for attribute in self.wav_conditions:wav, length, sample_rate, path, seek_time = sample.wav[attribute]assert wav.dim() == 3, f"Got wav with dim={wav.dim()}, but expected 3 [1, C, T]"assert wav.size(0) == 1, f"Got wav [B, C, T] with shape={wav.shape}, but expected B == 1"# mono-channel conditioningwav = wav.mean(1, keepdim=True) # [1, 1, T]wavs[attribute].append(wav.flatten()) # [T]lengths[attribute].append(length)sample_rates[attribute].extend(sample_rate)paths[attribute].extend(path)seek_times[attribute].extend(seek_time)# stack all wavs to a single tensorfor attribute in self.wav_conditions:stacked_wav, _ = collate(wavs[attribute], dim=0)out[attribute] = WavCondition(stacked_wav.unsqueeze(1), torch.cat(lengths[attribute]), sample_rates[attribute],paths[attribute], seek_times[attribute])return outdef _collate_joint_embeds(self, samples: tp.List[ConditioningAttributes]) -> tp.Dict[str, JointEmbedCondition]:"""Generate a dict where the keys are attributes by which we compute joint embeddings,and the values are Tensors of pre-computed embeddings and the corresponding text attributes.Args:samples (list[ConditioningAttributes]): List of ConditioningAttributes samples.Returns:A dictionary mapping an attribute name to joint embeddings."""texts = defaultdict(list)wavs = defaultdict(list)lengths = defaultdict(list)sample_rates = defaultdict(list)paths = defaultdict(list)seek_times = defaultdict(list)channels: int = 0out = {}for sample in samples:for attribute in self.joint_embed_conditions:wav, text, length, sample_rate, path, seek_time = sample.joint_embed[attribute]assert wav.dim() == 3if channels == 0:channels = wav.size(1)else:assert channels == wav.size(1), "not all audio has same number of channels in batch"assert wav.size(0) == 1, "Expecting single-wav batch in the collate method"wav = einops.rearrange(wav, "b c t -> (b c t)") # [1, C, T] => [C * T]wavs[attribute].append(wav)texts[attribute].extend(text)lengths[attribute].append(length)sample_rates[attribute].extend(sample_rate)paths[attribute].extend(path)seek_times[attribute].extend(seek_time)for attribute in self.joint_embed_conditions:stacked_texts = texts[attribute]stacked_paths = paths[attribute]stacked_seek_times = seek_times[attribute]stacked_wavs = pad_sequence(wavs[attribute]).to(self.device)stacked_wavs = einops.rearrange(stacked_wavs, "(c t) b -> b c t", c=channels)stacked_sample_rates = sample_rates[attribute]stacked_lengths = torch.cat(lengths[attribute]).to(self.device)assert stacked_lengths.size(0) == stacked_wavs.size(0)assert len(stacked_sample_rates) == stacked_wavs.size(0)assert len(stacked_texts) == stacked_wavs.size(0)out[attribute] = JointEmbedCondition(text=stacked_texts, wav=stacked_wavs,length=stacked_lengths, sample_rate=stacked_sample_rates,path=stacked_paths, seek_time=stacked_seek_times)return out20 unmodified linessuper().__init__()assert all([k in self.FUSING_METHODS for k in fuse2cond.keys()]), f"Got invalid fuse method, allowed methods: {self.FUSING_METHODS}"self.cross_attention_pos_emb = cross_attention_pos_embself.cross_attention_pos_emb_scale = cross_attention_pos_emb_scaleself.fuse2cond: tp.Dict[str, tp.List[str]] = fuse2cond4 unmodified linesdef forward(self,input: torch.Tensor,conditions: tp.Dict[str, ConditionType]) -> tp.Tuple[torch.Tensor, tp.Optional[torch.Tensor]]:"""Fuse the conditions to the provided model input.Args:input (torch.Tensor): Transformer input.conditions (dict[str, ConditionType]): Dict of conditions.Returns:tuple[torch.Tensor, torch.Tensor]: The first tensor is the transformer inputafter the conditions have been fused. The second output tensor is the tensorused for cross-attention or None if no cross attention inputs exist."""12 unmodified linescross_attention_output = Nonefor cond_type, (cond, cond_mask) in conditions.items():op = self.cond2fuse[cond_type]if op == 'sum':input += condelif op == 'input_interpolate':cond = einops.rearrange(cond, "b t d -> b d t")cond = F.interpolate(cond, size=input.shape[1])input += einops.rearrange(cond, "b d t -> b t d")elif op == 'prepend':if first_step:input = torch.cat([cond, input], dim=1)elif op == 'cross':if cross_attention_output is not None:cross_attention_output = torch.cat([cross_attention_output, cond], dim=1)else:
10 unmodified lines1112131415161710 unmodified linesimport torchfrom torch import nnfrom torch.nn import functional as Ffrom torch.nn.utils import spectral_norm, weight_normCONV_NORMALIZATIONS = frozenset(['none', 'weight_norm', 'spectral_norm',10 unmodified lines1112131415161710 unmodified linesimport torchfrom torch import nnfrom torch.nn import functional as Ffrom torch.nn.utils.parametrizations import spectral_norm, weight_normCONV_NORMALIZATIONS = frozenset(['none', 'weight_norm', 'spectral_norm',
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272# Copyright (c) Meta Platforms, Inc. and affiliates.# All rights reserved.## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.
"""Functions for Noise Schedule, defines diffusion process, reverse process and data processor."""
from collections import namedtupleimport randomimport typing as tpimport juliusimport torch
TrainingItem = namedtuple("TrainingItem", "noisy noise step")
def betas_from_alpha_bar(alpha_bar): alphas = torch.cat([torch.Tensor([alpha_bar[0]]), alpha_bar[1:]/alpha_bar[:-1]]) return 1 - alphas
class SampleProcessor(torch.nn.Module): def project_sample(self, x: torch.Tensor): """Project the original sample to the 'space' where the diffusion will happen.""" return x
def return_sample(self, z: torch.Tensor): """Project back from diffusion space to the actual sample space.""" return z
class MultiBandProcessor(SampleProcessor): """ MultiBand sample processor. The input audio is splitted across frequency bands evenly distributed in mel-scale.
Each band will be rescaled to match the power distribution of Gaussian noise in that band, using online metrics computed on the first few samples.
Args: n_bands (int): Number of mel-bands to split the signal over. sample_rate (int): Sample rate of the audio. num_samples (int): Number of samples to use to fit the rescaling for each band. The processor won't be stable until it has seen that many samples. power_std (float or list/tensor): The rescaling factor computed to match the power of Gaussian noise in each band is taken to that power, i.e. `1.` means full correction of the energy in each band, and values less than `1` means only partial correction. Can be used to balance the relative importance of low vs. high freq in typical audio signals. """ def __init__(self, n_bands: int = 8, sample_rate: float = 24_000, num_samples: int = 10_000, power_std: tp.Union[float, tp.List[float], torch.Tensor] = 1.): super().__init__() self.n_bands = n_bands self.split_bands = julius.SplitBands(sample_rate, n_bands=n_bands) self.num_samples = num_samples self.power_std = power_std if isinstance(power_std, list): assert len(power_std) == n_bands power_std = torch.tensor(power_std) self.register_buffer('counts', torch.zeros(1)) self.register_buffer('sum_x', torch.zeros(n_bands)) self.register_buffer('sum_x2', torch.zeros(n_bands)) self.register_buffer('sum_target_x2', torch.zeros(n_bands)) self.counts: torch.Tensor self.sum_x: torch.Tensor self.sum_x2: torch.Tensor self.sum_target_x2: torch.Tensor
@property def mean(self): mean = self.sum_x / self.counts return mean
@property def std(self): std = (self.sum_x2 / self.counts - self.mean**2).clamp(min=0).sqrt() return std
@property def target_std(self): target_std = self.sum_target_x2 / self.counts return target_std
def project_sample(self, x: torch.Tensor): assert x.dim() == 3 bands = self.split_bands(x) if self.counts.item() < self.num_samples: ref_bands = self.split_bands(torch.randn_like(x)) self.counts += len(x) self.sum_x += bands.mean(dim=(2, 3)).sum(dim=1) self.sum_x2 += bands.pow(2).mean(dim=(2, 3)).sum(dim=1) self.sum_target_x2 += ref_bands.pow(2).mean(dim=(2, 3)).sum(dim=1) rescale = (self.target_std / self.std.clamp(min=1e-12)) ** self.power_std # same output size bands = (bands - self.mean.view(-1, 1, 1, 1)) * rescale.view(-1, 1, 1, 1) return bands.sum(dim=0)
def return_sample(self, x: torch.Tensor): assert x.dim() == 3 bands = self.split_bands(x) rescale = (self.std / self.target_std) ** self.power_std bands = bands * rescale.view(-1, 1, 1, 1) + self.mean.view(-1, 1, 1, 1) return bands.sum(dim=0)
class NoiseSchedule: """Noise schedule for diffusion.
Args: beta_t0 (float): Variance of the first diffusion step. beta_t1 (float): Variance of the last diffusion step. beta_exp (float): Power schedule exponent num_steps (int): Number of diffusion step. variance (str): choice of the sigma value for the denoising eq. Choices: "beta" or "beta_tilde" clip (float): clipping value for the denoising steps rescale (float): rescaling value to avoid vanishing signals unused by default (i.e 1) repartition (str): shape of the schedule only power schedule is supported sample_processor (SampleProcessor): Module that normalize data to match better the gaussian distribution noise_scale (float): Scaling factor for the noise """ def __init__(self, beta_t0: float = 1e-4, beta_t1: float = 0.02, num_steps: int = 1000, variance: str = 'beta', clip: float = 5., rescale: float = 1., device='cuda', beta_exp: float = 1, repartition: str = "power", alpha_sigmoid: dict = {}, n_bands: tp.Optional[int] = None, sample_processor: SampleProcessor = SampleProcessor(), noise_scale: float = 1.0, **kwargs):
self.beta_t0 = beta_t0 self.beta_t1 = beta_t1 self.variance = variance self.num_steps = num_steps self.clip = clip self.sample_processor = sample_processor self.rescale = rescale self.n_bands = n_bands self.noise_scale = noise_scale assert n_bands is None if repartition == "power": self.betas = torch.linspace(beta_t0 ** (1 / beta_exp), beta_t1 ** (1 / beta_exp), num_steps, device=device, dtype=torch.float) ** beta_exp else: raise RuntimeError('Not implemented') self.rng = random.Random(1234)
def get_beta(self, step: tp.Union[int, torch.Tensor]): if self.n_bands is None: return self.betas[step] else: return self.betas[:, step] # [n_bands, len(step)]
def get_initial_noise(self, x: torch.Tensor): if self.n_bands is None: return torch.randn_like(x) return torch.randn((x.size(0), self.n_bands, x.size(2)))
def get_alpha_bar(self, step: tp.Optional[tp.Union[int, torch.Tensor]] = None) -> torch.Tensor: """Return 'alpha_bar', either for a given step, or as a tensor with its value for each step.""" if step is None: return (1 - self.betas).cumprod(dim=-1) # works for simgle and multi bands if type(step) is int: return (1 - self.betas[:step + 1]).prod() else: return (1 - self.betas).cumprod(dim=0)[step].view(-1, 1, 1)
def get_training_item(self, x: torch.Tensor, tensor_step: bool = False) -> TrainingItem: """Create a noisy data item for diffusion model training:
Args: x (torch.Tensor): clean audio data torch.tensor(bs, 1, T) tensor_step (bool): If tensor_step = false, only one step t is sample, the whole batch is diffused to the same step and t is int. If tensor_step = true, t is a tensor of size (x.size(0),) every element of the batch is diffused to a independently sampled. """ step: tp.Union[int, torch.Tensor] if tensor_step: bs = x.size(0) step = torch.randint(0, self.num_steps, size=(bs,), device=x.device) else: step = self.rng.randrange(self.num_steps) alpha_bar = self.get_alpha_bar(step) # [batch_size, n_bands, 1]
x = self.sample_processor.project_sample(x) noise = torch.randn_like(x) noisy = (alpha_bar.sqrt() / self.rescale) * x + (1 - alpha_bar).sqrt() * noise * self.noise_scale return TrainingItem(noisy, noise, step)
def generate(self, model: torch.nn.Module, initial: tp.Optional[torch.Tensor] = None, condition: tp.Optional[torch.Tensor] = None, return_list: bool = False): """Full ddpm reverse process.
Args: model (nn.Module): Diffusion model. initial (tensor): Initial Noise. condition (tensor): Input conditionning Tensor (e.g. encodec compressed representation). return_list (bool): Whether to return the whole process or only the sampled point. """ alpha_bar = self.get_alpha_bar(step=self.num_steps - 1) current = initial iterates = [initial] for step in range(self.num_steps)[::-1]: with torch.no_grad(): estimate = model(current, step, condition=condition).sample alpha = 1 - self.betas[step] previous = (current - (1 - alpha) / (1 - alpha_bar).sqrt() * estimate) / alpha.sqrt() previous_alpha_bar = self.get_alpha_bar(step=step - 1) if step == 0: sigma2 = 0 elif self.variance == 'beta': sigma2 = 1 - alpha elif self.variance == 'beta_tilde': sigma2 = (1 - previous_alpha_bar) / (1 - alpha_bar) * (1 - alpha) elif self.variance == 'none': sigma2 = 0 else: raise ValueError(f'Invalid variance type {self.variance}')
if sigma2 > 0: previous += sigma2**0.5 * torch.randn_like(previous) * self.noise_scale if self.clip: previous = previous.clamp(-self.clip, self.clip) current = previous alpha_bar = previous_alpha_bar if step == 0: previous *= self.rescale if return_list: iterates.append(previous.cpu())
if return_list: return iterates else: return self.sample_processor.return_sample(previous)
def generate_subsampled(self, model: torch.nn.Module, initial: torch.Tensor, step_list: tp.Optional[list] = None, condition: tp.Optional[torch.Tensor] = None, return_list: bool = False): """Reverse process that only goes through Markov chain states in step_list.""" if step_list is None: step_list = list(range(1000))[::-50] + [0] alpha_bar = self.get_alpha_bar(step=self.num_steps - 1) alpha_bars_subsampled = (1 - self.betas).cumprod(dim=0)[list(reversed(step_list))].cpu() betas_subsampled = betas_from_alpha_bar(alpha_bars_subsampled) current = initial * self.noise_scale iterates = [current] for idx, step in enumerate(step_list[:-1]): with torch.no_grad(): estimate = model(current, step, condition=condition).sample * self.noise_scale alpha = 1 - betas_subsampled[-1 - idx] previous = (current - (1 - alpha) / (1 - alpha_bar).sqrt() * estimate) / alpha.sqrt() previous_alpha_bar = self.get_alpha_bar(step_list[idx + 1]) if step == step_list[-2]: sigma2 = 0 previous_alpha_bar = torch.tensor(1.0) else: sigma2 = (1 - previous_alpha_bar) / (1 - alpha_bar) * (1 - alpha) if sigma2 > 0: previous += sigma2**0.5 * torch.randn_like(previous) * self.noise_scale if self.clip: previous = previous.clamp(-self.clip, self.clip) current = previous alpha_bar = previous_alpha_bar if step == 0: previous *= self.rescale if return_list: iterates.append(previous.cpu()) if return_list: return iterates else: return self.sample_processor.return_sample(previous)17 unmodified lines1819202122232411 unmodified lines363738394041424311 unmodified lines5556575859606112 unmodified lines74757677787980811 unmodified line83848586878889909192939495962 unmodified lines9910010110210310410510610710810911011111211311411511611711811912012112212312417 unmodified linesdim (int): Embedding dimension.smoothing (float): Smoothing factor applied to the decay rates.base_scale (int): Base decay rate, given in terms of scaling time.device (torch.device or None): Device on which to initialize the module.dtype (torch.dtype): dtype to use to generate the embedding."""def __init__(self, dim: int, smoothing: float = 0.4, base_scale: int = 512,11 unmodified linesself.decay: tp.Optional[torch.Tensor] = Nonedef get_decay(self, start: int, end: int):"""Create complex decay tensor, cache values for fast computation."""if self.decay is None or end > self.decay.shape[0]:assert isinstance(self.decay_rates, torch.Tensor) # Satisfy type checker.idx = torch.arange(end, device=self.decay_rates.device, dtype=self.dtype)11 unmodified linesmax_period (float): Maximum period of the rotation frequencies.xpos (bool): Use xPos, applies an exponential decay to rotation matrix.scale (float): Scale of positional embedding, set to 0 to deactivate.device (torch.device or None): Device on which to initialize the module.dtype (torch.dtype): dtype to use to generate the embedding."""def __init__(self, dim: int, max_period: float = 10000.0, xpos: bool = False,12 unmodified linesself.xpos = XPos(dim, device=device, dtype=dtype) if xpos else Nonedef get_rotation(self, start: int, end: int):"""Create complex rotation tensor, cache values for fast computation."""if self.rotation is None or end > self.rotation.shape[0]:assert isinstance(self.frequencies, torch.Tensor) # Satisfy type checker.idx = torch.arange(end, device=self.frequencies.device, dtype=self.dtype)1 unmodified lineself.rotation = torch.polar(torch.ones_like(angles), angles)return self.rotation[start:end]def rotate(self, x: torch.Tensor, start: int = 0, invert_decay: bool = False):"""Apply rope rotation to query or key tensor."""T = x.shape[1]rotation = self.get_rotation(start, start + T).unsqueeze(0).unsqueeze(2)if self.xpos:decay = self.xpos.get_decay(start, start + T).unsqueeze(0).unsqueeze(2)else:decay = 1.02 unmodified linesx_complex = torch.view_as_complex(x.to(self.dtype).reshape(*x.shape[:-1], -1, 2))scaled_rotation = (rotation * decay) * self.scale + (1.0 - self.scale)x_out = torch.view_as_real(x_complex * scaled_rotation).flatten(-2)return x_out.type_as(x)def rotate_qk(self, query: torch.Tensor, key: torch.Tensor, start: int = 0):""" Apply rope rotation to both query and key tensors.Supports streaming mode, in which query and key are not expected to have the same shape.In streaming mode, key will be of legnth [P + C] with P the cached past timesteps, butquery will be [C] (typically C == 1).Args:query (torch.Tensor): Query to rotate.key (torch.Tensor): Key to rotate.start (int): Start index of the sequence for time offset."""query_timesteps = query.shape[1]key_timesteps = key.shape[1]streaming_offset = key_timesteps - query_timestepsquery_out = self.rotate(query, start + streaming_offset)key_out = self.rotate(key, start, invert_decay=True)return query_out, key_out17 unmodified lines1819202122232411 unmodified lines3637383940414211 unmodified lines5455565758596012 unmodified lines737475767778791 unmodified line818283848586878889909192939495962 unmodified lines9910010110210310410510610710810911011111211311411511611711811912012112212312412517 unmodified linesdim (int): Embedding dimension.smoothing (float): Smoothing factor applied to the decay rates.base_scale (int): Base decay rate, given in terms of scaling time.device (torch.device, optional): Device on which to initialize the module.dtype (torch.dtype): dtype to use to generate the embedding."""def __init__(self, dim: int, smoothing: float = 0.4, base_scale: int = 512,11 unmodified linesself.decay: tp.Optional[torch.Tensor] = Nonedef get_decay(self, start: int, end: int):"""Create complex decay tensor, cache values for fast computation."""if self.decay is None or end > self.decay.shape[0]:assert isinstance(self.decay_rates, torch.Tensor) # Satisfy type checker.idx = torch.arange(end, device=self.decay_rates.device, dtype=self.dtype)11 unmodified linesmax_period (float): Maximum period of the rotation frequencies.xpos (bool): Use xPos, applies an exponential decay to rotation matrix.scale (float): Scale of positional embedding, set to 0 to deactivate.device (torch.device, optional): Device on which to initialize the module.dtype (torch.dtype): dtype to use to generate the embedding."""def __init__(self, dim: int, max_period: float = 10000.0, xpos: bool = False,12 unmodified linesself.xpos = XPos(dim, device=device, dtype=dtype) if xpos else Nonedef get_rotation(self, start: int, end: int):"""Create complex rotation tensor, cache values for fast computation."""if self.rotation is None or end > self.rotation.shape[0]:assert isinstance(self.frequencies, torch.Tensor) # Satisfy type checker.idx = torch.arange(end, device=self.frequencies.device, dtype=self.dtype)1 unmodified lineself.rotation = torch.polar(torch.ones_like(angles), angles)return self.rotation[start:end]def rotate(self, x: torch.Tensor, start: int = 0, time_dim: int = 1, invert_decay: bool = False):"""Apply rope rotation to query or key tensor."""T = x.shape[time_dim]target_shape = [1] * x.dim()target_shape[time_dim] = Ttarget_shape[-1] = -1rotation = self.get_rotation(start, start + T).view(target_shape)if self.xpos:decay = self.xpos.get_decay(start, start + T).view(target_shape)else:decay = 1.02 unmodified linesx_complex = torch.view_as_complex(x.to(self.dtype).reshape(*x.shape[:-1], -1, 2))scaled_rotation = (rotation * decay) * self.scale + (1.0 - self.scale)x_out = torch.view_as_real(x_complex * scaled_rotation).view_as(x)return x_out.type_as(x)def rotate_qk(self, query: torch.Tensor, key: torch.Tensor, start: int = 0, time_dim: int = 1):""" Apply rope rotation to both query and key tensors.Supports streaming mode, in which query and key are not expected to have the same shape.In streaming mode, key will be of length [P + C] with P the cached past timesteps, butquery will be [C] (typically C == 1).Args:query (torch.Tensor): Query to rotate.key (torch.Tensor): Key to rotate.start (int): Start index of the sequence for time offset.time_dim (int): which dimension represent the time steps."""query_timesteps = query.shape[time_dim]key_timesteps = key.shape[time_dim]streaming_offset = key_timesteps - query_timestepsquery_out = self.rotate(query, start + streaming_offset, time_dim)key_out = self.rotate(key, start, time_dim, invert_decay=True)return query_out, key_out
34 unmodified lines353637383940414246 unmodified lines899091929394959697989911 unmodified lines11111211311411511611711811912012112212312419 unmodified lines14414514614714814915015115215315415515615715815916016116216316416568 unmodified lines23423523623723823924024124224324424524624716 unmodified lines26426526626726826927027 unmodified lines2982993003013023033043055 unmodified lines31131231331431531631731831932032132232332432532667 unmodified lines39439539639739839940040153 unmodified lines4554564574584594604613 unmodified lines46546646746846947047147247347447547647747847996 unmodified lines57657757857958058158258358458558658758858959059159259359459559659759859960060160260360460543 unmodified lines64965065165265365465557 unmodified lines71371471571671771871934 unmodified lines_efficient_attention_backend = backenddef _get_attention_time_dimension() -> int:if _efficient_attention_backend == 'torch':return 2else:return 146 unmodified linesreturn torch.cat([torch.cos(phase), torch.sin(phase)], dim=-1)def expand_repeated_kv(x: torch.Tensor, n_rep: int) -> torch.Tensor:"""torch.repeat_interleave(x, dim=2, repeats=n_rep) from xlformers"""if n_rep == 1:return xif _efficient_attention_backend == 'torch':bs, n_kv_heads, slen, head_dim = x.shapereturn (x[:, :, None, :, :]11 unmodified linesclass LayerScale(nn.Module):"""Layer scale from [Touvron et al 2021] (https://arxiv.org/pdf/2103.17239.pdf).This rescales diagonaly the residual outputs close to 0, with a learnt scale.Args:channels (int): Number of channels.init (float): Initial scale.channel_last (bool): If True, expect `[*, C]` shaped tensors, otherwise, `[*, C, T]`.device (torch.device or None): Device on which to initialize the module.dtype (torch.dtype or None): dtype to use to initialize the module."""def __init__(self, channels: int, init: float = 1e-4, channel_last: bool = True,device=None, dtype=None):19 unmodified linesdropout (float): Dropout level.bias (bool): Use bias in projections.causal (bool): Causal mask applied automatically.past_context (int or None): Receptive field for the causal mask, infinite if None.custom (bool): Use custom MHA implementation, for testing / benchmarking.memory_efficient (bool): Use xformers based memory efficient attention.attention_as_float32 (bool): Perform the attention as float32(especially important with memory_efficient as autocast won't do this automatically).rope (`RotaryEmbedding` or None): Rope embedding to use.cross_attention: Should be true when used as a cross attention.All keys and values must be available at once, streaming is only for the queries.Cannot be used with `causal` or `rope` (as it wouldn't make sens tointepret the time steps in the keys relative to those in the queries).safe_streaming (bool): Bug fix, will go away with xformers update.qk_layer_norm (bool): Layer normalization applied to queries and keys before dot product.kv_repeat (int): If > 1, will repeat keys and queries multiple times (need to divide num_heads).This will lead to faster decoding time on A100 or other GPUs with tensorcore.device (torch.device or None): Sevice on which to initialize.dtype (torch.dtype or None): dtype to use."""def __init__(self, embed_dim: int, num_heads: int, dropout: float = 0.0, bias: bool = True,causal: bool = False, past_context: tp.Optional[int] = None, custom: bool = False,68 unmodified lines# Return a causal mask, accounting for potentially stored past keys/values# We actually return a bias for the attention score, as this has the same# convention both in the builtin MHA in Pytorch, and Xformers functions.time_dim = _get_attention_time_dimension()if self.memory_efficient:from xformers.ops import LowerTriangularMaskif current_steps == 1:# If we only have one step, then we do not need a mask.return Noneelif 'past_keys' in self._streaming_state:raise RuntimeError('Not supported at the moment')else:# Then we can safely use a lower triangular maskreturn LowerTriangularMask()16 unmodified linestorch.full([], float('-inf'), device=device, dtype=dtype))def _complete_kv(self, k, v):time_dim = _get_attention_time_dimension()if self.cross_attention:# With cross attention we assume all keys and values# are already available, and streaming is with respect27 unmodified linesreturn nk, nvdef _apply_rope(self, query: torch.Tensor, key: torch.Tensor):# TODO: fix and verify layout.assert _efficient_attention_backend == 'xformers', 'Rope not supported with torch attn.'# Apply rope embeddings to query and key tensors.assert self.rope is not Noneif 'past_keys' in self._streaming_state:5 unmodified lineselse:past_context_offset = 0streaming_offset = past_context_offset + past_keys_offsetreturn self.rope.rotate_qk(query, key, start=streaming_offset)def forward(self, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor,key_padding_mask=None, need_weights=False, attn_mask=None,average_attn_weights=True, is_causal=False):assert attn_mask is Noneassert not is_causal, ("new param added in torch 2.0.1 not supported, ""use the causal args in the constructor.")time_dim = _get_attention_time_dimension()if time_dim == 2:layout = "b h t d"else:67 unmodified linesq, k = self._apply_rope(q, k)k, v = self._complete_kv(k, v)if self.kv_repeat > 1:k = expand_repeated_kv(k, self.kv_repeat)v = expand_repeated_kv(v, self.kv_repeat)if self.attention_as_float32:q, k, v = [x.float() for x in [q, k, v]]if self.memory_efficient:53 unmodified linesbias_ff (bool): Use bias for FF.bias_attn (bool): Use bias for MHA.causal (bool): Causal mask applied automatically.past_context (int or None): Receptive field for the causal mask, infinite if None.custom (bool): Use custom MHA implementation, for testing / benchmarking.memory_efficient (bool): Use xformers based memory efficient attention.attention_as_float32 (bool): Perform the attention as float323 unmodified linescross_attention (bool): If True, expect to get secondary input for cross-attention.Cross attention will use the default MHA, as it typically won't requirespecial treatment.layer_scale (float or None): If not None, LayerScale will be used withthe given value as initial scale.rope (`RotaryEmbedding` or None): Rope embedding to use.attention_dropout (float or None): If not None, separate the value of the dimension dropoutin FFN and of the attention dropout.kv_repeat (int): If > 1, will repeat keys and queries multiple times (need to divide num_heads).This will lead to faster decoding time on A100 or other GPUs with tensorcore.device (torch.device or None): Device on which to initialize.dtype (torch.dtype or None): dtype to use.**kwargs: See `nn.TransformerEncoderLayer`."""def __init__(self, d_model: int, num_heads: int, dim_feedforward: int = 2048, dropout: float = 0.1,96 unmodified linesbias_ff (bool): Use bias for FF.bias_attn (bool): Use bias for MHA.causal (bool): Causal mask applied automatically.past_context (int or None): Receptive field for the causal mask, infinite if None.custom (bool): Use custom MHA implementation, for testing / benchmarking.memory_efficient (bool): Use xformers based memory efficient attention.attention_as_float32 (bool): Perform the attention as float32(especially important with memory_efficient as autocast won't do this automatically).cross_attention (bool): If True, expect to get secondary input for cross-attention.layer_scale (float or None): If not None, LayerScale will be usedwith the given value as initial scale.positional_embedding (str): Positional embedding strategy (sin, rope, or sin_rope).max_period (float): Maximum period of the time embedding.positional_scale (float): Scale of positional embedding, set to 0 to deactivate.xpos (bool): Apply xpos exponential decay to positional embedding (rope only).lr (float or None): learning rate override through the `make_optim_group` API.weight_decay (float or None): Weight_decay override through the `make_optim_group` API.layer_class: (subclass of `StreamingTransformerLayer): class to useto initialize the layers, allowing further customization outside of Audiocraft.checkpointing (str): Checkpointing strategy to reduce memory usage.No checkpointing if set to 'none'. Per layer checkpointing using PyTorchif set to 'torch' (entire layer checkpointed, i.e. linears are evaluated twice,minimal memory usage, but maximal runtime). Finally, `xformers_default` providea policy for opting-out some operations of the checkpointing likelinear layers and attention, providing a middle ground between speed and memory.device (torch.device or None): Device on which to initialize.dtype (torch.dtype or None): dtype to use.**kwargs: See `nn.TransformerEncoderLayer`."""def __init__(self, d_model: int, num_heads: int, num_layers: int, dim_feedforward: int = 2048,43 unmodified lines# see audiocraft/optim/fsdp.py, magic signal to indicate this requires fixing the# backward hook inside of FSDP...layer._magma_checkpointed = True # type: ignoreassert layer.layer_drop == 0., "Need further checking" # type: ignoredef _apply_layer(self, layer, *args, **kwargs):method = self.checkpointing57 unmodified linesreturn group# special attention attention related functiondef _verify_xformers_memory_efficient_compat():try:34 unmodified lines353637383940414246 unmodified lines899091929394959697989911 unmodified lines11111211311411511611711811912012112212312419 unmodified lines14414514614714814915015115215315415515615715815916016116216316416568 unmodified lines23423523623723823924024124224324424524624716 unmodified lines26426526626726826927027 unmodified lines2982993003013023033045 unmodified lines31031131231331431531631731831932032132232332432567 unmodified lines39339439539639739839940053 unmodified lines4544554564574584594603 unmodified lines46446546646746846947047147247347447547647747896 unmodified lines57557657757857958058158258358458558658758858959059159259359459559659759859960060160260360443 unmodified lines64864965065165265357 unmodified lines71171271371471571671734 unmodified lines_efficient_attention_backend = backenddef _get_attention_time_dimension(memory_efficient: bool) -> int:if _efficient_attention_backend == 'torch' and memory_efficient:return 2else:return 146 unmodified linesreturn torch.cat([torch.cos(phase), torch.sin(phase)], dim=-1)def expand_repeated_kv(x: torch.Tensor, n_rep: int, memory_efficient: bool) -> torch.Tensor:"""torch.repeat_interleave(x, dim=2, repeats=n_rep) from xlformers."""if n_rep == 1:return xif _efficient_attention_backend == 'torch' and memory_efficient:bs, n_kv_heads, slen, head_dim = x.shapereturn (x[:, :, None, :, :]11 unmodified linesclass LayerScale(nn.Module):"""Layer scale from [Touvron et al 2021] (https://arxiv.org/pdf/2103.17239.pdf).This rescales diagonally the residual outputs close to 0, with a learnt scale.Args:channels (int): Number of channels.init (float): Initial scale.channel_last (bool): If True, expect `[*, C]` shaped tensors, otherwise, `[*, C, T]`.device (torch.device or str, optional): Device on which to initialize the module.dtype (torch.dtype, optional): dtype to use to initialize the module."""def __init__(self, channels: int, init: float = 1e-4, channel_last: bool = True,device=None, dtype=None):19 unmodified linesdropout (float): Dropout level.bias (bool): Use bias in projections.causal (bool): Causal mask applied automatically.past_context (int, optional): Receptive field for the causal mask, infinite if None.custom (bool): Use custom MHA implementation, for testing / benchmarking.memory_efficient (bool): Use xformers based memory efficient attention.attention_as_float32 (bool): Perform the attention as float32(especially important with memory_efficient as autocast won't do this automatically).rope (`RotaryEmbedding`, optional): Rope embedding to use.cross_attention: Should be true when used as a cross attention.All keys and values must be available at once, streaming is only for the queries.Cannot be used with `causal` or `rope` (as it wouldn't make sens tointerpret the time steps in the keys relative to those in the queries).safe_streaming (bool): Bug fix, will go away with xformers update.qk_layer_norm (bool): Layer normalization applied to queries and keys before dot product.kv_repeat (int): If > 1, will repeat keys and queries multiple times (need to divide num_heads).This will lead to faster decoding time on A100 or other GPUs with tensorcore.device (torch.device, optional): Device on which to initialize.dtype (torch.dtype, optional): dtype to use."""def __init__(self, embed_dim: int, num_heads: int, dropout: float = 0.0, bias: bool = True,causal: bool = False, past_context: tp.Optional[int] = None, custom: bool = False,68 unmodified lines# Return a causal mask, accounting for potentially stored past keys/values# We actually return a bias for the attention score, as this has the same# convention both in the builtin MHA in Pytorch, and Xformers functions.time_dim = _get_attention_time_dimension(self.memory_efficient)if self.memory_efficient:from xformers.ops import LowerTriangularMaskif current_steps == 1:# If we only have one step, then we do not need a mask.return Noneelif 'past_keys' in self._streaming_state:raise RuntimeError("Not supported at the moment")else:# Then we can safely use a lower triangular maskreturn LowerTriangularMask()16 unmodified linestorch.full([], float('-inf'), device=device, dtype=dtype))def _complete_kv(self, k, v):time_dim = _get_attention_time_dimension(self.memory_efficient)if self.cross_attention:# With cross attention we assume all keys and values# are already available, and streaming is with respect27 unmodified linesreturn nk, nvdef _apply_rope(self, query: torch.Tensor, key: torch.Tensor):time_dim = _get_attention_time_dimension(self.memory_efficient)# Apply rope embeddings to query and key tensors.assert self.rope is not Noneif 'past_keys' in self._streaming_state:5 unmodified lineselse:past_context_offset = 0streaming_offset = past_context_offset + past_keys_offsetreturn self.rope.rotate_qk(query, key, start=streaming_offset, time_dim=time_dim)def forward(self, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor,key_padding_mask=None, need_weights=False, attn_mask=None,average_attn_weights=True, is_causal=False):assert attn_mask is Noneassert not is_causal, ("New param added in torch 2.0.1 not supported, ""use the causal args in the constructor.")time_dim = _get_attention_time_dimension(self.memory_efficient)if time_dim == 2:layout = "b h t d"else:67 unmodified linesq, k = self._apply_rope(q, k)k, v = self._complete_kv(k, v)if self.kv_repeat > 1:k = expand_repeated_kv(k, self.kv_repeat, self.memory_efficient)v = expand_repeated_kv(v, self.kv_repeat, self.memory_efficient)if self.attention_as_float32:q, k, v = [x.float() for x in [q, k, v]]if self.memory_efficient:53 unmodified linesbias_ff (bool): Use bias for FF.bias_attn (bool): Use bias for MHA.causal (bool): Causal mask applied automatically.past_context (int, optional): Receptive field for the causal mask, infinite if None.custom (bool): Use custom MHA implementation, for testing / benchmarking.memory_efficient (bool): Use xformers based memory efficient attention.attention_as_float32 (bool): Perform the attention as float323 unmodified linescross_attention (bool): If True, expect to get secondary input for cross-attention.Cross attention will use the default MHA, as it typically won't requirespecial treatment.layer_scale (float, optional): If not None, LayerScale will be used withthe given value as initial scale.rope (`RotaryEmbedding`, optional): Rope embedding to use.attention_dropout (float, optional): If not None, separate the value of the dimension dropoutin FFN and of the attention dropout.kv_repeat (int): If > 1, will repeat keys and queries multiple times (need to divide num_heads).This will lead to faster decoding time on A100 or other GPUs with tensorcore.device (torch.device, optional): Device on which to initialize.dtype (torch.dtype, optional): dtype to use.**kwargs: See `nn.TransformerEncoderLayer`."""def __init__(self, d_model: int, num_heads: int, dim_feedforward: int = 2048, dropout: float = 0.1,96 unmodified linesbias_ff (bool): Use bias for FF.bias_attn (bool): Use bias for MHA.causal (bool): Causal mask applied automatically.past_context (int, optional): Receptive field for the causal mask, infinite if None.custom (bool): Use custom MHA implementation, for testing / benchmarking.memory_efficient (bool): Use xformers based memory efficient attention.attention_as_float32 (bool): Perform the attention as float32(especially important with memory_efficient as autocast won't do this automatically).cross_attention (bool): If True, expect to get secondary input for cross-attention.layer_scale (float, optional): If not None, LayerScale will be usedwith the given value as initial scale.positional_embedding (str): Positional embedding strategy (sin, rope, or sin_rope).max_period (float): Maximum period of the time embedding.positional_scale (float): Scale of positional embedding, set to 0 to deactivate.xpos (bool): Apply xpos exponential decay to positional embedding (rope only).lr (float, optional): learning rate override through the `make_optim_group` API.weight_decay (float, optional): Weight_decay override through the `make_optim_group` API.layer_class: (subclass of `StreamingTransformerLayer): class to useto initialize the layers, allowing further customization outside of AudioCraft.checkpointing (str): Checkpointing strategy to reduce memory usage.No checkpointing if set to 'none'. Per layer checkpointing using PyTorchif set to 'torch' (entire layer checkpointed, i.e. linears are evaluated twice,minimal memory usage, but maximal runtime). Finally, `xformers_default` providea policy for opting-out some operations of the checkpointing likelinear layers and attention, providing a middle ground between speed and memory.device (torch.device, optional): Device on which to initialize.dtype (torch.dtype, optional): dtype to use.**kwargs: See `nn.TransformerEncoderLayer`."""def __init__(self, d_model: int, num_heads: int, num_layers: int, dim_feedforward: int = 2048,43 unmodified lines# see audiocraft/optim/fsdp.py, magic signal to indicate this requires fixing the# backward hook inside of FSDP...layer._magma_checkpointed = True # type: ignoredef _apply_layer(self, layer, *args, **kwargs):method = self.checkpointing57 unmodified linesreturn group# special attention related functiondef _verify_xformers_memory_efficient_compat():try:
74 unmodified lines75767778798081155 unmodified lines23723823924024124224396 unmodified lines34034134234334434534624 unmodified lines37137237337437537637737837938038174 unmodified linesreturn means, binsdef orthgonal_loss_fn(t):# eq (2) from https://arxiv.org/abs/2112.00384n = t.shape[0]normed_codes = l2norm(t)155 unmodified linesorthogonal_reg_weight (float): Orthogonal regularization weights.orthogonal_reg_active_codes_only (bool): Apply orthogonal regularization only on active codes.orthogonal_reg_max_codes (optional int): Maximum number of codes to considerfor orthogonal regulariation.threshold_ema_dead_code (int): Threshold for dead code expiration. Replace any codesthat have an exponential moving average cluster size less than the specified threshold withrandomly selected vector from the current batch.96 unmodified linesrand_ids = torch.randperm(num_codes, device=device)[:self.orthogonal_reg_max_codes]codebook = codebook[rand_ids]orthogonal_reg_loss = orthgonal_loss_fn(codebook)loss = loss + orthogonal_reg_loss * self.orthogonal_reg_weightquantize = self.project_out(quantize)24 unmodified linesfor i, layer in enumerate(self.layers[:n_q]):quantized, indices, loss = layer(residual)residual = residual - quantizedquantized_out = quantized_out + quantizedall_indices.append(indices)all_losses.append(loss)out_losses, out_indices = map(torch.stack, (all_losses, all_indices))return quantized_out, out_indices, out_losses74 unmodified lines75767778798081155 unmodified lines23723823924024124224396 unmodified lines34034134234334434534624 unmodified lines37137237337437537637737837938038138238338438538674 unmodified linesreturn means, binsdef orthogonal_loss_fn(t):# eq (2) from https://arxiv.org/abs/2112.00384n = t.shape[0]normed_codes = l2norm(t)155 unmodified linesorthogonal_reg_weight (float): Orthogonal regularization weights.orthogonal_reg_active_codes_only (bool): Apply orthogonal regularization only on active codes.orthogonal_reg_max_codes (optional int): Maximum number of codes to considerfor orthogonal regularization.threshold_ema_dead_code (int): Threshold for dead code expiration. Replace any codesthat have an exponential moving average cluster size less than the specified threshold withrandomly selected vector from the current batch.96 unmodified linesrand_ids = torch.randperm(num_codes, device=device)[:self.orthogonal_reg_max_codes]codebook = codebook[rand_ids]orthogonal_reg_loss = orthogonal_loss_fn(codebook)loss = loss + orthogonal_reg_loss * self.orthogonal_reg_weightquantize = self.project_out(quantize)24 unmodified linesfor i, layer in enumerate(self.layers[:n_q]):quantized, indices, loss = layer(residual)quantized = quantized.detach()residual = residual - quantizedquantized_out = quantized_out + quantizedall_indices.append(indices)all_losses.append(loss)if self.training:# Solving subtle bug with STE and RVQ: https://github.com/facebookresearch/encodec/issues/25quantized_out = x + (quantized_out - x).detach()out_losses, out_indices = map(torch.stack, (all_losses, all_indices))return quantized_out, out_indices, out_losses
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324# Copyright (c) Meta Platforms, Inc. and affiliates.# All rights reserved.## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.
from concurrent.futures import ThreadPoolExecutorfrom collections import dequefrom functools import partialfrom hashlib import sha1import loggingfrom pathlib import Pathimport sysimport typing as tpimport zipfile
import flashyimport torch
logger = logging.getLogger(__name__)
def get_full_embed(full_embed: torch.Tensor, x: tp.Any, idx: int, device: tp.Union[str, torch.device]) -> torch.Tensor: """Utility function for the EmbeddingCache, returning the full embedding without any chunking. This method can be used in case there is no need in extracting a chunk of the full embedding read from the cache.
Args: full_embed (torch.Tensor): The full embedding. x (any): Batch object from which the full embedding is derived. idx (torch.Tensor): Index of object to consider in the batch object. Returns: full_embed (torch.Tensor): The full embedding """ return full_embed.to(device)
class EmbeddingCache: """Cache around embeddings computation for faster execution. The EmbeddingCache is storing pre-computed embeddings on disk and provides a simple API to retrieve the pre-computed embeddings on full inputs and extract only a given chunk using a user-provided function. When the cache is warm (all embeddings are pre-computed), the EmbeddingCache allows for faster training as it removes the need of computing the embeddings. Additionally, it provides in-memory cache around the loaded embeddings to limit IO footprint and synchronization points in the forward calls.
Args: cache_path (Path): Path to folder where all pre-computed embeddings are saved on disk. device (str or torch.device): Device on which the embedding is returned. compute_embed_fn (callable[[Path, any, int], torch.Tensor], optional): Function to compute the embedding from a given object and path. This user provided function can compute the embedding from the provided object or using the provided path as entry point. The last parameter specify the index corresponding to the current embedding in the object that can represent batch metadata. extract_embed_fn (callable[[torch.Tensor, any, int], torch.Tensor], optional): Function to extract the desired embedding chunk from the full embedding loaded from the cache. The last parameter specify the index corresponding to the current embedding in the object that can represent batch metadata. If not specified, will return the full embedding unmodified. """ def __init__(self, cache_path: tp.Union[str, Path], device: tp.Union[str, torch.device], compute_embed_fn: tp.Callable[[Path, tp.Any, int], torch.Tensor], extract_embed_fn: tp.Optional[tp.Callable[[torch.Tensor, tp.Any, int], torch.Tensor]] = None): self.cache_path = Path(cache_path) self.device = device self._compute_embed_fn = compute_embed_fn self._extract_embed_fn: tp.Callable[[torch.Tensor, tp.Any, int], torch.Tensor] if extract_embed_fn is not None: self._extract_embed_fn = extract_embed_fn else: self._extract_embed_fn = partial(get_full_embed, device=device) if self.cache_path is not None: self.cache_path.mkdir(exist_ok=True, parents=True) logger.info(f"Cache instantiated at: {self.cache_path}") self.pool = ThreadPoolExecutor(8) self.pool.__enter__() self._current_batch_cache: dict = {} self._memory_cache: dict = {}
def _get_cache_path(self, path: tp.Union[Path, str]): """Get cache path for the given file path.""" sig = sha1(str(path).encode()).hexdigest() return self.cache_path / sig
@staticmethod def _get_full_embed_from_cache(cache: Path): """Loads full pre-computed embedding from the cache.""" try: embed = torch.load(cache, 'cpu') except Exception as exc: logger.error("Error loading %s: %r", cache, exc) embed = None return embed
def get_embed_from_cache(self, paths: tp.List[Path], x: tp.Any) -> torch.Tensor: """Get embedding from cache, computing and storing it to cache if not already cached. The EmbeddingCache first tries to load the embedding from the in-memory cache containing the pre-computed chunks populated through `populate_embed_cache`. If not found, the full embedding is computed and stored on disk to be later accessed to populate the in-memory cache, and the desired embedding chunk is extracted and returned.
Args: paths (list[Path or str]): List of paths from where the embeddings can be loaded. x (any): Object from which the embedding is extracted. """ embeds = [] for idx, path in enumerate(paths): cache = self._get_cache_path(path) if cache in self._current_batch_cache: embed = self._current_batch_cache[cache] else: full_embed = self._compute_embed_fn(path, x, idx) try: with flashy.utils.write_and_rename(cache, pid=True) as f: torch.save(full_embed.cpu(), f) except Exception as exc: logger.error('Error saving embed %s (%s): %r', cache, full_embed.shape, exc) else: logger.info('New embed cache saved: %s (%s)', cache, full_embed.shape) embed = self._extract_embed_fn(full_embed, x, idx) embeds.append(embed) embed = torch.stack(embeds, dim=0) return embed
def populate_embed_cache(self, paths: tp.List[Path], x: tp.Any) -> None: """Populate in-memory caches for embeddings reading from the embeddings stored on disk. The in-memory caches consist in a cache for the full embedding and another cache for the final embedding chunk. Such caches are used to limit the IO access when computing the actual embeddings and reduce the IO footprint and synchronization points during forward passes.
Args: paths (list[Path]): List of paths from where the embeddings can be loaded. x (any): Object from which the embedding is extracted. """ self._current_batch_cache.clear() if self.cache_path is not None: futures: list = [] for path in paths: assert path is not None, "Path is required for computation from cache" cache = self._get_cache_path(path) if cache in self._memory_cache or not cache.exists(): futures.append(None) else: futures.append(self.pool.submit(EmbeddingCache._get_full_embed_from_cache, cache)) for idx, (path, future) in enumerate(zip(paths, futures)): assert path is not None cache = self._get_cache_path(path) full_embed = None if future is None: if cache in self._memory_cache: full_embed = self._memory_cache[cache] else: full_embed = future.result() if full_embed is not None: self._memory_cache[cache] = full_embed full_embed = full_embed.to(self.device) if full_embed is not None: embed = self._extract_embed_fn(full_embed, x, idx) self._current_batch_cache[cache] = embed
class CachedBatchWriter: """Write pre computed caches for mini batches. This can make loading a lot more efficient depending on your filesystem.
Args: cache_folder (Path): folder in which the cached minibatches will be stored.
Inside cache folder, the structure is the following: `epoch_number / update_number.zip` And the zip file contains one entry per batch item.
It is possible to use the cache with a batch size smaller than created with but obviously not larger. Make sure to call the `start_epoch(epoch)` method for indicating changes of epochs.
See the grid `audiocraft/grids/musicgen/musicgen_warmup_cache.py` for an example of how to warmup the cache. """ def __init__(self, cache_folder: Path): self.cache_folder = cache_folder self._current_epoch: tp.Optional[int] = None self._current_index = 0
def start_epoch(self, epoch: int): """Call at the beginning of each epoch. """ self._current_epoch = epoch self._current_index = 0 self._zip_path.parent.mkdir(exist_ok=True, parents=True)
@staticmethod def _get_zip_path(cache_folder: Path, epoch: int, index: int): return cache_folder / f"{epoch:05d}" / f"{index:06d}.zip"
@property def _zip_path(self): assert self._current_epoch is not None return CachedBatchWriter._get_zip_path(self.cache_folder, self._current_epoch, self._current_index)
def save(self, *content): """Save one mini batch. This function is distributed-aware and will automatically merge all the items from the different workers. """ all_contents = [] for rank in range(flashy.distrib.world_size()): their_content = flashy.distrib.broadcast_object(content, src=rank) all_contents.append(their_content)
if flashy.distrib.is_rank_zero(): idx = 0 with flashy.utils.write_and_rename(self._zip_path) as tmp: with zipfile.ZipFile(tmp, 'w') as zf: for content in all_contents: for vals in zip(*content): with zf.open(f'{idx}', 'w') as f: # type: ignore torch.save(vals, f) idx += 1 flashy.distrib.barrier() self._current_index += 1
class CachedBatchLoader: """Loader for cached mini-batches dumped with `CachedBatchWriter`.
Args: cache_folder (Path): folder in which the cached minibatches are stored. batch_size (int): batch size (per GPU) expected. num_workers (int): number of workers to use for loading. min_length (int): minimum expected length for each epoch. If some mini-batches are missing, and error is raised.
This is iterable just like a regular DataLoader. """
def __init__(self, cache_folder: Path, batch_size: int, num_workers: int = 10, min_length: int = 1): self.cache_folder = cache_folder self.batch_size = batch_size self.num_workers = num_workers self.min_length = min_length self._current_epoch: tp.Optional[int] = None self.sampler = None # for compatibility with the regular DataLoader
def __len__(self): path = CachedBatchWriter._get_zip_path(self.cache_folder, self._current_epoch or 0, 0).parent return len([p for p in path.iterdir() if p.suffix == ".zip"])
def start_epoch(self, epoch: int): """Call at the beginning of each epoch. """ self._current_epoch = epoch
def _zip_path(self, index: int): assert self._current_epoch is not None return CachedBatchWriter._get_zip_path(self.cache_folder, self._current_epoch, index)
def _load_one(self, index: int): zip_path = self._zip_path(index) if not zip_path.exists(): if index < self.min_length: raise RuntimeError(f"Cache should have at least {self.min_length} batches, but {index} doesn't exist")
return None mode = "rb" if sys.version_info >= (3, 9) else "r" try: with zipfile.ZipFile(zip_path, 'r') as zf: rank = flashy.distrib.rank() world_size = flashy.distrib.world_size() root = zipfile.Path(zf) items = list(root.iterdir()) total_batch_size = self.batch_size * world_size if len(items) < total_batch_size: raise RuntimeError( f"The cache can handle a max batch size of {len(items)}, " f"but {total_batch_size} is needed.") start = rank * self.batch_size items = items[start: start + self.batch_size] assert len(items) == self.batch_size entries = [] entries = [torch.load(item.open(mode), 'cpu') for item in items] # type: ignore transposed = zip(*entries) out = [] for part in transposed: assert len(part) > 0 if isinstance(part[0], torch.Tensor): out.append(torch.stack(part)) else: assert isinstance(part, torch.Tensor) out.append(part) return out except Exception: logger.error("Error when reading zip path %s", zip_path) raise
def __iter__(self): """This will yields tuples, exactly as provided to the `CachedBatchWriter.save` method. """ pool = ThreadPoolExecutor(self.num_workers) next_index = 0 queue = deque()
def _get_next(): nonlocal next_index r = queue.popleft().result() if r is None: return None else: queue.append(pool.submit(self._load_one, next_index)) next_index += 1 return r
with pool: # fill the buffer of fetching jobs. for _ in range(2 * self.num_workers): queue.append(pool.submit(self._load_one, next_index)) next_index += 1 while True: batch = _get_next() if batch is None: return yield batch123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475# Copyright (c) Meta Platforms, Inc. and affiliates.# All rights reserved.## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.
"""Utility functions for SLURM configuration and cluster settings."""
from enum import Enumimport osimport socketimport typing as tp
import omegaconf
class ClusterType(Enum): AWS = "aws" FAIR = "fair" RSC = "rsc" LOCAL_DARWIN = "darwin" DEFAULT = "default" # used for any other cluster.
def _guess_cluster_type() -> ClusterType: uname = os.uname() fqdn = socket.getfqdn() if uname.sysname == "Linux" and (uname.release.endswith("-aws") or ".ec2" in fqdn): return ClusterType.AWS
if fqdn.endswith(".fair"): return ClusterType.FAIR
if fqdn.endswith(".facebook.com"): return ClusterType.RSC
if uname.sysname == "Darwin": return ClusterType.LOCAL_DARWIN
return ClusterType.DEFAULT
def get_cluster_type( cluster_type: tp.Optional[ClusterType] = None,) -> tp.Optional[ClusterType]: if cluster_type is None: return _guess_cluster_type()
return cluster_type
def get_slurm_parameters( cfg: omegaconf.DictConfig, cluster_type: tp.Optional[ClusterType] = None) -> omegaconf.DictConfig: """Update SLURM parameters in configuration based on cluster type. If the cluster type is not specify, it infers it automatically. """ from ..environment import AudioCraftEnvironment cluster_type = get_cluster_type(cluster_type) # apply cluster-specific adjustments if cluster_type == ClusterType.AWS: cfg["mem_per_gpu"] = None cfg["constraint"] = None cfg["setup"] = [] elif cluster_type == ClusterType.RSC: cfg["mem_per_gpu"] = None cfg["setup"] = [] cfg["constraint"] = None cfg["partition"] = "learn" slurm_exclude = AudioCraftEnvironment.get_slurm_exclude() if slurm_exclude is not None: cfg["exclude"] = slurm_exclude return cfg10 unmodified lines1112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555610 unmodified linesfrom pathlib import Pathimport typing as tpfrom omegaconf import OmegaConf, DictConfigimport torchdef _clean_lm_cfg(cfg: DictConfig):OmegaConf.set_struct(cfg, False)# This used to be set automatically in the LM solver, need a more robust solution# for the future.cfg['transformer_lm']['card'] = 2048cfg['transformer_lm']['n_q'] = 4# Experimental params no longer supported.bad_params = ['spectral_norm_attn_iters', 'spectral_norm_ff_iters','residual_balancer_attn', 'residual_balancer_ff', 'layer_drop']for name in bad_params:del cfg['transformer_lm'][name]OmegaConf.set_struct(cfg, True)return cfgdef export_encodec(checkpoint_path: tp.Union[Path, str], out_folder: tp.Union[Path, str]):sig = Path(checkpoint_path).parent.nameassert len(sig) == 8, "Not a valid Dora signature"pkg = torch.load(checkpoint_path, 'cpu')new_pkg = {'best_state': pkg['ema']['state']['model'],'xp.cfg': OmegaConf.to_yaml(pkg['xp.cfg']),}out_file = Path(out_folder) / f'{sig}.th'torch.save(new_pkg, out_file)return out_filedef export_lm(checkpoint_path: tp.Union[Path, str], out_folder: tp.Union[Path, str]):sig = Path(checkpoint_path).parent.nameassert len(sig) == 8, "Not a valid Dora signature"pkg = torch.load(checkpoint_path, 'cpu')new_pkg = {'best_state': pkg['fsdp_best_state']['model'],'xp.cfg': OmegaConf.to_yaml(_clean_lm_cfg(pkg['xp.cfg']))}out_file = Path(out_folder) / f'{sig}.th'torch.save(new_pkg, out_file)return out_file10 unmodified lines11121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787910 unmodified linesfrom pathlib import Pathimport typing as tpfrom omegaconf import OmegaConfimport torchfrom audiocraft import __version__def export_encodec(checkpoint_path: tp.Union[Path, str], out_file: tp.Union[Path, str]):"""Export only the best state from the given EnCodec checkpoint. Thisshould be used if you trained your own EnCodec model."""pkg = torch.load(checkpoint_path, 'cpu')new_pkg = {'best_state': pkg['best_state']['model'],'xp.cfg': OmegaConf.to_yaml(pkg['xp.cfg']),'version': __version__,'exported': True,}Path(out_file).parent.mkdir(exist_ok=True, parents=True)torch.save(new_pkg, out_file)return out_filedef export_pretrained_compression_model(pretrained_encodec: str, out_file: tp.Union[Path, str]):"""Export a compression model (potentially EnCodec) from a pretrained model.This is required for packaging the audio tokenizer along a MusicGen or AudioGen model.Do not include the //pretrained/ prefix. For instance if you trained a modelwith `facebook/encodec_32khz`, just put that as a name. Same for `dac_44khz`.In that case, this will not actually include a copy of the model, simply the referenceto the model used."""if Path(pretrained_encodec).exists():pkg = torch.load(pretrained_encodec)assert 'best_state' in pkgassert 'xp.cfg' in pkgassert 'version' in pkgassert 'exported' in pkgelse:pkg = {'pretrained': pretrained_encodec,'exported': True,'version': __version__,}Path(out_file).parent.mkdir(exist_ok=True, parents=True)torch.save(pkg, out_file)def export_lm(checkpoint_path: tp.Union[Path, str], out_file: tp.Union[Path, str]):"""Export only the best state from the given MusicGen or AudioGen checkpoint."""pkg = torch.load(checkpoint_path, 'cpu')if pkg['fsdp_best_state']:best_state = pkg['fsdp_best_state']['model']else:assert pkg['best_state']best_state = pkg['best_state']['model']new_pkg = {'best_state': best_state,'xp.cfg': OmegaConf.to_yaml(pkg['xp.cfg']),'version': __version__,'exported': True,}Path(out_file).parent.mkdir(exist_ok=True, parents=True)torch.save(new_pkg, out_file)return out_file
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556# Copyright (c) Meta Platforms, Inc. and affiliates.# All rights reserved.## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.
"""Utility to export a training checkpoint to a lightweight release checkpoint."""
from pathlib import Pathimport typing as tp
from omegaconf import OmegaConf, DictConfigimport torch
def _clean_lm_cfg(cfg: DictConfig): OmegaConf.set_struct(cfg, False) # This used to be set automatically in the LM solver, need a more robust solution # for the future. cfg['transformer_lm']['card'] = 2048 cfg['transformer_lm']['n_q'] = 4 # Experimental params no longer supported. bad_params = ['spectral_norm_attn_iters', 'spectral_norm_ff_iters', 'residual_balancer_attn', 'residual_balancer_ff', 'layer_drop'] for name in bad_params: del cfg['transformer_lm'][name] OmegaConf.set_struct(cfg, True) return cfg
def export_encodec(checkpoint_path: tp.Union[Path, str], out_folder: tp.Union[Path, str]): sig = Path(checkpoint_path).parent.name assert len(sig) == 8, "Not a valid Dora signature" pkg = torch.load(checkpoint_path, 'cpu') new_pkg = { 'best_state': pkg['ema']['state']['model'], 'xp.cfg': OmegaConf.to_yaml(pkg['xp.cfg']), } out_file = Path(out_folder) / f'{sig}.th' torch.save(new_pkg, out_file) return out_file
def export_lm(checkpoint_path: tp.Union[Path, str], out_folder: tp.Union[Path, str]): sig = Path(checkpoint_path).parent.name assert len(sig) == 8, "Not a valid Dora signature" pkg = torch.load(checkpoint_path, 'cpu') new_pkg = { 'best_state': pkg['fsdp_best_state']['model'], 'xp.cfg': OmegaConf.to_yaml(_clean_lm_cfg(pkg['xp.cfg'])) } out_file = Path(out_folder) / f'{sig}.th' torch.save(new_pkg, out_file) return out_file178 unmodified lines17918018118218318418594 unmodified lines280281282283284285286287288178 unmodified linesdescriptions=[text],melody_wavs=verse,sample_rate=sr,progress=False,prompt=prompt_segment,)# If user selects a prompt segment, use the prompt segment for all segments94 unmodified linesif font is None:try:req = requests.get(font_name)font = ImageFont.truetype(BytesIO(req.content), font_size)except (FileNotFoundError, OSError):print(f"Font not found: {font_name} Using default font\n")if font:print(f"Font loaded {font.getname()}")else:178 unmodified lines17918018118218318418594 unmodified lines280281282283284285286287288289178 unmodified linesdescriptions=[text],melody_wavs=verse,sample_rate=sr,progress=True,prompt=prompt_segment,)# If user selects a prompt segment, use the prompt segment for all segments94 unmodified linesif font is None:try:req = requests.get(font_name)font = ImageFont.truetype(BytesIO(req.content), font_size)except (FileNotFoundError, OSError):print(f"Font not found: {font_name} Using default font\n")if font:print(f"Font loaded {font.getname()}")else:
4 unmodified lines56789101112136 unmodified lines202122232425146 unmodified lines17217317417517617717853 unmodified lines2322332344 unmodified lines# LICENSE file in the root directory of this source tree.from concurrent.futures import ProcessPoolExecutorfrom functools import wrapsimport hashlibimport loggingimport typing as tpimport flashy6 unmodified lineslogger = logging.getLogger(__name__)def dict_from_config(cfg: omegaconf.DictConfig) -> dict:"""Convenience function to map an omegaconf configuration to a dictionary.146 unmodified linesassert len(lengths.shape) == 1, "Length shape should be 1 dimensional."final_length = lengths.max().item() if not max_len else max_lenfinal_length = max(final_length, 1) # if all seqs are of len zero we don't want a zero-size tensorreturn torch.arange(final_length)[None, :].to(lengths.device) < lengths[:, None]def hash_trick(word: str, vocab_size: int) -> int:53 unmodified linespadded_tensors = padded_tensors.transpose(0, 1)padded_tensors = padded_tensors.transpose(1, dim + 1)return padded_tensors, lens4 unmodified lines56789101112131415166 unmodified lines232425262728293031323334353637383940146 unmodified lines18718818919019119219353 unmodified lines2472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993004 unmodified lines# LICENSE file in the root directory of this source tree.from concurrent.futures import ProcessPoolExecutorfrom contextlib import contextmanagerfrom functools import wraps, lru_cacheimport hashlibimport jsonimport loggingfrom pathlib import Pathimport typing as tpimport flashy6 unmodified lineslogger = logging.getLogger(__name__)def model_hash(model: torch.nn.Module) -> str:"""Return a model hash. This should allow us to track regressions in model initfrom the logs of past experiments."""hasher = hashlib.sha1()for p in model.parameters():hasher.update(p.data.cpu().numpy().tobytes())return hasher.hexdigest()def dict_from_config(cfg: omegaconf.DictConfig) -> dict:"""Convenience function to map an omegaconf configuration to a dictionary.146 unmodified linesassert len(lengths.shape) == 1, "Length shape should be 1 dimensional."final_length = lengths.max().item() if not max_len else max_lenfinal_length = max(final_length, 1) # if all seqs are of len zero we don't want a zero-size tensorreturn torch.arange(final_length, device=lengths.device)[None, :] < lengths[:, None]def hash_trick(word: str, vocab_size: int) -> int:53 unmodified linespadded_tensors = padded_tensors.transpose(0, 1)padded_tensors = padded_tensors.transpose(1, dim + 1)return padded_tensors, lens# TODO: Move to flashy?def copy_state(state: tp.Any, device: tp.Union[torch.device, str] = 'cpu',dtype: tp.Optional[torch.dtype] = None) -> tp.Any:if isinstance(state, torch.Tensor):if dtype is None or not state.is_floating_point():dtype = state.dtypereturn state.detach().to(device=device, dtype=dtype, copy=True)elif isinstance(state, dict):return {k: copy_state(v, device, dtype) for k, v in state.items()}elif isinstance(state, list):return [copy_state(v, device, dtype) for v in state]# TODO: Move to flashy?@contextmanagerdef swap_state(model, state, **kwargs):old_state = copy_state(model.state_dict())model.load_state_dict(state, **kwargs)try:yieldfinally:model.load_state_dict(old_state)@lru_cache(None)def warn_once(logger, msg):"""Warn about a given message only once."""logger.warning(msg)def is_jsonable(x: tp.Any):"""Check if an object can be serialized into a json:"""try:json.dumps(x)return Trueexcept (TypeError, OverflowError):return Falsedef load_clap_state_dict(clap_model, path: tp.Union[str, Path]):"""Wrapper around state dict loading of CLAP modeladdressing compatibility issues between CLAP and AudioCraftHuggingFace transformer version.See: https://github.com/LAION-AI/CLAP/issues/118"""from clap_module.factory import load_state_dict # type: ignorepkg = load_state_dict(path)pkg.pop('text_branch.embeddings.position_ids', None)clap_model.model.load_state_dict(pkg)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091# file_utilsimport osimport shutilfrom pathlib import Path
def get_file_parts(file_path: str): # Split the path into directory and filename directory, filename = os.path.split(file_path) # Split the filename into name and extension name, ext = os.path.splitext(filename) # Convert the extension to lowercase new_ext = ext.lower() return directory, filename, name, ext, new_ext
def rename_file_to_lowercase_extension(file_path: str) -> str: """ Renames a file's extension to lowercase in place.
Parameters: file_path (str): The original file path.
Returns: str: The new file path with the lowercase extension.
Raises: OSError: If there is an error renaming the file (e.g., file not found, permissions issue). """ directory, filename, name, ext, new_ext = get_file_parts(file_path) # If the extension changes, rename the file if ext != new_ext: new_filename = name + new_ext new_file_path = os.path.join(directory, new_filename) try: os.rename(file_path, new_file_path) print(f"Rename {file_path} to {new_file_path}\n") except Exception as e: print(f"os.rename failed: {e}. Falling back to binary copy operation.") try: # Read the file in binary mode and write it to new_file_path with open(file_path, 'rb') as f: data = f.read() with open(new_file_path, 'wb') as f: f.write(data) print(f"Copied {file_path} to {new_file_path}\n") # Optionally, remove the original file after copying #os.remove(file_path) except Exception as inner_e: print(f"Failed to copy file from {file_path} to {new_file_path}: {inner_e}") raise inner_e return new_file_path else: return file_path
def get_filename(file): # extract filename from file object filename = None if file is not None: filename = file.name return filename
def convert_title_to_filename(title): # convert title to filename filename = title.lower().replace(" ", "_").replace("/", "_") return filename
def get_filename_from_filepath(filepath): file_name = os.path.basename(filepath) file_base, file_extension = os.path.splitext(file_name) return file_base, file_extension
def delete_file(file_path: str) -> None: """ Deletes the specified file.
Parameters: file_path (str): The path to thefile to delete.
Raises: FileNotFoundError: If the file does not exist. Exception: If there is an error deleting the file. """ try: path = Path(file_path) path.unlink() print(f"Deleted original file: {file_path}") except FileNotFoundError: print(f"File not found: {file_path}") except Exception as e: print(f"Error deleting file: {e}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272# modules.gradio # holds updates and lost code from gradio changesimport osimport gradio as grimport numpy as npimport PILimport PIL.Imageimport shutilimport subprocessfrom tempfile import NamedTemporaryFilefrom pathlib import Path
class MatplotlibBackendMananger: def __enter__(self): try: import matplotlib
self._original_backend = matplotlib.get_backend() matplotlib.use("agg") except ImportError: pass
def __exit__(self, exc_type, exc_val, exc_tb): try: import matplotlib
matplotlib.use(self._original_backend) except ImportError: pass
gr.utils.MatplotlibBackendMananger = MatplotlibBackendMananger
def make_waveform( audio: str | tuple[int, np.ndarray], *, bg_color: str = "#f3f4f6", bg_image: str | None = None, fg_alpha: float = 0.75, bars_color: str | tuple[str, str] = ("#fbbf24", "#ea580c"), bar_count: int = 50, bar_width: float = 0.6, animate: bool = False, name: str = "",) -> str: """ Generates a waveform video from an audio file. Useful for creating an easy to share audio visualization. The output should be passed into a `gr.Video` component. Parameters: audio: Audio file path or tuple of (sample_rate, audio_data) bg_color: Background color of waveform (ignored if bg_image is provided) bg_image: Background image of waveform fg_alpha: Opacity of foreground waveform bars_color: Color of waveform bars. Can be a single color or a tuple of (start_color, end_color) of gradient bar_count: Number of bars in waveform bar_width: Width of bars in waveform. 1 represents full width, 0.5 represents half width, etc. animate: If true, the audio waveform overlay will be animated, if false, it will be static. Returns: A filepath to the output video in mp4 format. """ import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation
if isinstance(audio, str): audio_file = audio audio = gr.processing_utils.audio_from_file(audio) else: tmp_wav = NamedTemporaryFile(suffix=".wav", delete=False, prefix = name) gr.processing_utils.audio_to_file(audio[0], audio[1], tmp_wav.name, format="wav") audio_file = tmp_wav.name
if not os.path.isfile(audio_file): raise ValueError("Audio file not found.")
ffmpeg = shutil.which("ffmpeg") if not ffmpeg: raise RuntimeError("ffmpeg not found.")
duration = round(len(audio[1]) / audio[0], 4)
# Helper methods to create waveform def hex_to_rgb(hex_str): return [int(hex_str[i : i + 2], 16) for i in range(1, 6, 2)]
def get_color_gradient(c1, c2, n): if n < 1: raise ValueError("Must have at least one stop in gradient") c1_rgb = np.array(hex_to_rgb(c1)) / 255 c2_rgb = np.array(hex_to_rgb(c2)) / 255 mix_pcts = [x / (n - 1) for x in range(n)] rgb_colors = [((1 - mix) * c1_rgb + (mix * c2_rgb)) for mix in mix_pcts] return [ "#" + "".join(f"{int(round(val * 255)):02x}" for val in item) for item in rgb_colors ]
# Reshape audio to have a fixed number of bars samples = audio[1] if len(samples.shape) > 1: samples = np.mean(samples, 1) bins_to_pad = bar_count - (len(samples) % bar_count) samples = np.pad(samples, [(0, bins_to_pad)]) samples = np.reshape(samples, (bar_count, -1)) samples = np.abs(samples) samples = np.max(samples, 1)
with MatplotlibBackendMananger(): plt.clf() # Plot waveform color = ( bars_color if isinstance(bars_color, str) else get_color_gradient(bars_color[0], bars_color[1], bar_count) )
if animate: fig = plt.figure(figsize=(5, 1), dpi=200, frameon=False) fig.subplots_adjust(left=0, bottom=0, right=1, top=1) plt.axis("off") plt.margins(x=0)
bar_alpha = fg_alpha if animate else 1.0 barcollection = plt.bar( np.arange(0, bar_count), samples * 2, bottom=(-1 * samples), width=bar_width, color=color, alpha=bar_alpha, )
tmp_img = NamedTemporaryFile(suffix=".png", delete=False, prefix = name)
savefig_kwargs: dict[str, Any] = {"bbox_inches": "tight"} if bg_image is not None: savefig_kwargs["transparent"] = True if animate: savefig_kwargs["facecolor"] = "none" else: savefig_kwargs["facecolor"] = bg_color plt.savefig(tmp_img.name, **savefig_kwargs)
if not animate: waveform_img = PIL.Image.open(tmp_img.name) waveform_img = waveform_img.resize((1000, 400))
# Composite waveform with background image if bg_image is not None: waveform_array = np.array(waveform_img) waveform_array[:, :, 3] = waveform_array[:, :, 3] * fg_alpha waveform_img = PIL.Image.fromarray(waveform_array)
bg_img = PIL.Image.open(bg_image) waveform_width, waveform_height = waveform_img.size bg_width, bg_height = bg_img.size if waveform_width != bg_width: bg_img = bg_img.resize( ( waveform_width, 2 * int(bg_height * waveform_width / bg_width / 2), ) ) bg_width, bg_height = bg_img.size composite_height = max(bg_height, waveform_height) composite = PIL.Image.new( "RGBA", (waveform_width, composite_height), "#FFFFFF" ) composite.paste(bg_img, (0, composite_height - bg_height)) composite.paste( waveform_img, (0, composite_height - waveform_height), waveform_img ) composite.save(tmp_img.name) img_width, img_height = composite.size else: img_width, img_height = waveform_img.size waveform_img.save(tmp_img.name) else:
def _animate(_): for idx, b in enumerate(barcollection): rand_height = np.random.uniform(0.8, 1.2) b.set_height(samples[idx] * rand_height * 2) b.set_y((-rand_height * samples)[idx])
frames = int(duration * 10) anim = FuncAnimation( fig, # type: ignore _animate, # type: ignore repeat=False, blit=False, frames=frames, interval=100, ) anim.save( tmp_img.name, writer="pillow", fps=10, codec="png", savefig_kwargs=savefig_kwargs, )
# Convert waveform to video with ffmpeg output_mp4 = NamedTemporaryFile(suffix=".mp4", delete=False, prefix = name)
if animate and bg_image is not None: ffmpeg_cmd = [ ffmpeg, "-loop", "1", "-i", bg_image, "-i", tmp_img.name, "-i", audio_file, "-filter_complex", "[0:v]scale=w=trunc(iw/2)*2:h=trunc(ih/2)*2[bg];[1:v]format=rgba,colorchannelmixer=aa=1.0[ov];[bg][ov]overlay=(main_w-overlay_w*0.9)/2:main_h-overlay_h*0.9/2[output]", "-t", str(duration), "-map", "[output]", "-map", "2:a", "-c:v", "libx264", "-c:a", "aac", "-shortest", "-y", output_mp4.name, ] elif animate and bg_image is None: ffmpeg_cmd = [ ffmpeg, "-i", tmp_img.name, "-i", audio_file, "-filter_complex", "[0:v][1:a]concat=n=1:v=1:a=1[v];[v]scale=1000:400,format=yuv420p[v_scaled]", "-map", "[v_scaled]", "-map", "1:a", "-c:v", "libx264", "-c:a", "aac", "-shortest", "-y", output_mp4.name, ] else: ffmpeg_cmd = [ ffmpeg, "-loop", "1", "-i", tmp_img.name, "-i", audio_file, "-vf", f"color=c=#FFFFFF77:s={img_width}x{img_height}[bar];[0][bar]overlay=-w+(w/{duration})*t:H-h:shortest=1", # type: ignore "-t", str(duration), "-y", output_mp4.name, ]
subprocess.check_call(ffmpeg_cmd) return output_mp4.name
gr.make_waveform = make_waveform123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598"""User History is a plugin that you can add to your Spaces to cache generated images for your users.
Key features:- 🤗 Sign in with Hugging Face- Save generated image, video, audio and document files with their metadata: prompts, timestamp, hyper-parameters, etc.- Export your history as zip.- Delete your history to respect privacy.- Compatible with Persistent Storage for long-term storage.- Admin panel to check configuration and disk usage .
Useful links:- Demo: https://huggingface.co/spaces/Wauplin/gradio-user-history- README: https://huggingface.co/spaces/Wauplin/gradio-user-history/blob/main/README.md- Source file: https://huggingface.co/spaces/Wauplin/gradio-user-history/blob/main/user_history.py- Discussions: https://huggingface.co/spaces/Wauplin/gradio-user-history/discussions"""
__version__ = "0.2.0"
import jsonimport osimport shutilimport warningsfrom datetime import datetimefrom functools import cachefrom pathlib import Pathfrom typing import Callable, Dict, List, Tuple, Anyfrom uuid import uuid4
import gradio as grimport numpy as npimport requestsfrom filelock import FileLockfrom PIL.Image import Imageimport filetypeimport wavefrom mutagen.mp3 import MP3, EasyMP3import torchaudioimport subprocess
def setup(folder_path: str | Path | None = None) -> None: user_history = _UserHistory() user_history.folder_path = _resolve_folder_path(folder_path) user_history.initialized = True
def render() -> None: user_history = _UserHistory()
# initialize with default config if not user_history.initialized: print("Initializing user history with default config. Use `user_history.setup(...)` to customize folder_path.") setup()
# Render user history tab gr.Markdown( "## Your past generations\n\nLog in to keep a gallery of your previous generations. Your history will be saved" " and available on your next visit. Make sure to export your images from time to time as this gallery may be" " deleted in the future." )
if os.getenv("SYSTEM") == "spaces" and not os.path.exists("/data"): gr.Markdown( "**⚠️ Persistent storage is disabled, meaning your history will be lost if the Space gets restarted." " Only the Space owner can setup a Persistent Storage. If you are not the Space owner, consider" " duplicating this Space to set your own storage.⚠️**" )
with gr.Row(): gr.LoginButton(min_width=250) #gr.LogoutButton(min_width=250) refresh_button = gr.Button( "Refresh", icon="./assets/icon_refresh.png", ) export_button = gr.Button( "Export", icon="./assets/icon_download.png", ) delete_button = gr.Button( "Delete history", icon="./assets/icon_delete.png", )
# "Export zip" row (hidden by default) with gr.Row(): export_file = gr.File(file_count="single", file_types=[".zip"], label="Exported history", visible=False)
# "Config deletion" row (hidden by default) with gr.Row(): confirm_button = gr.Button("Confirm delete all history", variant="stop", visible=False) cancel_button = gr.Button("Cancel", visible=False)
# Gallery gallery = gr.Gallery( label="Past images", show_label=True, elem_id="gradio_user_history_gallery", object_fit="cover", columns=5, height=600, preview=False, show_share_button=False, show_download_button=True, ) gr.Markdown( "User history is powered by" " [Wauplin/gradio-user-history](https://huggingface.co/spaces/Wauplin/gradio-user-history). Integrate it to" " your own Space in just a few lines of code!" ) gallery.attach_load_event(_fetch_user_history, every=None)
# Interactions refresh_button.click(fn=_fetch_user_history, inputs=[], outputs=[gallery], queue=False) export_button.click(fn=_export_user_history, inputs=[], outputs=[export_file], queue=False)
# Taken from https://github.com/gradio-app/gradio/issues/3324#issuecomment-1446382045 delete_button.click( lambda: [gr.update(visible=True), gr.update(visible=True)], outputs=[confirm_button, cancel_button], queue=False, ) cancel_button.click( lambda: [gr.update(visible=False), gr.update(visible=False)], outputs=[confirm_button, cancel_button], queue=False, ) confirm_button.click(_delete_user_history).then( lambda: [gr.update(visible=False), gr.update(visible=False)], outputs=[confirm_button, cancel_button], queue=False, )
# Admin section (only shown locally or when logged in as Space owner) _admin_section()
def save_image( profile: gr.OAuthProfile | None, image: Image | np.ndarray | str | Path, label: str | None = None, metadata: Dict | None = None,): # Ignore images from logged out users if profile is None: return username = profile["preferred_username"]
# Ignore images if user history not used user_history = _UserHistory() if not user_history.initialized: warnings.warn( "User history is not set in Gradio demo. Saving image is ignored. You must use `user_history.render(...)`" " first." ) return
# Copy image to storage image_path = _copy_image(image, dst_folder=user_history._user_images_path(username))
# Save new image + metadata if metadata is None: metadata = {} if "datetime" not in metadata: metadata["datetime"] = str(datetime.now()) data = {"path": str(image_path), "label": label, "metadata": metadata} with user_history._user_lock(username): with user_history._user_jsonl_path(username).open("a") as f: f.write(json.dumps(data) + "\n") def save_file( profile: gr.OAuthProfile | None, image: Image | np.ndarray | str | Path | None = None, video: str | Path | None = None, audio: str | Path | None = None, document: str | Path | None = None, label: str | None = None, metadata: Dict | None = None,): # Ignore files from logged out users if profile is None: return username = profile["preferred_username"]
# Ignore files if user history not used user_history = _UserHistory() if not user_history.initialized: warnings.warn( "User history is not set in Gradio demo. Saving files is ignored. You must use `user_history.render(...)`" " first." ) return # Save new files + metadata if metadata is None: metadata = {} if "datetime" not in metadata: metadata["datetime"] = str(datetime.now()) # Copy image to storage image_path = None if image is not None: image_path = _copy_image(image, dst_folder=user_history._user_images_path(username)) image_path = _add_metadata(image_path, metadata)
# Copy video to storage if video is not None: video_path = _copy_file(video, dst_folder=user_history._user_file_path(username, "videos")) video_path = _add_metadata(video_path, metadata)
# Copy audio to storage if audio is not None: audio_path = _copy_file(audio, dst_folder=user_history._user_file_path(username, "audios")) audio_path = _add_metadata(audio_path, metadata) # Copy document to storage if document is not None: document_path = _copy_file(document, dst_folder=user_history._user_file_path(username, "documents")) document_path = _add_metadata(document_path, metadata)
# Save Json file data = {"image_path": str(image_path), "video_path": str(video_path), "audio_path": str(audio_path), "document_path": str(document_path), "label": label, "metadata": metadata} with user_history._user_lock(username): with user_history._user_jsonl_path(username).open("a") as f: f.write(json.dumps(data) + "\n")
############## Internals ##############
class _UserHistory(object): _instance = None initialized: bool = False folder_path: Path
def __new__(cls): # Using singleton pattern => we don't want to expose an object (more complex to use) but still want to keep # state between `render` and `save_image` calls. if cls._instance is None: cls._instance = super(_UserHistory, cls).__new__(cls) return cls._instance
def _user_path(self, username: str) -> Path: path = self.folder_path / username path.mkdir(parents=True, exist_ok=True) return path
def _user_lock(self, username: str) -> FileLock: """Ensure history is not corrupted if concurrent calls.""" return FileLock(self.folder_path / f"{username}.lock") # lock outside of folder => better when exporting ZIP
def _user_jsonl_path(self, username: str) -> Path: return self._user_path(username) / "history.jsonl"
def _user_images_path(self, username: str) -> Path: path = self._user_path(username) / "images" path.mkdir(parents=True, exist_ok=True) return path def _user_file_path(self, username: str, filetype: str = "images") -> Path: path = self._user_path(username) / filetype path.mkdir(parents=True, exist_ok=True) return path
def _fetch_user_history(profile: gr.OAuthProfile | None) -> List[Tuple[str, str]]: """Return saved history for that user, if it exists.""" # Cannot load history for logged out users if profile is None: return [] username = profile["preferred_username"]
user_history = _UserHistory() if not user_history.initialized: warnings.warn("User history is not set in Gradio demo. You must use `user_history.render(...)` first.") return []
with user_history._user_lock(username): # No file => no history saved yet jsonl_path = user_history._user_jsonl_path(username) if not jsonl_path.is_file(): return []
# Read history images = [] for line in jsonl_path.read_text().splitlines(): data = json.loads(line) images.append((data["path"], data["label"] or "")) return list(reversed(images))
def _export_user_history(profile: gr.OAuthProfile | None) -> Dict | None: """Zip all history for that user, if it exists and return it as a downloadable file.""" # Cannot load history for logged out users if profile is None: return None username = profile["preferred_username"]
user_history = _UserHistory() if not user_history.initialized: warnings.warn("User history is not set in Gradio demo. You must use `user_history.render(...)` first.") return None
# Zip history with user_history._user_lock(username): path = shutil.make_archive( str(_archives_path() / f"history_{username}"), "zip", user_history._user_path(username) )
return gr.update(visible=True, value=path)
def _delete_user_history(profile: gr.OAuthProfile | None) -> None: """Delete all history for that user.""" # Cannot load history for logged out users if profile is None: return username = profile["preferred_username"]
user_history = _UserHistory() if not user_history.initialized: warnings.warn("User history is not set in Gradio demo. You must use `user_history.render(...)` first.") return
with user_history._user_lock(username): shutil.rmtree(user_history._user_path(username))
##################### Internal helpers #####################
def _copy_image(image: Image | np.ndarray | str | Path, dst_folder: Path) -> Path: try: """Copy image to the images folder.""" # Already a path => copy it if isinstance(image, str): image = Path(image) if isinstance(image, Path): dst = dst_folder / f"{uuid4().hex}_{Path(image).name}" # keep file ext shutil.copyfile(image, dst) return dst
# Still a Python object => serialize it if isinstance(image, np.ndarray): image = Image.fromarray(image) if isinstance(image, Image): dst = dst_folder / f"{Path(file).name}_{uuid4().hex}.png" image.save(dst) return dst
raise ValueError(f"Unsupported image type: {type(image)}") except Exception as e: print(f"An error occurred: {e}") if not isinstance(dst, Path): dst = Path(image) return dst # Return the original file_location if an error occurs
def _copy_file(file: Any | np.ndarray | str | Path, dst_folder: Path) -> Path: try: """Copy file to the appropriate folder.""" # Already a path => copy it if isinstance(file, str): file = Path(file) if isinstance(file, Path): dst = dst_folder / f"{file.stem}_{uuid4().hex}{file.suffix}" # keep file ext shutil.copyfile(file, dst) return dst
# Still a Python object => serialize it if isinstance(file, np.ndarray): file = Image.fromarray(file) dst = dst_folder / f"{file.filename}_{uuid4().hex}{file.suffix}" file.save(dst) return dst
# try other file types kind = filetype.guess(file) if kind is not None: dst = dst_folder / f"{Path(file).stem}_{uuid4().hex}.{kind.extension}" shutil.copyfile(file, dst) return dst raise ValueError(f"Unsupported file type: {type(file)}")
except Exception as e: print(f"An error occurred: {e}") if not isinstance(dst, Path): dst = Path(file) return dst # Return the original file_location if an error occurs
def _add_metadata(file_location: Path, metadata: Dict[str, Any]) -> Path: try: file_type = file_location.suffix valid_file_types = [".wav", ".mp3", ".mp4", ".png"] if file_type not in valid_file_types: raise ValueError("Invalid file type. Valid file types are .wav, .mp3, .mp4, .png")
if file_type == ".wav": # Open and process .wav file with wave.open(file_location, 'rb') as wav_file: # Get the current metadata current_metadata = {key: value for key, value in wav_file.getparams()._asdict().items() if isinstance(value, (int, float))} # Update metadata current_metadata.update(metadata)
# Reopen the WAV file in write mode with wave.open(file_location, 'wb') as wav_output_file: # Set the new metadata wav_output_file.setparams(wav_file.getparams())
# Save the WAV file (overwriting the previous version) wav_output_file.close() elif file_type == ".mp3": # Open and process .mp3 file audio = EasyMP3(file_location)
# Add metadata to the file for key, value in metadata.items(): audio[key] = value
# Save the MP3 file (overwriting the previous version) audio.save() elif file_type == ".mp4": # Open and process .mp4 file # Add metadata to the file wav_file_location = file_location.with_suffix(".wav") wave_exists = wav_file_location.exists() if not wave_exists: # Use torchaudio to create the WAV file if it doesn't exist audio, sample_rate = torchaudio.load(file_location, normalize=True) torchaudio.save(wav_file_location, audio, sample_rate, format='wav')
# Use ffmpeg to add metadata to the video file metadata_args = [f"{key}={value}" for key, value in metadata.items()] ffmpeg_metadata = ":".join(metadata_args) ffmpeg_cmd = f'ffmpeg -i "{file_location}" -i "{wav_file_location}" -map 0:v:0 -map 1:a:0 -c:v copy -c:a aac -metadata "{ffmpeg_metadata}" "{file_location}"' subprocess.run(ffmpeg_cmd, shell=True, check=True)
# Remove temporary WAV file if not wave_exists: wav_file_location.unlink() elif file_type == ".png": # Open and process .png file image = Image.open(file_location) exif_data = image.info.get("exif", {}) exif_data.update(metadata) # Add metadata to the file image.save(file_location, exif=exif_data)
return file_location # Return the path to the modified file
except Exception as e: print(f"An error occurred: {e}") return file_location # Return the original file_location if an error occurs def _resolve_folder_path(folder_path: str | Path | None) -> Path: if folder_path is not None: return Path(folder_path).expanduser().resolve()
if os.getenv("SYSTEM") == "spaces" and os.path.exists("/data"): # Persistent storage is enabled! return Path("/data") / "_user_history"
# Not in a Space or Persistent storage not enabled => local folder return Path("_user_history").resolve()
def _archives_path() -> Path: # Doesn't have to be on persistent storage as it's only used for download path = Path(__file__).parent / "_user_history_exports" path.mkdir(parents=True, exist_ok=True) return path
################## Admin section ##################
def _admin_section() -> None: title = gr.Markdown() title.attach_load_event(_display_if_admin(), every=None)
def _display_if_admin() -> Callable: def _inner(profile: gr.OAuthProfile | None) -> str: if profile is None: return "" if profile["preferred_username"] in _fetch_admins(): return _admin_content() return ""
return _inner
def _admin_content() -> str: return f"""## Admin section
Running on **{os.getenv("SYSTEM", "local")}** (id: {os.getenv("SPACE_ID")}). {_get_msg_is_persistent_storage_enabled()}
Admins: {', '.join(_fetch_admins())}
{_get_nb_users()} user(s), {_get_nb_images()} image(s)
### Configuration
History folder: *{_UserHistory().folder_path}*
Exports folder: *{_archives_path()}*
### Disk usage
{_disk_space_warning_message()}"""
def _get_nb_users() -> int: user_history = _UserHistory() if not user_history.initialized: return 0 if user_history.folder_path is not None and user_history.folder_path.exists(): return len([path for path in user_history.folder_path.iterdir() if path.is_dir()]) return 0
def _get_nb_images() -> int: user_history = _UserHistory() if not user_history.initialized: return 0 if user_history.folder_path is not None and user_history.folder_path.exists(): return len([path for path in user_history.folder_path.glob("*/images/*")]) return 0
def _get_msg_is_persistent_storage_enabled() -> str: if os.getenv("SYSTEM") == "spaces": if os.path.exists("/data"): return "Persistent storage is enabled." else: return ( "Persistent storage is not enabled. This means that user histories will be deleted when the Space is" " restarted. Consider adding a Persistent Storage in your Space settings." ) return ""
def _disk_space_warning_message() -> str: user_history = _UserHistory() if not user_history.initialized: return ""
message = "" if user_history.folder_path is not None: total, used, _ = _get_disk_usage(user_history.folder_path) message += f"History folder: **{used / 1e9 :.0f}/{total / 1e9 :.0f}GB** used ({100*used/total :.0f}%)."
total, used, _ = _get_disk_usage(_archives_path()) message += f"\n\nExports folder: **{used / 1e9 :.0f}/{total / 1e9 :.0f}GB** used ({100*used/total :.0f}%)."
return f"{message.strip()}"
def _get_disk_usage(path: Path) -> Tuple[int, int, int]: for path in [path] + list(path.parents): # first check target_dir, then each parents one by one try: return shutil.disk_usage(path) except OSError: # if doesn't exist or can't read => fail silently and try parent one pass return 0, 0, 0
@cachedef _fetch_admins() -> List[str]: # Running locally => fake user is admin if os.getenv("SYSTEM") != "spaces": return ["FakeGradioUser"]
# Running in Space but no space_id => ??? space_id = os.getenv("SPACE_ID") if space_id is None: return ["Unknown"]
# Running in Space => try to fetch organization members # Otherwise, it's not an organization => namespace is the user namespace = space_id.split("/")[0] response = requests.get(f"https://huggingface.co/api/organizations/{namespace}/members") if response.status_code == 200: return sorted((member["user"] for member in response.json()), key=lambda x: x.lower()) return [namespace]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123# modules/version_info.py
from audiocraft import __version__ as audiocraft_versionimport subprocessimport osimport sysimport gcimport gradio as gr
git = os.environ.get('GIT', "git")
def commit_hash(): try: return subprocess.check_output([git, "rev-parse", "HEAD"], shell=False, encoding='utf8').strip() except Exception: return "<none>"
def get_xformers_version(): try: import xformers return xformers.__version__ except Exception: return "<none>"def get_transformers_version(): try: import transformers return transformers.__version__ except Exception: return "<none>"
def get_accelerate_version(): try: import accelerate return accelerate.__version__ except Exception: return "<none>"def get_safetensors_version(): try: import safetensors return safetensors.__version__ except Exception: return "<none>"def get_diffusers_version(): try: import diffusers return diffusers.__version__ except Exception: return "<none>"
def get_torch_info(): from torch import __version__ as torch_version_, version, cuda, backends device_type = initialize_cuda() if device_type == "cuda": try: info = [torch_version_, f"CUDA Version:{version.cuda}", f"Available:{cuda.is_available()}", f"flash attention enabled: {backends.cuda.flash_sdp_enabled()}", f"Capabilities: {cuda.get_device_capability(0)}", f"Device Name: {cuda.get_device_name(0)}", f"Device Count: {cuda.device_count()}"] del torch_version_, version, cuda, backends return info except Exception: del torch_version_, version, cuda, backends return "<none>" else: return "Not Recognized"
def release_torch_resources(): from torch import cuda # Clear the CUDA cache cuda.empty_cache() cuda.ipc_collect() # Delete any objects that are using GPU memory #for obj in gc.get_objects(): # if is_tensor(obj) or (hasattr(obj, 'data') and is_tensor(obj.data)): # del obj # Run garbage collection del cuda gc.collect()
def initialize_cuda(): from torch import cuda, version if cuda.is_available(): device = cuda.device("cuda") print(f"CUDA is available. Using device: {cuda.get_device_name(0)} with CUDA version: {version.cuda}") result = "cuda" else: print("CUDA is not available. Using CPU.") result = "cpu" return result
def versions_html(): from torch import __version__ as torch_version_ python_version = ".".join([str(x) for x in sys.version_info[0:3]]) commit = commit_hash()
# Define the Toggle Dark Mode link with JavaScript toggle_dark_link = ''' <a href="#" onclick="document.body.classList.toggle('dark'); return false;" style="cursor: pointer; text-decoration: underline;"> Toggle Dark Mode </a> '''
v_html = f""" version: <a href="https://huggingface.co/spaces/Surn/UnlimitedMusicGen/commit/{"huggingface" if commit == "<none>" else commit}" target="_blank">{"huggingface" if commit == "<none>" else commit}</a>  •  autocraft: {audiocraft_version}  •  python: <span title="{sys.version}">{python_version}</span>  •  torch: {torch_version_}  •  xformers: {get_xformers_version()}  •  transformers: {get_transformers_version()}  •  safetensors: {get_safetensors_version()}  •  gradio: {gr.__version__}  •  {toggle_dark_link} <br> Full GPU Info:{get_torch_info()} """ del torch_version_ return v_html1pip>=23.31pip>=24.0
12345678910111213141516171819202122# please make sure you have already a pytorch install that is cuda enabled!aveinopsflashy>=0.0.1hydra-core>=1.1hydra_colorlogjuliusnum2wordsnumpysentencepiecespacy==3.5.2torch==2.0.1torchaudio==2.0.2soundfilehuggingface_hubtqdmtransformers>=4.31.0xformers>=0.0.22demucslibrosagradio==3.38.00pillow1234567891011121314151617181920212223242526272829303132333435363738# please make sure you have already a pytorch install that is cuda enabled!av==11.0.0einopsflashy>=0.0.1hydra-core>=1.1hydra_colorlogtorch==2.6.0 --extra-index-url https://download.pytorch.org/whl/cu124torchaudio>=2.0.0,<2.6.2 --extra-index-url https://download.pytorch.org/whl/cu124soundfilehuggingface_hubtqdmtransformers>=4.48.0 # need Encodec there.xformers>=0.0.23 --index-url https://download.pytorch.org/whl/cu124demucslibrosasoundfilegradio==5.23.3gradio[oauth]pillowtorchmetricsencodecprotobuf>=3.20.1filetypewavemutagenfastapi>=0.88.0pydantictypertorchvision>=0.21.0 --extra-index-url https://download.pytorch.org/whl/cu124#torchtextpesqpystoijuliusspacy==3.7.6sentencepiecenum2wordsnumpy<1.26.4matplotlib
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215.interface-wrapper { max-width: 1024px; margin: 0 auto;}
.centered { margin: 0 auto; display: block; text-align:center;}
.solid { opacity: 1.0 !important; height: auto !important;}
.intro { font-size: 1.2em !important; font-weight: bold; text-align: center; background-color: rgba(242, 218, 163, 0.62);}
.dark .gradio-container.gradio-container-5-23-3 .contain .intro .prose { background-color: rgba(41, 18, 5, 0.38) !important;}.toast-body.info { background-color: rgba(242, 218, 163, 0.75);}.dark .toast-body.info { background-color: rgba(128, 128, 128, 0.75);}
.small { font-size: smaller !important; text-align: center;}
.imgcontainer img { object-fit: contain !important;}
#examples { font-weight: bolder;}
--background-fill-primary: #FBCE50 !important;#col-container { max-width: 1024px; margin-left: auto; margin-right: auto;}
a { text-decoration-line: underline; font-weight: 600;}
#btn-generate { background-image: linear-gradient(to right bottom, rgb(157, 255, 157), rgb(229, 255, 235)); color: var(--primary-800);}
#btn-generate:hover { background-image: linear-gradient(to right bottom, rgb(229, 255, 229), rgb(255, 255, 255)); }
#btn-generate:active { background-image: linear-gradient(to right bottom, rgb(229, 255, 235), rgb(157, 255, 157)); }
#versions { margin-top: 1em; width: 100%; text-align: center;}
.small-btn { max-width: 75px;}
#gallery .thumbnails, #lora_gallery .thumbnails { flex-direction: column !important; display: inline-flex !important; flex-wrap: wrap !important; position: relative !important;}
#gallery caption.caption, #lora_gallery caption.caption { flex-direction: row !important; display: inline-flex !important; flex-wrap: wrap; white-space: unset !important;}
#gallery .image-button img.with-caption, #lora_gallery .image-button img.with-caption { object-fit: cover !important; object-position: center !important;}
#gallery button.preview, #lora_gallery button.preview { position: relative !important;}
.gradio-container::before { content: ' '; display: block; position: absolute; left: 0; top: 0; width: 100%; height: 100%; opacity: 0.5; background-image: url('gradio_api/file=./assets/Vermilion-Musical-Notes-Typography-No-Background.svg'); background-repeat: no-repeat; background-position: 50% 25%; /*background-color: rgba(0,0,0,0.5);*/ background-size: 45vh; overflow: hidden;}
.gradio-container::after { content: ''; position: absolute; top: 0; left: -60%; /* Start off-screen */ width: 30%; height: 100%; background: linear-gradient( 120deg, rgba(255, 255, 255, 0) 10%, rgba(255, 255, 255, 0.60) 50%, rgba(255, 255, 255, 0) 90% ); animation: shine 30s infinite;}
#component-0, #component-1 { opacity: 0.9;}
#excluded_colors { width: 95%; margin: 0 auto; font-size: smaller;}
@media only screen and (min-width: 1920px) { .gradio-container, .gradio-container::before { max-width: 1920px !important; }}
.sidebar .toggle-button::before { content: 'Sketch Pad'; font-weight: bold; transform: rotate(180deg); margin-right: -120px; width: 120px; background-color: rgba(242, 218, 163, 0.62);}.dark .sidebar .toggle-button::before { background-color: rgba(41, 18, 5, 0.38) !important;} .sidebar.open .toggle-button::before { content: '';}
#sketchpd, #filters, #image_gen, #accordian_3d { outline-color: #bbf7d0; outline-style:solid; outline-width: 1px; outline-offset: 1px; padding: 2px; border-radius:6px;}.outline-important { outline-color: var(--accordion-text-color); outline-style: solid; outline-width: 2px; outline-offset: 2px; padding: 2px; border-radius: 6px;}.selected.svelte-1tcem6n.svelte-1tcem6n { font-size: large; font-weight: bold; color: var(--body-text-color);}.tab-wrapper.svelte-1tcem6n.svelte-1tcem6n { height: var(--size-12); padding-bottom: var(--size-1); text-align: center; background-blend-mode: multiply; border-radius: var(--block-radius); background-color: var(--block-background-fill);
outline-color: var(--accordion-text-color); outline-style: solid; outline-width: 2px; outline-offset: 2px; padding: 2px; border-radius: 6px;}
@keyframes shine { 0% { left: -100%; }
20% { left: 100%; }
100% { left: 125%; }}.gitattributes+2
CHANGELOG.md+61-12
README.md+48-7
app.py+270-202
assets/KuritaSurnLogox64.pngAdded

assets/Vermilion-Musical-Notes-Typography-No-Background.svgAdded
assets/a_duck_quacking_as_birds_chirp_and_a_pigeon_cooing.mp3Added
assets/icon_delete.pngAdded
assets/icon_download.pngAdded
assets/icon_refresh.pngAdded
assets/logo_animation_256.gifAdded

Large File Pointer Details
- SHA256: 84aa8c95f88f4c9d110dc87c344ec92786e8b5c464ac8141a9c3b12bedf2ed71
- Pointer size: 131 Bytes
- Size of remote file: 140 kB
assets/screenshot.pngAdded

Large File Pointer Details
- SHA256: 89abfaffefc18124ffe8f0775eb4dcdbc589bb97befc76dd3f3fc48992991e2e
- Pointer size: 131 Bytes
- Size of remote file: 388 kB
assets/sirens_and_a_humming_engine_approach_and_pass.mp3Added
audiocraft/__init__.py+1-1
audiocraft/data/__init__.py+1-1
audiocraft/data/audio.py+86-1
audiocraft/data/audio_dataset.py+93-31
audiocraft/data/audio_utils.py+9-6
audiocraft/data/info_audio_dataset.pyAdded+110
audiocraft/data/zip.py+8-6
audiocraft/environment.pyAdded+176
audiocraft/models/__init__.py+7
audiocraft/models/builders.py+89-49
audiocraft/models/encodec.py+267-63
audiocraft/models/lm.py+35-29
audiocraft/models/loaders.py+146-5
audiocraft/models/musicgen.py+108-34
audiocraft/models/unet.pyAdded+214
audiocraft/modules/__init__.py+1
audiocraft/modules/chroma.pyAdded+66
audiocraft/modules/codebooks_patterns.py+17-12
audiocraft/modules/conditioners.py+724-298
audiocraft/modules/conv.py+1-1
audiocraft/modules/diffusion_schedule.pyAdded+272
audiocraft/modules/rope.py+20-19
audiocraft/modules/transformer.py+36-38
audiocraft/quantization/core_vq.py+8-3
audiocraft/utils/cache.pyAdded+324
audiocraft/utils/cluster.pyAdded+75
audiocraft/utils/export.py+50-27
audiocraft/utils/export_legacy.pyAdded+56
audiocraft/utils/extend.py+5-4
audiocraft/utils/utils.py+68-2
modules/file_utils.pyAdded+91
modules/gradio.pyAdded+272
modules/user_history.pyAdded+598
modules/version_info.pyAdded+123
pre-requirements.txt+1-1
requirements.txt+28-12
style_20250331.cssAdded+215