Skip to content

Commit 16e8323

Browse files
anijain2305meta-codesync[bot]
authored andcommitted
Add tp_descr_get_impl infrastructure for descriptor protocol (#182213)
Summary: Add infrastructure for modeling CPython's descriptor protocol in Dynamo: - base.py: Route __get__ calls to tp_descr_get_impl when the VT implements it, mirroring CPython's slot_tp_descr_get. - utils.py: Add is_torch_class helper to identify torch-internal classes whose C-level descriptors should go through trace_rules instead of descriptor VTs. Authored with Claude. X-link: pytorch/pytorch#182213 Approved by: https://github.com/guilhermeleobas Reviewed By: huydhn Differential Revision: D104188384 fbshipit-source-id: 3f9441312d389ece2d77c31beec8a9b05c35a1db
1 parent 512df34 commit 16e8323

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

  • userbenchmark/dynamo/dynamobench/_dynamo

userbenchmark/dynamo/dynamobench/_dynamo/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4974,6 +4974,19 @@ def is_tensor_getset_descriptor(name: str) -> bool:
49744974
return False
49754975

49764976

4977+
def is_torch_class(cls: type) -> bool:
4978+
"""Check if cls is defined in torch or a torch submodule.
4979+
4980+
Torch-internal C-level descriptors (e.g. torch.Tensor.unsqueeze_) need
4981+
to go through VariableTracker.build / trace_rules so they get the right
4982+
VT (TorchInGraphFunctionVariable), which has guards like
4983+
inplace-view-on-input-tensor detection. This helper identifies classes
4984+
whose descriptors should take that path instead of descriptor VTs.
4985+
"""
4986+
module = getattr(cls, "__module__", None)
4987+
return module is not None and (module == "torch" or module.startswith("torch."))
4988+
4989+
49774990
def is_torch_function_object(value: Any) -> bool:
49784991
return hasattr(value, "__torch_function__")
49794992

0 commit comments

Comments
 (0)