Skip to content

No guards for frozen dataclasses and readonly configs #138226

@vmoens

Description

@vmoens

🚀 The feature, motivation and pitch

Currently, a guard will be put for a frozen dataclass:

TORCH_LOGS="+guards" python -c """from dataclasses import dataclass
import torch

@dataclass(frozen=True)
class MyDC:
    x: int

@torch.compile(fullgraph=True)
def func(dc: MyDC, t):
    return dc.x + t

func(MyDC(0), torch.randn(()))
"""

gives a guard for

V1017 16:30:38.479000 75647 torch/_dynamo/guards.py:2314] [0/0] [__guards] | | +- GuardManager: source=L['dc'].x, accessed_by=GetAttrGuardAccessor(x)
V1017 16:30:38.479000 75647 torch/_dynamo/guards.py:2314] [0/0] [__guards] | | | +- EQUALS_MATCH: L['dc'].x == 0                                                #   # <string>:10 in func

This is unnecessary as dc.x cannot be changed (dc can be guarded but dc.x is frozen).

The same would apply for readonly configs from omegaconf (when #138224 is fixed, this code will show guards for x):

import torch
from omegaconf import DictConfig

@torch.compile(fullgraph=True) # this will break currently
def func(dc, t):
    return dc.x + t

cfg = DictConfig({'x': 0}, flags={'readonly': True})
func(cfg, torch.randn(()))

Alternatives

I guess someone could hack their way through a frozen dataclass and that could lead to some errors but is it that much of a risk?

Additional context

No response

cc @ezyang @chauhang @penguinwu @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @amjames @rec

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions