Offline Inference Neuron#

Source vllm-project/vllm.

 1import os
 2
 3from vllm import LLM, SamplingParams
 4
 5# creates XLA hlo graphs for all the context length buckets.
 6os.environ['NEURON_CONTEXT_LENGTH_BUCKETS'] = "128,512,1024,2048"
 7# creates XLA hlo graphs for all the token gen buckets.
 8os.environ['NEURON_TOKEN_GEN_BUCKETS'] = "128,512,1024,2048"
 9
10# Sample prompts.
11prompts = [
12    "Hello, my name is",
13    "The president of the United States is",
14    "The capital of France is",
15    "The future of AI is",
16]
17# Create a sampling params object.
18sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
19
20# Create an LLM.
21llm = LLM(
22    model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
23    max_num_seqs=8,
24    # The max_model_len and block_size arguments are required to be same as
25    # max sequence length when targeting neuron device.
26    # Currently, this is a known limitation in continuous batching support
27    # in transformers-neuronx.
28    # TODO(liangfu): Support paged-attention in transformers-neuronx.
29    max_model_len=2048,
30    block_size=2048,
31    # The device can be automatically detected when AWS Neuron SDK is installed.
32    # The device argument can be either unspecified for automated detection,
33    # or explicitly assigned.
34    device="neuron",
35    tensor_parallel_size=2)
36# Generate texts from the prompts. The output is a list of RequestOutput objects
37# that contain the prompt, generated text, and other information.
38outputs = llm.generate(prompts, sampling_params)
39# Print the outputs.
40for output in outputs:
41    prompt = output.prompt
42    generated_text = output.outputs[0].text
43    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")