Instructions to use microsoft/Phi-3.5-vision-instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/Phi-3.5-vision-instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="microsoft/Phi-3.5-vision-instruct", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-3.5-vision-instruct", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use microsoft/Phi-3.5-vision-instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/Phi-3.5-vision-instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/Phi-3.5-vision-instruct", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/microsoft/Phi-3.5-vision-instruct
- SGLang
How to use microsoft/Phi-3.5-vision-instruct 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-3.5-vision-instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/Phi-3.5-vision-instruct", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'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-3.5-vision-instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/Phi-3.5-vision-instruct", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use microsoft/Phi-3.5-vision-instruct with Docker Model Runner:
docker model run hf.co/microsoft/Phi-3.5-vision-instruct
Unexpected behavior of Phi-3.5-vision model with respect to 4K token length.
While testing the Phi3.5 vision model, I noticed that if prompt length is less than 4096 and len(prompt) + len(generation tokens) is greater than 4096, the model either creates dummy output or stops generating.
In this case, the model tends to generate <|end|> tokens (in logit distribution), but generates dummy output because <|end|> is not a stop token in the HF implementation.
Here is the scripts I used to generate this behavior, but brought from code snippet in https://huggingface.co/microsoft/Phi-3.5-vision-instruct
I just reduced the number of images in the prompt.
from PIL import Image
import requests
from transformers import AutoModelForCausalLM
from transformers import AutoProcessor
model_id = "/opt/models/phi/Phi-3.5-vision-instruct/"
# Note: set _attn_implementation='eager' if you don't have flash_attn installed
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="cuda",
trust_remote_code=True,
torch_dtype="auto",
_attn_implementation='eager'
)
# for best performance, use num_crops=4 for multi-frame, num_crops=16 for single-frame.
processor = AutoProcessor.from_pretrained(model_id,
trust_remote_code=True,
num_crops=4
)
images = []
placeholder = ""
# Note: if OOM, you might consider reduce number of frames in this example.
for i in range(1, 6):
url = f"https://image.slidesharecdn.com/azureintroduction-191206101932/75/Introduction-to-Microsoft-Azure-Cloud-{i}-2048.jpg"
images.append(Image.open(requests.get(url, stream=True).raw))
placeholder += f"<|image_{i}|>\n"
messages = [
{"role": "user", "content": placeholder+"Summarize the deck of slides."},
]
prompt = processor.tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
inputs = processor(prompt, images, return_tensors="pt").to("cuda:0")
generation_args = {
"max_new_tokens": 512,
"temperature": 0.0,
}
generate_ids = model.generate(**inputs,
eos_token_id=processor.tokenizer.eos_token_id,
**generation_args
)
print("input prompt size", inputs.input_ids.shape[1])
generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
print("output tokens size", generate_ids.shape[1])
response = processor.batch_decode(generate_ids,
skip_special_tokens=True,
clean_up_tokenization_spaces=False)[0]
it prints
input prompt size 3817
output tokens size 512
To encapsulate, the slides feature the following segments:
- Introduction to Azure:
The presentation introduces Microsoft Azure, a cloud computing platform. It highlights the three types of Azure services: Enterprise, Hybrid, and Hyper-scale. The presenter is Dinesh Kumar Wickramasinghe, a Senior Software Engineer from CMS Private Limited in Sri Lanka.
- Azure Services Overview:
Azure offers a continuously expanding set of cloud services to help organizations meet their current and future business challenges. It provides the freedom to build, manage, and deploy applications on a massive global network using favorite tools and frameworks.
- Cloud Computing Models:
The presentation explains the three main models of cloud computing: IaaS (Infrastructure-as-a-Service), PaaS (Platform-as-a-Service), and SaaS (Software-as-a-Service). Each model is represented by a unique icon and color.
- Cloud Service Comparison:
The presentation compares the roles of the user in different cloud service models using a dining table analogy. In IaaS, the user manages the infrastructure. In PaaS, the user manages the platform. In SaaS, the user manages, and and and services. The, and the and the service. The the and and the service.
s the service. The and and the service. The is the service. The service. The service. The service. The service. The service. The service. The and and the is the service. The service.
and the and the service. The and and and
and and the and ands.
in the service. The service. The in the
and and the to the
the the the home.
and and the the the vendor.
and and and ands and and and and the me.
and and and and and and and and and and and and and
and
and
and and and:
and
and
andS
How can it be solved, or is it an intended behavior?