Feature Extraction
Transformers
Safetensors
moss-audio-tokenizer
audio
audio-tokenizer
neural-codec
moss-tts-family
MOSS Audio Tokenizer
speech-tokenizer
trust-remote-code
custom_code
Instructions to use OpenMOSS-Team/MOSS-Audio-Tokenizer-v2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use OpenMOSS-Team/MOSS-Audio-Tokenizer-v2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="OpenMOSS-Team/MOSS-Audio-Tokenizer-v2", trust_remote_code=True, device_map="auto")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("OpenMOSS-Team/MOSS-Audio-Tokenizer-v2", trust_remote_code=True, dtype="auto", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload MOSS Audio Tokenizer v2
Browse files- .gitattributes +48 -0
- .gitignore +1 -2
- README.md +151 -119
- configuration_moss_audio_tokenizer.py +3 -2
- modeling_moss_audio_tokenizer.py +3 -43
.gitattributes
CHANGED
|
@@ -1 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.db* filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.ark* filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
**/*ckpt*data* filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
**/*ckpt*.meta filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
**/*ckpt*.index filter=lfs diff=lfs merge=lfs -text
|
| 33 |
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*.gguf* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.ggml filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
*.llamafile* filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
*.pt2 filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 44 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
| 45 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 46 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 47 |
+
*.wav filter=lfs diff=lfs merge=lfs -text
|
| 48 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 49 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
CHANGED
|
@@ -1,3 +1,2 @@
|
|
| 1 |
dev/*
|
| 2 |
-
*.
|
| 3 |
-
__pycache__/
|
|
|
|
| 1 |
dev/*
|
| 2 |
+
demo/demo_rec*.wav
|
|
|
README.md
CHANGED
|
@@ -1,119 +1,151 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: apache-2.0
|
| 3 |
-
library_name: transformers
|
| 4 |
-
tags:
|
| 5 |
-
- audio
|
| 6 |
-
- audio-tokenizer
|
| 7 |
-
- neural-codec
|
| 8 |
-
- moss-tts-family
|
| 9 |
-
-
|
| 10 |
-
--
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
model.
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
##
|
| 82 |
-
|
| 83 |
-
`
|
| 84 |
-
the `
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
library_name: transformers
|
| 4 |
+
tags:
|
| 5 |
+
- audio
|
| 6 |
+
- audio-tokenizer
|
| 7 |
+
- neural-codec
|
| 8 |
+
- moss-tts-family
|
| 9 |
+
- MOSS Audio Tokenizer
|
| 10 |
+
- speech-tokenizer
|
| 11 |
+
- trust-remote-code
|
| 12 |
+
- arxiv:2602.10934
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# Moss-Audio-Tokenizer-v2
|
| 16 |
+
|
| 17 |
+
This is the code for the 48khz stereo version of MOSS-Audio-Tokenizer presented in [MOSS-Audio-Tokenizer: Scaling Audio Tokenizers for Future Audio Foundation Models](https://arxiv.org/abs/2602.10934).
|
| 18 |
+
|
| 19 |
+
**MOSS-Audio-Tokenizer-v2** is a unified discrete audio tokenizer based on the **Cat** (**C**ausal **A**udio **T**okenizer with **T**ransformer) architecture. Scaling to 2 billion parameters, it functions as a unified discrete interface, delivering both lossless-quality reconstruction and high-level semantic alignment.
|
| 20 |
+
|
| 21 |
+
**Key Features:**
|
| 22 |
+
|
| 23 |
+
* **Extreme Compression & Variable Bitrate**: It compresses 48kHz stereo audio into a remarkably low frame rate of 12.5Hz. Utilizing a 32-layer Residual Vector Quantization stack, it supports high-fidelity reconstruction across a wide range of bitrates.
|
| 24 |
+
* **Pure Transformer Architecture**: The model features a "CNN-free" homogeneous architecture built entirely from Causal Transformer blocks. With 2B combined parameters (Encoder + Decoder), it ensures exceptional scalability and supports low-latency streaming inference.
|
| 25 |
+
* **Large-Scale General Audio Training**: Trained on 3 million hours of diverse audio data, the model excels at encoding and reconstructing all audio domains, including speech, sound effects, and music.
|
| 26 |
+
* **Unified Semantic-Acoustic Representation**: While achieving state-of-the-art reconstruction quality, Cat produces discrete tokens that are "semantic-rich," making them ideal for downstream tasks like speech understanding (ASR) and generation (TTS).
|
| 27 |
+
* **Fully Trained From Scratch**: Cat does not rely on any pretrained encoders (such as HuBERT or Whisper) or distillation from teacher models. All representations are learned autonomously from raw data.
|
| 28 |
+
* **End-to-End Joint Optimization**: All components—including the encoder, quantizer, decoder, discriminator, and a decoder-only LLM for semantic alignment—are optimized jointly in a single unified training pipeline.
|
| 29 |
+
|
| 30 |
+
**Summary:**
|
| 31 |
+
By combining a simple, scalable architecture with massive-scale data, the Cat architecture overcomes the bottlenecks of traditional audio tokenizers. It provides a robust, high-fidelity, and semantically grounded interface for the next generation of native audio foundation models.
|
| 32 |
+
|
| 33 |
+
This repository contains a lightweight remote-code implementation that mirrors the current 🤗 Transformers
|
| 34 |
+
`transformers.models.moss_audio_tokenizer` module. It is hosted as a Hugging Face Hub model repository and should be
|
| 35 |
+
loaded with `trust_remote_code=True`.
|
| 36 |
+
|
| 37 |
+
<br>
|
| 38 |
+
<p align="center">
|
| 39 |
+
<img src="images/arch.png" width="95%"> <br>
|
| 40 |
+
Architecture of MossAudioTokenizer
|
| 41 |
+
</p>
|
| 42 |
+
<br>
|
| 43 |
+
|
| 44 |
+
## Usage
|
| 45 |
+
|
| 46 |
+
### Quickstart
|
| 47 |
+
|
| 48 |
+
```python
|
| 49 |
+
import torch
|
| 50 |
+
from transformers import AutoModel
|
| 51 |
+
import torchaudio
|
| 52 |
+
|
| 53 |
+
repo_id = "OpenMOSS-Team/MOSS-Audio-Tokenizer-v2"
|
| 54 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 55 |
+
model = AutoModel.from_pretrained(repo_id, trust_remote_code=True).eval().to(device)
|
| 56 |
+
|
| 57 |
+
audio_path = "demo/demo_gt.wav" # replace with your own 48 kHz stereo audio path if needed
|
| 58 |
+
wav, sr = torchaudio.load(audio_path)
|
| 59 |
+
if sr != model.sampling_rate:
|
| 60 |
+
wav = torchaudio.functional.resample(wav, sr, model.sampling_rate)
|
| 61 |
+
if wav.shape[0] == 1:
|
| 62 |
+
wav = wav.repeat(model.config.number_channels, 1)
|
| 63 |
+
else:
|
| 64 |
+
wav = wav[: model.config.number_channels]
|
| 65 |
+
wav = wav.unsqueeze(0).to(device)
|
| 66 |
+
enc = model.encode(wav, return_dict=True)
|
| 67 |
+
print(f"enc.audio_codes.shape: {enc.audio_codes.shape}")
|
| 68 |
+
dec = model.decode(enc.audio_codes, return_dict=True)
|
| 69 |
+
print(f"dec.audio.shape: {dec.audio.shape}")
|
| 70 |
+
wav = dec.audio.squeeze(0)
|
| 71 |
+
torchaudio.save("demo/demo_rec.wav", wav.cpu(), sample_rate=model.sampling_rate)
|
| 72 |
+
|
| 73 |
+
# Decode using only the first 8 layers of the RVQ
|
| 74 |
+
dec_rvq8 = model.decode(enc.audio_codes[:8], return_dict=True)
|
| 75 |
+
wav_rvq8 = dec_rvq8.audio.squeeze(0)
|
| 76 |
+
torchaudio.save("demo/demo_rec_rvq8.wav", wav_rvq8.cpu(), sample_rate=model.sampling_rate)
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
For production use with `trust_remote_code=True`, pin `revision` to a reviewed commit hash.
|
| 80 |
+
|
| 81 |
+
### Attention Backend And Compute Dtype
|
| 82 |
+
|
| 83 |
+
`config.attention_implementation` controls whether transformer layers prefer `sdpa` or `flash_attention_2`.
|
| 84 |
+
`config.compute_dtype` controls the non-quantizer autocast dtype and supports `fp32`, `bf16`.
|
| 85 |
+
|
| 86 |
+
```python
|
| 87 |
+
model.set_attention_implementation("flash_attention_2")
|
| 88 |
+
model.set_compute_dtype("bf16")
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
The quantizer always runs in fp32.
|
| 92 |
+
|
| 93 |
+
### Streaming
|
| 94 |
+
|
| 95 |
+
`MossAudioTokenizerModel.encode`, `decode`, `batch_encode`, and `batch_decode` all support streaming through a
|
| 96 |
+
`chunk_duration` argument.
|
| 97 |
+
|
| 98 |
+
- `chunk_duration` is expressed in seconds.
|
| 99 |
+
- `chunk_duration * MossAudioTokenizerConfig.sampling_rate` must be divisible by `MossAudioTokenizerConfig.downsample_rate`.
|
| 100 |
+
- Streaming batch inference is supported.
|
| 101 |
+
- The public waveform interface expects stereo inputs shaped `(2, T)` or batched stereo inputs shaped `(B, 2, T)`.
|
| 102 |
+
|
| 103 |
+
```python
|
| 104 |
+
import torch
|
| 105 |
+
from transformers import AutoModel
|
| 106 |
+
|
| 107 |
+
repo_id = "OpenMOSS-Team/MOSS-Audio-Tokenizer-v2"
|
| 108 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 109 |
+
model = AutoModel.from_pretrained(repo_id, trust_remote_code=True).eval().to(device)
|
| 110 |
+
audio = torch.randn(2, 48000 * 6).to(device) # dummy stereo waveform
|
| 111 |
+
|
| 112 |
+
# 6.0s @ 48kHz = 288000 samples, divisible by downsample_rate=3840
|
| 113 |
+
enc = model.encode(audio.unsqueeze(0), return_dict=True, chunk_duration=0.08)
|
| 114 |
+
dec = model.decode(enc.audio_codes, return_dict=True, chunk_duration=0.08)
|
| 115 |
+
|
| 116 |
+
batch_enc = model.batch_encode([audio, audio[:, : 48000 * 3]], chunk_duration=0.08)
|
| 117 |
+
codes_list = [
|
| 118 |
+
batch_enc.audio_codes[:, i, : batch_enc.audio_codes_lengths[i]]
|
| 119 |
+
for i in range(batch_enc.audio_codes.shape[1])
|
| 120 |
+
]
|
| 121 |
+
batch_dec = model.batch_decode(codes_list, chunk_duration=0.08)
|
| 122 |
+
```
|
| 123 |
+
|
| 124 |
+
## Repository layout
|
| 125 |
+
|
| 126 |
+
- `configuration_moss_audio_tokenizer.py`
|
| 127 |
+
- `modeling_moss_audio_tokenizer.py`
|
| 128 |
+
- `__init__.py`
|
| 129 |
+
- `config.json`
|
| 130 |
+
- `model.safetensors.index.json`
|
| 131 |
+
- sharded model weights: `model-00001-of-00003.safetensors`, `model-00002-of-00003.safetensors`,
|
| 132 |
+
`model-00003-of-00003.safetensors`
|
| 133 |
+
- `demo/demo_gt.wav`
|
| 134 |
+
|
| 135 |
+
## Citation
|
| 136 |
+
If you use this code or result in your paper, please cite our work as:
|
| 137 |
+
```tex
|
| 138 |
+
@misc{gong2026mossaudiotokenizerscaling,
|
| 139 |
+
title={MOSS-Audio-Tokenizer: Scaling Audio Tokenizers for Future Audio Foundation Models},
|
| 140 |
+
author={Yitian Gong and Kuangwei Chen and Zhaoye Fei and Xiaogui Yang and Ke Chen and Yang Wang and Kexin Huang and Mingshu Chen and Ruixiao Li and Qingyuan Cheng and Shimin Li and Xipeng Qiu},
|
| 141 |
+
year={2026},
|
| 142 |
+
eprint={2602.10934},
|
| 143 |
+
archivePrefix={arXiv},
|
| 144 |
+
primaryClass={cs.SD},
|
| 145 |
+
url={https://arxiv.org/abs/2602.10934}
|
| 146 |
+
}
|
| 147 |
+
```
|
| 148 |
+
|
| 149 |
+
## License
|
| 150 |
+
MOSS-Audio-Tokenizer-v2 is released under the Apache 2.0 license. See `LICENSE` for the full license text.
|
| 151 |
+
|
configuration_moss_audio_tokenizer.py
CHANGED
|
@@ -31,7 +31,8 @@ class MossAudioTokenizerConfig(PreTrainedConfig):
|
|
| 31 |
This is the configuration class to store the configuration of a [`MossAudioTokenizerModel`]. It is used to instantiate a
|
| 32 |
MossAudioTokenizer model according to the specified arguments, defining the model architecture.
|
| 33 |
|
| 34 |
-
Instantiating a configuration with the defaults will yield a
|
|
|
|
| 35 |
|
| 36 |
Configuration objects inherit from [`PreTrainedConfig`] and can be used to control the model outputs. Read the
|
| 37 |
documentation from [`PreTrainedConfig`] for more information.
|
|
@@ -57,7 +58,7 @@ class MossAudioTokenizerConfig(PreTrainedConfig):
|
|
| 57 |
Attention implementation to prefer for transformer layers. Supported values are `"sdpa"` and
|
| 58 |
`"flash_attention_2"`.
|
| 59 |
compute_dtype (`str`, *optional*, defaults to `"fp32"`):
|
| 60 |
-
Inference compute dtype for non-quantizer modules. Supported values are `"fp32"`, `"bf16"`
|
| 61 |
quantizer_type (`str`, *optional*, defaults to `"rlfq"`):
|
| 62 |
Quantizer type. Options include `"rvq"`, `"spec_rvq"`, `"rlfq"`, `"random_prefix_rlfq"`.
|
| 63 |
quantizer_kwargs (`dict`, *optional*):
|
|
|
|
| 31 |
This is the configuration class to store the configuration of a [`MossAudioTokenizerModel`]. It is used to instantiate a
|
| 32 |
MossAudioTokenizer model according to the specified arguments, defining the model architecture.
|
| 33 |
|
| 34 |
+
Instantiating a configuration with the defaults will yield a similar configuration to that of the
|
| 35 |
+
[VoiceAgentGroup/moss_audio_tokenizer](https://huggingface.co/VoiceAgentGroup/moss_audio_tokenizer) architecture.
|
| 36 |
|
| 37 |
Configuration objects inherit from [`PreTrainedConfig`] and can be used to control the model outputs. Read the
|
| 38 |
documentation from [`PreTrainedConfig`] for more information.
|
|
|
|
| 58 |
Attention implementation to prefer for transformer layers. Supported values are `"sdpa"` and
|
| 59 |
`"flash_attention_2"`.
|
| 60 |
compute_dtype (`str`, *optional*, defaults to `"fp32"`):
|
| 61 |
+
Inference compute dtype for non-quantizer modules. Supported values are `"fp32"`, `"bf16"`.
|
| 62 |
quantizer_type (`str`, *optional*, defaults to `"rlfq"`):
|
| 63 |
Quantizer type. Options include `"rvq"`, `"spec_rvq"`, `"rlfq"`, `"random_prefix_rlfq"`.
|
| 64 |
quantizer_kwargs (`dict`, *optional*):
|
modeling_moss_audio_tokenizer.py
CHANGED
|
@@ -61,7 +61,7 @@ except ImportError:
|
|
| 61 |
|
| 62 |
|
| 63 |
SUPPORTED_ATTENTION_IMPLEMENTATIONS = {"sdpa", "flash_attention_2"}
|
| 64 |
-
SUPPORTED_COMPUTE_DTYPES = {"fp32": None, "bf16": torch.bfloat16
|
| 65 |
|
| 66 |
|
| 67 |
def resolve_compute_dtype(compute_dtype: str) -> torch.dtype | None:
|
|
@@ -1361,7 +1361,7 @@ class MossAudioTokenizerPatchedPretransform(nn.Module):
|
|
| 1361 |
x = x.reshape(b, d, -1, h).permute(0, 1, 3, 2).reshape(b, d * h, -1)
|
| 1362 |
# We pad the input waveform to a multiple of `downsample_rate` before applying the encoder.
|
| 1363 |
# Use a ceil division to match that padding and avoid dropping the last (partially padded) frame.
|
| 1364 |
-
output_lengths =
|
| 1365 |
return x, output_lengths
|
| 1366 |
|
| 1367 |
def decode(self, x, input_lengths):
|
|
@@ -2469,7 +2469,7 @@ class MossAudioTokenizerModel(MossAudioTokenizerPreTrainedModel):
|
|
| 2469 |
>>> import torch
|
| 2470 |
>>> from transformers import MossAudioTokenizerModel
|
| 2471 |
|
| 2472 |
-
>>> model = MossAudioTokenizerModel.from_pretrained("
|
| 2473 |
|
| 2474 |
>>> # Create dummy audio input
|
| 2475 |
>>> audio = torch.randn(1, 1, 24000) # 1 second of audio at 24kHz
|
|
@@ -2485,38 +2485,10 @@ class MossAudioTokenizerModel(MossAudioTokenizerPreTrainedModel):
|
|
| 2485 |
output_audio_codes_lengths: torch.Tensor | None = None
|
| 2486 |
output_audio: torch.Tensor | None = None
|
| 2487 |
output_audio_lengths: torch.Tensor | None = None
|
| 2488 |
-
input_audio_lengths: torch.Tensor | None = None
|
| 2489 |
decoded_from_encoded_codes = False
|
| 2490 |
|
| 2491 |
# Encode if input_values provided
|
| 2492 |
if input_values is not None:
|
| 2493 |
-
if padding_mask is not None:
|
| 2494 |
-
if padding_mask.dim() == 1:
|
| 2495 |
-
input_audio_lengths = padding_mask.sum().view(1).long()
|
| 2496 |
-
elif padding_mask.dim() == 2:
|
| 2497 |
-
input_audio_lengths = padding_mask.sum(dim=-1).long()
|
| 2498 |
-
elif input_values.dim() == 1:
|
| 2499 |
-
input_audio_lengths = torch.full(
|
| 2500 |
-
(1,),
|
| 2501 |
-
input_values.shape[-1],
|
| 2502 |
-
device=input_values.device,
|
| 2503 |
-
dtype=torch.long,
|
| 2504 |
-
)
|
| 2505 |
-
elif input_values.dim() == 2:
|
| 2506 |
-
batch_size = input_values.shape[0] if self.number_channels == 1 else 1
|
| 2507 |
-
input_audio_lengths = torch.full(
|
| 2508 |
-
(batch_size,),
|
| 2509 |
-
input_values.shape[-1],
|
| 2510 |
-
device=input_values.device,
|
| 2511 |
-
dtype=torch.long,
|
| 2512 |
-
)
|
| 2513 |
-
elif input_values.dim() == 3:
|
| 2514 |
-
input_audio_lengths = torch.full(
|
| 2515 |
-
(input_values.shape[0],),
|
| 2516 |
-
input_values.shape[-1],
|
| 2517 |
-
device=input_values.device,
|
| 2518 |
-
dtype=torch.long,
|
| 2519 |
-
)
|
| 2520 |
encoder_output = self.encode(input_values, padding_mask, num_quantizers, return_dict=True)
|
| 2521 |
encoder_output = cast(MossAudioTokenizerEncoderOutput, encoder_output)
|
| 2522 |
output_audio_codes = encoder_output.audio_codes
|
|
@@ -2542,18 +2514,6 @@ class MossAudioTokenizerModel(MossAudioTokenizerPreTrainedModel):
|
|
| 2542 |
decoder_output = cast(MossAudioTokenizerDecoderOutput, decoder_output)
|
| 2543 |
output_audio = decoder_output.audio
|
| 2544 |
output_audio_lengths = decoder_output.audio_lengths
|
| 2545 |
-
if decoded_from_encoded_codes and output_audio is not None and input_audio_lengths is not None:
|
| 2546 |
-
input_audio_lengths = input_audio_lengths.to(device=output_audio.device, dtype=torch.long)
|
| 2547 |
-
input_audio_lengths = input_audio_lengths.clamp(min=0, max=output_audio.shape[-1])
|
| 2548 |
-
target_length = int(input_audio_lengths.max().item()) if input_audio_lengths.numel() > 0 else 0
|
| 2549 |
-
if target_length != output_audio.shape[-1] or not bool((input_audio_lengths == target_length).all().item()):
|
| 2550 |
-
trimmed_audio = output_audio.new_zeros(
|
| 2551 |
-
(output_audio.shape[0], output_audio.shape[1], target_length)
|
| 2552 |
-
)
|
| 2553 |
-
for batch_index, length in enumerate(input_audio_lengths.tolist()):
|
| 2554 |
-
trimmed_audio[batch_index, :, : int(length)] = output_audio[batch_index, :, : int(length)]
|
| 2555 |
-
output_audio = trimmed_audio
|
| 2556 |
-
output_audio_lengths = input_audio_lengths
|
| 2557 |
|
| 2558 |
if not return_dict:
|
| 2559 |
return (output_audio_codes, output_audio, output_audio_lengths)
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
SUPPORTED_ATTENTION_IMPLEMENTATIONS = {"sdpa", "flash_attention_2"}
|
| 64 |
+
SUPPORTED_COMPUTE_DTYPES = {"fp32": None, "bf16": torch.bfloat16}
|
| 65 |
|
| 66 |
|
| 67 |
def resolve_compute_dtype(compute_dtype: str) -> torch.dtype | None:
|
|
|
|
| 1361 |
x = x.reshape(b, d, -1, h).permute(0, 1, 3, 2).reshape(b, d * h, -1)
|
| 1362 |
# We pad the input waveform to a multiple of `downsample_rate` before applying the encoder.
|
| 1363 |
# Use a ceil division to match that padding and avoid dropping the last (partially padded) frame.
|
| 1364 |
+
output_lengths = input_lengths // self.patch_size
|
| 1365 |
return x, output_lengths
|
| 1366 |
|
| 1367 |
def decode(self, x, input_lengths):
|
|
|
|
| 2469 |
>>> import torch
|
| 2470 |
>>> from transformers import MossAudioTokenizerModel
|
| 2471 |
|
| 2472 |
+
>>> model = MossAudioTokenizerModel.from_pretrained("moss_audio_tokenizer-model")
|
| 2473 |
|
| 2474 |
>>> # Create dummy audio input
|
| 2475 |
>>> audio = torch.randn(1, 1, 24000) # 1 second of audio at 24kHz
|
|
|
|
| 2485 |
output_audio_codes_lengths: torch.Tensor | None = None
|
| 2486 |
output_audio: torch.Tensor | None = None
|
| 2487 |
output_audio_lengths: torch.Tensor | None = None
|
|
|
|
| 2488 |
decoded_from_encoded_codes = False
|
| 2489 |
|
| 2490 |
# Encode if input_values provided
|
| 2491 |
if input_values is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2492 |
encoder_output = self.encode(input_values, padding_mask, num_quantizers, return_dict=True)
|
| 2493 |
encoder_output = cast(MossAudioTokenizerEncoderOutput, encoder_output)
|
| 2494 |
output_audio_codes = encoder_output.audio_codes
|
|
|
|
| 2514 |
decoder_output = cast(MossAudioTokenizerDecoderOutput, decoder_output)
|
| 2515 |
output_audio = decoder_output.audio
|
| 2516 |
output_audio_lengths = decoder_output.audio_lengths
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2517 |
|
| 2518 |
if not return_dict:
|
| 2519 |
return (output_audio_codes, output_audio, output_audio_lengths)
|