Instructions to use microsoft/Florence-2-large with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/Florence-2-large with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="microsoft/Florence-2-large", trust_remote_code=True)# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True) model = AutoModelForMultimodalLM.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use microsoft/Florence-2-large with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/Florence-2-large" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/Florence-2-large", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/microsoft/Florence-2-large
- SGLang
How to use microsoft/Florence-2-large 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/Florence-2-large" \ --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/Florence-2-large", "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/Florence-2-large" \ --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/Florence-2-large", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use microsoft/Florence-2-large with Docker Model Runner:
docker model run hf.co/microsoft/Florence-2-large
Fix TypeError: EncoderDecoderCache is not subscriptable in prepare_inputs_for_generation
Browse filesprepare_inputs_for_generation (both Florence2LanguageForConditionalGeneration and Florence2ForConditionalGeneration -- identical code in both) read past_length via past_key_values[0][0].shape[2], assuming the legacy tuple-of-tuples KV cache format. Current transformers passes a Cache object (EncoderDecoderCache for encoder-decoder models like this one), which isn't subscriptable. Every Cache subclass implements get_seq_length() for exactly this purpose -- use it when available, falling back to the legacy tuple indexing for older transformers/custom cache objects that predate the Cache class.
- modeling_florence2.py +14 -2
|
@@ -2194,7 +2194,13 @@ class Florence2LanguageForConditionalGeneration(Florence2LanguagePreTrainedModel
|
|
| 2194 |
):
|
| 2195 |
# cut decoder_input_ids if past_key_values is used
|
| 2196 |
if past_key_values is not None:
|
| 2197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2198 |
|
| 2199 |
# Some generation methods already pass only the last input ID
|
| 2200 |
if decoder_input_ids.shape[1] > past_length:
|
|
@@ -2823,7 +2829,13 @@ class Florence2ForConditionalGeneration(Florence2PreTrainedModel):
|
|
| 2823 |
):
|
| 2824 |
# cut decoder_input_ids if past_key_values is used
|
| 2825 |
if past_key_values is not None:
|
| 2826 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2827 |
|
| 2828 |
# Some generation methods already pass only the last input ID
|
| 2829 |
if decoder_input_ids.shape[1] > past_length:
|
|
|
|
| 2194 |
):
|
| 2195 |
# cut decoder_input_ids if past_key_values is used
|
| 2196 |
if past_key_values is not None:
|
| 2197 |
+
# past_key_values is a Cache object (e.g. EncoderDecoderCache) in current
|
| 2198 |
+
# transformers, not the legacy tuple-of-tuples this code was written for --
|
| 2199 |
+
# it isn't subscriptable, but every Cache subclass implements get_seq_length()
|
| 2200 |
+
if hasattr(past_key_values, "get_seq_length"):
|
| 2201 |
+
past_length = past_key_values.get_seq_length()
|
| 2202 |
+
else:
|
| 2203 |
+
past_length = past_key_values[0][0].shape[2]
|
| 2204 |
|
| 2205 |
# Some generation methods already pass only the last input ID
|
| 2206 |
if decoder_input_ids.shape[1] > past_length:
|
|
|
|
| 2829 |
):
|
| 2830 |
# cut decoder_input_ids if past_key_values is used
|
| 2831 |
if past_key_values is not None:
|
| 2832 |
+
# past_key_values is a Cache object (e.g. EncoderDecoderCache) in current
|
| 2833 |
+
# transformers, not the legacy tuple-of-tuples this code was written for --
|
| 2834 |
+
# it isn't subscriptable, but every Cache subclass implements get_seq_length()
|
| 2835 |
+
if hasattr(past_key_values, "get_seq_length"):
|
| 2836 |
+
past_length = past_key_values.get_seq_length()
|
| 2837 |
+
else:
|
| 2838 |
+
past_length = past_key_values[0][0].shape[2]
|
| 2839 |
|
| 2840 |
# Some generation methods already pass only the last input ID
|
| 2841 |
if decoder_input_ids.shape[1] > past_length:
|