o
    h'4                     @   s  d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZ d dlZd dlmZ d dlmZmZmZmZmZmZ d dlmZmZ e
dedejd	efd
dZe
dedejd	efddZe
dedejd	efddZe
dddejd	dfddZdd Zdeded	efddZdd ZG dd dZdedefddZ dedefddZ!ddded ed!e"fd"d#Z#ddd$e$d!e"fd%d&Z%d'd( Z&d)ed	efd*d+Z'dS ),    N)wraps)chain)Callable
NamedTupleOptionaloverloadSequenceTuple)ELEMENTWISE_TYPE_PROMOTION_KINDNumber
NumberType	ShapeType
TensorLikeTensorLikeType)tree_flattentree_unflattenadtypereturnc                 C      d S N r   r   r   r   R/var/www/html/ai/venv/lib/python3.10/site-packages/torch/_prims_common/wrappers.py_maybe_convert_to_dtype      r   c                 C   r   r   r   r   r   r   r   r      r   c                 C   r   r   r   r   r   r   r   r      r   c                 C   r   r   r   r   r   r   r   r   $   r   c                    sz   t | tr| j kr|  S | S t | trt | S t | tr-t fdd| D S | d u r3d S t	dt
|  d)Nc                 3       | ]}t | V  qd S r   r   .0xr   r   r   	<genexpr>2       z*_maybe_convert_to_dtype.<locals>.<genexpr>zReceived type z& that is neither a tensor or a number!)
isinstancer   r   tor   utilsdtype_to_type_ctorr   tuple
ValueErrortyper   r   r!   r   r   *   s   




typc                 C   s^   t | tsdt|  d}t|tt| |s+d|  dt|  d| d}t||| S )NzFound unknown type z  when trying to convert scalars!zScalar z	 of type z cannot be safely cast to type !)r$   r   r*   r)   r&   is_weakly_lesser_type)r   r+   msgr   r   r   _maybe_convert_to_type;   s   
r/   c                 C   s4   t |dr|jD ]}t| |dr dS qdS | |u S )N__args__)r+   
annotationTF)hasattrr0   _annotation_has_type)r+   r1   r   r   r   r   r3   F   s   

r3   c                   @   sB   e Zd ZdZdddedeee  fddZde	d	e	fd
dZ
dS )"elementwise_type_promotion_wrappera  
    Adds elementwise type promotion to a Python reference implementation.

    Takes two kwargs, type_promoting_args and type_promotion_kind.

    type_promoting_args must be a string Sequence specifiying the argument names of all
    arguments that participate in type promotion (and should be type promoted). If the
    arg specifies a Sequence-type then every element of the Sequence will participate in
    type promotion.

    type_promotion_kind must be one of the kinds specified by ELEMENTWISE_TYPE_PROMOTION_KIND.
    See its documentation for details.

    Other type promotion behavior, like validating the Python type of scalar arguments, must
    be handled separately.
    N)type_promoting_argstype_promotion_kindr5   c                C   s   || _ || _d S r   )type_promoting_arg_namesr6   )selfr6   r5   r   r   r   __init__b   s   
z+elementwise_type_promotion_wrapper.__init__fnr   c                    s,   t  t  fdd}|_|S )Nc                     s   j | i | t fddjD }t|d }tj|dji\ fddjD } j| d	i  j}t	|t
rHt|S t	|trXtfdd|D S tdt| )
Nc                 3   s(    | ]}| j  v r j | V  qd S r   )	argumentskeysr   )boundr   r   r"   q   s    zKelementwise_type_promotion_wrapper.__call__.<locals>._fn.<locals>.<genexpr>r   r6   c                    s,   i | ]}| j  v r|t j | qS r   )r;   r<   r   r   )r=   compute_dtyper   r   
<dictcomp>}   s
    zLelementwise_type_promotion_wrapper.__call__.<locals>._fn.<locals>.<dictcomp>c                 3   r   r   r   r   )result_dtyper   r   r"      r#   zUnhandled result type: r   )bindr(   r7   r   r&   elementwise_dtypesr6   r;   updater$   r   r   r   AssertionErrorr*   )argskwargsr5   flattened_type_promoting_argspromoted_argsresultr:   r8   sig)r=   r>   r@   r   _fnn   s(   



z8elementwise_type_promotion_wrapper.__call__.<locals>._fninspect	signaturer   __signature__)r8   r:   rL   r   rJ   r   __call__k   s
   
z+elementwise_type_promotion_wrapper.__call__)__name__
__module____qualname____doc__r
   r   r   strr9   r   rQ   r   r   r   r   r4   P   s    

	r4   outshapec                 C   s>   t | j|r	dS |  dkrdt| j d}t| dS )NFr   zCAn output with one or more elements was resized since it had shape a   which does not match the required output shape {str(shape)}. This behavior is deprecated, and in a future PyTorch release outputs will not be resized unless they have zero elements. You can explicitly reuse an out tensor t by resizing it, inplace, to zero elements with t.resize_(0).T)r&   
same_shaperX   numelrV   warningswarn)rW   rX   r.   r   r   r   _resize_output_check   s   
r]   c                 C   s   t | |r
| |S | S r   )r]   resize_)rW   rX   r   r   r   _maybe_resize_out   s   

