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
Disabled autocast
In line 306 and 307 of modeling_phi.py autocast is disabled due to overflow issues when using fp16. Bfloat16 does not have these issues so it should not be disabled in this case, right?
Also, wasn't Phi-2 trained in mixed precision fp16? Why wasn't this an issue when training but it seems to be in inference?
Same question, can we simply enable autocast?
@xueyanz actually I do think that is needed, even if using bfloat16. For some reason autocasting the forward of the attention module leads to instability issues in training. I will keep disabling autocast
Thanks so much for your prompt reply, I am trying to train phi2 in a VLM model using auto-cast. To disable autocast, do you manually transfer to fp16?
I load it in fp32 and use torch.amp with bfloat16. The last version of modeling_phi.py already disables autocast by itself in the forward method of the attention module. I'm actually also building a VLM using phi-2, would you care to explain what you are doing at a high level? Also, MoE-LLaVa states that there are training instabilities when using phi-2, maybe because they used a past version of the modeling_phi.py file.
I am using the inference api on huggingface. I attempted to load and access the model using the huggingface production end points. I gave up after several attempts. Any special settings or configs I need to be aware of to enable on a private huggingface inference api? Would help heaps.
I load it in fp32 and use
torch.ampwithbfloat16. The last version ofmodeling_phi.pyalready disables autocast by itself in the forward method of the attention module. I'm actually also building a VLM using phi-2, would you care to explain what you are doing at a high level? Also, MoE-LLaVa states that there are training instabilities when using phi-2, maybe because they used a past version of themodeling_phi.pyfile.
I will not train the language model, so I simply enable Autocast to see the performance, and the inference result seems reasonable. I am building vlm along the lines of my work in the past.
ok, it seems that auto-casting would make the outputs NaN even during evaluation.
Edited - nevermind - I figured out how to use float16 & bfloat16 without needing to autocast. Thank you.
Edited - nevermind - I figured out how to use float16 & bfloat16 without needing to autocast. Thank you.
Care to share what you did? Thanks.