Generation with Quantization
Source https://github.com/NVIDIA/TensorRT-LLM/tree/main/examples/llm-api/llm_quantization.py.
1### Generation with Quantization
2import logging
3
4import torch
5
6from tensorrt_llm import LLM, SamplingParams
7from tensorrt_llm.llmapi import CalibConfig, QuantAlgo, QuantConfig
8
9major, minor = torch.cuda.get_device_capability()
10enable_fp8 = major > 8 or (major == 8 and minor >= 9)
11enable_nvfp4 = major >= 10
12
13quant_and_calib_configs = []
14
15if not enable_nvfp4:
16 # Example 1: Specify int4 AWQ quantization to QuantConfig.
17 # We can skip specifying CalibConfig or leave a None as the default value.
18 quant_and_calib_configs.append(
19 (QuantConfig(quant_algo=QuantAlgo.W4A16_AWQ), None))
20
21if enable_fp8:
22 # Example 2: Specify FP8 quantization to QuantConfig.
23 # We can create a CalibConfig to specify the calibration dataset and other details.
24 # Note that the calibration dataset could be either HF dataset name or a path to local HF dataset.
25 quant_and_calib_configs.append(
26 (QuantConfig(quant_algo=QuantAlgo.FP8,
27 kv_cache_quant_algo=QuantAlgo.FP8),
28 CalibConfig(calib_dataset='cnn_dailymail',
29 calib_batches=256,
30 calib_max_seq_length=256)))
31else:
32 logging.error(
33 "FP8 quantization only works on post-ada GPUs. Skipped in the example.")
34
35if enable_nvfp4:
36 # Example 3: Specify NVFP4 quantization to QuantConfig.
37 quant_and_calib_configs.append(
38 (QuantConfig(quant_algo=QuantAlgo.NVFP4,
39 kv_cache_quant_algo=QuantAlgo.FP8),
40 CalibConfig(calib_dataset='cnn_dailymail',
41 calib_batches=256,
42 calib_max_seq_length=256)))
43else:
44 logging.error(
45 "NVFP4 quantization only works on Blackwell. Skipped in the example.")
46
47
48def main():
49
50 for quant_config, calib_config in quant_and_calib_configs:
51 # The built-in end-to-end quantization is triggered according to the passed quant_config.
52 llm = LLM(model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
53 quant_config=quant_config,
54 calib_config=calib_config)
55
56 # Sample prompts.
57 prompts = [
58 "Hello, my name is",
59 "The president of the United States is",
60 "The capital of France is",
61 "The future of AI is",
62 ]
63
64 # Create a sampling params.
65 sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
66
67 for output in llm.generate(prompts, sampling_params):
68 print(
69 f"Prompt: {output.prompt!r}, Generated text: {output.outputs[0].text!r}"
70 )
71
72 # Got output like
73 # Prompt: 'Hello, my name is', Generated text: 'Jane Smith. I am a resident of the city. Can you tell me more about the public services provided in the area?'
74 # Prompt: 'The president of the United States is', Generated text: 'considered the head of state, and the vice president of the United States is considered the head of state. President and Vice President of the United States (US)'
75 # Prompt: 'The capital of France is', Generated text: 'located in Paris, France. The population of Paris, France, is estimated to be 2 million. France is home to many famous artists, including Picasso'
76 # Prompt: 'The future of AI is', Generated text: 'an open and collaborative project. The project is an ongoing effort, and we invite participation from members of the community.\n\nOur community is'
77
78
79if __name__ == '__main__':
80 main()