Ë
    g^(h   ã                   óÆ   — U d dl Z d dlZd dlmZmZ d dlmZ d dlZd dlm	Z	m
Z
  G d„ d«      Zdeded	efd
„Zd„ Zeaeed<   e j"                  d„ «       Z G d„ d«      Zdd„Zy)é    N)ÚCallableÚOptional)Ú
deprecated)ÚKernelÚRegistrationHandlec                   ó0   — e Zd ZdZdefd„Zdededefd„Zy)	ÚFakeImplHolderz0A holder where one can register an fake impl to.Úqualnamec                 ó.   — || _         d | _        d | _        y ©N)r
   ÚkernelÚlib)Úselfr
   s     úV/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/_library/fake_impl.pyÚ__init__zFakeImplHolder.__init__   s   € Ø%ˆŒØ(,ˆŒØ48ˆ�ó    ÚfuncÚsourceÚreturnc                 óâ  ‡ — ‰ j                   �0t        d‰ j                  › d‰ j                   j                  › d�«      ‚t        j
                  j                  ‰ j                  d«      rt        d‰ j                  › d�«      ‚t        j
                  j                  ‰ j                  d«      rt        d‰ j                  › d�«      ‚t        ||«      ‰ _         ‰ j                  €C‰ j                  j                  d«      d	   }t        j                  j                  |d
