Instructions to use amd/gemma-4-31B-it-MXFP4 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use amd/gemma-4-31B-it-MXFP4 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="amd/gemma-4-31B-it-MXFP4") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("amd/gemma-4-31B-it-MXFP4") model = AutoModelForMultimodalLM.from_pretrained("amd/gemma-4-31B-it-MXFP4", device_map="auto") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use amd/gemma-4-31B-it-MXFP4 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "amd/gemma-4-31B-it-MXFP4" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "amd/gemma-4-31B-it-MXFP4", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/amd/gemma-4-31B-it-MXFP4
- SGLang
How to use amd/gemma-4-31B-it-MXFP4 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "amd/gemma-4-31B-it-MXFP4" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "amd/gemma-4-31B-it-MXFP4", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "amd/gemma-4-31B-it-MXFP4" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "amd/gemma-4-31B-it-MXFP4", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use amd/gemma-4-31B-it-MXFP4 with Docker Model Runner:
docker model run hf.co/amd/gemma-4-31B-it-MXFP4
Model Overview
- Base model:
google/gemma-4-31B-it - Model architecture:
Gemma4ForConditionalGeneration(text-only eval usesGemma4ForCausalLMoverride) - Quantizer: AMD Quark
- Quantization scheme:
mxfp4 - Weight quantization: FP4, per-group (MX format)
- Activation quantization: FP4, dynamic, per-group (MX format)
- Export format: Hugging Face safetensors (
quant_method=quark) - Excluded from quantization: vision tower, multimodal projector,
lm_head
Runtime Environment
This workflow is intended to run on AMD Instinct MI355.
Start Docker Container
docker run -it -d \
--name vllm-gemma4-openai \
--ipc=host \
--shm-size=64g \
--network=host \
--privileged \
--cap-add=CAP_SYS_ADMIN \
--device=/dev/kfd \
--device=/dev/dri \
--device=/dev/mem \
--cap-add=SYS_PTRACE \
--security-opt seccomp=unconfined \
--entrypoint /bin/bash \
-v /shareddata:/shareddata -v /mnt:/mnt -v /data:/data -v /home/:/home -v /raid:/raid -v /docker:/docker -v /datasets:/datasets -v /scratch:/scratch -v /group:/group \
vllm/vllm-openai-rocm:gemma4
Quantization
Run quantization inside Docker image vllm/vllm-openai-rocm:gemma4.
Minimal quantization code
from quark.common.profiler import GlobalProfiler
from quark.torch import LLMTemplate, ModelQuantizer
ckpt_path = "google/gemma-4-31B-it"
output_dir = "/path/to/amd/gemma-4-31B-it-mxfp4"
exclude_layers = ["*vision_tower*", "*multi_modal_projector*", "*lm_head"]
if "gemma4" not in LLMTemplate.list_available():
gemma4_template = LLMTemplate(
model_type="gemma4",
kv_layers_name=["*language_model.*k_proj", "*language_model.*v_proj"],
q_layer_name="*language_model.*q_proj",
exclude_layers_name=exclude_layers,
)
LLMTemplate.register_template(gemma4_template)
template = LLMTemplate.get("gemma4")
quant_config = template.get_config(
scheme="mxfp4",
exclude_layers=exclude_layers,
)
profiler = GlobalProfiler(output_path=f"{output_dir}/quark_profile.yaml")
quantizer = ModelQuantizer(quant_config)
quantizer.direct_quantize_checkpoint(
pretrained_model_path=ckpt_path,
save_path=output_dir,
)
Evaluation
Reproduction command
CUDA_VISIBLE_DEVICES=6,7 lm_eval run \
--model vllm \
--tasks gsm8k \
--model_args '{"pretrained":"amd/gemma-4-31B-it-mxfp4","tensor_parallel_size":2,"max_model_len":16384,"gpu_memory_utilization":0.90,"max_gen_toks":2048,"trust_remote_code":true,"chat_template":"/scratch/kewang/workspace/vllm/examples/tool_chat_template_gemma4.jinja","reasoning_parser":"gemma4","hf_overrides":{"architectures":["Gemma4ForCausalLM"]}}' \
--num_fewshot 5 \
--batch_size auto \
--apply_chat_template
Accuracy
The following GSM8K results are from local runs in this workspace.
| Benchmark | Metric | google/gemma-4-31B-it |
amd/gemma-4-31B-it-mxfp4 |
|---|---|---|---|
| gsm8k | exact_match (flexible-extract) | 92.65 | 91.74 |
| gsm8k | exact_match (strict-match) | 92.27 | 91.36 |
License
This checkpoint is derived from google/gemma-4-31B-it. Please follow the upstream model license and usage terms.
- Downloads last month
- 53