Phi for Tenstorrent Tensix
Collection
Phi quantized to BFP4/BFP8 with GPTQ. • 5 items • Updated
These weights from microsoft/phi-4 are quantized for Tenstorrent Tensix NPUs, with each format in its own folder.
| Folder | Formats | Size (GB, packed) | Mean KL to BF16 |
|---|---|---|---|
bfp4/ |
Attention and MLP gate and up bfloat4_b; MLP down bfloat8_b; the rest bf16 | 11.559 | 0.02224 |
bfp8/ |
Attention and MLP bfloat8_b; the rest bf16 | 16.539 | 0.00071 |
| BF16 (microsoft/phi-4) | - | 29.32 | 0 |
| Q4_K_M (llama.cpp) | - | 9.059 | 0.0180 |
KL covers 16,280 positions of English and Korean text (WikiText-2 test, Korean Wikipedia), plain and in the chat template.
hf download Xylolabs/phi-4-Tenstorrent-Tensix --include "bfp8/*" --local-dir phi-4-Tenstorrent-Tensix
export WEIGHTS_DIR="$PWD/phi-4-Tenstorrent-Tensix/bfp8"
from transformers import AutoModelForCausalLM, AutoTokenizer
tok = AutoTokenizer.from_pretrained("phi-4-Tenstorrent-Tensix/bfp8")
model = AutoModelForCausalLM.from_pretrained("phi-4-Tenstorrent-Tensix/bfp8", dtype="bfloat16", device_map="auto")
messages = [{"role": "user", "content": "What is the capital of France?"}]
inputs = tok.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt",
return_dict=True).to(model.device)
out = model.generate(**inputs, max_new_tokens=64)
print(tok.decode(out[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True))
Pack one matrix:
import json
import torch
import ttnn
from safetensors import safe_open
d = "phi-4-Tenstorrent-Tensix/bfp8"
name = "model.layers.0.mlp.down_proj.weight"
fmt = json.load(open(f"{d}/quant4.json"))["layers"]["0"]["mlp.down_proj"]["format"]
dtype = {"bfp4": ttnn.bfloat4_b, "bfp8": ttnn.bfloat8_b}[fmt]
shard = json.load(open(f"{d}/model.safetensors.index.json"))["weight_map"][name]
with safe_open(f"{d}/{shard}", "pt") as f:
w = f.get_tensor(name).t().contiguous()
device = ttnn.open_device(device_id=0)
w_tt = ttnn.as_tensor(w, dtype=dtype, layout=ttnn.TILE_LAYOUT,
device=device, memory_config=ttnn.DRAM_MEMORY_CONFIG)
assert torch.equal(ttnn.to_torch(w_tt).float(), w.float())
ttnn.close_device(device)
python phi-4-Tenstorrent-Tensix/bfp8/pack_tensix.py phi-4-Tenstorrent-Tensix/bfp8
python phi-4-Tenstorrent-Tensix/bfp8/verify_grid.py phi-4-Tenstorrent-Tensix/bfp8
Add --device to round-trip the packed values.
tt-transformers requires tt-metal v0.81.1 and --use_hf_rope.
git clone --branch v0.81.1 https://github.com/tenstorrent/tt-metal.git && cd tt-metal
# install tt-metal v0.81.1 as its INSTALLING.md describes, then
pip install -r tt_metal/python_env/requirements-dev.txt
export HF_MODEL="$WEIGHTS_DIR"
export TT_CACHE_PATH="$PWD/model_cache/phi-4-Tenstorrent-Tensix/bfp8"
pytest models/tt_transformers/demo/simple_text_demo.py -k "performance and batch-1" --use_hf_rope \
--optimizations 'precision_cfg = {ff1_3: bfp8, ff2: bfp8, wqkv: bfp8, wo: bfp8, kv_cache: bfp8, activation: mixed}, fidelity_cfg = {li_ff1_3: hifi2, li_ff2: lofi, li_qkv_decode: hifi2, li_o_decode: hifi2, sdpa_decode: hifi2na, li_qkv_prefill: hifi2, li_o_prefill: hifi2fp16, sdpa_prefill: hifi4}'
To use another format, download its folder the same way and substitute its local path.
MIT, as the base model (LICENSE). The changes are listed in NOTICE. pack_tensix.py and verify_grid.py
are by Xylolabs under Apache-2.0.
Each folder carries its own LICENSE and NOTICE.
Cite microsoft/phi-4.
Base model
microsoft/phi-4