
    [Th                         S r SSKrSSKrSSKJr  S r " S S\\5      r " S S	\	\
5      rS
 rS rSS jrSS jrS rS rS rSS.S jrS rS rS rSS jrS rg)zLAssorted utilities, which do not need anything other then torch and stdlib.
    N   )_dtypes_implc                 h    [        U [        5      (       a  g [        U 5        g! [         a     gf = f)NFT)
isinstancestrlen	Exception)seqs    J/var/www/auris/envauris/lib/python3.13/site-packages/torch/_numpy/_util.pyis_sequencer      s7    #sC   s   $ 
11c                       \ rS rSrSrg)	AxisError    N__name__
__module____qualname____firstlineno____static_attributes__r       r   r   r          r   r   c                       \ rS rSrSrg)UFuncTypeError   r   Nr   r   r   r   r   r      r   r   r   c                 N    Ub!  U R                   U:w  a  U R                  U5      n U $ N)dtypeto)tensorr   s     r   cast_if_neededr!       s&    V\\U25!Mr   c                     [         R                  " U R                  5      S:  a.  U R                  [         R                  " 5       R
                  5      n U $ )N   )r   	_categoryr   r   default_dtypesfloat_dtype)xs    r   cast_int_to_floatr(   '   s;    agg&*DD,,.::;Hr   c                 \    U* U s=::  a  U:  d  O  [        SU  SU 35      eU S:  a  X-  n U $ )Nzaxis z) is out of bounds for array of dimension r   )r   )axndimargnames      r   normalize_axis_indexr-   /   s=    ER$%t#LTFSTT	Av

Ir   c           
         [        U 5      [        [        4;  a   [        R                  " U 5      /n [        U  Vs/ s H  n[        XAU5      PM     sn5      n U(       dP  [        [        [        [        U 5      5      5      [        U 5      :w  a!  U(       a  [        SU S35      e[        S5      eU $ ! [
         a     Nf = fs  snf )ar  
Normalizes an axis argument into a tuple of non-negative integer axes.

This handles shorthands such as ``1`` and converts them to ``(1,)``,
as well as performing the handling of negative indices covered by
`normalize_axis_index`.

By default, this forbids axes from being specified multiple times.
Used internally by multi-axis-checking logic.

Parameters
----------
axis : int, iterable of int
    The un-normalized index or indices of the axis.
ndim : int
    The number of dimensions of the array that `axis` should be normalized
    against.
argname : str, optional
    A prefix to put before the error message, typically the name of the
    argument.
allow_duplicate : bool, optional
    If False, the default, disallow an axis from being specified twice.

Returns
-------
normalized_axes : tuple of int
    The normalized axis index, such that `0 <= normalized_axis < ndim`
zrepeated axis in `z
` argumentzrepeated axis)typetuplelistoperatorindex	TypeErrorr-   r   setmapint
ValueError)axisr+   r,   allow_duplicater*   s        r   normalize_axis_tupler;   8   s    < Dz%&	NN4()D DIDb&r9DIJDs3s3~#673t9D1'*EFF_--K  		 Js   B1 C1
B>=B>c                 J    U c  U $ [        U 5      S:w  a  [        S5      eU S   $ )Nr   zdoes not handle tuple axisr   )r   NotImplementedErrorr9   s    r   allow_only_single_axisr?   e   s,    |
4yA~!">??7Nr   c                     [        U5      [        [        4;  a  U4n[        U5      [        U 5      -   n[	        X5      n[        U 5      n[        U5       Vs/ s H  oDU;   a  SO
[        U5      PM     nnU$ s  snf )Nr   )r/   r1   r0   r   r;   iterrangenext)	arr_shaper9   out_ndimshape_itr*   shapes         r   expand_shaperH   m   sn    Dz$&w4y3y>)H/DIH;@?K?R*Q$x.0?EKL Ls   A7c                     Uc&  SU-  nU R                  U5      R                  5       n U $ [        U R                  U5      nU R	                  U5      n U $ )Nr   )expand
contiguousrH   rG   reshape)r    r9   r+   rG   s       r   apply_keepdimsrN   x   sS    |tu%002 M V\\40&Mr   r>   c                 :    U c  [        S U 5       5      nUS4$ X4$ )z#Flatten the arrays if axis is None.c              3   @   #    U  H  oR                  5       v   M     g 7fr   )flatten).0ars     r   	<genexpr>$axis_none_flatten.<locals>.<genexpr>   s     7w

