Views
No views yet


1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3
4model_id = "RedHatAI/NVIDIA-Nemotron-Nano-9B-v2-quantized.w4a16"
5number_gpus = 1
6sampling_params = SamplingParams(temperature=0.6, top_p=0.95, top_k=20, min_p=0, max_tokens=256)
7
8messages = [
9 {"role": "user", "content": prompt}
10]
11
12tokenizer = AutoTokenizer.from_pretrained(model_id)
13
14messages = [{"role": "user", "content": "Give me a short introduction to large language model."}]
15
16prompts = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
17
18llm = LLM(model=model_id, tensor_parallel_size=number_gpus)
19
20outputs = llm.generate(prompts, sampling_params)
21
22generated_text = outputs[0].outputs[0].text
23print(generated_text)1podman run --rm -it --device nvidia.com/gpu=all -p 8000:8000 \
2 --ipc=host \
3--env "HUGGING_FACE_HUB_TOKEN=$HF_TOKEN" \
4--env "HF_HUB_OFFLINE=0" -v ~/.cache/vllm:/home/vllm/.cache \
5--name=vllm \
6registry.access.redhat.com/rhaiis/rh-vllm-cuda \
7vllm serve \
8--tensor-parallel-size 8 \
9--max-model-len 32768 \
10--enforce-eager --model RedHatAI/NVIDIA-Nemotron-Nano-9B-v2-quantized.w4a161# Setting up vllm server with ServingRuntime
2# Save as: vllm-servingruntime.yaml
3apiVersion: serving.kserve.io/v1alpha1
4kind: ServingRuntime
5metadata:
6 name: vllm-cuda-runtime # OPTIONAL CHANGE: set a unique name
7 annotations:
8 openshift.io/display-name: vLLM NVIDIA GPU ServingRuntime for KServe
9 opendatahub.io/recommended-accelerators: '["nvidia.com/gpu"]'
10 labels:
11 opendatahub.io/dashboard: 'true'
12spec:
13 annotations:
14 prometheus.io/port: '8080'
15 prometheus.io/path: '/metrics'
16 multiModel: false
17 supportedModelFormats:
18 - autoSelect: true
19 name: vLLM
20 containers:
21 - name: kserve-container
22 image: quay.io/modh/vllm:rhoai-2.25-cuda # CHANGE if needed. If AMD: quay.io/modh/vllm:rhoai-2.25-rocm
23 command:
24 - python
25 - -m
26 - vllm.entrypoints.openai.api_server
27 args:
28 - "--port=8080"
29 - "--model=/mnt/models"
30 - "--served-model-name={{.Name}}"
31 env:
32 - name: HF_HOME
33 value: /tmp/hf_home
34 ports:
35 - containerPort: 8080
36 protocol: TCP1# Attach model to vllm server. This is an NVIDIA template
2# Save as: inferenceservice.yaml
3apiVersion: serving.kserve.io/v1beta1
4kind: InferenceService
5metadata:
6 annotations:
7 openshift.io/display-name: NVIDIA-Nemotron-Nano-9B-v2-quantized.w4a16 # OPTIONAL CHANGE
8 serving.kserve.io/deploymentMode: RawDeployment
9 name: NVIDIA-Nemotron-Nano-9B-v2-quantized.w4a16 # specify model name. This value will be used to invoke the model in the payload
10 labels:
11 opendatahub.io/dashboard: 'true'
12spec:
13 predictor:
14 maxReplicas: 1
15 minReplicas: 1
16 model:
17 modelFormat:
18 name: vLLM
19 name: ''
20 resources:
21 limits:
22 cpu: '2' # this is model specific
23 memory: 8Gi # this is model specific
24 nvidia.com/gpu: '1' # this is accelerator specific
25 requests: # same comment for this block
26 cpu: '1'
27 memory: 4Gi
28 nvidia.com/gpu: '1'
29 runtime: vllm-cuda-runtime # must match the ServingRuntime name above
30 storageUri: oci://registry.redhat.io/rhelai1/modelcar-nvidia-nemotron-nano-9b-v2-quantized.w4a16:1.5
31 tolerations:
32 - effect: NoSchedule
33 key: nvidia.com/gpu
34 operator: Exists1# make sure first to be in the project where you want to deploy the model
2# oc project <project-name>
3
4# apply both resources to run model
5
6# Apply the ServingRuntime
7oc apply -f vllm-servingruntime.yaml
81# Replace <inference-service-name> and <cluster-ingress-domain> below:
2# - Run `oc get inferenceservice` to find your URL if unsure.
3
4# Call the server using curl:
5curl https://<inference-service-name>-predictor-default.<domain>/v1/chat/completions
6 -H "Content-Type: application/json" \
7 -d '{
8 "model": "NVIDIA-Nemotron-Nano-9B-v2-quantized.w4a16",
9 "stream": true,
10 "stream_options": {
11 "include_usage": true
12 },
13 "max_tokens": 1,
14 "messages": [
15 {
16 "role": "user",
17 "content": "How can a bee fly when its wings are so small?"
18 }
19 ]
20}'
211from compressed_tensors.quantization import QuantizationScheme, QuantizationArgs, QuantizationType, QuantizationStrategy
2from llmcompressor.modifiers.quantization import GPTQModifier
3from llmcompressor.transformers import oneshot
4from transformers import AutoModelForCausalLM, AutoTokenizer
5
6# Load model
7model_stub = "nvidia/NVIDIA-Nemotron-Nano-9B-v2"
8model_name = model_stub.split("/")[-1]
9
10num_samples = 1024
11max_seq_len = 8192
12
13model = AutoModelForCausalLM.from_pretrained(model_stub)
14
15tokenizer = AutoTokenizer.from_pretrained(model_stub)
16
17def preprocess_fn(example):
18 return {"text": tokenizer.apply_chat_template(example["messages"], add_generation_prompt=False, tokenize=False)}
19
20ds = load_dataset("neuralmagic/LLM_compression_calibration", split="train")
21ds = ds.map(preprocess_fn)
22
23# Configure the quantization algorithm and scheme
24quant_scheme = QuantizationScheme(
25 targets=["Linear"],
26 weights=QuantizationArgs(
27 num_bits=4,
28 type=QuantizationType.INT,
29 symmetric=True,
30 group_size=64,
31 strategy=QuantizationStrategy.GROUP,
32 observer="mse",
33 actorder="weight"
34 ),
35 input_activations=None,
36 output_activations=None,
37)
38
39recipe = [
40 GPTQModifier(
41 ignore=["lm_head", "NemotronHMamba2Mixer"],
42 dampening_frac=0.07,
43 config_groups={"group_0": quant_scheme},
44 )
45]
46
47# Apply quantization
48oneshot(
49 model=model,
50 dataset=ds,
51 recipe=recipe,
52 max_seq_length=max_seq_len,
53 num_calibration_samples=num_samples,
54)
55
56# Save to disk in compressed-tensors format
57save_path = model_name + "-quantized.w4a16"
58model.save_pretrained(save_path)
59tokenizer.save_pretrained(save_path)
60print(f"Model and tokenizer saved to: {save_path}")v0.11.1.dev0.
vLLM v0.11.1rc2.dev191+g80e945298.precompiled was used as the inference engine for all evaluations.1model_parameters:
2 model_name: "hosted_vllm/RedHatAI/NVIDIA-Nemotron-Nano-9B-v2-quantized.w4a16"
3 base_url: "http://0.0.0.0:8000/v1"
4 generation_parameters:
5 temperature: 0.6
6 min_p: 0.0
7 max_new_tokens: 65536
8 top_p: 0.95
9 seed: 0lighteval endpoint litellm lighteval_model_arguments.yaml \
"lighteval|aime25|0,lighteval|math_500|0,lighteval|gpqa:diamond|0" \
--output-dir $OUTPUT_DIR \
--save-detailsvllm serve RedHatAI/NVIDIA-Nemotron-Nano-9B-v2-quantized.w4a16 \
--trust-remote-code \
--mamba_ssm_cache_dtype float32 \
-tp 1 \
--port 8000 \
--gpu-memory-utilization 0.9| Category | Benchmark | NVIDIA-Nemotron-Nano-9B-v2 | NVIDIA-Nemotron-Nano-9B-v2-quantized.w4a16 (this model) | Recovery |
|---|---|---|---|---|
| Reasoning (generation) | ||||
| AIME 2025 | 61.33 | 58.00 | 94.6% | |
| GPQA diamond | 56.26 | 56.16 | 99.8% | |
| Math-lvl-5 | 96.08 | 96.16 | 100.0% | |
| Average Score | 71.22 | 70.11 | 98.44% |