fix: compute stochastic-depth rates in pure Python for transformers 5.x meta-device init

#4
H2O.ai org

Fix Tensor.item() cannot be called on meta tensors under transformers 5.x

InternVisionEncoder.__init__ computes the stochastic-depth decay rule with
torch.linspace(...).item(). As of transformers 5.x, from_pretrained runs
model __init__ inside a with torch.device("meta") context, so
torch.linspace(...) returns a meta tensor and .item() raises
RuntimeError: Tensor.item() cannot be called on meta tensors. This makes the
model fail to load via AutoModel.from_pretrained(..., trust_remote_code=True)
on transformers>=5.

This computes the identical decay values in pure Python (no device-dependent
tensor), so init works regardless of the ambient device context. The values
match torch.linspace(0, drop_path_rate, num_hidden_layers) to within float32
rounding (~5e-9), which is inconsequential for drop-path probabilities.

ilu000 changed pull request status to merged

Sign up or log in to comment