Ë
    g^(há1  ã                   óâ  — d dl Z d dlZd dlmZmZmZmZ d dlZd dlm	Z	 d dl
mZ  ej                  e«      Z G d„ d«      Z G d„ d«      Z G d	„ d
e«      Z G d„ d«      Z e«       Zd„ Zdej*                  defd„Zdej*                  deeej*                  f   fd„Zd dee   fd„Zd„ Zdefd„Zdee   fd„Zdedefd„Zdedefd„Zdede eef   fd„Z!dej*                  defd„Z"dZ#defd„Z$y)!é    N)ÚAnyÚOptionalÚProtocolÚUnion©Úparse_namespace©Ú_disable_current_modesc                   ó4   — e Zd Zdededej                  fd„Zy)ÚFakeScriptObjectÚwrapped_objÚscript_class_nameÚxc                 óì   — || _         || _        	 t        «       5  t        j                  |«      | _        d d d «       y # 1 sw Y   y xY w# t        $ r  t        j                  d|«       || _        Y y w xY w)NzœUnable to deepcopy the custom object %s. Defaulting to the user given object. This might be dangerous as side effects may be directly applied to the object.)	r   r   r
   ÚcopyÚdeepcopyÚreal_objÚRuntimeErrorÚlogÚwarning)Úselfr   r   r   s       ú`/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/_library/fake_class_registry.pyÚ__init__zFakeScriptObject.__init__   sq   € Ø&ˆÔð "3ˆÔð	Ü'Ó)ñ 1Ü $§¡¨aÓ 0�”÷1÷ 1ñ 1ûäò 	Ü�K‰Kð!ð "ôð ˆDŽMð	ús+   �
A
 š>µA
 ¾AÁA
 ÁA
 Á
&A3Á2A3N)Ú__name__Ú
__module__Ú__qualname__r   ÚstrÚtorchÚScriptObjectr   © ó    r   r   r      s"   „ ð Cð ¸Cð ÀE×DVÑDVô r!   r   c                   ó@   — e Zd Zdededeej                     fd„Zd„ Z	y)ÚFakeScriptMethodÚself_fake_objÚmethod_nameÚschemac                 ó.   — || _         || _        || _        y ©N)r$   r%   r&   )r   r$   r%   r&   s       r   r   zFakeScriptMethod.__init__#   s   € ð +ˆÔØ&ˆÔØˆ�r!   c                 óP   — ddl m}  || j                  | j                  g|¢­i |¤ŽS )Nr   )Úcall_torchbind)Ú!torch._higher_order_ops.torchbindr*   r$   r%   )r   ÚargsÚkwargsr*   s       r   Ú__call__zFakeScriptMethod.__call__-   s(   € ÝDá˜d×0Ñ0°$×2BÑ2BÐTÀTÒTÈVÑTÐTr!   N)
r   r   r   r   r   r   r   ÚFunctionSchemar   r.   r    r!   r   r#   r#   "   s4   „ ðà'ðð ðð ˜×-Ñ-Ñ.ó	óUr!   r#   c                   ó6   — e Zd Zedej
                  fd„«       Zy)ÚHasStaticMethodFromRealr   c                  ó   — y r(   r    )Úclsr   s     r   Ú	from_realz!HasStaticMethodFromReal.from_real4   s   € àr!   N)r   r   r   Úclassmethodr   r   r4   r    r!   r   r1   r1   3   s!   „ Øð ×!3Ñ!3ò ó ñr!   r1   c                   ón   — e Zd Zdd„Zdedefd„Zdedefd„Zddeddfd„Z	dedefd„Z
dd	„Zdeddfd
„Zy)ÚFakeClassRegistryÚreturnNc                 ó   — i | _         y r(   ©Ú_registered_class©r   s    r   r   zFakeClassRegistry.__init__:   s
   € Ø13ˆÕr!   Úfull_qualnamec                 ó   — || j                   v S r(   r:   ©r   r=   s     r   Úhas_implzFakeClassRegistry.has_impl=   s   € Ø × 6Ñ 6Ð6Ð6r!   c                 óB   — | j                  |«       | j                  |   S r(   )Ú_check_registeredr;   r?   s     r   Úget_implzFakeClassRegistry.get_impl@   s!   € Ø×Ñ˜}Ô-Ø×%Ñ% mÑ4Ð4r!   c                 ór   — | j                  |«      rt        j                  d||«       || j                  |<   y )NzE%s is already registered. Previous fake class is overridden with  %s.)r@   r   r   r;   )r   r=   Ú
fake_classs      r   ÚregisterzFakeClassRegistry.registerD   s5   € Ø�=‰=˜Ô'Ü�K‰KØWØØôð
 1;ˆ×Ñ˜}Ò-r!   c                 óˆ   — | j                  |«      st        j                  d|«       y | j                  j	                  |«      S )NziCannot deregister %s. Please use register_fake_class to register it first. Or do you dereigster it twice?)r@   r   r   r;   Úpopr?   s     r   Ú
deregisterzFakeClassRegistry.deregisterM   s;   € Ø�}‰}˜]Ô+Ü�K‰Kð2àõð ×)Ñ)×-Ñ-¨mÓ<Ð<r!   c                 ó8   — | j                   j                  «        y r(   )r;   Úclearr<   s    r   rK   zFakeClassRegistry.clearW   s   € Ø×Ñ×$Ñ$Õ&r!   c                 ó<   — || j                   vrt        |› d�«      ‚y )NzH is not registered. Please use register_fake_class to register it first.)r;   r   r?   s     r   rB   z#FakeClassRegistry._check_registeredZ   s-   € Ø × 6Ñ 6Ñ6ÜØ �/Ð!iÐjóð ð 7r!   )r8   Nr(   )r   r   r   r   r   Úboolr@   r   rC   rF   rI   rK   rB   r    r!   r   r7   r7   9   sk   „ ó4ð7 cð 7¨dó 7ð5 cð 5¨có 5ñ; cð ;¸tó ;ð=¨ð =°ó =ó'ð¨sð °tô r!   r7   c                 óÖ   — t        | t        «      st        d«      ‚| D ]I  }t        |t        «      st        d«      ‚t        |«      dk(  rt        |d   t        «      rŒ@t        d«      ‚ y )NzExpect flat x to be a tuple.z&Expect flat x to be a tuple of tuples.é   r   zXExpect element of flat x to be a tuple of two elements with first element being a string)Ú
isinstanceÚtupler   Úlenr   )Úflat_xÚtps     r   Ú_check_valid_flat_script_objrU   e   se   € Ü�fœeÔ$ÜÐ9Ó:Ð:àò ˆÜ˜"œeÔ$ÜÐGÓHÐHä�2‹w˜!Š|¤:¨b°©e´SÕ#9ÜØjóð ñr!   r   r8   c                 ó–   — t        | d«      sy| j                  «       dv sJ d| j                  «       › �«       ‚| j                  «       dk(  S )NÚtracing_modeF)ÚrealÚfakez0tracing_mode can be either real or fake but got rX   )ÚhasattrrW   )r   s    r   Útracing_with_realr[   s   s\   € Ü�1�nÔ%Øà�>‰>Óð  ñ ð Mð 
:¸!¿.¹.Ó:JÐ9KÐLóMð ð �>‰>Ó˜vÑ%Ð%r!   c           
      óÚ  ‡ — dd l mc m} ddlm} t        |«      r|S  |«       5  |j                  «       }d d d «       t        «       |j                  t        j                  ˆ fd„|«      }t        |«      j                  |«      }t        ||j                  «       j                  «       |«      }|j!                  «       D ]›  }t#        ||d «      }	|	rjt%        |	«      st'        d|› d|	› d�«      ‚t#        ||«      }
d }t)        |
t        j*                  «      r|
j,                  }t/        ||t1        |||«      «       Œ|h d£}||vsŒ…t2        j5                  d||«       Œ� |S # 1 sw Y   �Œ,xY w)	Nr   r	   c                 ó&   •— ‰j                  | «      S r(   )Úfrom_tensor)ÚtÚ	fake_modes    €r   ú<lambda>z#maybe_to_fake_obj.<locals>.<lambda>”   s   ø€ �)×'Ñ'¨Ó*€ r!   zExpect z to be a callable but got ú.>   Ú__get_state__Ú__set_state__Ú__obj_flatten__z.fake object of %s doesn't implement method %s.)Útorch.utils._pytreeÚutilsÚ_pytreeÚtorch.utils._python_dispatchr
   r[   re   rU   Útree_map_onlyr   ÚTensorÚ"_find_fake_class_for_script_objectÚ__obj_unflatten__r   Ú_typeÚqualified_nameÚ_method_namesÚgetattrÚcallabler   rP   ÚScriptMethodr&   Úsetattrr#   r   r   )r`   r   Úpytreer
   rS   Úfake_flattenedÚfake_xÚfake_x_wrappedÚnameÚattrÚ	real_attrÚmethod_schemaÚoverride_skip_lists   `            r   Úmaybe_to_fake_objr~   ~   se  ø€ ÷ )Ð(ÝCô ˜ÔØˆñ 
 Ó	!ñ %Ø×"Ñ"Ó$ˆ÷%ô ! Ô(à×)Ñ)Ü�‰Û*Øó€Nô 0°Ó2×DÑDÀ^ÓT€Fä% f¨a¯g©g«i×.FÑ.FÓ.HÈ!ÓL€Nà—‘Ó!ò WˆÜ�v˜t TÓ*ˆÙÜ˜D”>Ü" W¨T¨FÐ2LÈTÈFÐRSÐ#TÓUÐUä  4Ó(ˆIð =AˆMÜ˜)¤U×%7Ñ%7Ô8Ø )× 0Ñ 0�äØØÜ  °°}ÓEõò "WÐØÐ-Ò-Ü—‘ÐLÈaÐQUÕVð+Wð, Ð÷K%ñ %ús   ¥E Å E*rE   c                 ó4   ‡ — dt         fˆ fd„}|€|S  ||«      S )a¤
  Register a fake implementation for this class.

    It's in the same spirit of registering a fake implementation for
    an operator but with the difference that it
    associates a fake class with the original torch bind class (registered
    with torch::class_). In this way, torch.compile can handle them properly
    in components such as Dynamo and AOTAutograd.

    This API may be used as a decorator (see example). For the fake class, users
    are required to provide a from_real classmethod that takes a real object and
    returns an instance of the fake class. All tensors in the fake object should also
    be properly fakified with to_fake_tensor() in from_real.


    Examples:
        # For a custom class Foo defined in test_custom_class_registration.cpp:

        TORCH_LIBRARY(_TorchScriptTesting, m) {
          m.class_<TensorQueue>("_TensorQueue")
            .def(torch::init<at::Tensor>())
            .def("push", &TensorQueue::push)
            .def("pop", &TensorQueue::pop)
            .def("top", &TensorQueue::top)
            .def("size", &TensorQueue::size)
            .def("clone_queue", &TensorQueue::clone_queue)
            .def("__obj_flatten__", &TensorQueue::__obj_flatten__)
            .def_pickle(
                // __getstate__
                [](const c10::intrusive_ptr<TensorQueue>& self)
                    -> c10::Dict<std::string, at::Tensor> {
                  return self->serialize();
                },
                // __setstate__
                [](c10::Dict<std::string, at::Tensor> data)
                    -> c10::intrusive_ptr<TensorQueue> {
                  return c10::make_intrusive<TensorQueue>(std::move(data));
                });
            };
        # We could register a fake class FakeTensorQueue in Python as follows:
        import torch

        @torch._library.register_fake_class("_TorchScriptTesting::_TensorQueue")
        class FakeTensorQueue:
            def __init__(self, queue):
                self.queue = queue

            @classmethod
            def __obj_unflatten__(cls, flattened_ctx):
                return cls(**dict(ctx))

            def push(self, x):
                self.queue.append(x)

            def pop(self):
                return self.queue.pop(0)

            def size(self):
                return len(self.queue)

    In this example, the original TensorQeue need to addd a __obj_flatten__ method
    to the class TensorQueue and the flattend result is passed into FakeTensorQueue's
    __obj_unflatten__ as inputs to create a fake class. This protocol allows pytorch to look
    at the contents of the script object and properly handle them in the subsystems
    like dynamo, aot_aotugrad or more.
    rE   c                 óX  •— t        ‰«      \  }}t        j                  j                  ||«       t	        | t
        d «      }|st        | › dt
        › d�«      ‚t        | j                  t
           t        «      st        t
        › d�«      ‚t        j                  t        ‰«      | «       | S )Nz doesn't define a classmethod rb   z method is not a classmethod.)r   r   Ú_CÚ _get_custom_class_python_wrapperrq   Ú_CONVERT_FROM_REAL_NAMEr   rP   Ú__dict__r5   Úglobal_fake_class_registryrF   Ú_full_qual_class_name)rE   Únsry   Úfrom_methodÚqualnames       €r   Úinnerz"register_fake_class.<locals>.innerø   s©   ø€ Ü" 8Ó,‰ˆˆDô 	�‰×1Ñ1°"°dÔ;ä˜jÔ*AÀ4ÓHˆÙÜØ�,Ð<Ô=TÐ<UÐUVÐWóð ô ˜*×-Ñ-Ô.EÑFÌÔTÜÜ*Ð+Ð+HÐIóð ô 	#×+Ñ+Ô,AÀ(Ó,KÈZÔXØÐr!   )r1   )r‰   rE   rŠ   s   `  r   Úregister_fake_classr‹   µ   s)   ø€ ðFÔ1õ ð( ÐØˆÙ�ÓÐr!   c                 ó>   — t         j                  t        | «      «      S r(   )r…   rI   r†   )r‰   s    r   Úderegister_fake_classr�     s   € Ü%×0Ñ0Ô1FÀxÓ1PÓQÐQr!   c                 ó,   — t         j                  | «      S r(   )r…   r@   ©r=   s    r   Úhas_fake_classr�     s   € Ü%×.Ñ.¨}Ó=Ð=r!   c                 óD   — t        | «      sy t        j                  | «      S r(   )r�   r…   rC   r�   s    r   Úfind_fake_classr’     s   € Ü˜-Ô(ØÜ%×.Ñ.¨}Ó=Ð=r!   r‰   c                 ó4   — t        | «      \  }}d|z   dz   |z   S )Nz__torch__.torch.classes.rb   r   )r‰   r‡   ry   s      r   r†   r†     s%   € Ü˜xÓ(�H€BˆØ%¨Ñ*¨SÑ0°4Ñ7Ð7r!   Úobjc                 ó”   — t        | t        j                  «      xr- | j                  «       j	                  «       j                  d«      S )Nz__torch__.torch.classes)rP   r   r   rn   ro   Ú
