
    ȅiw!                        % S r SSKJr  SSKJrJrJr  SSKJr  SSK	r	SSK
Jr  \" S5       " S	 S
5      5       rSr\" S\	R                  R                  5      r\ " S S5      5       r\" 5       r\\\4   \S'   0 r\\\4   \S'   S\S\4S jrS\S\SS4S jrS\S\4S jrS\S\4S jrS\S\4S jrS\S\\\\\4   4   4S jrg)aM  
Note [Opaque Objects]

Opaque objects are the way we allow custom operators to accept a user-defined
"black box" object as an input.

There are two kinds of opaque types: VALUE type and REFERENCE type.
The distinction determines how torch.compile handles the object.

REFERENCE TYPES (default):

Reference-typed opaque objects represent mutable stateful objects and are
treated as black boxes. In torch.compile, since torch.compile cannot optimize
the anything (including tensors) within the object, the object must be an
input to the graph.

You can register a custom class as being a reference-based opaque object class
through `register_opaque_type(MyClass, typ="reference")`.

VALUE TYPES:

Value-typed opaque objects represent constant values.
In torch.compile, the graph specializes on the object like how other constants
are. Therefore there are a couple of methods on the class that must be
implemented before registering it as a value-typed opaque object class:
  - __eq__: torch.compile will create guards based on the equality of this
  object, meaning that a recompilation will happen if __eq__ returns False.
  - __hash__: This must be implemented for Fake Tensor caching
  - __fx_repr__: This must be implemented to provide an evaluable representation
    for FX graph codegen. It should return a tuple of (repr_string, dict[str, type])
    where repr_string can reconstruct the object and the dict maps names used in
    repr_string to their corresponding types.

You can register a custom class as being a reference-based opaque object class
through `register_opaque_type(MyClass, typ="value")`.
    )	dataclass)AnyLiteralNewType)WeakKeyDictionaryN   )register_fake_classzaten::OpaqueObjectc                   D    \ rS rSrSS jr\S\\\4   SS4S j5       r	Sr
g)	FakeOpaqueObject/   returnNc                     g )N )selfs    V/home/james-whalen/.local/lib/python3.13/site-packages/torch/_library/opaque_object.py__init__FakeOpaqueObject.__init__1   s        flattened_ctxc                     [        S5      e)NzFakeOpaqueObject should not be created through __obj_unflatten__ and should be special handled. Please file an issue to Github.)RuntimeError)clsr   s     r   __obj_unflatten__"FakeOpaqueObject.__obj_unflatten__4   s    M
 	
r   r   )r   N)__name__
__module____qualname____firstlineno__r   classmethoddictstrr   r   __static_attributes__r   r   r   r   r   /   s1     
d38n 
 
 
r   r   z)__torch__.torch.classes.aten.OpaqueObject
OpaqueTypec                   0    \ rS rSr% \\S'   \S   \S'   Srg)_OpaqueTypeInfoA   
class_name	referencevalue
opaque_typr   N)r   r   r   r   r!   __annotations__r   r"   r   r   r   r%   r%   A   s    O,--r   r%   _OPAQUE_TYPES_OPAQUE_TYPES_BY_NAMEr   r   c                 t    U [         ;  a  [        SU  SU R                   S35      e[         U    R                  $ )z
Gets the registered opaque type name for a given class.

Args:
    cls (type): The class to get the type name for.

Returns:
    str: The registered type name for the class.

Raises:
    ValueError: If the class is not registered as an opaque type.
