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
- Local Apps Settings
- 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
Slow inference times on gpu
#17
by loretoparisi - opened
While model loading is pretty fast (once downloaded) and it takes around 1.5 seconds an inference for 2048 token (max_length) on a A10G / 24GB took ~80 sec.
loading function was
def load_hf_local(model_name, device, dtype:torch.float16):
"""
load model via Huggingface AutoTokenizer, AutoModelForCausalLM
"""
start_time = time. time()
torch.set_default_dtype(dtype)
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name, local_files_only=True, trust_remote_code=True)
with torch.device(device):
model = transformers.AutoModelForCausalLM.from_pretrained(model_name, local_files_only=True, device_map="auto", torch_dtype=dtype, trust_remote_code=True)
model.to(device)
print(f"Loaded in {time.time() - start_time: .2f} seconds")
return tokenizer, model
generate function was
def LLM_generate(model, tokenizer, prompt, length):
start_time = time.time()
inputs = tokenizer(prompt, return_tensors="pt", return_attention_mask=False)
model_inputs = inputs.to(device)
model.to(device)
input_token_len = len(model_inputs.tokens())
outputs = model.generate(**model_inputs, max_length=length if length >= input_token_len else input_token_len)
print(f"generated in {time.time() - start_time: .2f} seconds")
return tokenizer.batch_decode(outputs)[0]
while setting max_length to 512 tokens, led to ~20 seconds.
This is a test ranging from 128 to 2048
generated in 4.37 seconds
max_length:128, elapsed:4.372055530548096
generated in 9.16 seconds
max_length:256, elapsed:9.158923625946045
generated in 19.05 seconds
max_length:512, elapsed:19.05333709716797
generated in 38.90 seconds
max_length:1024, elapsed:38.89565658569336
generated in 79.18 seconds
max_length:2048, elapsed:79.17627263069153
Phi models are compatible with vLLM, have you considered using it?
https://docs.vllm.ai/en/latest/index.html
gugarosa changed discussion status to closed
vLLM crashes with Phi 2.0: AttributeError: 'PhiConfig' object has no attribute 'layer_norm_epsilon'
