Views
No views yet
max_context_length of 1024. If you wish to change this, re-export the quantized model following the instructions in Exporting to ExecuTorch.)uv by following https://docs.astral.sh/uv/getting-started/installation1uv venv ~/.uv-hf --python 3.13
2source ~/.uv-hf/bin/activate
3uv pip install transformers==4.56.2 'trl[vllm]==0.23.1' tensorboard
4uv pip install --pre --index-url https://download.pytorch.org/whl/nightly/cu126 torchaocurl -O https://huggingface.co/datasets/pytorch/parq-sft/resolve/main/qat_sft.pydataset_name to your desired dataset from the HuggingFace datasets hub in addition to max_steps.1source ~/.uv-hf/bin/activate
2
3SEED=$RANDOM
4SAVE_DIR=checkpoints/phi-4-mini-2wei-4emb-${SEED}
5
6dataset_name=<TODO>
7max_steps=<TODO>
8ngpu=8
9device_batch_size=4
10grad_accum_steps=2
11lr=3e-5
12TOKENIZERS_PARALLELISM=$(( ngpu == 1 )) \
13 PYTORCH_ALLOC_CONF=expandable_segments:True \
14 torchrun \
15 --nproc-per-node $ngpu \
16 --rdzv-id $SEED \
17 --rdzv-backend c10d \
18 --rdzv-endpoint localhost:$(shuf -i 29000-29500 -n 1) \
19 -m qat_sft \
20 --model_name_or_path microsoft/Phi-4-mini-instruct \
21 --bf16 true \
22 --num_train_epochs 1 \
23 --per_device_train_batch_size $device_batch_size \
24 --gradient_accumulation_steps $grad_accum_steps \
25 --dataset_name $dataset_name \
26 --dataloader_num_workers 4 \
27 --max_length 4096 \
28 --max_steps $max_steps \
29 --report_to tensorboard \
30 --learning_rate $lr \
31 --lr_scheduler_type linear \
32 --warmup_ratio 0.0 \
33 --seed $SEED \
34 --output_dir $SAVE_DIR \
35 --weight_bits 2 \
36 --linear_pat 'proj\.weight$' \
37 --embed_bits 4 \
38 --embed_pat '(lm_head|embed_tokens)'--resume_from_checkpoint ${SAVE_DIR}/checkpoint-{SAVE_STEP}. The exported model will be saved to ${SAVE_DIR}/quant_converted.1import os
2
3from huggingface_hub import whoami, get_token
4from transformers import AutoModelForCausalLM, AutoTokenizer
5
6set_seed(0)
7model_path = f"{SAVE_DIR}"
8model = AutoModelForCausalLM.from_pretrained(
9 model_path, device_map="auto", dtype="auto"
10)
11tokenizer = AutoTokenizer.from_pretrained(model_path)
12
13# Manual testing
14prompt = "Hey, are you conscious? Can you talk to me?"
15messages = [{"role": "user", "content": prompt}]
16templated_prompt = tokenizer.apply_chat_template(
17 messages,
18 tokenize=False,
19 add_generation_prompt=True,
20)
21inputs = tokenizer(templated_prompt, return_tensors="pt").to(model.device)
22inputs.pop("token_type_ids", None)
23
24start_idx = len(inputs.input_ids[0])
25response_ids = model.generate(**inputs, max_new_tokens=256, **kwargs)[0]
26response_ids = response_ids[start_idx:].tolist()
27output_text = tokenizer.decode(response_ids, skip_special_tokens=True)
28print(output_text)1lm_eval \
2 --model hf \
3 --model_args pretrained=$SAVE_DIR,dtype=auto \
4 --tasks arc_easy,arc_challenge,boolq,hellaswag,mathqa,openbookqa,piqa,social_iqa,winogrande \
5 --output_path ${SAVE_DIR}/eval_results.json \
6 --batch_size auto \
7 --trust_remote_code| Phi-4-mini-instruct | 4-bit PTQ | 2-bit QAT | |
|---|---|---|---|
| arc_easy | 80.30 | 74.28 | 68.98 |
| arc_challenge | 58.45 | 52.65 | 43.17 |
| boolq | 83.46 | 69.11 | 71.50 |
| hellaswag | 72.76 | 68.97 | 62.10 |
| mathqa | 41.27 | 38.12 | 32.76 |
| openbookqa | 41.80 | 39.80 | 38.40 |
| piqa | 78.29 | 76.22 | 73.83 |
| social_iqa | 49.64 | 45.55 | 46.93 |
| winogrande | 71.51 | 68.67 | 64.48 |
1git clone https://github.com/pytorch/executorch.git
2pushd executorch
3git submodule update --init --recursive
4python install_executorch.py
5USE_CPP=1 TORCHAO_BUILD_KLEIDIAI=1 pip install third-party/ao
6popd1# 1. Download QAT'd weights from HF
2HF_DIR=pytorch/Phi-4-mini-instruct-parq-2w-4e-shared
3WEIGHT_DIR=$(hf download ${HF_DIR})
4
5# 2. Rename the weight keys to ones that ExecuTorch expects
6python -m executorch.examples.models.phi_4_mini.convert_weights $WEIGHT_DIR pytorch_model_converted.bin
7
8# 3. Download model config from the ExecuTorch repo
9curl -L -o phi_4_mini_config.json https://raw.githubusercontent.com/pytorch/executorch/main/examples/models/phi_4_mini/config/config.json
10
11# 4. Export the model to ExecuTorch pte file
12python -m executorch.examples.models.llama.export_llama \
13 --model "phi_4_mini" \
14 --checkpoint pytorch_model_converted.bin \
15 --params phi_4_mini_config.json \
16 --output_name phi4_model_2bit.pte \
17 -kv \
18 --use_sdpa_with_kv_cache \
19 --use-torchao-kernels \
20 --max_context_length 1024 \
21 --max_seq_length 256 \
22 --dtype fp32 \
23 --metadata '{"get_bos_id":199999, "get_eos_ids":[200020,199999]}'
24
25# # 5. (optional) Upload pte file to HuggingFace
26# hf upload ${HF_DIR} phi4_model_2bit.pte