zClass z@ is not registered as an opaque type. Call register_opaque_type(z) first.)r-   
ValueErrorr   r'   r   s    r   get_opaque_type_namer2   M   sI     -SE )),h@
 	
 (((r   typc                X   SSK Js  Jn  U R                  S:X  d  U [        R
                  L a  [        SU  S35      eXR                  ;   a  [        U  S35      eUS;   d   S5       eUS	:X  ah  U R                  [        R                  L a  [        S
U  S35      eU R                  c  [        S
U  S35      e[        U S5      (       d  [        S
U  S35      eU R                   SU R                   3n[        X15      nU[        U '   U[         U'   [        R"                  R%                  U5        g)as  
Registers the given type as an opaque type which allows this to be consumed
by a custom operator.

The type name will be automatically generated from the class's fully
qualified name (ex. my_module.MyClass).

Args:
    cls (type): The class to register as an opaque type.
    typ (str): Either "reference" or "value". See Note [Opaque Objects] for
        more details.
r   Nbuiltinsz!Unable to register built-in type z] as an opaque type. Please wrap it in a custom class and register the custom class as opaque.zv cannot be registered as an opaque object as it has been registered as a pytree. Opaque objects must be pytree leaves.r(   z1Opaque type must be either 'reference' or 'value'r*   !Value-type opaque object of type z is expected to have a non-default `__eq__` implementation as we will use this in torch.compile to guard on the equality of objects.zy is expected to have a non-default `__hash__` implementation as we will use this in torch.compile for FakeTensor caching.__fx_repr__z is expected to have a `__fx_repr__` method implementation as we will use this to reconstruct the object in the FX codegen. __fx_repr__ should return a tuple of (repr_string, set_of_types)..)torch.utils._pytreeutils_pytreer   torchTensorr0   SUPPORTED_NODES__eq__object	TypeError__hash__hasattrr   r%   r-   r.   _C_register_opaque_type)r   r3   pytreename	type_infos        r   register_opaque_typerI   b   sg    )( ~~#sell':/u 5X X
 	

 $$$e L L
 	

 (( ;( g~::&3C5 97 7  <<3C5 9* *  sM**3C5 9: :  nnQs//01D*I"M#"+$	HH""4(r   c                     [        U [        5      (       a  [        R                  R	                  U 5      $ U [
        ;  a  g[        R                  R	                  [
        U    R                  5      $ )z-
Checks if the given type is an opaque type.
F)
isinstancer!   r<   rD   _is_opaque_type_registeredr-   r'   r1   s    r   is_opaque_typerM      sQ     #sxx22377
-88..}S/A/L/LMMr   c                     [        U 5      (       d  g[        U [        5      (       a  [        U    R                  S:H  $ [
        U    R                  S:H  $ )zg
Checks if the given type is an opaque **value** type.
See Note [Opaque Objects] for more information.
Fr*   rM   rK   r!   r.   r+   r-   r1   s    r   is_opaque_value_typerP      sK    
 ##s$S)44??((G33r   c                     [        U 5      (       d  g[        U [        5      (       a  [        U    R                  S:H  $ [
        U    R                  S:H  $ )zk
Checks if the given type is an opaque **reference** type.
See Note [Opaque Objects] for more information.
Fr)   rO   r1   s    r   is_opaque_reference_typerR      sK    
 ##s$S)44CC((K77r   objc                    [        U S5      (       d  [        SU  S35      eU R                  5       u  p[        U[        5      (       d7  [        S[        U 5      R                   S[        U5      R                   35      e[        U[        5      (       d7  [        S[        U 5      R                   S[        U5      R                   35      eX4$ )a  
Get the FX-evaluable repr for an opaque object and collect required globals.

Objects must implement __fx_repr__() which should return:
    (repr_string, dict_mapping_name_to_type)

where repr_string is an evaluable string representation and
dict_mapping_name_to_type maps the names used in repr_string to their types.

For example, if repr_string is "Foo(bar=Bar(1))", the dict should be:
    {"Foo": Foo, "Bar": Bar}
r7   r6   z is expected to have a `__fx_repr__` method implementation as we will use this to reconstruct the object in the FX codegen. __fx_repr__ should return a tuple of (repr_string, dict[str, type]).z__fx_repr__ for z0 must return a string as the first element, got z/ must return a dict as the second element, got )rC   rA   r7   rK   r!   typer   r    )rS   repr_strglobals_dicts      r   get_opaque_obj_reprrX      s     3&&/u 59 9
 	
 !__.Hh$$tCy112 3""&x."9"9!:<
 	

 lD))tCy112 3##'#5#>#>"?A
 	

 !!r   ) __doc__dataclassesr   typingr   r   r   weakrefr   r<   fake_class_registryr	   r   OpaqueTypeStrrD   ScriptObjectr#   r%   r-   r,   r.   r    r!   r2   rI   boolrM   rP   rR   tuplerU   rX   r   r   r   <module>rb      s7  #J " ( ( %  4 )*	
 	
 +	
 <\588#8#89
 . . . :K9L o!56 L46 tC01 6)c )c )*C)c C)3 C)4 C)L
N 
N 
N4c 4d 48# 8$ 8$"S $"U3S$Y+?%@ $"r   