Run this model with HF Jobs
Run google/gemma-4-31B-it on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs and TRL to fine-tune https://huggingface.co/google/gemma-4-31B-it. Ask me which dataset to train on, then push the LoRA adapter to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs and Transformers to run batch inference with https://huggingface.co/google/gemma-4-31B-it on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the predictions back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Evaluate on a benchmark
Use HF Jobs and Lighteval to evaluate https://huggingface.co/google/gemma-4-31B-it. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
Quantize for lower memory use
Use HF Jobs to create a 4-bit version of https://huggingface.co/google/gemma-4-31B-it with Transformers and bitsandbytes. Push it to a new model repo, then reload it from there and report its memory footprint. Run `hf jobs --help` first to learn the CLI.
Instructions to use google/gemma-4-31B-it with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/gemma-4-31B-it with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="google/gemma-4-31B-it") 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)# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("google/gemma-4-31B-it") model = AutoModelForMultimodalLM.from_pretrained("google/gemma-4-31B-it", 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=256) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
- AMD Developer Cloud
- Local Apps Settings
- vLLM
How to use google/gemma-4-31B-it with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "google/gemma-4-31B-it" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-4-31B-it", "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/google/gemma-4-31B-it
- SGLang
How to use google/gemma-4-31B-it 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 "google/gemma-4-31B-it" \ --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": "google/gemma-4-31B-it", "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 "google/gemma-4-31B-it" \ --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": "google/gemma-4-31B-it", "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 google/gemma-4-31B-it with Docker Model Runner:
docker model run hf.co/google/gemma-4-31B-it
fix(chat_template): update SI and tool call handling
Browse files12345678955 unmodified lines65666768697071106 unmodified lines1781791801811821831841851861871881891901911921931941952 unmodified lines19819920020120220320497 unmodified lines30230330430530630720 unmodified lines3283293303313323333343353363376 unmodified lines344345346347{%- macro format_parameters(properties, required) -%}{%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}{%- set ns = namespace(found_first=false) -%}{%- for key, value in properties | dictsort -%}{%- set add_comma = false -%}{%- if key not in standard_keys -%}{%- if ns.found_first %},{% endif -%}{%- set ns.found_first = true -%}{{ key }}:{55 unmodified lines{%- elif value is mapping -%}{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}properties:{{{- format_parameters(value, value['required'] | default([])) -}}}{%- endif -%}{%- if value['required'] -%}106 unmodified lines{#- Handle System/Tool Definitions Block -#}{%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}{{- '<|turn>system\n' -}}{#- Inject Thinking token at the very top of the FIRST system turn -#}{%- if enable_thinking is defined and enable_thinking -%}{{- '<|think|>\n' -}}{%- set ns.prev_message_type = 'think' -%}{%- endif -%}{%- if messages[0]['role'] in ['system', 'developer'] -%}{{- messages[0]['content'] | trim -}}{%- set loop_messages = messages[1:] -%}{%- endif -%}{%- if tools -%}{%- for tool in tools %}{{- '<|tool>' -}}2 unmodified lines{%- endfor %}{%- set ns.prev_message_type = 'tool' -%}{%- endif -%}{{- '<turn|>\n' -}}{%- endif %}97 unmodified lines{%- endfor -%}{%- endif -%}{%- if message['content'] is string -%}{%- if role == 'model' -%}{{- strip_thinking(message['content']) -}}20 unmodified lines{%- endif -%}{%- endfor -%}{%- endif -%}{%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}{{- '<|tool_response>' -}}{%- elif not (ns_tr_out.flag and not message.get('content')) -%}{{- '<turn|>\n' -}}{%- endif -%}{%- endif -%}6 unmodified lines{{- '<|channel>thought\n<channel|>' -}}{%- endif -%}{%- endif -%}{%- endif -%}12345678955 unmodified lines65666768697071106 unmodified lines1781791801811821831841851861871881891901911921931941951961971982 unmodified lines20120220320420520697 unmodified lines30430530630730830931020 unmodified lines3313323333343353363373383393403413423433446 unmodified lines351352353354{%- macro format_parameters(properties, required, filter_keys=false) -%}{%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}{%- set ns = namespace(found_first=false) -%}{%- for key, value in properties | dictsort -%}{%- set add_comma = false -%}{%- if not filter_keys or key not in standard_keys -%}{%- if ns.found_first %},{% endif -%}{%- set ns.found_first = true -%}{{ key }}:{55 unmodified lines{%- elif value is mapping -%}{%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}properties:{{{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}}{%- endif -%}{%- if value['required'] -%}106 unmodified lines{#- Handle System/Tool Definitions Block -#}{%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}{{- '<|turn>system\n' -}}{#- Inject Thinking token at the very top of the FIRST system turn -#}{%- if enable_thinking is defined and enable_thinking -%}{{- '<|think|>\n' -}}{%- set ns.prev_message_type = 'think' -%}{%- endif -%}{%- if messages[0]['role'] in ['system', 'developer'] -%}{%- if messages[0]['content'] is string -%}{{- messages[0]['content'] | trim -}}{%- elif messages[0]['content'] is sequence -%}{%- for item in messages[0]['content'] -%}{{- item['text'] | trim + ' '-}}{%- endfor -%}{%- endif -%}{%- set loop_messages = messages[1:] -%}{%- endif -%}{%- if tools -%}{%- for tool in tools %}{{- '<|tool>' -}}2 unmodified lines{%- endfor %}{%- set ns.prev_message_type = 'tool' -%}{%- endif -%}{{- '<turn|>\n' -}}{%- endif %}97 unmodified lines{%- endfor -%}{%- endif -%}{%- set captured_content -%}{%- if message['content'] is string -%}{%- if role == 'model' -%}{{- strip_thinking(message['content']) -}}20 unmodified lines{%- endif -%}{%- endfor -%}{%- endif -%}{%- endset -%}{{- captured_content -}}{%- set has_content = captured_content | trim | length > 0 -%}{%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}{{- '<|tool_response>' -}}{%- elif not (ns_tr_out.flag and not has_content) -%}{{- '<turn|>\n' -}}{%- endif -%}{%- endif -%}6 unmodified lines{{- '<|channel>thought\n<channel|>' -}}{%- endif -%}{%- endif -%}{%- endif -%}
chat_template.jinja+17-10