[BUG] `PatchEmbed` returns a view tensor, triggering an FSDP2 warning
Author: sjiang95Created Sep 9, 2026Updated Sep 16, 2026
Labelsbug
Describe the bug
/usr/local/lib/python3.12/dist-packages/torch/distributed/fsdp/_fully_shard/_fsdp_state.py:336: UserWarning: FSDP2-wrapped module (FSDPPatchEmbed) returned a view tensor. An in-place op on this view (e.g., `x += y`) will silently drop the pre-backward hook and skip the all-gather, which can cause backward to fail or produce wrong gradients. Use out-of-place ops (`out = out + y`, not `out += y`) or `.clone()` the output before any in-place op.
output = self._register_pre_backward_hook(output)This warning was introduced to pytorch in https://github.com/pytorch/pytorch/pull/181850
To Reproduce Steps to reproduce the behavior:
- install torch 2.14.0+cu126
- save the following to
timmpe_fsdp2warn.py
import torch
import torch.distributed as dist
from torch.distributed.fsdp import fully_shard
from timm.layers import PatchEmbed
deviceid = torch.cuda.current_device()
device = torch.device(f"cuda:{deviceid}")
backend = torch.distributed.get_default_backend_for_device(device)
dist.init_process_group(
backend=backend,
init_method="env://",
device_id=device,
)
pe = PatchEmbed()
pe.cuda()
fully_shard(pe)
x = torch.randn(2, 3, 224, 224, device="cuda")
out = pe(x)- run
$ torchrun --standalone --nproc-per-node gpu timmpe_fsdp2warn.py
/usr/local/lib/python3.12/dist-packages/torch/distributed/fsdp/_fully_shard/_fsdp_state.py:336: UserWarning: FSDP2-wrapped module (FSDPPatchEmbed) returned a view tensor. An in-place op on this view (e.g., `x += y`) will silently drop the pre-backward hook and skip the all-gather, which can cause backward to fail or produce wrong gradients. Use out-of-place ops (`out = out + y`, not `out += y`) or `.clone()` the output before any in-place op.
output = self._register_pre_backward_hook(output)Expected behavior Avoid the warning.
Should not return a view tensor
Desktop (please complete the following information):
- OS: Ubuntu 22.04
- timm v1.0.29
- torch==2.14.0+cu126
Source: huggingface/pytorch-image-models