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)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("OpenMOSS-Team/MOSS-Audio-Tokenizer-v2", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload MOSS Audio Tokenizer v2
Browse files- .gitattributes +0 -48
- .gitignore +2 -1
- README.md +119 -151
- configuration_moss_audio_tokenizer.py +2 -3
- modeling_moss_audio_tokenizer.py +43 -3
.gitattributes
CHANGED
|
@@ -1,49 +1 @@
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.gitignore
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
dev/*
|
| 2 |
-
|
|
|
|
|
|
| 1 |
dev/*
|
| 2 |
+
*.pyc
|
| 3 |
+
__pycache__/
|
README.md
CHANGED
|
@@ -1,151 +1,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 |
-
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
##
|
| 82 |
-
|
| 83 |
-
`
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
``
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
]
|
| 121 |
-
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 |
-
|
|
|
|
| 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 |
+
- trust-remote-code
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# MOSS-Audio-Tokenizer-V2
|
| 13 |
+
|
| 14 |
+
MOSS-Audio-Tokenizer-V2 is the 48 kHz stereo audio tokenizer used by the
|
| 15 |
+
MOSS-TTS v1.5 local-transformer release. It exposes a Hugging Face
|
| 16 |
+
`AutoModel` interface with `trust_remote_code=True` and supports both waveform
|
| 17 |
+
reconstruction and chunked streaming encode/decode.
|
| 18 |
+
|
| 19 |
+
## Model Details
|
| 20 |
+
|
| 21 |
+
| Item | Value |
|
| 22 |
+
| --- | --- |
|
| 23 |
+
| Repository | `OpenMOSS-Team/MOSS-Audio-Tokenizer-V2` |
|
| 24 |
+
| Interface | Hugging Face `AutoModel` remote code |
|
| 25 |
+
| Audio format | 48 kHz stereo |
|
| 26 |
+
| Frame rate | 12.5 Hz |
|
| 27 |
+
| Quantizers | 32 |
|
| 28 |
+
| Weight format | sharded `*.safetensors` |
|
| 29 |
+
| Attention backends | `sdpa`, `flash_attention_2` |
|
| 30 |
+
| Compute dtypes | `fp32`, `bf16`, `fp16` |
|
| 31 |
+
|
| 32 |
+
The public waveform interface accepts stereo tensors shaped `(2, T)` or batched
|
| 33 |
+
stereo tensors shaped `(B, 2, T)`. Mono inputs can be repeated to stereo before
|
| 34 |
+
encoding.
|
| 35 |
+
|
| 36 |
+
## Quickstart
|
| 37 |
+
|
| 38 |
+
```python
|
| 39 |
+
import torch
|
| 40 |
+
import torchaudio
|
| 41 |
+
from transformers import AutoModel
|
| 42 |
+
|
| 43 |
+
repo_id = "OpenMOSS-Team/MOSS-Audio-Tokenizer-V2"
|
| 44 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 45 |
+
|
| 46 |
+
model = AutoModel.from_pretrained(repo_id, trust_remote_code=True).to(device).eval()
|
| 47 |
+
|
| 48 |
+
wav, sr = torchaudio.load("input.wav")
|
| 49 |
+
if sr != model.sampling_rate:
|
| 50 |
+
wav = torchaudio.functional.resample(wav, sr, model.sampling_rate)
|
| 51 |
+
|
| 52 |
+
if wav.shape[0] == 1:
|
| 53 |
+
wav = wav.repeat(model.config.number_channels, 1)
|
| 54 |
+
elif wav.shape[0] > model.config.number_channels:
|
| 55 |
+
wav = wav[: model.config.number_channels]
|
| 56 |
+
|
| 57 |
+
wav = wav.unsqueeze(0).to(device)
|
| 58 |
+
|
| 59 |
+
with torch.inference_mode():
|
| 60 |
+
encoded = model.encode(wav, return_dict=True)
|
| 61 |
+
decoded = model.decode(encoded.audio_codes, return_dict=True)
|
| 62 |
+
|
| 63 |
+
torchaudio.save("reconstructed.wav", decoded.audio.squeeze(0).cpu(), model.sampling_rate)
|
| 64 |
+
print(encoded.audio_codes.shape)
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
## Attention Backend And Compute Dtype
|
| 68 |
+
|
| 69 |
+
`config.attention_implementation` controls the transformer attention backend.
|
| 70 |
+
`config.compute_dtype` controls non-quantizer inference autocast. The quantizer
|
| 71 |
+
runs in fp32.
|
| 72 |
+
|
| 73 |
+
```python
|
| 74 |
+
model.set_attention_implementation("flash_attention_2")
|
| 75 |
+
model.set_compute_dtype("bf16")
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
Use `flash_attention_2` only when FlashAttention 2 is installed and the GPU
|
| 79 |
+
supports it. Otherwise use `sdpa`.
|
| 80 |
+
|
| 81 |
+
## Streaming Encode/Decode
|
| 82 |
+
|
| 83 |
+
`encode`, `decode`, `batch_encode`, and `batch_decode` support streaming through
|
| 84 |
+
the `chunk_duration` argument.
|
| 85 |
+
|
| 86 |
+
- `chunk_duration` is expressed in seconds.
|
| 87 |
+
- `chunk_duration * config.sampling_rate` must be divisible by
|
| 88 |
+
`config.downsample_rate`.
|
| 89 |
+
- Streaming batch inference is supported.
|
| 90 |
+
|
| 91 |
+
```python
|
| 92 |
+
import torch
|
| 93 |
+
from transformers import AutoModel
|
| 94 |
+
|
| 95 |
+
repo_id = "OpenMOSS-Team/MOSS-Audio-Tokenizer-V2"
|
| 96 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 97 |
+
|
| 98 |
+
model = AutoModel.from_pretrained(repo_id, trust_remote_code=True).to(device).eval()
|
| 99 |
+
audio = torch.randn(2, 48000 * 6, device=device)
|
| 100 |
+
|
| 101 |
+
with torch.inference_mode():
|
| 102 |
+
encoded = model.encode(audio.unsqueeze(0), return_dict=True, chunk_duration=0.08)
|
| 103 |
+
decoded = model.decode(encoded.audio_codes, return_dict=True, chunk_duration=0.08)
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
## Repository Files
|
| 107 |
+
|
| 108 |
+
- `configuration_moss_audio_tokenizer.py`
|
| 109 |
+
- `modeling_moss_audio_tokenizer.py`
|
| 110 |
+
- `__init__.py`
|
| 111 |
+
- `config.json`
|
| 112 |
+
- `model.safetensors.index.json`
|
| 113 |
+
- `model-00001-of-00003.safetensors`
|
| 114 |
+
- `model-00002-of-00003.safetensors`
|
| 115 |
+
- `model-00003-of-00003.safetensors`
|
| 116 |
+
|
| 117 |
+
## License
|
| 118 |
+
|
| 119 |
+
Apache License 2.0.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
configuration_moss_audio_tokenizer.py
CHANGED
|
@@ -31,8 +31,7 @@ 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 |
-
[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,7 +57,7 @@ class MossAudioTokenizerConfig(PreTrainedConfig):
|
|
| 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*):
|
|
|
|
| 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 MOSS-Audio-Tokenizer-V2 style architecture.
|
|
|
|
| 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 |
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"`, and `"fp16"`.
|
| 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*):
|
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 = input_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,10 +2485,38 @@ 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 |
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,6 +2542,18 @@ class MossAudioTokenizerModel(MossAudioTokenizerPreTrainedModel):
|
|
| 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)
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
SUPPORTED_ATTENTION_IMPLEMENTATIONS = {"sdpa", "flash_attention_2"}
|
| 64 |
+
SUPPORTED_COMPUTE_DTYPES = {"fp32": None, "bf16": torch.bfloat16, "fp16": torch.float16}
|
| 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 = torch.div(input_lengths + self.patch_size - 1, self.patch_size, rounding_mode="floor")
|
| 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("OpenMOSS-Team/MOSS-Audio-Tokenizer-V2")
|
| 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 |
+
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 |
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)
|