Instructions to use microsoft/phi-2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/phi-2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="microsoft/phi-2")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2") model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2") - Inference
- Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use microsoft/phi-2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/phi-2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/phi-2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/microsoft/phi-2
- SGLang
How to use microsoft/phi-2 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "microsoft/phi-2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/phi-2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "microsoft/phi-2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/phi-2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use microsoft/phi-2 with Docker Model Runner:
docker model run hf.co/microsoft/phi-2
Loading a qlora finetuned checkpoint with the new updates outputs differently
The checkpoint was finetuned and saved under transformers == 4.36.1 and phi-2 HF code revision 834565c23f9b28b96ccbeabe614dd906b6db551a
After updating to the latest revision and re-installing transformers as a development package transformers-4.37.0.dev0, if the same checkpoint loaded, it predicts similar but different outputs. The checkpoint loading codeline:AutoPeftModelForCausalLM.from_pretrained(ckpt_path, device_map=device, torch_dtype="auto", trust_remote_code=True)
I just noticed the model layers' name had been changed at the new update. Maybe something not matched? I didn't dive into the details yet, wonder which tool is practical for network structure comparison purpose.
You should be able to convert your old checkpoint to the new format using this script: https://github.com/huggingface/transformers/blob/main/src/transformers/models/phi/convert_phi_weights_to_hf.py.
After the conversion, the layers' name will match back and the generation should be as expected.
You should be able to convert your old checkpoint to the new format using this script: https://github.com/huggingface/transformers/blob/main/src/transformers/models/phi/convert_phi_weights_to_hf.py.
After the conversion, the layers' name will match back and the generation should be as expected.
Thanks for your prompt reply.
Tried this script, having key mismatched error when loading converted weights to new model defination.
By checking the convert function, I don't think it can properly convert the old model weights (before revision 834565c23f9b28b96ccbeabe614dd906b6db551a) into the new transfermor intergated network defination.
For example, "Wqkv" layer will be converted into "query_key_value", while can only find "q_proj" etc in the latest model defination. The layer should be further handled and splited into q, k, v
Oh, my bad, I thought the conversion script had been updated to the splitted q, k and v.
Let me work on that and send a PR with an updated script.
PHI_MAPPING = {
"transformer.embd.wte.weight": "model.embed_tokens.weight",
"lm_head.linear": "lm_head",
"lm_head.ln": "model.final_layernorm",
"layers": "model.layers",
"transformer": "model",
".h.": ".layers.",
"ln": "input_layernorm",
"mixer": "self_attn",
"out_proj": "dense",
}
def convert_weights(original_weights, mapping, config):
converted_weights = {}
original_weights_keys = sorted(original_weights.keys())
for original_weights_key in original_weights_keys:
new_key = original_weights_key
if "rotary_emb" in original_weights_key:
continue
for k, v in mapping.items():
if k in new_key:
new_key = new_key.replace(k, v)
if "Wqkv" in original_weights_key:
if "weight" in original_weights_key:
weight = original_weights.pop(original_weights_key)
weight = weight.view(3, -1, config.hidden_size)
q_w, k_w, v_w = weight.chunk(3, dim=0)
converted_weights[new_key.replace("Wqkv", "q_proj")] = q_w.squeeze(0)
converted_weights[new_key.replace("Wqkv", "k_proj")] = k_w.squeeze(0)
converted_weights[new_key.replace("Wqkv", "v_proj")] = v_w.squeeze(0)
elif "bias" in original_weights_key:
bias = original_weights.pop(original_weights_key)
bias = bias.view(3, -1)
q_b, k_b, v_b = bias.chunk(3, dim=0)
converted_weights[new_key.replace("Wqkv", "q_proj")] = q_b.squeeze(0)
converted_weights[new_key.replace("Wqkv", "k_proj")] = k_b.squeeze(0)
converted_weights[new_key.replace("Wqkv", "v_proj")] = v_b.squeeze(0)
else:
converted_weights[new_key] = original_weights.pop(original_weights_key)
return converted_weights
This works for me and the output is as expected after conversion.
BTW, by comparing the new model weights and the old one, I just found that for the same layer, the weights' value are not the same. Is it re-trained? cause simpliy mapping the name shouldn't change the weights value
What do you mean by not the same?
If you are comparing tensor by tensor (shape by shape), there will be a time where a mismatch appears between Wqkv and the separate queries, keys and values. However, their internal value should be the same (except for the index where it concatenates q, k and v).
The logits are even expected to be really really close (or even the same).