pytorch - ✅(Solved) Fix [dynamo] Support creation of pydantic.dataclass inside the compiled region [1 pull requests, 1 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#177986Fetched 2026-04-08 01:07:42
View on GitHub
Comments
0
Participants
1
Timeline
69
Reactions
0
Participants
Timeline (top)
mentioned ×28subscribed ×28labeled ×6referenced ×5

Error Message

def test_pydantic_dataclass_construction(self): try: import pydantic import pydantic.dataclasses except ImportError: raise unittest.SkipTest("pydantic not available")

    @pydantic.dataclasses.dataclass(config=pydantic.ConfigDict(arbitrary_types_allowed=True))
    class Point:
        x: torch.Tensor
        y: torch.Tensor

    # Pydantic dataclass constructed inside the compiled function.
    # The constructor runs validation, which Dynamo must handle without
    # graph-breaking on the tensor arithmetic that follows.
    def fn(x, y):
        p = Point(x=x, y=y)
        return p.x + p.y

    x, y = torch.ones(3), torch.ones(3) * 2
    expected = fn(x, y)

    fn_opt = torch.compile(fn, backend="eager")
    actual = fn_opt(x, y)
    self.assertEqual(actual, expected)

Fix Action

Fixed

PR fix notes

PR #178033: [dynamo] Graph break pydantic dataclass construction

Description (problem / solution / changelog)

Fix #177986

Summary

  1. Root cause Pydantic dataclass constructors delegate field population to an external validator call. Dynamo traced into the constructor, but could not observe the validator's instance mutations, so the validated object was not soundly available for later attribute access.

  2. Proposed fix Detect pydantic dataclass classes by their marker attribute and graph break at the constructor call instead of tracing into it. Add a regression test that simulates pydantic's marker plus external-validator construction pattern and checks that the following tensor add still compiles.

  3. Why the proposed fix is the right long term fix The validator is an opaque eager boundary that mutates self, so keeping the constructor eager is the sound behavior. This preserves pydantic validation semantics while still letting Dynamo resume tracing the tensor work that follows.

Drafted via Codex, published after manual review by @bobrenjc93

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

Changed files

  • test/dynamo/test_functions.py (modified, +42/-1)
  • torch/_dynamo/graph_break_registry.json (modified, +8/-0)
  • torch/_dynamo/variables/user_defined.py (modified, +18/-0)

Code Example

def test_pydantic_dataclass_construction(self):
        try:
            import pydantic
            import pydantic.dataclasses
        except ImportError:
            raise unittest.SkipTest("pydantic not available")

        @pydantic.dataclasses.dataclass(config=pydantic.ConfigDict(arbitrary_types_allowed=True))
        class Point:
            x: torch.Tensor
            y: torch.Tensor

        # Pydantic dataclass constructed inside the compiled function.
        # The constructor runs validation, which Dynamo must handle without
        # graph-breaking on the tensor arithmetic that follows.
        def fn(x, y):
            p = Point(x=x, y=y)
            return p.x + p.y

        x, y = torch.ones(3), torch.ones(3) * 2
        expected = fn(x, y)

        fn_opt = torch.compile(fn, backend="eager")
        actual = fn_opt(x, y)
        self.assertEqual(actual, expected)
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

    def test_pydantic_dataclass_construction(self):
        try:
            import pydantic
            import pydantic.dataclasses
        except ImportError:
            raise unittest.SkipTest("pydantic not available")

        @pydantic.dataclasses.dataclass(config=pydantic.ConfigDict(arbitrary_types_allowed=True))
        class Point:
            x: torch.Tensor
            y: torch.Tensor

        # Pydantic dataclass constructed inside the compiled function.
        # The constructor runs validation, which Dynamo must handle without
        # graph-breaking on the tensor arithmetic that follows.
        def fn(x, y):
            p = Point(x=x, y=y)
            return p.x + p.y

        x, y = torch.ones(3), torch.ones(3) * 2
        expected = fn(x, y)

        fn_opt = torch.compile(fn, backend="eager")
        actual = fn_opt(x, y)
        self.assertEqual(actual, expected)

Error logs

No response

Versions

NA

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

extent analysis

Fix Plan

The fix involves modifying the fn function to avoid constructing the Pydantic dataclass inside the compiled function.

  • Define the Pydantic dataclass outside the compiled function.
  • Modify the fn function to accept the Pydantic dataclass instance as an argument.

Example Code

import torch
import pydantic
import pydantic.dataclasses
import unittest

@pydantic.dataclasses.dataclass(config=pydantic.ConfigDict(arbitrary_types_allowed=True))
class Point:
    x: torch.Tensor
    y: torch.Tensor

def fn(point):
    return point.x + point.y

class TestPydanticDataclassConstruction(unittest.TestCase):
    def test_pydantic_dataclass_construction(self):
        try:
            import pydantic
            import pydantic.dataclasses
        except ImportError:
            raise unittest.SkipTest("pydantic not available")

        x, y = torch.ones(3), torch.ones(3) * 2
        point = Point(x=x, y=y)
        expected = fn(point)

        fn_opt = torch.compile(fn, backend="eager")
        actual = fn_opt(point)
        self.assertEqual(actual, expected)

if __name__ == '__main__':
    unittest.main()

Verification

Run the test case test_pydantic_dataclass_construction to verify that the fix works.

Extra Tips

  • Ensure that the Pydantic dataclass is defined outside the compiled function to avoid graph-breaking issues.
  • Use the torch.compile function with the backend="eager" argument to compile the function with eager execution.

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 [dynamo] Support creation of pydantic.dataclass inside the compiled region [1 pull requests, 1 participants]