ws   r   r0   )r9   tensorss     r   axis_none_flattenrX      s)    |7w77z}r   c           	          [         R                  nU" U R                  XS9(       d  [        SU R                   SU SU S35      e[	        X5      $ )a  Dtype-cast tensor to target_dtype.

Parameters
----------
t : torch.Tensor
    The tensor to cast
target_dtype : torch dtype object
    The array dtype to cast all tensors to
casting : str
    The casting mode, see `np.can_cast`

 Returns
 -------
`torch.Tensor` of the `target_dtype` dtype

 Raises
 ------
 ValueError
    if the argument cannot be cast according to the `casting` rule

)castingzCannot cast array data from z to z according to the rule '')r   can_cast_implr   r4   r!   )ttarget_dtyperZ   can_casts       r   typecast_tensorr`      s[    , ))HAGG\;*177) 4~5gYaA
 	
 !**r   c                 2   ^^ [        UU4S jU  5       5      $ )Nc              3   >   >#    U  H  n[        UTT5      v   M     g 7fr   )r`   )rR   r]   rZ   r^   s     r   rT   #typecast_tensors.<locals>.<genexpr>   s     LGqL'::Gs   rV   )rW   r^   rZ   s    ``r   typecast_tensorsrd      s    LGLLLr   c                      [         R                  " U 5      nU$ ! [         a"  nSU  S[        U5       S3n[	        U5      eS nAff = f)Nzfailed to convert z! to ndarray. 
Internal error is: .)torch	as_tensorr	   r   r=   )objr    emesgs       r   _try_convert_to_tensorrl      sS    (% M  (#C5(J3q6(RST!$''(s    
AAAc                    [        U [        R                  5      (       a  U nOo[        R                  " 5       n[        R                  " [
        R                  " [        R                  5      5         [        U 5      n[        R                  " U5        [        XA5      nX4R                  -
  nUS:  a!  UR                  SU-  UR                  -   5      nU(       a  UR                  5       nU$ ! [        R                  " U5        f = f)ad  The core logic of the array(...) function.

Parameters
----------
obj : tensor_like
    The thing to coerce
dtype : torch.dtype object or None
    Coerce to this torch dtype
copy : bool
    Copy or not
ndmin : int
    The results as least this many dimensions
is_weak : bool
    Whether obj is a weakly typed python scalar.

Returns
-------
tensor : torch.Tensor
    a tensor object with requested dtype, ndim and copy semantics.

Notes
-----
This is almost a "tensor_like" coersion function. Does not handle wrapper
ndarrays (those should be handled in the ndarray-aware layer prior to
invoking this function).
r   rJ   )r   rg   Tensorget_default_dtypeset_default_dtyper   get_default_dtype_forfloat32rl   r!   r+   viewrG   clone)ri   r   copyndminr    default_dtype
ndim_extras          r   _coerce_to_tensorry      s    6 #u||$$ //1 B B5== QR	3+C0F##M2 F*F $JA~TJ.=> M ##M2s   1C+ +Dc                  v   SSK Jn  [        U 5      S:X  a
  [        5       $ [        U 5      S:X  aj  U S   n[	        X!5      (       a  UR
                  $ [	        U[        5      (       a2  / nU H  n[        U5      nUR                  U5        M!     [        U5      $ U$ [	        U [        5      (       d   e[        U 5      $ )zHConvert all ndarrays from `inputs` to tensors. (other things are intact)r   )ndarrayr   )	_ndarrayr{   r   r8   r   r    r0   ndarrays_to_tensorsappend)inputsr{   input_result	sub_input
sub_results         r   r}   r}      s    !
6{a|	V	f&&== &&F#	0;
j) $ = M&%(((("6**r   r   )NF)NFr   )__doc__r2   rg    r   r   r8   
IndexErrorr   r4   RuntimeErrorr   r!   r(   r-   r;   r?   rH   rN   rX   r`   rd   rl   ry   r}   r   r   r   <module>r      s      	
J 		Y 	*Z &* +@M5p+r   