jerryzh168 commited on
Commit
74373dd
·
verified ·
1 Parent(s): d76329f

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +330 -169
README.md CHANGED
@@ -1,199 +1,360 @@
1
  ---
2
- library_name: transformers
3
- tags: []
 
 
 
 
 
 
4
  ---
5
 
6
- # Model Card for Model ID
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- <!-- Provide a quick summary of what the model is/does. -->
 
 
 
9
 
 
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- ## Model Details
 
 
13
 
14
- ### Model Description
15
 
16
- <!-- Provide a longer summary of what this model is. -->
17
 
18
- This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
19
 
20
- - **Developed by:** [More Information Needed]
21
- - **Funded by [optional]:** [More Information Needed]
22
- - **Shared by [optional]:** [More Information Needed]
23
- - **Model type:** [More Information Needed]
24
- - **Language(s) (NLP):** [More Information Needed]
25
- - **License:** [More Information Needed]
26
- - **Finetuned from model [optional]:** [More Information Needed]
27
 
28
- ### Model Sources [optional]
29
 
30
- <!-- Provide the basic links for the model. -->
 
 
 
 
 
31
 
32
- - **Repository:** [More Information Needed]
33
- - **Paper [optional]:** [More Information Needed]
34
- - **Demo [optional]:** [More Information Needed]
35
 
36
- ## Uses
37
 
38
- <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
 
 
 
39
 
40
- ### Direct Use
 
 
 
41
 
42
- <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
43
 
44
- [More Information Needed]
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
- ### Out-of-Scope Use
53
 
54
- <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
55
 
56
- [More Information Needed]
 
57
 
58
- ## Bias, Risks, and Limitations
59
 
60
- <!-- This section is meant to convey both technical and sociotechnical limitations. -->
 
 
61
 
62
- [More Information Needed]
63
 
64
- ### Recommendations
 
65
 
66
- <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
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
+ # AWQ-INT4 google/gemma-3-12b-it model
13
+
14
+ - **Developed by:** jerryzh168
15
+ - **License:** apache-2.0
16
+ - **Quantized from Model :** google/gemma-3-12b-it
17
+ - **Quantization Method :** AWQ-INT4
18
+
19
+
20
+ # Inference with vLLM
21
+ Install vllm nightly and torchao nightly to get some recent changes:
22
+ ```
23
+ pip install vllm --pre --extra-index-url https://wheels.vllm.ai/nightly
24
+ pip install torchao
25
+ ```
26
+
27
+ ## Serving
28
+ Then we can serve with the following command:
29
+ ```Shell
30
+ # Server
31
+ export MODEL=jerryzh168/gemma-3-12b-it-AWQ-INT4
32
+ VLLM_DISABLE_COMPILE_CACHE=1 vllm serve $MODEL --tokenizer $MODEL -O3
33
+ ```
34
+
35
+ ```Shell
36
+ # Client
37
+ curl http://localhost:8000/v1/chat/completions -H "Content-Type: application/json" -d '{
38
+ "model": "jerryzh168/gemma-3-12b-it-AWQ-INT4",
39
+ "messages": [
40
+ {"role": "user", "content": "Give me a short introduction to large language models."}
41
+ ],
42
+ "temperature": 0.6,
43
+ "top_p": 0.95,
44
+ "top_k": 20,
45
+ "max_tokens": 32768
46
+ }'
47
+ ```
48
+
49
+ 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,
50
+ this is expected be resolved in pytorch 2.8.
51
+
52
+ # Inference with Transformers
53
+
54
+ Install the required packages:
55
+ ```Shell
56
+ pip install git+https://github.com/huggingface/transformers@main
57
+ pip install torchao
58
+ pip install torch
59
+ pip install accelerate
60
+ ```
61
+
62
+ Example:
63
+ ```Py
64
+ import torch
65
+ from transformers import AutoModelForCausalLM, AutoTokenizer
66
+
67
+ model_name = "jerryzh168/gemma-3-12b-it-AWQ-INT4"
68
+
69
+ # load the tokenizer and the model
70
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
71
+ model = AutoModelForCausalLM.from_pretrained(
72
+ model_name,
73
+ torch_dtype="auto",
74
+ device_map="cuda:0"
75
+ )
76
+
77
+ # prepare the model input
78
+ prompt = "Give me a short introduction to large language model."
79
+ messages = [
80
+ {"role": "user", "content": prompt}
81
+ ]
82
+ text = tokenizer.apply_chat_template(
83
+ messages,
84
+ tokenize=False,
85
+ add_generation_prompt=True,
86
+ enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
87
+ )
88
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
89
+
90
+ # conduct text completion
91
+ generated_ids = model.generate(
92
+ **model_inputs,
93
+ max_new_tokens=32768
94
+ )
95
+ output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
96
+
97
+ # parsing thinking content
98
+ try:
99
+ # rindex finding 151668 (</think>)
100
+ index = len(output_ids) - output_ids[::-1].index(151668)
101
+ except ValueError:
102
+ index = 0
103
+
104
+ thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("
105
+ ")
106
+ content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("
107
+ ")
108
+
109
+ print("thinking content:", thinking_content)
110
+ print("content:", content)
111
+ ```
112
+
113
+
114
+
115
+
116
+ # Quantization 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/cu126
123
+ pip install accelerate
124
+ ```
125
+
126
+
127
+
128
+ Use the following code to get the quantized model:
129
+ ```Py
130
+ import torch
131
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig
132
+
133
+ model_id = "google/gemma-3-12b-it"
134
+ model_to_quantize = "google/gemma-3-12b-it"
135
+
136
+
137
+ from torchao.quantization import Int4WeightOnlyConfig, quantize_
138
+ from torchao.prototype.awq import (
139
+ AWQConfig,
140
+ )
141
+ from torchao._models._eval import TransformerEvalWrapper
142
+ model = AutoModelForCausalLM.from_pretrained(
143
+ model_to_quantize,
144
+ device_map="cuda:0",
145
+ torch_dtype=torch.bfloat16,
146
+ )
147
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
148
+
149
+ base_config = Int4WeightOnlyConfig(group_size=128, int4_packing_format="tile_packed_to_4d", int4_choose_qparams_algorithm="hqq")
150
+ quant_config = AWQConfig(base_config, step="prepare")
151
+ quantize_(
152
+ model,
153
+ quant_config,
154
+ )
155
+ TransformerEvalWrapper(
156
+ model=model,
157
+ tokenizer=tokenizer,
158
+ max_seq_length=max_seq_length,
159
+ ).run_eval(
160
+ tasks=tasks,
161
+ limit=calibration_limit,
162
+ )
163
+ quant_config = AWQConfig(base_config, step="convert")
164
+ quantize_(model, quant_config)
165
+
166
+ quantized_model = model
167
+ quant_config = AWQConfig(base_config, step="prepare_for_loading")
168
+ quantized_model.config.quantization_config = TorchAoConfig(quant_config)
169
+
170
+
171
+ # Push to hub
172
+ USER_ID = "YOUR_USER_ID"
173
+ MODEL_NAME = model_id.split("/")[-1]
174
+ save_to = f"{USER_ID}/{MODEL_NAME}-AWQ-INT4"
175
+ quantized_model.push_to_hub(save_to, safe_serialization=False)
176
+ tokenizer.push_to_hub(save_to)
177
+
178
+ # Manual Testing
179
+ prompt = "Hey, are you conscious? Can you talk to me?"
180
+ messages = [
181
+ {
182
+ "role": "system",
183
+ "content": "",
184
+ },
185
+ {"role": "user", "content": prompt},
186
+ ]
187
+ templated_prompt = tokenizer.apply_chat_template(
188
+ messages,
189
+ tokenize=False,
190
+ add_generation_prompt=True,
191
+ )
192
+ print("Prompt:", prompt)
193
+ print("Templated prompt:", templated_prompt)
194
+ inputs = tokenizer(
195
+ templated_prompt,
196
+ return_tensors="pt",
197
+ ).to("cuda")
198
+ generated_ids = quantized_model.generate(**inputs, max_new_tokens=128)
199
+ output_text = tokenizer.batch_decode(
200
+ generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
201
+ )
202
+ print("Response:", output_text[0][len(prompt):])
203
+ ```
204
+
205
+ Note: to `push_to_hub` you need to run
206
+ ```Shell
207
+ pip install -U "huggingface_hub[cli]"
208
+ huggingface-cli login
209
+ ```
210
+ and use a token with write access, from https://huggingface.co/settings/tokens
211
+
212
+ # Model Quality
213
+ 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.
214
+
215
+ | Benchmark | | |
216
+ |----------------------------------|----------------|---------------------------|
217
+ | | google/gemma-3-12b-it | jerryzh168/gemma-3-12b-it-AWQ-INT4 |
218
+ | mmlu | To be filled | To be filled |
219
+
220
+
221
+ <details>
222
+ <summary> Reproduce Model Quality Results </summary>
223
+
224
+ Need to install lm-eval from source:
225
+ https://github.com/EleutherAI/lm-evaluation-harness#install
226
+
227
+ ## baseline
228
+ ```Shell
229
+ lm_eval --model hf --model_args pretrained=google/gemma-3-12b-it --tasks mmlu --device cuda:0 --batch_size 8
230
+ ```
231
+
232
+ ## AWQ-INT4
233
+ ```Shell
234
+ export MODEL=jerryzh168/gemma-3-12b-it-AWQ-INT4
235
+ lm_eval --model hf --model_args pretrained=$MODEL --tasks mmlu --device cuda:0 --batch_size 8
236
+ ```
237
+ </details>
238
+
239
+
240
+
241
+
242
+ # Peak Memory Usage
243
+
244
+ ## Results
245
+
246
+ | Benchmark | | |
247
+ |------------------|----------------|--------------------------------|
248
+ | | google/gemma-3-12b-it | jerryzh168/gemma-3-12b-it-AWQ-INT4 |
249
+ | Peak Memory (GB) | To be filled | To be filled (?% reduction) |
250
+
251
+
252
+
253
+ <details>
254
+ <summary> Reproduce Peak Memory Usage Results </summary>
255
+
256
+ We can use the following code to get a sense of peak memory usage during inference:
257
+
258
+ ```Py
259
+ import torch
260
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig
261
 
