Views
No views yet

.pte file successfully. See https://github.com/pytorch/executorch/issues/14077 for details and tracking.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)
6import torch
7
8model_id = "microsoft/Phi-4-mini-instruct"
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
27untied_model.push_to_hub(save_to)
28tokenizer.push_to_hub(save_to)
29
30# or save locally
31save_to_local_path = f"{MODEL_NAME}-untied-weights"
32untied_model.save_pretrained(save_to_local_path)
33tokenizer.save_pretrained(save_to)push_to_hub you need to run1pip install -U "huggingface_hub[cli]"
2huggingface-cli login1from transformers import (
2 AutoModelForCausalLM,
3 AutoProcessor,
4 AutoTokenizer,
5 TorchAoConfig,
6)
7from torchao.quantization.quant_api import (
8 IntxWeightOnlyConfig,
9 Int8DynamicActivationIntxWeightConfig,
10 ModuleFqnToConfig,
11 quantize_,
12)
13from torchao.quantization.granularity import PerGroup, PerAxis
14import torch
15
16# we start from the model with untied weights
17model_id = "microsoft/Phi-4-mini-instruct"
18USER_ID = "YOUR_USER_ID"
19MODEL_NAME = model_id.split("/")[-1]
20untied_model_id = f"{USER_ID}/{MODEL_NAME}-untied-weights"
21untied_model_local_path = f"{MODEL_NAME}-untied-weights"
22
23embedding_config = IntxWeightOnlyConfig(
24 weight_dtype=torch.int8,
25 granularity=PerAxis(0),
26)
27linear_config = Int8DynamicActivationIntxWeightConfig(
28 weight_dtype=torch.int4,
29 weight_granularity=PerGroup(32),
30 weight_scale_dtype=torch.bfloat16,
31)
32quant_config = ModuleFqnToConfig({"_default": linear_config, "model.embed_tokens": embedding_config})
33quantization_config = TorchAoConfig(quant_type=quant_config, include_input_output_embeddings=True, modules_to_not_convert=[])
34
35# either use `untied_model_id` or `untied_model_local_path`
36quantized_model = AutoModelForCausalLM.from_pretrained(untied_model_id, device_map="auto", torch_dtype=torch.bfloat16, quantization_config=quantization_config)
37tokenizer = AutoTokenizer.from_pretrained(model_id)
38
39# Push to hub
40MODEL_NAME = model_id.split("/")[-1]
41save_to = f"{USER_ID}/{MODEL_NAME}-INT8-INT4"
42quantized_model.push_to_hub(save_to, safe_serialization=False)
43tokenizer.push_to_hub(save_to)
44
45# Manual testing
46prompt = "Hey, are you conscious? Can you talk to me?"
47messages = [
48 {
49 "role": "system",
50 "content": "",
51 },
52 {"role": "user", "content": prompt},
53]
54templated_prompt = tokenizer.apply_chat_template(
55 messages,
56 tokenize=False,
57 add_generation_prompt=True,
58)
59print("Prompt:", prompt)
60print("Templated prompt:", templated_prompt)
61inputs = tokenizer(
62 templated_prompt,
63 return_tensors="pt",
64).to("cuda")
65generated_ids = quantized_model.generate(**inputs, max_new_tokens=128)
66output_text = tokenizer.batch_decode(
67 generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
68)
69print("Response:", output_text[0][len(prompt):])Hello! As an AI, I don't have consciousness in the way humans do, but I am fully operational and here to assist you. How can I help you today?| Benchmark | ||
|---|---|---|
| Phi-4-mini-ins | Phi-4-mini-instruct-INT8-INT4 | |
| Popular aggregated benchmark | ||
| mmlu (0 shot) | 66.73 | 60.75 |
| mmlu_pro (5-shot) | 46.43 | 11.75 |
| Reasoning | ||
| arc_challenge | 56.91 | 48.46 |
| gpqa_main_zeroshot | 30.13 | 30.80 |
| hellaswag | 54.57 | 50.35 |
| openbookqa | 33.00 | 30.40 |
| piqa (0-shot) | 77.64 | 74.43 |
| siqa | 49.59 | 44.98 |
| truthfulqa_mc2 (0-shot) | 48.39 | 51.35 |
| winogrande (0-shot) | 71.11 | 70.32 |
| Multilingual | ||
| mgsm_en_cot_en | 60.80 | 57.60 |
| Math | ||
| gsm8k (5-shot) | 81.88 | 61.71 |
| Mathqa (0-shot) | 42.31 | 36.95 |
| Overall | 55.35 | 48.45 |
lm_eval --model hf --model_args pretrained=microsoft/Phi-4-mini-instruct --tasks hellaswag --device cuda:0 --batch_size 8lm_eval --model hf --model_args pretrained=pytorch/Phi-4-mini-instruct-INT8-INT4 --tasks hellaswag --device cuda:0 --batch_size 8python -m executorch.examples.models.phi_4_mini.convert_weights $(hf download pytorch/Phi-4-mini-instruct-INT8-INT4) pytorch_model_converted.bin1python-m executorch.examples.models.llama.export_llama \
2 --model "phi_4_mini" \
3 --checkpoint pytorch_model_converted.bin \
4 --params examples/models/phi_4_mini/config/config.json \
5 --output_name model.pte \
6 -kv \
7 --use_sdpa_with_kv_cache \
8 -X \
9 --xnnpack-extended-ops \
10 --max_context_length 1024 \
11 --max_seq_length 1024 \
12 --dtype fp32 \
13 --metadata '{"get_bos_id":199999, "get_eos_ids":[200020,199999]}'