startswith)r”   s    r   Ú_is_script_objectr—   $  s?   € ÜØŒU×Ñóò à
�)‰)‹+×
$Ñ
$Ó
&×
1Ñ
1Ø!óðr!   r=   c                 ól   — | j                  d«      }t        |«      dk(  s
J d| ›�«       ‚|\  }}}}}||fS )Nrb   é   zCould not split full_qualname=)ÚsplitrR   )r=   ÚsplitsÚ_torchÚ	_torch_nsÚ_classesr‡   Ú
class_names          r   Ú_ns_and_class_namer    -  sL   € Ø× Ñ  Ó%€FÜˆv‹;˜!ÒÐ@Ð>°Ð/?Ð@Ó@ÐØ28Ñ/€FˆI�x  ZØˆzˆ>Ðr!   c                 ó°   — | j                  «       j                  «       }t        |«      \  }}t        |«      }|€t	        d|› d|› d|› dt
        › d�	«      ‚|S )Nz ScriptObject's zA haven't registered a fake class. Please use register_fake_class(z::a‚  ) to annotate a fake class for the script obj. Specifically, create a python class that implements a fake version for all the methods that're used in the program and put annotated class in the program e.g. after loading the library. The fake methods can be written in the same way as a meta kernel for an operator but need to additionally simulate the object's states. Be sure to add a z; classmethod to enable creating a fake obj from a real one.)rn   ro   r    r’   r   rƒ   )r   r=   r‡   rŸ   rE   s        r   rl   rl   4  s{   € Ø—G‘G“I×,Ñ,Ó.€MÜ'¨Ó6�N€Bˆ
Ü  Ó/€JØÐÜØ˜}˜oð ./Ø/1¨d°"°Z°Lð A?ô @WÐ>Wð X>ð?ó
ð 	
ð Ðr!   rm   c                 ód  ‡— t        |«      }t        |t        d «      }|st        |› dt        › d�«      ‚t        j
                  j                  j                  | d «      Št        j
                  j                  j                  ˆfd„«      5  |j                  |«      cd d d «       S # 1 sw Y   y xY w)Nz must define a classmethod z2 that converts the real object to the fake object.c                  ó   •— ‰ S r(   r    )Úctxs   €r   ra   z%_fake_obj_from_real.<locals>.<lambda>T  s   ø€ ¸€ r!   )
