Text Generation
Transformers
Safetensors
multilingual
phi3_v
nlp
code
vision
conversational
custom_code
Instructions to use microsoft/Phi-3-vision-128k-instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/Phi-3-vision-128k-instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="microsoft/Phi-3-vision-128k-instruct", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-vision-128k-instruct", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use microsoft/Phi-3-vision-128k-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-vision-128k-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-vision-128k-instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/microsoft/Phi-3-vision-128k-instruct
- SGLang
How to use microsoft/Phi-3-vision-128k-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-vision-128k-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-vision-128k-instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'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-vision-128k-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-vision-128k-instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use microsoft/Phi-3-vision-128k-instruct with Docker Model Runner:
docker model run hf.co/microsoft/Phi-3-vision-128k-instruct
Fix generation when `repetition_penalty` is activated
#57
by YenChunChen - opened
- image_embedding_phi3_v.py +10 -1
image_embedding_phi3_v.py
CHANGED
|
@@ -12,6 +12,7 @@
|
|
| 12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
# See the License for the specific language governing permissions and
|
| 14 |
# limitations under the License.
|
|
|
|
| 15 |
|
| 16 |
import torch
|
| 17 |
from torch import nn
|
|
@@ -191,7 +192,15 @@ class Phi3ImageEmbedding(nn.Module):
|
|
| 191 |
# positions for image tokens
|
| 192 |
positions = torch.nonzero((input_ids < 0) & (input_ids > -MAX_INPUT_ID), as_tuple=True)
|
| 193 |
has_image = len(positions[0].tolist()) > 0
|
| 194 |
-
input_ids = input_ids.clamp_min(0).clamp_max(self.vocab_size).detach()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
hidden_states = self.wte(input_ids)
|
| 196 |
|
| 197 |
if has_image:
|
|
|
|
| 12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
# See the License for the specific language governing permissions and
|
| 14 |
# limitations under the License.
|
| 15 |
+
import warnings
|
| 16 |
|
| 17 |
import torch
|
| 18 |
from torch import nn
|
|
|
|
| 192 |
# positions for image tokens
|
| 193 |
positions = torch.nonzero((input_ids < 0) & (input_ids > -MAX_INPUT_ID), as_tuple=True)
|
| 194 |
has_image = len(positions[0].tolist()) > 0
|
| 195 |
+
# input_ids = input_ids.clamp_min(0).clamp_max(self.vocab_size).detach()
|
| 196 |
+
input_ids.clamp_min_(0).clamp_max_(self.vocab_size)
|
| 197 |
+
warnings.warn(
|
| 198 |
+
"Phi-3-V modifies `input_ids` in-place and the tokens indicating images will be "
|
| 199 |
+
"removed after model forward. If your workflow requires multiple forward passes on "
|
| 200 |
+
"the same `input_ids`, please make a copy of `input_ids` before passing it to the "
|
| 201 |
+
"model."
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
hidden_states = self.wte(input_ids)
|
| 205 |
|
| 206 |
if has_image:
|