Ë
    L²jÌ"  ã                   óÂ   — U d dl Z d dlZd dl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)Ú
deprecated)ÚKernelÚRegistrationHandlec                   ój   — e Zd ZdZdefd„Zed„ «       Zej                  d„ «       Zddœde	d	ed
e
fd„Zy)ÚFakeImplHolderz0A holder where one can register an fake impl to.Úqualnamec                 ó    — || _         g | _        y ©N)r	   Úkernels)Úselfr	   s     úW/var/www/html/ia-enhanced/venv/lib/python3.12/site-packages/torch/_library/fake_impl.pyÚ__init__zFakeImplHolder.__init__   s   € Ø%ˆŒð &(ˆ�ó    c                 óR   — t        | j                  «      dk(  ry | j                  d   S )Nr   éÿÿÿÿ)Úlenr   )r   s    r   ÚkernelzFakeImplHolder.kernel   s%   € äˆt�|‰|Ó Ò!ØØ�|‰|˜BÑÐr   c                 ó   — t        d«      ‚)NzUnable to directly set kernel.©ÚRuntimeError)r   Úvalues     r   r   zFakeImplHolder.kernel   s   € äÐ;Ó<Ð<r   F©Úallow_overrideÚfuncÚsourceÚreturnc                ól  ‡ ‡— |sÂ‰ 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                  j                  ‰«       ˆˆ fd„}t        ‰ j                  ‰ «      }|j                  ‰ j                  |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.c                  ó<   •— ‰j                   j                  ‰ «       y r   )r   Úremove)r   r   s   €€r   Úderegister_fake_kernelz7FakeImplHolder.register.<locals>.deregister_fake_kernelN   s   ø€ Ø�L‰L×Ñ Õ'r   r   )r   r   r	   r   ÚtorchÚ_CÚ%_dispatch_has_kernel_for_dispatch_keyr   r   ÚappendÚconstruct_meta_kernelÚimplr   )	r   r   r   Úlibr   r$   Úmeta_kernelÚhandler   s	   `       @r   ÚregisterzFakeImplHolder.register"   s&  ù€ ñ Ø�{‰{Ð&Ü"Ø7¸¿¹°ð G>à—{‘{×)Ñ)Ð*¨!ð-óð ô
 �x‰x×=Ñ=¸d¿m¹mÈVÔTÜ"Ø7¸¿¹°ð G%ð &óð ô �x‰x×=Ñ=Ø—‘Ð:ôô #Ø7¸¿¹°ð G;ð <ó
ð 
ô ˜˜fÓ%ˆØ�‰×Ñ˜FÔ#õ	(ô ,¨D¯M©M¸4Ó@ˆØ�‰�—‘ ¨VÀNˆÔSä#Ð$:Ó;ˆØˆr   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   Úpropertyr   Úsetterr   r   r.   © r   r   r   r      sa   „ Ù:ð( ó (ð ñ ó ð ð
 ‡]�]ñ=ó ð=ð CHò3Øð3Ø&)ð3à	ô3r   r   r	   Úfake_impl_holderr   c                 óœ   ‡ ‡— ‰j                   €t        d«      ‚t        j                  ‰j                   j                  «      ˆˆ fd„«       }|S )Nú(fake_impl_holder.kernel must not be Nonec                  óÒ   •‡— ‰j                   €t        d«      ‚‰j                   j                  Šˆˆfd„}t        |«      5   ‰j                   | i |¤Žcd d d «       S # 1 sw Y   y xY w)Nr9   c                  ó&   •— 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_ctxb   s'   ø€ ÜØ�*˜B˜v˜hð 'Nð Oó	ð 	r   )r   ÚAssertionErrorr   Úset_ctx_getter)ÚargsÚkwargsr<   r   r7   r	   s      @€€r   r,   z*construct_meta_kernel.<locals>.meta_kernel\   sf   ù€ à×"Ñ"Ð*Ü Ð!KÓLÐLØ!×(Ñ(×/Ñ/ˆõ
	ô ˜LÓ)ñ 	<Ø*Ð#×*Ñ*¨DÐ;°FÑ;÷	<÷ 	<ò 	<ús   ÁAÁA&)r   r=   Ú	functoolsÚwrapsr   )r	   r7   r,   s   `` r   r)   r)   X   sK   ù€ Ø×ÑÐ&ÜÐGÓHÐHä‡_�_Ð%×,Ñ,×1Ñ1Ó2ô<ó 3ð<ð( Ðr   c                   ó   — y r   r6   r6   r   r   Úget_nonerD   t   s   € Ør   Úglobal_ctx_getterc              #   ó8   K  — t         }	 | a d –— |a y # |a w xY w­wr   )rE   )Ú
ctx_getterÚprevs     r   r>   r>   {   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   rL   rO   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 ©NrR   )Únew_dynamic_size©r   rS   rT   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.)rN   Úallow_dynamic_output_shape_opsr%   Ú_subclassesÚfake_tensorÚDynamicOutputShapeExceptionrO   Ú
isinstanceÚSymIntÚ
ValueErrorÚallocate_sizerX   s      r   rW   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/   r0   r1   r2   r   r   ÚFutureWarningr%   r`   rY   rW   r6   r   r   rJ   rJ   †   sV   „ ñòñ
 ØWØôð -.°4ò 7¸E¿L¹Lò 7ó	ð7ð '(¨Tò F8°e·l±lô F8r   rJ   c                 ó’   — | j                  «       }t        j                  j                  j                  j                  |||¬«       |S rV   )rY   r%   ÚfxÚexperimentalÚsymbolic_shapesÚ_constrain_range_for_size)rM   Úmin_valÚmax_valÚresults       r   rb   rb   à   sB   € Ø×-Ñ-Ó/€FÜ	‡H�H×Ñ×)Ñ)×CÑCØ�G ð Dô ð €Mr   )r   N)Ú
contextlibrA   Úcollections.abcr   Útyping_extensionsr   r%   Útorch._library.utilsr   r   r   r3   r)   rD   rE   Ú__annotations__Úcontextmanagerr>   rJ   rb   r6   r   r   ú<module>rr      s|   ðä Û Ý $Ý (ã ß ;÷Jñ JðZ Cð ¸>ð Èhó ò8ð 'Ð �8Ó &ð ×Ññ!ó ð!÷W8ñ W8ôtr   