eadx YWMditto commited on
Commit
1abcd6c
·
0 Parent(s):

Duplicate from OpenMOSS-Team/MOSS-TTS-v1.5

Browse files

Co-authored-by: YWMditto <YWMditto@users.noreply.huggingface.co>

.gitattributes ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt 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
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
32
+ *.wasm filter=lfs diff=lfs merge=lfs -text
33
+ *.wav filter=lfs diff=lfs merge=lfs -text
34
+ *.xz filter=lfs diff=lfs merge=lfs -text
35
+ *.zip filter=lfs diff=lfs merge=lfs -text
36
+ *.zst filter=lfs diff=lfs merge=lfs -text
37
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,260 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ tags:
4
+ - text-to-speech
5
+ language:
6
+ - zh
7
+ - yue
8
+ - en
9
+ - ar
10
+ - cs
11
+ - da
12
+ - de
13
+ - nl
14
+ - es
15
+ - fr
16
+ - fi
17
+ - el
18
+ - he
19
+ - hi
20
+ - hu
21
+ - ja
22
+ - it
23
+ - ko
24
+ - mk
25
+ - ms
26
+ - ru
27
+ - fa
28
+ - pl
29
+ - pt
30
+ - sv
31
+ - ro
32
+ - sw
33
+ - tl
34
+ - th
35
+ - tr
36
+ - vi
37
+ ---
38
+ # MOSS-TTS Family
39
+
40
+
41
+ <br>
42
+
43
+ <p align="center">
44
+ &nbsp;&nbsp;&nbsp;&nbsp;
45
+ <img src="https://speech-demo.oss-cn-shanghai.aliyuncs.com/moss_tts_demo/tts_readme_imgaes_demo/openmoss_x_mosi" height="50" align="middle" />
46
+ </p>
47
+
48
+
49
+
50
+ <div align="center">
51
+ <a href="https://github.com/OpenMOSS/MOSS-TTS/tree/main"><img src="https://img.shields.io/badge/Project%20Page-GitHub-blue"></a>
52
+ <a href="https://modelscope.cn/collections/OpenMOSS-Team/MOSS-TTS"><img src="https://img.shields.io/badge/ModelScope-Models-lightgrey?logo=modelscope&amp"></a>
53
+ <a href="https://mosi.cn/#models"><img src="https://img.shields.io/badge/Blog-View-blue?logo=internet-explorer&amp"></a>
54
+ <a href="https://arxiv.org/abs/2603.18090"><img src="https://img.shields.io/badge/Arxiv-2603.18090-red?logo=Arxiv&amp"></a>
55
+
56
+ <a href="https://studio.mosi.cn"><img src="https://img.shields.io/badge/AIStudio-Try-green?logo=internet-explorer&amp"></a>
57
+ <a href="https://studio.mosi.cn/docs/moss-tts"><img src="https://img.shields.io/badge/API-Docs-00A3FF?logo=fastapi&amp"></a>
58
+ <a href="https://x.com/Open_MOSS"><img src="https://img.shields.io/badge/Twitter-Follow-black?logo=x&amp"></a>
59
+ <a href="https://discord.gg/fvm5TaWjU3"><img src="https://img.shields.io/badge/Discord-Join-5865F2?logo=discord&amp"></a>
60
+ </div>
61
+
62
+
63
+ # MOSS-TTS-v1.5
64
+
65
+ **MOSS-TTS-v1.5** is continued from [MOSS-TTS 1.0](https://huggingface.co/OpenMOSS-Team/MOSS-TTS). It preserves the main 1.0 capabilities, including zero-shot voice cloning, long-form speech generation, token-level duration control, Pinyin/IPA pronunciation control, multilingual synthesis, and code-switching. For the full 1.0 feature walkthrough, input schema, decoding hyperparameters, and evaluation tables, please refer to the [MOSS-TTS 1.0 README](https://huggingface.co/OpenMOSS-Team/MOSS-TTS).
66
+
67
+ Compared with MOSS-TTS 1.0, v1.5 focuses on the following improvements:
68
+
69
+ - **Stronger multilingual synthesis with language tags**: when the `language` field is omitted, v1.5 may improve some languages and regress slightly on others compared with 1.0. When the language is specified, v1.5 is stronger than 1.0 on almost all supported languages. Set the tag when building the user message, for example `processor.build_user_message(text=text_fr, language="French")`.
70
+ - **More stable voice cloning**: v1.5 improves speaker similarity and reduces cloning variance, making repeated generations more consistent.
71
+ - **Better long-reference, short-text cloning**: v1.5 handles scenarios where the reference audio is much longer than the target text more reliably than 1.0.
72
+ - **More stable punctuation-following prosody**: v1.5 follows punctuation-driven pauses more closely, especially in long sentences.
73
+ - **Explicit pause control**: v1.5 supports inline pause markers such as `"[pause 3.2s]"`. For example, `我今天学习了一首中国的古诗,它的名字是[pause 3.2s]静夜思!` inserts an explicit 3.2s pause before `静夜思`.
74
+
75
+ ## Supported Languages
76
+
77
+ MOSS-TTS-v1.5 currently supports **31 languages**. It keeps the 20 languages supported by [MOSS-TTS 1.0](https://huggingface.co/OpenMOSS-Team/MOSS-TTS) and extends multilingual continued training to additional languages including Cantonese, Dutch, Finnish, Hindi, Macedonian, Malay, Romanian, Swahili, Tagalog, Thai, and Vietnamese.
78
+
79
+ | Language | Code | Flag | Language | Code | Flag | Language | Code | Flag |
80
+ |---|---|---|---|---|---|---|---|---|
81
+ | Chinese | zh | 🇨🇳 | Cantonese | yue | 🇭🇰 | English | en | 🇺🇸 |
82
+ | Arabic | ar | 🇸🇦 | Czech | cs | 🇨🇿 | Danish | da | 🇩🇰 |
83
+ | Dutch | nl | 🇳🇱 | Finnish | fi | 🇫🇮 | French | fr | 🇫🇷 |
84
+ | German | de | 🇩🇪 | Greek | el | 🇬🇷 | Hebrew | he | 🇮🇱 |
85
+ | Hindi | hi | 🇮🇳 | Hungarian | hu | 🇭🇺 | Italian | it | 🇮🇹 |
86
+ | Japanese | ja | 🇯🇵 | Korean | ko | 🇰🇷 | Macedonian | mk | 🇲🇰 |
87
+ | Malay | ms | 🇲🇾 | Persian (Farsi) | fa | 🇮🇷 | Polish | pl | 🇵🇱 |
88
+ | Portuguese | pt | 🇵🇹 | Romanian | ro | 🇷🇴 | Russian | ru | 🇷🇺 |
89
+ | Spanish | es | 🇪🇸 | Swahili | sw | 🇹🇿 | Swedish | sv | 🇸🇪 |
90
+ | Tagalog | tl | 🇵🇭 | Thai | th | 🇹🇭 | Turkish | tr | 🇹🇷 |
91
+ | Vietnamese | vi | 🇻🇳 | | | | | | |
92
+
93
+
94
+ ## Quick Start
95
+
96
+ ### Environment Setup
97
+
98
+ We recommend a clean, isolated Python environment with **Transformers 5.0.0** to avoid dependency conflicts.
99
+
100
+ ```bash
101
+ conda create -n moss-tts python=3.12 -y
102
+ conda activate moss-tts
103
+ ```
104
+
105
+ Install all required dependencies:
106
+
107
+ ```bash
108
+ git clone https://github.com/OpenMOSS/MOSS-TTS.git
109
+ cd MOSS-TTS
110
+ pip install --extra-index-url https://download.pytorch.org/whl/cu128 -e .
111
+ ```
112
+
113
+ #### (Optional) Install FlashAttention 2
114
+
115
+ For better speed and lower GPU memory usage, you can install FlashAttention 2 if your hardware supports it.
116
+
117
+ ```bash
118
+ pip install --extra-index-url https://download.pytorch.org/whl/cu128 -e ".[flash-attn]"
119
+ ```
120
+
121
+ If your machine has limited RAM and many CPU cores, you can cap build parallelism:
122
+
123
+ ```bash
124
+ MAX_JOBS=4 pip install --extra-index-url https://download.pytorch.org/whl/cu128 -e ".[flash-attn]"
125
+ ```
126
+
127
+ Notes:
128
+ - Dependencies are managed in `pyproject.toml`, which currently pins `torch==2.9.1+cu128` and `torchaudio==2.9.1+cu128`.
129
+ - If FlashAttention 2 fails to build on your machine, you can skip it and use the default attention backend.
130
+ - FlashAttention 2 is only available on supported GPUs and is typically used with `torch.float16` or `torch.bfloat16`.
131
+
132
+
133
+ ### Basic Usage
134
+
135
+ > Tip: MOSS-TTS-v1.5 uses the same generation API as the 1.0 **MossTTSDelay-8B** checkpoint. For multilingual inputs, set `language` whenever the language is known.
136
+
137
+ MOSS-TTS provides a convenient `generate` interface for rapid usage. The examples below cover:
138
+ 1. Direct generation (Chinese / English / multilingual text with language tags / Pinyin / IPA)
139
+ 2. Voice cloning
140
+ 3. Duration control
141
+ 4. Explicit pause control with `[pause X.Ys]`
142
+
143
+ ```python
144
+ from pathlib import Path
145
+ import importlib.util
146
+ import torch
147
+ import torchaudio
148
+ from transformers import AutoModel, AutoProcessor
149
+ # Disable the broken cuDNN SDPA backend
150
+ torch.backends.cuda.enable_cudnn_sdp(False)
151
+ # Keep these enabled as fallbacks
152
+ torch.backends.cuda.enable_flash_sdp(True)
153
+ torch.backends.cuda.enable_mem_efficient_sdp(True)
154
+ torch.backends.cuda.enable_math_sdp(True)
155
+
156
+
157
+ pretrained_model_name_or_path = "OpenMOSS-Team/MOSS-TTS-v1.5"
158
+ device = "cuda" if torch.cuda.is_available() else "cpu"
159
+ dtype = torch.bfloat16 if device == "cuda" else torch.float32
160
+
161
+ def resolve_attn_implementation() -> str:
162
+ # Prefer FlashAttention 2 when package + device conditions are met.
163
+ if (
164
+ device == "cuda"
165
+ and importlib.util.find_spec("flash_attn") is not None
166
+ and dtype in {torch.float16, torch.bfloat16}
167
+ ):
168
+ major, _ = torch.cuda.get_device_capability()
169
+ if major >= 8:
170
+ return "flash_attention_2"
171
+
172
+ # CUDA fallback: use PyTorch SDPA kernels.
173
+ if device == "cuda":
174
+ return "sdpa"
175
+
176
+ # CPU fallback.
177
+ return "eager"
178
+
179
+
180
+ attn_implementation = resolve_attn_implementation()
181
+ print(f"[INFO] Using attn_implementation={attn_implementation}")
182
+
183
+ processor = AutoProcessor.from_pretrained(
184
+ pretrained_model_name_or_path,
185
+ trust_remote_code=True,
186
+ )
187
+ processor.audio_tokenizer = processor.audio_tokenizer.to(device)
188
+
189
+ text_1 = "亲爱的你,\n你好呀。\n\n今天,我想用最认真、最温柔的声音,对你说一些重要的话。\n这些话,像一颗小小的星星,希望能在你的心里慢慢发光。\n\n首先,我想祝你——\n每天都能平平安安、快快乐乐。\n\n希望你早上醒来的时候,\n窗外有光,屋子里很安静,\n你的心是轻轻的,没有着急,也没有害怕。\n\n希望你吃饭的时候胃口很好,\n走路的时候脚步稳稳,\n晚上睡觉的时候,能做一个又一个甜甜的梦。\n\n我希望你能一直保持好奇心。\n对世界充满问题,\n对天空、星星、花草、书本和故事感兴趣。\n当你问“为什么”的时候,\n希望总有人愿意认真地听你说话。\n\n我也希望你学会温柔。\n温柔地对待朋友,\n温柔地对待小动物,\n也温柔地对待自己。\n\n如果有一天你犯了错,\n请不要太快责怪自己,\n因为每一个认真成长的人,\n都会在路上慢慢学会更好的方法。\n\n愿你拥有勇气。\n当你站在陌生的地方时,\n当你第一次举手发言时,\n当你遇到困难、感到害怕的时候,\n希望你能轻轻地告诉自己:\n“我可以试一试。”\n\n就算没有一次成功,也没有关系。\n失败不是坏事,\n它只是告诉你,你正在努力。\n\n我希望你学会分享快乐。\n把开心的事情告诉别人,\n把笑声送给身边的人,\n因为快乐被分享的时候,\n会变得更大、更亮。\n\n如果有一天你感到难过,\n我希望你知道——\n难过并不丢脸,\n哭泣也不是软弱。\n\n愿你能找到一个安全的地方,\n慢慢把心里的话说出来,\n然后再一次抬起头,看见希望。\n\n我还希望你能拥有梦想。\n这个梦想也许很大,\n也许很小,\n也许现在还说不清楚。\n\n没关系。\n梦想会和你一起长大,\n在时间里慢慢变得清楚。\n\n最后,我想送你一个最最重要的祝福:\n\n愿你被世界温柔对待,\n也愿你成为一个温柔的人。\n\n愿你的每一天,\n都值得被记住,\n都值得被珍惜。\n\n亲爱的你,\n请记住,\n你是独一无二的,\n你已经很棒了,\n而你的未来,\n一定会慢慢变得闪闪发光。\n\n祝你健康、勇敢、幸福,\n祝你永远带着笑容向前走。"
190
+ text_2 = "We stand on the threshold of the AI era.\nArtificial intelligence is no longer just a concept in laboratories, but is entering every industry, every creative endeavor, and every decision. It has learned to see, hear, speak, and think, and is beginning to become an extension of human capabilities. AI is not about replacing humans, but about amplifying human creativity, making knowledge more equitable, more efficient, and allowing imagination to reach further. A new era, jointly shaped by humans and intelligent systems, has arrived."
191
+ text_3 = "nin2 hao3,qing3 wen4 nin2 lai2 zi4 na3 zuo4 cheng2 shi4?"
192
+ text_4 = "nin2 hao3,qing4 wen3 nin2 lai2 zi4 na4 zuo3 cheng4 shi3?"
193
+ text_5 = "您好,请问您来自哪 zuo4 cheng2 shi4?"
194
+ text_6 = "/həloʊ, meɪ aɪ æsk wɪtʃ sɪti juː ɑːr frʌm?/"
195
+ text_7 = "Bonjour, je voudrais essayer une voix française naturelle et stable."
196
+ text_8 = "我今天学习了一首中国的古诗,它的名字是[pause 3.2s]静夜思!"
197
+
198
+ # Use audio from ./assets/audio to avoid downloading from the cloud.
199
+ ref_audio_1 = "https://speech-demo.oss-cn-shanghai.aliyuncs.com/moss_tts_demo/tts_readme_demo/reference_zh.wav"
200
+ ref_audio_2 = "https://speech-demo.oss-cn-shanghai.aliyuncs.com/moss_tts_demo/tts_readme_demo/reference_en.m4a"
201
+
202
+ conversations = [
203
+ # Direct TTS (no reference). Language tags are recommended in v1.5.
204
+ [processor.build_user_message(text=text_1)],
205
+ [processor.build_user_message(text=text_2)],
206
+ # Direct TTS (no reference). For languages ​​other than Chinese and English, it is recommended to use language tags.
207
+ [processor.build_user_message(text=text_7, language="French")],
208
+ # Pinyin or IPA input
209
+ [processor.build_user_message(text=text_3)],
210
+ [processor.build_user_message(text=text_4)],
211
+ [processor.build_user_message(text=text_5)],
212
+ [processor.build_user_message(text=text_6)],
213
+ # Explicit pause control. Use [pause X.Ys], such as [pause 3.2s].
214
+ [processor.build_user_message(text=text_8)],
215
+ # Voice cloning (with reference)
216
+ [processor.build_user_message(text=text_1, reference=[ref_audio_1])],
217
+ [processor.build_user_message(text=text_2, reference=[ref_audio_2])],
218
+ # Duration control
219
+ [processor.build_user_message(text=text_2, tokens=325)],
220
+ [processor.build_user_message(text=text_2, tokens=600)],
221
+ ]
222
+
223
+ model = AutoModel.from_pretrained(
224
+ pretrained_model_name_or_path,
225
+ trust_remote_code=True,
226
+ # If FlashAttention 2 is installed, you can set attn_implementation="flash_attention_2"
227
+ attn_implementation=attn_implementation,
228
+ torch_dtype=dtype,
229
+ ).to(device)
230
+ model.eval()
231
+
232
+ batch_size = 1
233
+
234
+ save_dir = Path("inference_root")
235
+ save_dir.mkdir(exist_ok=True, parents=True)
236
+ sample_idx = 0
237
+ with torch.no_grad():
238
+ for start in range(0, len(conversations), batch_size):
239
+ batch_conversations = conversations[start : start + batch_size]
240
+ batch = processor(batch_conversations, mode="generation")
241
+ input_ids = batch["input_ids"].to(device)
242
+ attention_mask = batch["attention_mask"].to(device)
243
+
244
+ outputs = model.generate(
245
+ input_ids=input_ids,
246
+ attention_mask=attention_mask,
247
+ max_new_tokens=4096,
248
+ )
249
+
250
+ for message in processor.decode(outputs):
251
+ audio = message.audio_codes_list[0]
252
+ out_path = save_dir / f"sample{sample_idx}.wav"
253
+ sample_idx += 1
254
+ torchaudio.save(out_path, audio.unsqueeze(0), processor.model_config.sampling_rate)
255
+
256
+ ```
257
+
258
+ ## More Usage
259
+
260
+ MOSS-TTS-v1.5 is API-compatible with MOSS-TTS 1.0. For continuation with prefix audio, detailed `UserMessage` and `AssistantMessage` fields, generation hyperparameters, Pinyin/IPA preprocessing examples, and evaluation results, see the [MOSS-TTS 1.0 README](https://huggingface.co/OpenMOSS-Team/MOSS-TTS).
__init__.py ADDED
File without changes
added_tokens.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "</think>": 151668,
3
+ "</tool_call>": 151658,
4
+ "</tool_response>": 151666,
5
+ "<think>": 151667,
6
+ "<tool_call>": 151657,
7
+ "<tool_response>": 151665,
8
+ "<|audio_end|>": 151653,
9
+ "<|audio_pad|>": 151654,
10
+ "<|audio_start|>": 151652,
11
+ "<|box_end|>": 151649,
12
+ "<|box_start|>": 151648,
13
+ "<|endoftext|>": 151643,
14
+ "<|file_sep|>": 151664,
15
+ "<|fim_middle|>": 151660,
16
+ "<|fim_pad|>": 151662,
17
+ "<|fim_prefix|>": 151659,
18
+ "<|fim_suffix|>": 151661,
19
+ "<|im_end|>": 151645,
20
+ "<|im_start|>": 151644,
21
+ "<|image_pad|>": 151655,
22
+ "<|object_ref_end|>": 151647,
23
+ "<|object_ref_start|>": 151646,
24
+ "<|quad_end|>": 151651,
25
+ "<|quad_start|>": 151650,
26
+ "<|repo_name|>": 151663,
27
+ "<|video_pad|>": 151656
28
+ }
chat_template.jinja ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {% for message in messages %}<|im_start|>{{ message['role'] }}
2
+ {% if message['content'] is string %}{{ message['content'] }}{% else %}{% for content in message['content'] %}{% if content.get('type') == 'text' %}{{ content['text'] }}{% endif %}{% endfor %}{% endif %}<|im_end|>
3
+ {% endfor %}{% if add_generation_prompt %}<|im_start|>assistant
4
+ {% endif %}
config.json ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "moss_tts_delay",
3
+ "architectures": [
4
+ "MossTTSDelayModel"
5
+ ],
6
+ "auto_map": {
7
+ "AutoConfig": "configuration_moss_tts.MossTTSDelayConfig",
8
+ "AutoModel": "modeling_moss_tts.MossTTSDelayModel"
9
+ },
10
+ "dtype": "bfloat16",
11
+ "initializer_range": 0.02,
12
+ "language_config": {
13
+ "_name_or_path": "Qwen/Qwen3-8B",
14
+ "architectures": [
15
+ "Qwen3ForCausalLM"
16
+ ],
17
+ "attention_bias": false,
18
+ "attention_dropout": 0.0,
19
+ "bos_token_id": 151643,
20
+ "eos_token_id": 151645,
21
+ "pad_token_id": 151643,
22
+ "head_dim": 128,
23
+ "hidden_act": "silu",
24
+ "hidden_size": 4096,
25
+ "initializer_range": 0.02,
26
+ "intermediate_size": 12288,
27
+ "layer_types": [
28
+ "full_attention",
29
+ "full_attention",
30
+ "full_attention",
31
+ "full_attention",
32
+ "full_attention",
33
+ "full_attention",
34
+ "full_attention",
35
+ "full_attention",
36
+ "full_attention",
37
+ "full_attention",
38
+ "full_attention",
39
+ "full_attention",
40
+ "full_attention",
41
+ "full_attention",
42
+ "full_attention",
43
+ "full_attention",
44
+ "full_attention",
45
+ "full_attention",
46
+ "full_attention",
47
+ "full_attention",
48
+ "full_attention",
49
+ "full_attention",
50
+ "full_attention",
51
+ "full_attention",
52
+ "full_attention",
53
+ "full_attention",
54
+ "full_attention",
55
+ "full_attention",
56
+ "full_attention",
57
+ "full_attention",
58
+ "full_attention",
59
+ "full_attention",
60
+ "full_attention",
61
+ "full_attention",
62
+ "full_attention",
63
+ "full_attention"
64
+ ],
65
+ "max_position_embeddings": 40960,
66
+ "max_window_layers": 36,
67
+ "model_type": "qwen3",
68
+ "num_attention_heads": 32,
69
+ "num_hidden_layers": 36,
70
+ "num_key_value_heads": 8,
71
+ "rms_norm_eps": 1e-06,
72
+ "rope_scaling": null,
73
+ "rope_theta": 1000000,
74
+ "sliding_window": null,
75
+ "use_cache": true,
76
+ "use_sliding_window": false,
77
+ "vocab_size": 155648
78
+ },
79
+ "n_vq": 32,
80
+ "audio_vocab_size": 1024,
81
+ "audio_user_slot_token_id": 151654,
82
+ "audio_assistant_gen_slot_token_id": 151656,
83
+ "audio_assistant_delay_slot_token_id": 151662,
84
+ "audio_start_token_id": 151652,
85
+ "audio_end_token_id": 151653,
86
+ "audio_pad_code": 1024,
87
+ "sampling_rate": 24000,
88
+ "transformers_version": "4.57.1"
89
+ }
configuration_moss_tts.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2026 OpenMOSS and the HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """ MossTTSDelay model configuration """
16
+
17
+ from typing import Optional, Union
18
+ from transformers.configuration_utils import PretrainedConfig
19
+ from transformers.utils import logging
20
+ from transformers.models.qwen3 import Qwen3Config
21
+
22
+ logger = logging.get_logger(__name__)
23
+
24
+
25
+ class MossTTSDelayConfig(PretrainedConfig):
26
+ r"""
27
+ This is the configuration class to store the configuration of a [`MossTTSDelayModel`]. It is used to instantiate an
28
+ MossTTSDelay model according to the specified arguments, defining the model architecture. Instantiating a configuration
29
+ with the defaults will yield a similar configuration to that of the MossTTSDelay [MossTTSDelay-8B](https://huggingface.co/OpenMOSS/mosstts-8b) architecture.
30
+
31
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
32
+ documentation from [`PretrainedConfig`] for more information.
33
+
34
+ Args:
35
+ language_config (`Union[Qwen3Config, dict]`, *optional*):
36
+ Configuration for the backbone language model (Qwen3).
37
+ initializer_range (`float`, *optional*, defaults to 0.02):
38
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
39
+ n_vq (`int`, *optional*, defaults to 32):
40
+ Number of additional VQ (Vector Quantization) heads/channels for audio.
41
+ Determines the number of codebooks used in the audio representation.
42
+ audio_vocab_size (`int`, *optional*, defaults to 1024):
43
+ Vocabulary size for the audio tokens (codebooks 1 to N).
44
+ audio_user_slot_token_id (`int`, *optional*, defaults to 151654):
45
+ The specific token ID used as a placeholder/slot for user-side audio inputs in the prompt.
46
+ audio_assistant_gen_slot_token_id (`int`, *optional*, defaults to 151656):
47
+ The specific token ID representing the generation slot for the assistant's audio output.
48
+ Acting as the trigger for the TTS generation process.
49
+ audio_assistant_delay_slot_token_id (`int`, *optional*, defaults to 151662):
50
+ The token ID used in the 'Delay Pattern' paradigm to represent the delayed/offset positions
51
+ between different VQ channels.
52
+ audio_start_token_id (`int`, *optional*, defaults to 151652):
53
+ Special token ID used to denote the start of an audio sequence in the stream.
54
+ audio_end_token_id (`int`, *optional*, defaults to 151653):
55
+ Special token ID used to denote the end of an audio sequence (EOS for audio).
56
+ audio_pad_code (`int`, *optional*, defaults to 1024):
57
+ The padding value used within the audio VQ codebooks. Typically equals `audio_vocab_size`.
58
+ """
59
+ model_type = "moss_tts_delay"
60
+ keys_to_ignore_at_inference = ["past_key_values"]
61
+
62
+ def __init__(
63
+ self,
64
+ language_config: Optional[Union[Qwen3Config, dict]] = None,
65
+ initializer_range: float = 0.02,
66
+ n_vq: int = 32,
67
+ pad_token_id: int = 151643,
68
+ im_start_token_id: int = 151644,
69
+ im_end_token_id: int = 151645,
70
+ audio_vocab_size: int = 1024,
71
+ audio_user_slot_token_id: int = 151654,
72
+ audio_assistant_gen_slot_token_id: int = 151656,
73
+ audio_assistant_delay_slot_token_id: int = 151662,
74
+ audio_start_token_id: int = 151652,
75
+ audio_end_token_id: int = 151653,
76
+ audio_pad_code: int = 1024,
77
+ sampling_rate: int = 24000,
78
+ **kwargs,
79
+ ):
80
+ if isinstance(language_config, dict):
81
+ self.language_config = Qwen3Config(**language_config)
82
+ elif language_config is None:
83
+ self.language_config = Qwen3Config()
84
+ else:
85
+ self.language_config = language_config
86
+
87
+ self.initializer_range = initializer_range
88
+ self.n_vq = n_vq
89
+ self.audio_vocab_size = audio_vocab_size
90
+ self.audio_user_slot_token_id = audio_user_slot_token_id
91
+ self.audio_assistant_gen_slot_token_id = audio_assistant_gen_slot_token_id
92
+ self.audio_assistant_delay_slot_token_id = audio_assistant_delay_slot_token_id
93
+ self.audio_start_token_id = audio_start_token_id
94
+ self.audio_end_token_id = audio_end_token_id
95
+ self.audio_pad_code = audio_pad_code
96
+ self.sampling_rate = sampling_rate
97
+
98
+ self.hidden_size = self.language_config.hidden_size
99
+ self.vocab_size = self.language_config.vocab_size
100
+ self.im_start_token_id = self.language_config
101
+ self.pad_token_id = pad_token_id
102
+ self.im_start_token_id = im_start_token_id
103
+ self.im_end_token_id = im_end_token_id
104
+
105
+
106
+ super().__init__(**kwargs)
107
+
108
+ def to_dict(self):
109
+ output = super().to_dict()
110
+ if hasattr(self.language_config, "to_dict"):
111
+ output["language_config"] = self.language_config.to_dict()
112
+ else:
113
+ output["language_config"] = self.language_config
114
+ return output
inference_utils.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchaudio
3
+ import torch.nn.functional as F
4
+ from typing import Optional, List, Tuple
5
+ from tqdm import tqdm
6
+
7
+
8
+ def apply_top_k(logits, top_k):
9
+ batch_size, vocab_size = logits.shape
10
+ top_k = min(top_k, vocab_size)
11
+ top_k_values, top_k_indices = torch.topk(logits, top_k, dim=-1)
12
+ filtered_logits = torch.full_like(logits, float("-inf"))
13
+ batch_indices = torch.arange(batch_size).unsqueeze(-1)
14
+ filtered_logits[batch_indices, top_k_indices] = top_k_values
15
+ return filtered_logits
16
+
17
+
18
+ def apply_top_p(logits, top_p):
19
+ probs = F.softmax(logits, dim=-1)
20
+ sorted_probs, sorted_indices = torch.sort(probs, descending=True, dim=-1)
21
+ cumulative_probs = torch.cumsum(sorted_probs, dim=-1)
22
+ sorted_indices_to_remove = cumulative_probs > top_p
23
+ sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
24
+ sorted_indices_to_remove[..., 0] = False
25
+ batch_size = logits.shape[0]
26
+ filtered_logits = logits.clone()
27
+ for i in range(batch_size):
28
+ indices_to_remove = sorted_indices[i][sorted_indices_to_remove[i]]
29
+ filtered_logits[i, indices_to_remove] = float("-inf")
30
+ return filtered_logits
31
+
32
+
33
+ def apply_top_p_optimized(logits, top_p):
34
+ probs = F.softmax(logits, dim=-1)
35
+ sorted_probs, sorted_indices = torch.sort(probs, descending=True, dim=-1)
36
+
37
+ cumulative_probs = torch.cumsum(sorted_probs, dim=-1)
38
+
39
+ sorted_indices_to_remove = cumulative_probs > top_p
40
+ sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
41
+ sorted_indices_to_remove[..., 0] = False
42
+
43
+ indices_to_remove = torch.zeros_like(logits, dtype=torch.bool).scatter_(
44
+ dim=-1, index=sorted_indices, src=sorted_indices_to_remove
45
+ )
46
+
47
+ logits[indices_to_remove] = float("-inf")
48
+ return logits
49
+
50
+
51
+ def apply_repetition_penalty_delay_pattern(
52
+ logits: torch.Tensor,
53
+ prev_tokens: torch.LongTensor,
54
+ penalty: float,
55
+ ):
56
+ """
57
+ logits: [B, H, V] or [N, V]
58
+ prev_tokens: [B, T, H] or [N, T] or [B, H]
59
+
60
+ Apply the repetition penalty independently for each H (VQ head).
61
+ """
62
+ if penalty == 1.0 or prev_tokens is None:
63
+ return logits
64
+
65
+ vocab_size = logits.size(-1)
66
+
67
+ # Case 1: regular [N, V] (text layer)
68
+ if logits.dim() == 2:
69
+ prev_tokens_flat = prev_tokens.reshape(-1)
70
+ unique_tokens = torch.unique(prev_tokens_flat)
71
+
72
+ token_logits = logits[:, unique_tokens]
73
+ pos_mask = token_logits > 0
74
+ token_logits[pos_mask] /= penalty
75
+ token_logits[~pos_mask] *= penalty
76
+ logits[:, unique_tokens] = token_logits
77
+ return logits
78
+
79
+ # Case 2: Delay Pattern audio [B, H, V]
80
+ assert logits.dim() == 3, "Delay Pattern audio logits must be [B, H, V]"
81
+ B, H, V = logits.shape
82
+
83
+ for h in range(H):
84
+ # prev_tokens_h: [B, T] or [B]
85
+ prev_tokens_h = prev_tokens[..., h].reshape(-1)
86
+ unique_tokens = torch.unique(prev_tokens_h)
87
+
88
+ if unique_tokens.numel() == 0:
89
+ continue
90
+
91
+ token_logits = logits[:, h, unique_tokens]
92
+ pos_mask = token_logits > 0
93
+ token_logits[pos_mask] /= penalty
94
+ token_logits[~pos_mask] *= penalty
95
+ logits[:, h, unique_tokens] = token_logits
96
+
97
+ return logits
98
+
99
+
100
+ def sample_token(
101
+ logits,
102
+ prev_tokens: Optional[torch.LongTensor] = None,
103
+ repetition_penalty: float = 1.0,
104
+ top_p=None,
105
+ top_k=None,
106
+ do_sample=True,
107
+ ):
108
+ vocab_size = logits.size(-1)
109
+
110
+ # ===== Repetition Penalty (before reshaping!) =====
111
+ if prev_tokens is not None and repetition_penalty != 1.0:
112
+ logits = apply_repetition_penalty_delay_pattern(
113
+ logits,
114
+ prev_tokens,
115
+ repetition_penalty,
116
+ )
117
+
118
+ if not do_sample:
119
+ return torch.argmax(logits, dim=-1)
120
+
121
+ # ===== Only flatten after this, for top-k / top-p / multinomial =====
122
+ original_shape = logits.shape
123
+ reshaped_logits = logits.view(-1, vocab_size)
124
+
125
+ if top_k is not None and top_k > 0:
126
+ reshaped_logits = apply_top_k(reshaped_logits, top_k)
127
+
128
+ if top_p is not None and top_p < 1.0:
129
+ reshaped_logits = apply_top_p_optimized(reshaped_logits, top_p)
130
+
131
+ probs = F.softmax(reshaped_logits, dim=-1)
132
+ next_tokens = torch.multinomial(probs, num_samples=1)
133
+
134
+ return next_tokens.view(original_shape[:-1])
135
+
136
+
137
+ def find_last_equal_C(tensor, C):
138
+ """
139
+ tensor: torch.Tensor of shape [batch_size, seq_len]
140
+ C: scalar value to match
141
+ Returns: torch.Tensor of shape [batch_size] with last indices
142
+ """
143
+ mask = (tensor == C).int() # Shape: [batch_size, seq_len], bool tensor
144
+ flipped_mask = mask.flip(dims=[1]) # Flip along sequence dimension
145
+ flipped_indices = flipped_mask.argmax(dim=1) # First True in flipped
146
+ seq_len = tensor.shape[1]
147
+ last_indices = (seq_len - 1) - flipped_indices # Convert to original indices
148
+
149
+ # Optional: Handle cases with no C (set to -1), though problem assumes existence
150
+ actual_values = tensor[torch.arange(tensor.shape[0]), last_indices]
151
+ no_match = actual_values != C
152
+ last_indices[no_match] = -1
153
+
154
+ return last_indices
merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
model-00001-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:749b82d07cebea77dd15af365e2efeb1dc0dd54537f7010795fdf42f520743a9
3
+ size 4932667368
model-00002-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4d891c8adb8bf9c135e8a44cd9f003f1dbf8b09278ced48d5af1fe7d989efb39
3
+ size 4915961640
model-00003-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:111013e05174e67748443eafdd4d64c051292d0e5fce2cac23a16988b661eea1
3
+ size 4983069760
model-00004-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:951ee88cf85996ee5c39441ace6e50f52894adad5da42ad2314b9a9db7f940b8
3
+ size 2148040304
model.safetensors.index.json ADDED
@@ -0,0 +1,471 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "total_parameters": 8489841664,
4
+ "total_size": 16979683328
5
+ },
6
+ "weight_map": {
7
+ "emb_ext.0.weight": "model-00004-of-00004.safetensors",
8
+ "emb_ext.1.weight": "model-00004-of-00004.safetensors",
9
+ "emb_ext.10.weight": "model-00004-of-00004.safetensors",
10
+ "emb_ext.11.weight": "model-00004-of-00004.safetensors",
11
+ "emb_ext.12.weight": "model-00004-of-00004.safetensors",
12
+ "emb_ext.13.weight": "model-00004-of-00004.safetensors",
13
+ "emb_ext.14.weight": "model-00004-of-00004.safetensors",
14
+ "emb_ext.15.weight": "model-00004-of-00004.safetensors",
15
+ "emb_ext.16.weight": "model-00004-of-00004.safetensors",
16
+ "emb_ext.17.weight": "model-00004-of-00004.safetensors",
17
+ "emb_ext.18.weight": "model-00004-of-00004.safetensors",
18
+ "emb_ext.19.weight": "model-00004-of-00004.safetensors",
19
+ "emb_ext.2.weight": "model-00004-of-00004.safetensors",
20
+ "emb_ext.20.weight": "model-00004-of-00004.safetensors",
21
+ "emb_ext.21.weight": "model-00004-of-00004.safetensors",
22
+ "emb_ext.22.weight": "model-00004-of-00004.safetensors",
23
+ "emb_ext.23.weight": "model-00004-of-00004.safetensors",
24
+ "emb_ext.24.weight": "model-00004-of-00004.safetensors",
25
+ "emb_ext.25.weight": "model-00004-of-00004.safetensors",
26
+ "emb_ext.26.weight": "model-00004-of-00004.safetensors",
27
+ "emb_ext.27.weight": "model-00004-of-00004.safetensors",
28
+ "emb_ext.28.weight": "model-00004-of-00004.safetensors",
29
+ "emb_ext.29.weight": "model-00004-of-00004.safetensors",
30
+ "emb_ext.3.weight": "model-00004-of-00004.safetensors",
31
+ "emb_ext.30.weight": "model-00004-of-00004.safetensors",
32
+ "emb_ext.31.weight": "model-00004-of-00004.safetensors",
33
+ "emb_ext.4.weight": "model-00004-of-00004.safetensors",
34
+ "emb_ext.5.weight": "model-00004-of-00004.safetensors",
35
+ "emb_ext.6.weight": "model-00004-of-00004.safetensors",
36
+ "emb_ext.7.weight": "model-00004-of-00004.safetensors",
37
+ "emb_ext.8.weight": "model-00004-of-00004.safetensors",
38
+ "emb_ext.9.weight": "model-00004-of-00004.safetensors",
39
+ "language_model.embed_tokens.weight": "model-00001-of-00004.safetensors",
40
+ "language_model.layers.0.input_layernorm.weight": "model-00001-of-00004.safetensors",
41
+ "language_model.layers.0.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
42
+ "language_model.layers.0.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
43
+ "language_model.layers.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
44
+ "language_model.layers.0.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
45
+ "language_model.layers.0.self_attn.k_norm.weight": "model-00001-of-00004.safetensors",
46
+ "language_model.layers.0.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
47
+ "language_model.layers.0.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
48
+ "language_model.layers.0.self_attn.q_norm.weight": "model-00001-of-00004.safetensors",
49
+ "language_model.layers.0.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
50
+ "language_model.layers.0.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
51
+ "language_model.layers.1.input_layernorm.weight": "model-00001-of-00004.safetensors",
52
+ "language_model.layers.1.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
53
+ "language_model.layers.1.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
54
+ "language_model.layers.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
55
+ "language_model.layers.1.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
56
+ "language_model.layers.1.self_attn.k_norm.weight": "model-00001-of-00004.safetensors",
57
+ "language_model.layers.1.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
58
+ "language_model.layers.1.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
59
+ "language_model.layers.1.self_attn.q_norm.weight": "model-00001-of-00004.safetensors",
60
+ "language_model.layers.1.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
61
+ "language_model.layers.1.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
62
+ "language_model.layers.10.input_layernorm.weight": "model-00002-of-00004.safetensors",
63
+ "language_model.layers.10.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
64
+ "language_model.layers.10.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
65
+ "language_model.layers.10.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
66
+ "language_model.layers.10.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
67
+ "language_model.layers.10.self_attn.k_norm.weight": "model-00002-of-00004.safetensors",
68
+ "language_model.layers.10.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
69
+ "language_model.layers.10.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
70
+ "language_model.layers.10.self_attn.q_norm.weight": "model-00002-of-00004.safetensors",
71
+ "language_model.layers.10.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
72
+ "language_model.layers.10.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
73
+ "language_model.layers.11.input_layernorm.weight": "model-00002-of-00004.safetensors",
74
+ "language_model.layers.11.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
75
+ "language_model.layers.11.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
76
+ "language_model.layers.11.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
77
+ "language_model.layers.11.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
78
+ "language_model.layers.11.self_attn.k_norm.weight": "model-00002-of-00004.safetensors",
79
+ "language_model.layers.11.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
80
+ "language_model.layers.11.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
81
+ "language_model.layers.11.self_attn.q_norm.weight": "model-00002-of-00004.safetensors",
82
+ "language_model.layers.11.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
83
+ "language_model.layers.11.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
84
+ "language_model.layers.12.input_layernorm.weight": "model-00002-of-00004.safetensors",
85
+ "language_model.layers.12.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
86
+ "language_model.layers.12.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
87
+ "language_model.layers.12.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
88
+ "language_model.layers.12.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
89
+ "language_model.layers.12.self_attn.k_norm.weight": "model-00002-of-00004.safetensors",
90
+ "language_model.layers.12.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
91
+ "language_model.layers.12.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
92
+ "language_model.layers.12.self_attn.q_norm.weight": "model-00002-of-00004.safetensors",
93
+ "language_model.layers.12.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
94
+ "language_model.layers.12.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
95
+ "language_model.layers.13.input_layernorm.weight": "model-00002-of-00004.safetensors",
96
+ "language_model.layers.13.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
97
+ "language_model.layers.13.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
98
+ "language_model.layers.13.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
99
+ "language_model.layers.13.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
100
+ "language_model.layers.13.self_attn.k_norm.weight": "model-00002-of-00004.safetensors",
101
+ "language_model.layers.13.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
102
+ "language_model.layers.13.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
103
+ "language_model.layers.13.self_attn.q_norm.weight": "model-00002-of-00004.safetensors",
104
+ "language_model.layers.13.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
105
+ "language_model.layers.13.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
106
+ "language_model.layers.14.input_layernorm.weight": "model-00002-of-00004.safetensors",
107
+ "language_model.layers.14.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
108
+ "language_model.layers.14.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
109
+ "language_model.layers.14.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
110
+ "language_model.layers.14.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
111
+ "language_model.layers.14.self_attn.k_norm.weight": "model-00002-of-00004.safetensors",
112
+ "language_model.layers.14.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
113
+ "language_model.layers.14.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
114
+ "language_model.layers.14.self_attn.q_norm.weight": "model-00002-of-00004.safetensors",
115
+ "language_model.layers.14.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
116
+ "language_model.layers.14.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
117
+ "language_model.layers.15.input_layernorm.weight": "model-00002-of-00004.safetensors",
118
+ "language_model.layers.15.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
119
+ "language_model.layers.15.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
120
+ "language_model.layers.15.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
121
+ "language_model.layers.15.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
122
+ "language_model.layers.15.self_attn.k_norm.weight": "model-00002-of-00004.safetensors",
123
+ "language_model.layers.15.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
124
+ "language_model.layers.15.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
125
+ "language_model.layers.15.self_attn.q_norm.weight": "model-00002-of-00004.safetensors",
126
+ "language_model.layers.15.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
127
+ "language_model.layers.15.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
128
+ "language_model.layers.16.input_layernorm.weight": "model-00002-of-00004.safetensors",
129
+ "language_model.layers.16.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
130
+ "language_model.layers.16.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
131
+ "language_model.layers.16.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
132
+ "language_model.layers.16.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
133
+ "language_model.layers.16.self_attn.k_norm.weight": "model-00002-of-00004.safetensors",
134
+ "language_model.layers.16.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
135
+ "language_model.layers.16.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
136
+ "language_model.layers.16.self_attn.q_norm.weight": "model-00002-of-00004.safetensors",
137
+ "language_model.layers.16.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
138
+ "language_model.layers.16.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
139
+ "language_model.layers.17.input_layernorm.weight": "model-00002-of-00004.safetensors",
140
+ "language_model.layers.17.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
141
+ "language_model.layers.17.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
142
+ "language_model.layers.17.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
143
+ "language_model.layers.17.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
144
+ "language_model.layers.17.self_attn.k_norm.weight": "model-00002-of-00004.safetensors",
145
+ "language_model.layers.17.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
146
+ "language_model.layers.17.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
147
+ "language_model.layers.17.self_attn.q_norm.weight": "model-00002-of-00004.safetensors",
148
+ "language_model.layers.17.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
149
+ "language_model.layers.17.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
150
+ "language_model.layers.18.input_layernorm.weight": "model-00002-of-00004.safetensors",
151
+ "language_model.layers.18.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
152
+ "language_model.layers.18.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
153
+ "language_model.layers.18.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
154
+ "language_model.layers.18.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
155
+ "language_model.layers.18.self_attn.k_norm.weight": "model-00002-of-00004.safetensors",
156
+ "language_model.layers.18.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
157
+ "language_model.layers.18.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
158
+ "language_model.layers.18.self_attn.q_norm.weight": "model-00002-of-00004.safetensors",
159
+ "language_model.layers.18.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
160
+ "language_model.layers.18.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
161
+ "language_model.layers.19.input_layernorm.weight": "model-00002-of-00004.safetensors",
162
+ "language_model.layers.19.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
163
+ "language_model.layers.19.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
164
+ "language_model.layers.19.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
165
+ "language_model.layers.19.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
166
+ "language_model.layers.19.self_attn.k_norm.weight": "model-00002-of-00004.safetensors",
167
+ "language_model.layers.19.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
168
+ "language_model.layers.19.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
169
+ "language_model.layers.19.self_attn.q_norm.weight": "model-00002-of-00004.safetensors",
170
+ "language_model.layers.19.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
171
+ "language_model.layers.19.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
172
+ "language_model.layers.2.input_layernorm.weight": "model-00001-of-00004.safetensors",
173
+ "language_model.layers.2.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
174
+ "language_model.layers.2.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
175
+ "language_model.layers.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
176
+ "language_model.layers.2.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
177
+ "language_model.layers.2.self_attn.k_norm.weight": "model-00001-of-00004.safetensors",
178
+ "language_model.layers.2.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
179
+ "language_model.layers.2.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
180
+ "language_model.layers.2.self_attn.q_norm.weight": "model-00001-of-00004.safetensors",
181
+ "language_model.layers.2.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
182
+ "language_model.layers.2.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
183
+ "language_model.layers.20.input_layernorm.weight": "model-00002-of-00004.safetensors",
184
+ "language_model.layers.20.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
185
+ "language_model.layers.20.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
186
+ "language_model.layers.20.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
187
+ "language_model.layers.20.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
188
+ "language_model.layers.20.self_attn.k_norm.weight": "model-00002-of-00004.safetensors",
189
+ "language_model.layers.20.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
190
+ "language_model.layers.20.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
191
+ "language_model.layers.20.self_attn.q_norm.weight": "model-00002-of-00004.safetensors",
192
+ "language_model.layers.20.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
193
+ "language_model.layers.20.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
194
+ "language_model.layers.21.input_layernorm.weight": "model-00002-of-00004.safetensors",
195
+ "language_model.layers.21.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
196
+ "language_model.layers.21.mlp.gate_proj.weight": "model-00002-of-00004.safetensors",
197
+ "language_model.layers.21.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
198
+ "language_model.layers.21.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
199
+ "language_model.layers.21.self_attn.k_norm.weight": "model-00002-of-00004.safetensors",
200
+ "language_model.layers.21.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
201
+ "language_model.layers.21.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
202
+ "language_model.layers.21.self_attn.q_norm.weight": "model-00002-of-00004.safetensors",
203
+ "language_model.layers.21.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
204
+ "language_model.layers.21.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
205
+ "language_model.layers.22.input_layernorm.weight": "model-00003-of-00004.safetensors",
206
+ "language_model.layers.22.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
207
+ "language_model.layers.22.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
208
+ "language_model.layers.22.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
209
+ "language_model.layers.22.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
210
+ "language_model.layers.22.self_attn.k_norm.weight": "model-00002-of-00004.safetensors",
211
+ "language_model.layers.22.self_attn.k_proj.weight": "model-00002-of-00004.safetensors",
212
+ "language_model.layers.22.self_attn.o_proj.weight": "model-00002-of-00004.safetensors",
213
+ "language_model.layers.22.self_attn.q_norm.weight": "model-00002-of-00004.safetensors",
214
+ "language_model.layers.22.self_attn.q_proj.weight": "model-00002-of-00004.safetensors",
215
+ "language_model.layers.22.self_attn.v_proj.weight": "model-00002-of-00004.safetensors",
216
+ "language_model.layers.23.input_layernorm.weight": "model-00003-of-00004.safetensors",
217
+ "language_model.layers.23.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
218
+ "language_model.layers.23.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
219
+ "language_model.layers.23.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
220
+ "language_model.layers.23.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
221
+ "language_model.layers.23.self_attn.k_norm.weight": "model-00003-of-00004.safetensors",
222
+ "language_model.layers.23.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
223
+ "language_model.layers.23.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
224
+ "language_model.layers.23.self_attn.q_norm.weight": "model-00003-of-00004.safetensors",
225
+ "language_model.layers.23.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
226
+ "language_model.layers.23.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
227
+ "language_model.layers.24.input_layernorm.weight": "model-00003-of-00004.safetensors",
228
+ "language_model.layers.24.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
229
+ "language_model.layers.24.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
230
+ "language_model.layers.24.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
231
+ "language_model.layers.24.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
232
+ "language_model.layers.24.self_attn.k_norm.weight": "model-00003-of-00004.safetensors",
233
+ "language_model.layers.24.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
234
+ "language_model.layers.24.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
235
+ "language_model.layers.24.self_attn.q_norm.weight": "model-00003-of-00004.safetensors",
236
+ "language_model.layers.24.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
237
+ "language_model.layers.24.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
238
+ "language_model.layers.25.input_layernorm.weight": "model-00003-of-00004.safetensors",
239
+ "language_model.layers.25.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
240
+ "language_model.layers.25.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
241
+ "language_model.layers.25.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
242
+ "language_model.layers.25.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
243
+ "language_model.layers.25.self_attn.k_norm.weight": "model-00003-of-00004.safetensors",
244
+ "language_model.layers.25.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
245
+ "language_model.layers.25.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
246
+ "language_model.layers.25.self_attn.q_norm.weight": "model-00003-of-00004.safetensors",
247
+ "language_model.layers.25.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
248
+ "language_model.layers.25.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
249
+ "language_model.layers.26.input_layernorm.weight": "model-00003-of-00004.safetensors",
250
+ "language_model.layers.26.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
251
+ "language_model.layers.26.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
252
+ "language_model.layers.26.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
253
+ "language_model.layers.26.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
254
+ "language_model.layers.26.self_attn.k_norm.weight": "model-00003-of-00004.safetensors",
255
+ "language_model.layers.26.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
256
+ "language_model.layers.26.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
257
+ "language_model.layers.26.self_attn.q_norm.weight": "model-00003-of-00004.safetensors",
258
+ "language_model.layers.26.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
259
+ "language_model.layers.26.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
260
+ "language_model.layers.27.input_layernorm.weight": "model-00003-of-00004.safetensors",
261
+ "language_model.layers.27.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
262
+ "language_model.layers.27.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
263
+ "language_model.layers.27.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
264
+ "language_model.layers.27.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
265
+ "language_model.layers.27.self_attn.k_norm.weight": "model-00003-of-00004.safetensors",
266
+ "language_model.layers.27.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
267
+ "language_model.layers.27.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
268
+ "language_model.layers.27.self_attn.q_norm.weight": "model-00003-of-00004.safetensors",
269
+ "language_model.layers.27.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
270
+ "language_model.layers.27.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
271
+ "language_model.layers.28.input_layernorm.weight": "model-00003-of-00004.safetensors",
272
+ "language_model.layers.28.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
273
+ "language_model.layers.28.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
274
+ "language_model.layers.28.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
275
+ "language_model.layers.28.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
276
+ "language_model.layers.28.self_attn.k_norm.weight": "model-00003-of-00004.safetensors",
277
+ "language_model.layers.28.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
278
+ "language_model.layers.28.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
279
+ "language_model.layers.28.self_attn.q_norm.weight": "model-00003-of-00004.safetensors",
280
+ "language_model.layers.28.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
281
+ "language_model.layers.28.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
282
+ "language_model.layers.29.input_layernorm.weight": "model-00003-of-00004.safetensors",
283
+ "language_model.layers.29.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
284
+ "language_model.layers.29.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
285
+ "language_model.layers.29.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
286
+ "language_model.layers.29.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
287
+ "language_model.layers.29.self_attn.k_norm.weight": "model-00003-of-00004.safetensors",
288
+ "language_model.layers.29.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
289
+ "language_model.layers.29.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
290
+ "language_model.layers.29.self_attn.q_norm.weight": "model-00003-of-00004.safetensors",
291
+ "language_model.layers.29.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
292
+ "language_model.layers.29.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
293
+ "language_model.layers.3.input_layernorm.weight": "model-00001-of-00004.safetensors",
294
+ "language_model.layers.3.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
295
+ "language_model.layers.3.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
296
+ "language_model.layers.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
297
+ "language_model.layers.3.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
298
+ "language_model.layers.3.self_attn.k_norm.weight": "model-00001-of-00004.safetensors",
299
+ "language_model.layers.3.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
300
+ "language_model.layers.3.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
301
+ "language_model.layers.3.self_attn.q_norm.weight": "model-00001-of-00004.safetensors",
302
+ "language_model.layers.3.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
303
+ "language_model.layers.3.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
304
+ "language_model.layers.30.input_layernorm.weight": "model-00003-of-00004.safetensors",
305
+ "language_model.layers.30.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
306
+ "language_model.layers.30.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
307
+ "language_model.layers.30.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
308
+ "language_model.layers.30.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
309
+ "language_model.layers.30.self_attn.k_norm.weight": "model-00003-of-00004.safetensors",
310
+ "language_model.layers.30.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
311
+ "language_model.layers.30.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
312
+ "language_model.layers.30.self_attn.q_norm.weight": "model-00003-of-00004.safetensors",
313
+ "language_model.layers.30.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
314
+ "language_model.layers.30.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
315
+ "language_model.layers.31.input_layernorm.weight": "model-00003-of-00004.safetensors",
316
+ "language_model.layers.31.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
317
+ "language_model.layers.31.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
318
+ "language_model.layers.31.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
319
+ "language_model.layers.31.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
320
+ "language_model.layers.31.self_attn.k_norm.weight": "model-00003-of-00004.safetensors",
321
+ "language_model.layers.31.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
322
+ "language_model.layers.31.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
323
+ "language_model.layers.31.self_attn.q_norm.weight": "model-00003-of-00004.safetensors",
324
+ "language_model.layers.31.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
325
+ "language_model.layers.31.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
326
+ "language_model.layers.32.input_layernorm.weight": "model-00003-of-00004.safetensors",
327
+ "language_model.layers.32.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
328
+ "language_model.layers.32.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
329
+ "language_model.layers.32.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
330
+ "language_model.layers.32.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
331
+ "language_model.layers.32.self_attn.k_norm.weight": "model-00003-of-00004.safetensors",
332
+ "language_model.layers.32.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
333
+ "language_model.layers.32.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
334
+ "language_model.layers.32.self_attn.q_norm.weight": "model-00003-of-00004.safetensors",
335
+ "language_model.layers.32.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
336
+ "language_model.layers.32.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
337
+ "language_model.layers.33.input_layernorm.weight": "model-00003-of-00004.safetensors",
338
+ "language_model.layers.33.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
339
+ "language_model.layers.33.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
340
+ "language_model.layers.33.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
341
+ "language_model.layers.33.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
342
+ "language_model.layers.33.self_attn.k_norm.weight": "model-00003-of-00004.safetensors",
343
+ "language_model.layers.33.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
344
+ "language_model.layers.33.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
345
+ "language_model.layers.33.self_attn.q_norm.weight": "model-00003-of-00004.safetensors",
346
+ "language_model.layers.33.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
347
+ "language_model.layers.33.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
348
+ "language_model.layers.34.input_layernorm.weight": "model-00003-of-00004.safetensors",
349
+ "language_model.layers.34.mlp.down_proj.weight": "model-00003-of-00004.safetensors",
350
+ "language_model.layers.34.mlp.gate_proj.weight": "model-00003-of-00004.safetensors",
351
+ "language_model.layers.34.mlp.up_proj.weight": "model-00003-of-00004.safetensors",
352
+ "language_model.layers.34.post_attention_layernorm.weight": "model-00003-of-00004.safetensors",
353
+ "language_model.layers.34.self_attn.k_norm.weight": "model-00003-of-00004.safetensors",
354
+ "language_model.layers.34.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
355
+ "language_model.layers.34.self_attn.o_proj.weight": "model-00003-of-00004.safetensors",
356
+ "language_model.layers.34.self_attn.q_norm.weight": "model-00003-of-00004.safetensors",
357
+ "language_model.layers.34.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
358
+ "language_model.layers.34.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
359
+ "language_model.layers.35.input_layernorm.weight": "model-00004-of-00004.safetensors",
360
+ "language_model.layers.35.mlp.down_proj.weight": "model-00004-of-00004.safetensors",
361
+ "language_model.layers.35.mlp.gate_proj.weight": "model-00004-of-00004.safetensors",
362
+ "language_model.layers.35.mlp.up_proj.weight": "model-00004-of-00004.safetensors",
363
+ "language_model.layers.35.post_attention_layernorm.weight": "model-00004-of-00004.safetensors",
364
+ "language_model.layers.35.self_attn.k_norm.weight": "model-00004-of-00004.safetensors",
365
+ "language_model.layers.35.self_attn.k_proj.weight": "model-00003-of-00004.safetensors",
366
+ "language_model.layers.35.self_attn.o_proj.weight": "model-00004-of-00004.safetensors",
367
+ "language_model.layers.35.self_attn.q_norm.weight": "model-00004-of-00004.safetensors",
368
+ "language_model.layers.35.self_attn.q_proj.weight": "model-00003-of-00004.safetensors",
369
+ "language_model.layers.35.self_attn.v_proj.weight": "model-00003-of-00004.safetensors",
370
+ "language_model.layers.4.input_layernorm.weight": "model-00001-of-00004.safetensors",
371
+ "language_model.layers.4.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
372
+ "language_model.layers.4.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
373
+ "language_model.layers.4.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
374
+ "language_model.layers.4.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
375
+ "language_model.layers.4.self_attn.k_norm.weight": "model-00001-of-00004.safetensors",
376
+ "language_model.layers.4.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
377
+ "language_model.layers.4.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
378
+ "language_model.layers.4.self_attn.q_norm.weight": "model-00001-of-00004.safetensors",
379
+ "language_model.layers.4.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
380
+ "language_model.layers.4.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
381
+ "language_model.layers.5.input_layernorm.weight": "model-00001-of-00004.safetensors",
382
+ "language_model.layers.5.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
383
+ "language_model.layers.5.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
384
+ "language_model.layers.5.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
385
+ "language_model.layers.5.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
386
+ "language_model.layers.5.self_attn.k_norm.weight": "model-00001-of-00004.safetensors",
387
+ "language_model.layers.5.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
388
+ "language_model.layers.5.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
389
+ "language_model.layers.5.self_attn.q_norm.weight": "model-00001-of-00004.safetensors",
390
+ "language_model.layers.5.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
391
+ "language_model.layers.5.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
392
+ "language_model.layers.6.input_layernorm.weight": "model-00001-of-00004.safetensors",
393
+ "language_model.layers.6.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
394
+ "language_model.layers.6.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
395
+ "language_model.layers.6.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
396
+ "language_model.layers.6.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
397
+ "language_model.layers.6.self_attn.k_norm.weight": "model-00001-of-00004.safetensors",
398
+ "language_model.layers.6.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
399
+ "language_model.layers.6.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
400
+ "language_model.layers.6.self_attn.q_norm.weight": "model-00001-of-00004.safetensors",
401
+ "language_model.layers.6.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
402
+ "language_model.layers.6.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
403
+ "language_model.layers.7.input_layernorm.weight": "model-00001-of-00004.safetensors",
404
+ "language_model.layers.7.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
405
+ "language_model.layers.7.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
406
+ "language_model.layers.7.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
407
+ "language_model.layers.7.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
408
+ "language_model.layers.7.self_attn.k_norm.weight": "model-00001-of-00004.safetensors",
409
+ "language_model.layers.7.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
410
+ "language_model.layers.7.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
411
+ "language_model.layers.7.self_attn.q_norm.weight": "model-00001-of-00004.safetensors",
412
+ "language_model.layers.7.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
413
+ "language_model.layers.7.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
414
+ "language_model.layers.8.input_layernorm.weight": "model-00001-of-00004.safetensors",
415
+ "language_model.layers.8.mlp.down_proj.weight": "model-00001-of-00004.safetensors",
416
+ "language_model.layers.8.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
417
+ "language_model.layers.8.mlp.up_proj.weight": "model-00001-of-00004.safetensors",
418
+ "language_model.layers.8.post_attention_layernorm.weight": "model-00001-of-00004.safetensors",
419
+ "language_model.layers.8.self_attn.k_norm.weight": "model-00001-of-00004.safetensors",
420
+ "language_model.layers.8.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
421
+ "language_model.layers.8.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
422
+ "language_model.layers.8.self_attn.q_norm.weight": "model-00001-of-00004.safetensors",
423
+ "language_model.layers.8.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
424
+ "language_model.layers.8.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
425
+ "language_model.layers.9.input_layernorm.weight": "model-00002-of-00004.safetensors",
426
+ "language_model.layers.9.mlp.down_proj.weight": "model-00002-of-00004.safetensors",
427
+ "language_model.layers.9.mlp.gate_proj.weight": "model-00001-of-00004.safetensors",
428
+ "language_model.layers.9.mlp.up_proj.weight": "model-00002-of-00004.safetensors",
429
+ "language_model.layers.9.post_attention_layernorm.weight": "model-00002-of-00004.safetensors",
430
+ "language_model.layers.9.self_attn.k_norm.weight": "model-00001-of-00004.safetensors",
431
+ "language_model.layers.9.self_attn.k_proj.weight": "model-00001-of-00004.safetensors",
432
+ "language_model.layers.9.self_attn.o_proj.weight": "model-00001-of-00004.safetensors",
433
+ "language_model.layers.9.self_attn.q_norm.weight": "model-00001-of-00004.safetensors",
434
+ "language_model.layers.9.self_attn.q_proj.weight": "model-00001-of-00004.safetensors",
435
+ "language_model.layers.9.self_attn.v_proj.weight": "model-00001-of-00004.safetensors",
436
+ "language_model.norm.weight": "model-00004-of-00004.safetensors",
437
+ "lm_heads.0.weight": "model-00004-of-00004.safetensors",
438
+ "lm_heads.1.weight": "model-00004-of-00004.safetensors",
439
+ "lm_heads.10.weight": "model-00004-of-00004.safetensors",
440
+ "lm_heads.11.weight": "model-00004-of-00004.safetensors",
441
+ "lm_heads.12.weight": "model-00004-of-00004.safetensors",
442
+ "lm_heads.13.weight": "model-00004-of-00004.safetensors",
443
+ "lm_heads.14.weight": "model-00004-of-00004.safetensors",
444
+ "lm_heads.15.weight": "model-00004-of-00004.safetensors",
445
+ "lm_heads.16.weight": "model-00004-of-00004.safetensors",
446
+ "lm_heads.17.weight": "model-00004-of-00004.safetensors",
447
+ "lm_heads.18.weight": "model-00004-of-00004.safetensors",
448
+ "lm_heads.19.weight": "model-00004-of-00004.safetensors",
449
+ "lm_heads.2.weight": "model-00004-of-00004.safetensors",
450
+ "lm_heads.20.weight": "model-00004-of-00004.safetensors",
451
+ "lm_heads.21.weight": "model-00004-of-00004.safetensors",
452
+ "lm_heads.22.weight": "model-00004-of-00004.safetensors",
453
+ "lm_heads.23.weight": "model-00004-of-00004.safetensors",
454
+ "lm_heads.24.weight": "model-00004-of-00004.safetensors",
455
+ "lm_heads.25.weight": "model-00004-of-00004.safetensors",
456
+ "lm_heads.26.weight": "model-00004-of-00004.safetensors",
457
+ "lm_heads.27.weight": "model-00004-of-00004.safetensors",
458
+ "lm_heads.28.weight": "model-00004-of-00004.safetensors",
459
+ "lm_heads.29.weight": "model-00004-of-00004.safetensors",
460
+ "lm_heads.3.weight": "model-00004-of-00004.safetensors",
461
+ "lm_heads.30.weight": "model-00004-of-00004.safetensors",
462
+ "lm_heads.31.weight": "model-00004-of-00004.safetensors",
463
+ "lm_heads.32.weight": "model-00004-of-00004.safetensors",
464
+ "lm_heads.4.weight": "model-00004-of-00004.safetensors",
465
+ "lm_heads.5.weight": "model-00004-of-00004.safetensors",
466
+ "lm_heads.6.weight": "model-00004-of-00004.safetensors",
467
+ "lm_heads.7.weight": "model-00004-of-00004.safetensors",
468
+ "lm_heads.8.weight": "model-00004-of-00004.safetensors",
469
+ "lm_heads.9.weight": "model-00004-of-00004.safetensors"
470
+ }
471
+ }
modeling_moss_tts.py ADDED
@@ -0,0 +1,525 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2026 OpenMOSS and the HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """ Modeling classes for MossTTSDelay. """
16
+
17
+ from dataclasses import dataclass
18
+ from typing import List, Optional, Tuple, Union
19
+ from tqdm import tqdm
20
+
21
+ import torch
22
+ import torch.nn as nn
23
+ from torch.nn import CrossEntropyLoss
24
+
25
+ from transformers.modeling_utils import PreTrainedModel
26
+ from transformers.modeling_outputs import ModelOutput
27
+ from transformers.utils import (
28
+ add_start_docstrings,
29
+ add_start_docstrings_to_model_forward,
30
+ logging,
31
+ replace_return_docstrings,
32
+ )
33
+ from transformers.cache_utils import Cache
34
+ from transformers.models.qwen3 import Qwen3Model
35
+ from transformers import initialization as init
36
+
37
+ from .configuration_moss_tts import MossTTSDelayConfig
38
+ from .inference_utils import sample_token, find_last_equal_C
39
+
40
+ try:
41
+ from .processing_moss_tts import UserMessage, AssistantMessage, MossTTSDelayProcessor
42
+ except Exception:
43
+ UserMessage = None
44
+ AssistantMessage = None
45
+ MossTTSDelayProcessor = None
46
+
47
+ logger = logging.get_logger(__name__)
48
+
49
+ _CONFIG_FOR_DOC = "MossTTSDelayConfig"
50
+
51
+
52
+ @dataclass
53
+ class MossTTSDelayOutputWithPast(ModelOutput):
54
+ """
55
+ Base class for model's outputs that may also contain a past key/values (to speed up sequential decoding).
56
+
57
+ Args:
58
+ loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
59
+ Weighted sum of channel losses.
60
+ all_sum_losses (`torch.FloatTensor` of shape `(batch_size, n_vq + 1)`, *optional*):
61
+ Sum of losses for each sample and each channel before averaging.
62
+ all_token_nums (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
63
+ Number of non-masked tokens per sample.
64
+ sample_losses (`torch.FloatTensor` of shape `(batch_size,)`, *optional*):
65
+ Loss per sample.
66
+ channel_losses (`torch.FloatTensor` of shape `(n_vq + 1,)`, *optional*):
67
+ Loss per channel (text head + vq heads).
68
+ logits (`List[torch.FloatTensor]`, *optional*):
69
+ List of prediction scores from each head.
70
+ past_key_values (`Cache`, *optional*):
71
+ Pre-computed hidden-states (key and values in the self-attention blocks).
72
+ hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed):
73
+ Tuple of torch.FloatTensor (one for the output of the embeddings, if the model has an embedding layer, +
74
+ one for the output of each layer).
75
+ attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed):
76
+ Tuple of torch.FloatTensor (one for each layer) of the attention weights.
77
+ """
78
+ loss: Optional[torch.FloatTensor] = None
79
+ all_sum_losses: Optional[torch.FloatTensor] = None
80
+ all_token_nums: Optional[torch.LongTensor] = None
81
+ sample_losses: Optional[torch.FloatTensor] = None
82
+ channel_losses: Optional[torch.FloatTensor] = None
83
+ logits: Optional[List[torch.FloatTensor]] = None
84
+ past_key_values: Optional[Cache] = None
85
+ hidden_states: Optional[Tuple[torch.FloatTensor]] = None
86
+ attentions: Optional[Tuple[torch.FloatTensor]] = None
87
+
88
+
89
+
90
+
91
+ class MossTTSDelayPreTrainedModel(PreTrainedModel):
92
+ config_class = MossTTSDelayConfig
93
+ base_model_prefix = "model"
94
+ supports_gradient_checkpointing = True
95
+ _no_split_modules = ["Qwen3DecoderLayer"]
96
+ _skip_keys_device_placement = "past_key_values"
97
+ _supports_flash_attn = True
98
+ _supports_flash_attn_2 = True
99
+ _supports_sdpa = True
100
+ _supports_flex_attn = True
101
+
102
+ def _init_weights(self, module):
103
+ """
104
+ Transformers 5.0+ safe init:
105
+ - MUST use transformers.initialization helpers
106
+ - MUST respect param._is_hf_initialized to avoid overwriting ckpt-loaded params
107
+ """
108
+ # Let HF handle its standard modules first (LayerNorm, Linear, Embedding, etc.)
109
+ super()._init_weights(module)
110
+
111
+ # Pick a std consistent with HF conventions
112
+ # Prefer model/text config initializer_range if present.
113
+ std = None
114
+ if hasattr(self.config, "initializer_range"):
115
+ std = self.config.initializer_range
116
+ elif hasattr(self.config, "language_config") and hasattr(self.config.language_config, "initializer_range"):
117
+ std = self.config.language_config.initializer_range
118
+ else:
119
+ std = 0.02
120
+
121
+ # Initialize extra audio embeddings
122
+ if isinstance(module, nn.Embedding):
123
+ # Only touch our extra embeddings (avoid double touching LM's embeddings if not desired)
124
+ # If you prefer, you can skip this check and rely on super()._init_weights for all embeddings.
125
+ if getattr(module, "num_embeddings", None) == self.config.audio_vocab_size + 1:
126
+ init.normal_(module.weight, mean=0.0, std=std)
127
+ # If you later set padding_idx, you must explicitly zero it (and respect _is_hf_initialized!)
128
+ # init.zeros_ will internally check param flags, but slicing needs manual care.
129
+
130
+ # Initialize multi-head projections you added
131
+ if isinstance(module, nn.Linear):
132
+ # For your lm_heads, super()._init_weights already covers typical Linear.
133
+ # This block is only needed if you have custom Linear variants later.
134
+ pass
135
+
136
+
137
+
138
+ MOSSTTS_START_DOCSTRING = r"""
139
+ This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
140
+ library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
141
+ etc.)
142
+
143
+ This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
144
+ Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
145
+ and behavior.
146
+
147
+ Parameters:
148
+ config ([`MossTTSDelayConfig`]):
149
+ Model configuration class with all the parameters of the model. Initializing with a config file does not
150
+ load the weights associated with the model, only the configuration. Check out the
151
+ [`~PreTrainedModel.from_pretrained`] method to load the model weights.
152
+ """
153
+
154
+
155
+ @add_start_docstrings(
156
+ "The MossTTSDelay Model architecture tailored for Text-to-Speech generation with multi-head VQ prediction.",
157
+ MOSSTTS_START_DOCSTRING,
158
+ )
159
+ class MossTTSDelayModel(MossTTSDelayPreTrainedModel):
160
+ UserMessage = UserMessage
161
+ AssistantMessage = AssistantMessage
162
+ Processor = MossTTSDelayProcessor
163
+
164
+ def __init__(self, config: MossTTSDelayConfig):
165
+ super().__init__(config)
166
+ self.config = config
167
+
168
+ config.language_config.torch_dtype = config.torch_dtype
169
+
170
+ self.language_model = Qwen3Model(config.language_config)
171
+
172
+ # Audio VQ Embeddings (Extra channels)
173
+ # Note: input_ids[..., 0] uses Qwen's embedding.
174
+ # input_ids[..., 1:] use these extensions.
175
+ self.emb_ext = nn.ModuleList()
176
+ for vq_idx in range(self.config.n_vq):
177
+ # Add +1 for potential padding/special tokens logic if strictly required by upstream data prep
178
+ self.emb_ext.append(
179
+ nn.Embedding(self.config.audio_vocab_size + 1, config.language_config.hidden_size, padding_idx=None)
180
+ )
181
+
182
+ # Multi-Head Prediction Layers
183
+ # Head 0: Main language head
184
+ # Head 1..N: Audio VQ heads
185
+ self.lm_heads = nn.ModuleList([
186
+ nn.Linear(config.language_config.hidden_size, config.language_config.vocab_size, bias=False)
187
+ ])
188
+ for vq_idx in range(self.config.n_vq):
189
+ self.lm_heads.append(
190
+ nn.Linear(config.language_config.hidden_size, self.config.audio_vocab_size + 1, bias=False)
191
+ )
192
+
193
+ # Initialize weights and apply final processing
194
+ self.post_init()
195
+
196
+ def get_input_embeddings(self, input_ids: torch.LongTensor) -> torch.Tensor:
197
+ """
198
+ Computes the combined embeddings from text and multiple audio VQ channels.
199
+
200
+ Args:
201
+ input_ids: Shape (Batch, Seq_Len, 1 + n_vq)
202
+ """
203
+ # Base Text/Content Embedding
204
+ # input_ids[..., 0] is standard text or semantic tokens
205
+ inputs_embeds = self.language_model.get_input_embeddings()(input_ids[..., 0])
206
+
207
+ # Add VQ Embeddings
208
+ for i, embed_layer in enumerate(self.emb_ext):
209
+ # i corresponds to channel i+1 in input_ids
210
+ # We assume the data pipeline ensures indices are within range
211
+ inputs_embeds = inputs_embeds + embed_layer(input_ids[..., i + 1])
212
+
213
+ return inputs_embeds
214
+
215
+ def set_input_embeddings(self, value):
216
+ self.language_model.embed_tokens = value
217
+
218
+ def get_output_embeddings(self):
219
+ # Returning a list of heads might break some HF utilities expecting a single head.
220
+ # However, for custom models, this is acceptable.
221
+ return self.lm_heads
222
+
223
+ @add_start_docstrings_to_model_forward(MOSSTTS_START_DOCSTRING)
224
+ @replace_return_docstrings(output_type=MossTTSDelayOutputWithPast, config_class=_CONFIG_FOR_DOC)
225
+ def forward(
226
+ self,
227
+ input_ids: Optional[torch.LongTensor] = None,
228
+ attention_mask: Optional[torch.Tensor] = None,
229
+ position_ids: Optional[torch.LongTensor] = None,
230
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
231
+ inputs_embeds: Optional[torch.FloatTensor] = None,
232
+ labels: Optional[torch.LongTensor] = None,
233
+ use_cache: Optional[bool] = None,
234
+ output_attentions: Optional[bool] = None,
235
+ cache_position: Optional[torch.LongTensor] = None,
236
+ hidden_out_layers: Optional[List[int]] = None,
237
+ channelwise_loss_weight: Optional[List[float]] = None,
238
+ **kwargs,
239
+ ) -> Union[Tuple, MossTTSDelayOutputWithPast]:
240
+ r"""
241
+ Args:
242
+ input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length, 1 + n_vq)`):
243
+ Indices of input sequence tokens in the vocabulary.
244
+ Dimension 2 contains: [Text/Semantics, VQ_0, VQ_1, ..., VQ_N].
245
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length, 1 + n_vq)`, *optional*):
246
+ Labels for computing the masked language modeling loss.
247
+ channelwise_loss_weight (`List[float]`, *optional*):
248
+ Manual weights for summing losses across different heads (Text vs Audio channels).
249
+
250
+ Returns:
251
+ """
252
+
253
+ if len(input_ids.shape) != 3 or input_ids.shape[-1] != self.config.n_vq + 1:
254
+ raise ValueError("`Input_ids`'s shape should be exactly (batch_size, sequence_length, 1 + n_vq).")
255
+
256
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
257
+
258
+ # 1. Prepare Embeddings
259
+ if inputs_embeds is None:
260
+ inputs_embeds = self.get_input_embeddings(input_ids)
261
+
262
+ # 2. Backbone Forward
263
+ # Qwen3Model outputs standard CausalLMOutputWithPast or similar
264
+ outputs = self.language_model(
265
+ input_ids=None, # Passed via inputs_embeds
266
+ position_ids=position_ids,
267
+ attention_mask=attention_mask,
268
+ past_key_values=past_key_values,
269
+ inputs_embeds=inputs_embeds,
270
+ use_cache=use_cache,
271
+ output_attentions=output_attentions,
272
+ output_hidden_states=True, # Always need hidden states for multi-head projection
273
+ return_dict=True,
274
+ cache_position=cache_position,
275
+ **kwargs,
276
+ )
277
+
278
+ # 3. Handle specific layer outputs if requested (Delay Pattern often requires features from specific layers)
279
+ last_hidden_state = outputs.last_hidden_state
280
+ if hidden_out_layers is None:
281
+ # Default to using the last layer for all heads
282
+ # In some architectures (like MusicGen), different codebooks come from different transformer layers.
283
+ # Here we default to the final layer as per original code behavior [-1] * (n + 1).
284
+ hidden_states_for_heads = [last_hidden_state] * (len(self.lm_heads))
285
+ else:
286
+ # If hidden_out_layers is provided (e.g. [-1, -2, -3...]), fetch them from all_hidden_states
287
+ # Note: outputs.hidden_states includes embedding output at index 0 usually.
288
+ all_hs = outputs.hidden_states
289
+ hidden_states_for_heads = [all_hs[idx] for idx in hidden_out_layers]
290
+
291
+ # 4. Project to Logits (Multi-Head)
292
+ layer_logits = []
293
+ for i, (hs, head) in enumerate(zip(hidden_states_for_heads, self.lm_heads)):
294
+ logits = head(hs)
295
+ # Original code logic: Mask the last token index for audio heads (indices > 0)
296
+ # This implies the vocab size is (N+1) but the model shouldn't predict the (N+1)-th token
297
+ # (perhaps reserved for padding in the input but invalid for prediction).
298
+ if i > 0:
299
+ logits[..., -1] = float("-inf")
300
+ layer_logits.append(logits)
301
+
302
+ # 5. Loss Calculation
303
+ loss = None
304
+ all_sum_losses = None
305
+ all_token_nums = None
306
+ sample_losses = None
307
+ channel_losses = None
308
+
309
+ if labels is not None:
310
+ # Ensure labels match input shape rank (B, S, C)
311
+ if labels.dim() != 3:
312
+ raise ValueError(f"Labels must have rank 3 (B, S, C), got {labels.shape}")
313
+
314
+ batch_size = labels.size(0)
315
+ n_heads = len(layer_logits)
316
+
317
+ # Container for per-sample, per-channel losses
318
+ # Shape: [Batch, n_heads]
319
+ all_sum_losses_list = []
320
+
321
+ # Count valid tokens (not -100) per sample.
322
+ # Note: Assuming mask is consistent across channels or we take sum over dim 1 (seq)
323
+ # Usually strict masking means checking one channel or all.
324
+ # Original code: torch.sum(labels != -100, dim=1) -> [B, C]
325
+ all_token_nums = torch.sum(labels != -100, dim=1)
326
+
327
+ for i, logits in enumerate(layer_logits):
328
+ # logits: [B, S, V]
329
+ # cur_labels: [B, S]
330
+ cur_labels = labels[..., i]
331
+
332
+ # Flatten for CrossEntropy
333
+ # logits: [B*S, V], labels: [B*S]
334
+ loss_fct = CrossEntropyLoss(reduction='none')
335
+ vocab_size = logits.size(-1)
336
+
337
+ reshaped_logits = logits.view(-1, vocab_size)
338
+ reshaped_labels = cur_labels.contiguous().view(-1)
339
+
340
+ # Calculate loss per token
341
+ per_token_loss = loss_fct(reshaped_logits, reshaped_labels)
342
+
343
+ # Reshape back to [B, S] and sum over Sequence dimension to get per-sample loss
344
+ per_token_loss = per_token_loss.view(batch_size, -1)
345
+ per_sample_loss = torch.sum(per_token_loss, dim=-1) # [B]
346
+
347
+ all_sum_losses_list.append(per_sample_loss)
348
+
349
+ # Stack to [B, n_heads]
350
+ all_sum_losses = torch.stack(all_sum_losses_list, dim=1)
351
+
352
+ # Weighted Loss Aggregation
353
+ if channelwise_loss_weight is not None:
354
+ if len(channelwise_loss_weight) != n_heads:
355
+ raise ValueError(f"channelwise_loss_weight length {len(channelwise_loss_weight)} != {n_heads}")
356
+
357
+ w_tensor = torch.tensor(channelwise_loss_weight, device=all_sum_losses.device, dtype=all_sum_losses.dtype)
358
+
359
+ # Sample losses: Weighted sum over channels per sample / Total weight
360
+ # Normalize by token count per channel
361
+ # Avoid division by zero with epsilon or mask
362
+ token_counts_safe = all_token_nums.float().clamp(min=1.0)
363
+
364
+ normalized_losses = all_sum_losses / token_counts_safe
365
+ sample_losses = (normalized_losses * w_tensor).sum(dim=1) / w_tensor.sum()
366
+
367
+ # Channel losses: Sum over batch / Sum tokens over batch
368
+ total_loss_per_channel = all_sum_losses.sum(dim=0)
369
+ total_tokens_per_channel = all_token_nums.sum(dim=0).float().clamp(min=1.0)
370
+ channel_losses = total_loss_per_channel / total_tokens_per_channel
371
+
372
+ # Final scalar loss
373
+ loss = (channel_losses * w_tensor).sum() / w_tensor.sum()
374
+ else:
375
+ # Default average if no weights provided
376
+ total_tokens = all_token_nums.sum().float().clamp(min=1.0)
377
+ loss = all_sum_losses.sum() / total_tokens
378
+ channel_losses = all_sum_losses.sum(dim=0) / all_token_nums.sum(dim=0).clamp(min=1.0)
379
+
380
+ return MossTTSDelayOutputWithPast(
381
+ loss=loss,
382
+ all_sum_losses=all_sum_losses,
383
+ all_token_nums=all_token_nums,
384
+ sample_losses=sample_losses,
385
+ channel_losses=channel_losses,
386
+ logits=layer_logits,
387
+ past_key_values=outputs.past_key_values,
388
+ hidden_states=outputs.hidden_states,
389
+ attentions=outputs.attentions,
390
+ )
391
+
392
+ @torch.inference_mode()
393
+ def generate(
394
+ self,
395
+ input_ids: torch.LongTensor,
396
+ attention_mask: Optional[torch.Tensor] = None,
397
+ max_new_tokens: int = 1000,
398
+ text_temperature: float = 1.5,
399
+ text_top_p: float = 1.0,
400
+ text_top_k: int = 50,
401
+ audio_temperature: float = 1.7,
402
+ audio_top_p: float = 0.8,
403
+ audio_top_k: int = 25,
404
+ audio_repetition_penalty: float = 1.0,
405
+ ):
406
+ if text_temperature > 0:
407
+ text_do_sample = True
408
+ else:
409
+ text_temperature = 1
410
+ text_do_sample = False
411
+ if audio_temperature > 0:
412
+ audio_do_sample = True
413
+ else:
414
+ audio_temperature = 1
415
+ audio_do_sample = False
416
+
417
+ past_key_values = None
418
+ device = input_ids.device
419
+ current_input_ids = input_ids
420
+ current_attention_mask = attention_mask
421
+ batch_size, seq_len, n_vq = input_ids.shape
422
+ n_vq -= 1
423
+
424
+ generation_ids = input_ids[:]
425
+ is_stopping = torch.zeros(batch_size, dtype=torch.bool, device=device)
426
+
427
+ audio_lengths = torch.zeros(batch_size, dtype=torch.int64, device=device)
428
+ torch_int64_max = torch.iinfo(torch.int64).max
429
+ delayed_lengths = torch.full((batch_size,), torch_int64_max, dtype=torch.int64, device=device)
430
+
431
+ is_continuation = (input_ids[:, -1, 0] == self.config.audio_start_token_id) | (input_ids[:, -1, 0] == self.config.audio_assistant_gen_slot_token_id)
432
+ audio_start_indices = find_last_equal_C(input_ids[..., 0], self.config.audio_start_token_id)
433
+ audio_start_mask = is_continuation & (audio_start_indices != -1)
434
+ audio_lengths[audio_start_mask] = seq_len - audio_start_indices[audio_start_mask]
435
+
436
+ is_audio = audio_start_mask.clone()
437
+
438
+ pre_exclude_mask0 = torch.tensor([self.config.pad_token_id, self.config.audio_assistant_gen_slot_token_id, self.config.audio_assistant_delay_slot_token_id, self.config.audio_end_token_id], device=device)
439
+ pre_exclude_mask1 = torch.ones(self.config.language_config.vocab_size, device=device).bool()
440
+ pre_exclude_mask1[[self.config.audio_assistant_gen_slot_token_id, self.config.audio_assistant_delay_slot_token_id]] = False
441
+
442
+ for time_step in tqdm(range(max_new_tokens), desc=f"Generating bs{batch_size} ..."):
443
+ outputs = self(
444
+ input_ids=current_input_ids,
445
+ attention_mask=current_attention_mask,
446
+ past_key_values=past_key_values,
447
+ use_cache=True,
448
+ )
449
+ past_key_values = outputs.past_key_values
450
+
451
+ next_token_logits = [logit[:, -1, :] / text_temperature if logit_idx == 0 else logit[:, -1, :] / audio_temperature for logit_idx, logit in enumerate(outputs.logits)] # List, len=n_vq+1, [batch_size, 1, vocab_size];
452
+ next_token_logits[0] = next_token_logits[0].clone()
453
+ next_text_token = torch.full((batch_size,), self.config.pad_token_id, device=device)
454
+ next_text_token[~is_stopping & (delayed_lengths < n_vq)] = self.config.audio_assistant_delay_slot_token_id
455
+ is_audio_eos = ~is_stopping & (delayed_lengths == n_vq)
456
+ next_text_token[is_audio_eos] = self.config.audio_end_token_id
457
+ is_audio[is_audio_eos] = False
458
+ sampling_text_mask = ~is_stopping & (delayed_lengths > n_vq)
459
+ next_token_logits[0][~is_audio] = next_token_logits[0][~is_audio].index_fill(-1, pre_exclude_mask0, float('-inf'))
460
+ next_token_logits[0][is_audio] = next_token_logits[0][is_audio].masked_fill(pre_exclude_mask1, float('-inf'))
461
+ if time_step == 0:
462
+ next_token_logits[0][..., 151662] = float('-inf')
463
+ if time_step <= n_vq:
464
+ next_token_logits[0][..., self.config.im_end_token_id] = float('-inf')
465
+
466
+ next_text_token[sampling_text_mask] = sample_token(
467
+ logits=next_token_logits[0][sampling_text_mask],
468
+ top_p=text_top_p,
469
+ top_k=text_top_k,
470
+ do_sample=text_do_sample
471
+ )
472
+ is_audio[next_text_token == self.config.audio_start_token_id] = True
473
+ is_stopping[next_text_token == self.config.im_end_token_id] = True
474
+
475
+ next_audio_tokens = torch.full((batch_size, n_vq), self.config.audio_pad_code, device=device)
476
+
477
+ pre_audio_mask = audio_lengths.unsqueeze(1) > torch.arange(n_vq, dtype=int, device=device).expand(batch_size, n_vq)
478
+ post_audio_mask = torch.arange(n_vq, dtype=int, device=device).expand(batch_size, n_vq) > delayed_lengths.unsqueeze(1) - 1
479
+ post_audio_mask[delayed_lengths == torch_int64_max] = True
480
+ sampling_audio_mask = pre_audio_mask & post_audio_mask
481
+ next_audio_tokens[~sampling_audio_mask] = self.config.audio_pad_code
482
+
483
+ if sampling_audio_mask.sum() > 0:
484
+ audio_ch0_logits = next_token_logits[1][sampling_audio_mask[:, 0]]
485
+ audio_logits = torch.stack(next_token_logits[2:], dim=1)[sampling_audio_mask[:, 1:]]
486
+ audio_ch0_logits[..., self.config.audio_pad_code] = float('-inf')
487
+ audio_logits[..., self.config.audio_pad_code] = float('-inf')
488
+ next_audio_tokens[:, 0][sampling_audio_mask[:, 0]] = sample_token(
489
+ logits=audio_ch0_logits,
490
+ prev_tokens=generation_ids[:, :, 1],
491
+ repetition_penalty=audio_repetition_penalty,
492
+ top_p=audio_top_p,
493
+ top_k=audio_top_k,
494
+ do_sample=audio_do_sample
495
+ )
496
+ next_audio_tokens[:, 1:][sampling_audio_mask[:, 1:]] = sample_token(
497
+ logits=audio_logits,
498
+ prev_tokens=generation_ids[:, :, 2:],
499
+ repetition_penalty=audio_repetition_penalty,
500
+ top_p=audio_top_p,
501
+ top_k=audio_top_k,
502
+ do_sample=audio_do_sample
503
+ )
504
+
505
+ audio_lengths[(next_text_token == self.config.audio_start_token_id) | (next_text_token == self.config.audio_assistant_gen_slot_token_id) | (next_text_token == self.config.audio_assistant_delay_slot_token_id)] += 1
506
+ audio_lengths[next_text_token == self.config.audio_end_token_id] = 0
507
+ delayed_lengths[(delayed_lengths == torch_int64_max) & (next_text_token == self.config.audio_assistant_delay_slot_token_id)] = 0
508
+ delayed_lengths[delayed_lengths != torch_int64_max] += 1
509
+ delayed_lengths[delayed_lengths > n_vq] = torch_int64_max
510
+
511
+ current_input_ids = torch.cat([next_text_token[:, None, None], next_audio_tokens[:, None, :]], dim=2)
512
+ current_attention_mask = torch.cat([current_attention_mask, (~is_stopping).unsqueeze(-1)], dim=-1)
513
+ generation_ids = torch.cat([generation_ids, current_input_ids], dim=1)
514
+
515
+ if is_stopping.sum() == batch_size:
516
+ break
517
+
518
+ start_indices = find_last_equal_C(input_ids[..., 0], self.config.im_start_token_id) + 3
519
+ start_lengths = seq_len - start_indices
520
+
521
+ output = []
522
+ for start_idx, start_length, cur_generation_ids in zip(start_indices, start_lengths, generation_ids):
523
+ output.append((start_length, cur_generation_ids[start_idx:]))
524
+
525
+ return output
processing_moss_tts.py ADDED
@@ -0,0 +1,949 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2026 OpenMOSS and the HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ import os
17
+ from typing import Any, Dict, List, Optional, Tuple, Type, Union, Literal, Final, cast
18
+ from dataclasses import dataclass
19
+ from pathlib import Path
20
+ import re
21
+ import torchaudio
22
+
23
+ from transformers import processing_utils
24
+
25
+ processing_utils.MODALITY_TO_BASE_CLASS_MAPPING["audio_tokenizer"] = "PreTrainedModel"
26
+
27
+ import torch
28
+ from transformers import (
29
+ PreTrainedTokenizerBase,
30
+ BatchFeature,
31
+ ProcessorMixin,
32
+ logging,
33
+ AutoConfig,
34
+ AutoModel,
35
+ AutoTokenizer,
36
+ )
37
+
38
+ from .configuration_moss_tts import MossTTSDelayConfig
39
+ from .tts_robust_normalizer_single_script import normalize_tts_text
40
+
41
+
42
+ logger = logging.get_logger(__name__)
43
+
44
+
45
+ AUDIO_PLACEHOLDER = "<|audio|>"
46
+
47
+
48
+ @dataclass
49
+ class Message:
50
+ def to_dict(self) -> Dict[str, Any]:
51
+ raise NotImplementedError
52
+
53
+
54
+ @dataclass
55
+ class UserMessage(Message):
56
+ text: Optional[str] = None
57
+ reference: Optional[List[Optional[Union[str, torch.Tensor]]]] = None
58
+ instruction: Optional[str] = None
59
+ tokens: Optional[int] = None
60
+ quality: Optional[str] = None
61
+ sound_event: Optional[str] = None
62
+ ambient_sound: Optional[str] = None
63
+ language: Optional[str] = None
64
+
65
+ def __post_init__(self):
66
+ template = """<user_inst>
67
+ - Reference(s):
68
+ {reference}
69
+ - Instruction:
70
+ {instruction}
71
+ - Tokens:
72
+ {tokens}
73
+ - Quality:
74
+ {quality}
75
+ - Sound Event:
76
+ {sound_event}
77
+ - Ambient Sound:
78
+ {ambient_sound}
79
+ - Language:
80
+ {language}
81
+ - Text:
82
+ {text}
83
+ </user_inst>"""
84
+
85
+ audio_codes_list = []
86
+ if self.reference is None:
87
+ reference = "None"
88
+ elif isinstance(self.reference, List):
89
+ reference = []
90
+ for speaker_idx, speaker_reference in enumerate(self.reference):
91
+ if speaker_reference is not None:
92
+ reference.append(f"[S{speaker_idx+1}]:\n{AUDIO_PLACEHOLDER}")
93
+ reference = "\n".join(reference)
94
+ audio_codes_list = [
95
+ speaker_reference
96
+ for speaker_reference in self.reference
97
+ if speaker_reference is not None
98
+ ]
99
+ else:
100
+ raise TypeError("`reference` should be exactly a list when it is not None.")
101
+
102
+ content = (
103
+ template.replace("{reference}", str(reference))
104
+ .replace("{instruction}", str(self.instruction))
105
+ .replace("{tokens}", str(self.tokens))
106
+ .replace("{quality}", str(self.quality))
107
+ .replace("{sound_event}", str(self.sound_event))
108
+ .replace("{ambient_sound}", str(self.ambient_sound))
109
+ .replace("{language}", str(self.language))
110
+ .replace("{text}", str(self.text))
111
+ )
112
+
113
+ self._content = content
114
+ self._audio_codes_list = audio_codes_list
115
+
116
+ def to_dict(self):
117
+ return {
118
+ "role": "user",
119
+ "content": self._content,
120
+ "audio_codes_list": self._audio_codes_list,
121
+ }
122
+
123
+
124
+ @dataclass
125
+ class AssistantMessage(Message):
126
+ audio_codes_list: List[Union[str, torch.Tensor]]
127
+ content: str = AUDIO_PLACEHOLDER
128
+
129
+ def to_dict(self):
130
+ return {
131
+ "role": "assistant",
132
+ "content": self.content,
133
+ "audio_codes_list": self.audio_codes_list,
134
+ }
135
+
136
+
137
+ USER_MESSAGE_FIELDS = (
138
+ "text",
139
+ "reference",
140
+ "instruction",
141
+ "tokens",
142
+ "quality",
143
+ "sound_event",
144
+ "ambient_sound",
145
+ "language",
146
+ )
147
+
148
+
149
+ class MossTTSDelayProcessor(ProcessorMixin):
150
+ tokenizer_class = "AutoTokenizer"
151
+ audio_tokenizer_class = "AutoModel"
152
+
153
+ tokenizer: PreTrainedTokenizerBase
154
+ audio_tokenizer: Any
155
+
156
+ def __init__(
157
+ self,
158
+ tokenizer: PreTrainedTokenizerBase,
159
+ audio_tokenizer: Any = None,
160
+ model_config: Optional[MossTTSDelayConfig] = None,
161
+ **kwargs,
162
+ ):
163
+ super().__init__(tokenizer=tokenizer, audio_tokenizer=audio_tokenizer, **kwargs)
164
+
165
+ # Explicit assignments for type-checkers; ProcessorMixin sets these too.
166
+ self.tokenizer = tokenizer
167
+ self.audio_tokenizer = audio_tokenizer
168
+ if model_config is None:
169
+ model_config = MossTTSDelayConfig()
170
+ self.model_config = model_config
171
+
172
+ self.imstart_token_id = tokenizer.convert_tokens_to_ids("<|im_start|>")
173
+ self.imend_token_id = tokenizer.convert_tokens_to_ids("<|im_end|>")
174
+ self.newline_token_id = 198
175
+
176
+ def _id_to_token(token_id: int) -> str:
177
+ tok = tokenizer.convert_ids_to_tokens(int(token_id))
178
+ if isinstance(tok, list):
179
+ return tok[0] if len(tok) > 0 else ""
180
+ return cast(str, tok)
181
+
182
+ self.audio_user_slot_token = _id_to_token(
183
+ self.model_config.audio_user_slot_token_id
184
+ )
185
+ self.audio_assistant_gen_slot_token = _id_to_token(
186
+ self.model_config.audio_assistant_gen_slot_token_id
187
+ )
188
+ self.audio_assistant_delay_slot_token = _id_to_token(
189
+ self.model_config.audio_assistant_delay_slot_token_id
190
+ )
191
+ self.audio_start_token = _id_to_token(self.model_config.audio_start_token_id)
192
+ self.audio_end_token = _id_to_token(self.model_config.audio_end_token_id)
193
+
194
+ @classmethod
195
+ def from_pretrained(cls, pretrained_model_name_or_path, *args, **kwargs):
196
+ trust_remote_code = kwargs.pop("trust_remote_code", True)
197
+ kwargs.pop("_from_auto", None)
198
+
199
+ audio_tokenizer_name_or_path = kwargs.pop("codec_path", None)
200
+ if audio_tokenizer_name_or_path is None:
201
+ processor_lookup_kwargs = dict(kwargs)
202
+ try:
203
+ processor_dict, _ = cls.get_processor_dict(
204
+ pretrained_model_name_or_path,
205
+ **processor_lookup_kwargs,
206
+ )
207
+ audio_tokenizer_name_or_path = processor_dict.get(
208
+ "audio_tokenizer_name_or_path"
209
+ )
210
+ audio_tokenizer_dict = processor_dict.get("audio_tokenizer", {})
211
+ if isinstance(audio_tokenizer_dict, dict):
212
+ audio_tokenizer_name_or_path = audio_tokenizer_dict.get(
213
+ "audio_tokenizer_name_or_path"
214
+ ) or audio_tokenizer_name_or_path
215
+ except Exception:
216
+ audio_tokenizer_name_or_path = None
217
+ if audio_tokenizer_name_or_path is None:
218
+ audio_tokenizer_name_or_path = "OpenMOSS-Team/MOSS-Audio-Tokenizer"
219
+
220
+ pretrained_model_name_or_path = Path(pretrained_model_name_or_path)
221
+ model_config = cast(
222
+ MossTTSDelayConfig,
223
+ AutoConfig.from_pretrained(
224
+ pretrained_model_name_or_path,
225
+ *args,
226
+ trust_remote_code=trust_remote_code,
227
+ **kwargs,
228
+ ),
229
+ )
230
+ tokenizer = AutoTokenizer.from_pretrained(
231
+ pretrained_model_name_or_path,
232
+ *args,
233
+ trust_remote_code=trust_remote_code,
234
+ **kwargs,
235
+ )
236
+ audio_tokenizer = AutoModel.from_pretrained(
237
+ audio_tokenizer_name_or_path,
238
+ trust_remote_code=trust_remote_code,
239
+ **kwargs,
240
+ )
241
+
242
+ return cls(
243
+ tokenizer=tokenizer,
244
+ audio_tokenizer=audio_tokenizer,
245
+ model_config=model_config,
246
+ **kwargs,
247
+ )
248
+
249
+ def __call__(self, *args, **kwargs) -> BatchFeature:
250
+ conversations = args[0] if len(args) > 0 else kwargs.pop("conversations")
251
+ mode: str = kwargs.pop("mode", "generation")
252
+ apply_chat_template: bool = kwargs.pop("apply_chat_template", True)
253
+ n_vq: Optional[int] = kwargs.pop("n_vq", None)
254
+
255
+ # Common ProcessorMixin kwargs that we ignore because we always return torch tensors.
256
+ kwargs.pop("return_tensors", None)
257
+ kwargs.pop("padding", None)
258
+ kwargs.pop("truncation", None)
259
+
260
+ """
261
+ mode only works when a Message is converted to a dict.
262
+ """
263
+
264
+ if mode not in {"generation", "continuation", "computing_loss"}:
265
+ raise RuntimeError
266
+
267
+ if isinstance(conversations, (Message, Dict)):
268
+ conversations = [conversations]
269
+
270
+ truncation = False
271
+ if mode == "continuation":
272
+ truncation = True
273
+
274
+ input_ids_list = []
275
+ for conversation in conversations:
276
+ if isinstance(conversation, (Message, Dict)):
277
+ conversation = [conversation]
278
+
279
+ # Normalize early so downstream logic always deals with dict messages.
280
+ conversation = [self._normalize_message(m) for m in conversation]
281
+
282
+ if (mode == "generation") ^ (len(conversation) % 2 != 0):
283
+ raise ValueError
284
+
285
+ if (mode == "generation") ^ (conversation[-1]["role"] == "user"):
286
+ raise ValueError
287
+
288
+ unified_codes = []
289
+ for message_idx, message in enumerate(conversation):
290
+ if apply_chat_template:
291
+ add_generation_prompt = (
292
+ mode == "generation" and message_idx == len(conversation) - 1
293
+ )
294
+ try:
295
+ content = self.tokenizer.apply_chat_template(
296
+ [{"role": message["role"], "content": message["content"]}],
297
+ add_generation_prompt=add_generation_prompt,
298
+ tokenize=False,
299
+ )
300
+ except TypeError:
301
+ try:
302
+ content = self.tokenizer.apply_chat_template(
303
+ [
304
+ {
305
+ "role": message["role"],
306
+ "content": message["content"],
307
+ }
308
+ ],
309
+ add_generation_prompt=add_generation_prompt,
310
+ )
311
+ except Exception:
312
+ logger.warning(
313
+ "apply_chat_template failed; fallback to raw content."
314
+ )
315
+ content = message["content"]
316
+ else:
317
+ content = message["content"]
318
+
319
+ if not isinstance(content, str):
320
+ content = str(content)
321
+
322
+ # Batch-encode all path-based references in one call when possible.
323
+ # This ensures we actually exercise audio_tokenizer.batch_encode for multi-reference prompts,
324
+ # instead of repeatedly calling it with batch=1.
325
+ raw_audio_items = message.get("audio_codes_list", [])
326
+
327
+ audio_codes_list: List[torch.Tensor] = []
328
+ if len(raw_audio_items) > 0:
329
+ encoded_items: List[Optional[torch.Tensor]] = [None] * len(
330
+ raw_audio_items
331
+ )
332
+ paths: List[str] = []
333
+ path_positions: List[int] = []
334
+
335
+ for idx, item in enumerate(raw_audio_items):
336
+ if isinstance(item, torch.Tensor):
337
+ if n_vq is not None and item.shape[1] != n_vq:
338
+ raise RuntimeError(
339
+ "audio_codes's n_vq is not equal to the parameter `n_vq`. Your can set the parameter `n_vq` as None if you have already tokenzied the wavs."
340
+ )
341
+ encoded_items[idx] = item
342
+ continue
343
+
344
+ if isinstance(item, (str, os.PathLike)):
345
+ paths.append(str(item))
346
+ path_positions.append(idx)
347
+ continue
348
+
349
+ raise TypeError(
350
+ "Each audio item must be a torch.Tensor of codes or a path-like string."
351
+ )
352
+
353
+ if len(paths) > 0:
354
+ encoded_from_paths = self.encode_audios_from_path(paths, n_vq)
355
+ if len(encoded_from_paths) != len(paths):
356
+ raise RuntimeError(
357
+ "encode_audios_from_path returned an unexpected number of items."
358
+ )
359
+ for pos, codes in zip(path_positions, encoded_from_paths):
360
+ encoded_items[pos] = codes
361
+
362
+ audio_codes_list = [cast(torch.Tensor, t) for t in encoded_items]
363
+ unified_codes.append(
364
+ self._get_unified_codes(
365
+ message["role"], content, audio_codes_list, truncation
366
+ )
367
+ )
368
+
369
+ unified_codes = torch.cat(unified_codes)
370
+ input_ids_list.append(unified_codes)
371
+
372
+ return BatchFeature(data=self._pad(input_ids_list))
373
+
374
+ @staticmethod
375
+ def build_user_message(
376
+ text: Optional[str] = None,
377
+ reference: Optional[List[Optional[Union[str, torch.Tensor]]]] = None,
378
+ instruction: Optional[str] = None,
379
+ tokens: Optional[int] = None,
380
+ quality: Optional[str] = None,
381
+ sound_event: Optional[str] = None,
382
+ ambient_sound: Optional[str] = None,
383
+ language: Optional[str] = None,
384
+ ) -> Dict:
385
+ if reference is not None and not isinstance(reference, list):
386
+ reference = [reference]
387
+ text = normalize_tts_text(text)
388
+ return UserMessage(
389
+ text=text,
390
+ reference=reference,
391
+ instruction=instruction,
392
+ tokens=tokens,
393
+ quality=quality,
394
+ sound_event=sound_event,
395
+ ambient_sound=ambient_sound,
396
+ language=language,
397
+ ).to_dict()
398
+
399
+ @staticmethod
400
+ def build_assistant_message(
401
+ audio_codes_list: List[Union[str, torch.Tensor]],
402
+ content: str = AUDIO_PLACEHOLDER,
403
+ ) -> Dict:
404
+ return AssistantMessage(
405
+ audio_codes_list=audio_codes_list,
406
+ content=content,
407
+ ).to_dict()
408
+
409
+ def _normalize_message(self, message: Union[Message, Dict]) -> Dict:
410
+ if isinstance(message, Message):
411
+ return message.to_dict()
412
+ if not isinstance(message, dict):
413
+ raise TypeError("Each message must be a Message or dict.")
414
+ if "role" not in message:
415
+ raise ValueError("Message dict must include a 'role' field.")
416
+ if "content" in message and "audio_codes_list" in message:
417
+ return message
418
+ role = message["role"]
419
+ if role == "user":
420
+ kwargs = {key: message.get(key) for key in USER_MESSAGE_FIELDS}
421
+ return self.build_user_message(**kwargs)
422
+ if role == "assistant":
423
+ return self.build_assistant_message(
424
+ audio_codes_list=message.get("audio_codes_list", []),
425
+ content=message.get("content", AUDIO_PLACEHOLDER),
426
+ )
427
+ raise ValueError(f"Unsupported role: {role}")
428
+
429
+ def _pad(self, input_ids_list: List[torch.Tensor]):
430
+ device = input_ids_list[0].device
431
+ lengths = torch.tensor([w.shape[0] for w in input_ids_list], device=device)
432
+ pad_input_ids = torch.nn.utils.rnn.pad_sequence(
433
+ input_ids_list,
434
+ batch_first=True,
435
+ padding_value=self.model_config.audio_pad_code,
436
+ padding_side="left",
437
+ )
438
+ other_channel_mask = (pad_input_ids.shape[1] - lengths).unsqueeze(
439
+ 1
440
+ ) > torch.arange(pad_input_ids.shape[1], device=device).unsqueeze(0)
441
+ pad_input_ids[..., 0][other_channel_mask] = self.model_config.pad_token_id
442
+ attention_mask = torch.zeros(
443
+ pad_input_ids.shape[0], pad_input_ids.shape[1], device=device
444
+ )
445
+ attention_mask[~other_channel_mask] = 1
446
+ attention_mask = attention_mask.bool()
447
+ return {
448
+ "input_ids": pad_input_ids, # [batch_size, seqlen, n_vq]
449
+ "attention_mask": attention_mask,
450
+ }
451
+
452
+ @staticmethod
453
+ def _replace_audio_placeholders(
454
+ content: str,
455
+ lengths: List[int],
456
+ n_vq: int,
457
+ gen_slot_token: str,
458
+ delay_slot_token: str,
459
+ audio_start_token: str,
460
+ audio_end_token: str,
461
+ ) -> str:
462
+ if n_vq < 1:
463
+ raise ValueError(f"n_vq must be >= 1, got {n_vq}")
464
+
465
+ num_placeholders = content.count(AUDIO_PLACEHOLDER)
466
+ if num_placeholders != len(lengths):
467
+ raise ValueError(
468
+ f"Number of {AUDIO_PLACEHOLDER} ({num_placeholders}) "
469
+ f"does not match lengths ({len(lengths)})"
470
+ )
471
+
472
+ def build_audio_block(length: int) -> str:
473
+ if length < 0:
474
+ raise ValueError(f"length must be >= 0, got {length}")
475
+
476
+ if length == 0:
477
+ return f"{audio_start_token}{audio_end_token}"
478
+
479
+ step_tokens = gen_slot_token * length + (delay_slot_token * (n_vq - 1))
480
+ return f"{audio_start_token}{step_tokens}{audio_end_token}"
481
+
482
+ lengths_iter = iter(lengths)
483
+
484
+ def replacer(match: re.Match) -> str:
485
+ length = next(lengths_iter)
486
+ return build_audio_block(length)
487
+
488
+ result = re.sub(re.escape(AUDIO_PLACEHOLDER), replacer, content)
489
+
490
+ return result
491
+
492
+ @staticmethod
493
+ def _merge_consecutive_audio_placeholders(
494
+ content: str,
495
+ audio_codes_list: List[torch.Tensor],
496
+ ) -> Tuple[str, List[torch.Tensor]]:
497
+ matches = list(re.finditer(re.escape(AUDIO_PLACEHOLDER), content))
498
+ if len(matches) <= 1:
499
+ return content, audio_codes_list
500
+
501
+ if len(matches) != len(audio_codes_list):
502
+ raise ValueError(
503
+ "Audio placeholders do not match the provided audio codes list."
504
+ )
505
+
506
+ new_audio_codes_list = []
507
+ new_parts = []
508
+ last_pos = 0
509
+ i = 0
510
+ while i < len(matches):
511
+ j = i
512
+ while (
513
+ j + 1 < len(matches)
514
+ and content[matches[j].end() : matches[j + 1].start()].strip() == ""
515
+ ):
516
+ j += 1
517
+
518
+ new_parts.append(content[last_pos : matches[i].start()])
519
+ new_parts.append(AUDIO_PLACEHOLDER)
520
+ last_pos = matches[j].end()
521
+
522
+ if j == i:
523
+ new_audio_codes_list.append(audio_codes_list[i])
524
+ else:
525
+ new_audio_codes_list.append(
526
+ torch.cat(audio_codes_list[i : j + 1], dim=0)
527
+ )
528
+
529
+ i = j + 1
530
+
531
+ new_parts.append(content[last_pos:])
532
+ return "".join(new_parts), new_audio_codes_list
533
+
534
+ @staticmethod
535
+ def apply_delay_pattern(codes: torch.Tensor, pad_code: int) -> torch.Tensor:
536
+ delayed_tokens = torch.full(
537
+ (codes.shape[0] + codes.shape[1] - 1, codes.shape[1]),
538
+ pad_code,
539
+ device=codes.device,
540
+ dtype=codes.dtype,
541
+ )
542
+ for i in range(codes.shape[1]):
543
+ delayed_tokens[i : i + codes.shape[0], i] = codes[:, i]
544
+ return delayed_tokens
545
+
546
+ @staticmethod
547
+ def apply_de_delay_pattern(delay_codes: torch.Tensor) -> torch.Tensor:
548
+ tokens = torch.full(
549
+ (delay_codes.shape[0] - delay_codes.shape[1] + 1, delay_codes.shape[1]),
550
+ 0,
551
+ device=delay_codes.device,
552
+ dtype=delay_codes.dtype,
553
+ )
554
+ for i in range(delay_codes.shape[1]):
555
+ tokens[:, i] = delay_codes[i : i + tokens.shape[0], i]
556
+ return tokens
557
+
558
+ def _get_unified_codes(
559
+ self,
560
+ role: str,
561
+ content: str,
562
+ audio_codes_list: List[torch.Tensor],
563
+ truncation: bool,
564
+ ) -> torch.Tensor:
565
+ """
566
+ 此时的 content 已经是带上了对话格式
567
+ """
568
+ if role == "user":
569
+ audio_gen_slot_token = audio_delay_slot_token = self.audio_user_slot_token
570
+ truncation = False
571
+ else:
572
+ audio_gen_slot_token = self.audio_assistant_gen_slot_token
573
+ audio_delay_slot_token = self.audio_assistant_delay_slot_token
574
+
575
+ if len(audio_codes_list):
576
+ n_vq = audio_codes_list[0].shape[1]
577
+ else:
578
+ n_vq = self.model_config.n_vq
579
+
580
+ if len(audio_codes_list) > 1 and AUDIO_PLACEHOLDER in content:
581
+ content, audio_codes_list = self._merge_consecutive_audio_placeholders(
582
+ content, audio_codes_list
583
+ )
584
+ content = self._replace_audio_placeholders(
585
+ content=content,
586
+ lengths=[len(audio_codes) for audio_codes in audio_codes_list],
587
+ n_vq=n_vq,
588
+ gen_slot_token=audio_gen_slot_token,
589
+ delay_slot_token=audio_delay_slot_token,
590
+ audio_start_token=self.audio_start_token,
591
+ audio_end_token=self.audio_end_token,
592
+ )
593
+ text_codes = torch.tensor(
594
+ self.tokenizer.encode(content),
595
+ device=audio_codes_list[0].device if audio_codes_list else None,
596
+ )
597
+
598
+ audio_start_indices = torch.where(
599
+ text_codes == self.model_config.audio_start_token_id
600
+ )[0]
601
+ audio_end_indices = torch.where(
602
+ text_codes == self.model_config.audio_end_token_id
603
+ )[0]
604
+ if len(audio_start_indices) != len(audio_codes_list) or len(
605
+ audio_end_indices
606
+ ) != len(audio_codes_list):
607
+ raise ValueError(
608
+ "Audio placeholders do not match the provided audio codes list."
609
+ )
610
+
611
+ delay_audio_codes_list = []
612
+ if len(audio_codes_list) == 0:
613
+ delay_audio_codes_list = torch.full(
614
+ (len(text_codes), n_vq),
615
+ self.model_config.audio_pad_code,
616
+ device=text_codes.device,
617
+ dtype=text_codes.dtype,
618
+ )
619
+ else:
620
+ prefix_idx = 0
621
+ for audio_start_idx_t, audio_end_idx_t, audio_codes in zip(
622
+ audio_start_indices, audio_end_indices, audio_codes_list
623
+ ):
624
+ audio_start_idx = int(audio_start_idx_t.item())
625
+ audio_end_idx = int(audio_end_idx_t.item())
626
+ delay_audio_codes = self.apply_delay_pattern(
627
+ audio_codes, self.model_config.audio_pad_code
628
+ )
629
+ pad_codes = torch.full(
630
+ (audio_start_idx - prefix_idx + 1, n_vq),
631
+ self.model_config.audio_pad_code,
632
+ device=audio_codes.device,
633
+ dtype=audio_codes.dtype,
634
+ )
635
+ delay_audio_codes_list.extend([pad_codes, delay_audio_codes])
636
+ prefix_idx = audio_end_idx
637
+
638
+ if truncation:
639
+ delay_audio_codes_list[-1] = delay_audio_codes_list[-1][
640
+ : -(n_vq - 1), :
641
+ ]
642
+ else:
643
+ last_audio_end_idx = int(audio_end_indices[-1].item())
644
+ pad_codes = torch.full(
645
+ (len(text_codes) - last_audio_end_idx, n_vq),
646
+ self.model_config.audio_pad_code,
647
+ device=audio_codes_list[0].device,
648
+ dtype=audio_codes_list[0].dtype,
649
+ )
650
+ delay_audio_codes_list.append(pad_codes)
651
+
652
+ delay_audio_codes_list = torch.cat(delay_audio_codes_list)
653
+
654
+ if text_codes.shape[0] != delay_audio_codes_list.shape[0]:
655
+ text_codes = text_codes[: delay_audio_codes_list.shape[0]]
656
+
657
+ unified_codes = torch.cat(
658
+ [text_codes.unsqueeze(1), delay_audio_codes_list], dim=1
659
+ )
660
+ return unified_codes
661
+
662
+ def _parse_text_codes(self, start_length, text_codes):
663
+ text = cast(str, self.tokenizer.decode(text_codes))
664
+ prefix = cast(str, self.tokenizer.decode(text_codes[:start_length]))
665
+ text = text[len(prefix) :]
666
+
667
+ AUDIO_PATTERN = re.compile(
668
+ rf"(?:{self.audio_start_token})?"
669
+ rf"(?:{self.audio_assistant_gen_slot_token})*"
670
+ rf"(?:{self.audio_assistant_delay_slot_token})*"
671
+ rf"{self.audio_end_token}"
672
+ )
673
+
674
+ def normalize_audio_segments(text: str) -> str:
675
+ def repl(match: re.Match) -> str:
676
+ seg = match.group(0)
677
+ # Replace with <|audio|> if gen_slot is present in the segment;
678
+ if self.audio_assistant_gen_slot_token in seg:
679
+ return AUDIO_PLACEHOLDER
680
+ # Otherwise, remove it.
681
+ return ""
682
+
683
+ return AUDIO_PATTERN.sub(repl, text)
684
+
685
+ return normalize_audio_segments(text)
686
+
687
+ def _parse_audio_codes(self, start_length, audio_codes):
688
+ # De-delay back to [T', n_vq]
689
+ audio_codes = self.apply_de_delay_pattern(audio_codes)
690
+
691
+ # Rows that are all pad are separators between real audio segments.
692
+ is_pad = (audio_codes == self.model_config.audio_pad_code).all(dim=1)
693
+ non_pad = ~is_pad
694
+ if not non_pad.any():
695
+ return []
696
+
697
+ idx = torch.nonzero(non_pad).squeeze(1)
698
+ breaks = torch.where(idx[1:] != idx[:-1] + 1)[0] + 1
699
+ if breaks.numel() == 0:
700
+ segments_idx = [idx]
701
+ else:
702
+ segments_idx = torch.split(idx, breaks.tolist())
703
+
704
+ audio_codes_list = [audio_codes[s] for s in segments_idx]
705
+
706
+ # Batch-decode all audio segments together.
707
+ decoded_audio_list = self.decode_audio_codes(audio_codes_list)
708
+
709
+ # Keep codec causal context by decoding the whole first segment first,
710
+ # then trim at waveform level according to start_length ratio.
711
+ if (
712
+ start_length > 0
713
+ and len(audio_codes_list) > 0
714
+ and len(decoded_audio_list) > 0
715
+ ):
716
+ first_codes_length = audio_codes_list[0].shape[0]
717
+ if first_codes_length > 0:
718
+ trim_ratio = max(
719
+ 0.0, min(float(start_length) / float(first_codes_length), 1.0)
720
+ )
721
+ first_audio = decoded_audio_list[0]
722
+ if trim_ratio >= 1.0:
723
+ decoded_audio_list = decoded_audio_list[1:]
724
+ elif trim_ratio > 0.0:
725
+ trim_samples = int(first_audio.shape[-1] * trim_ratio)
726
+ decoded_audio_list[0] = first_audio[..., trim_samples:]
727
+
728
+ return decoded_audio_list
729
+
730
+ def decode(self, output: List[Tuple[int, torch.Tensor]]):
731
+ """
732
+ 1. 这里不管怎样,都需要一个完整的 assistant generation ids;
733
+ 2. 支持从任意位置进行截断;
734
+ """
735
+
736
+ genearted_messages = []
737
+ for start_length, generation_ids in output:
738
+ content = self._parse_text_codes(start_length, generation_ids[:, 0])
739
+ audio_codes_list = self._parse_audio_codes(
740
+ start_length, generation_ids[:, 1:]
741
+ )
742
+ if content == "":
743
+ message = None
744
+ else:
745
+ message = AssistantMessage(
746
+ content=content,
747
+ audio_codes_list=cast(
748
+ List[Union[str, torch.Tensor]], audio_codes_list
749
+ ),
750
+ )
751
+ genearted_messages.append(message)
752
+ return genearted_messages
753
+
754
+ @staticmethod
755
+ def loudness_normalize(
756
+ wav: torch.Tensor,
757
+ target_dbfs: float = -20,
758
+ gain_range: tuple[float, float] = (-3.0, 3.0),
759
+ ) -> torch.Tensor:
760
+ wav = wav.to(torch.float32)
761
+ if wav.numel() == 0:
762
+ return wav
763
+ current_dbfs = 10.0 * torch.log10(torch.mean(wav**2) + 1e-9)
764
+ gain = float(target_dbfs - current_dbfs)
765
+ gain = max(gain_range[0], min(gain, gain_range[1]))
766
+ factor = 10.0 ** (gain / 20.0)
767
+ return wav * factor
768
+
769
+ def _get_audio_tokenizer_device(self) -> torch.device:
770
+ """Best-effort device inference for `self.audio_tokenizer`.
771
+
772
+ Notes:
773
+ - Old TAC wrapper exposed `.device`, but standard `torch.nn.Module` does not.
774
+ - New MossAudioTokenizerModel is a `PreTrainedModel`; parameters define its device.
775
+ """
776
+
777
+ audio_tokenizer = getattr(self, "audio_tokenizer", None)
778
+ if audio_tokenizer is None:
779
+ logger.warning(
780
+ "audio_tokenizer is not set on processor. Using CPU as default."
781
+ )
782
+ return torch.device("cpu")
783
+
784
+ device_attr = getattr(audio_tokenizer, "device", None)
785
+ if isinstance(device_attr, torch.device):
786
+ return device_attr
787
+
788
+ try:
789
+ return next(audio_tokenizer.parameters()).device
790
+ except StopIteration:
791
+ # No parameters (shouldn't happen for real models); default to CPU.
792
+ logger.warning(
793
+ "No parameters found on audio_tokenizer. Using CPU as default."
794
+ )
795
+ return torch.device("cpu")
796
+
797
+ def encode_audios_from_wav(
798
+ self,
799
+ wav_list: List[torch.Tensor],
800
+ sampling_rate: int,
801
+ n_vq: Optional[int] = None,
802
+ ):
803
+ if self.audio_tokenizer is None:
804
+ raise RuntimeError("audio_tokenizer is not set on processor.")
805
+ audio_tokenizer = self.audio_tokenizer
806
+
807
+ if isinstance(wav_list, torch.Tensor):
808
+ wav_list = [wav_list]
809
+ wav_list_ = []
810
+ resample = False
811
+ if sampling_rate != self.model_config.sampling_rate:
812
+ resample = True
813
+ device = self._get_audio_tokenizer_device()
814
+ for wav in wav_list:
815
+ if wav.shape[0] > 1:
816
+ wav = torch.mean(wav, dim=0, keepdim=True)
817
+ if resample:
818
+ wav = torchaudio.functional.resample(
819
+ waveform=wav,
820
+ orig_freq=sampling_rate,
821
+ new_freq=self.model_config.sampling_rate,
822
+ )
823
+ wav = wav.to(device)
824
+ wav_list_.append(self.loudness_normalize(wav.squeeze(0)))
825
+
826
+ # New MossAudioTokenizerModel API: prefer batch_encode(list[wav])
827
+ if hasattr(audio_tokenizer, "batch_encode"):
828
+ enc = audio_tokenizer.batch_encode(wav_list_, num_quantizers=n_vq)
829
+ audio_codes = enc.audio_codes # (NQ, B, T)
830
+ audio_codes_lengths = enc.audio_codes_lengths # (B,)
831
+ else:
832
+ # Fallback: use encode() with explicit padding.
833
+ max_len = max(int(wav.shape[-1]) for wav in wav_list_)
834
+ input_values = torch.zeros(
835
+ len(wav_list_), 1, max_len, device=device, dtype=torch.float32
836
+ )
837
+ padding_mask = torch.zeros(
838
+ len(wav_list_), max_len, device=device, dtype=torch.bool
839
+ )
840
+ for i, wav in enumerate(wav_list_):
841
+ this_len = int(wav.shape[-1])
842
+ input_values[i, 0, :this_len] = wav
843
+ padding_mask[i, :this_len] = True
844
+ enc = audio_tokenizer.encode(
845
+ input_values,
846
+ padding_mask=padding_mask,
847
+ num_quantizers=n_vq,
848
+ return_dict=True,
849
+ )
850
+ audio_codes = enc.audio_codes
851
+ audio_codes_lengths = enc.audio_codes_lengths
852
+
853
+ if audio_codes is None or audio_codes_lengths is None:
854
+ raise RuntimeError(
855
+ "audio_tokenizer.encode() returned empty outputs (audio_codes/audio_codes_lengths)."
856
+ )
857
+
858
+ # Keep processor's historical contract: list[Tensor] with shape (T, NQ)
859
+ # and on CPU (so downstream text/audio packing remains device-agnostic).
860
+ codes_list: List[torch.Tensor] = []
861
+ for i in range(int(audio_codes.shape[1])):
862
+ length_i = int(audio_codes_lengths[i].item())
863
+ codes_i = (
864
+ audio_codes[:, i, :length_i]
865
+ .transpose(0, 1)
866
+ .contiguous()
867
+ .to(torch.long)
868
+ .cpu()
869
+ )
870
+ codes_list.append(codes_i)
871
+ return codes_list
872
+
873
+ def encode_audios_from_path(
874
+ self, wav_path_list: Union[str, List[str]], n_vq: Optional[int] = None
875
+ ):
876
+ if isinstance(wav_path_list, str):
877
+ wav_path_list = [wav_path_list]
878
+
879
+ if len(wav_path_list) == 0:
880
+ raise ValueError("Empty wav_path_list")
881
+
882
+ # Load + (if needed) resample each wav independently, so callers can
883
+ # pass a heterogeneous batch of files while still benefiting from
884
+ # audio_tokenizer.batch_encode.
885
+ target_sr = int(self.model_config.sampling_rate)
886
+ wav_list: List[torch.Tensor] = []
887
+ for wav_path in wav_path_list:
888
+ wav, sr = torchaudio.load(wav_path)
889
+ if int(sr) != target_sr:
890
+ wav = torchaudio.functional.resample(
891
+ waveform=wav,
892
+ orig_freq=int(sr),
893
+ new_freq=target_sr,
894
+ )
895
+ wav_list.append(wav)
896
+
897
+ return self.encode_audios_from_wav(wav_list, target_sr, n_vq)
898
+
899
+ def decode_audio_codes(
900
+ self, audio_tokens_list: Union[torch.Tensor, List[torch.Tensor]]
901
+ ):
902
+ if self.audio_tokenizer is None:
903
+ raise RuntimeError("audio_tokenizer is not set on processor.")
904
+ audio_tokenizer = self.audio_tokenizer
905
+
906
+ if isinstance(audio_tokens_list, torch.Tensor):
907
+ audio_tokens_list = [audio_tokens_list]
908
+ if len(audio_tokens_list) == 0:
909
+ return []
910
+
911
+ device = self._get_audio_tokenizer_device()
912
+
913
+ # Processor uses (T, NQ); MossAudioTokenizer expects (NQ, T) (or (NQ, B, T)).
914
+ codes_list = [
915
+ codes.transpose(0, 1).contiguous().to(device=device, dtype=torch.long)
916
+ for codes in audio_tokens_list
917
+ ]
918
+
919
+ # Fallback: pad to (NQ, B, T) + mask, then decode.
920
+ nq = int(codes_list[0].shape[0])
921
+ max_t = max(int(c.shape[1]) for c in codes_list)
922
+ audio_codes = torch.zeros(
923
+ nq, len(codes_list), max_t, device=device, dtype=torch.long
924
+ )
925
+ padding_mask = torch.zeros(
926
+ len(codes_list), max_t, device=device, dtype=torch.bool
927
+ )
928
+ for i, c in enumerate(codes_list):
929
+ t = int(c.shape[1])
930
+ audio_codes[:, i, :t] = c
931
+ padding_mask[i, :t] = True
932
+ dec = audio_tokenizer.decode(
933
+ audio_codes, padding_mask=padding_mask, return_dict=True, chunk_duration=8
934
+ )
935
+ audio = dec.audio
936
+ audio_lengths = dec.audio_lengths
937
+
938
+ if audio is None or audio_lengths is None:
939
+ raise RuntimeError(
940
+ "audio_tokenizer.decode() returned empty outputs (audio/audio_lengths)."
941
+ )
942
+
943
+ # Return historical contract: list of 1D waveforms (T,)
944
+ wav_list: List[torch.Tensor] = []
945
+ for i in range(int(audio.shape[0])):
946
+ length_i = int(audio_lengths[i].item())
947
+ wav = audio[i, 0, :length_i].contiguous().to(torch.float32).cpu()
948
+ wav_list.append(wav)
949
+ return wav_list
processor_config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "processor_class": "MossTTSDelayProcessor",
3
+ "auto_map": {
4
+ "AutoProcessor": "processing_moss_tts.MossTTSDelayProcessor"
5
+ }
6
+ }
special_tokens_map.json ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "additional_special_tokens": [
3
+ "<|im_start|>",
4
+ "<|im_end|>",
5
+ "<|object_ref_start|>",
6
+ "<|object_ref_end|>",
7
+ "<|box_start|>",
8
+ "<|box_end|>",
9
+ "<|quad_start|>",
10
+ "<|quad_end|>",
11
+ "<|audio_start|>",
12
+ "<|audio_end|>",
13
+ "<|audio_user_slot|>",
14
+ "<|image_pad|>",
15
+ "<|audio_assistant_gen_slot|>"
16
+ ],
17
+ "eos_token": {
18
+ "content": "<|im_end|>",
19
+ "lstrip": false,
20
+ "normalized": false,
21
+ "rstrip": false,
22
+ "single_word": false
23
+ },
24
+ "pad_token": {
25
+ "content": "<|endoftext|>",
26
+ "lstrip": false,
27
+ "normalized": false,
28
+ "rstrip": false,
29
+ "single_word": false
30
+ }
31
+ }
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cb3c8fa82993d515469c2800cc455bff4aaa3c4fed9da1f2b0c0668c304f335a
3
+ size 11422691
tokenizer_config.json ADDED
@@ -0,0 +1,240 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": false,
3
+ "add_prefix_space": false,
4
+ "added_tokens_decoder": {
5
+ "151643": {
6
+ "content": "<|endoftext|>",
7
+ "lstrip": false,
8
+ "normalized": false,
9
+ "rstrip": false,
10
+ "single_word": false,
11
+ "special": true
12
+ },
13
+ "151644": {
14
+ "content": "<|im_start|>",
15
+ "lstrip": false,
16
+ "normalized": false,
17
+ "rstrip": false,
18
+ "single_word": false,
19
+ "special": true
20
+ },
21
+ "151645": {
22
+ "content": "<|im_end|>",
23
+ "lstrip": false,
24
+ "normalized": false,
25
+ "rstrip": false,
26
+ "single_word": false,
27
+ "special": true
28
+ },
29
+ "151646": {
30
+ "content": "<|object_ref_start|>",
31
+ "lstrip": false,
32
+ "normalized": false,
33
+ "rstrip": false,
34
+ "single_word": false,
35
+ "special": true
36
+ },
37
+ "151647": {
38
+ "content": "<|object_ref_end|>",
39
+ "lstrip": false,
40
+ "normalized": false,
41
+ "rstrip": false,
42
+ "single_word": false,
43
+ "special": true
44
+ },
45
+ "151648": {
46
+ "content": "<|box_start|>",
47
+ "lstrip": false,
48
+ "normalized": false,
49
+ "rstrip": false,
50
+ "single_word": false,
51
+ "special": true
52
+ },
53
+ "151649": {
54
+ "content": "<|box_end|>",
55
+ "lstrip": false,
56
+ "normalized": false,
57
+ "rstrip": false,
58
+ "single_word": false,
59
+ "special": true
60
+ },
61
+ "151650": {
62
+ "content": "<|quad_start|>",
63
+ "lstrip": false,
64
+ "normalized": false,
65
+ "rstrip": false,
66
+ "single_word": false,
67
+ "special": true
68
+ },
69
+ "151651": {
70
+ "content": "<|quad_end|>",
71
+ "lstrip": false,
72
+ "normalized": false,
73
+ "rstrip": false,
74
+ "single_word": false,
75
+ "special": true
76
+ },
77
+ "151652": {
78
+ "content": "<|audio_start|>",
79
+ "lstrip": false,
80
+ "normalized": false,
81
+ "rstrip": false,
82
+ "single_word": false,
83
+ "special": true
84
+ },
85
+ "151653": {
86
+ "content": "<|audio_end|>",
87
+ "lstrip": false,
88
+ "normalized": false,
89
+ "rstrip": false,
90
+ "single_word": false,
91
+ "special": true
92
+ },
93
+ "151654": {
94
+ "content": "<|audio_user_slot|>",
95
+ "lstrip": false,
96
+ "normalized": false,
97
+ "rstrip": false,
98
+ "single_word": false,
99
+ "special": true
100
+ },
101
+ "151655": {
102
+ "content": "<|image_pad|>",
103
+ "lstrip": false,
104
+ "normalized": false,
105
+ "rstrip": false,
106
+ "single_word": false,
107
+ "special": true
108
+ },
109
+ "151656": {
110
+ "content": "<|audio_assistant_gen_slot|>",
111
+ "lstrip": false,
112
+ "normalized": false,
113
+ "rstrip": false,
114
+ "single_word": false,
115
+ "special": true
116
+ },
117
+ "151657": {
118
+ "content": "<tool_call>",
119
+ "lstrip": false,
120
+ "normalized": false,
121
+ "rstrip": false,
122
+ "single_word": false,
123
+ "special": false
124
+ },
125
+ "151658": {
126
+ "content": "</tool_call>",
127
+ "lstrip": false,
128
+ "normalized": false,
129
+ "rstrip": false,
130
+ "single_word": false,
131
+ "special": false
132
+ },
133
+ "151659": {
134
+ "content": "<|fim_prefix|>",
135
+ "lstrip": false,
136
+ "normalized": false,
137
+ "rstrip": false,
138
+ "single_word": false,
139
+ "special": false
140
+ },
141
+ "151660": {
142
+ "content": "<|fim_middle|>",
143
+ "lstrip": false,
144
+ "normalized": false,
145
+ "rstrip": false,
146
+ "single_word": false,
147
+ "special": false
148
+ },
149
+ "151661": {
150
+ "content": "<|fim_suffix|>",
151
+ "lstrip": false,
152
+ "normalized": false,
153
+ "rstrip": false,
154
+ "single_word": false,
155
+ "special": false
156
+ },
157
+ "151662": {
158
+ "content": "<|audio_assistant_delay_slot|>",
159
+ "lstrip": false,
160
+ "normalized": false,
161
+ "rstrip": false,
162
+ "single_word": false,
163
+ "special": false
164
+ },
165
+ "151663": {
166
+ "content": "<|repo_name|>",
167
+ "lstrip": false,
168
+ "normalized": false,
169
+ "rstrip": false,
170
+ "single_word": false,
171
+ "special": false
172
+ },
173
+ "151664": {
174
+ "content": "<|file_sep|>",
175
+ "lstrip": false,
176
+ "normalized": false,
177
+ "rstrip": false,
178
+ "single_word": false,
179
+ "special": false
180
+ },
181
+ "151665": {
182
+ "content": "<tool_response>",
183
+ "lstrip": false,
184
+ "normalized": false,
185
+ "rstrip": false,
186
+ "single_word": false,
187
+ "special": false
188
+ },
189
+ "151666": {
190
+ "content": "</tool_response>",
191
+ "lstrip": false,
192
+ "normalized": false,
193
+ "rstrip": false,
194
+ "single_word": false,
195
+ "special": false
196
+ },
197
+ "151667": {
198
+ "content": "<think>",
199
+ "lstrip": false,
200
+ "normalized": false,
201
+ "rstrip": false,
202
+ "single_word": false,
203
+ "special": false
204
+ },
205
+ "151668": {
206
+ "content": "</think>",
207
+ "lstrip": false,
208
+ "normalized": false,
209
+ "rstrip": false,
210
+ "single_word": false,
211
+ "special": false
212
+ }
213
+ },
214
+ "additional_special_tokens": [
215
+ "<|im_start|>",
216
+ "<|im_end|>",
217
+ "<|object_ref_start|>",
218
+ "<|object_ref_end|>",
219
+ "<|box_start|>",
220
+ "<|box_end|>",
221
+ "<|quad_start|>",
222
+ "<|quad_end|>",
223
+ "<|audio_start|>",
224
+ "<|audio_end|>",
225
+ "<|audio_user_slot|>",
226
+ "<|image_pad|>",
227
+ "<|audio_assistant_gen_slot|>"
228
+ ],
229
+ "bos_token": null,
230
+ "clean_up_tokenization_spaces": false,
231
+ "eos_token": "<|im_end|>",
232
+ "errors": "replace",
233
+ "extra_special_tokens": {},
234
+ "model_max_length": 131072,
235
+ "pad_token": "<|endoftext|>",
236
+ "processor_class": "AsteroidProcessor",
237
+ "split_special_tokens": false,
238
+ "tokenizer_class": "Qwen2Tokenizer",
239
+ "unk_token": null
240
+ }
tts_robust_normalizer_single_script.py ADDED
@@ -0,0 +1,346 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ TTS 输入鲁棒性正则化器(非语义 TN)
6
+
7
+ 目标
8
+ ----
9
+ 1. 只做“鲁棒性清洗”,不做数字/单位/日期/金额等语义展开。
10
+ 2. 优先保护高风险 token,避免把 `.map`、`app.js.map`、`v2.3.1`、URL、Email、@mention、#hashtag 清坏。
11
+ 3. 保留 `[]` / `{}` 的内容,为后续声音事件、控制标签留接口。
12
+ 4. 对结构性符号做“替换而非删除”:
13
+ - `【】 / 〖〗 / 『』 / 「」` 在结构位置转成句边界。
14
+ - `《》` 只在“独立标题/栏目名”场景拆开;嵌入式标题保持不变。
15
+ - `—— / -- / ——...` 转成句边界。
16
+ - `-> / => / →` 等流程连接符转成中文逗号,避免 TTS 读入崩溃。
17
+ 5. 对社交平台常见噪声做弱归一化:
18
+ - `...... / ……` -> `。`
19
+ - `???!!!` -> `?!`
20
+ - `!!!` -> `!`
21
+ 6. 空格按脚本类型处理:
22
+ - 西文片段内部:连续空格压缩为 1 个。
23
+ - 汉字 / 日文假名片段内部:删除空格。
24
+ - 汉字 / 日文假名 与“拉丁字母类 token / 受保护 token”相邻:保留或补 1 个空格。
25
+ - 汉字 / 日文假名 与纯数字相邻:不强行补空格。
26
+ 7. 轻量处理 Markdown 与换行:
27
+ - `[text](url)` -> `text url`
28
+ - 去掉标题 `#`、引用 `>`、列表前缀
29
+ - 换行转句边界 `。`
30
+
31
+ 非目标
32
+ ------
33
+ 1. 不决定“应该怎么读”。
34
+ 2. 不删除 `[] / {}` 内容。
35
+ 3. 不做 HTML/SSML/语义标签解释。
36
+ """
37
+
38
+ from __future__ import annotations
39
+
40
+ import re
41
+ import unicodedata
42
+
43
+
44
+ # ---------------------------
45
+ # 基础常量与正则
46
+ # ---------------------------
47
+
48
+ # 不依赖空格分词的脚本:汉字 + 日文假名
49
+ _CJK_CHARS = r"\u3400-\u4dbf\u4e00-\u9fff\u3040-\u30ff"
50
+ _CJK = f"[{_CJK_CHARS}]"
51
+
52
+ # 保护占位符
53
+ _PROT = r"___PROT\d+___"
54
+
55
+ # 需要保护的高风险 token
56
+ _URL_RE = re.compile(r"https?://[^\s\u3000,。!?;、)】》〉」』]+")
57
+ _EMAIL_RE = re.compile(r"(?<![\w.+-])[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}(?![\w.-])")
58
+ _MENTION_RE = re.compile(r"(?<![A-Za-z0-9_])@[A-Za-z0-9_]{1,32}")
59
+ _REDDIT_RE = re.compile(r"(?<![A-Za-z0-9_])(?:u|r)/[A-Za-z0-9_]+")
60
+ _HASHTAG_RE = re.compile(r"(?<![A-Za-z0-9_])#(?!\s)[^\s#]+")
61
+
62
+ # `.map` / `.env` / `.gitignore`
63
+ _DOT_TOKEN_RE = re.compile(r"(?<![A-Za-z0-9_])\.(?=[A-Za-z0-9._-]*[A-Za-z0-9])[A-Za-z0-9._-]+")
64
+
65
+ # `app.js.map` / `index.d.ts` / `v2.3.1` / `foo/bar-baz.py` 等
66
+ _FILELIKE_RE = re.compile(
67
+ r"(?<![A-Za-z0-9_])"
68
+ r"(?=[A-Za-z0-9._/+:-]*[A-Za-z])"
69
+ r"(?=[A-Za-z0-9._/+:-]*[._/+:-])"
70
+ r"[A-Za-z0-9](?:[A-Za-z0-9._/+:-]*[A-Za-z0-9])?"
71
+ r"(?![A-Za-z0-9_])"
72
+ )
73
+
74
+ # 参与“中英混排边界补空格”的 token:必须至少含 1 个拉丁字母,或本身就是受保护 token
75
+ _LATINISH = rf"(?:{_PROT}|(?=[A-Za-z0-9._/+:-]*[A-Za-z])[A-Za-z0-9][A-Za-z0-9._/+:-]*)"
76
+
77
+ # 零宽字符
78
+ _ZERO_WIDTH_RE = re.compile(r"[\u200b-\u200d\ufeff]")
79
+
80
+
81
+ # ---------------------------
82
+ # 主函数
83
+ # ---------------------------
84
+
85
+ def normalize_tts_text(text: str) -> str:
86
+ """对 TTS 输入做鲁棒性正则化。"""
87
+ text = _base_cleanup(text)
88
+ text = _normalize_markdown_and_lines(text)
89
+ text, protected = _protect_spans(text)
90
+
91
+ text = _normalize_spaces(text)
92
+ text = _normalize_structural_punctuation(text)
93
+ text = _normalize_repeated_punctuation(text)
94
+ text = _normalize_spaces(text)
95
+
96
+ text = _restore_spans(text, protected)
97
+ return text.strip()
98
+
99
+
100
+ # ---------------------------
101
+ # 具体规则
102
+ # ---------------------------
103
+
104
+ def _base_cleanup(text: str) -> str:
105
+ text = text.replace("\r\n", "\n").replace("\r", "\n").replace("\u3000", " ")
106
+ text = _ZERO_WIDTH_RE.sub("", text)
107
+
108
+ cleaned = []
109
+ for ch in text:
110
+ cat = unicodedata.category(ch)
111
+ if ch in "\n\t " or not cat.startswith("C"):
112
+ cleaned.append(ch)
113
+ return "".join(cleaned)
114
+
115
+
116
+ def _normalize_markdown_and_lines(text: str) -> str:
117
+ # Markdown 链接:[text](url) -> text url
118
+ text = re.sub(r"\[([^\[\]]+?)\]\((https?://[^)\s]+)\)", r"\1 \2", text)
119
+
120
+ lines = []
121
+ for raw in text.splitlines():
122
+ line = raw.strip()
123
+ if not line:
124
+ continue
125
+
126
+ line = re.sub(r"^#{1,6}\s+", "", line) # 标题
127
+ line = re.sub(r"^>\s+", "", line) # 引用
128
+ line = re.sub(r"^[-*+]\s+", "", line) # 无序列表
129
+ line = re.sub(r"^\d+[.)]\s+", "", line) # 有序列表
130
+ lines.append(line)
131
+
132
+ return "。".join(lines) if lines else ""
133
+
134
+
135
+ def _protect_spans(text: str) -> tuple[str, list[str]]:
136
+ protected: list[str] = []
137
+
138
+ def repl(match: re.Match[str]) -> str:
139
+ idx = len(protected)
140
+ protected.append(match.group(0))
141
+ return f"___PROT{idx}___"
142
+
143
+ for pattern in (
144
+ _URL_RE,
145
+ _EMAIL_RE,
146
+ _MENTION_RE,
147
+ _REDDIT_RE,
148
+ _HASHTAG_RE,
149
+ _DOT_TOKEN_RE,
150
+ _FILELIKE_RE,
151
+ ):
152
+ text = pattern.sub(repl, text)
153
+
154
+ return text, protected
155
+
156
+
157
+ def _restore_spans(text: str, protected: list[str]) -> str:
158
+ for idx, original in enumerate(protected):
159
+ text = text.replace(f"___PROT{idx}___", original)
160
+ return text
161
+
162
+
163
+ def _normalize_spaces(text: str) -> str:
164
+ # 统一空白
165
+ text = re.sub(r"[ \t\r\f\v]+", " ", text)
166
+
167
+ # 汉字 / 日文片段内部:删除空格
168
+ text = re.sub(rf"({_CJK})\s+(?={_CJK})", r"\1", text)
169
+
170
+ # 汉字 / 日文 与纯数字之间:删除空格(不强行保留)
171
+ text = re.sub(rf"({_CJK})\s+(?=\d)", r"\1", text)
172
+ text = re.sub(rf"(\d)\s+(?={_CJK})", r"\1", text)
173
+
174
+ # 汉字 / 日文 与拉丁字母类 token / protected token 相邻:保留或补 1 个空格
175
+ text = re.sub(rf"({_CJK})(?=({_LATINISH}))", r"\1 ", text)
176
+ text = re.sub(rf"(({_LATINISH}))(?={_CJK})", r"\1 ", text)
177
+
178
+ # 再压一遍连续空格
179
+ text = re.sub(r" {2,}", " ", text)
180
+
181
+ # 中文标点前后不保留空格
182
+ text = re.sub(r"\s+([,。!?;:、”’」』】)》])", r"\1", text)
183
+ text = re.sub(r"([(【「『《“‘])\s+", r"\1", text)
184
+ text = re.sub(r"([,。!?;:、])\s*", r"\1", text)
185
+
186
+ # ASCII 标点前不留空格;后面的英文空格不强改
187
+ text = re.sub(r"\s+([,.;!?])", r"\1", text)
188
+
189
+ return re.sub(r" {2,}", " ", text).strip()
190
+
191
+
192
+ def _normalize_structural_punctuation(text: str) -> str:
193
+ # 结构性括号:在“结构位置”解包并转成句边界
194
+ # 连续块要支持收敛,因此做两轮
195
+ for _ in range(2):
196
+ text = re.sub(
197
+ r"(^|[。!?!?;;]\s*)[【〖『「]([^】〗』」]+)[】〗』」]\s*",
198
+ r"\1\2。",
199
+ text,
200
+ )
201
+
202
+ # 《》只处理独立标题,不处理嵌入式标题
203
+ # 例:重磅。《新品发布》——现在开始! -> 重磅。新品发布。现在开始!
204
+ text = re.sub(
205
+ r"(^|[。!?!?;;]\s*)《([^》]+)》(?=\s*(?:___PROT\d+___|[—–―-]{2,}|$|[。!?!?;;,,]))",
206
+ r"\1\2",
207
+ text,
208
+ )
209
+
210
+ # 流程 / 映射箭头:转成中文逗号,保留链路结构但避免把 `->` 原样喂给 TTS。
211
+ text = re.sub(
212
+ r"\s*(?:<[-=]+>|[-=]+>|<[-=]+|[→←↔⇒⇐⇔⟶⟵⟷⟹⟸⟺↦↤↪↩])\s*",
213
+ ",",
214
+ text,
215
+ )
216
+
217
+ # 长破折号 / 多连字符:转句边界
218
+ text = re.sub(r"\s*(?:—|–|―|-){2,}\s*", "。", text)
219
+
220
+ return text
221
+
222
+
223
+ def _normalize_repeated_punctuation(text: str) -> str:
224
+ # 省略号 / 连续句点
225
+ text = re.sub(r"(?:\.{3,}|…{2,}|……+)", "。", text)
226
+
227
+ # 同类重复标点
228
+ text = re.sub(r"[。.]{2,}", "。", text)
229
+ text = re.sub(r"[,,]{2,}", ",", text)
230
+ text = re.sub(r"[!!]{2,}", "!", text)
231
+ text = re.sub(r"[??]{2,}", "?", text)
232
+
233
+ # 混合问叹号:收敛到 ?!
234
+ def _mixed_qe(match: re.Match[str]) -> str:
235
+ s = match.group(0)
236
+ has_q = any(ch in s for ch in "??")
237
+ has_e = any(ch in s for ch in "!!")
238
+ if has_q and has_e:
239
+ return "?!"
240
+ return "?" if has_q else "!"
241
+
242
+ text = re.sub(r"[!?!?]{2,}", _mixed_qe, text)
243
+ return text
244
+
245
+
246
+ # ---------------------------
247
+ # 测试
248
+ # ---------------------------
249
+
250
+ TEST_CASES = [
251
+ # 1) .map / dot-leading token / 文件名 / 版本号
252
+ (
253
+ "dot_map_sentence",
254
+ "2026 年 3 月 31 日,安全研究员 Chaofan Shou (@Fried_rice) 发现 Anthropic 的 npm 包中暴露了 .map 文件,",
255
+ "2026年3月31日,安全研究员 Chaofan Shou (@Fried_rice) 发现 Anthropic 的 npm 包中暴露了 .map 文件,",
256
+ ),
257
+ ("dot_tokens", "别把 .env、.npmrc、.gitignore 提交上去。", "别把 .env、.npmrc、.gitignore 提交上去。"),
258
+ ("file_names", "请检查 bundle.min.js、package.json 和 processing_moss_tts.py。", "请检查 bundle.min.js、package.json 和 processing_moss_tts.py。"),
259
+ ("index_d_ts", "index.d.ts 里也有同样的问题。", "index.d.ts 里也有同样的问题。"),
260
+ ("version_build", "Bug 的讨论可以精确到 v2.3.1 (Build 15)。", "Bug 的讨论可以精确到 v2.3.1 (Build 15)。"),
261
+ ("version_rc", "3.0.0-rc.1 还不能上生产。", "3.0.0-rc.1 还不能上生产。"),
262
+ ("jar_name", "fabric-api-0.91.3+1.20.2.jar 需要单独下载。", "fabric-api-0.91.3+1.20.2.jar 需要单独下载。"),
263
+
264
+ # 2) URL / Email / mention / hashtag / Reddit
265
+ ("url", "仓库地址是 https://github.com/instructkr/claude-code", "仓库地址是 https://github.com/instructkr/claude-code"),
266
+ ("email", "联系邮箱:ops+tts@example.ai", "联系邮箱:ops+tts@example.ai"),
267
+ ("mention", "@Fried_rice 说这是 source map 暴露。", "@Fried_rice 说这是 source map 暴露。"),
268
+ ("reddit", "去 r/singularity 看讨论。", "去 r/singularity 看讨论。"),
269
+ ("hashtag_chain", "#张雪峰#张雪峰[话题]#张雪峰事件", "#张雪峰#张雪峰[话题]#张雪峰事件"),
270
+ ("mention_hashtag_boundary", "关注@biscuit0228_��转发#thetime_tbs", "关注 @biscuit0228_ 并转发 #thetime_tbs"),
271
+
272
+ # 3) bracket / 控制 token:保留,不删除
273
+ ("speaker_bracket", "[S1]你好。[S2]收到。", "[S1]你好。[S2]收到。"),
274
+ ("event_bracket", "请模仿 {whisper} 的语气说“别出声”。", "请模仿 {whisper} 的语气说“别出声”。"),
275
+ ("order_bracket", "订单号:[AB-1234-XYZ]", "订单号:[AB-1234-XYZ]"),
276
+
277
+ # 4) 结构性符号:替换成边界,而不是直接删除
278
+ ("struct_headline", "〖重磅〗《新品发布》——现在开始!", "重磅。新品发布。现在开始!"),
279
+ ("struct_notice", "【公告】今天 20:00 维护——预计 30 分钟。", "公告。今天20:00维护。预计30分钟。"),
280
+ ("struct_quote_chain", "『特别提醒』「不要外传」", "特别提醒。不要外传。"),
281
+ ("flow_arrow_chain", "请求接入 -> 身份与策略判定 -> 域服务处理", "请求接入,身份与策略判定,域服务处理"),
282
+ ("flow_arrow_no_space", "A->B", "A,B"),
283
+ ("flow_arrow_unicode", "配置中心→推理编排→运行时执行", "配置中心,推理编排,运行时执行"),
284
+ (
285
+ "flow_arrow_maas_example",
286
+ "MaaS 主链遵循请求接入 -> 身份与策略判定 -> 域服务处理 -> 推理编排 -> 运行时执行的在线数据面结构。Dashboard 不直接承载单次在线推理请求,而是负责统一配置、统一治理、统一运营和统一展示。",
287
+ "MaaS 主链遵循请求接入,身份与策略判定,域服务处理,推理编排,运行时执行的在线数据面结构。Dashboard 不直接承载单次在线推理请求,而是负责统一配置、统一治理、统一运营和统一展示。",
288
+ ),
289
+
290
+ # 5) 嵌入式标题:保留
291
+ ("embedded_title", "我喜欢《哈姆雷特》这本书。", "我喜欢《哈姆雷特》这本书。"),
292
+
293
+ # 6) 重复标点 / 社交噪声
294
+ ("noise_qe", "真的假的???!!!", "真的假的?!"),
295
+ ("noise_ellipsis", "这个包把 app.js.map 也发上去了......太离谱了!!!", "这个包把 app.js.map 也发上去了。太离谱了!"),
296
+ ("noise_ellipsis_cn", "【系统提示】请模仿{sad}低沉语气,说“今天下雨了……”", "系统提示。请模仿{sad}低沉语气,说“今天下雨了。”"),
297
+
298
+ # 7) 空格规则:英文压缩、中文删除、中英混排保留边界
299
+ ("english_spaces", "This is a test.", "This is a test."),
300
+ ("chinese_spaces", "这 是 一 段 含有多种空白的文本。", "这是一段含有多种空白的文本。"),
301
+ ("mixed_spaces_1", "这是Anthropic的npm包", "这是 Anthropic 的 npm 包"),
302
+ ("mixed_spaces_2", "今天update到v2.3.1了", "今天 update 到 v2.3.1 了"),
303
+ ("mixed_spaces_3", "处理app.js.map文件", "处理 app.js.map 文件"),
304
+
305
+ # 8) Markdown / 列表 / 换行
306
+ ("markdown_link", "详情见 [release note](https://github.com/example/release)", "详情见 release note https://github.com/example/release"),
307
+ ("markdown_heading", "# I made a free open source app to help with markdown files", "I made a free open source app to help with markdown files"),
308
+ ("list_lines", "- 修复 .map 泄露\n- 发布 v2.3.1", "修复 .map 泄露。发布 v2.3.1"),
309
+ ("numbered_lines", "1. 安装依赖\n2. 运行测试\n3. 发布 v2.3.1", "安装依赖。运行测试。发布 v2.3.1"),
310
+ ("newlines", "第一行\n第二行\n第三行", "第一行。第二行。第三行"),
311
+
312
+ # 9) 零宽字符 / 幂等性
313
+ ("zero_width_url", "详见 https://x.com/\u200bSafety", "详见 https://x.com/Safety"),
314
+ ]
315
+
316
+
317
+ def run_tests(verbose: bool = True) -> None:
318
+ failed = []
319
+
320
+ for name, text, expected in TEST_CASES:
321
+ actual = normalize_tts_text(text)
322
+ if actual != expected:
323
+ failed.append((name, text, expected, actual))
324
+ continue
325
+
326
+ # 幂等性:第二次归一化不应继续改动结果
327
+ second = normalize_tts_text(actual)
328
+ if second != actual:
329
+ failed.append((name + "_idempotence", actual, actual, second))
330
+
331
+ if failed:
332
+ lines = ["\nTEST FAILED:\n"]
333
+ for name, text, expected, actual in failed:
334
+ lines.append(f"[{name}]")
335
+ lines.append(f"input : {text}")
336
+ lines.append(f"expected: {expected}")
337
+ lines.append(f"actual : {actual}")
338
+ lines.append("")
339
+ raise AssertionError("\n".join(lines))
340
+
341
+ if verbose:
342
+ print(f"All {len(TEST_CASES)} tests passed.")
343
+
344
+
345
+ if __name__ == "__main__":
346
+ run_tests()
vocab.json ADDED
The diff for this file is too large to render. See raw diff