forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_opaque_base.py
More file actions
38 lines (29 loc) · 1.46 KB
/
Copy path_opaque_base.py
File metadata and controls
38 lines (29 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Cached lazily on first __instancecheck__ miss to avoid an import cycle at
# module load (FakeScriptObject's module imports torch, which imports us).
_FakeScriptObject_cls: type | None = None
class OpaqueBaseMeta(type):
def __instancecheck__(cls, instance):
# When checking against OpaqueBase itself (not a concrete subclass),
# delegate to the registration system which correctly covers all
# opaque types (value types, metaclass-only reference types, and
# FakeScriptObject wrappers).
if cls is OpaqueBase:
from torch._library.opaque_object import is_opaque_value
return is_opaque_value(instance)
if super().__instancecheck__(instance):
return True
# Check FakeScriptObject before hasattr to avoid triggering custom
# __getattr__ on arbitrary user objects (e.g. dict-like objects that
# raise KeyError on unknown attributes).
# e.g. test/dynamo/test_dynamic_shapes.py -k test_user_getattr1_dynamic_shapes
global _FakeScriptObject_cls
if _FakeScriptObject_cls is None:
from torch._library.fake_class_registry import FakeScriptObject
_FakeScriptObject_cls = FakeScriptObject
if isinstance(instance, _FakeScriptObject_cls) and hasattr(
instance, "real_obj"
):
return super().__instancecheck__(instance.real_obj)
return False
class OpaqueBase(metaclass=OpaqueBaseMeta):
pass