import requests
import torch
from PIL import Image
from transformers import AutoProcessor, AutoModelForCausalLM
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-large", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large", trust_remote_code=False)
prompt = "<OD>"
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
image = Image.open(requests.get(url, stream=True).raw)
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
generated_ids = model.generate(
input_ids=inputs["input_ids"],
pixel_values=inputs["pixel_values"],
max_new_tokens=4096,
num_beams=3,
do_sample=False
)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
parsed_answer = processor.post_process_generation(generated_text, task="<OD>", image_size=(image.width, image.height))
print(parsed_answer)
`torch_dtype` is deprecated! Use `dtype` instead!
Traceback (most recent call last):
File "/home/ruzickal/Code/entity-analyzer-backend/audio-llm-api/test.py", line 11, in <module>
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-large", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ruzickal/Code/entity-analyzer-backend/transformers/src/transformers/models/auto/auto_factory.py", line 378, in from_pretrained
return model_class.from_pretrained(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ruzickal/Code/entity-analyzer-backend/transformers/src/transformers/modeling_utils.py", line 270, in _wrapper
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/home/ruzickal/Code/entity-analyzer-backend/transformers/src/transformers/modeling_utils.py", line 4473, in from_pretrained
model = cls(config, *model_args, **model_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ruzickal/.cache/huggingface/modules/transformers_modules/microsoft/Florence_hyphen_2_hyphen_large/21a599d414c4d928c9032694c424fb94458e3594/modeling_florence2.py", line 2535, in __init__
super().__init__(config)
File "/home/ruzickal/Code/entity-analyzer-backend/transformers/src/transformers/modeling_utils.py", line 1822, in __init__
self.config._attn_implementation_internal = self._check_and_adjust_attn_implementation(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ruzickal/Code/entity-analyzer-backend/transformers/src/transformers/modeling_utils.py", line 2416, in _check_and_adjust_attn_implementation
applicable_attn_implementation = self.get_correct_attn_implementation(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ruzickal/Code/entity-analyzer-backend/transformers/src/transformers/modeling_utils.py", line 2450, in get_correct_attn_implementation
self._sdpa_can_dispatch(is_init_check)
File "/home/ruzickal/Code/entity-analyzer-backend/transformers/src/transformers/modeling_utils.py", line 2314, in _sdpa_can_dispatch
if not self._supports_sdpa:
^^^^^^^^^^^^^^^^^^^
File "/home/ruzickal/.cache/pypoetry/virtualenvs/audio-llm-api-nPR59pGu-py3.12/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1962, in __getattr__
raise AttributeError(
AttributeError: 'Florence2ForConditionalGeneration' object has no attribute '_supports_sdpa'
System Info
transformersversion: 4.57.0.dev0Who can help?
@yonigozlan @molbap
Information
Tasks
examplesfolder (such as GLUE/SQuAD, ...)Reproduction
Running the example script:
Leads to the following error:
Expected behavior
Output the detections