U
    9%e'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
eejeddd	Ze
eejedd
d	Ze
eejeddd	Ze
dejdddd	Zdd	 ZeeedddZdd ZG dd dZeedddZ eedddZ!ddeee"dddZ#dde$e"dd d!Z%d"d# Z&eed$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   s   d S N r   r   r   r   [/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/_prims_common/wrappers.py_maybe_convert_to_dtype   s    r   c                 C   s   d S r   r   r   r   r   r   r      s    c                 C   s   d S r   r   r   r   r   r   r      s    c                 C   s   d S r   r   r   r   r   r   r   $   s    c                    s~   t | tr"| j kr|  S | S t | tr:t | S t | trZt fdd| D S | d krfd S t	dt
|  dd S )Nc                 3   s   | ]}t | V  qd S r   r   .0xr   r   r   	<genexpr>2   s     z*_maybe_convert_to_dtype.<locals>.<genexpr>zReceived type z& that is neither a tensor or a number!)
isinstancer   r   tor   utilsZdtype_to_type_ctorr   tuple
ValueErrortyper   r   r   r   r   *   s    




)r   typr   c                 C   s^   t | ts"dt|  d}t|tt| |sVd|  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"   Z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 | |kS )N__args__)r&   
annotationTF)hasattrr*   _annotation_has_type)r&   r+   r   r   r   r   r-   F   s    

r-   c                   @   s>   e Zd ZdZddeeee  dddZe	e	d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_kindr/   c                C   s   || _ || _d S r   )type_promoting_arg_namesr0   )selfr0   r/   r   r   r   __init__b   s    z+elementwise_type_promotion_wrapper.__init__fnr   c                    s,   t  t  fdd}|_|S )Nc                     s   j | | t fddjD }t|d }tj|dji\ fddjD } j| f  j}t	|t
rt|S t	|trtfdd|D S tdt| d S )	Nc                 3   s&   | ]}| j  kr j | V  qd S r   )	argumentskeysr   )boundr   r   r   q   s   zKelementwise_type_promotion_wrapper.__call__.<locals>._fn.<locals>.<genexpr>r   r0   c                    s,   i | ]$}| j  kr|t j | qS r   )r6   r7   r   r   )r8   compute_dtyper   r   
<dictcomp>}   s    zLelementwise_type_promotion_wrapper.__call__.<locals>._fn.<locals>.<dictcomp>c                 3   s   | ]}t | V  qd S r   r   r   )result_dtyper   r   r      s     zUnhandled result type: )bindr#   r1   r   r"   Zelementwise_dtypesr0   r6   updater    r   r   r   AssertionErrorr%   )argskwargsr/   Zflattened_type_promoting_argsZpromoted_argsresultr5   r2   sig)r8   r9   r;   r   _fnn   s(    



z8elementwise_type_promotion_wrapper.__call__.<locals>._fninspect	signaturer   __signature__)r2   r5   rD   r   rB   r   __call__k   s
    
z+elementwise_type_promotion_wrapper.__call__)__name__
__module____qualname____doc__r
   r   r   strr3   r   rI   r   r   r   r   r.   P   s   
	r.   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"   Z
same_shaperQ   ZnumelrN   warningswarn)rP   rQ   r(   r   r   r   _resize_output_check   s    
rT   c                 C   s   t | |r| |S | S d S r   )rT   Zresize_rO   r   r   r   _maybe_resize_out   s    

rU   F)exact_dtype	copy_fromcopy_torV   c                    sv    j j kr$d j j }t||rHt 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                      s   d j  dj  dS )Nz"Expected out tensor to have dtype z	 but got z insteadr   r   rX   rY   r   r   <lambda>       z _safe_copy_out.<locals>.<lambda>)Z	cast_fromZcast_toc                      s   d j  dj  dS )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   rZ   r   r   r[      r\   )	deviceformatRuntimeErrortorch_checkr   r"   Zcan_safe_cast_toZcopy_)rX   rY   rV   r(   r   rZ   r   _safe_copy_out   s      
rb   )	out_namesrV   c                    s<   t dks t dks tttd fdd}|S )Nr      r4   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kst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>Zreturn_types_c                 S   s   g | ]}|t fqS r   re   )r   or   r   r   
<listcomp>   s     z5out_wrapper.<locals>._out_wrapper.<locals>.<listcomp>)r]   r   c                 3   s   | ]}| j kV  qd S r   )
parameters)r   p)rC   r   r   r      s     N)rP   c                    s  r0 d k	r0D ]}t  |}||kr|||< q||ttrHsfttrbttksft d k	rrt tstt j t d n^t tstt	
t tk fdd t D ]"\}}t||j t||d qn r
 S   S )NrW   c                      s   dt  dt   S )Nzexpected tuple of z elements but got )lenr   rP   rA   r   r   r[     r\   z@out_wrapper.<locals>._out_wrapper.<locals>._fn.<locals>.<lambda>)getattrr    r   r	   rk   r>   rU   rQ   rb   r`   _check_typezip)rP   r?   r@   kZout_attrrrg   )rV   factory_kwargsr5   is_factory_fn	is_tensorrc   return_typerl   r   rD      s:    


z.out_wrapper.<locals>._out_wrapper.<locals>._fnrP   )kinddefaultr+   )ri   return_annotationr   )r   r	   r#   rangerk   r   rJ   rF   rG   allr   	ParameterKEYWORD_ONLYrx   emptyr>   r   ri   values	SignaturerH   __annotations__)r5   Zout_typerD   Z	out_paramparamsrV   rt   rc   )rr   r5   rs   ru   rC   r   _out_wrapper   s@    
 
 3 

z!out_wrapper.<locals>._out_wrapper)rk   r>   r   )rV   rc   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              
      s>   t j * t jt jjj} | |W  5 Q R  S Q R X d S r   )r`   Z_CZ_AutoDispatchBelowAutogradZ&_dispatch_tls_is_dispatch_key_excludedZDispatchKeyZADInplaceOrView)r?   r@   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_argsr?   r@   r   r   r   forward.  s    z>backwards_not_supported.<locals>.BackwardsNotSupported.forwardc                 W   s   t dd S )Nzbackwards not supported on prim)r_   )r   r?   r   r   r   backward3  s    z?backwards_not_supported.<locals>.BackwardsNotSupported.backwardN)rJ   rK   rL   staticmethodr   r   r   r   r   r   BackwardsNotSupported-  s   r   c                     sH   t | |f\}}t r:tdd |D r: j|f| S | |S d S )Nc                 s   s    | ]}t |tjr|jV  qd S r   )r    r`   TensorZrequires_grad)r   r   r   r   r   r   :  s     zBbackwards_not_supported.<locals>._autograd_impl.<locals>.<genexpr>)r   r`   Zis_grad_enabledanyapply)r?   r@   r   r   )r   r   r   r   _autograd_impl7  s    z/backwards_not_supported.<locals>._autograd_impl)r`   ZautogradFunctionr   )r   r   r   )r   r   r   r   backwards_not_supported%  s
    
r   r4   c                    s(   t  }t  fdd}||_|S )zQ
    Allows unary operators that accept tensors to work with Python numbers.
    c                     sv   t | dkrlt| d trltt| d }t| }tj| d |d|d<  ||}t|tj	sdt
| S  | |S )Nr   r   )rk   r    r   r"   Ztype_to_dtyper%   listr`   Ztensorr   r>   item)r?   r@   r   args_rA   r5   r   r   rD   W  s    
z-elementwise_unary_scalar_wrapper.<locals>._fnrE   )r5   rC   rD   r   r   r    elementwise_unary_scalar_wrapperQ  s
    
r   )(rF   rR   	functoolsr   	itertoolsr   typingr   r   r   r   r   r	   r`   Ztorch._prims_commonZ_prims_commonr"   r
   r   r   r   r   r   Ztorch.utils._pytreer   r   r   r   r%   r)   r-   r.   rT   rU   boolrb   rN   r   r   r   r   r   r   r   <module>   s>     
A  a,