U
    yh.                  
   @  s  d Z ddlmZ ddlZddlmZmZ ddlZddlm	Z	m
Z
 erdddlmZmZmZmZmZmZ dd	 Zd
d ZedGddddddddZedHdddddddddZedIdddddddddZedJddddddddddZedKdddddddddd ZedLddddddd!d"d#ZeZedMddddddd!d$d%ZeZedNdddddd&d'ZedOddd(ddddd)d*d+ZedPddd(ddddd)d,d-ZeZ edQddddd(dddd.d/d0Z!edRddddd(dddd.d1d2Z"edSddddd(dddd.d3d4Z#dTddd(dd5d6d7Z$dUddd(dd5d8d9Z%e%Z&dVddddd:d;d<Z'dWdd>ddddddd?d@dAZ(dXdd>ddddddd?dBdCZ)dYddddDdEdFZ*dS )Zz Implementation of reduction operations, to be wrapped into arrays, dtypes etc
in the 'public' layer.

Anything here only deals with torch objects, e.g. "dtype" is a torch.dtype instance etc
    )annotationsN)OptionalTYPE_CHECKING   )_dtypes_impl_util)	ArrayLikeAxisLike	DTypeLikeKeepDimsNotImplementedTypeOutArrayc                   s   t  d fdd	}|S )z
    Generically handle axis arguments in reductions.
    axis is *always* the 2nd arg in the function so no need to have a look at its signature
    Nc                   sN   |d k	rt || j}|dkr<t j| jdd}| |} d} | |f||S )N r   axisr   )r   normalize_axis_tuplendimZexpand_shapeshapeZreshape)ar   argskwdsZnewshapefuncr   O/var/www/html/venv/lib/python3.8/site-packages/torch/_numpy/_reductions_impl.pywrapped"   s    
z"_deco_axis_expand.<locals>.wrapped)N)	functoolswraps)r   r   r   r   r   _deco_axis_expand   s    r   c                 C  s&   | dkr|} | j s"| js"t jS | S )a$  Return a dtype that is real or complex floating-point.

    For inputs that are boolean or integer dtypes, this returns the default
    float dtype; inputs that are complex get converted to the default complex
    dtype; real floating-point dtypes (`float*`) get passed through unchanged
    N)is_floating_point
is_complexr   default_dtypesfloat_dtype)dtypeZother_dtyper   r   r   _atleast_float3   s
    
r$   F)keepdimsr   r	   r   r   r   r%   c                C  s
   |  |S N)count_nonzeror&   r   r   r   r(   A   s    r(   zOptional[OutArray]r   r   outr%   c                C  sH   |   rtd| j dt|}| jtjkr<| tj} t	| |S )Nzargmax with dtype=.)
r    NotImplementedErrorr#   r   allow_only_single_axistorchbooltouint8argmaxr)   r   r   r   r2   F   s    
r2   c                C  sH   |   rtd| j dt|}| jtjkr<| tj} t	| |S )Nzargmin with dtype=r+   )
r    r,   r#   r   r-   r.   r/   r0   r1   argminr)   r   r   r   r3   Z   s    
r3   )wherer   )r   r   r*   r%   r4   c                C  s,   t |}|d kri nd|i}tj| f|S Ndim)r   r-   r.   anyr   r   r*   r%   r4   axis_kwr   r   r   r7   n   s    	
r7   c                C  s,   t |}|d kri nd|i}tj| f|S r5   )r   r-   r.   allr8   r   r   r   r:   |   s    	
r:   r   r   r*   r%   initialr4   c                 C  s"   |   rtd| j | |S )Nzamax with dtype=)r    r,   r#   amaxr;   r   r   r   r=      s    	r=   c                 C  s"   |   rtd| j | |S )Nzamin with dtype=)r    r,   r#   aminr;   r   r   r   r>      s    	r>   c                 C  s   |  || | S r'   )r=   r>   r)   r   r   r   ptp   s    r?   zOptional[DTypeLike])r   r   r#   r*   r%   r<   r4   c                 C  sT   |d kst |tjst|tjkr,t j}|d kr8i nd|i}| jf d|i|S Nr6   r#   )	
isinstancer.   r#   AssertionErrorr/   r   r!   	int_dtypesumr   r   r#   r*   r%   r<   r4   r9   r   r   r   rD      s
    


rD   c                 C  sF   t |}|tjkrt j}|d kr*i nd|i}| jf d|i|S r@   )r   r-   r.   r/   r   r!   rC   prodrE   r   r   r   rF      s
    



rF   )r   r   r#   r*   r%   r4   c                C  s8   t || j}|d kri nd|i}| jf d|i|}|S r@   )r$   r#   mean)r   r   r#   r*   r%   r4   r9   resultr   r   r   rG      s    
rG   c          
      C  s6   |}t || j}t| |}|j||d}	t|	|S N)r6   Z
correction)r$   r#   r   cast_if_neededstd
r   r   r#   r*   Zddofr%   r4   Zin_dtypeZtensorrH   r   r   r   rK      s
    rK   c          
      C  s6   |}t || j}t| |}|j||d}	t|	|S rI   )r$   r#   r   rJ   varrL   r   r   r   rM     s
    rM   r   r   r#   r*   c                 C  sR   |t jkrt j}|d kr"| j}tj| |d\\} }t|| j	}| j
||dS Nr   )r   r#   )r.   r/   r   r!   rC   r#   r   axis_none_flattennormalize_axis_indexr   cumsumrN   r   r   r   rR     s    

rR   c                 C  sR   |t jkrt j}|d kr"| j}tj| |d\\} }t|| j	}| j
||dS rO   )r.   r/   r   r!   rC   r#   r   rP   rQ   r   cumprodrN   r   r   r   rS   -  s    

rS   )r   weightsc          	      C  s2  |d kr2t | |d}tj|  |  |jd}n| jjsB|  } | j|jkr|d kr^td|j	dkrptd|jd | j| krt
dt|| j	d d |j }|d	|}t| |}t| | ||d}t|||d}|| }|rt||| j	}|r*|j|jkr"t||j }||fS |S d S )
Nr   )r#   z;Axis must be specified when shapes of a and weights differ.r   z81D weights expected when shapes of a and weights differ.r   z5Length of weights not compatible with specified axis.)r   )rG   r.   	as_tensorZnumelr#   r   doubler   	TypeErrorr   
ValueErrorZbroadcast_toZswapaxesr   Zresult_type_implrD   r   Zapply_keepdimsclone)	r   r   rT   Zreturnedr%   rH   ZwsumZresult_dtype	numeratorr   r   r   averageA  s>    
r\   linear)interpolation)r   qr   r*   r%   r^   c          	      C  s   |r| j js t j}| |} | j tjkr8| tj} |d krV| 	 } |	 }d}nt
|| j}t
|}t
|| j }tj| |||dS )Nr   )r   r^   )r#   r   r   r!   r"   r0   r.   Zfloat16Zfloat32flattenr   r   r   r-   rJ   quantile)	r   r_   r   r*   overwrite_inputmethodr%   r^   r#   r   r   r   ra   v  s    


ra   c          	   	   C  s>   t |jtkr |t  j}|d }t| ||||||dS )Ng      Y@)r   rb   rc   r%   r^   )r   Zpython_type_for_torchr#   intr0   r!   r"   ra   )	r   r_   r   r*   rb   rc   r%   r^   Zqqr   r   r   
percentile  s    re   )r   r*   r%   c                 C  s   t | td||||dS )Ng      ?)r   rb   r*   r%   )ra   r.   rV   )r   r   r*   rb   r%   r   r   r   median  s    rf   )N)NN)NN)NNF)NNF)NNFNN)NNFNN)NNF)NNNFNN)NNNFNN)NNNF)NNNr   F)NNNr   F)NNN)NNN)NNF)NNFr]   F)NNFr]   F)NNFF)+__doc__
__future__r   r   typingr   r   r.    r   r   Z_normalizationsr   r	   r
   r   r   r   r   r$   r(   r2   r3   r7   r:   r=   maxr>   minr?   rD   rF   productrG   rK   rM   rR   rS   Z
cumproductr\   ra   re   rf   r   r   r   r   <module>   s   
                       	                                   8     	.     	    