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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
|
| 14 |
-
### Model Description
|
| 15 |
|
| 16 |
-
<!-- Provide a longer summary of what this model is. -->
|
| 17 |
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
-
|
| 24 |
-
|
| 25 |
-
- **License:** [More Information Needed]
|
| 26 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
### Downstream Use [optional]
|
| 47 |
|
| 48 |
-
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
| 49 |
|
| 50 |
-
[More Information Needed]
|
| 51 |
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
## Bias, Risks, and Limitations
|
| 59 |
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
|
| 64 |
-
### Recommendations
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
| 69 |
-
|
| 70 |
-
## How to Get Started with the Model
|
| 71 |
-
|
| 72 |
-
Use the code below to get started with the model.
|
| 73 |
-
|
| 74 |
-
[More Information Needed]
|
| 75 |
-
|
| 76 |
-
## Training Details
|
| 77 |
-
|
| 78 |
-
### Training Data
|
| 79 |
-
|
| 80 |
-
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
| 81 |
-
|
| 82 |
-
[More Information Needed]
|
| 83 |
-
|
| 84 |
-
### Training Procedure
|
| 85 |
-
|
| 86 |
-
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
| 87 |
-
|
| 88 |
-
#### Preprocessing [optional]
|
| 89 |
-
|
| 90 |
-
[More Information Needed]
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
#### Training Hyperparameters
|
| 94 |
-
|
| 95 |
-
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
| 96 |
-
|
| 97 |
-
#### Speeds, Sizes, Times [optional]
|
| 98 |
-
|
| 99 |
-
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
| 100 |
-
|
| 101 |
-
[More Information Needed]
|
| 102 |
-
|
| 103 |
-
## Evaluation
|
| 104 |
-
|
| 105 |
-
<!-- This section describes the evaluation protocols and provides the results. -->
|
| 106 |
-
|
| 107 |
-
### Testing Data, Factors & Metrics
|
| 108 |
-
|
| 109 |
-
#### Testing Data
|
| 110 |
-
|
| 111 |
-
<!-- This should link to a Dataset Card if possible. -->
|
| 112 |
-
|
| 113 |
-
[More Information Needed]
|
| 114 |
-
|
| 115 |
-
#### Factors
|
| 116 |
-
|
| 117 |
-
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 118 |
-
|
| 119 |
-
[More Information Needed]
|
| 120 |
-
|
| 121 |
-
#### Metrics
|
| 122 |
-
|
| 123 |
-
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
| 124 |
-
|
| 125 |
-
[More Information Needed]
|
| 126 |
-
|
| 127 |
-
### Results
|
| 128 |
-
|
| 129 |
-
[More Information Needed]
|
| 130 |
-
|
| 131 |
-
#### Summary
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
## Model Examination [optional]
|
| 136 |
-
|
| 137 |
-
<!-- Relevant interpretability work for the model goes here -->
|
| 138 |
-
|
| 139 |
-
[More Information Needed]
|
| 140 |
-
|
| 141 |
-
## Environmental Impact
|
| 142 |
-
|
| 143 |
-
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 144 |
-
|
| 145 |
-
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
| 146 |
-
|
| 147 |
-
- **Hardware Type:** [More Information Needed]
|
| 148 |
-
- **Hours used:** [More Information Needed]
|
| 149 |
-
- **Cloud Provider:** [More Information Needed]
|
| 150 |
-
- **Compute Region:** [More Information Needed]
|
| 151 |
-
- **Carbon Emitted:** [More Information Needed]
|
| 152 |
-
|
| 153 |
-
## Technical Specifications [optional]
|
| 154 |
-
|
| 155 |
-
### Model Architecture and Objective
|
| 156 |
-
|
| 157 |
-
[More Information Needed]
|
| 158 |
-
|
| 159 |
-
### Compute Infrastructure
|
| 160 |
-
|
| 161 |
-
[More Information Needed]
|
| 162 |
-
|
| 163 |
-
#### Hardware
|
| 164 |
-
|
| 165 |
-
[More Information Needed]
|
| 166 |
-
|
| 167 |
-
#### Software
|
| 168 |
-
|
| 169 |
-
[More Information Needed]
|
| 170 |
-
|
| 171 |
-
## Citation [optional]
|
| 172 |
-
|
| 173 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 174 |
-
|
| 175 |
-
**BibTeX:**
|
| 176 |
-
|
| 177 |
-
[More Information Needed]
|
| 178 |
-
|
| 179 |
-
**APA:**
|
| 180 |
-
|
| 181 |
-
[More Information Needed]
|
| 182 |
-
|
| 183 |
-
## Glossary [optional]
|
| 184 |
-
|
| 185 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 186 |
-
|
| 187 |
-
[More Information Needed]
|
| 188 |
-
|
| 189 |
-
## More Information [optional]
|
| 190 |
-
|
| 191 |
-
[More Information Needed]
|
| 192 |
-
|
| 193 |
-
## Model Card Authors [optional]
|
| 194 |
-
|
| 195 |
-
[More Information Needed]
|
| 196 |
-
|
| 197 |
-
## Model Card Contact
|
| 198 |
-
|
| 199 |
-
[More Information Needed]
|
|
|
|
| 1 |
---
|
| 2 |
+
base_model: google/gemma-3-12b-it
|
| 3 |
+
tags:
|
| 4 |
+
- transformers
|
| 5 |
+
- torchao
|
| 6 |
+
- gemma3
|
| 7 |
+
license: apache-2.0
|
| 8 |
+
language:
|
| 9 |
+
- en
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# QAT INT4 google/gemma-3-12b-it model
|
| 13 |
+
|
| 14 |
+
- **Developed by:** pytorch
|
| 15 |
+
- **License:** apache-2.0
|
| 16 |
+
- **Quantized from Model :** google/gemma-3-12b-it
|
| 17 |
+
- **Quantization Method :** QAT INT4
|
| 18 |
+
- **Terms of Use**: [Terms][terms]
|
| 19 |
+
|
| 20 |
+
[gemma-3-12b-it](https://huggingface.co/google/gemma-3-12b-it) fine-tuned with [unsloth](https://github.com/unslothai/unsloth) using quantization-aware training (QAT) from [torchao](https://huggingface.co/docs/transformers/main/en/quantization/torchao), and quantized with int4 weight only quantization, by PyTorch team.
|
| 21 |
+
Use it directly or serve using [vLLM](https://docs.vllm.ai/en/latest/) for 66% VRAM reduction (8.34 GB needed) and 1.73x speedup on H100 GPUs.
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# Inference with vLLM
|
| 25 |
+
Install vllm nightly and torchao nightly to get some recent changes:
|
| 26 |
+
```
|
| 27 |
+
pip install vllm --pre --extra-index-url https://wheels.vllm.ai/nightly
|
| 28 |
+
pip install torchao
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
## Serving
|
| 32 |
+
Then we can serve with the following command:
|
| 33 |
+
```Shell
|
| 34 |
+
# Server
|
| 35 |
+
export MODEL=pytorch/gemma-3-12b-it-QAT-INT4
|
| 36 |
+
VLLM_DISABLE_COMPILE_CACHE=1 vllm serve $MODEL --tokenizer $MODEL -O3
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
```Shell
|
| 40 |
+
# Client
|
| 41 |
+
curl http://localhost:8000/v1/chat/completions -H "Content-Type: application/json" -d '{
|
| 42 |
+
"model": "pytorch/gemma-3-12b-it-QAT-INT4",
|
| 43 |
+
"messages": [
|
| 44 |
+
{"role": "user", "content": "Give me a short introduction to large language models."}
|
| 45 |
+
],
|
| 46 |
+
"temperature": 0.6,
|
| 47 |
+
"top_p": 0.95,
|
| 48 |
+
"top_k": 20,
|
| 49 |
+
"max_tokens": 32768
|
| 50 |
+
}'
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
Note: please use `VLLM_DISABLE_COMPILE_CACHE=1` to disable compile cache when running this code, e.g. `VLLM_DISABLE_COMPILE_CACHE=1 python example.py`, since there are some issues with the composability of compile in vLLM and torchao,
|
| 54 |
+
this is expected be resolved in pytorch 2.8.
|
| 55 |
+
|
| 56 |
+
# Inference with Transformers
|
| 57 |
+
|
| 58 |
+
Install the required packages:
|
| 59 |
+
```Shell
|
| 60 |
+
pip install git+https://github.com/huggingface/transformers@main
|
| 61 |
+
pip install torchao
|
| 62 |
+
pip install torch
|
| 63 |
+
pip install accelerate
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
Example:
|
| 67 |
+
```Py
|
| 68 |
+
import torch
|
| 69 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 70 |
+
|
| 71 |
+
model_name = "pytorch/gemma-3-12b-it-QAT-INT4"
|
| 72 |
+
|
| 73 |
+
# load the tokenizer and the model
|
| 74 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 75 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 76 |
+
model_name,
|
| 77 |
+
torch_dtype="auto",
|
| 78 |
+
device_map="auto"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# prepare the model input
|
| 82 |
+
prompt = "Give me a short introduction to large language model."
|
| 83 |
+
messages = [
|
| 84 |
+
{"role": "user", "content": prompt}
|
| 85 |
+
]
|
| 86 |
+
text = tokenizer.apply_chat_template(
|
| 87 |
+
messages,
|
| 88 |
+
tokenize=False,
|
| 89 |
+
add_generation_prompt=True,
|
| 90 |
+
enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
|
| 91 |
+
)
|
| 92 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 93 |
+
|
| 94 |
+
# conduct text completion
|
| 95 |
+
generated_ids = model.generate(
|
| 96 |
+
**model_inputs,
|
| 97 |
+
max_new_tokens=32768
|
| 98 |
+
)
|
| 99 |
+
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
|
| 100 |
+
|
| 101 |
+
# parsing thinking content
|
| 102 |
+
try:
|
| 103 |
+
# rindex finding 151668 (</think>)
|
| 104 |
+
index = len(output_ids) - output_ids[::-1].index(151668)
|
| 105 |
+
except ValueError:
|
| 106 |
+
index = 0
|
| 107 |
+
|
| 108 |
+
thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
|
| 109 |
+
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
|
| 110 |
+
|
| 111 |
+
print("thinking content:", thinking_content)
|
| 112 |
+
print("content:", content)
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
# Fine-tuning Recipe
|
| 117 |
+
|
| 118 |
+
Install the required packages:
|
| 119 |
+
```Shell
|
| 120 |
+
pip install torch
|
| 121 |
+
pip install git+https://github.com/huggingface/transformers@main
|
| 122 |
+
pip install --pre torchao --index-url https://download.pytorch.org/whl/nightly/cu128
|
| 123 |
+
pip install unsloth
|
| 124 |
+
pip install accelerate
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
Use the following code to fine-tune the model
|
| 128 |
+
```Py
|
| 129 |
+
# Modeled after https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Qwen3_(14B)-Reasoning-Conversational.ipynb
|
| 130 |
+
|
| 131 |
+
from unsloth import FastModel
|
| 132 |
+
from unsloth.chat_templates import (
|
| 133 |
+
get_chat_template,
|
| 134 |
+
standardize_data_formats,
|
| 135 |
+
standardize_sharegpt,
|
| 136 |
+
train_on_responses_only,
|
| 137 |
+
)
|
| 138 |
+
from datasets import load_dataset
|
| 139 |
+
from trl import SFTConfig, SFTTrainer
|
| 140 |
+
import torch
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
max_seq_length = 2048
|
| 144 |
+
dtype = torch.bfloat16
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
# ==============
|
| 148 |
+
# Model setup |
|
| 149 |
+
# ==============
|
| 150 |
+
|
| 151 |
+
model, tokenizer = FastModel.from_pretrained(
|
| 152 |
+
model_name = "unsloth/gemma-3-12b-it",
|
| 153 |
+
max_seq_length = max_seq_length,
|
| 154 |
+
dtype = dtype,
|
| 155 |
+
load_in_4bit = False,
|
| 156 |
+
full_finetuning = False,
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
model = FastModel.get_peft_model(
|
| 160 |
+
model,
|
| 161 |
+
finetune_vision_layers = False,
|
| 162 |
+
r = 8,
|
| 163 |
+
lora_alpha = 8,
|
| 164 |
+
lora_dropout = 0,
|
| 165 |
+
qat_scheme = "int4",
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
tokenizer = get_chat_template(tokenizer, chat_template="gemma3")
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
# =============
|
| 172 |
+
# Data setup |
|
| 173 |
+
# =============
|
| 174 |
+
|
| 175 |
+
def format_into_conversation(example):
|
| 176 |
+
choices = ["A", "B", "C", "D"]
|
| 177 |
+
correct_choice = choices[example["answer"]]
|
| 178 |
+
question = "Choose the correct answer for the following question: "
|
| 179 |
+
question += f"{example['question']}\n\n"
|
| 180 |
+
question += "Choices:\n"
|
| 181 |
+
question += f"A. {example['choices'][0]}\n"
|
| 182 |
+
question += f"B. {example['choices'][1]}\n"
|
| 183 |
+
question += f"C. {example['choices'][2]}\n"
|
| 184 |
+
question += f"D. {example['choices'][3]}"
|
| 185 |
+
answer = f"The correct answer is {correct_choice}."
|
| 186 |
+
return {"conversations": [
|
| 187 |
+
{"from": "human", "value": question},
|
| 188 |
+
{"from": "gpt", "value": answer},
|
| 189 |
+
]}
|
| 190 |
+
|
| 191 |
+
def formatting_prompts_func(examples):
|
| 192 |
+
convos = examples["conversations"]
|
| 193 |
+
texts = [tokenizer.apply_chat_template(convo, tokenize = False, add_generation_prompt = False).removeprefix('<bos>') for convo in convos]
|
| 194 |
+
return { "text" : texts, }
|
| 195 |
+
|
| 196 |
+
dataset = load_dataset("cais/mmlu", "all", split="auxiliary_train")
|
| 197 |
+
dataset = dataset.map(format_into_conversation)
|
| 198 |
+
dataset = dataset.remove_columns(["question", "subject", "choices", "answer"])
|
| 199 |
+
dataset = standardize_data_formats(dataset)
|
| 200 |
+
dataset = dataset.map(formatting_prompts_func, batched = True,)
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
# ========
|
| 204 |
+
# Train |
|
| 205 |
+
# ========
|
| 206 |
+
|
| 207 |
+
trainer = SFTTrainer(
|
| 208 |
+
model = model,
|
| 209 |
+
tokenizer = tokenizer,
|
| 210 |
+
train_dataset = dataset,
|
| 211 |
+
dataset_text_field = "text",
|
| 212 |
+
max_seq_length = max_seq_length,
|
| 213 |
+
packing = False,
|
| 214 |
+
args = SFTConfig(
|
| 215 |
+
per_device_train_batch_size = 32,
|
| 216 |
+
gradient_accumulation_steps = 1,
|
| 217 |
+
warmup_steps = 5,
|
| 218 |
+
num_train_epochs = 1,
|
| 219 |
+
max_steps = 100,
|
| 220 |
+
learning_rate = 2e-5,
|
| 221 |
+
logging_steps = 1,
|
| 222 |
+
optim = "adamw_8bit",
|
| 223 |
+
weight_decay = 0.01,
|
| 224 |
+
lr_scheduler_type = "linear",
|
| 225 |
+
seed = 3407,
|
| 226 |
+
output_dir = "outputs",
|
| 227 |
+
report_to = "none",
|
| 228 |
+
),
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
trainer = train_on_responses_only(
|
| 232 |
+
trainer,
|
| 233 |
+
instruction_part = "<start_of_turn>user\n",
|
| 234 |
+
response_part = "<start_of_turn>model\n",
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
trainer_stats = trainer.train()
|
| 238 |
+
```
|
| 239 |
+
|
| 240 |
+
# Model Quality
|
| 241 |
+
We rely on [lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness) to evaluate the quality of the quantized model. Here we only run on mmlu for sanity check.
|
| 242 |
+
|
| 243 |
+
| Benchmark | | |
|
| 244 |
+
|----------------------------------|----------------|---------------------------------|
|
| 245 |
+
| | mmlu accuracy | Normalized accuracy degradation |
|
| 246 |
+
| **google/gemma-3-12b-it** | | |
|
| 247 |
+
| bf16 | 71.51 | -0% |
|
| 248 |
+
| int4 | 69.48 | -100% |
|
| 249 |
+
| **Fine-tuned without QAT** | | |
|
| 250 |
+
| bf16 | 71.55 | +2% |
|
| 251 |
+
| int4 | 69.58 | -95% |
|
| 252 |
+
| **Fine-tuned with QAT** | | |
|
| 253 |
+
| int4 | 70.18 | -65.5% |
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
<details>
|
| 257 |
+
<summary> Reproduce Model Quality Results </summary>
|
| 258 |
+
|
| 259 |
+
## language eval
|
| 260 |
+
Need to install lm-eval from source:
|
| 261 |
+
https://github.com/EleutherAI/lm-evaluation-harness#install
|
| 262 |
+
|
| 263 |
+
```Shell
|
| 264 |
+
export MODEL=google/gemma-3-12b-it # or pytorch/gemma-3-12b-it-QAT-INT4
|
| 265 |
+
lm_eval --model hf --model_args pretrained=$MODEL --tasks mmlu --device cuda:0 --batch_size 8
|
| 266 |
+
```
|
| 267 |
+
|
| 268 |
+
## multi-modal eval
|
| 269 |
+
Need to install lmms-eval from source:
|
| 270 |
+
`pip install git+https://github.com/EvolvingLMMs-Lab/lmms-eval.git`
|
| 271 |
+
|
| 272 |
+
```Shell
|
| 273 |
+
NUM_PROCESSES=8
|
| 274 |
+
MAIN_PORT=12345
|
| 275 |
+
MODEL_ID=google/gemma-3-12b-it # or pytorch/gemma-3-12b-it-QAT-INT4
|
| 276 |
+
TASKS=chartqa # or tasks from https://github.com/EvolvingLMMs-Lab/lmms-eval/tree/main/lmms_eval/models/simple
|
| 277 |
+
BATCH_SIZE=32
|
| 278 |
+
OUTPUT_PATH=./logs/
|
| 279 |
+
|
| 280 |
+
accelerate launch --num_processes "${NUM_PROCESSES}" --main_process_port "${MAIN_PORT}" -m lmms_eval \
|
| 281 |
+
--model gemma3 \
|
| 282 |
+
--model_args "pretrained=${MODEL_ID}" \
|
| 283 |
+
--tasks "${TASKS}" \
|
| 284 |
+
--batch_size "${BATCH_SIZE}" --output_path "${OUTPUT_PATH}"
|
| 285 |
+
```
|
| 286 |
+
</details>
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
# Peak Memory Usage
|
| 290 |
+
|
| 291 |
+
## Results
|
| 292 |
+
|
| 293 |
+
| Benchmark | | |
|
| 294 |
+
|------------------|-------------------------|-------------------------------------|
|
| 295 |
+
| | google/gemma-3-12b-it | pytorch/gemma-3-12b-it-QAT-INT4 |
|
| 296 |
+
| Peak Memory (GB) | 24.50 | 8.34 (66% reduction) |
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
<details>
|
| 301 |
+
<summary> Reproduce Peak Memory Usage Results </summary>
|
| 302 |
+
|
| 303 |
+
We can use the following code to get a sense of peak memory usage during inference:
|
| 304 |
+
|
| 305 |
+
```Py
|
| 306 |
+
import torch
|
| 307 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig
|
| 308 |
+
|
| 309 |
+
# use "google/gemma-3-12b-it" or "pytorch/gemma-3-12b-it-QAT-INT4"
|
| 310 |
+
model_id = "pytorch/gemma-3-12b-it-QAT-INT4"
|
| 311 |
+
quantized_model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype=torch.bfloat16)
|
| 312 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 313 |
|
| 314 |
+
torch.cuda.reset_peak_memory_stats()
|
| 315 |
|
| 316 |
+
prompt = "Hey, are you conscious? Can you talk to me?"
|
| 317 |
+
messages = [
|
| 318 |
+
{
|
| 319 |
+
"role": "system",
|
| 320 |
+
"content": "",
|
| 321 |
+
},
|
| 322 |
+
{"role": "user", "content": prompt},
|
| 323 |
+
]
|
| 324 |
+
templated_prompt = tokenizer.apply_chat_template(
|
| 325 |
+
messages,
|
| 326 |
+
tokenize=False,
|
| 327 |
+
add_generation_prompt=True,
|
| 328 |
+
)
|
| 329 |
+
print("Prompt:", prompt)
|
| 330 |
+
print("Templated prompt:", templated_prompt)
|
| 331 |
+
inputs = tokenizer(
|
| 332 |
+
templated_prompt,
|
| 333 |
+
return_tensors="pt",
|
| 334 |
+
).to("cuda")
|
| 335 |
+
generated_ids = quantized_model.generate(**inputs, max_new_tokens=128)
|
| 336 |
+
output_text = tokenizer.batch_decode(
|
| 337 |
+
generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 338 |
+
)
|
| 339 |
+
print("Response:", output_text[0][len(prompt):])
|
| 340 |
|
| 341 |
+
mem = torch.cuda.max_memory_reserved() / 1e9
|
| 342 |
+
print(f"Peak Memory Usage: {mem:.02f} GB")
|
| 343 |
+
```
|
| 344 |
|
| 345 |
+
</details>
|
| 346 |
|
|
|
|
| 347 |
|
|
|
|
| 348 |
|
| 349 |
+
# Model Performance
|
| 350 |
|
| 351 |
+
## Results (H100 machine)
|
| 352 |
+
| Benchmark (Latency) | | |
|
| 353 |
+
|----------------------------------|-------------------------|------------------------------------|
|
| 354 |
+
| | google/gemma-3-12b-it | pytorch/gemma-3-12b-it-QAT-INT4 |
|
| 355 |
+
| latency (batch_size=1) | 3.73s | 2.16s (1.73x speedup) |
|
|
|
|
|
|
|
| 356 |
|
| 357 |
+
<details>
|
| 358 |
+
<summary> Reproduce Model Performance Results </summary>
|
| 359 |
|
| 360 |
+
## Setup
|
| 361 |
|
| 362 |
+
Get vllm source code:
|
| 363 |
+
```Shell
|
| 364 |
+
git clone [email protected]:vllm-project/vllm.git
|
| 365 |
+
```
|
| 366 |
|
| 367 |
+
Install vllm
|
| 368 |
+
```
|
| 369 |
+
VLLM_USE_PRECOMPILED=1 pip install --editable .
|
| 370 |
+
```
|
| 371 |
|
| 372 |
+
Run the benchmarks under `vllm` root folder:
|
| 373 |
|
| 374 |
+
## benchmark_latency
|
| 375 |
|
| 376 |
+
### baseline
|
| 377 |
+
```Shell
|
| 378 |
+
vllm bench latency --input-len 256 --output-len 256 --model google/gemma-3-12b-it --batch-size 1
|
| 379 |
+
```
|
| 380 |
|
| 381 |
+
### INT4
|
| 382 |
+
```Shell
|
| 383 |
+
VLLM_DISABLE_COMPILE_CACHE=1 vllm bench latency --input-len 256 --output-len 256 --model pytorch/gemma-3-12b-it-QAT-INT4 --batch-size 1
|
| 384 |
+
```
|
| 385 |
+
</details>
|
| 386 |
|
|
|
|
| 387 |
|
|
|
|
| 388 |
|
|
|
|
| 389 |
|
| 390 |
+
# Paper: TorchAO: PyTorch-Native Training-to-Serving Model Optimization
|
| 391 |
+
The model's quantization is powered by **TorchAO**, a framework presented in the paper [TorchAO: PyTorch-Native Training-to-Serving Model Optimization](https://huggingface.co/papers/2507.16099).
|
| 392 |
|
| 393 |
+
**Abstract:** We present TorchAO, a PyTorch-native model optimization framework leveraging quantization and sparsity to provide an end-to-end, training-to-serving workflow for AI models. TorchAO supports a variety of popular model optimization techniques, including FP8 quantized training, quantization-aware training (QAT), post-training quantization (PTQ), and 2:4 sparsity, and leverages a novel tensor subclass abstraction to represent a variety of widely-used, backend agnostic low precision data types, including INT4, INT8, FP8, MXFP4, MXFP6, and MXFP8. TorchAO integrates closely with the broader ecosystem at each step of the model optimization pipeline, from pre-training (TorchTitan) to fine-tuning (TorchTune, Axolotl) to serving (HuggingFace, vLLM, SGLang, ExecuTorch), connecting an otherwise fragmented space in a single, unified workflow. TorchAO has enabled recent launches of the quantized Llama 3.2 1B/3B and LlamaGuard3-8B models and is open-source at this https URL .
|
| 394 |
|
| 395 |
+
# Resources
|
| 396 |
+
* **Official TorchAO GitHub Repository:** [https://github.com/pytorch/ao](https://github.com/pytorch/ao)
|
| 397 |
+
* **TorchAO Documentation:** [https://docs.pytorch.org/ao/stable/index.html](https://docs.pytorch.org/ao/stable/index.html)
|
| 398 |
|
|
|
|
| 399 |
|
| 400 |
+
# Disclaimer
|
| 401 |
+
PyTorch has not performed safety evaluations or red teamed the quantized models. Performance characteristics, outputs, and behaviors may differ from the original models. Users are solely responsible for selecting appropriate use cases, evaluating and mitigating for accuracy, safety, and fairness, ensuring security, and complying with all applicable laws and regulations.
|
| 402 |
|
| 403 |
+
Nothing contained in this Model Card should be interpreted as or deemed a restriction or modification to the licenses the models are released under, including any limitations of liability or disclaimers of warranties provided therein.
|
| 404 |
|
|
|
|
| 405 |
|
| 406 |
+
[terms]: https://ai.google.dev/gemma/terms
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|