§
    ŠŠtjÖ#  ã                   óô   — U d dl Z 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
  ej        e¦  «        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)Ú
deprecated)ÚKernelÚRegistrationHandlec                   ón   — 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dS )ÚFakeImplHolderz/A holder where one can register a fake impl to.Úqualnamec                 ó"   — || _         g | _        d S ©N)r	   Úkernels)Úselfr	   s     úV/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/_library/fake_impl.pyÚ__init__zFakeImplHolder.__init__   s   € Ø%ˆŒð &(ˆŒˆˆó    c                 óP   — t          | j        ¦  «        dk    rd S | j        d         S )Nr   éÿÿÿÿ)Úlenr   )r   s    r   ÚkernelzFakeImplHolder.kernel   s)   € åˆtŒ|ÑÔ Ò!Ð!Ø�4ØŒ|˜BÔÐr   c                 ó    — t          d¦  «        ‚)NzUnable to directly set kernel.©ÚRuntimeError)r   Úvalues     r   r   zFakeImplHolder.kernel"   s   € åÐ;Ñ<Ô<Ð<r   T©Úallow_overrideÚfuncÚsourceÚreturnc                óÔ  ‡ ‡— |s¦‰ 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         	                    ‰¦  «         ˆˆ fd	„}t          ‰ j        ‰ ¦  «        }	 |                     ‰ j        |d|¬
¦  «         nI# t          $ r< t                               d‰ j        ¦  «         ‰ j                             ‰¦  «         ‚ w xY wt!          |¦  «        }|S )z|Register a fake impl.

        Returns a RegistrationHandle that one can use to de-register this
        fake impl.
        Nz!register_fake(...): the operator z' already has a fake impl registered at ú.ÚMetaz³ already has a 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 a fake impl; instead, the operator will decompose into its constituents and those can have fake impls defined on them.c                  ó<   •— ‰j                              ‰ ¦  «         d S r   )r   Úremove)r   r   s   €€r   Úderegister_fake_kernelz7FakeImplHolder.register.<locals>.deregister_fake_kernelR   s   ø€ ØŒL×Ò Ñ'Ô'Ð'Ð'Ð'r   r   z"Failed to register fake_impl '%s':)r   r   r	   r   ÚtorchÚ_CÚ%_dispatch_has_kernel_for_dispatch_keyr   r   ÚappendÚconstruct_meta_kernelÚimplÚ	ExceptionÚlogÚinfor#   r   )	r   r   r   Úlibr   r$   Úmeta_kernelÚhandler   s	   `       @r   ÚregisterzFakeImplHolder.register&   sÏ  øø€ ð ð 	ØŒ{Ð&Ý"ð-¸¼ð -ð -à”{Ô)ð-ð -ð -ñô ð õ
 Œx×=Ò=¸d¼mÈVÑTÔTð Ý"ð&¸¼ð &ð &ð &ñô ð õ Œx×=Ò=Ø”Ð:ñô ð õ #ð<¸¼ð <ð <ð <ñ
ô 
ð 
õ ˜˜fÑ%Ô%ˆØŒ×Ò˜FÑ#Ô#Ð#ð	(ð 	(ð 	(ð 	(ð 	(ð 	(õ ,¨D¬M¸4Ñ@Ô@ˆð	Ø�HŠH�T”] K°ÈˆHÑWÔWÐWÐWøÝð 	ð 	ð 	Ý�HŠHØ4Ø”ñô ð ð ŒL×Ò Ñ'Ô'Ð'Øð	øøøõ $Ð$:Ñ;Ô;ˆØˆs   Ã1D ÄAEN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   Úpropertyr   Úsetterr   r   r1   © r   r   r   r      sª   € € € € € Ø9Ð9ð( ð (ð (ð (ð (ð ð ð  ñ „Xð ð
 „]ð=ð =ñ „]ð=ð CGð;ð ;ð ;Øð;Ø&)ð;à	ð;ð ;ð ;ð ;ð ;ð ;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xY w Y   d S )Nr<   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_ctxn   s8   ø€ ÝØð Oð O˜vð Oð Oð Oñ	ô 	ð 	r   )r   ÚAssertionErrorr   Úset_ctx_getter)ÚargsÚkwargsr?   r   r:   r	   s      @€€r   r/   z*construct_meta_kernel.<locals>.meta_kernelh   sÏ   øø€ àÔ"Ð*Ý Ð!KÑLÔLÐLØ!Ô(Ô/ˆð
	ð 
	ð 
	ð 
	ð 
	ð 
	õ ˜LÑ)Ô)ð 	<ð 	<Ø*Ð#Ô*¨DÐ;°FÐ;Ð;ð	<ð 	<ð 	<ð 	<ñ 	<ô 	<ð 	<ð 	<ð 	<ð 	<ð 	<ð 	<øøøð 	<ð 	<ð 	<ð 	<ð 	<ð 	<s   ºAÁAÁA)r   r@   Ú	functoolsÚwrapsr   )r	   r:   r/   s   `` r   r)   r)   d   s^   øø€ ØÔÐ&ÝÐGÑHÔHÐHå„_Ð%Ô,Ô1Ñ2Ô2ð<ð <ð <ð <ð <ñ 3Ô2ð<ð( Ðr   c                  ó   — d S r   r9   r9   r   r   Úget_nonerG   €   s   € Øˆ4r   Úglobal_ctx_getterc              #   ó8   K  — t           }	 | a d V — |a d S # |a w xY wr   )rH   )Ú
ctx_getterÚprevs     r   rA   rA   ‡   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   rO   rR   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 ©NrU   )Únew_dynamic_size©r   rV   rW   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 a 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.)rQ   Úallow_dynamic_output_shape_opsr%   Ú_subclassesÚfake_tensorÚDynamicOutputShapeExceptionrR   Ú
isinstanceÚSymIntÚ
ValueErrorÚallocate_sizer[   s      r   rZ   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   )r2   r3   r4   r5   r   r   ÚFutureWarningr%   rc   r\   rZ   r9   r   r   rM   rM   ’   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   rM   c                 ó†   — |                       ¦   «         }t          j        j        j                             |||¬¦  «         |S rY   )r\   r%   ÚfxÚexperimentalÚsymbolic_shapesÚ_constrain_range_for_size)rP   Úmin_valÚmax_valÚresults       r   re   re   ì   sG   € Ø×-Ò-Ñ/Ô/€FÝ	„HÔÔ)×CÒCØ�G ð Dñ ô ð ð €Mr   )r   N)Ú
contextlibrD   ÚloggingÚcollections.abcr   Útyping_extensionsr   r%   Útorch._library.utilsr   r   Ú	getLoggerr2   r,   r   r6   r)   rG   rH   Ú__annotations__ÚcontextmanagerrA   rM   re   r9   r   r   ú<module>rw      sl  ðà Ð Ð Ð Ð Ø Ð Ð Ð Ø €€€Ø $Ð $Ð $Ð $Ð $Ð $Ø (Ð (Ð (Ð (Ð (Ð (à €€€Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;ð €gÔ˜Ñ!Ô!€ðRð Rð Rð Rð Rñ Rô Rð Rðj Cð ¸>ð Èhð ð ð ð ð8ð ð ð 'Ð �8Ð &Ð &Ñ &ð Ôð!ð !ñ Ôð!ðW8ð W8ð W8ð W8ð W8ñ W8ô W8ð W8ðtð ð ð ð ð r   