Views
No views yet
1pip install git+https://github.com/huggingface/transformers@main
2pip install --pre torchao --index-url https://download.pytorch.org/whl/nightly/cu126
3pip install torch
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-8B-Base"
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 weight_scale_dtype=torch.bfloat16,
22)
23quant_config = ModuleFqnToConfig({{"_default": linear_config, "model.embed_tokens": embedding_config}})
24quantization_config = TorchAoConfig(quant_type=quant_config, include_embedding=True, untie_embedding_weights=True, modules_to_not_convert=[])
25
26quantized_model = AutoModelForCausalLM.from_pretrained(model_to_quantize, device_map="auto", torch_dtype=torch.bfloat16, quantization_config=quantization_config)
27tokenizer = AutoTokenizer.from_pretrained(model_id)
28
29# Push to hub
30USER_ID = "YOUR_USER_ID"
31MODEL_NAME = model_id.split("/")[-1]
32save_to = f"{USER_ID}/{MODEL_NAME}-INT8-INT4"
33quantized_model.push_to_hub(save_to, safe_serialization=False)
34tokenizer.push_to_hub(save_to)
35
36# Manual Testing
37prompt = "Hey, are you conscious? Can you talk to me?"
38messages = [
39 {
40 "role": "system",
41 "content": "",
42 },
43 {"role": "user", "content": prompt},
44]
45templated_prompt = tokenizer.apply_chat_template(
46 messages,
47 tokenize=False,
48 add_generation_prompt=True,
49)
50print("Prompt:", prompt)
51print("Templated prompt:", templated_prompt)
52inputs = tokenizer(
53 templated_prompt,
54 return_tensors="pt",
55).to("cuda")
56generated_ids = quantized_model.generate(**inputs, max_new_tokens=128)
57output_text = tokenizer.batch_decode(
58 generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
59)
60print("Response:", output_text[0][len(prompt):])push_to_hub you need to run1pip install -U "huggingface_hub[cli]"
2huggingface-cli login| Benchmark | ||
|---|---|---|
| Qwen/Qwen3-8B-Base | q10/Qwen3-8B-Base-INT8-INT4 | |
| mmlu | To be filled | To be filled |
lm_eval --model hf --model_args pretrained=Qwen/Qwen3-8B-Base --tasks mmlu --device cuda:0 --batch_size 81export MODEL=q10/Qwen3-8B-Base-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 $(huggingface-cli download q10/Qwen3-8B-Base-INT8-INT4) pytorch_model_converted.bin1PARAMS="executorch/examples/models/qwen3/4b_config.json"
2python -m executorch.examples.models.llama.export_llama --model "qwen3-4b" --checkpoint "pytorch_model_converted.bin" --params "$PARAMS" -kv --use_sdpa_with_kv_cache -d fp32
3 -X --metadata '{"get_bos_id":199999, "get_eos_ids":[200020,199999]}' --max_seq_length 1024 --max_context_length 1024 --output_name="qwen3-4b-INT8-INT4-1024-cxt.pte"