pytorch - ✅(Solved) Fix nn.Parameter creation in FSDPParam crashes for non floating point tensors [1 pull requests, 3 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#177844Fetched 2026-04-08 01:03:13
View on GitHub
Comments
3
Participants
3
Timeline
78
Reactions
0
Author
Assignees
Timeline (top)
mentioned ×28subscribed ×28referenced ×8labeled ×6

Error Message

RuntimeError: only Tensors of floating point dtype can require gradients

Fix Action

Fix / Workaround

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 52 bits physical, 57 bits virtual Byte Order: Little Endian CPU(s): 64 On-line CPU(s) list: 0-63 Vendor ID: GenuineIntel Model name: Intel(R) Xeon(R) Gold 6444Y CPU family: 6 Model: 143 Thread(s) per core: 2 Core(s) per socket: 16 Socket(s): 2 Stepping: 8 CPU max MHz: 4000.0000 CPU min MHz: 800.0000 BogoMIPS: 7200.00 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cat_l2 cdp_l3 invpcid_single intel_ppin cdp_l2 ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect avx_vnni avx512_bf16 wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b enqcmd fsrm md_clear serialize tsxldtrk pconfig arch_lbr amx_bf16 avx512_fp16 amx_tile amx_int8 flush_l1d arch_capabilities Virtualization: VT-x L1d cache: 1.5 MiB (32 instances) L1i cache: 1 MiB (32 instances) L2 cache: 64 MiB (32 instances) L3 cache: 90 MiB (2 instances) NUMA node(s): 2 NUMA node0 CPU(s): 0-15,32-47 NUMA node1 CPU(s): 16-31,48-63 Vulnerability Gather data sampling: Not affected Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Not affected Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Not affected Vulnerability Spec rstack overflow: Not affected Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Not affected

PR fix notes

PR #177948: [FSDP2] support non-float parameters

Description (problem / solution / changelog)

fixes: https://github.com/pytorch/pytorch/issues/177844

resolving two issues

  • requires_grad errors: nn.Parameter() construction didn't propagate requires_grad=False
  • incorrect mixed precision: mp_policy.param_dtype would attempt to cast non-float to float, eg int8 to bfloat16

How all-gather and reduce-scatter work with non-float

  • All-gather: Non-float params are sharded and included in the all-gather just like float params. AG buffer is uint8 bytes if we mix float with non-float params
  • Reduce-scatter: Non-float params are simply excluded

Stack from ghstack (oldest at bottom):

  • -> #177948

Co-authored-by: roycho96 [email protected]

Changed files

  • test/distributed/_composable/fsdp/test_fully_shard_init.py (modified, +107/-1)
  • torch/distributed/fsdp/_fully_shard/_fsdp_param.py (modified, +10/-5)

Code Example

RuntimeError: only Tensors of floating point dtype can require gradients

---

# Line 501 (correct)
self._unsharded_param = nn.Parameter(
    unsharded_param, requires_grad=self.sharded_param.requires_grad
)

# Line 381 (crashes before line 382 fixup)
self.sharded_param = nn.Parameter(self.to_sharded_dtensor(sharded_param))
self.sharded_param.requires_grad_(param.requires_grad)

# Line 546 (no fixup at all)
self._sharded_post_forward_param = nn.Parameter(
    self.to_sharded_post_forward_dtensor(sharded_post_forward_tensor)
)

---

# Line 381
self.sharded_param = nn.Parameter(
    self.to_sharded_dtensor(sharded_param),
    requires_grad=param.requires_grad,
)

# Line 546
self._sharded_post_forward_param = nn.Parameter(
    self.to_sharded_post_forward_dtensor(sharded_post_forward_tensor),
    requires_grad=self.sharded_param.requires_grad,
)
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

FSDPParam._init_sharded_param() (line 381) and to_sharded_post_forward() (line 546) in torch/distributed/fsdp/_fully_shard/_fsdp_param.py create nn.Parameter without passing requires_grad, defaulting to True. For non-floating-point tensors (e.g. uint8), this crashes:

RuntimeError: only Tensors of floating point dtype can require gradients

The intended fix is already present on line 382 and line 501, already correctly passes requires_grad:

# Line 501 (correct)
self._unsharded_param = nn.Parameter(
    unsharded_param, requires_grad=self.sharded_param.requires_grad
)

# Line 381 (crashes before line 382 fixup)
self.sharded_param = nn.Parameter(self.to_sharded_dtensor(sharded_param))
self.sharded_param.requires_grad_(param.requires_grad)

# Line 546 (no fixup at all)
self._sharded_post_forward_param = nn.Parameter(
    self.to_sharded_post_forward_dtensor(sharded_post_forward_tensor)
)

The fix is to pass requires_grad at creation time, consistent with line 501:

# Line 381
self.sharded_param = nn.Parameter(
    self.to_sharded_dtensor(sharded_param),
    requires_grad=param.requires_grad,
)

# Line 546
self._sharded_post_forward_param = nn.Parameter(
    self.to_sharded_post_forward_dtensor(sharded_post_forward_tensor),
    requires_grad=self.sharded_param.requires_grad,
)

Versions

PyTorch version: 2.8.0+cu128 Is debug build: False CUDA used to build PyTorch: 12.8 ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.5 LTS (x86_64) GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04.2) 11.4.0 Clang version: Could not collect CMake version: version 4.2.1 Libc version: glibc-2.35

Python version: 3.11.14 (main, Oct 10 2025, 08:54:03) [GCC 11.4.0] (64-bit runtime) Python platform: Linux-5.15.0-130-generic-x86_64-with-glibc2.35 Is CUDA available: True CUDA runtime version: Could not collect CUDA_MODULE_LOADING set to: LAZY GPU models and configuration: GPU 0: NVIDIA H100 PCIe Nvidia driver version: 550.54.15 cuDNN version: Probably one of the following: /usr/lib/x86_64-linux-gnu/libcudnn.so.9.7.0 /usr/lib/x86_64-linux-gnu/libcudnn_adv.so.9.7.0 /usr/lib/x86_64-linux-gnu/libcudnn_cnn.so.9.7.0 /usr/lib/x86_64-linux-gnu/libcudnn_engines_precompiled.so.9.7.0 /usr/lib/x86_64-linux-gnu/libcudnn_engines_runtime_compiled.so.9.7.0 /usr/lib/x86_64-linux-gnu/libcudnn_graph.so.9.7.0 /usr/lib/x86_64-linux-gnu/libcudnn_heuristic.so.9.7.0 /usr/lib/x86_64-linux-gnu/libcudnn_ops.so.9.7.0 HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 52 bits physical, 57 bits virtual Byte Order: Little Endian CPU(s): 64 On-line CPU(s) list: 0-63 Vendor ID: GenuineIntel Model name: Intel(R) Xeon(R) Gold 6444Y CPU family: 6 Model: 143 Thread(s) per core: 2 Core(s) per socket: 16 Socket(s): 2 Stepping: 8 CPU max MHz: 4000.0000 CPU min MHz: 800.0000 BogoMIPS: 7200.00 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cat_l2 cdp_l3 invpcid_single intel_ppin cdp_l2 ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect avx_vnni avx512_bf16 wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b enqcmd fsrm md_clear serialize tsxldtrk pconfig arch_lbr amx_bf16 avx512_fp16 amx_tile amx_int8 flush_l1d arch_capabilities Virtualization: VT-x L1d cache: 1.5 MiB (32 instances) L1i cache: 1 MiB (32 instances) L2 cache: 64 MiB (32 instances) L3 cache: 90 MiB (2 instances) NUMA node(s): 2 NUMA node0 CPU(s): 0-15,32-47 NUMA node1 CPU(s): 16-31,48-63 Vulnerability Gather data sampling: Not affected Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Not affected Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Not affected Vulnerability Spec rstack overflow: Not affected Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Not affected

Versions of relevant libraries: [pip3] numpy==2.2.6 [pip3] nvidia-cublas-cu12==12.8.4.1 [pip3] nvidia-cuda-cupti-cu12==12.8.90 [pip3] nvidia-cuda-nvrtc-cu12==12.8.93 [pip3] nvidia-cuda-runtime-cu12==12.8.90 [pip3] nvidia-cudnn-cu12==9.10.2.21 [pip3] nvidia-cufft-cu12==11.3.3.83 [pip3] nvidia-curand-cu12==10.3.9.90 [pip3] nvidia-cusolver-cu12==11.7.3.90 [pip3] nvidia-cusparse-cu12==12.5.8.93 [pip3] nvidia-cusparselt-cu12==0.7.1 [pip3] nvidia-nccl-cu12==2.27.3 [pip3] nvidia-nvjitlink-cu12==12.8.93 [pip3] nvidia-nvtx-cu12==12.8.90 [pip3] torch==2.8.0+cu128 [pip3] torch-optimi==0.2.1 [pip3] torchao==0.13.0 [pip3] torchvision==0.23.0+cu128 [pip3] triton==3.4.0 [conda] Could not collect

cc @ezyang @gchanan @kadeng @msaroufim @awgu @wanchaol @fegin @fduwjj @wz337 @wconstab @d4l3k @pragupta @dcci @aditvenk @xmfan @albanD @gqchen @nikitaved @soulitzer @Varal7 @bobrenjc93 @zhaojuanmao @mrshenli @rohan-varma @chauhang @mori360 @ppwwyyxx

extent analysis

Fix Plan

To fix the issue, we need to pass requires_grad when creating nn.Parameter instances. Here are the steps:

  • Update the FSDPParam._init_sharded_param() method to pass requires_grad:
self.sharded_param = nn.Parameter(
    self.to_sharded_dtensor(sharded_param),
    requires_grad=param.requires_grad,
)
  • Update the to_sharded_post_forward() method to pass requires_grad:
self._sharded_post_forward_param = nn.Parameter(
    self.to_sharded_post_forward_dtensor(sharded_post_forward_tensor),
    requires_grad=self.sharded_param.requires_grad,
)

Verification

To verify that the fix worked, you can test the code with non-floating-point tensors (e.g., uint8) and check that it no longer crashes with the RuntimeError.

Extra Tips

  • Make sure to test the code with different tensor types and requires_grad values to ensure that the fix is working correctly.
  • Consider adding a test case to the PyTorch test suite to prevent regressions in the future.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - ✅(Solved) Fix nn.Parameter creation in FSDPParam crashes for non floating point tensors [1 pull requests, 3 comments, 3 participants]