Views
No views yet
OpenMOSS-Team/MOSS-Audio-Tokenizer-Nano using mlx-audio version 0.4.0.transformers.models.moss_audio_tokenizer module. It is intended to be uploaded to a Hugging Face Hub model repository
and loaded with trust_remote_code=True when needed.1import torch
2from transformers import AutoModel
3import torchaudio
4
5repo_id = "OpenMOSS-Team/MOSS-Audio-Tokenizer"
6model = AutoModel.from_pretrained(repo_id, trust_remote_code=True).eval()
7
8wav, sr = torchaudio.load('demo/demo_gt.wav')
9if sr != model.sampling_rate:
10 wav = torchaudio.functional.resample(wav, sr, model.sampling_rate)
11if wav.shape[0] == 1:
12 wav = wav.repeat(model.config.number_channels, 1)
13else:
14 wav = wav[: model.config.number_channels]
15wav = wav.unsqueeze(0)
16enc = model.encode(wav, return_dict=True)
17print(f"enc.audio_codes.shape: {enc.audio_codes.shape}")
18dec = model.decode(enc.audio_codes, return_dict=True)
19print(f"dec.audio.shape: {dec.audio.shape}")
20wav = dec.audio.squeeze(0)
21torchaudio.save("demo/demo_rec.wav", wav, sample_rate=model.sampling_rate)
22
23# Decode using only the first 8 layers of the RVQ
24dec_rvq8 = model.decode(enc.audio_codes[:8], return_dict=True)
25wav_rvq8 = dec_rvq8.audio.squeeze(0)
26torchaudio.save("demo/demo_rec_rvq8.wav", wav_rvq8, sample_rate=model.sampling_rate)config.attention_implementation controls whether transformer layers prefer sdpa or flash_attention_2.
config.compute_dtype controls the non-quantizer autocast dtype and supports fp32, bf16, and fp16.1model.set_attention_implementation("flash_attention_2")
2model.set_compute_dtype("fp16")MossAudioTokenizerModel.encode, decode, batch_encode, and batch_decode all support streaming through a
chunk_duration argument.chunk_duration is expressed in seconds.chunk_duration * MossAudioTokenizerConfig.sampling_rate must be divisible by MossAudioTokenizerConfig.downsample_rate.(2, T) or batched stereo inputs shaped (B, 2, T).1import torch
2from transformers import AutoModel
3
4repo_id = "OpenMOSS-Team/MOSS-Audio-Tokenizer"
5model = AutoModel.from_pretrained(repo_id, trust_remote_code=True).eval()
6audio = torch.randn(2, 48000 * 6) # dummy stereo waveform
7
8# 6.0s @ 48kHz = 288000 samples, divisible by downsample_rate=3840
9enc = model.encode(audio.unsqueeze(0), return_dict=True, chunk_duration=0.08)
10dec = model.decode(enc.audio_codes, return_dict=True, chunk_duration=0.08)
11
12batch_enc = model.batch_encode([audio, audio[:, : 48000 * 3]], chunk_duration=0.08)
13codes_list = [
14 batch_enc.audio_codes[:, i, : batch_enc.audio_codes_lengths[i]]
15 for i in range(batch_enc.audio_codes.shape[1])
16]
17batch_dec = model.batch_decode(codes_list, chunk_duration=0.08)batch_decode(..., streaming=True, ...).max_batch_size=.... If it is omitted, the first batch size reserves the
fixed-slot decoder budget for that public stream.finalize_indices means "decode these rows one last time, then evict them". The indices are interpreted against the
pre-call logical order.reset_stream=True discards the hidden public streaming state and starts a fresh stream.max_batch_size1import torch
2from transformers import AutoModel
3
4repo_id = "OpenMOSS-Team/MOSS-Audio-Tokenizer"
5model = AutoModel.from_pretrained(repo_id, trust_remote_code=True).eval()
6num_quantizers = model.config.quantizer_kwargs["num_quantizers"]
7
8codes_a0 = torch.randint(0, 8, (num_quantizers, 2))
9codes_b0 = torch.randint(0, 8, (num_quantizers, 3))
10codes_a1 = torch.randint(0, 8, (num_quantizers, 2))
11codes_b1 = torch.randint(0, 8, (num_quantizers, 2))
12codes_c0 = torch.randint(0, 8, (num_quantizers, 1))
13codes_a2 = torch.randint(0, 8, (num_quantizers, 1))
14codes_b2 = torch.randint(0, 8, (num_quantizers, 2))
15codes_c1 = torch.randint(0, 8, (num_quantizers, 2))
16codes_b3 = torch.randint(0, 8, (num_quantizers, 1))
17codes_c2 = torch.randint(0, 8, (num_quantizers, 1))
18
19# First call reserves 3 fixed decoder slots for A and B.
20out_ab0 = model.batch_decode(
21 [codes_a0, codes_b0],
22 streaming=True,
23 max_batch_size=3,
24 reset_stream=True,
25)
26
27# Same logical rows continue in-order; C is a tail append.
28out_abc1 = model.batch_decode(
29 [codes_a1, codes_b1, codes_c0],
30 streaming=True,
31)
32
33# Finalize A against the pre-call logical order. A still decodes in this call,
34# then is evicted immediately afterward.
35out_abc2 = model.batch_decode(
36 [codes_a2, codes_b2, codes_c1],
37 streaming=True,
38 finalize_indices=[0],
39)
40
41# The next call can shrink to the surviving logical rows only.
42out_bc3 = model.batch_decode(
43 [codes_b3, codes_c2],
44 streaming=True,
45)configuration_moss_audio_tokenizer.pymodeling_moss_audio_tokenizer.py__init__.pyconfig.json