Views
No views yet


1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3
4model_id = "neuralmagic-ent/phi-4-quantized.w8a8"
5number_gpus = 1
6
7sampling_params = SamplingParams(temperature=0.7, top_p=0.8, max_tokens=256)
8
9tokenizer = AutoTokenizer.from_pretrained(model_id)
10
11messages = [
12 {"role": "user", "content": "Give me a short introduction to large language model."},
13]
14
15prompts = tokenizer.apply_chat_template(messages, tokenize=False)
16
17llm = LLM(model=model_id, tensor_parallel_size=number_gpus)
18
19outputs = llm.generate(prompts, sampling_params)
20
21generated_text = outputs[0].outputs[0].text
22print(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/phi-4-quantized.w8a81# Download model from Red Hat Registry via docker
2# Note: This downloads the model to ~/.cache/instructlab/models unless --model-dir is specified.
3ilab model download --repository docker://registry.redhat.io/rhelai1/phi-4-quantized-w8a8:1.51# Serve model via ilab
2ilab model serve --model-path ~/.cache/instructlab/models/phi-4-quantized-w8a8
3
4# Chat with model
5ilab model chat --model ~/.cache/instructlab/models/phi-4-quantized-w8a81# 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.20-cuda # CHANGE if needed. If AMD: quay.io/modh/vllm:rhoai-2.20-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: phi-4-quantized.w8a8 # OPTIONAL CHANGE
8 serving.kserve.io/deploymentMode: RawDeployment
9 name: phi-4-quantized.w8a8 # 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-phi-4-quantized-w8a8: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# apply both resources to run model
4# Apply the ServingRuntime
5oc apply -f vllm-servingruntime.yaml
6# Apply the InferenceService
7oc apply -f qwen-inferenceservice.yaml1# Replace <inference-service-name> and <cluster-ingress-domain> below:
2# - Run `oc get inferenceservice` to find your URL if unsure.
3# Call the server using curl:
4curl https://<inference-service-name>-predictor-default.<domain>/v1/chat/completions
5 -H "Content-Type: application/json" \
6 -d '{
7 "model": "phi-4-quantized.w8a8",
8 "stream": true,
9 "stream_options": {
10 "include_usage": true
11 },
12 "max_tokens": 1,
13 "messages": [
14 {
15 "role": "user",
16 "content": "How can a bee fly when its wings are so small?"
17 }
18 ]
19}'1from transformers import AutoModelForCausalLM, AutoTokenizer
2from llmcompressor.modifiers.quantization import GPTQModifier
3from llmcompressor.modifiers.smoothquant import SmoothQuantModifier
4from llmcompressor.transformers import oneshot
5from datasets import load_dataset
6
7# Load model
8model_stub = "microsoft/phi-4"
9model_name = model_stub.split("/")[-1]
10
11num_samples = 1024
12max_seq_len = 8192
13
14tokenizer = AutoTokenizer.from_pretrained(model_stub)
15
16model = AutoModelForCausalLM.from_pretrained(
17 model_stub,
18 device_map="auto",
19 torch_dtype="auto",
20)
21
22def preprocess_fn(example):
23 return {"text": tokenizer.apply_chat_template(example["messages"], add_generation_prompt=False, tokenize=False)}
24
25ds = load_dataset("neuralmagic/LLM_compression_calibration", split="train")
26ds = ds.map(preprocess_fn)
27
28# Configure the quantization algorithm and scheme
29recipe = [
30 SmoothQuantModifier(
31 smoothing_strength=0.7,
32 mappings=[
33 [["re:.*qkv_proj"], "re:.*input_layernorm"],
34 [["re:.*gate_up_proj"], "re:.*post_attention_layernorm"],
35 ],
36 ),
37 GPTQModifier(
38 ignore=["lm_head"],
39 sequential_targets=["Phi3DecoderLayer"],
40 dampening_frac=0.01,
41 targets="Linear",
42 scheme="W8A8",
43 ),
44]
45
46# Apply quantization
47oneshot(
48 model=model,
49 dataset=ds,
50 recipe=recipe,
51 max_seq_length=max_seq_len,
52 num_calibration_samples=num_samples,
53)
54
55# Save to disk in compressed-tensors format
56save_path = model_name + "-quantized.w8a8"
57model.save_pretrained(save_path)
58tokenizer.save_pretrained(save_path)
59print(f"Model and tokenizer saved to: {save_path}")lm_eval \
--model vllm \
--model_args pretrained="neuralmagic-ent/phi-4-quantized.w8a8",dtype=auto,gpu_memory_utilization=0.6,max_model_len=4096,enable_chunk_prefill=True,tensor_parallel_size=1 \
--tasks openllm \
--batch_size auto| Benchmark | phi-4 | phi-4-quantized.w8a8 (this model) | Recovery |
| MMLU (5-shot) | 80.30 | 80.39 | 100.1% |
| ARC Challenge (25-shot) | 64.42 | 64.33 | 99.9% |
| GSM-8K (5-shot, strict-match) | 90.07 | 90.30 | 100.3% |
| Hellaswag (10-shot) | 84.37 | 84.30 | 99.9% |
| Winogrande (5-shot) | 80.58 | 79.95 | 99.2% |
| TruthfulQA (0-shot, mc2) | 59.37 | 58.82 | 99.1% |
| Average | 76.52 | 76.35 | 99.8% |