262
+ # use "google/gemma-3-12b-it" or "jerryzh168/gemma-3-12b-it-AWQ-INT4"
263
+ model_id = "jerryzh168/gemma-3-12b-it-AWQ-INT4"
264
+ quantized_model = AutoModelForCausalLM.from_pretrained(model_id, device_map="cuda:0", torch_dtype=torch.bfloat16)
265
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
266
 
267
+ torch.cuda.reset_peak_memory_stats()
268
 
269
+ prompt = "Hey, are you conscious? Can you talk to me?"
270
+ messages = [
271
+ {
272
+ "role": "system",
273
+ "content": "",
274
+ },
275
+ {"role": "user", "content": prompt},
276
+ ]
277
+ templated_prompt = tokenizer.apply_chat_template(
278
+ messages,
279
+ tokenize=False,
280
+ add_generation_prompt=True,
281
+ )
282
+ print("Prompt:", prompt)
283
+ print("Templated prompt:", templated_prompt)
284
+ inputs = tokenizer(
285
+ templated_prompt,
286
+ return_tensors="pt",
287
+ ).to("cuda")
288
+ generated_ids = quantized_model.generate(**inputs, max_new_tokens=128)
289
+ output_text = tokenizer.batch_decode(
290
+ generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
291
+ )
292
+ print("Response:", output_text[0][len(prompt):])
293
 
294
+ mem = torch.cuda.max_memory_reserved() / 1e9
295
+ print(f"Peak Memory Usage: {mem:.02f} GB")
296
+ ```
297
 
298
+ </details>
299
 
 
300
 
 
301
 
 
 
 
 
 
 
 
302
 
303
+ # Model Performance
304
 
305
+ ## Results (A100 machine)
306
+ | Benchmark (Latency) | | |
307
+ |----------------------------------|----------------|--------------------------|
308
+ | | google/gemma-3-12b-it | jerryzh168/gemma-3-12b-it-AWQ-INT4 |
309
+ | latency (batch_size=1) | ?s | ?s (?x speedup) |
310
+ | latency (batch_size=256) | ?s | ?s (?x speedup) |
311
 
312
+ <details>
313
+ <summary> Reproduce Model Performance Results </summary>
 
314
 
315
+ ## Setup
316
 
317
+ Get vllm source code:
318
+ ```Shell
319
+ git clone [email protected]:vllm-project/vllm.git
320
+ ```
321
 
322
+ Install vllm
323
+ ```
324
+ VLLM_USE_PRECOMPILED=1 pip install --editable .
325
+ ```
326
 
327
+ Run the benchmarks under `vllm` root folder:
328
 
329
+ ## benchmark_latency
330
 
331
+ ### baseline
332
+ ```Shell
333
+ export MODEL=google/gemma-3-12b-it
334
+ python benchmarks/benchmark_latency.py --input-len 256 --output-len 256 --model $MODEL --batch-size 1
335
+ ```
336
 
337
+ ### AWQ-INT4
338
+ ```Shell
339
+ export MODEL=jerryzh168/gemma-3-12b-it-AWQ-INT4
340
+ VLLM_DISABLE_COMPILE_CACHE=1 python benchmarks/benchmark_latency.py --input-len 256 --output-len 256 --model $MODEL --batch-size 1
341
+ ```
342
+ </details>
343
 
 
344
 
 
345
 
 
346
 
347
+ # Paper: TorchAO: PyTorch-Native Training-to-Serving Model Optimization
348
+ 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).
349
 
350
+ **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 .
351
 
352
+ # Resources
353
+ * **Official TorchAO GitHub Repository:** [https://github.com/pytorch/ao](https://github.com/pytorch/ao)
354
+ * **TorchAO Documentation:** [https://docs.pytorch.org/ao/stable/index.html](https://docs.pytorch.org/ao/stable/index.html)
355
 
 
356
 
357
+ # Disclaimer
358
+ 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.
359
 
360
+ 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.