File size: 4,678 Bytes
9040f4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5f0c8b
9040f4d
 
 
 
f5f0c8b
 
 
 
 
 
 
 
 
 
 
 
 
 
9040f4d
 
 
 
 
 
 
 
 
 
 
 
 
 
f5f0c8b
9040f4d
 
 
 
 
 
f5f0c8b
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
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
# WannabeTokenizer inference

A self-contained, offline inference package for:

1. converting an audio file into YuE2 semantic token IDs at 25 Hz; and
2. reconstructing audio from those IDs with YuE2's NAR and VAE decoder.

The unified tokenizer checkpoint includes both its MERT2 encoder and cosine quantizer. The YuE2 checkpoint is supplied separately. No Hub downloads, remote model code, training code, dataset tooling, or pickle checkpoints are used.

## Requirements

- Python 3.10+
- PyTorch 2.5+ and a matching torchaudio build
- A CUDA GPU is strongly recommended for YuE2 reconstruction
- The bf16 YuE2 all-in-one checkpoint (`yue2_3b_bf16.safetensors`)

Install the Python dependencies in your environment:

```bash
python -m pip install -r requirements.txt
```

## Run

From this folder:

```bash
python run_tokenizer.py /path/to/input.wav --tokenizer checkpoints/WannabeTokenizer.safetensors --yue2-checkpoint /path/to/yue2_3b_bf16.safetensors --output-dir output
```

Outputs:

- `output/tokens.npy` — one-dimensional `int32` NumPy array, IDs in `0..32767`
- `output/reconstructed.wav` — 48 kHz stereo PCM-24 WAV

Existing outputs are protected unless `--overwrite` is supplied. Common input formats supported by libsndfile or torchaudio include WAV, FLAC, OGG, MP3, and M4A.

Optional settings:

```bash
python run_tokenizer.py input.flac --tokenizer checkpoints/WannabeTokenizer.safetensors --yue2-checkpoint /path/to/yue2_3b_bf16.safetensors --output-dir output --device cuda:0 --seed 1 --steps 32 --level all
```

Use `python run_tokenizer.py --help` for all options. If installed as a package, the equivalent entry point is `wannabe-tokenize`.

## Loudness

The tokenizer writes the input's loudness into the IDs, and YuE2 renders whatever level the IDs carry. YuE2's own songs sit in a fairly narrow loudness range, so by default a song outside that range is brought into it before it is tokenized: one gain for the whole song from its gated RMS, with a peak limiter if the song was raised. Per-section gains are never used, so the song keeps its dynamics.

`--level` chooses this:

- `band` (default): a song inside YuE2's usual range is left alone, and one outside it is moved only as far as the nearest edge. The checkpoint names its range (metadata `head.level_band`); one that names none uses -21 to -13 dBFS (a -25 dBFS song to -21, a -9 dBFS master to -13). This is the smallest change that puts a song in the range YuE2's own songs cover, and how ai-toolkit reads songs by default.
- `all`: every song is moved to -16.5 dBFS (about -14 LUFS), the level YuE2's own covers of real songs sit at, whatever the original master's.
- `asis`: the song is read as recorded. Use it for audio YuE2 generated itself, which is already at its own level.

The run prints the gain it applied.

Decode each song from the same file every time. The IDs also depend on where the 40-ms frame grid falls in the audio, so two encodes of one recording (an MP3 and an M4A download, say) can give noticeably different IDs.

## Behavior and resource use

Reconstruction is generative and seed-dependent, not a lossless inversion of the recording. It uses YuE2's base NAR with an empty tags/lyrics prompt. Different seeds produce slightly different audio from the same tokens.

The tokenizer is released before YuE2 loads so the encoder and renderer do not occupy accelerator memory at the same time. The loader skips YuE2's unused language-model head and VAE encoder. VAE decode tiles automatically shrink after an out-of-memory error. Longer inputs require more memory and time.

## Python API

```python
from wannabe_tokenizer import WannabeTokenizer, YuE2Renderer
from wannabe_tokenizer.audio import load_audio, save_wav_atomic

waveform = load_audio("input.wav")
tokenizer = WannabeTokenizer("checkpoints/WannabeTokenizer.safetensors", device="cuda")
tokens, gain_db = tokenizer.tokenize(waveform, level="band", return_gain=True)

del tokenizer  # release encoder memory before loading YuE2 in a real application
renderer = YuE2Renderer("/path/to/yue2_3b_bf16.safetensors", device="cuda")
reconstructed = renderer.reconstruct(tokens, seed=0, steps=32)
save_wav_atomic("reconstructed.wav", reconstructed)
```

## Tests

The lightweight tests do not load model weights:

```bash
python -m unittest discover -s tests
```

An end-to-end run requires both checkpoints and is the recommended numerical smoke test after installation.

## Third-party models

Model weights are subject to their respective upstream licenses. The YuE2 checkpoint identifies its license as CC-BY-NC-4.0. Review the applicable model and dependency licenses before redistribution or commercial use.