rl   rq   rƒ   r   r   Ú_libraryÚ	fake_implÚFakeImplCtxÚset_ctx_getterr4   )r`   r   rE   Úfrom_real_methodr¤   s       @r   Ú_fake_obj_from_realrª   H  s�   ø€ Ü3°AÓ6€Jä˜zÔ+BÀDÓIÐÙÜØˆlÐ5Ô6MÐ5NØ@ðBó
ð 	
ô �.‰.×
"Ñ
"×
.Ñ
.¨y¸$Ó
?€CÜ	�‰×	!Ñ	!×	0Ñ	0³Ó	=ñ 'Ø×#Ñ# AÓ&÷'÷ 'ò 'ús   ÂB&Â&B/r(   )%r   ÚloggingÚtypingr   r   r   r   r   Útorch._library.utilsr   ri   r
   Ú	getLoggerr   r   r   r#   r1   r7   r…   rU   r   rM   r[   r~   r‹   r�   r�   r’   r   r†   r—   rQ   r    rl   rƒ   rª   r    r!   r   ú<module>r¯      sV  ðã Û ß 1Ó 1ã Ý 0Ý ?ð €g×Ñ˜Ó!€÷ñ ÷(Uñ Uô"˜hô ÷%ñ %ñP /Ó0Ð òð&˜×+Ñ+ð &°ó &ð4Ø×$Ñ$ð4à
Ð˜U×/Ñ/Ð/Ñ0ó4ñnY¨hÐ7NÑ.Oó YòxRð> Tó >ð> h¨s¡mó >ð8 Cð 8¨Có 8ð
˜3ð  4ó ð cð ¨e°C¸°H©oó ð¨%×*<Ñ*<ð Àó ð" .Ð ð'¨ô 'r!   