Image-Text-to-Text
Transformers
Safetensors
multilingual
phi3_v
text-generation
nlp
code
vision
conversational
custom_code
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, dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- 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
fix_rope_scaling
#28
by haipingwu - opened
- modeling_phi3_v.py +8 -1
modeling_phi3_v.py
CHANGED
|
@@ -441,7 +441,7 @@ class Phi3SuScaledRotaryEmbedding(Phi3RotaryEmbedding):
|
|
| 441 |
|
| 442 |
@torch.no_grad()
|
| 443 |
def forward(self, x, position_ids, seq_len=None):
|
| 444 |
-
seq_len = torch.max(position_ids) + 1
|
| 445 |
if seq_len > self.original_max_position_embeddings:
|
| 446 |
ext_factors = torch.tensor(self.long_factor, dtype=torch.float32, device=x.device)
|
| 447 |
else:
|
|
@@ -1647,6 +1647,13 @@ class Phi3VForCausalLM(Phi3VPreTrainedModel):
|
|
| 1647 |
def prepare_inputs_for_generation(
|
| 1648 |
self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, pixel_values=None, image_sizes=None, **kwargs
|
| 1649 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1650 |
if past_key_values is not None:
|
| 1651 |
if isinstance(past_key_values, Cache):
|
| 1652 |
cache_length = past_key_values.get_seq_length()
|
|
|
|
| 441 |
|
| 442 |
@torch.no_grad()
|
| 443 |
def forward(self, x, position_ids, seq_len=None):
|
| 444 |
+
seq_len = seq_len or torch.max(position_ids) + 1
|
| 445 |
if seq_len > self.original_max_position_embeddings:
|
| 446 |
ext_factors = torch.tensor(self.long_factor, dtype=torch.float32, device=x.device)
|
| 447 |
else:
|
|
|
|
| 1647 |
def prepare_inputs_for_generation(
|
| 1648 |
self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, pixel_values=None, image_sizes=None, **kwargs
|
| 1649 |
):
|
| 1650 |
+
# When the first time input length reached long and short factor switching point, enforce re-compute cache
|
| 1651 |
+
# It will cause downside of slower at this single token position, however, better than current failure.
|
| 1652 |
+
if past_key_values and self.config.rope_scaling and input_ids.shape[1] >= self.config.original_max_position_embeddings + 1:
|
| 1653 |
+
past_length = past_key_values.seen_tokens if isinstance(past_key_values, Cache) else past_key_values[0][0].shape[2]
|
| 1654 |
+
if past_length <= self.config.original_max_position_embeddings:
|
| 1655 |
+
past_key_values = None
|
| 1656 |
+
|
| 1657 |
if past_key_values is not None:
|
| 1658 |
if isinstance(past_key_values, Cache):
|
| 1659 |
cache_length = past_key_values.get_seq_length()
|