Views
No views yet
1pip install torch
2pip install git+https://github.com/huggingface/transformers@main
3pip install --pre torchao --index-url https://download.pytorch.org/whl/nightly/cu126
4pip install accelerate1from transformers import (
2 AutoModelForCausalLM,
3 AutoProcessor,
4 AutoTokenizer,
5)
6import torch
7
8model_id = "{base_model}"
9untied_model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
10tokenizer = AutoTokenizer.from_pretrained(model_id)
11
12print(untied_model)
13from transformers.modeling_utils import find_tied_parameters
14print("tied weights:", find_tied_parameters(untied_model))
15if getattr(untied_model.config.get_text_config(decoder=True), "tie_word_embeddings"):
16 setattr(untied_model.config.get_text_config(decoder=True), "tie_word_embeddings", False)
17
18untied_model._tied_weights_keys = []
19untied_model.lm_head.weight = torch.nn.Parameter(untied_model.lm_head.weight.clone())
20
21print("tied weights:", find_tied_parameters(untied_model))
22
23USER_ID = "YOUR_USER_ID"
24MODEL_NAME = model_id.split("/")[-1]
25save_to = f"{{USER_ID}}/{{MODEL_NAME}}-untied-weights"
26
27# save locally (we use this in the recipe)
28save_to_local_path = f"{{MODEL_NAME}}-untied-weights"
29untied_model.save_pretrained(save_to_local_path)
30tokenizer.save_pretrained(save_to_local_path)
31
32
33# or push to hub
34untied_model.push_to_hub(save_to)
35tokenizer.push_to_hub(save_to)push_to_hub you need to run1pip install -U "huggingface_hub[cli]"
2huggingface-cli login1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig
3
4model_id = "Qwen/Qwen3-4B"
5model_to_quantize = "f"{{MODEL_NAME}}-untied-weights""
6
7
8from torchao.quantization.quant_api import (
9 IntxWeightOnlyConfig,
10 Int8DynamicActivationIntxWeightConfig,
11 ModuleFqnToConfig,
12)
13from torchao.quantization.granularity import PerGroup, PerAxis
14embedding_config = IntxWeightOnlyConfig(
15 weight_dtype=torch.int8,
16 granularity=PerAxis(0),
17)
18linear_config = Int8DynamicActivationIntxWeightConfig(
19 weight_dtype=torch.int4,
20 weight_granularity=PerGroup(32),
21)
22quant_config = ModuleFqnToConfig({{"_default": linear_config, "model.embed_tokens": embedding_config}})
23quantization_config = TorchAoConfig(quant_type=quant_config, include_input_output_embeddings=True, modules_to_not_convert=[])
24quantized_model = AutoModelForCausalLM.from_pretrained(model_to_quantize, device_map="auto", torch_dtype=torch.bfloat16, quantization_config=quantization_config)
25tokenizer = AutoTokenizer.from_pretrained(model_id)
26
27
28# Push to hub
29USER_ID = "YOUR_USER_ID"
30MODEL_NAME = model_id.split("/")[-1]
31save_to = f"{USER_ID}/{MODEL_NAME}-INT8-INT4"
32quantized_model.push_to_hub(save_to, safe_serialization=False)
33tokenizer.push_to_hub(save_to)
34
35# Manual Testing
36prompt = "Hey, are you conscious? Can you talk to me?"
37messages = [
38 {
39 "role": "system",
40 "content": "",
41 },
42 {"role": "user", "content": prompt},
43]
44templated_prompt = tokenizer.apply_chat_template(
45 messages,
46 tokenize=False,
47 add_generation_prompt=True,
48)
49print("Prompt:", prompt)
50print("Templated prompt:", templated_prompt)
51inputs = tokenizer(
52 templated_prompt,
53 return_tensors="pt",
54).to("cuda")
55generated_ids = quantized_model.generate(**inputs, max_new_tokens=128)
56output_text = tokenizer.batch_decode(
57 generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
58)
59print("Response:", output_text[0][len(prompt):])push_to_hub you need to run1pip install -U "huggingface_hub[cli]"
2huggingface-cli login| Benchmark | ||
|---|---|---|
| Qwen/Qwen3-4B | jerryzh168/Qwen3-4B-INT8-INT4 | |
| mmlu | To be filled | To be filled |
lm_eval --model hf --model_args pretrained=Qwen/Qwen3-4B --tasks mmlu --device cuda:0 --batch_size 81export MODEL=jerryzh168/Qwen3-4B-INT8-INT4
2lm_eval --model hf --model_args pretrained=$MODEL --tasks mmlu --device cuda:0 --batch_size 8python -m executorch.examples.models.qwen3.convert_weights $(hf download jerryzh168/Qwen3-4B-INT8-INT4) pytorch_model_converted.binpython -m executorch.examples.models.llama.export_llama --model "qwen3_4b" --checkpoint pytorch_model_converted.bin --params examples/models/qwen3/config/4b_config.json --output_name model.pte -kv --use_sdpa_with_kv_cache -X --xnnpack-extended-ops --max_context_length 1024 --max_seq_length 1024 --dtype fp32 --metadata '{"get_bos_id":199999, "get_eos_ids":[200020,199999]}'