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: x.item() can not be called when creating the model on a "meta" device #477

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

baldassarreFe
Copy link

Context: I want to load an empty DINOv2 model without pretrained weights on a meta device.

Issue: the constructor uses torch.linspace() and calls .item() on its elements, which gives an error when used inside a meta device context:

with torch.device("meta"):
    model = torch.hub.load("facebookresearch/dinov2", "dinov2_vits14_reg", pretrained=False)

# RuntimeError: Tensor.item() cannot be called on meta tensors
# File: dinov2/models/vision_transformer.py", line 119, in <listcomp>
#    dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)]  # stochastic depth decay rule

Solution: use numpy for creating the list of drop path rates:

with torch.device("meta"):
    model = torch.hub.load("baldassarrefe/dinov2:patch-2", "dinov2_vits14_reg", pretrained=False)
# all good

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants