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 torchaodataset_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-3wei-4emb-${SEED}
5
6dataset_name=<TODO>
7max_steps=<TODO>
8ngpu=1
9device_batch_size=4
10grad_accum_steps=2
11lr=5e-6
12PYTORCH_ALLOC_CONF=expandable_segments:True \
13 uv run "https://huggingface.co/datasets/pytorch/parq-sft/resolve/main/qat_sft.py" \
14 --model_name_or_path microsoft/Phi-4-mini-instruct \
15 --bf16 true \
16 --num_train_epochs 1 \
17 --per_device_train_batch_size $device_batch_size \
18 --gradient_accumulation_steps $grad_accum_steps \
19 --dataset_name $dataset_name \
20 --dataloader_num_workers 4 \
21 --max_length 4096 \
22 --max_steps $max_steps \
23 --report_to tensorboard \
24 --learning_rate $lr \
25 --lr_scheduler_type linear \
26 --warmup_ratio 0.0 \
27 --seed $SEED \
28 --output_dir $SAVE_DIR \
29 --weight_bits 4 \
30 --linear_pat 'proj\.weight$' \
31 --embed_bits 4 \
32 --embed_pat '(lm_head|embed_tokens)'${SAVE_DIR}/quant_converted. To change the source checkpoint, rerun the above script with --resume_from_checkpoint ${SAVE_DIR}/checkpoint-{SAVE_STEP}.1import os
2
3from huggingface_hub import whoami, get_token
4from transformers import AutoModelForCausalLM, AutoTokenizer
5
6model_path = f"{SAVE_DIR}"
7model = AutoModelForCausalLM.from_pretrained(
8 model_path, device_map="auto", dtype="auto"
9)
10tokenizer = AutoTokenizer.from_pretrained(model_path)
11
12# Manual testing
13prompt = "Hey, are you conscious? Can you talk to me?"
14messages = [{"role": "user", "content": prompt}]
15templated_prompt = tokenizer.apply_chat_template(
16 messages,
17 tokenize=False,
18 add_generation_prompt=True,
19)
20inputs = tokenizer(templated_prompt, return_tensors="pt").to(model.device)
21inputs.pop("token_type_ids", None)
22
23start_idx = len(inputs.input_ids[0])
24response_ids = model.generate(**inputs, max_new_tokens=256, **kwargs)[0]
25response_ids = response_ids[start_idx:].tolist()
26output_text = tokenizer.decode(response_ids, skip_special_tokens=True)
27print(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 | 4-bit QAT | |
|---|---|---|---|
| gsm8k | 81.43 | 64.29 | 75.06 |
| mathqa | 41.27 | 38.12 | 39.16 |
git clone https://github.com/pytorch/executorch.git
pushd executorch
git submodule update --init --recursive
python install_executorch.py
USE_CPP=1 TORCHAO_BUILD_KLEIDIAI=1 pip install third-party/ao
popd1# 1. Download QAT'd weights from HF
2HF_DIR=pytorch/Phi-4-mini-instruct-parq-4w-4e-shared-gsm
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_4bit.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_4bit.pte