«      ‰ _        t        ‰ j                  ‰ «      }‰ j                  j                  ‰ j                  |d«       ˆ fd„}t        |«      S )z}Register an fake impl.

        Returns a RegistrationHandle that one can use to de-register this
        fake impl.
        z!register_fake(...): the operator z( already has an fake impl registered at ú.ÚMetaz´ already has an DispatchKey::Meta implementation via a pre-existing torch.library or TORCH_LIBRARY registration. Please either remove that registration or don't call register_fake.ÚCompositeImplicitAutograda%   already has an implementation for this device type via a pre-existing registration to DispatchKey::CompositeImplicitAutograd.CompositeImplicitAutograd operators do not need an fake impl; instead, the operator will decompose into its constituents and those can have fake impls defined on them.z::r   ÚFRAGMENTc                  ón   •— ‰ j                   r!‰ j                   j                  «        d ‰ _         d ‰ _        y r   )r   Ú_destroyr   )r   s   €r   Úderegister_fake_classz6FakeImplHolder.register.<locals>.deregister_fake_classA   s(   ø€ Ø�xŠxØ—‘×!Ñ!Ô#Ø�”ØˆD�Kr   )r   ÚRuntimeErrorr
   r   ÚtorchÚ_CÚ%_dispatch_has_kernel_for_dispatch_keyr   r   ÚsplitÚlibraryÚLibraryÚconstruct_meta_kernelÚimplr   )r   r   r   ÚnsÚmeta_kernelr   s   `     r   ÚregisterzFakeImplHolder.register   sL  ø€ ð �;‰;Ð"ÜØ3°D·M±M°?ð C:à—;‘;×%Ñ%Ð& að)óð ô
 �8‰8×9Ñ9¸$¿-¹-ÈÔPÜØ3°D·M±M°?ð C!ð "óð ô �8‰8×9Ñ9Ø�M‰MÐ6ô
ô Ø3°D·M±M°?ð C7ð 8ó
ð 
ô ˜T 6Ó*ˆŒð �8‰8ÐØ—‘×$Ñ$ TÓ*¨1Ñ-ˆBÜ—}‘}×,Ñ,¨R°Ó<ˆDŒHÜ+¨D¯M©M¸4Ó@ˆØ�‰�‰�d—m‘m [°&Ô9ô	ô "Ð"7Ó8Ð8r   N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   r   r   r)   © r   r   r	   r	      s,   „ Ù:ð9 ó 9ð
49˜Xð 49¨sð 49Ð7Iô 49r   r	   r
   Úfake_impl_holderr   c                 óŠ   ‡ ‡— ‰j                   €J ‚t        j                  ‰j                   j                  «      ˆˆ fd„«       }|S )Nc                  óÀ   •‡— ‰j                   €J ‚‰j                   j                  Šˆˆfd„}t        |«      5   ‰j                   | i |¤Žcd d d «       S # 1 sw Y   y xY w)Nc                  ó&   •— t        ‰ › d‰› d�«      ‚)Nz (a¿  ): You're trying to run this operator with meta Tensors (as opposed to FakeTensors), but this operator may return an output Tensor with data-dependent shape. Meta Tensors don't support operators with outputs that have data-dependent shapes but FakeTensors do. If your operator does not return an output with data-dependent shape, make sure the FakeTensor and/or meta kernel does not call torch.library.get_ctx(). Otherwise, please use FakeTensors.)r   )r
   r   s   €€r   Úerror_on_ctxz@construct_meta_kernel.<locals>.meta_kernel.<locals>.error_on_ctxR   s'   ø€ ÜØ�*˜B˜v˜hð 'Nð Oó	ð 	r   )r   r   Úset_ctx_getter)ÚargsÚkwargsr4   r   r0   r
   s      @€€r   r(   z*construct_meta_kernel.<locals>.meta_kernelM   s`   ù€ à×&Ñ&Ð2Ð2Ð2Ø!×(Ñ(×/Ñ/ˆõ
	ô ˜LÓ)ñ 	<Ø*Ð#×*Ñ*¨DÐ;°FÑ;÷	<÷ 	<ò 	<ús   ¸AÁA)r   Ú	functoolsÚwrapsr   )r
   r0   r(   s   `` r   r%   r%   J   sE   ù€ Ø×"Ñ"Ð.Ð.Ð.ä‡_�_Ð%×,Ñ,×1Ñ1Ó2ô<ó 3ð<ð& Ðr   c                   ó   — y r   r/   r/   r   r   Úget_noner;   d   s   € Ør   Úglobal_ctx_getterc              #   ó8   K  — t         }	 | a d –— |a y # |a w xY w­wr   )r<   )Ú
ctx_getterÚprevs     r   r5   r5   k   s'   è ø€ ô €Dð!Ø&ÐÛà Ñø˜DÑüs   ‚Š �“—c                   ó€   — e Zd ZdZd„ Z ede¬«      dddœdej                  fd	„«       Z	d
ddœdej                  fd„Z
y)ÚFakeImplCtxzO
    Context object for writing fake implementations for custom operators.
    c                 óB   — || _         |j                  | _        || _        y r   )Ú
