Skip to content

[Bug] AutoModel.from_pretrained() crashes with RuntimeError: Tensor.item() cannot be called on meta tensors #87

Description

@aswin00000

Bug Report

Model: OpenGVLab/VideoMAEv2-Base
File: modeling_videomaev2.pyVisionTransformer.__init__() line 389
Severity: Blocks AutoModel.from_pretrained() — the standard HuggingFace loading API


Minimal Reproducible Example

from transformers import AutoModel

model = AutoModel.from_pretrained(
    "OpenGVLab/VideoMAEv2-Base",
    trust_remote_code=True
)

Full Traceback

RuntimeError: Tensor.item() cannot be called on meta tensors
  File "modeling_videomaev2.py", line 389, in __init__
    dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)]

Root Cause

Hugging Face Transformers wraps every model constructor inside torch.device("meta") for memory-efficient loading:

with torch.device("meta"):
    model = cls(config)  # ← VisionTransformer.__init__() runs here

A meta tensor has shape and dtype but no actual data values. Line 389 calls .item() to extract a scalar — which is impossible on a meta tensor.

Call Meta context? Result
VideoMAEv2(config) ❌ No ✅ Works
AutoModel.from_pretrained(...) ✅ Yes ❌ Crashes

Proposed Fix

- dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)]
+ # Meta-safe: no PyTorch tensor needed here
+ if depth > 1:
+     dpr = [drop_path_rate * i / (depth - 1) for i in range(depth)]
+ else:
+     dpr = [0.0] * depth

Produces numerically identical values. No tensor operations during construction.


Environment

Item Version
transformers latest
torch 2.x
OS Any

Happy to open a PR with this fix if maintainers confirm the approach.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions