pytorch - ✅(Solved) Fix `copy.deepcopy` of `LeafSpec` fails with AttributeError: 'LeafSpec' object has no attribute 'type' on Python 3.10.0 [1 pull requests, 1 comments, 2 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#177045Fetched 2026-04-08 00:22:33
View on GitHub
Comments
1
Participants
2
Timeline
48
Reactions
3
Timeline (top)
mentioned ×14subscribed ×14labeled ×11cross-referenced ×2

Calling copy.deepcopy on any pytree TreeSpec that contains LeafSpec nodes fails with:

AttributeError: 'LeafSpec' object has no attribute 'type'

This breaks ExportedProgram.run_decompositions() (and to_edge_transform_and_lower() in ExecuTorch), which internally deepcopies the module call graph containing TreeSpec objects.

The issue was introduced when LeafSpec was converted to a frozen=True, slots=True dataclass subclass of TreeSpec.

https://github.com/pytorch/pytorch/pull/172172

Error Message

AttributeError: 'LeafSpec' object has no attribute 'type'

Root Cause

LeafSpec in torch/utils/_pytree.py is defined as: https://github.com/pytorch/pytorch/blob/main/torch/utils/_pytree.py#L1340-L1353

Because all fields have init=False, the generated __init__ does not call object.setattr(self, "type", None) or object.setattr(self, "_context", None). On Python 3.10.0 with frozen=True + slots=True, simple defaults (default=None) for init=False fields are not assigned to the slot — unlike default_factory fields (e.g., _children gets set to [] correctly).

As a result, the type and _context slots are never populated. Normal attribute access like spec.type raises AttributeError.

When copy.deepcopy is called, it invokes _dataclass_getstate, which does:

def _dataclass_getstate(self):
    return [getattr(self, f.name) for f in fields(self)]

This calls getattr(self, 'type') on a LeafSpec whose type slot was never set → AttributeError.

The parent TreeSpec does not have this problem because it has a custom init(init=False on the dataclass decorator) that explicitly calls object.setattr(self, "type", type).

Fix Action

Fix / Workaround

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian Address sizes: 46 bits physical, 48 bits virtual CPU(s): 36 On-line CPU(s) list: 0-35 Thread(s) per core: 2 Core(s) per socket: 18 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 85 Model name: Intel(R) Core(TM) i9-10980XE CPU @ 3.00GHz Stepping: 7 CPU MHz: 3000.000 CPU max MHz: 4800,0000 CPU min MHz: 1200,0000 BogoMIPS: 6000.00 Virtualization: VT-x L1d cache: 576 KiB L1i cache: 576 KiB L2 cache: 18 MiB L3 cache: 24,8 MiB NUMA node0 CPU(s): 0-35 Vulnerability Gather data sampling: Mitigation; Microcode Vulnerability Itlb multihit: KVM: Mitigation: VMX disabled Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Mitigation; Enhanced IBRS 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 SW loop, KVM SW loop Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Mitigation; TSX disabled 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 pni pclmulqdq dtes64 monitor ds_cpl vmx 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 cdp_l3 invpcid_single 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 mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512_vnni md_clear flush_l1d arch_capabilities

PR fix notes

PR #172172: [BE][Ez]: Add slots to treespec dataclasses

Description (problem / solution / changelog)

Accelerate dataclasses using 3.10 dataclass slots kwargs

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @Lucaskabela @jataylo

Changed files

  • torch/_dynamo/polyfills/pytree.py (modified, +1/-1)
  • torch/utils/_pytree.py (modified, +2/-2)

Code Example

AttributeError: 'LeafSpec' object has no attribute 'type'

---

def _dataclass_getstate(self):
    return [getattr(self, f.name) for f in fields(self)]

---

"""
Reproducer for: AttributeError: 'LeafSpec' object has no attribute 'type'

Exporting a simple model and calling run_decompositions() crashes with
'LeafSpec' object has no attribute 'type' on Python 3.10 + PyTorch nightly.

Bug: LeafSpec (frozen+slots dataclass) never initializes the 'type' and
'_context' slots, so copy.deepcopy() inside run_decompositions() fails.
"""

import torch
import torch.nn as nn


class SimpleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(10, 5)

    def forward(self, x):
        return self.linear(x)


model = SimpleModel()
example_input = (torch.randn(1, 10),)

# Step 1: Export — works fine
print("Exporting model...")
exported = torch.export.export(model, example_input)
print("Export OK")

# Step 2: run_decompositions — crashes
print("Running decompositions...")
decomposed = exported.run_decompositions({})
# AttributeError: 'LeafSpec' object has no attribute 'type'

---

import copy
import torch.utils._pytree as pytree

# Direct reproduction
spec = pytree.treespec_leaf()
print(f"spec.is_leaf() = {spec.is_leaf()}")  # True

# Check that the slot is not set
for slot in ('type', '_context', '_children'):
    try:
        val = getattr(spec, slot)
        print(f"  {slot} = {val!r}")
    except AttributeError:
        print(f"  {slot} = <NOT SET>")  # type and _context are NOT SET

# This fails
copy.deepcopy(spec)  # AttributeError: 'LeafSpec' object has no attribute 'type'

---

def __post_init__(self) -> None:
    object.__setattr__(self, "type", None)
    object.__setattr__(self, "_context", None)
    object.__setattr__(self, "num_nodes", 1)
    object.__setattr__(self, "num_leaves", 1)
    object.__setattr__(self, "num_children", 0)
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Found with the Claude Opus 4.6

Description

Calling copy.deepcopy on any pytree TreeSpec that contains LeafSpec nodes fails with:

AttributeError: 'LeafSpec' object has no attribute 'type'

This breaks ExportedProgram.run_decompositions() (and to_edge_transform_and_lower() in ExecuTorch), which internally deepcopies the module call graph containing TreeSpec objects.

The issue was introduced when LeafSpec was converted to a frozen=True, slots=True dataclass subclass of TreeSpec.

https://github.com/pytorch/pytorch/pull/172172

Root Cause

LeafSpec in torch/utils/_pytree.py is defined as: https://github.com/pytorch/pytorch/blob/main/torch/utils/_pytree.py#L1340-L1353

Because all fields have init=False, the generated __init__ does not call object.setattr(self, "type", None) or object.setattr(self, "_context", None). On Python 3.10.0 with frozen=True + slots=True, simple defaults (default=None) for init=False fields are not assigned to the slot — unlike default_factory fields (e.g., _children gets set to [] correctly).

As a result, the type and _context slots are never populated. Normal attribute access like spec.type raises AttributeError.

When copy.deepcopy is called, it invokes _dataclass_getstate, which does:

def _dataclass_getstate(self):
    return [getattr(self, f.name) for f in fields(self)]

This calls getattr(self, 'type') on a LeafSpec whose type slot was never set → AttributeError.

The parent TreeSpec does not have this problem because it has a custom init(init=False on the dataclass decorator) that explicitly calls object.setattr(self, "type", type).

Reproducer

"""
Reproducer for: AttributeError: 'LeafSpec' object has no attribute 'type'

Exporting a simple model and calling run_decompositions() crashes with
'LeafSpec' object has no attribute 'type' on Python 3.10 + PyTorch nightly.

Bug: LeafSpec (frozen+slots dataclass) never initializes the 'type' and
'_context' slots, so copy.deepcopy() inside run_decompositions() fails.
"""

import torch
import torch.nn as nn


class SimpleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(10, 5)

    def forward(self, x):
        return self.linear(x)


model = SimpleModel()
example_input = (torch.randn(1, 10),)

# Step 1: Export — works fine
print("Exporting model...")
exported = torch.export.export(model, example_input)
print("Export OK")

# Step 2: run_decompositions — crashes
print("Running decompositions...")
decomposed = exported.run_decompositions({})
# AttributeError: 'LeafSpec' object has no attribute 'type'

Reproducer2:

import copy
import torch.utils._pytree as pytree

# Direct reproduction
spec = pytree.treespec_leaf()
print(f"spec.is_leaf() = {spec.is_leaf()}")  # True

# Check that the slot is not set
for slot in ('type', '_context', '_children'):
    try:
        val = getattr(spec, slot)
        print(f"  {slot} = {val!r}")
    except AttributeError:
        print(f"  {slot} = <NOT SET>")  # type and _context are NOT SET

# This fails
copy.deepcopy(spec)  # AttributeError: 'LeafSpec' object has no attribute 'type'

Environment: Python 3.10.0, PyTorch 2.11.0.dev20260215 (nightly). Also reported in intel/torch-xpu-ops#2843

Suggested Fix

Override __post_init__ in LeafSpec to explicitly set the missing slots:

def __post_init__(self) -> None:
    object.__setattr__(self, "type", None)
    object.__setattr__(self, "_context", None)
    object.__setattr__(self, "num_nodes", 1)
    object.__setattr__(self, "num_leaves", 1)
    object.__setattr__(self, "num_children", 0)

This mirrors how TreeSpec.__init__ already uses object.setattr to populate all slots explicitly.

PS: Could not reproduce with python3.10.6 https://colab.research.google.com/drive/1Nj1_IDr3Me3g1H9SwK0RezqiZSQbZ8uU?usp=sharing

CC: @XuehaiPan, @Lucaskabela

Versions

Collecting environment information... PyTorch version: 2.11.0.dev20260215+cpu Is debug build: False CUDA used to build PyTorch: Could not collect ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.6 LTS (x86_64) GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0 Clang version: Could not collect CMake version: version 3.31.10 Libc version: glibc-2.31

Python version: 3.10.0 (default, Mar 28 2024, 12:01:13) [GCC 9.4.0] (64-bit runtime) Python platform: Linux-5.15.0-139-generic-x86_64-with-glibc2.31 Is CUDA available: False CUDA runtime version: 12.4.131 CUDA_MODULE_LOADING set to: N/A GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090 GPU 1: NVIDIA GeForce RTX 3090 GPU 2: NVIDIA GeForce RTX 3090

Nvidia driver version: 575.57.08 cuDNN version: Probably one of the following: /usr/lib/x86_64-linux-gnu/libcudnn.so.9.10.2 /usr/lib/x86_64-linux-gnu/libcudnn_adv.so.9.10.2 /usr/lib/x86_64-linux-gnu/libcudnn_cnn.so.9.10.2 /usr/lib/x86_64-linux-gnu/libcudnn_engines_precompiled.so.9.10.2 /usr/lib/x86_64-linux-gnu/libcudnn_engines_runtime_compiled.so.9.10.2 /usr/lib/x86_64-linux-gnu/libcudnn_graph.so.9.10.2 /usr/lib/x86_64-linux-gnu/libcudnn_heuristic.so.9.10.2 /usr/lib/x86_64-linux-gnu/libcudnn_ops.so.9.10.2 Is XPU available: False HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True Caching allocator config: N/A

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian Address sizes: 46 bits physical, 48 bits virtual CPU(s): 36 On-line CPU(s) list: 0-35 Thread(s) per core: 2 Core(s) per socket: 18 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 85 Model name: Intel(R) Core(TM) i9-10980XE CPU @ 3.00GHz Stepping: 7 CPU MHz: 3000.000 CPU max MHz: 4800,0000 CPU min MHz: 1200,0000 BogoMIPS: 6000.00 Virtualization: VT-x L1d cache: 576 KiB L1i cache: 576 KiB L2 cache: 18 MiB L3 cache: 24,8 MiB NUMA node0 CPU(s): 0-35 Vulnerability Gather data sampling: Mitigation; Microcode Vulnerability Itlb multihit: KVM: Mitigation: VMX disabled Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Mitigation; Enhanced IBRS 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 SW loop, KVM SW loop Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Mitigation; TSX disabled 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 pni pclmulqdq dtes64 monitor ds_cpl vmx 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 cdp_l3 invpcid_single 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 mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512_vnni md_clear flush_l1d arch_capabilities

Versions of relevant libraries: [pip3] executorch==1.2.0a0+165d1ec [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.5 [pip3] nvidia-nvjitlink-cu12==12.8.93 [pip3] nvidia-nvtx-cu12==12.8.90 [pip3] pytorch_tokenizers==1.0.1 [pip3] torch==2.11.0.dev20260215+cpu [pip3] torchao==0.16.0+git026b76d12 [pip3] torchaudio==2.11.0.dev20260215+cpu [pip3] torchdata==0.11.0 [pip3] torchsr==1.0.4 [pip3] torchtune==0.0.0 [pip3] torchvision==0.26.0.dev20260215+cpu [pip3] triton==3.6.0+git9844da95 [conda] Could not collect

cc @mruberry @mikaylagawarecki @zou3519 @XuehaiPan @chauhang @penguinwu @avikchaudhuri @zhxchen17 @tugsbayasgalan @angelayi @suo @ydwu4

extent analysis

Fix Plan

Override __post_init__ in LeafSpec

To fix the issue, we need to override the __post_init__ method in LeafSpec to explicitly set the missing slots.

from dataclasses import dataclass, field
from typing import Any

@dataclass(frozen=True, slots=True)
class LeafSpec:
    # ... existing fields ...

    def __post_init__(self) -> None:
        object.__setattr__(self, "type", None)
        object.__setattr__(self, "_context", None)
        object.__setattr__(self, "num_nodes", 1)
        object.__setattr__(self, "num_leaves", 1)
        object.__setattr__(self, "num_children", 0)

Update TreeSpec to use __post_init__

We also need to update TreeSpec to use __post_init__ to set its slots.

from dataclasses import dataclass, field
from typing import Any

@dataclass(frozen=True, slots=True)
class TreeSpec:
    # ... existing fields ...

    def __post_init__(self) -> None:
        object.__setattr__(self, "type", None)
        object.__setattr__(self, "_context", None)
        # ... other slots ...

Verify the fix

To verify that the fix works, you can run the reproducer code again and check that it no longer crashes with an AttributeError.

import torch
import torch.nn as nn
import torch.utils._pytree as pytree

# ... reproducer code ...

# Step 2: run_decompositions — should not crash
print("Running decompositions...")
decomposed = exported.run_decompositions({})

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 `copy.deepcopy` of `LeafSpec` fails with AttributeError: 'LeafSpec' object has no attribute 'type' on Python 3.10.0 [1 pull requests, 1 comments, 2 participants]