_fake_modeÚ	shape_envÚ
_shape_envÚ_op)r   rC   rF   s      r   r   zFakeImplCtx.__init__{   s   € Ø$ˆŒØ$×.Ñ.ˆŒØˆ�r   zM`create_unbacked_symint` is deprecated, please use `new_dynamic_size` instead)Úcategoryé   N©ÚminÚmaxr   c                ó(   — | j                  ||¬«      S ©NrI   )Únew_dynamic_size©r   rJ   rK   s      r   Úcreate_unbacked_symintz"FakeImplCtx.create_unbacked_symint€   s   € ð
 ×$Ñ$¨°#Ð$Ó6Ð6r   r   c                óŽ  — | j                   �| j                   j                  s3t        j                  j                  j                  | j                  «      ‚t        |t        j                  «      st        |t        j                  «      rt        d|› d|› d�«      ‚|dk  rt        d|› d�«      ‚t        | j                   ||«      S )a	  Constructs a new symint (symbolic int) representing a data-dependent value.

        This is useful for writing the fake implementation (which is necessary
        for torch.compile) for a CustomOp where an output Tensor has a size
        that depends on the data of the input Tensors.

        Args:
            min (int): A statically known inclusive lower bound for this symint. Default: 0
            max (Optional[int]): A statically known inclusive upper bound for this
                symint. Default: None

        .. warning:

            It is important that the ``min`` and ``max`` (if not None) values are set
            correctly, otherwise, there will be undefined behavior under
            torch.compile. The default value of ``min`` is 2 due to torch.compile
            specializing on 0/1 sizes.

            You must also verify that your implementation on concrete Tensors
            (e.g. CPU/CUDA) only returns Tensors where the size that corresponds
            to the symint also has respects these constraint.
            The easiest way to do this is to add an assertion in the CPU/CUDA/etc
            implementation that the size follows these bounds.

        Example::

            >>> # An operator with data-dependent output shape
            >>> lib = torch.library.Library("mymodule", "FRAGMENT")
            >>> lib.define("mymodule::custom_nonzero(Tensor x) -> Tensor")
            >>>
            >>> @torch.library.register_fake("mymodule::custom_nonzero")
            >>> def _(x):
            >>>     # Number of nonzero-elements is data-dependent.
            >>>     # Since we cannot peek at the data in an fake impl,
            >>>     # we use the ctx object to construct a new symint that
            >>>     # represents the data-dependent size.
            >>>     ctx = torch.library.get_ctx()
            >>>     nnz = ctx.new_dynamic_size()
            >>>     shape = [nnz, x.dim()]
            >>>     result = x.new_empty(shape, dtype=torch.int64)
            >>>     return result
            >>>
            >>> @torch.library.impl(lib, "custom_nonzero", "CPU")
            >>> def _(x):
            >>>     x_np = x.numpy()
            >>>     res = np.stack(np.nonzero(x_np), axis=1)
            >>>     return torch.tensor(res, device=x.device)

        zctx.new_dynamic_size(min=z, max=zZ): expected min and max to be statically known ints but got SymInt. This is not supported.r   zc, ...): expected min to be greater than or equal to 0: this API can only create non-negative sizes.)rE   Úallow_dynamic_output_shape_opsr   Ú_subclassesÚfake_tensorÚDynamicOutputShapeExceptionrF   Ú
isinstanceÚSymIntÚ
ValueErrorÚallocate_sizerO   s      r   rN   zFakeImplCtx.new_dynamic_size‡   s¸   € ðf �O‰OÐ#Ø—?‘?×AÒAä×#Ñ#×/Ñ/×KÑKÈDÏHÉHÓUÐUä�cœ5Ÿ<™<Ô(¬J°s¼E¿L¹LÔ,IÜØ+¨C¨5°°s°eð <)ð *óð ð �Š7ÜØ+¨C¨5ð 1&ð 'óð ô ˜TŸ_™_¨c°3Ó7Ð7r   )r*   r+   r,   r-   r   r   ÚFutureWarningr   rW   rP   rN   r/   r   r   rA   rA   v   sV   „ ñòñ
 ØWØôð -.°4ò 7¸E¿L¹Lò 7ó	ð7ð '(¨Tò F8°e·l±lô F8r   rA   c                 ó’   — | j                  «       }t        j                  j                  j                  j                  |||¬«       |S rM   )rP   r   ÚfxÚexperimentalÚsymbolic_shapesÚ_constrain_range_for_size)rD   Úmin_valÚmax_valÚresults       r   rY   rY   Ð   sB   € Ø×-Ñ-Ó/€FÜ	‡H�H×Ñ×)Ñ)×CÑCØ�G ð Dô ð €Mr   )r   N)Ú
contextlibr8   Útypingr   r   Útyping_extensionsr   r   Útorch._library.utilsr   r   r	   r.   r%   r;   r<   Ú__annotations__Úcontextmanagerr5   rA   rY   r/   r   r   ú<module>ri      sz   ðä Û ß %Ý (ã ß ;÷<9ñ <9ð~ Cð ¸>ð Èhó ò4ð 'Ð �8Ó &ð ×Ññ!ó ð!÷W8ñ W8ôtr   