§
    Ÿ²ÅjÖ   ã                   óÐ   — 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dS )é    N)ÚCallableÚOptional)Ú
deprecated)ÚKernelÚRegistrationHandlec                   ó2   — e Zd ZdZdefd„Zdededefd„ZdS )	ÚFakeImplHolderz0A holder where one can register an fake impl to.Úqualnamec                 ó0   — || _         d | _        d | _        d S ©N)r
   ÚkernelÚlib)Úselfr
   s     úIC:\Projects\Historical\venv\Lib\site-packages\torch/_library/fake_impl.pyÚ__init__zFakeImplHolder.__init__   s   € Ø%ˆŒØ(,ˆŒØ48ˆŒˆˆó    ÚfuncÚsourceÚreturnc                 ó¦  ‡ — ‰ j         �%t          d‰ j        › d‰ j         j        › d�¦  «        ‚t          j                             ‰ j        d¦  «        rt          d‰ j        › d�¦  «        ‚t          j                             ‰ j        d¦  «        rt          d‰ j        › d�¦  «        ‚t          ||¦  «        ‰ _         ‰ j        €E‰ j         	                    d	¦  «        d
         }t          j
                             |d¦  «        ‰ _        t          ‰ 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.
        Nz!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                  ód   •— ‰ j         r ‰ j                              ¦   «          d ‰ _         d ‰ _        d S r   )r   Ú_destroyr   )r   s   €r   Úderegister_fake_classz6FakeImplHolder.register.<locals>.deregister_fake_classA   s3   ø€ ØŒxð  Ø”×!Ò!Ñ#Ô#Ð#Ø�”ØˆDŒKˆKˆ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   s†  ø€ ð Œ;Ð"Ýð)°D´Mð )ð )à”;Ô%ð)ð )ð )ñô ð õ
 Œ8×9Ò9¸$¼-ÈÑPÔPð 	Ýð"°D´Mð "ð "ð "ñô ð õ Œ8×9Ò9ØŒMÐ6ñ
ô 
ð 	õ ð8°D´Mð 8ð 8ð 8ñ
ô 
ð 
õ ˜T 6Ñ*Ô*ˆŒð Œ8ÐØ”×$Ò$ TÑ*Ô*¨1Ô-ˆBÝ”}×,Ò,¨R°Ñ<Ô<ˆDŒHÝ+¨D¬M¸4Ñ@Ô@ˆØŒ�Š�d”m [°&Ñ9Ô9Ð9ð	ð 	ð 	ð 	ð 	õ "Ð"7Ñ8Ô8Ð8r   N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   r   r   r)   © r   r   r	   r	      s_   € € € € € Ø:Ð:ð9 ð 9ð 9ð 9ð 9ð
49˜Xð 49¨sð 49Ð7Ið 49ð 49ð 49ð 49ð 49ð 49r   r	   r
   Úfake_impl_holderr   c                 óp   ‡ ‡— ‰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xY w Y   d S )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   s8   ø€ ÝØð Oð O˜vð Oð Oð 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ÁA)r   Ú	functoolsÚwrapsr   )r
   r0   r(   s   `` r   r%   r%   J   sU   øø€ ØÔ"Ð.Ð.Ð.å„_Ð%Ô,Ô1Ñ2Ô2ð<ð <ð <ð <ð <ñ 3Ô2ð<ð& Ðr   c                  ó   — d S r   r/   r/   r   r   Úget_noner;   d   s   € Øˆ4r   Úglobal_ctx_getterc              #   ó8   K  — t           }	 | a d V — |a d S # |a w xY wr   )r<   )Ú
ctx_getterÚprevs     r   r5   r5   k   s?   è è € õ €Dð!Ø&ÐØˆˆˆà ÐÐÐø˜DÐÐ Ð Ð Ð s   ‹ •c                   óz   — 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
dS )ÚFakeImplCtxzO
    Context object for writing fake implementations for custom operators.
    c                 ó:   — || _         |j        | _        || _        d S 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                ó0   — |                       ||¬¦  «        S ©NrI   )Únew_dynamic_size©r   rJ   rK   s      r   Úcreate_unbacked_symintz"FakeImplCtx.create_unbacked_symint€   s   € ð
 ×$Ò$¨°#Ð$Ñ6Ô6Ð6r   r   c                ól  — | j         �| j         j        s)t          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)

        Nz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Ð#Ø”?ÔAð $õ Ô#Ô/×KÒKÈDÌHÑUÔUÐUå�c�5œ<Ñ(Ô(ð 	­J°s½E¼LÑ,IÔ,Ið 	Ýð*¨Cð *ð *°sð *ð *ð *ñô ð ð �Š7ˆ7Ýð'¨Cð 'ð 'ð 'ñô ð õ ˜Tœ_¨c°3Ñ7Ô7Ð7r   )r*   r+   r,   r-   r   r   ÚFutureWarningr   rW   rP   rN   r/   r   r   rA   rA   v   s¸   € € € € € ðð ðð ð ð
 €ZØWØðñ ô ð -.°4ð 7ð 7ð 7¸E¼Lð 7ð 7ð 7ñ	ô ð7ð '(¨Tð F8ð F8ð F8°e´lð F8ð F8ð F8ð F8ð F8ð F8r   rA   c                 ó†   — |                       ¦   «         }t          j        j        j                             |||¬¦  «         |S rM   )rP   r   ÚfxÚexperimentalÚsymbolic_shapesÚ_constrain_range_for_size)rD   Úmin_valÚmax_valÚresults       r   rY   rY   Ð   sG   € Ø×-Ò-Ñ/Ô/€FÝ	„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      sM  ðà Ð Ð Ð Ð Ø Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %Ð %Ð %Ø (Ð (Ð (Ð (Ð (Ð (à €€€Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;ð<9ð <9ð <9ð <9ð <9ñ <9ô <9ð <9ð~ Cð ¸>ð Èhð ð ð ð ð4ð ð ð 'Ð �8Ð &Ð &Ñ &ð Ôð!ð !ñ Ôð!ðW8ð W8ð W8ð W8ð W8ñ W8ô W8ð W8ðtð ð ð ð ð r   