Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tracing dinov2 #27561

Merged
merged 3 commits into from
Nov 21, 2023
Merged

Fix tracing dinov2 #27561

merged 3 commits into from
Nov 21, 2023

Conversation

amyeroberts
Copy link
Collaborator

@amyeroberts amyeroberts commented Nov 17, 2023

What does this PR do?

Tracing currently fails for DinoV2 due to an issue when calling torch's torch._C._nn._upsample_bicubic function through nn.functional.interpolate. There is an issue if the passed in scale_factor is (tensor(float), tensor(float)) so we must convert to a tuple of floats (float, float) as per this PR to enable tracing.

I have run the slow tracing tests to make sure everything works.

tests/models/dinov2/test_modeling_dinov2.py::Dinov2ModelTest::test_torchscript_simple <- tests/test_modeling_common.py PASSED                                                                                                                                              [ 33%]
tests/models/dinov2/test_modeling_dinov2.py::Dinov2ModelTest::test_torchscript_output_hidden_state <- tests/test_modeling_common.py PASSED                                                                                                                                 [ 66%]
tests/models/dinov2/test_modeling_dinov2.py::Dinov2ModelTest::test_torchscript_output_attentions <- tests/test_modeling_common.py PASSED                                                                                                                                   [100%]

The following now works:

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[0] #.last_hidden_state

# We have to force return_dict=False for tracing
model.config.return_dict = False

with torch.no_grad():
    traced_model = torch.jit.trace(model, [inputs.pixel_values])
    traced_outputs = traced_model(inputs.pixel_values)

print((last_hidden_states - traced_outputs[0]).abs().max())

Note: although the model outputs are close, they still have a significant absolute difference on the order of ~1e-4.

/Users/amyroberts/code/transformers/src/transformers/models/dinov2/modeling_dinov2.py:162: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if num_channels != self.num_channels:
/Users/amyroberts/code/transformers/src/transformers/models/dinov2/modeling_dinov2.py:94: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if num_patches == num_positions and height == width:
/Users/amyroberts/code/transformers/src/transformers/models/dinov2/modeling_dinov2.py:104: TracerWarning: Converting a tensor to a Python float might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  patch_pos_embed = patch_pos_embed.reshape(1, int(math.sqrt(num_positions)), int(math.sqrt(num_positions)), dim)
/Users/amyroberts/code/transformers/src/transformers/models/dinov2/modeling_dinov2.py:108: TracerWarning: Converting a tensor to a Python float might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  scale_factor=(float(height / math.sqrt(num_positions)), float(width / math.sqrt(num_positions))),
/Users/amyroberts/code/transformers/src/transformers/models/dinov2/modeling_dinov2.py:112: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if int(height) != patch_pos_embed.shape[-2] or int(width) != patch_pos_embed.shape[-1]:
/Users/amyroberts/code/transformers/src/transformers/models/dinov2/modeling_dinov2.py:112: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if int(height) != patch_pos_embed.shape[-2] or int(width) != patch_pos_embed.shape[-1]:
/Users/amyroberts/opt/miniconda3/envs/ml/lib/python3.10/site-packages/torch/jit/_trace.py:1093: TracerWarning: Output nr 1. of the traced function does not match the corresponding output of the Python function. Detailed error:
Tensor-likes are not close!

Mismatched elements: 1693 / 197376 (0.9%)
Greatest absolute difference: 0.00012087821960449219 at index (0, 46, 415) (up to 1e-05 allowed)
Greatest relative difference: 0.4337851929092805 at index (0, 206, 249) (up to 1e-05 allowed)
  _check_trace(
/Users/amyroberts/opt/miniconda3/envs/ml/lib/python3.10/site-packages/torch/jit/_trace.py:1093: TracerWarning: Output nr 2. of the traced function does not match the corresponding output of the Python function. Detailed error:
Tensor-likes are not close!

Mismatched elements: 5 / 768 (0.7%)
Greatest absolute difference: 1.6689300537109375e-05 at index (0, 688) (up to 1e-05 allowed)
Greatest relative difference: 0.0002756976223801738 at index (0, 6) (up to 1e-05 allowed)
  _check_trace(
tensor(0.0001, grad_fn=<MaxBackward1>)

Fixes #27537

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests? No - but tests added

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Nov 17, 2023

The documentation is not available anymore as the PR was closed or merged.

@amyeroberts amyeroberts changed the title Fix tracing dino Fix tracing dinov2 Nov 17, 2023
Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! 🤗
Would it make sense to add the tracing snippet to the dinov2.md ? 🤗

@amyeroberts
Copy link
Collaborator Author

@ArthurZucker - nice idea, I'll add it!

@@ -25,6 +25,37 @@ The abstract from the paper is the following:
This model was contributed by [nielsr](https://huggingface.co/nielsr).
The original code can be found [here](https://github.com/facebookresearch/dinov2).

## Usage tips
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ArthurZucker WDYT of this addition to the model page?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it!
We often have people asking how xx model can be compiled / traced so great to be careful when we add support for this! 😉

@amyeroberts amyeroberts merged commit 0145c68 into huggingface:main Nov 21, 2023
18 checks passed
@amyeroberts amyeroberts deleted the fix-tracing-dino branch November 21, 2023 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow script tracing DINOv2
3 participants