r_   F)exact_dtype	copy_fromcopy_tor`   c                    sv    j j krd j j }t||r$t jjk fdd nttj jjd fdd  S )NzXAttempting to copy from device {} to device {}, but cross-device copies are not allowed!c                         d j  dj  dS )Nz"Expected out tensor to have dtype z	 but got z insteadr!   r   ra   rb   r   r   <lambda>   s   
 z _safe_copy_out.<locals>.<lambda>)	cast_fromcast_toc                      rc   )NzAttempting to cast from z to out tensor with dtype z0, but this can't be cast because it is not safe!r!   r   rd   r   r   re      s    )	deviceformatRuntimeErrortorch_checkr   r&   can_safe_cast_tocopy_)ra   rb   r`   r.   r   rd   r   _safe_copy_out   s   

ro   	out_namesc                    s>   t dkst dksJ dtdtf fdd}|S )Nr      r:   r   c              	      s   rt nttdd ttD  }rt ntdj dd D td t	fdd D t
dd	 fd
d
}tjdtjjd|d}jj|fv s_J tj |f}tj|d|_j|_||jd< |jd< |S )z?
        Adds the out parameter to a Python reference.
        c                 s   s    | ]}t V  qd S r   r   )r   _r   r   r   r"      s    z4out_wrapper.<locals>._out_wrapper.<locals>.<genexpr>return_types_c                 S   s   g | ]}|t fqS r   rr   )r   or   r   r   
<listcomp>   s    z5out_wrapper.<locals>._out_wrapper.<locals>.<listcomp>)rh   r   c                 3   s    | ]}| j v V  qd S r   )
parameters)r   p)rK   r   r   r"      r#   N)rW   c                    s  r d urD ]}t  |}||vr|||< q|i |ttr&s5ttr3ttks5J  d urrPt tsBJ t j t d n2t tsWJ t	t tk fdd t
 D ]\}}t||j t||d qmn r S   S )N)ra   rb   r`   c                      s   dt  dt   S )Nzexpected tuple of z elements but got )lenr   rW   rI   r   r   re     s    z@out_wrapper.<locals>._out_wrapper.<locals>._fn.<locals>.<lambda>)getattrr$   r   r	   ry   r_   rX   ro   rk   _check_typezip)rW   rE   rF   kout_attrrru   )r`   factory_kwargsr:   is_factory_fn	is_tensorrp   return_typerz   r   rL      s<   
z.out_wrapper.<locals>._out_wrapper.<locals>._fnrW   )kinddefaultr1   )rw   return_annotationr   )r   r	   r(   rangery   r   rR   rN   rO   allr   	ParameterKEYWORD_ONLYr   emptyr   rw   values	SignaturerP   __annotations__)r:   out_typerL   	out_paramparamsr`   r   rp   )r   r:   r   r   rK   r   _out_wrapper   s<   
 3

z!out_wrapper.<locals>._out_wrapper)ry   r   )r`   rp   r   r   r   r   out_wrapper   s   Zr   c                    s>   fddG fdddt jj t fdd}|S )Nc                    sN   t j  t jt jjj} | i |W  d    S 1 s w   Y  d S r   )rk   _C_AutoDispatchBelowAutograd&_dispatch_tls_is_dispatch_key_excludedDispatchKeyADInplaceOrView)rE   rF   old)primr   r   redispatch_prim&  s   $z0backwards_not_supported.<locals>.redispatch_primc                       s(   e Zd Ze fddZedd ZdS )z6backwards_not_supported.<locals>.BackwardsNotSupportedc                    s   t ||\}} ||S r   )r   )ctx	args_spec	flat_argsrE   rF   r   r   r   forward.  s   
z>backwards_not_supported.<locals>.BackwardsNotSupported.forwardc                 W   s   t d)Nzbackwards not supported on prim)rj   )r   rE   r   r   r   backward3  s   z?backwards_not_supported.<locals>.BackwardsNotSupported.backwardN)rR   rS   rT   staticmethodr   r   r   r   r   r   BackwardsNotSupported-  s
    r   c                     sF   t | |f\}}t rtdd |D r j|g|R  S | |S )Nc                 s   s"    | ]}t |tjr|jV  qd S r   )r$   rk   Tensorrequires_grad)r   r   r   r   r   r"   :  s    
zBbackwards_not_supported.<locals>._autograd_impl.<locals>.<genexpr>)r   rk   is_grad_enabledanyapply)rE   rF   r   r   )r   r   r   r   _autograd_impl7  s   
z/backwards_not_supported.<locals>._autograd_impl)rk   autogradFunctionr   )r   r   r   )r   r   r   r   backwards_not_supported%  s
   
r   r:   c                    s(   t  }t  fdd}||_|S )zQ
    Allows unary operators that accept tensors to work with Python numbers.
    c                     s~   t | dkr8t| d tr8tt| d }t| }tj| d |d|d<  |i |}t|tj	s4J |
 S  | i |S )Nr   r!   )ry   r$   r   r&   type_to_dtyper*   listrk   tensorr   item)rE   rF   r   args_rI   r:   r   r   rL   W  s   z-elementwise_unary_scalar_wrapper.<locals>._fnrM   )r:   rK   rL   r   r   r    elementwise_unary_scalar_wrapperQ  s
   
r   )(rN   r[   	functoolsr   	itertoolsr   typingr   r   r   r   r   r	   rk   torch._prims_common_prims_commonr&   r
   r   r   r   r   r   torch.utils._pytreer   r   r   r   r*   r/   r3   r4   r]   r_   boolro   rV   r   r   r   r   r   r   r   <module>   sD      
A
a,