Run this model with HF Jobs
Run microsoft/Florence-2-base on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs and TRL to fine-tune https://huggingface.co/microsoft/Florence-2-base. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/microsoft/Florence-2-base on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Evaluate on a benchmark
Use HF Jobs to evaluate https://huggingface.co/microsoft/Florence-2-base. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
Quantize for lower memory use
Use HF Jobs to create a 4-bit version of https://huggingface.co/microsoft/Florence-2-base. Push it to a new model repo, then reload it from there and report its memory footprint. Run `hf jobs --help` first to learn the CLI.
Instructions to use microsoft/Florence-2-base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/Florence-2-base with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="microsoft/Florence-2-base", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True) model = AutoModelForMultimodalLM.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use microsoft/Florence-2-base with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/Florence-2-base" # 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-base", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/microsoft/Florence-2-base
- SGLang
How to use microsoft/Florence-2-base 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-base" \ --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-base", "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-base" \ --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-base", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use microsoft/Florence-2-base with Docker Model Runner:
docker model run hf.co/microsoft/Florence-2-base
Fix weight init, deprecation warnings, safetensors (#27)
Browse files- Update modeling code and weights (9a303c20e479b3e899a8262364920a58227458f8)
- Add one more file (c2d78d1e36292674195414029d0d0c1a721c53e1)
- Update tied weights keys (bef74d7de9a4f1275b0c6e517b80afee13018e5c)
- Tie weights correctly and update weights (1e8c51ff43b3851d36c8a21f688ad903ebfe4f0a)
- Fix dtype (386f84a8872b44814ea429c0187e0b7406260d94)
Files changed (2)
- model.safetensorsAdded+3
- modeling_florence2.py+20-22
123version https://git-lfs.github.com/spec/v1oid sha256:03075d2d2d2bbd3e180b9ba0afae4aa8563226e2d32911656966e05b2f2ee060size 46322126625 unmodified lines26272829303132577 unmodified lines610611612613614615616617618619620621622623624625626627628629630631632633634635636637638812 unmodified lines145114521453145414551456617 unmodified lines207420752076207720782079451 unmodified lines2531253225332534253525368 unmodified lines2545254625472548254925502551255225 unmodified linesfrom torch.nn import CrossEntropyLossfrom collections import OrderedDictfrom einops import rearrangefrom timm.models.layers import DropPath, trunc_normal_from transformers.modeling_utils import PreTrainedModelfrom transformers.generation.utils import GenerationMixin577 unmodified linesself.avgpool = nn.AdaptiveAvgPool1d(1)self.head = nn.Linear(self.embed_dims[-1], num_classes) if num_classes > 0 else nn.Identity()self.apply(self._init_weights)@propertydef dim_out(self):return self.embed_dims[-1]def _init_weights(self, m):if isinstance(m, nn.Linear):trunc_normal_(m.weight, std=0.02)if m.bias is not None:nn.init.constant_(m.bias, 0)elif isinstance(m, nn.Conv2d):nn.init.normal_(m.weight, std=0.02)for name, _ in m.named_parameters():if name in ['bias']:nn.init.constant_(m.bias, 0)elif isinstance(m, nn.LayerNorm):nn.init.constant_(m.weight, 1.0)nn.init.constant_(m.bias, 0)elif isinstance(m, nn.BatchNorm2d):nn.init.constant_(m.weight, 1.0)nn.init.constant_(m.bias, 0)def forward_features_unpool(self, x):"""forward until avg pooling812 unmodified linesmodule.weight.data.normal_(mean=0.0, std=std)if module.padding_idx is not None:module.weight.data[module.padding_idx].zero_()@propertydef dummy_inputs(self):617 unmodified lines# Initialize weights and apply final processingself.post_init()def get_encoder(self):return self.model.get_encoder()451 unmodified linesFLORENCE2_START_DOCSTRING,)class Florence2ForConditionalGeneration(Florence2PreTrainedModel):def __init__(self, config: Florence2Config):super().__init__(config)assert config.vision_config.model_type == 'davit', 'only DaViT is supported for now'8 unmodified lineslanguage_model = Florence2LanguageForConditionalGeneration(config=config.text_config)if language_model._tied_weights_keys is not None:self._tied_weights_keys = [f"language_model.{k}" for k in language_model._tied_weights_keys]self.language_model = language_modelself.pad_token_id = self.config.pad_token_id if self.config.pad_token_id is not None else -125 unmodified lines26272829303132577 unmodified lines610611612613614615616617618619812 unmodified lines14321433143414351436143714381439144014411442144314441445144614471448617 unmodified lines206620672068206920702071207220732074207520762077451 unmodified lines252925302531253225332534253525368 unmodified lines25452546254725482549255025 unmodified linesfrom torch.nn import CrossEntropyLossfrom collections import OrderedDictfrom einops import rearrangefrom timm.layers import DropPath, trunc_normal_from transformers.modeling_utils import PreTrainedModelfrom transformers.generation.utils import GenerationMixin577 unmodified linesself.avgpool = nn.AdaptiveAvgPool1d(1)self.head = nn.Linear(self.embed_dims[-1], num_classes) if num_classes > 0 else nn.Identity()@propertydef dim_out(self):return self.embed_dims[-1]def forward_features_unpool(self, x):"""forward until avg pooling812 unmodified linesmodule.weight.data.normal_(mean=0.0, std=std)if module.padding_idx is not None:module.weight.data[module.padding_idx].zero_()elif isinstance(module, nn.Conv2d):nn.init.normal_(module.weight, std=0.02)for name, _ in module.named_parameters():if name == "bias":nn.init.constant_(module.bias, 0)elif isinstance(module, nn.LayerNorm):nn.init.constant_(module.weight, 1.0)nn.init.constant_(module.bias, 0)elif isinstance(module, nn.BatchNorm2d):nn.init.constant_(module.weight, 1.0)nn.init.constant_(module.bias, 0)@propertydef dummy_inputs(self):617 unmodified lines# Initialize weights and apply final processingself.post_init()def _tie_weights(self):if self.config.tie_word_embeddings:self._tie_or_clone_weights(self.model.encoder.embed_tokens, self.model.shared)self._tie_or_clone_weights(self.model.decoder.embed_tokens, self.model.shared)self._tie_or_clone_weights(self.lm_head, self.model.shared)def get_encoder(self):return self.model.get_encoder()451 unmodified linesFLORENCE2_START_DOCSTRING,)class Florence2ForConditionalGeneration(Florence2PreTrainedModel):_tied_weights_keys = ["language_model.encoder.embed_tokens.weight", "language_model.decoder.embed_tokens.weight", "language_model.lm_head.weight"]def __init__(self, config: Florence2Config):super().__init__(config)assert config.vision_config.model_type == 'davit', 'only DaViT is supported for now'8 unmodified lineslanguage_model = Florence2LanguageForConditionalGeneration(config=config.text_config)self.language_model = language_modelself.pad_token_id = self.config.pad_token_id if self.config.pad_token_id is not None else -1
model.safetensorsAdded+3
modeling_florence2.py+20-22