Offline Inference Neuron Int8 Quantization#
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# Quantizes neuron model weight to int8 ,
10# The default config for quantization is int8 dtype.
11os.environ['NEURON_QUANT_DTYPE'] = "s8"
12
13# Sample prompts.
14prompts = [
15 "Hello, my name is",
16 "The president of the United States is",
17 "The capital of France is",
18 "The future of AI is",
19]
20# Create a sampling params object.
21sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
22
23# Create an LLM.
24llm = LLM(
25 model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
26 max_num_seqs=8,
27 # The max_model_len and block_size arguments are required to be same as
28 # max sequence length when targeting neuron device.
29 # Currently, this is a known limitation in continuous batching support
30 # in transformers-neuronx.
31 # TODO(liangfu): Support paged-attention in transformers-neuronx.
32 max_model_len=2048,
33 block_size=2048,
34 # The device can be automatically detected when AWS Neuron SDK is installed.
35 # The device argument can be either unspecified for automated detection,
36 # or explicitly assigned.
37 device="neuron",
38 quantization="neuron_quant",
39 override_neuron_config={
40 "cast_logits_dtype": "bfloat16",
41 },
42 tensor_parallel_size=2)
43# Generate texts from the prompts. The output is a list of RequestOutput objects
44# that contain the prompt, generated text, and other information.
45outputs = llm.generate(prompts, sampling_params)
46# Print the outputs.
47for output in outputs:
48 prompt = output.prompt
49 generated_text = output.outputs[0].text
50 print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")