Distributed LLM Generation#
Source NVIDIA/TensorRT-LLM.
1### Distributed LLM Generation
2from tensorrt_llm import SamplingParams
3from tensorrt_llm._tensorrt_engine import LLM
4
5
6def main():
7 # model could accept HF model name or a path to local HF model.
8 llm = LLM(
9 model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
10 # Enable 2-way tensor parallelism
11 tensor_parallel_size=2
12 # Enable 2-way pipeline parallelism if needed
13 # pipeline_parallel_size=2
14 # Enable 2-way expert parallelism for MoE model's expert weights
15 # moe_expert_parallel_size=2
16 # Enable 2-way tensor parallelism for MoE model's expert weights
17 # moe_tensor_parallel_size=2
18 )
19
20 # Sample prompts.
21 prompts = [
22 "Hello, my name is",
23 "The president of the United States is",
24 "The capital of France is",
25 "The future of AI is",
26 ]
27
28 # Create a sampling params.
29 sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
30
31 for output in llm.generate(prompts, sampling_params):
32 print(
33 f"Prompt: {output.prompt!r}, Generated text: {output.outputs[0].text!r}"
34 )
35
36 # Got output like
37 # Prompt: 'Hello, my name is', Generated text: '\n\nJane Smith. I am a student pursuing my degree in Computer Science at [university]. I enjoy learning new things, especially technology and programming'
38 # Prompt: 'The president of the United States is', Generated text: 'likely to nominate a new Supreme Court justice to fill the seat vacated by the death of Antonin Scalia. The Senate should vote to confirm the'
39 # Prompt: 'The capital of France is', Generated text: 'Paris.'
40 # Prompt: 'The future of AI is', Generated text: 'an exciting time for us. We are constantly researching, developing, and improving our platform to create the most advanced and efficient model available. We are'
41
42
43# The entry point of the program need to be protected for spawning processes.
44if __name__ == '__main__':
45 main()