yuekaiz commited on
Commit
0955e96
·
1 Parent(s): 714c642

add triton

Browse files
runtime/triton_trtllm/build.sh ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ pip install -r /workspace_yuekai/spark-tts/Spark-TTS/requirements.txt
3
+ model_repo=./model_repo_test
4
+ rm -rf $model_repo
5
+
6
+ cp -r ./model_repo $model_repo
7
+
8
+ ENGINE_PATH=/workspace_yuekai/spark-tts/TensorRT-LLM/examples/qwen/Spark-TTS-0.5B_trt_engines_1gpu_bfloat16
9
+ MAX_QUEUE_DELAY_MICROSECONDS=0
10
+ gpu_device_ids=0
11
+ python3 fill_template.py -i ${model_repo}/tensorrt_llm/config.pbtxt gpu_device_ids:${gpu_device_ids},triton_backend:tensorrtllm,triton_max_batch_size:16,decoupled_mode:False,max_beam_width:1,engine_dir:${ENGINE_PATH},max_tokens_in_paged_kv_cache:2560,max_attention_window_size:2560,kv_cache_free_gpu_mem_fraction:0.5,exclude_input_in_output:True,enable_kv_cache_reuse:False,batching_strategy:inflight_fused_batching,max_queue_delay_microseconds:${MAX_QUEUE_DELAY_MICROSECONDS},encoder_input_features_data_type:TYPE_FP16,logits_datatype:TYPE_FP32
12
+
13
+ # enable_context_fmha_fp32_acc:${ENABLE_CONTEXT_FMHA_FP32_ACC}
14
+ export PYTHONPATH=/workspace_yuekai/spark-tts/Spark-TTS/
15
+ CUDA_VISIBLE_DEVICES=${gpu_device_ids} tritonserver --model-repository ${model_repo}
runtime/triton_trtllm/build_engine.sh ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+
4
+ # model_dir=./Qwen2.5-0.5B-Instruct/
5
+ # output_dir=./tllm_checkpoint_1gpu_fp16
6
+ # trt_engines_dir=./trt_engines
7
+
8
+ model_dir=/workspace_yuekai/spark-tts/Spark-TTS/pretrained_models/Spark-TTS-0.5B/LLM
9
+ base_name=Spark-TTS-0.5B
10
+ dtype=bfloat16
11
+ output_dir=./${base_name}_tllm_checkpoint_1gpu_${dtype}
12
+ trt_engines_dir=./${base_name}_trt_engines_1gpu_${dtype}
13
+
14
+
15
+ # python convert_checkpoint.py --model_dir $model_dir \
16
+ # --output_dir $output_dir \
17
+ # --dtype $dtype || exit 1
18
+
19
+ trtllm-build --checkpoint_dir $output_dir \
20
+ --output_dir $trt_engines_dir \
21
+ --max_batch_size 16 \
22
+ --max_num_tokens 32768 \
23
+ --gemm_plugin $dtype || exit 1
24
+ # trtllm-build --checkpoint_dir $output_dir \
25
+ # --output_dir $trt_engines_dir \
26
+ # --max_batch_size 16 \
27
+ # --max_num_tokens 32768 \
28
+ # --gemm_plugin $dtype || exit 1
29
+
30
+ python3 ../run.py --input_file /workspace_yuekai/spark-tts/Spark-TTS/model_inputs.npy \
31
+ --max_output_len=1500 \
32
+ --tokenizer_dir $model_dir \
33
+ --top_k 50 \
34
+ --top_p 0.95 \
35
+ --temperature 0.8 \
36
+ --output_npy ./output.npy \
37
+ --engine_dir=$trt_engines_dir || exit 1
38
+
39
+
40
+ # python3 ../run.py --input_file /workspace_yuekai/spark-tts/Spark-TTS/model_inputs.npy \
41
+ # --max_output_len=1500 \
42
+ # --tokenizer_dir $model_dir \
43
+ # --top_k 50 \
44
+ # --top_p 0.95 \
45
+ # --temperature 0.8 \
46
+ # --engine_dir=$trt_engines_dir || exit 1
runtime/triton_trtllm/client.py ADDED
@@ -0,0 +1,487 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # Copyright 2022 Xiaomi Corp. (authors: Fangjun Kuang)
3
+ # 2023 Nvidia (authors: Yuekai Zhang)
4
+ # 2023 Recurrent.ai (authors: Songtao Shi)
5
+ # See LICENSE for clarification regarding multiple authors
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ """
19
+ This script supports to load manifest files in kaldi format and sends it to the server
20
+ for decoding, in parallel.
21
+
22
+ Usage:
23
+ # For offline F5-TTS
24
+ # huggingface dataset
25
+ dataset_name=yuekai/aishell
26
+ subset_name=test
27
+ split_name=test
28
+ num_task=32
29
+ python3 client.py \
30
+ --server-addr localhost \
31
+ --model-name infer_bls \
32
+ --num-tasks $num_task \
33
+ --text-prompt "<|startoftranscript|><|zh|><|transcribe|><|notimestamps|>" \
34
+ --huggingface_dataset $dataset_name \
35
+ --subset_name $subset_name \
36
+ --split_name $split_name \
37
+ --log-dir ./log_sherpa_multi_hans_whisper_large_ifb_$num_task
38
+ """
39
+
40
+ import argparse
41
+ import asyncio
42
+ import json
43
+
44
+ import os
45
+ import time
46
+ import types
47
+ from pathlib import Path
48
+
49
+ import numpy as np
50
+ import soundfile as sf
51
+ import tritonclient
52
+ import tritonclient.grpc.aio as grpcclient
53
+ from tritonclient.utils import np_to_triton_dtype
54
+
55
+
56
+
57
+ def write_triton_stats(stats, summary_file):
58
+ with open(summary_file, "w") as summary_f:
59
+ model_stats = stats["model_stats"]
60
+ # write a note, the log is from triton_client.get_inference_statistics(), to better human readability
61
+ summary_f.write(
62
+ "The log is parsing from triton_client.get_inference_statistics(), to better human readability. \n"
63
+ )
64
+ summary_f.write("To learn more about the log, please refer to: \n")
65
+ summary_f.write(
66
+ "1. https://github.com/triton-inference-server/server/blob/main/docs/user_guide/metrics.md \n"
67
+ )
68
+ summary_f.write(
69
+ "2. https://github.com/triton-inference-server/server/issues/5374 \n\n"
70
+ )
71
+ summary_f.write(
72
+ "To better improve throughput, we always would like let requests wait in the queue for a while, and then execute them with a larger batch size. \n"
73
+ )
74
+ summary_f.write(
75
+ "However, there is a trade-off between the increased queue time and the increased batch size. \n"
76
+ )
77
+ summary_f.write(
78
+ "You may change 'max_queue_delay_microseconds' and 'preferred_batch_size' in the model configuration file to achieve this. \n"
79
+ )
80
+ summary_f.write(
81
+ "See https://github.com/triton-inference-server/server/blob/main/docs/user_guide/model_configuration.md#delayed-batching for more details. \n\n"
82
+ )
83
+ for model_state in model_stats:
84
+ if "last_inference" not in model_state:
85
+ continue
86
+ summary_f.write(f"model name is {model_state['name']} \n")
87
+ model_inference_stats = model_state["inference_stats"]
88
+ total_queue_time_s = int(model_inference_stats["queue"]["ns"]) / 1e9
89
+ total_infer_time_s = int(model_inference_stats["compute_infer"]["ns"]) / 1e9
90
+ total_input_time_s = int(model_inference_stats["compute_input"]["ns"]) / 1e9
91
+ total_output_time_s = (
92
+ int(model_inference_stats["compute_output"]["ns"]) / 1e9
93
+ )
94
+ summary_f.write(
95
+ f"queue time {total_queue_time_s:<5.2f} s, compute infer time {total_infer_time_s:<5.2f} s, compute input time {total_input_time_s:<5.2f} s, compute output time {total_output_time_s:<5.2f} s \n" # noqa
96
+ )
97
+ model_batch_stats = model_state["batch_stats"]
98
+ for batch in model_batch_stats:
99
+ batch_size = int(batch["batch_size"])
100
+ compute_input = batch["compute_input"]
101
+ compute_output = batch["compute_output"]
102
+ compute_infer = batch["compute_infer"]
103
+ batch_count = int(compute_infer["count"])
104
+ assert (
105
+ compute_infer["count"]
106
+ == compute_output["count"]
107
+ == compute_input["count"]
108
+ )
109
+ compute_infer_time_ms = int(compute_infer["ns"]) / 1e6
110
+ compute_input_time_ms = int(compute_input["ns"]) / 1e6
111
+ compute_output_time_ms = int(compute_output["ns"]) / 1e6
112
+ summary_f.write(
113
+ f"execuate inference with batch_size {batch_size:<2} total {batch_count:<5} times, total_infer_time {compute_infer_time_ms:<9.2f} ms, avg_infer_time {compute_infer_time_ms:<9.2f}/{batch_count:<5}={compute_infer_time_ms/batch_count:.2f} ms, avg_infer_time_per_sample {compute_infer_time_ms:<9.2f}/{batch_count:<5}/{batch_size}={compute_infer_time_ms/batch_count/batch_size:.2f} ms \n" # noqa
114
+ )
115
+ # summary_f.write(
116
+ # f"input {compute_input_time_ms:<9.2f} ms, avg {compute_input_time_ms/batch_count:.2f} ms, " # noqa
117
+ # )
118
+ # summary_f.write(
119
+ # f"output {compute_output_time_ms:<9.2f} ms, avg {compute_output_time_ms/batch_count:.2f} ms \n" # noqa
120
+ # )
121
+
122
+
123
+
124
+ def get_args():
125
+ parser = argparse.ArgumentParser(
126
+ formatter_class=argparse.ArgumentDefaultsHelpFormatter
127
+ )
128
+
129
+ parser.add_argument(
130
+ "--server-addr",
131
+ type=str,
132
+ default="localhost",
133
+ help="Address of the server",
134
+ )
135
+
136
+ parser.add_argument(
137
+ "--server-port",
138
+ type=int,
139
+ default=8001,
140
+ help="Grpc port of the triton server, default is 8001",
141
+ )
142
+
143
+ parser.add_argument(
144
+ "--reference-audio",
145
+ type=str,
146
+ default=None,
147
+ help="Path to a single audio file. It can't be specified at the same time with --manifest-dir",
148
+ )
149
+
150
+ parser.add_argument(
151
+ "--reference-text",
152
+ type=str,
153
+ default="",
154
+ help="",
155
+ )
156
+
157
+ parser.add_argument(
158
+ "--target-text",
159
+ type=str,
160
+ default="",
161
+ help="",
162
+ )
163
+
164
+ parser.add_argument(
165
+ "--huggingface-dataset",
166
+ type=str,
167
+ default="yuekai/seed_tts",
168
+ help="dataset name in huggingface dataset hub",
169
+ )
170
+
171
+ parser.add_argument(
172
+ "--split-name",
173
+ type=str,
174
+ default="wenetspeech4tts",
175
+ choices=["wenetspeech4tts", "test_zh", "test_en", "test_hard"],
176
+ help="dataset split name, default is 'test'",
177
+ )
178
+
179
+ parser.add_argument(
180
+ "--manifest-path",
181
+ type=str,
182
+ default=None,
183
+ help="Path to the manifest dir which includes wav.scp trans.txt files.",
184
+ )
185
+
186
+ parser.add_argument(
187
+ "--model-name",
188
+ type=str,
189
+ default="f5_tts",
190
+ choices=[
191
+ "f5_tts", "spark_tts"
192
+ ],
193
+ help="triton model_repo module name to request: transducer for k2, attention_rescoring for wenet offline, streaming_wenet for wenet streaming, infer_pipeline for paraformer large offline",
194
+ )
195
+
196
+ parser.add_argument(
197
+ "--num-tasks",
198
+ type=int,
199
+ default=1,
200
+ help="Number of concurrent tasks for sending",
201
+ )
202
+
203
+ parser.add_argument(
204
+ "--log-interval",
205
+ type=int,
206
+ default=5,
207
+ help="Controls how frequently we print the log.",
208
+ )
209
+
210
+ parser.add_argument(
211
+ "--compute-wer",
212
+ action="store_true",
213
+ default=False,
214
+ help="""True to compute WER.
215
+ """,
216
+ )
217
+
218
+ parser.add_argument(
219
+ "--log-dir",
220
+ type=str,
221
+ required=False,
222
+ default="./tmp",
223
+ help="log directory",
224
+ )
225
+
226
+ parser.add_argument(
227
+ "--batch-size",
228
+ type=int,
229
+ default=1,
230
+ help="Inference batch_size per request for offline mode.",
231
+ )
232
+
233
+ return parser.parse_args()
234
+
235
+
236
+ def load_audio(wav_path, target_sample_rate=16000):
237
+ assert target_sample_rate == 16000, "hard coding in server"
238
+ if isinstance(wav_path, dict):
239
+ waveform = wav_path["array"]
240
+ sample_rate = wav_path["sampling_rate"]
241
+ else:
242
+ waveform, sample_rate = sf.read(wav_path)
243
+ if sample_rate != target_sample_rate:
244
+ from scipy.signal import resample
245
+ num_samples = int(len(waveform) * (target_sample_rate / sample_rate))
246
+ waveform = resample(waveform, num_samples)
247
+ return waveform, target_sample_rate
248
+
249
+ async def send(
250
+ manifest_item_list: list,
251
+ name: str,
252
+ triton_client: tritonclient.grpc.aio.InferenceServerClient,
253
+ protocol_client: types.ModuleType,
254
+ log_interval: int,
255
+ model_name: str,
256
+ padding_duration: int = None,
257
+ audio_save_dir: str = "./",
258
+ ):
259
+ total_duration = 0.0
260
+ results = []
261
+ latency_data = []
262
+ task_id = int(name[5:])
263
+
264
+ print(f"manifest_item_list: {manifest_item_list}")
265
+ for i, item in enumerate(manifest_item_list):
266
+ if i % log_interval == 0:
267
+ print(f"{name}: {i}/{len(manifest_item_list)}")
268
+ waveform, sample_rate = load_audio(item["audio_filepath"], target_sample_rate=16000)
269
+ duration = len(waveform) / sample_rate
270
+ lengths = np.array([[len(waveform)]], dtype=np.int32)
271
+
272
+ reference_text, target_text = item["reference_text"], item["target_text"]
273
+
274
+ estimated_target_duration = duration / len(reference_text) * len(target_text)
275
+
276
+ if padding_duration:
277
+ # padding to nearset 10 seconds
278
+ samples = np.zeros(
279
+ (
280
+ 1,
281
+ padding_duration
282
+ * sample_rate
283
+ * ((int(duration) // padding_duration) + 1),
284
+ ),
285
+ dtype=np.float32,
286
+ )
287
+
288
+ samples[0, : len(waveform)] = waveform
289
+ else:
290
+ samples = waveform
291
+
292
+ samples = samples.reshape(1, -1).astype(np.float32)
293
+
294
+ inputs = [
295
+ protocol_client.InferInput(
296
+ "reference_wav", samples.shape, np_to_triton_dtype(samples.dtype)
297
+ ),
298
+ protocol_client.InferInput(
299
+ "reference_wav_len", lengths.shape, np_to_triton_dtype(lengths.dtype)
300
+ ),
301
+ protocol_client.InferInput("reference_text", [1, 1], "BYTES"),
302
+ protocol_client.InferInput("target_text", [1, 1], "BYTES")
303
+ ]
304
+ inputs[0].set_data_from_numpy(samples)
305
+ inputs[1].set_data_from_numpy(lengths)
306
+
307
+ input_data_numpy = np.array([reference_text], dtype=object)
308
+ input_data_numpy = input_data_numpy.reshape((1, 1))
309
+ inputs[2].set_data_from_numpy(input_data_numpy)
310
+
311
+ input_data_numpy = np.array([target_text], dtype=object)
312
+ input_data_numpy = input_data_numpy.reshape((1, 1))
313
+ inputs[3].set_data_from_numpy(input_data_numpy)
314
+
315
+ outputs = [protocol_client.InferRequestedOutput("waveform")]
316
+
317
+ sequence_id = 100000000 + i + task_id * 10
318
+ start = time.time()
319
+ response = await triton_client.infer(
320
+ model_name, inputs, request_id=str(sequence_id), outputs=outputs
321
+ )
322
+
323
+ audio = response.as_numpy("waveform").reshape(-1)
324
+
325
+ end = time.time() - start
326
+
327
+ audio_save_path = os.path.join(
328
+ audio_save_dir, f"{item['target_audio_path']}.wav"
329
+ )
330
+ sf.write(audio_save_path, audio, 16000, "PCM_16")
331
+
332
+ latency_data.append((end, estimated_target_duration))
333
+ total_duration += estimated_target_duration
334
+
335
+ return total_duration, latency_data
336
+
337
+ def load_manifests(manifest_path):
338
+ with open(manifest_path, "r") as f:
339
+ manifest_list = []
340
+ for line in f:
341
+ assert len(line.strip().split("|")) == 4
342
+ utt, prompt_text, prompt_wav, gt_text = line.strip().split("|")
343
+ utt = Path(utt).stem
344
+ # gt_wav = os.path.join(os.path.dirname(manifest_path), "wavs", utt + ".wav")
345
+ if not os.path.isabs(prompt_wav):
346
+ prompt_wav = os.path.join(os.path.dirname(manifest_path), prompt_wav)
347
+ manifest_list.append(
348
+ {
349
+ "audio_filepath": prompt_wav,
350
+ "reference_text": prompt_text,
351
+ "target_text": gt_text,
352
+ "target_audio_path": utt
353
+ }
354
+ )
355
+ return manifest_list
356
+
357
+
358
+ def split_data(data, k):
359
+ n = len(data)
360
+ if n < k:
361
+ print(
362
+ f"Warning: the length of the input list ({n}) is less than k ({k}). Setting k to {n}."
363
+ )
364
+ k = n
365
+
366
+ quotient = n // k
367
+ remainder = n % k
368
+
369
+ result = []
370
+ start = 0
371
+ for i in range(k):
372
+ if i < remainder:
373
+ end = start + quotient + 1
374
+ else:
375
+ end = start + quotient
376
+
377
+ result.append(data[start:end])
378
+ start = end
379
+
380
+ return result
381
+
382
+
383
+ async def main():
384
+ args = get_args()
385
+ url = f"{args.server_addr}:{args.server_port}"
386
+
387
+ triton_client = grpcclient.InferenceServerClient(url=url, verbose=False)
388
+ protocol_client = grpcclient
389
+
390
+ if args.reference_audio:
391
+ args.num_tasks = 1
392
+ args.log_interval = 1
393
+ manifest_item_list = [
394
+ {
395
+ "reference_text": args.reference_text,
396
+ "target_text": args.target_text,
397
+ "audio_filepath": args.reference_audio,
398
+ "target_audio_path": "test",
399
+ }
400
+ ]
401
+ elif args.huggingface_dataset:
402
+ import datasets
403
+
404
+ dataset = datasets.load_dataset(
405
+ args.huggingface_dataset,
406
+ split=args.split_name,
407
+ trust_remote_code=True,
408
+ )
409
+ manifest_item_list = []
410
+ for i in range(len(dataset)):
411
+ manifest_item_list.append(
412
+ {
413
+ "audio_filepath": dataset[i]["prompt_audio"],
414
+ "reference_text": dataset[i]["prompt_text"],
415
+ "target_audio_path": dataset[i]["id"],
416
+ "target_text": dataset[i]["target_text"],
417
+ }
418
+ )
419
+ else:
420
+ manifest_item_list = load_manifests(args.manifest_path)
421
+
422
+ args.num_tasks = min(args.num_tasks, len(manifest_item_list))
423
+ manifest_item_list = split_data(manifest_item_list, args.num_tasks)
424
+
425
+ os.makedirs(args.log_dir, exist_ok=True)
426
+ tasks = []
427
+ start_time = time.time()
428
+ for i in range(args.num_tasks):
429
+ task = asyncio.create_task(
430
+ send(
431
+ manifest_item_list[i],
432
+ name=f"task-{i}",
433
+ triton_client=triton_client,
434
+ protocol_client=protocol_client,
435
+ log_interval=args.log_interval,
436
+ model_name=args.model_name,
437
+ audio_save_dir=args.log_dir,
438
+ padding_duration=10,
439
+ )
440
+ )
441
+ tasks.append(task)
442
+
443
+ ans_list = await asyncio.gather(*tasks)
444
+
445
+ end_time = time.time()
446
+ elapsed = end_time - start_time
447
+
448
+
449
+ total_duration = 0.0
450
+ latency_data = []
451
+ for ans in ans_list:
452
+ total_duration += ans[0]
453
+ latency_data += ans[1]
454
+
455
+ rtf = elapsed / total_duration
456
+
457
+ s = f"RTF: {rtf:.4f}\n"
458
+ s += f"total_duration: {total_duration:.3f} seconds\n"
459
+ s += f"({total_duration/3600:.2f} hours)\n"
460
+ s += f"processing time: {elapsed:.3f} seconds " f"({elapsed/3600:.2f} hours)\n"
461
+
462
+ latency_list = [chunk_end for (chunk_end, chunk_duration) in latency_data]
463
+ latency_ms = sum(latency_list) / float(len(latency_list)) * 1000.0
464
+ latency_variance = np.var(latency_list, dtype=np.float64) * 1000.0
465
+ s += f"latency_variance: {latency_variance:.2f}\n"
466
+ s += f"latency_50_percentile_ms: {np.percentile(latency_list, 50) * 1000.0:.2f}\n"
467
+ s += f"latency_90_percentile_ms: {np.percentile(latency_list, 90) * 1000.0:.2f}\n"
468
+ s += f"latency_95_percentile_ms: {np.percentile(latency_list, 95) * 1000.0:.2f}\n"
469
+ s += f"latency_99_percentile_ms: {np.percentile(latency_list, 99) * 1000.0:.2f}\n"
470
+ s += f"average_latency_ms: {latency_ms:.2f}\n"
471
+
472
+ print(s)
473
+ if args.manifest_path:
474
+ name = Path(args.manifest_path).stem
475
+ elif args.split_name:
476
+ name = args.split_name
477
+ with open(f"{args.log_dir}/rtf-{name}.txt", "w") as f:
478
+ f.write(s)
479
+
480
+ stats = await triton_client.get_inference_statistics(model_name="", as_json=True)
481
+ write_triton_stats(stats, f"{args.log_dir}/stats_summary-{name}.txt")
482
+
483
+ metadata = await triton_client.get_model_config(model_name=args.model_name, as_json=True)
484
+ with open(f"{args.log_dir}/model_config-{name}.json", "w") as f:
485
+ json.dump(metadata, f, indent=4)
486
+ if __name__ == "__main__":
487
+ asyncio.run(main())
runtime/triton_trtllm/convert_checkpoint.py ADDED
@@ -0,0 +1,335 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import os
3
+ import time
4
+ import traceback
5
+ from concurrent.futures import ThreadPoolExecutor, as_completed
6
+
7
+ from transformers import AutoConfig
8
+
9
+ import tensorrt_llm
10
+ from tensorrt_llm._utils import release_gc
11
+ from tensorrt_llm.logger import logger
12
+ from tensorrt_llm.mapping import Mapping
13
+ from tensorrt_llm.models import QWenForCausalLM
14
+ from tensorrt_llm.models.modeling_utils import QuantConfig
15
+ from tensorrt_llm.quantization import QuantAlgo
16
+
17
+
18
+ def parse_arguments():
19
+ parser = argparse.ArgumentParser()
20
+ parser.add_argument('--model_dir', type=str, default=None, required=True)
21
+ parser.add_argument('--tp_size',
22
+ type=int,
23
+ default=1,
24
+ help='N-way tensor parallelism size')
25
+ parser.add_argument('--pp_size',
26
+ type=int,
27
+ default=1,
28
+ help='N-way pipeline parallelism size')
29
+ parser.add_argument(
30
+ '--dtype',
31
+ type=str,
32
+ default='auto',
33
+ choices=['auto', 'float16', 'bfloat16', 'float32'],
34
+ help=
35
+ "The data type for the model weights and activations if not quantized. "
36
+ "If 'auto', the data type is automatically inferred from the source model; "
37
+ "however, if the source dtype is float32, it is converted to float16.")
38
+ parser.add_argument(
39
+ '--use_weight_only',
40
+ default=False,
41
+ action="store_true",
42
+ help='Quantize weights for the various GEMMs to INT4/INT8.'
43
+ 'See --weight_only_precision to set the precision')
44
+ parser.add_argument(
45
+ '--disable_weight_only_quant_plugin',
46
+ default=False,
47
+ action="store_true",
48
+ help=
49
+ 'By default, using plugin implementation for weight quantization. Enabling disable_weight_only_quant_plugin flag will use ootb implementation instead of plugin.'
50
+ 'You must also use --use_weight_only for that argument to have an impact.'
51
+ )
52
+ parser.add_argument(
53
+ '--weight_only_precision',
54
+ const='int8',
55
+ type=str,
56
+ nargs='?',
57
+ default='int8',
58
+ choices=['int8', 'int4', 'int4_gptq'],
59
+ help=
60
+ 'Define the precision for the weights when using weight-only quantization.'
61
+ 'You must also use --use_weight_only for that argument to have an impact.'
62
+ )
63
+ parser.add_argument(
64
+ '--calib_dataset',
65
+ type=str,
66
+ default='ccdv/cnn_dailymail',
67
+ help=
68
+ "The huggingface dataset name or the local directory of the dataset for calibration."
69
+ )
70
+ parser.add_argument(
71
+ "--smoothquant",
72
+ "-sq",
73
+ type=float,
74
+ default=None,
75
+ help="Set the α parameter (see https://arxiv.org/pdf/2211.10438.pdf)"
76
+ " to Smoothquant the model, and output int8 weights."
77
+ " A good first try is 0.5. Must be in [0, 1]")
78
+ parser.add_argument(
79
+ '--per_channel',
80
+ action="store_true",
81
+ default=False,
82
+ help=
83
+ 'By default, we use a single static scaling factor for the GEMM\'s result. '
84
+ 'per_channel instead uses a different static scaling factor for each channel. '
85
+ 'The latter is usually more accurate, but a little slower.')
86
+ parser.add_argument(
87
+ '--per_token',
88
+ action="store_true",
89
+ default=False,
90
+ help=
91
+ 'By default, we use a single static scaling factor to scale activations in the int8 range. '
92
+ 'per_token chooses at run time, and for each token, a custom scaling factor. '
93
+ 'The latter is usually more accurate, but a little slower.')
94
+ parser.add_argument(
95
+ '--int8_kv_cache',
96
+ default=False,
97
+ action="store_true",
98
+ help=
99
+ 'By default, we use dtype for KV cache. int8_kv_cache chooses int8 quantization for KV'
100
+ )
101
+ parser.add_argument(
102
+ '--per_group',
103
+ default=False,
104
+ action="store_true",
105
+ help=
106
+ 'By default, we use a single static scaling factor to scale weights in the int4 range. '
107
+ 'per_group chooses at run time, and for each group, a custom scaling factor. '
108
+ 'The flag is built for GPTQ/AWQ quantization.')
109
+
110
+ parser.add_argument('--group_size',
111
+ type=int,
112
+ default=128,
113
+ help='Group size used in GPTQ quantization.')
114
+
115
+ parser.add_argument("--load_model_on_cpu", action="store_true")
116
+ parser.add_argument(
117
+ '--use_parallel_embedding',
118
+ action="store_true",
119
+ default=False,
120
+ help=
121
+ 'By default embedding parallelism is disabled. By setting this flag, embedding parallelism is enabled'
122
+ )
123
+ parser.add_argument(
124
+ '--embedding_sharding_dim',
125
+ type=int,
126
+ default=0,
127
+ choices=[0, 1],
128
+ help=
129
+ 'By default the embedding lookup table is sharded along vocab dimension (embedding_sharding_dim=0). '
130
+ 'To shard it along hidden dimension, set embedding_sharding_dim=1'
131
+ 'Note: embedding sharing is only enabled when embedding_sharding_dim = 0'
132
+ )
133
+ parser.add_argument('--output_dir',
134
+ type=str,
135
+ default='tllm_checkpoint',
136
+ help='The path to save the TensorRT-LLM checkpoint')
137
+ parser.add_argument(
138
+ '--workers',
139
+ type=int,
140
+ default=1,
141
+ help='The number of workers for converting checkpoint in parallel')
142
+ parser.add_argument(
143
+ '--moe_tp_size',
144
+ type=int,
145
+ default=-1,
146
+ help=
147
+ 'N-way tensor parallelism size for MOE, default is tp_size, which will do tp-only for MoE'
148
+ )
149
+ parser.add_argument(
150
+ '--moe_ep_size',
151
+ type=int,
152
+ default=-1,
153
+ help=
154
+ 'N-way expert parallelism size for MOE, default is 1, which will do tp-only for MoE'
155
+ )
156
+ args = parser.parse_args()
157
+ return args
158
+
159
+
160
+ def args_to_quant_config(args: argparse.Namespace) -> QuantConfig:
161
+ '''return config dict with quantization info based on the command line args
162
+ '''
163
+ quant_config = QuantConfig()
164
+ if args.use_weight_only:
165
+ if args.weight_only_precision == 'int8':
166
+ quant_config.quant_algo = QuantAlgo.W8A16
167
+ elif args.weight_only_precision == 'int4':
168
+ quant_config.quant_algo = QuantAlgo.W4A16
169
+ elif args.smoothquant:
170
+ quant_config.smoothquant_val = args.smoothquant
171
+ if args.per_channel:
172
+ if args.per_token:
173
+ quant_config.quant_algo = QuantAlgo.W8A8_SQ_PER_CHANNEL_PER_TOKEN_PLUGIN
174
+ else:
175
+ quant_config.quant_algo = QuantAlgo.W8A8_SQ_PER_CHANNEL_PER_TENSOR_PLUGIN
176
+ else:
177
+ if args.per_token:
178
+ quant_config.quant_algo = QuantAlgo.W8A8_SQ_PER_TENSOR_PER_TOKEN_PLUGIN
179
+ else:
180
+ quant_config.quant_algo = QuantAlgo.W8A8_SQ_PER_TENSOR_PLUGIN
181
+
182
+ if args.int8_kv_cache:
183
+ quant_config.kv_cache_quant_algo = QuantAlgo.INT8
184
+
185
+ if args.weight_only_precision == 'int4_gptq':
186
+ quant_config.group_size = args.group_size
187
+ quant_config.has_zero_point = True
188
+ quant_config.pre_quant_scale = False
189
+ quant_config.quant_algo = QuantAlgo.W4A16_GPTQ
190
+
191
+ return quant_config
192
+
193
+
194
+ def update_quant_config_from_hf(quant_config, hf_config,
195
+ override_fields) -> tuple[QuantConfig, dict]:
196
+ hf_config_dict = hf_config.to_dict()
197
+ if hf_config_dict.get('quantization_config'):
198
+ # update the quant_algo, and clamp_val.
199
+ if hf_config_dict['quantization_config'].get('quant_method') == 'awq':
200
+ logger.info(
201
+ "Load quantization configs from huggingface model_config.")
202
+ quant_config.quant_algo = QuantAlgo.W4A16_GPTQ
203
+ quant_config.group_size = hf_config_dict['quantization_config'].get(
204
+ 'group_size', 128)
205
+ quant_config.has_zero_point = hf_config_dict[
206
+ 'quantization_config'].get('zero_point', False)
207
+ override_fields.update({"use_autoawq": True})
208
+ elif hf_config_dict['quantization_config'].get(
209
+ 'quant_method') == 'gptq':
210
+ logger.info(
211
+ "Load quantization configs from huggingface model_config.")
212
+ desc_act = hf_config_dict['quantization_config'].get(
213
+ 'desc_act', False)
214
+ if desc_act:
215
+ raise ValueError("GPTQ with desc_act=True is not implemented!")
216
+ quant_config.quant_algo = QuantAlgo.W4A16_GPTQ
217
+ quant_config.group_size = hf_config_dict['quantization_config'].get(
218
+ 'group_size', 128)
219
+ quant_config.has_zero_point = hf_config_dict[
220
+ 'quantization_config'].get('sym', False)
221
+ return quant_config, override_fields
222
+
223
+
224
+ def args_to_build_options(args):
225
+ return {
226
+ 'use_parallel_embedding': args.use_parallel_embedding,
227
+ 'embedding_sharding_dim': args.embedding_sharding_dim,
228
+ 'disable_weight_only_quant_plugin':
229
+ args.disable_weight_only_quant_plugin
230
+ }
231
+
232
+
233
+ def convert_and_save_hf(args):
234
+ model_dir = args.model_dir
235
+ world_size = args.tp_size * args.pp_size
236
+ # Need to convert the cli args to the kay-value pairs and override them in the generate config dict.
237
+ # Ideally these fields will be moved out of the config and pass them into build API, keep them here for compatibility purpose for now,
238
+ # before the refactor is done.
239
+ override_fields = {}
240
+ override_fields.update(args_to_build_options(args))
241
+ quant_config = args_to_quant_config(args)
242
+
243
+ try:
244
+ hf_config = AutoConfig.from_pretrained(model_dir,
245
+ trust_remote_code=True)
246
+ quant_config, override_fields = update_quant_config_from_hf(
247
+ quant_config, hf_config, override_fields)
248
+ except:
249
+ logger.warning("AutoConfig cannot load the huggingface config.")
250
+
251
+ if args.smoothquant is not None or args.int8_kv_cache:
252
+ mapping = Mapping(
253
+ world_size=world_size,
254
+ tp_size=args.tp_size,
255
+ pp_size=args.pp_size,
256
+ moe_tp_size=args.moe_tp_size,
257
+ moe_ep_size=args.moe_ep_size,
258
+ )
259
+ QWenForCausalLM.quantize(args.model_dir,
260
+ args.output_dir,
261
+ dtype=args.dtype,
262
+ mapping=mapping,
263
+ quant_config=quant_config,
264
+ calib_dataset=args.calib_dataset,
265
+ **override_fields)
266
+ else:
267
+
268
+ def convert_and_save_rank(args, rank):
269
+ mapping = Mapping(world_size=world_size,
270
+ rank=rank,
271
+ tp_size=args.tp_size,
272
+ pp_size=args.pp_size,
273
+ moe_tp_size=args.moe_tp_size,
274
+ moe_ep_size=args.moe_ep_size)
275
+ qwen = QWenForCausalLM.from_hugging_face(model_dir,
276
+ args.dtype,
277
+ mapping=mapping,
278
+ quant_config=quant_config,
279
+ **override_fields)
280
+ qwen.save_checkpoint(args.output_dir, save_config=(rank == 0))
281
+ del qwen
282
+
283
+ execute(args.workers, [convert_and_save_rank] * world_size, args)
284
+ release_gc()
285
+
286
+
287
+ def execute(workers, func, args):
288
+ if workers == 1:
289
+ for rank, f in enumerate(func):
290
+ f(args, rank)
291
+ else:
292
+ with ThreadPoolExecutor(max_workers=workers) as p:
293
+ futures = [p.submit(f, args, rank) for rank, f in enumerate(func)]
294
+ exceptions = []
295
+ for future in as_completed(futures):
296
+ try:
297
+ future.result()
298
+ except Exception as e:
299
+ traceback.print_exc()
300
+ exceptions.append(e)
301
+ assert len(
302
+ exceptions
303
+ ) == 0, "Checkpoint conversion failed, please check error log."
304
+
305
+
306
+ def main():
307
+ print(tensorrt_llm.__version__)
308
+ args = parse_arguments()
309
+
310
+ if (args.moe_tp_size == -1 and args.moe_ep_size == -1):
311
+ # moe default to tp-only
312
+ args.moe_tp_size = args.tp_size
313
+ args.moe_ep_size = 1
314
+ elif (args.moe_tp_size == -1):
315
+ args.moe_tp_size = args.tp_size // args.moe_ep_size
316
+ elif (args.moe_ep_size == -1):
317
+ args.moe_ep_size = args.tp_size // args.moe_tp_size
318
+ assert (args.moe_tp_size * args.moe_ep_size == args.tp_size
319
+ ), "moe_tp_size * moe_ep_size must equal to tp_size"
320
+
321
+ tik = time.time()
322
+
323
+ if not os.path.exists(args.output_dir):
324
+ os.makedirs(args.output_dir)
325
+
326
+ assert args.model_dir is not None
327
+ convert_and_save_hf(args)
328
+
329
+ tok = time.time()
330
+ t = time.strftime('%H:%M:%S', time.gmtime(tok - tik))
331
+ print(f'Total time of converting checkpoints: {t}')
332
+
333
+
334
+ if __name__ == '__main__':
335
+ main()
runtime/triton_trtllm/fill_template.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #! /usr/bin/env python3
2
+ from argparse import ArgumentParser
3
+ from string import Template
4
+
5
+
6
+ def split(string, delimiter):
7
+ """Split a string using delimiter. Supports escaping.
8
+
9
+ Args:
10
+ string (str): The string to split.
11
+ delimiter (str): The delimiter to split the string with.
12
+
13
+ Returns:
14
+ list: A list of strings.
15
+ """
16
+ result = []
17
+ current = ""
18
+ escape = False
19
+ for char in string:
20
+ if escape:
21
+ current += char
22
+ escape = False
23
+ elif char == delimiter:
24
+ result.append(current)
25
+ current = ""
26
+ elif char == "\\":
27
+ escape = True
28
+ else:
29
+ current += char
30
+ result.append(current)
31
+ return result
32
+
33
+
34
+ def main(file_path, substitutions, in_place):
35
+ with open(file_path) as f:
36
+ pbtxt = Template(f.read())
37
+
38
+ sub_dict = {
39
+ "max_queue_size": 0,
40
+ 'max_queue_delay_microseconds': 0,
41
+ }
42
+ for sub in split(substitutions, ","):
43
+ key, value = split(sub, ":")
44
+ sub_dict[key] = value
45
+
46
+ assert key in pbtxt.template, f"key '{key}' does not exist in the file {file_path}."
47
+
48
+ pbtxt = pbtxt.safe_substitute(sub_dict)
49
+
50
+ if in_place:
51
+ with open(file_path, "w") as f:
52
+ f.write(pbtxt)
53
+ else:
54
+ print(pbtxt)
55
+
56
+
57
+ if __name__ == "__main__":
58
+ parser = ArgumentParser()
59
+ parser.add_argument("file_path", help="path of the .pbtxt to modify")
60
+ parser.add_argument(
61
+ "substitutions",
62
+ help=
63
+ "substitutions to perform, in the format variable_name_1:value_1,variable_name_2:value_2..."
64
+ )
65
+ parser.add_argument("--in_place",
66
+ "-i",
67
+ action="store_true",
68
+ help="do the operation in-place")
69
+ args = parser.parse_args()
70
+ main(**vars(args))
runtime/triton_trtllm/model_repo/audio_tokenizer/1/model.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ #
3
+ # Redistribution and use in source and binary forms, with or without
4
+ # modification, are permitted provided that the following conditions
5
+ # are met:
6
+ # * Redistributions of source code must retain the above copyright
7
+ # notice, this list of conditions and the following disclaimer.
8
+ # * Redistributions in binary form must reproduce the above copyright
9
+ # notice, this list of conditions and the following disclaimer in the
10
+ # documentation and/or other materials provided with the distribution.
11
+ # * Neither the name of NVIDIA CORPORATION nor the names of its
12
+ # contributors may be used to endorse or promote products derived
13
+ # from this software without specific prior written permission.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
16
+ # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23
+ # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ import json
27
+ import torch
28
+ from torch import nn
29
+ from torch.nn.utils.rnn import pad_sequence
30
+ import torch.nn.functional as F
31
+ from torch.utils.dlpack import from_dlpack, to_dlpack
32
+
33
+ import triton_python_backend_utils as pb_utils
34
+
35
+ import math
36
+ import os
37
+ from functools import wraps
38
+ import numpy as np
39
+
40
+
41
+ from sparktts.models.audio_tokenizer import BiCodecTokenizer
42
+
43
+ class TritonPythonModel:
44
+ def initialize(self, args):
45
+ parameters = json.loads(args['model_config'])['parameters']
46
+ for key, value in parameters.items():
47
+ parameters[key] = value["string_value"]
48
+ model_dir = parameters["model_dir"]
49
+ self.device = torch.device("cuda")
50
+ self.audio_tokenizer = BiCodecTokenizer(model_dir, device=self.device)
51
+
52
+ def get_ref_clip(self, wav: np.ndarray) -> np.ndarray:
53
+ """Get reference audio clip for speaker embedding."""
54
+ sample_rate = 16000
55
+ ref_segment_duration = 6
56
+ latent_hop_length = 320
57
+
58
+ ref_segment_length = (
59
+ int(sample_rate * ref_segment_duration)
60
+ // latent_hop_length
61
+ * latent_hop_length
62
+ )
63
+ wav_length = len(wav)
64
+
65
+ if ref_segment_length > wav_length:
66
+ # Repeat and truncate to handle insufficient length
67
+ wav = np.tile(wav, ref_segment_length // wav_length + 1)
68
+
69
+ return wav[:ref_segment_length]
70
+
71
+ def execute(self, requests):
72
+ reference_wav_list, reference_wav_ref_clip_list = [], []
73
+
74
+ for request in requests:
75
+ wav_array = pb_utils.get_input_tensor_by_name(request, "reference_wav").as_numpy()
76
+ wav_len = pb_utils.get_input_tensor_by_name(
77
+ request, "reference_wav_len").as_numpy().item()
78
+ # check shape
79
+ print(wav_array.shape, wav_len, 233333333333)
80
+ # squeeze the first dimension, for the numpy array
81
+ wav = wav_array[:, :wav_len].squeeze(0)
82
+ reference_wav_list.append(wav)
83
+
84
+ wav_ref_clip = self.get_ref_clip(wav)
85
+ print(wav_ref_clip.shape, 2333333333455)
86
+ reference_wav_ref_clip_list.append(torch.from_numpy(wav_ref_clip))
87
+ # (len,) -> B,len
88
+ ref_wav_clip_tensor = torch.stack(reference_wav_ref_clip_list, dim=0)
89
+ wav2vec2_features = self.audio_tokenizer.extract_wav2vec2_features(reference_wav_list)
90
+ audio_tokenizer_input_dict = {
91
+ "ref_wav": ref_wav_clip_tensor.to(self.device), # no padding, spaker encoder
92
+ "feat": wav2vec2_features.to(self.device),
93
+ }
94
+ semantic_tokens, global_tokens = self.audio_tokenizer.model.tokenize(audio_tokenizer_input_dict)
95
+
96
+
97
+ responses = []
98
+ for i in range(len(requests)):
99
+ global_tokens_tensor = pb_utils.Tensor.from_dlpack("global_tokens", to_dlpack(global_tokens[i]))
100
+ semantic_tokens_tensor = pb_utils.Tensor.from_dlpack("semantic_tokens", to_dlpack(semantic_tokens[i]))
101
+ inference_response = pb_utils.InferenceResponse(output_tensors=[global_tokens_tensor, semantic_tokens_tensor])
102
+ responses.append(inference_response)
103
+
104
+ return responses
runtime/triton_trtllm/model_repo/audio_tokenizer/config.pbtxt ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ name: "audio_tokenizer"
16
+ backend: "python"
17
+ max_batch_size: 16
18
+ dynamic_batching {
19
+ max_queue_delay_microseconds: 1
20
+ }
21
+ parameters [
22
+ {
23
+ key: "model_dir",
24
+ value: {string_value:"/workspace_yuekai/spark-tts/Spark-TTS/pretrained_models/Spark-TTS-0.5B"}
25
+ }
26
+ ]
27
+
28
+ input [
29
+ {
30
+ name: "reference_wav"
31
+ data_type: TYPE_FP32
32
+ dims: [-1]
33
+ },
34
+ {
35
+ name: "reference_wav_len"
36
+ data_type: TYPE_INT32
37
+ dims: [1]
38
+ }
39
+ ]
40
+ output [
41
+ {
42
+ name: "global_tokens"
43
+ data_type: TYPE_INT32
44
+ dims: [-1]
45
+ },
46
+ {
47
+ name: "semantic_tokens"
48
+ data_type: TYPE_INT32
49
+ dims: [-1]
50
+ }
51
+ ]
52
+
53
+ instance_group [
54
+ {
55
+ count: 1
56
+ kind: KIND_CPU
57
+ }
58
+ ]
runtime/triton_trtllm/model_repo/spark_tts/1/model.py ADDED
@@ -0,0 +1,287 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ #
3
+ # Redistribution and use in source and binary forms, with or without
4
+ # modification, are permitted provided that the following conditions
5
+ # are met:
6
+ # * Redistributions of source code must retain the above copyright
7
+ # notice, this list of conditions and the following disclaimer.
8
+ # * Redistributions in binary form must reproduce the above copyright
9
+ # notice, this list of conditions and the following disclaimer in the
10
+ # documentation and/or other materials provided with the distribution.
11
+ # * Neither the name of NVIDIA CORPORATION nor the names of its
12
+ # contributors may be used to endorse or promote products derived
13
+ # from this software without specific prior written permission.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
16
+ # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23
+ # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ import json
27
+ import torch
28
+ from torch import nn
29
+ from torch.nn.utils.rnn import pad_sequence
30
+ import torch.nn.functional as F
31
+ from torch.utils.dlpack import from_dlpack, to_dlpack
32
+
33
+ import triton_python_backend_utils as pb_utils
34
+
35
+ import math
36
+ import os
37
+ from functools import wraps
38
+
39
+ from transformers import AutoTokenizer
40
+
41
+ import numpy as np
42
+ import re
43
+ from typing import Tuple
44
+
45
+ from sparktts.utils.token_parser import LEVELS_MAP, GENDER_MAP, TASK_TOKEN_MAP
46
+
47
+
48
+ def process_prompt(
49
+ text: str,
50
+ prompt_text: str = None,
51
+ global_token_ids: torch.Tensor = None,
52
+ semantic_token_ids: torch.Tensor = None,
53
+ ) -> Tuple[str, torch.Tensor]:
54
+ """
55
+ Process input for voice cloning.
56
+
57
+ Args:
58
+ text (str): The text input to be converted to speech.
59
+ prompt_speech_path (Path): Path to the audio file used as a prompt.
60
+ prompt_text (str, optional): Transcript of the prompt audio.
61
+
62
+ Return:
63
+ Tuple[str, torch.Tensor]: Input prompt; global tokens
64
+ """
65
+
66
+ # global_token_ids, semantic_token_ids = self.audio_tokenizer.tokenize(
67
+ # prompt_speech_path
68
+ # )
69
+ global_tokens = "".join(
70
+ [f"<|bicodec_global_{i}|>" for i in global_token_ids.squeeze()]
71
+ )
72
+ print(global_tokens, 233333333333, len(global_tokens), "global_tokens")
73
+ # Prepare the input tokens for the model
74
+ if prompt_text is not None:
75
+ semantic_tokens = "".join(
76
+ [f"<|bicodec_semantic_{i}|>" for i in semantic_token_ids.squeeze()]
77
+ )
78
+ print(semantic_tokens, 233333333333, len(semantic_tokens), "semantic_tokens")
79
+ inputs = [
80
+ TASK_TOKEN_MAP["tts"],
81
+ "<|start_content|>",
82
+ prompt_text,
83
+ text,
84
+ "<|end_content|>",
85
+ "<|start_global_token|>",
86
+ global_tokens,
87
+ "<|end_global_token|>",
88
+ "<|start_semantic_token|>",
89
+ semantic_tokens,
90
+ ]
91
+ else:
92
+ inputs = [
93
+ TASK_TOKEN_MAP["tts"],
94
+ "<|start_content|>",
95
+ text,
96
+ "<|end_content|>",
97
+ "<|start_global_token|>",
98
+ global_tokens,
99
+ "<|end_global_token|>",
100
+ ]
101
+
102
+ inputs = "".join(inputs)
103
+
104
+ return inputs, global_token_ids
105
+
106
+ class TritonPythonModel:
107
+ def initialize(self, args):
108
+ parameters = json.loads(args['model_config'])['parameters']
109
+ for key, value in parameters.items():
110
+ parameters[key] = value["string_value"]
111
+ model_dir = parameters["model_dir"]
112
+ self.tokenizer = AutoTokenizer.from_pretrained(f"{model_dir}/LLM")
113
+ self.device = torch.device("cuda")
114
+ self.decoupled = False
115
+
116
+ def forward_llm(self, input_ids):
117
+ """
118
+ Prepares the response from the language model based on the provided
119
+ inputs. Creates a `pb_utils.InferenceRequest` object with passed
120
+ `llm_request_inputs` to send to a decoupled TensorRTLLM model.
121
+ For each response from the language model:
122
+ - Checks for errors and raise an exception if any are found.
123
+ - Extracts the "output_ids" tensor from the response.
124
+ - Determines the finish reason based on the presence of the
125
+ end-of-sequence token or reaching the maximum length.
126
+ - Appends the generated token IDs to `output_ids`.
127
+ - If the finish reason is determined, decodes the output IDs to text
128
+ and prepares the final response.
129
+
130
+ The final response includes the generated text, finish reason,
131
+ completion tokens, prompt tokens, and total tokens.
132
+
133
+ Parameters
134
+ ----------
135
+ - llm_request_inputs (dict): A dictionary containing the inputs for the language model.
136
+
137
+ Returns
138
+ -------
139
+ - pb_utils.InferenceResponse: The response object containing the generated text and additional metadata.
140
+ """
141
+ # convert input_ids to numpy, with shape [1, sequence_length]
142
+ input_ids = input_ids.cpu().numpy()
143
+ print(input_ids.shape, 233333333333, "input_ids")
144
+ max_tokens = 512
145
+ input_dict = {
146
+ "request_output_len": np.array([[max_tokens]], dtype=np.int32),
147
+ "end_id": np.array([[self.tokenizer.eos_token_id]], dtype=np.int32),
148
+ "pad_id": np.array([[self.tokenizer.pad_token_id]], dtype=np.int32),
149
+ "streaming": np.array([[self.decoupled]], dtype=np.bool_),
150
+ "runtime_top_p": np.array([[0.95]], dtype=np.float32),
151
+ "runtime_top_k": np.array([[50]], dtype=np.int32),
152
+ "temperature": np.array([[0.8]], dtype=np.float32),
153
+ "input_ids": input_ids,
154
+ "input_lengths": np.array([[input_ids.shape[1]]], dtype=np.int32),
155
+ }
156
+ for k, v in input_dict.items():
157
+ print(k, v.shape, 233333333333, v.dtype)
158
+ # exit()
159
+ input_tensor_list = [
160
+ pb_utils.Tensor(k, v) for k, v in input_dict.items()
161
+ ]
162
+ # input_tensor_list.append(pb_utils.Tensor.from_dlpack(
163
+ # "input_ids", to_dlpack(input_ids)
164
+ # ))
165
+ llm_request = pb_utils.InferenceRequest(
166
+ model_name="tensorrt_llm",
167
+ requested_output_names=["output_ids", "sequence_length"],
168
+ inputs=input_tensor_list,
169
+ )
170
+ print("=======================================")
171
+ llm_response = llm_request.exec(decoupled=self.decoupled)
172
+ if llm_response.has_error():
173
+ raise pb_utils.TritonModelException(
174
+ llm_response.error().message())
175
+ output_ids = pb_utils.get_output_tensor_by_name(
176
+ llm_response, "output_ids").as_numpy()
177
+ seq_lens = pb_utils.get_output_tensor_by_name(
178
+ llm_response, "sequence_length").as_numpy()
179
+ print(seq_lens, 233333333333, "seq_lens")
180
+ actual_output_ids = output_ids[0][0]
181
+ actual_output_ids = actual_output_ids[:seq_lens[0][0]]
182
+ print(actual_output_ids, 233333333333, "actual_output_ids")
183
+ return actual_output_ids
184
+
185
+ def forward_audio_tokenizer(self, wav, wav_len):
186
+ # input_tensor_0 = pb_utils.Tensor.
187
+ # input_tensor_1 = pb_utils.Tensor.from_dlpack("wav_len", to_dlpack(wav_len))
188
+
189
+ inference_request = pb_utils.InferenceRequest(
190
+ model_name='audio_tokenizer',
191
+ requested_output_names=['global_tokens', 'semantic_tokens'],
192
+ inputs=[wav, wav_len]
193
+ )
194
+ inference_response = inference_request.exec()
195
+ if inference_response.has_error():
196
+ raise pb_utils.TritonModelException(inference_response.error().message())
197
+ else:
198
+ global_tokens = pb_utils.get_output_tensor_by_name(inference_response,
199
+ 'global_tokens')
200
+ global_tokens = torch.utils.dlpack.from_dlpack(global_tokens.to_dlpack()).cpu()
201
+ semantic_tokens = pb_utils.get_output_tensor_by_name(inference_response,
202
+ 'semantic_tokens')
203
+ semantic_tokens = torch.utils.dlpack.from_dlpack(semantic_tokens.to_dlpack()).cpu()
204
+ return global_tokens, semantic_tokens
205
+
206
+ def forward_vocoder(self, global_token_ids, pred_semantic_ids):
207
+ global_token_ids = pb_utils.Tensor.from_dlpack("global_tokens", to_dlpack(global_token_ids))
208
+ pred_semantic_ids = pb_utils.Tensor.from_dlpack("semantic_tokens", to_dlpack(pred_semantic_ids))
209
+ inference_request = pb_utils.InferenceRequest(
210
+ model_name='vocoder',
211
+ requested_output_names=['waveform'],
212
+ inputs=[global_token_ids, pred_semantic_ids]
213
+ )
214
+ inference_response = inference_request.exec()
215
+ if inference_response.has_error():
216
+ raise pb_utils.TritonModelException(inference_response.error().message())
217
+ else:
218
+ waveform = pb_utils.get_output_tensor_by_name(inference_response,
219
+ 'waveform')
220
+ waveform = torch.utils.dlpack.from_dlpack(waveform.to_dlpack()).cpu()
221
+ return waveform
222
+
223
+ def execute(self, requests):
224
+ # reference_text_list, target_text_list, reference_wav_list, reference_wav_ref_clip_list = [], [], [], []
225
+ responses = []
226
+ for request in requests:
227
+ wav = pb_utils.get_input_tensor_by_name(request, "reference_wav")
228
+ wav_len = pb_utils.get_input_tensor_by_name(
229
+ request, "reference_wav_len")
230
+ global_tokens, semantic_tokens = self.forward_audio_tokenizer(wav, wav_len)
231
+ # print(wav_tensor.shape, wav_len.shape, 233333333333)
232
+ # reference_wav_list.append(wav)
233
+ # wav_ref_clip = self.get_ref_clip(wav[:, :wav_len])
234
+ # reference_wav_ref_clip_list.append(wav_ref_clip)
235
+
236
+
237
+ reference_text = pb_utils.get_input_tensor_by_name(
238
+ request, "reference_text").as_numpy()
239
+ reference_text = reference_text[0][0].decode('utf-8')
240
+ # reference_text_list.append(reference_text)
241
+
242
+ target_text = pb_utils.get_input_tensor_by_name(
243
+ request, "target_text").as_numpy()
244
+ target_text = target_text[0][0].decode('utf-8')
245
+ # target_text_list.append(target_text)
246
+
247
+ # ref_wav_clip_tensor = torch.cat(reference_wav_ref_clip_list, dim=0)
248
+ # wav2vec2_features = self.model.audio_tokenizer.extract_wav2vec2_features(reference_wav_list)
249
+ # audio_tokenizer_input_dict = {
250
+ # "ref_wav": ref_wav_clip_tensor, # no padding, spaker encoder
251
+ # "feat": wav2vec2_features,
252
+ # }
253
+
254
+ prompt, global_token_ids = process_prompt(
255
+ text=target_text,
256
+ prompt_text=reference_text,
257
+ global_token_ids=global_tokens,
258
+ semantic_token_ids=semantic_tokens,
259
+ )
260
+ print(semantic_tokens.shape, "semantic_tokens")
261
+ print(global_tokens.shape, "global_tokens")
262
+ print(prompt, "prompt", len(prompt))
263
+ model_inputs = self.tokenizer([prompt], return_tensors="pt").to(self.device)
264
+ print(model_inputs, "model_inputs")
265
+ input_ids = model_inputs.input_ids.to(torch.int32)
266
+ print(input_ids.shape, 233333333333, 455555555)
267
+
268
+ generated_ids = self.forward_llm(input_ids)
269
+ print(generated_ids, "generated_ids", len(generated_ids))
270
+ predicts = self.tokenizer.batch_decode([generated_ids], skip_special_tokens=True)[0]
271
+ print(predicts, "predicts", len(predicts))
272
+ pred_semantic_ids = (
273
+ torch.tensor([int(token) for token in re.findall(r"bicodec_semantic_(\d+)", predicts)])
274
+ .unsqueeze(0).to(torch.int32)
275
+ )
276
+ print(global_token_ids.shape, "global_token_ids")
277
+ print(pred_semantic_ids.shape, "pred_semantic_ids")
278
+ audio = self.forward_vocoder(
279
+ global_token_ids.to(self.device),
280
+ pred_semantic_ids.to(self.device),
281
+ )
282
+
283
+ audio = pb_utils.Tensor.from_dlpack("waveform", to_dlpack(audio))
284
+ inference_response = pb_utils.InferenceResponse(output_tensors=[audio])
285
+ responses.append(inference_response)
286
+
287
+ return responses
runtime/triton_trtllm/model_repo/spark_tts/config.pbtxt ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ name: "spark_tts"
16
+ backend: "python"
17
+ max_batch_size: 16
18
+ dynamic_batching {
19
+ max_queue_delay_microseconds: 1
20
+ }
21
+ parameters [
22
+ {
23
+ key: "model_dir",
24
+ value: {string_value:"/workspace_yuekai/spark-tts/Spark-TTS/pretrained_models/Spark-TTS-0.5B"}
25
+ }
26
+ ]
27
+
28
+ input [
29
+ {
30
+ name: "reference_wav"
31
+ data_type: TYPE_FP32
32
+ dims: [-1]
33
+ optional: True
34
+ },
35
+ {
36
+ name: "reference_wav_len"
37
+ data_type: TYPE_INT32
38
+ dims: [1]
39
+ optional: True
40
+ },
41
+ {
42
+ name: "reference_text"
43
+ data_type: TYPE_STRING
44
+ dims: [1]
45
+ },
46
+ {
47
+ name: "target_text"
48
+ data_type: TYPE_STRING
49
+ dims: [1]
50
+ }
51
+ ]
52
+ output [
53
+ {
54
+ name: "waveform"
55
+ data_type: TYPE_FP32
56
+ dims: [ -1 ]
57
+ }
58
+ ]
59
+
60
+ instance_group [
61
+ {
62
+ count: 4
63
+ kind: KIND_CPU
64
+ }
65
+ ]
runtime/triton_trtllm/model_repo/tensorrt_llm/1/.gitkeep ADDED
File without changes
runtime/triton_trtllm/model_repo/tensorrt_llm/config.pbtxt ADDED
@@ -0,0 +1,857 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ #
3
+ # Redistribution and use in source and binary forms, with or without
4
+ # modification, are permitted provided that the following conditions
5
+ # are met:
6
+ # * Redistributions of source code must retain the above copyright
7
+ # notice, this list of conditions and the following disclaimer.
8
+ # * Redistributions in binary form must reproduce the above copyright
9
+ # notice, this list of conditions and the following disclaimer in the
10
+ # documentation and/or other materials provided with the distribution.
11
+ # * Neither the name of NVIDIA CORPORATION nor the names of its
12
+ # contributors may be used to endorse or promote products derived
13
+ # from this software without specific prior written permission.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
16
+ # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23
+ # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+
27
+ name: "tensorrt_llm"
28
+ backend: "${triton_backend}"
29
+ max_batch_size: ${triton_max_batch_size}
30
+
31
+ model_transaction_policy {
32
+ decoupled: ${decoupled_mode}
33
+ }
34
+
35
+ dynamic_batching {
36
+ preferred_batch_size: [ ${triton_max_batch_size} ]
37
+ max_queue_delay_microseconds: ${max_queue_delay_microseconds}
38
+ default_queue_policy: { max_queue_size: ${max_queue_size} }
39
+ }
40
+
41
+ input [
42
+ {
43
+ name: "input_ids"
44
+ data_type: TYPE_INT32
45
+ dims: [ -1 ]
46
+ allow_ragged_batch: true
47
+ optional: true
48
+ },
49
+ {
50
+ name: "encoder_input_features"
51
+ data_type: ${encoder_input_features_data_type}
52
+ dims: [ -1, -1 ]
53
+ allow_ragged_batch: true
54
+ optional: true
55
+ },
56
+ {
57
+ name: "encoder_output_lengths"
58
+ data_type: TYPE_INT32
59
+ dims: [ 1 ]
60
+ reshape: { shape: [ ] }
61
+ optional: true
62
+ },
63
+ {
64
+ name: "input_lengths"
65
+ data_type: TYPE_INT32
66
+ dims: [ 1 ]
67
+ reshape: { shape: [ ] }
68
+ },
69
+ {
70
+ name: "request_output_len"
71
+ data_type: TYPE_INT32
72
+ dims: [ 1 ]
73
+ reshape: { shape: [ ] }
74
+ },
75
+ {
76
+ name: "num_return_sequences"
77
+ data_type: TYPE_INT32
78
+ dims: [ 1 ]
79
+ reshape: { shape: [ ] }
80
+ optional: true
81
+ },
82
+ {
83
+ name: "draft_input_ids"
84
+ data_type: TYPE_INT32
85
+ dims: [ -1 ]
86
+ optional: true
87
+ allow_ragged_batch: true
88
+ },
89
+ {
90
+ name: "decoder_input_ids"
91
+ data_type: TYPE_INT32
92
+ dims: [ -1 ]
93
+ optional: true
94
+ allow_ragged_batch: true
95
+ },
96
+ {
97
+ name: "decoder_input_lengths"
98
+ data_type: TYPE_INT32
99
+ dims: [ 1 ]
100
+ optional: true
101
+ reshape: { shape: [ ] }
102
+ },
103
+ {
104
+ name: "draft_logits"
105
+ data_type: ${logits_datatype}
106
+ dims: [ -1, -1 ]
107
+ optional: true
108
+ allow_ragged_batch: true
109
+ },
110
+ {
111
+ name: "draft_acceptance_threshold"
112
+ data_type: TYPE_FP32
113
+ dims: [ 1 ]
114
+ reshape: { shape: [ ] }
115
+ optional: true
116
+ },
117
+ {
118
+ name: "end_id"
119
+ data_type: TYPE_INT32
120
+ dims: [ 1 ]
121
+ reshape: { shape: [ ] }
122
+ optional: true
123
+ },
124
+ {
125
+ name: "pad_id"
126
+ data_type: TYPE_INT32
127
+ dims: [ 1 ]
128
+ reshape: { shape: [ ] }
129
+ optional: true
130
+ },
131
+ {
132
+ name: "stop_words_list"
133
+ data_type: TYPE_INT32
134
+ dims: [ 2, -1 ]
135
+ optional: true
136
+ allow_ragged_batch: true
137
+ },
138
+ {
139
+ name: "bad_words_list"
140
+ data_type: TYPE_INT32
141
+ dims: [ 2, -1 ]
142
+ optional: true
143
+ allow_ragged_batch: true
144
+ },
145
+ {
146
+ name: "embedding_bias"
147
+ data_type: TYPE_FP32
148
+ dims: [ -1 ]
149
+ optional: true
150
+ allow_ragged_batch: true
151
+ },
152
+ {
153
+ name: "beam_width"
154
+ data_type: TYPE_INT32
155
+ dims: [ 1 ]
156
+ reshape: { shape: [ ] }
157
+ optional: true
158
+ },
159
+ {
160
+ name: "temperature"
161
+ data_type: TYPE_FP32
162
+ dims: [ 1 ]
163
+ reshape: { shape: [ ] }
164
+ optional: true
165
+ },
166
+ {
167
+ name: "runtime_top_k"
168
+ data_type: TYPE_INT32
169
+ dims: [ 1 ]
170
+ reshape: { shape: [ ] }
171
+ optional: true
172
+ },
173
+ {
174
+ name: "runtime_top_p"
175
+ data_type: TYPE_FP32
176
+ dims: [ 1 ]
177
+ reshape: { shape: [ ] }
178
+ optional: true
179
+ },
180
+ {
181
+ name: "runtime_top_p_min"
182
+ data_type: TYPE_FP32
183
+ dims: [ 1 ]
184
+ reshape: { shape: [ ] }
185
+ optional: true
186
+ },
187
+ {
188
+ name: "runtime_top_p_decay"
189
+ data_type: TYPE_FP32
190
+ dims: [ 1 ]
191
+ reshape: { shape: [ ] }
192
+ optional: true
193
+ },
194
+ {
195
+ name: "runtime_top_p_reset_ids"
196
+ data_type: TYPE_INT32
197
+ dims: [ 1 ]
198
+ reshape: { shape: [ ] }
199
+ optional: true
200
+ },
201
+ {
202
+ name: "len_penalty"
203
+ data_type: TYPE_FP32
204
+ dims: [ 1 ]
205
+ reshape: { shape: [ ] }
206
+ optional: true
207
+ },
208
+ {
209
+ name: "early_stopping"
210
+ data_type: TYPE_BOOL
211
+ dims: [ 1 ]
212
+ reshape: { shape: [ ] }
213
+ optional: true
214
+ },
215
+ {
216
+ name: "repetition_penalty"
217
+ data_type: TYPE_FP32
218
+ dims: [ 1 ]
219
+ reshape: { shape: [ ] }
220
+ optional: true
221
+ },
222
+ {
223
+ name: "min_length"
224
+ data_type: TYPE_INT32
225
+ dims: [ 1 ]
226
+ reshape: { shape: [ ] }
227
+ optional: true
228
+ },
229
+ {
230
+ name: "beam_search_diversity_rate"
231
+ data_type: TYPE_FP32
232
+ dims: [ 1 ]
233
+ reshape: { shape: [ ] }
234
+ optional: true
235
+ },
236
+ {
237
+ name: "presence_penalty"
238
+ data_type: TYPE_FP32
239
+ dims: [ 1 ]
240
+ reshape: { shape: [ ] }
241
+ optional: true
242
+ },
243
+ {
244
+ name: "frequency_penalty"
245
+ data_type: TYPE_FP32
246
+ dims: [ 1 ]
247
+ reshape: { shape: [ ] }
248
+ optional: true
249
+ },
250
+ {
251
+ name: "random_seed"
252
+ data_type: TYPE_UINT64
253
+ dims: [ 1 ]
254
+ reshape: { shape: [ ] }
255
+ optional: true
256
+ },
257
+ {
258
+ name: "return_log_probs"
259
+ data_type: TYPE_BOOL
260
+ dims: [ 1 ]
261
+ reshape: { shape: [ ] }
262
+ optional: true
263
+ },
264
+ {
265
+ name: "return_context_logits"
266
+ data_type: TYPE_BOOL
267
+ dims: [ 1 ]
268
+ reshape: { shape: [ ] }
269
+ optional: true
270
+ },
271
+ {
272
+ name: "return_generation_logits"
273
+ data_type: TYPE_BOOL
274
+ dims: [ 1 ]
275
+ reshape: { shape: [ ] }
276
+ optional: true
277
+ },
278
+ {
279
+ name: "return_perf_metrics"
280
+ data_type: TYPE_BOOL
281
+ dims: [ 1 ]
282
+ reshape: { shape: [ ] }
283
+ optional: true
284
+ },
285
+ {
286
+ name: "exclude_input_in_output"
287
+ data_type: TYPE_BOOL
288
+ dims: [ 1 ]
289
+ reshape: { shape: [ ] }
290
+ optional: true
291
+ },
292
+ {
293
+ name: "stop"
294
+ data_type: TYPE_BOOL
295
+ dims: [ 1 ]
296
+ reshape: { shape: [ ] }
297
+ optional: true
298
+ },
299
+ {
300
+ name: "streaming"
301
+ data_type: TYPE_BOOL
302
+ dims: [ 1 ]
303
+ reshape: { shape: [ ] }
304
+ optional: true
305
+ },
306
+ {
307
+ name: "prompt_embedding_table"
308
+ data_type: TYPE_FP16
309
+ dims: [ -1, -1 ]
310
+ optional: true
311
+ allow_ragged_batch: true
312
+ },
313
+ {
314
+ name: "prompt_table_extra_ids"
315
+ data_type: TYPE_UINT64
316
+ dims: [ -1 ]
317
+ optional: true
318
+ allow_ragged_batch: true
319
+ },
320
+ {
321
+ name: "prompt_vocab_size"
322
+ data_type: TYPE_INT32
323
+ dims: [ 1 ]
324
+ reshape: { shape: [ ] }
325
+ optional: true
326
+ },
327
+ # cross_attention_mask shape `[bs, seq_len, num_images*num_tiles]`
328
+ {
329
+ name: "cross_attention_mask"
330
+ data_type: TYPE_BOOL
331
+ dims: [ -1, -1 ]
332
+ optional: true
333
+ allow_ragged_batch: true
334
+ },
335
+ # Mrope param when mrope is used
336
+ {
337
+ name: "mrope_rotary_cos_sin"
338
+ data_type: TYPE_FP32
339
+ dims: [ -1 ]
340
+ optional: true
341
+ },
342
+ {
343
+ name: "mrope_position_deltas"
344
+ data_type: TYPE_INT64
345
+ dims: [ 1 ]
346
+ optional: true
347
+ },
348
+ # the unique task ID for the given LoRA.
349
+ # To perform inference with a specific LoRA for the first time `lora_task_id` `lora_weights` and `lora_config` must all be given.
350
+ # The LoRA will be cached, so that subsequent requests for the same task only require `lora_task_id`.
351
+ # If the cache is full the oldest LoRA will be evicted to make space for new ones. An error is returned if `lora_task_id` is not cached.
352
+ {
353
+ name: "lora_task_id"
354
+ data_type: TYPE_UINT64
355
+ dims: [ 1 ]
356
+ reshape: { shape: [ ] }
357
+ optional: true
358
+ },
359
+ # weights for a lora adapter shape [ num_lora_modules_layers, D x Hi + Ho x D ]
360
+ # where the last dimension holds the in / out adapter weights for the associated module (e.g. attn_qkv) and model layer
361
+ # each of the in / out tensors are first flattened and then concatenated together in the format above.
362
+ # D=adapter_size (R value), Hi=hidden_size_in, Ho=hidden_size_out.
363
+ {
364
+ name: "lora_weights"
365
+ data_type: TYPE_FP16
366
+ dims: [ -1, -1 ]
367
+ optional: true
368
+ allow_ragged_batch: true
369
+ },
370
+ # module identifier (same size a first dimension of lora_weights)
371
+ # See LoraModule::ModuleType for model id mapping
372
+ #
373
+ # "attn_qkv": 0 # compbined qkv adapter
374
+ # "attn_q": 1 # q adapter
375
+ # "attn_k": 2 # k adapter
376
+ # "attn_v": 3 # v adapter
377
+ # "attn_dense": 4 # adapter for the dense layer in attention
378
+ # "mlp_h_to_4h": 5 # for llama2 adapter for gated mlp layer after attention / RMSNorm: up projection
379
+ # "mlp_4h_to_h": 6 # for llama2 adapter for gated mlp layer after attention / RMSNorm: down projection
380
+ # "mlp_gate": 7 # for llama2 adapter for gated mlp later after attention / RMSNorm: gate
381
+ #
382
+ # last dim holds [ module_id, layer_idx, adapter_size (D aka R value) ]
383
+ {
384
+ name: "lora_config"
385
+ data_type: TYPE_INT32
386
+ dims: [ -1, 3 ]
387
+ optional: true
388
+ allow_ragged_batch: true
389
+ },
390
+ {
391
+ name: "context_phase_params"
392
+ data_type: TYPE_UINT8
393
+ dims: [ -1 ]
394
+ optional: true
395
+ allow_ragged_batch: true
396
+ },
397
+ # skip_cross_attn_blocks shape `[bs, 1]`, only used in mllama
398
+ {
399
+ name: "skip_cross_attn_blocks"
400
+ data_type: TYPE_BOOL
401
+ dims: [ 1 ]
402
+ optional: true
403
+ allow_ragged_batch: true
404
+ },
405
+ {
406
+ name: "retention_token_range_starts"
407
+ data_type: TYPE_INT32
408
+ dims: [ -1 ]
409
+ optional: true
410
+ allow_ragged_batch: true
411
+ },
412
+ {
413
+ name: "retention_token_range_ends"
414
+ data_type: TYPE_INT32
415
+ dims: [ -1 ]
416
+ optional: true
417
+ allow_ragged_batch: true
418
+ },
419
+ {
420
+ name: "retention_token_range_priorities"
421
+ data_type: TYPE_INT32
422
+ dims: [ -1 ]
423
+ optional: true
424
+ allow_ragged_batch: true
425
+ },
426
+ {
427
+ name: "retention_token_range_durations_ms"
428
+ data_type: TYPE_INT32
429
+ dims: [ -1 ]
430
+ optional: true
431
+ allow_ragged_batch: true
432
+ },
433
+ {
434
+ name: "retention_decode_priority"
435
+ data_type: TYPE_INT32
436
+ dims: [ 1 ]
437
+ optional: true
438
+ allow_ragged_batch: true
439
+ },
440
+ {
441
+ name: "retention_decode_duration_ms"
442
+ data_type: TYPE_INT32
443
+ dims: [ 1 ]
444
+ optional: true
445
+ allow_ragged_batch: true
446
+ },
447
+ {
448
+ name: "guided_decoding_guide_type"
449
+ data_type: TYPE_STRING
450
+ dims: [ 1 ]
451
+ optional: true
452
+ allow_ragged_batch: true
453
+ },
454
+ {
455
+ name: "guided_decoding_guide"
456
+ data_type: TYPE_STRING
457
+ dims: [ 1 ]
458
+ optional: true
459
+ allow_ragged_batch: true
460
+ },
461
+ {
462
+ name: "lookahead_window_size"
463
+ data_type: TYPE_INT32
464
+ dims: [ 1 ]
465
+ optional: true
466
+ allow_ragged_batch: true
467
+ },
468
+ {
469
+ name: "lookahead_ngram_size"
470
+ data_type: TYPE_INT32
471
+ dims: [ 1 ]
472
+ optional: true
473
+ allow_ragged_batch: true
474
+ },
475
+ {
476
+ name: "lookahead_verification_set_size"
477
+ data_type: TYPE_INT32
478
+ dims: [ 1 ]
479
+ optional: true
480
+ allow_ragged_batch: true
481
+ }
482
+ ]
483
+ output [
484
+ {
485
+ name: "output_ids"
486
+ data_type: TYPE_INT32
487
+ dims: [ -1, -1 ]
488
+ },
489
+ {
490
+ name: "sequence_length"
491
+ data_type: TYPE_INT32
492
+ dims: [ -1 ]
493
+ },
494
+ {
495
+ name: "cum_log_probs"
496
+ data_type: TYPE_FP32
497
+ dims: [ -1 ]
498
+ },
499
+ {
500
+ name: "output_log_probs"
501
+ data_type: TYPE_FP32
502
+ dims: [ -1, -1 ]
503
+ },
504
+ {
505
+ name: "context_logits"
506
+ data_type: ${logits_datatype}
507
+ dims: [ -1, -1 ]
508
+ },
509
+ {
510
+ name: "generation_logits"
511
+ data_type: ${logits_datatype}
512
+ dims: [ -1, -1, -1 ]
513
+ },
514
+ {
515
+ name: "batch_index"
516
+ data_type: TYPE_INT32
517
+ dims: [ 1 ]
518
+ },
519
+ {
520
+ name: "sequence_index"
521
+ data_type: TYPE_INT32
522
+ dims: [ 1 ]
523
+ },
524
+ {
525
+ name: "context_phase_params"
526
+ data_type: TYPE_UINT8
527
+ dims: [ -1 ]
528
+ },
529
+ {
530
+ name: "kv_cache_alloc_new_blocks"
531
+ data_type: TYPE_INT32
532
+ dims: [ 1 ]
533
+ },
534
+ {
535
+ name: "kv_cache_reused_blocks"
536
+ data_type: TYPE_INT32
537
+ dims: [ 1 ]
538
+ },
539
+ {
540
+ name: "kv_cache_alloc_total_blocks"
541
+ data_type: TYPE_INT32
542
+ dims: [ 1 ]
543
+ },
544
+ {
545
+ name: "arrival_time_ns"
546
+ data_type: TYPE_INT64
547
+ dims: [ 1 ]
548
+ },
549
+ {
550
+ name: "first_scheduled_time_ns"
551
+ data_type: TYPE_INT64
552
+ dims: [ 1 ]
553
+ },
554
+ {
555
+ name: "first_token_time_ns"
556
+ data_type: TYPE_INT64
557
+ dims: [ 1 ]
558
+ },
559
+ {
560
+ name: "last_token_time_ns"
561
+ data_type: TYPE_INT64
562
+ dims: [ 1 ]
563
+ },
564
+ {
565
+ name: "acceptance_rate"
566
+ data_type: TYPE_FP32
567
+ dims: [ 1 ]
568
+ },
569
+ {
570
+ name: "total_accepted_draft_tokens"
571
+ data_type: TYPE_INT32
572
+ dims: [ 1 ]
573
+ },
574
+ {
575
+ name: "total_draft_tokens"
576
+ data_type: TYPE_INT32
577
+ dims: [ 1 ]
578
+ }
579
+ ]
580
+ instance_group [
581
+ {
582
+ count: 1
583
+ kind : KIND_CPU
584
+ }
585
+ ]
586
+ parameters: {
587
+ key: "max_beam_width"
588
+ value: {
589
+ string_value: "${max_beam_width}"
590
+ }
591
+ }
592
+ parameters: {
593
+ key: "FORCE_CPU_ONLY_INPUT_TENSORS"
594
+ value: {
595
+ string_value: "no"
596
+ }
597
+ }
598
+ parameters: {
599
+ key: "gpt_model_type"
600
+ value: {
601
+ string_value: "${batching_strategy}"
602
+ }
603
+ }
604
+ parameters: {
605
+ key: "gpt_model_path"
606
+ value: {
607
+ string_value: "${engine_dir}"
608
+ }
609
+ }
610
+ parameters: {
611
+ key: "encoder_model_path"
612
+ value: {
613
+ string_value: "${encoder_engine_dir}"
614
+ }
615
+ }
616
+ parameters: {
617
+ key: "max_tokens_in_paged_kv_cache"
618
+ value: {
619
+ string_value: "${max_tokens_in_paged_kv_cache}"
620
+ }
621
+ }
622
+ parameters: {
623
+ key: "max_attention_window_size"
624
+ value: {
625
+ string_value: "${max_attention_window_size}"
626
+ }
627
+ }
628
+ parameters: {
629
+ key: "sink_token_length"
630
+ value: {
631
+ string_value: "${sink_token_length}"
632
+ }
633
+ }
634
+ parameters: {
635
+ key: "batch_scheduler_policy"
636
+ value: {
637
+ string_value: "${batch_scheduler_policy}"
638
+ }
639
+ }
640
+ parameters: {
641
+ key: "kv_cache_free_gpu_mem_fraction"
642
+ value: {
643
+ string_value: "${kv_cache_free_gpu_mem_fraction}"
644
+ }
645
+ }
646
+ parameters: {
647
+ key: "cross_kv_cache_fraction"
648
+ value: {
649
+ string_value: "${cross_kv_cache_fraction}"
650
+ }
651
+ }
652
+ parameters: {
653
+ key: "kv_cache_host_memory_bytes"
654
+ value: {
655
+ string_value: "${kv_cache_host_memory_bytes}"
656
+ }
657
+ }
658
+ # kv_cache_onboard_blocks is for internal implementation.
659
+ parameters: {
660
+ key: "kv_cache_onboard_blocks"
661
+ value: {
662
+ string_value: "${kv_cache_onboard_blocks}"
663
+ }
664
+ }
665
+ # enable_trt_overlap is deprecated and doesn't have any effect on the runtime
666
+ # parameters: {
667
+ # key: "enable_trt_overlap"
668
+ # value: {
669
+ # string_value: "${enable_trt_overlap}"
670
+ # }
671
+ # }
672
+ parameters: {
673
+ key: "exclude_input_in_output"
674
+ value: {
675
+ string_value: "${exclude_input_in_output}"
676
+ }
677
+ }
678
+ parameters: {
679
+ key: "cancellation_check_period_ms"
680
+ value: {
681
+ string_value: "${cancellation_check_period_ms}"
682
+ }
683
+ }
684
+ parameters: {
685
+ key: "stats_check_period_ms"
686
+ value: {
687
+ string_value: "${stats_check_period_ms}"
688
+ }
689
+ }
690
+ parameters: {
691
+ key: "iter_stats_max_iterations"
692
+ value: {
693
+ string_value: "${iter_stats_max_iterations}"
694
+ }
695
+ }
696
+ parameters: {
697
+ key: "request_stats_max_iterations"
698
+ value: {
699
+ string_value: "${request_stats_max_iterations}"
700
+ }
701
+ }
702
+ parameters: {
703
+ key: "enable_kv_cache_reuse"
704
+ value: {
705
+ string_value: "${enable_kv_cache_reuse}"
706
+ }
707
+ }
708
+ parameters: {
709
+ key: "normalize_log_probs"
710
+ value: {
711
+ string_value: "${normalize_log_probs}"
712
+ }
713
+ }
714
+ parameters: {
715
+ key: "enable_chunked_context"
716
+ value: {
717
+ string_value: "${enable_chunked_context}"
718
+ }
719
+ }
720
+ parameters: {
721
+ key: "gpu_device_ids"
722
+ value: {
723
+ string_value: "${gpu_device_ids}"
724
+ }
725
+ }
726
+ parameters: {
727
+ key: "participant_ids"
728
+ value: {
729
+ string_value: "${participant_ids}"
730
+ }
731
+ }
732
+ parameters: {
733
+ key: "lora_cache_optimal_adapter_size"
734
+ value: {
735
+ string_value: "${lora_cache_optimal_adapter_size}"
736
+ }
737
+ }
738
+ parameters: {
739
+ key: "lora_cache_max_adapter_size"
740
+ value: {
741
+ string_value: "${lora_cache_max_adapter_size}"
742
+ }
743
+ }
744
+ parameters: {
745
+ key: "lora_cache_gpu_memory_fraction"
746
+ value: {
747
+ string_value: "${lora_cache_gpu_memory_fraction}"
748
+ }
749
+ }
750
+ parameters: {
751
+ key: "lora_cache_host_memory_bytes"
752
+ value: {
753
+ string_value: "${lora_cache_host_memory_bytes}"
754
+ }
755
+ }
756
+ parameters: {
757
+ key: "lora_prefetch_dir"
758
+ value: {
759
+ string_value: "${lora_prefetch_dir}"
760
+ }
761
+ }
762
+ parameters: {
763
+ key: "decoding_mode"
764
+ value: {
765
+ string_value: "${decoding_mode}"
766
+ }
767
+ }
768
+ parameters: {
769
+ key: "executor_worker_path"
770
+ value: {
771
+ string_value: "/opt/tritonserver/backends/tensorrtllm/trtllmExecutorWorker"
772
+ }
773
+ }
774
+ parameters: {
775
+ key: "lookahead_window_size"
776
+ value: {
777
+ string_value: "${lookahead_window_size}"
778
+ }
779
+ }
780
+ parameters: {
781
+ key: "lookahead_ngram_size"
782
+ value: {
783
+ string_value: "${lookahead_ngram_size}"
784
+ }
785
+ }
786
+ parameters: {
787
+ key: "lookahead_verification_set_size"
788
+ value: {
789
+ string_value: "${lookahead_verification_set_size}"
790
+ }
791
+ }
792
+ parameters: {
793
+ key: "medusa_choices"
794
+ value: {
795
+ string_value: "${medusa_choices}"
796
+ }
797
+ }
798
+ parameters: {
799
+ key: "eagle_choices"
800
+ value: {
801
+ string_value: "${eagle_choices}"
802
+ }
803
+ }
804
+ parameters: {
805
+ key: "gpu_weights_percent"
806
+ value: {
807
+ string_value: "${gpu_weights_percent}"
808
+ }
809
+ }
810
+ parameters: {
811
+ key: "enable_context_fmha_fp32_acc"
812
+ value: {
813
+ string_value: "${enable_context_fmha_fp32_acc}"
814
+ }
815
+ }
816
+ parameters: {
817
+ key: "multi_block_mode"
818
+ value: {
819
+ string_value: "${multi_block_mode}"
820
+ }
821
+ }
822
+ parameters: {
823
+ key: "cuda_graph_mode"
824
+ value: {
825
+ string_value: "${cuda_graph_mode}"
826
+ }
827
+ }
828
+ parameters: {
829
+ key: "cuda_graph_cache_size"
830
+ value: {
831
+ string_value: "${cuda_graph_cache_size}"
832
+ }
833
+ }
834
+ parameters: {
835
+ key: "speculative_decoding_fast_logits"
836
+ value: {
837
+ string_value: "${speculative_decoding_fast_logits}"
838
+ }
839
+ }
840
+ parameters: {
841
+ key: "tokenizer_dir"
842
+ value: {
843
+ string_value: "${tokenizer_dir}"
844
+ }
845
+ }
846
+ parameters: {
847
+ key: "guided_decoding_backend"
848
+ value: {
849
+ string_value: "${guided_decoding_backend}"
850
+ }
851
+ }
852
+ parameters: {
853
+ key: "xgrammar_tokenizer_info_path"
854
+ value: {
855
+ string_value: "${xgrammar_tokenizer_info_path}"
856
+ }
857
+ }
runtime/triton_trtllm/model_repo/vocoder/1/model.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ #
3
+ # Redistribution and use in source and binary forms, with or without
4
+ # modification, are permitted provided that the following conditions
5
+ # are met:
6
+ # * Redistributions of source code must retain the above copyright
7
+ # notice, this list of conditions and the following disclaimer.
8
+ # * Redistributions in binary form must reproduce the above copyright
9
+ # notice, this list of conditions and the following disclaimer in the
10
+ # documentation and/or other materials provided with the distribution.
11
+ # * Neither the name of NVIDIA CORPORATION nor the names of its
12
+ # contributors may be used to endorse or promote products derived
13
+ # from this software without specific prior written permission.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
16
+ # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23
+ # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ import json
27
+ import torch
28
+ from torch import nn
29
+ from torch.nn.utils.rnn import pad_sequence
30
+ import torch.nn.functional as F
31
+ from torch.utils.dlpack import from_dlpack, to_dlpack
32
+
33
+ import triton_python_backend_utils as pb_utils
34
+
35
+ import math
36
+ import os
37
+ from functools import wraps
38
+
39
+ from sparktts.models.bicodec import BiCodec
40
+
41
+ class TritonPythonModel:
42
+ def initialize(self, args):
43
+ parameters = json.loads(args['model_config'])['parameters']
44
+ for key, value in parameters.items():
45
+ parameters[key] = value["string_value"]
46
+ model_dir = parameters["model_dir"]
47
+ self.device = torch.device("cuda")
48
+ self.vocoder = BiCodec.load_from_checkpoint(f"{model_dir}/BiCodec").to(
49
+ self.device
50
+ )
51
+
52
+ def execute(self, requests):
53
+ global_tokens_list, semantic_tokens_list = [], []
54
+
55
+ for request in requests:
56
+ global_tokens_tensor = pb_utils.get_input_tensor_by_name(request, "global_tokens").as_numpy()
57
+ semantic_tokens_tensor = pb_utils.get_input_tensor_by_name(request, "semantic_tokens").as_numpy()
58
+ # check shape
59
+ global_tokens_list.append(torch.from_numpy(global_tokens_tensor).to(self.device))
60
+ semantic_tokens_list.append(torch.from_numpy(semantic_tokens_tensor).to(self.device))
61
+
62
+ global_tokens = torch.cat(global_tokens_list, dim=0)
63
+ semantic_tokens = torch.cat(semantic_tokens_list, dim=0)
64
+ print(global_tokens.shape, semantic_tokens.shape, 233333333333, "global_tokens, semantic_tokens")
65
+
66
+ wavs = self.vocoder.detokenize(semantic_tokens, global_tokens.unsqueeze(1))
67
+
68
+ responses = []
69
+ for i in range(len(requests)):
70
+ wav_tensor = pb_utils.Tensor.from_dlpack("waveform", to_dlpack(wavs[i]))
71
+ inference_response = pb_utils.InferenceResponse(output_tensors=[wav_tensor])
72
+ responses.append(inference_response)
73
+
74
+ return responses
runtime/triton_trtllm/model_repo/vocoder/config.pbtxt ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ name: "vocoder"
16
+ backend: "python"
17
+ max_batch_size: 16
18
+ dynamic_batching {
19
+ max_queue_delay_microseconds: 1
20
+ }
21
+ parameters [
22
+ {
23
+ key: "model_dir",
24
+ value: {string_value:"/workspace_yuekai/spark-tts/Spark-TTS/pretrained_models/Spark-TTS-0.5B"}
25
+ }
26
+ ]
27
+
28
+ input [
29
+ {
30
+ name: "global_tokens"
31
+ data_type: TYPE_INT32
32
+ dims: [-1]
33
+ },
34
+ {
35
+ name: "semantic_tokens"
36
+ data_type: TYPE_INT32
37
+ dims: [-1]
38
+ }
39
+ ]
40
+ output [
41
+ {
42
+ name: "waveform"
43
+ data_type: TYPE_FP32
44
+ dims: [ -1 ]
45
+ }
46
+ ]
47
+
48
+ instance_group [
49
+ {
50
+ count: 1
51
+ kind: KIND_CPU
52
+ }
53
+ ]