huggingface / transformers

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
https://huggingface.co/transformers
Apache License 2.0
131.92k stars 26.27k forks source link

Allow script tracing DINOv2 #27537

Closed Danil328 closed 9 months ago

Danil328 commented 10 months ago

I found PR to dinov2 "Pass scale factor as a tuple of floats to F.interpolate() to allow tracing." https://github.com/facebookresearch/dinov2/pull/247

https://github.com/huggingface/transformers/blob/85fde09c97213bf7e8625f83096bb2a9e183f987/src/transformers/models/dinov2/modeling_dinov2.py#L104C19-L104C19

Danil328 commented 10 months ago

I have exception now:

image
amyeroberts commented 10 months ago

Hi @Danil328, thanks for raising this issue!

Could you make sure to follow the issue template and include details of your running environment and a minimal reproducible snippet?

From the error it looks like the scale_factor values being passed to interpolate is a NoneType.

Danil328 commented 10 months ago

Same problem in facebookresearch - https://github.com/facebookresearch/dinov2/issues/102

Reproduction

import torch
from transformers import AutoImageProcessor, AutoModel
from PIL import Image
import requests

url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
image = Image.open(requests.get(url, stream=True).raw)

processor = AutoImageProcessor.from_pretrained('facebook/dinov2-base')
model = AutoModel.from_pretrained('facebook/dinov2-base')

inputs = processor(images=image, return_tensors="pt")
outputs = model(**inputs)
last_hidden_states = outputs.last_hidden_state

with torch.no_grad():
    example_input = torch.rand(1, 3, 224, 224, dtype=torch.float32, device="cuda")
    traced_model = torch.jit.trace(model.cuda(), example_input) # fails here

Error

image

Expected behavior

Success

Enviroment

bash python=3.8 torch==2.0.1 transformers==4.35.0

amyeroberts commented 10 months ago

@Danil328 - thanks for providing the snippet! I've opened a PR which should resolve the issue