ó
    EñiÌ"  ã                   óÆ   • % S SK r S SKrS SKJr  S SKJr  S SKrS SKJrJ	r	   " S S5      r
S\S\
S	\4S
 jrS r\q\\S'   \ R                   S 5       r " S S5      rSS jrg)é    N)ÚCallable)Ú
deprecated)ÚKernelÚRegistrationHandlec                   óx   • \ rS rSrSrS\4S jr\S 5       r\R                  S 5       rSS.S	\
S
\S\4S jjrSrg)ÚFakeImplHolderé   z0A holder where one can register an fake impl to.Úqualnamec                 ó   • Xl         / U l        g ©N)r
   Úkernels)Úselfr
   s     ÚU/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/_library/fake_impl.pyÚ__init__ÚFakeImplHolder.__init__   s   € Ø%Œð &(ˆ�ó    c                 óT   • [        U R                  5      S:X  a  g U R                  S   $ )Nr   éÿÿÿÿ)Úlenr   )r   s    r   ÚkernelÚFakeImplHolder.kernel   s%   € äˆt�|‰|Ó Ó!ØØ�|‰|˜BÑÐr   c                 ó   • [        S5      e)NzUnable to directly set kernel.©ÚRuntimeError)r   Úvalues     r   r   r      s   € äÐ;Ó<Ð<r   F©Úallow_overrideÚfuncÚsourceÚreturnc                ó†  ^ ^• U(       dÍ  T R                   b0  [        ST R                   ST R                   R                   S35      e[        R
                  R                  T R                  S5      (       a  [        ST R                   S35      e[        R
                  R                  T R                  S5      (       a  [        ST R                   S35      e[        X5      mT R                  R                  T5        UU 4S jn[        T R                  T 5      nUR                  T R                  USUS	9  [        U5      nU$ )
zeRegister 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                  ó<   >• TR                   R                  T 5        g r   )r   Úremove)r   r   s   €€r   Úderegister_fake_kernelÚ7FakeImplHolder.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   ÚregisterÚFakeImplHolder.register"   s,  ù€ ö Ø�{‰{Ñ&Ü"Ø7¸¿¹°ð G>à—{‘{×)Ñ)Ð*¨!ð-óð ô
 �x‰x×=Ñ=¸d¿m¹mÈV×TÑTÜ"Ø7¸¿¹°ð G%ð &óð ô �x‰x×=Ñ=Ø—‘Ð:÷ñ ô #Ø7¸¿¹°ð G;ð <ó
ð 
ô ˜Ó%ˆØ�‰×Ñ˜FÔ#ö	(ô ,¨D¯M©M¸4Ó@ˆØ�‰�—‘ ¨VÀNˆÑSä#Ð$:Ó;ˆØˆr   )r   r
   N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ústrr   Úpropertyr   Úsetterr   r   r2   Ú__static_attributes__© r   r   r   r      sf   † Ù:ð( ô (ð ñ ó ð ð
 ‡]�]ñ=ó ð=ð CHò3Øð3Ø&)ð3à	÷3ð 3r   r   r
   Úfake_impl_holderr    c                 ó¢   ^ ^• TR                   c  [        S5      e[        R                  " TR                   R                  5      UU 4S j5       nU$ )Nú(fake_impl_holder.kernel must not be Nonec                  óà   >^• TR                   c  [        S5      eTR                   R                  mUU4S jn[        U5         TR                   " U 0 UD6sS S S 5        $ ! , (       d  f       g = f)Nr@   c                  ó&   >• [        T  ST S35      e)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_ctxÚ@construct_meta_kernel.<locals>.meta_kernel.<locals>.error_on_ctxb   s'   ø€ ÜØ�*˜B˜v˜hð 'Nð Oó	ð 	r   )r   ÚAssertionErrorr   Úset_ctx_getter)ÚargsÚkwargsrC   r   r>   r
   s      @€€r   r0   Ú*construct_meta_kernel.<locals>.meta_kernel\   sZ   ù€ à×"Ñ"Ñ*Ü Ð!KÓLÐLØ!×(Ñ(×/Ñ/ˆö
	ô ˜LÕ)Ø#×*Ò*¨DÐ;°FÑ;÷ *×)×)ús   ÁAÁ
A-)r   rE   Ú	functoolsÚwrapsr   )r
   r>   r0   s   `` r   r-   r-   X   sK   ù€ Ø×ÑÑ&ÜÐGÓHÐHä‡_‚_Ð%×,Ñ,×1Ñ1Ó2õ<ó 3ð<ð( Ðr   c                  ó   • g r   r=   r=   r   r   Úget_nonerM   t   s   € Ør   Úglobal_ctx_getterc              #   ó8   #   • [         n U q S v •  Uq g ! Uq f = f7fr   )rN   )Ú
ctx_getterÚprevs     r   rF   rF   {   s&   é € ô €Dð!Ø&ÐÛà Ñø˜DÑüs   ‚Š �“—c                   óŠ   • \ rS rSrSrS r\" S\S9SSS.S	\R                  4S
 jj5       r
SSS.S	\R                  4S jjrSrg)ÚFakeImplCtxé†   zG
Context object for writing fake implementations for custom operators.
c                 ó>   • Xl         UR                  U l        X l        g r   )Ú
_fake_modeÚ	shape_envÚ
_shape_envÚ_op)r   rV   rY   s      r   r   ÚFakeImplCtx.__init__‹   s   € Ø$ŒØ$×.Ñ.ˆŒØ�r   zM`create_unbacked_symint` is deprecated, please use `new_dynamic_size` instead)Úcategoryé   N©ÚminÚmaxr    c                ó    • U R                  XS9$ ©Nr]   )Únew_dynamic_size©r   r^   r_   s      r   Úcreate_unbacked_symintÚ"FakeImplCtx.create_unbacked_symint�   s   € ð
 ×$Ñ$¨Ð$Ð6Ð6r   r   c                ó®  • U R                   b  U R                   R                  (       d3  [        R                  R                  R                  U R                  5      e[        U[        R                  5      (       d  [        U[        R                  5      (       a  [        SU SU S35      eUS:  a  [        SU S35      e[        U R                   X5      $ )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.)rX   Úallow_dynamic_output_shape_opsr)   Ú_subclassesÚfake_tensorÚDynamicOutputShapeExceptionrY   Ú
isinstanceÚSymIntÚ
ValueErrorÚallocate_sizerc   s      r   rb   Ú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Ñ,IÜØ+¨C¨5°°s°eð <)ð *óð ð �‹7ÜØ+¨C¨5ð 1&ð 'óð ô ˜TŸ_™_¨cÓ7Ð7r   )rV   rY   rX   )r4   r5   r6   r7   r8   r   r   ÚFutureWarningr)   rl   rd   rb   r<   r=   r   r   rS   rS   †   s\   † ñòñ
 ØWØñð -.°4ò 7¸E¿L¹Lô 7ó	ð7ð '(¨Tò F8°e·l±l÷ F8ð F8r   rS   c                 óŠ   • U R                  5       n[        R                  R                  R                  R                  X1US9  U$ ra   )rd   r)   ÚfxÚexperimentalÚsymbolic_shapesÚ_constrain_range_for_size)rW   Úmin_valÚmax_valÚresults       r   rn   rn   à   s@   € Ø×-Ñ-Ó/€FÜ	‡H�H×Ñ×)Ñ)×CÑCØ ð Dñ ð €Mr   )r   N)Ú
contextlibrJ   Úcollections.abcr   Útyping_extensionsr   r)   Útorch._library.utilsr   r   r   r9   r-   rM   rN   Ú__annotations__ÚcontextmanagerrF   rS   rn   r=   r   r   Ú<module>r      s|   ðä Û Ý $Ý (ã ß ;÷Jñ JðZ Cð ¸>ð Èhô ò8ð 'Ð �8Ó &ð ×Ññ!ó ð!÷W8ñ W8õtr   