Views
No views yet

1pip install git+https://github.com/huggingface/transformers@main
2pip install --pre torchao torch --index-url https://download.pytorch.org/whl/nightly/cu1261from transformers import (
2 AutoModelForCausalLM,
3 AutoProcessor,
4 AutoTokenizer,
5 TorchAoConfig,
6)
7
8model_id = "google/gemma-3-4b-it"
9MODEL_NAME = model_id.split("/")[-1]
10save_to_local_path = f"{MODEL_NAME}-untied-weights"
11
12untied_model = AutoModelForCausalLM.from_pretrained(
13 model_id, torch_dtype="auto", device_map="cuda:0"
14)
15tokenizer = AutoTokenizer.from_pretrained(model_id)
16processor = AutoProcessor.from_pretrained(model_id)
17
18from transformers.modeling_utils import find_tied_parameters
19
20if getattr(
21 untied_model.config.get_text_config(decoder=True), "tie_word_embeddings"
22):
23 setattr(
24 untied_model.config.get_text_config(decoder=True),
25 "tie_word_embeddings",
26 False,
27 )
28
29untied_model._tied_weights_keys = []
30untied_model.lm_head.weight = torch.nn.Parameter(
31 untied_model.lm_head.weight.clone()
32)
33
34print("tied weights:", find_tied_parameters(untied_model))
35
36# save locally
37untied_model.save_pretrained(save_to_local_path)
38tokenizer.save_pretrained(save_to_local_path)
39processor.save_pretrained(save_to_local_path)1
2from torchao.quantization.quant_api import (
3 IntxWeightOnlyConfig,
4 Int8DynamicActivationIntxWeightConfig,
5 ModuleFqnToConfig,
6 quantize_,
7)
8from torchao.quantization.granularity import PerGroup, PerAxis
9import torch
10
11USER_ID = "YOUR_USER_ID"
12
13# We start from the model with untied weights
14model_to_quantize = save_to_local_path
15
16
17int8_int4_config = Int8DynamicActivationIntxWeightConfig(
18 weight_dtype=torch.int4,
19 weight_granularity=PerGroup(32),
20 intx_choose_qparams_algorithm="hqq_scale_only",
21)
22int8_int8_config = Int8DynamicActivationIntxWeightConfig(
23 weight_dtype=torch.int8,
24 weight_granularity=PerAxis(0),
25 intx_choose_qparams_algorithm="hqq_scale_only",
26)
27int8_weight_only_config = IntxWeightOnlyConfig(
28 weight_dtype=torch.int8,
29 granularity=PerAxis(0),
30 intx_choose_qparams_algorithm="hqq_scale_only",
31)
32
33fqn_to_config = {}
34fqn_to_config["_default"] = int8_int4_config
35fqn_to_config["model.language_model.embed_tokens"] = int8_weight_only_config
36fqn_to_config["model.vision_tower.vision_model.embeddings.position_embedding"] = int8_weight_only_config
37for i in range(27):
38 fqn_to_config[f"model.vision_tower.vision_model.encoder.layers.{i}.mlp.fc2"] = int8_int8_config
39quant_config = ModuleFqnToConfig(fqn_to_config)
40quantization_config = TorchAoConfig(quant_type=quant_config, include_input_output_embeddings=True, modules_to_not_convert=[])
41
42quantized_model = AutoModelForCausalLM.from_pretrained(model_to_quantize, device_map="auto", torch_dtype=torch.bfloat16, quantization_config=quantization_config)
43tokenizer = AutoTokenizer.from_pretrained(model_to_quantize)
44processor = AutoProcessor.from_pretrained(model_to_quantize)
45
46# Push to hub
47save_to = f"{USER_ID}/{MODEL_NAME}-HQQ-INT8-INT4"
48quantized_model.push_to_hub(save_to, safe_serialization=False)
49tokenizer.push_to_hub(save_to)
50processor.push_to_hub(save_to)
51
52# Manual testing
53prompt = "Hey, are you conscious? Can you talk to me?"
54messages = [
55 {
56 "role": "system",
57 "content": "",
58 },
59 {"role": "user", "content": prompt},
60]
61templated_prompt = tokenizer.apply_chat_template(
62 messages,
63 tokenize=False,
64 add_generation_prompt=True,
65)
66print("Prompt:", prompt)
67print("Templated prompt:", templated_prompt)
68inputs = tokenizer(
69 templated_prompt,
70 return_tensors="pt",
71).to("cuda")
72generated_ids = quantized_model.generate(**inputs, max_new_tokens=128)
73output_text = tokenizer.batch_decode(
74 generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
75)
76print("Response:", output_text[0][len(templated_prompt):])That's a really fascinating question! And a very common one when people interact with AI like me.
The short answer is: I can *simulate* conversation and respond to you in a way that *feels* like talking, but I'm not conscious in the way a human is.| Benchmark | ||
|---|---|---|
| gemma-3-4b-it | pytorch/gemma-3-4b-it-HQQ-INT8-INT4 | |
| Benchmark | ||
| mmlu | 57.68 | 55.65 |
| chartqa (multimodal) | 50.56 | 42.88 |
lm_eval --model hf --model_args pretrained=google/gemma-3-4b-it --tasks mmlu --device cuda:0 --batch_size autolm_eval --model hf --model_args pretrained=pytorch/gemma-3-4b-it-HQQ-INT8-INT4 --tasks mmlu --device cuda:0 --batch_size autopip install git+https://github.com/EvolvingLMMs-Lab/lmms-eval.gitlmms-eval --model gemma3 --model_args "pretrained=google/gemma-3-4b-it,trust_remote_code=True,device_map=auto" --tasks chartqa --batch_size 1# Set up executorch
git clone https://github.com/pytorch/executorch.git
pushd executorch
git submodule update --init --recursive
python install_executorch.py
popd
# Install optimum-executorch
git clone https://github.com/huggingface/optimum-executorch.git
pushd optimum-executorch
python install_dev.py --skip_override_torch
popdoptimum-cli export executorch --model "pytorch/gemma-3-4b-it-HQQ-INT8-INT4" --task "multimodal-text-to-text" --recipe "xnnpack" --use_custom_sdpa --use_custom_kv_cache --max_seq_len 1024 --output_dir ./
hf upload pytorch/gemma-3-4b-it-HQQ-INT8-INT4 model.pte