o
    Zh.                     @  sr  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r2ddlmZmZmZmZmZmZ dd	 Zd
d ZedGdddHddZe		dIdddJddZe		dIdddJddZe			dKdddLdd Ze			dKdddLd!d"Ze					dMdNd$d%ZeZe					dMdNd&d'ZeZe			dKdJd(d)Ze						dOdPd,d-Ze						dOdPd.d/ZeZ e				dQdddRd0d1Z!e					dSdddRd2d3Z"e					dSdddRd4d5Z#			dTdUd6d7Z$			dTdUd8d9Z%e%Z&			dKdddVd;d<Z'				=	dWdd>dXdAdBZ(				=	dWdd>dXdCdDZ)				dYdZdEdFZ*dS )[z 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                   sT   |d urt || j}|dkrt j| jdd}| |} d} | |g|R i |S )N r   axisr   )r   normalize_axis_tuplendimZexpand_shapeshapeZreshape)ar   argskwdsZnewshapefuncr   L/var/www/auris/lib/python3.10/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u 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_float4   s
   
r%   F)keepdimsr   r   r   r	   r&   r   c                C  s
   |  |S r   )count_nonzero)r   r   r&   r   r   r   r'   B   s   
r'   outOptional[OutArray]c                C  H   |   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argmaxr   r   r(   r&   r   r   r   r2   G      
r2   c                C  r*   )Nzargmin with dtype=r+   )
r!   r,   r$   r   r-   r.   r/   r0   r1   argminr3   r   r   r   r5   [   r4   r5   )wherer6   r   c                C  0   t |}|d u ri nd|i}tj| fi |S Ndim)r   r-   r.   anyr   r   r(   r&   r6   axis_kwr   r   r   r:   o      
	r:   c                C  r7   r8   )r   r-   r.   allr;   r   r   r   r>   }   r=   r>   initialc                 C  "   |   rtd| j | |S )Nzamax with dtype=)r!   r,   r$   amaxr   r   r(   r&   r?   r6   r   r   r   rA         	
rA   c                 C  r@   )Nzamin with dtype=)r!   r,   r$   aminrB   r   r   r   rD      rC   rD   c                 C  s   |  || | S r   )rA   rD   r3   r   r   r   ptp   s   rE   r$   Optional[DTypeLike]c                 C  sT   |d u st |tjsJ |tjkrt j}|d u ri nd|i}| jdd|i|S Nr9   r$   r   )
isinstancer.   r$   r/   r   r"   	int_dtypesumr   r   r$   r(   r&   r?   r6   r<   r   r   r   rJ      s
   


rJ   c                 C  sF   t |}|tjkrt j}|d u ri nd|i}| jdd|i|S rG   )r   r-   r.   r/   r   r"   rI   prodrK   r   r   r   rL      s
   



rL   c                C  s8   t || j}|d u ri nd|i}| jdd|i|}|S rG   )r%   r$   mean)r   r   r$   r(   r&   r6   r<   resultr   r   r   rM      s   
rM   c          
      C  6   |}t || j}t| |}|j||d}	t|	|S N)r9   Z
correction)r%   r$   r   cast_if_neededstd
r   r   r$   r(   Zddofr&   r6   Zin_dtypeZtensorrN   r   r   r   rR      
   rR   c          
      C  rO   rP   )r%   r$   r   rQ   varrS   r   r   r   rU     rT   rU   c                 C  R   |t jkr
t j}|d u r| j}tj| |d\\} }t|| j	}| j
||dS Nr   )r   r$   )r.   r/   r   r"   rI   r$   r   axis_none_flattennormalize_axis_indexr   cumsumr   r   r$   r(   r   r   r   rZ        

rZ   c                 C  rV   rW   )r.   r/   r   r"   rI   r$   r   rX   rY   r   cumprodr[   r   r   r   r]   .  r\   r]   weightsc          	      C  s*  |d u rt | |d}tj|  |  |jd}n[| jjs!|  } | j|jkrZ|d u r/td|j	dkr8td|jd | j| krFt
dt|| j	d d |j }|d	|}t| |}t| | ||d}t|||d}|| }|r~t||| j	}|r|j|jkrt||j }||fS |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   )rM   r.   	as_tensorZnumelr$   r    doubler   	TypeErrorr   
ValueErrorZbroadcast_toZswapaxesr   Zresult_type_implrJ   r   Zapply_keepdimsclone)	r   r   r^   returnedr&   rN   ZwsumZresult_dtype	numeratorr   r   r   averageB  s>   
rg   linear)interpolationqri   c          	      C  s   |r	 | j jst j}| |} | j tjkr| tj} |d u r,| 	 } |	 }d}nt
|| j}t
|}t
|| j }tj| |||dS )Nr   )r   ri   )r$   r    r   r"   r#   r0   r.   Zfloat16Zfloat32flattenr   r   r   r-   rQ   quantile)	r   rj   r   r(   overwrite_inputmethodr&   ri   r$   r   r   r   rl   w  s   


rl   c          	   	   C  s>   t |jtkr|t  j}|d }t| ||||||dS )Ng      Y@)r   rm   rn   r&   ri   )r   Zpython_type_for_torchr$   intr0   r"   r#   rl   )	r   rj   r   r(   rm   rn   r&   ri   Zqqr   r   r   
percentile  s   rp   c                 C  s   t | td||||dS )Ng      ?)r   rm   r(   r&   )rl   r.   r`   )r   r   r(   rm   r&   r   r   r   median  s   rq   r   )r   r   r   r	   r&   r   )NN)r   r   r   r	   r(   r)   r&   r   )NNF)
r   r   r   r	   r(   r)   r&   r   r6   r   )NNFNN)r   r   r   r	   r(   r)   r&   r   r?   r   r6   r   )NNNFNN)r   r   r   r	   r$   rF   r(   r)   r&   r   r?   r   r6   r   )NNNF)r   r   r   r	   r$   rF   r(   r)   r&   r   r6   r   )NNNr   F)NNN)r   r   r   r	   r$   rF   r(   r)   )r   r   r^   r   )NNFrh   F)r   r   rj   r   r   r	   r(   r)   r&   r   ri   r   )NNFF)r   r   r(   r)   r&   r   )+__doc__
__future__r   r   typingr   r   r.    r   r   Z_normalizationsr   r	   r
   r   r   r   r   r%   r'   r2   r5   r:   r>   rA   maxrD   minrE   rJ   rL   productrM   rR   rU   rZ   r]   Z
cumproductrg   rl   rp   rq   r   r   r   r   <module>   s   
	8	.	