
    `ir                     n    d dl Z d dlZd dlZd Zd Zd ZddZd Zd Zd Z	d	 Z
ddZd ZddZddZdS )    Nc                 t    t          j        |t           j                  r|                     |            dS dS )z?Rounds arr inplace if the destination dtype is an integer.
    )outN)cupy
issubdtypeintegerround)arrdtypes     e/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupy/_padding/pad.py_round_if_neededr      s>     udl++ 		c	     c                 B    t          d          f|z  | fz   t          fz   S )aG  Constructs a tuple of slices to slice an array in the given dimension.

    Args:
      sl(slice): The slice for the given dimension.
      axis(int): The axis to which `sl` is applied. All other dimensions are
          left "unsliced".

    Returns:
      tuple of slices: A tuple with slices matching `shape` in length.
    N)sliceEllipsis)slaxiss     r   _slice_at_axisr      s%     $KK>D B5(H;66r   c                 X    |dz  }t          d          f|z  ||d         z   }| |         S )aZ  Gets a view of the current region of interest during iterative padding.

    When padding multiple dimensions iteratively corner values are
    unnecessarily overwritten multiple times. This function reduces the
    working area for the first dimensions so that corners are excluded.

    Args:
      array(cupy.ndarray): The array with the region of interest.
      original_area_slice(tuple of slices): Denotes the area with original
          values of the unpadded array.
      axis(int): The currently padded dimension assuming that `axis` is padded
          before `axis` + 1.

    Returns:
       Nr   )arrayoriginal_area_slicer   r   s       r   	_view_roir   !   s8      	AID
++$	!4TUU!;	;B9r   c                 J   t          d t          | j        |          D                       }| j        j        rdnd}t          j        || j        |          }||                    |           t          d t          | j        |          D                       }| ||<   ||fS )a  Pads an array on all sides with either a constant or undefined values.

    Args:
      array(cupy.ndarray): Array to grow.
      pad_width(sequence of tuple[int, int]): Pad width on both sides for each
          dimension in `arr`.
      fill_value(scalar, optional): If provided the padded area is
          filled with this value, otherwise the pad area left undefined.
          (Default value = None)
    c              3   2   K   | ]\  }\  }}||z   |z   V  d S N .0sizeleftrights       r   	<genexpr>z_pad_simple.<locals>.<genexpr>B   sG        D-4 	te     r   FC)r
   orderNc              3   H   K   | ]\  }\  }}t          |||z             V  d S r   r   r   s       r   r#   z_pad_simple.<locals>.<genexpr>M   sL          D-4 	dD4K             r   )	tuplezipshapeflagsfncr   emptyr
   fill)r   	pad_width
fill_value	new_shaper&   paddedr   s          r   _pad_simpler3   6   s       #&u{I#>#>    I ;?+CCEZ	EBBBFJ      #&u{I#>#>        #(F&&&r   c                     t          t          d|d                   |          }|d         | |<   t          t          | j        |         |d         z
  d          |          }|d         | |<   dS )z1Set an empty-padded area in given dimension.
    Nr   r   r   r   r*   )r2   r   
width_pair
value_pair
left_sliceright_slices         r   _set_pad_arear:   V   ss      dJqM : :DAAJ#AF: fl4 :a=0$77 K %Q-F;r   c                     |d         }t          t          ||dz             |          }| |         }| j        |         |d         z
  }t          t          |dz
  |          |          }| |         }||fS )a6  Retrieves edge values from an empty-padded array along a given axis.

    Args:
      padded(cupy.ndarray): Empty-padded array.
      axis(int): Dimension in which the edges are considered.
      width_pair((int, int)): Pair of widths that mark the pad area on both
          sides in the given dimension.
    r   r   r5   )	r2   r   r6   
left_indexr8   	left_edgeright_indexr9   
right_edges	            r   
_get_edgesr@   b   s{     AJj*q. A A4HHJz"I,t$z!}4K {Q!D!DdKKK$Jj  r   c                    t          | ||          }t          j        |d         |d                             |          |d         d| j        |          }t          j        |d         |d                             |          |d         d| j        |          }|t          t          ddd          |                   }||fS )a  Constructs linear ramps for an empty-padded array along a given axis.

    Args:
      padded(cupy.ndarray): Empty-padded array.
      axis(int): Dimension in which the ramps are constructed.
      width_pair((int, int)): Pair of widths that mark the pad area on both
          sides in the given dimension.
      end_value_pair((scalar, scalar)): End values for the linear ramps which
          form the edge of the fully padded array. These values are included in
          the linear ramps.
    r   F)startstopnumendpointr
   r   r   N)r@   r   linspacesqueezer
   r   r   )r2   r   r6   end_value_pair	edge_pair	left_ramp
right_ramps          r   _get_linear_rampsrM   v   s     6444IQq\!!$''qMl  I Qq\!!$''qMl  J N5tR+@+@$GGHJj  r   c                    |d         }| j         |         |d         z
  }||z
  }|\  }}	|||k     r|}|	||	k     r|}	t          t          |||z             |          }
| |
         } |||d          }t          || j                   ||	cxk    r|k    rn n||fS t          t          ||	z
  |          |          }| |         } |||d          }t          || j                   ||fS )a  Calculates a statistic for an empty-padded array along a given axis.

    Args:
      padded(cupy.ndarray): Empty-padded array.
      axis(int): Dimension in which the statistic is calculated.
      width_pair((int, int)): Pair of widths that mark the pad area on both
          sides in the given dimension.
      length_pair(2-element sequence of None or int): Gives the number of
          values in valid area from each side that is taken into account when
          calculating the statistic. If None the entire valid area in `padded`
          is considered.
      stat_func(function): Function to compute statistic. The expected
          signature is
          ``stat_func(x: ndarray, axis: int, keepdims: bool) -> ndarray``.
    r   r   NT)r   keepdims)r*   r   r   r   r
   )r2   r   r6   length_pair	stat_funcr<   r>   
max_lengthleft_lengthright_lengthr8   
left_chunk	left_statr9   right_chunk
right_stats                   r   
_get_statsrY      sL   " AJ,t$z!}4Kz)J !,Kj;66 zL88!  j*{233T J 
#J	*4$???IY---l0000j00000)## !kL(+66 K %K;TDAAAJZ...j  r   Fc                 &   |\  }}| j         |         |z
  |z
  }|rd}nd}|dz  }|dk    rt          ||          }	||z
  }
|
|	z   }t          t          ||
d          |          }| |         }|dk    r/t          t          ||dz             |          }d| |         z  |z
  }||	z
  }|}
t          t          ||
          |          }|| |<   ||	z  }|dk    rt          ||          }	| |z   dz
  }||	z
  }
t          t          ||
d          |          }| |         }|dk    r1t          t          | dz
  |           |          }d| |         z  |z
  }| j         |         |z
  }||	z   }
t          t          ||
          |          }|| |<   ||	z  }||fS )a,  Pads an `axis` of `arr` using reflection.

    Args:
      padded(cupy.ndarray): Input array of arbitrary shape.
      axis(int): Axis along which to pad `arr`.
      width_pair((int, int)): Pair of widths that mark the pad area on both
          sides in the given dimension.
      method(str): Controls method of reflection; options are 'even' or 'odd'.
      include_edge(bool, optional): If true, edge value is included in
          reflection, otherwise the edge value forms the symmetric axis to the
          reflection. (Default value = False)
    r   r   rF   odd   )r*   minr   r   )r2   r   r6   methodinclude_edgeleft_pad	right_pad
old_lengthedge_offsetchunk_lengthrC   rB   r8   rU   
edge_slicepad_arear9   rW   s                     r   _set_reflect_bothrg      s     %Hid#i/(:J a
!|| :x00+%|##E%r$:$:DAA
J'
U??'h1(E(EtLLJVJ//*<J <'!%t"4"4d;;%xL 1}} :y11
[(1,|#$U5$%;%;TBB[)U??'yj1nyj114 J fZ00;>K T"Y.|#!%t"4"4d;;&x\!	Yr   c           
         |\  }}| j         |         |z
  |z
  }d}d}|dk    rt          t          | t          ||          z
  |dk    r| nd          |          }| |         }	||k    r't          t          ||z
  |          |          }
||z
  }nt          t          d|          |          }
|	| |
<   |dk    rt          t          ||t          ||          z             |          }| |         }||k    r)t          t          | | |z             |          }
||z
  }nt          t          | d          |          }
|| |
<   ||fS )a  Pads an `axis` of `arr` with wrapped values.

    Args:
      padded(cupy.ndarray): Input array of arbitrary shape.
      axis(int): Axis along which to pad `arr`.
      width_pair((int, int)): Pair of widths that mark the pad area on both
          sides in the given dimension.
    r   N)r*   r   r   r]   )r2   r   r6   r`   ra   periodnew_left_padnew_right_padr9   rW   rf   r8   rU   s                r   _set_wrap_bothrl     s    %Hi\$)+h6F LM!||
 %
S222'1nn

$  
 
 [)f%eHv,=x&H&H$OOH#f,LL &eD(&;&;TBBH&x1}}
 $(Hs69'='==>>
 

 J'
v%yj9*v"566 H &.MM &eYJ&=&=tDDH%x&&r   c                 2   | d|z  S t          | t          j                  r|rt          |           } | | ff|z  S t	          j        |           } |r2t	          j        t	          j        |           t          j                  } | j        dk     r| j	        dk    r?| 
                                } |r| dk     rt          d          | d         | d         ff|z  S | j	        dk    r\| j        d	k    rQ| 
                                } |r'| d         dk     s| d         dk     rt          d          | d         | d         ff|z  S |r'|                                 dk     rt          d          |                                 }|df|_        |                                S )
a}  Broadcasts `x` to an array with shape (`ndim`, 2).

    A helper function for `pad` that prepares and validates arguments like
    `pad_width` for iteration in pairs.

    Args:
      x(scalar or array-like, optional): The object to broadcast to the shape
          (`ndim`, 2).
      ndim(int): Number of pairs the broadcasted `x` will have.
      as_index(bool, optional): If `x` is not None, try to round each
          element of `x` to an integer (dtype `cupy.intp`) and ensure every
          element is positive. (Default value = False)

    Returns:
      nested iterables, shape (`ndim`, 2): The broadcasted version of `x`.
    N))NN)r
      r   r   z#index can't contain negative valuesr\   )r\   r   )
isinstancenumbersNumberr   numpyr   asarrayintpndimr    ravel
ValueErrorr*   r]   viewtolist)xru   as_indexx_views       r   	_as_pairsr}   \  s   " 	y %%	Aw~	&	&   	aAAy4AA <M%+a..
;;;vzz
 6Q;;		A HAEE !FGGGqT1Q4L?T))6Q;;17f,,
 		A HQqTAXX1 !FGGGqT1Q4L?T)) @AEEGGaKK>??? VVXXF!9FL==??r   constantc                 8   t          |t          j                  r||ff| j        z  }nJt	          j        |          }|j        j        dk    st          d          t          || j        d          }t          |          r|}t          | |d          \  }}t          |j                  D ]d}t          j        ||d          }t	          j        |j        dd                   }	d	 |	D             }	|	D ]}
 |||
         ||         ||           e|S g g g d
gdgdgdgdgdgdgd
}	 t#          |          t#          ||                   z
  }n0# t$          $ r# t'          d                    |                    w xY w|r#t'          d                    ||                    |dk    rc|                    d
d          }t          |t          j                  r3|dk    r-| j        dk    s| j        dk     rt          | |d          d         S t          j        t          j        t          j        d}t          | |          \  }}t          |j                  }|dk    rRt          ||j                  }t7          |||          D ])\  }}}t9          |||          }t;          ||||           *n|dk    rn| j        dk    rZt7          ||          D ]G\  }}| j        |         dk    r1t=          |          r"t'          d                    |                    Hn}|dk    rLt7          ||          D ]9\  }}t9          |||          }t?          |||          }t;          ||||           :n+|dk    rz|                    dd          }t          ||j                  }t7          |||          D ];\  }}}t9          |||          }tA          ||||          }t;          ||||           <n||v r||         }|                    dd          }t          ||j        d          }t7          |||          D ]<\  }}}t9          |||          }tC          |||||          }t;          ||||           =n"|dv r|                    dd          }|dk    rdnd}t7          ||          D ]\  }\  }}| j        |         dk    r4|dk    s|dk    r(t?          ||||f          }t;          ||||f|           Mt9          |||          }|dk    s|dk    r$tE          ||||f||          \  }}|dk    |dk    $n^|dk    rXt7          ||          D ]G\  }\  }}t9          |||          }|dk    s|dk    r"tG          ||||f          \  }}|dk    |dk    "H|S )a  Pads an array with specified widths and values.

    Args:
      array(cupy.ndarray): The array to pad.
      pad_width(sequence, array_like or int): Number of values padded to the
          edges of each axis. ((before_1, after_1), ... (before_N, after_N))
          unique pad widths for each axis. ((before, after),) yields same
          before and after pad for each axis. (pad,) or int is a shortcut for
          before = after = pad width for all axes. You cannot specify
          ``cupy.ndarray``.
      mode(str or function, optional): One of the following string values or a
          user supplied function

          'constant' (default)
              Pads with a constant value.
          'edge'
              Pads with the edge values of array.
          'linear_ramp'
              Pads with the linear ramp between end_value and the array edge
              value.
          'maximum'
              Pads with the maximum value of all or part of the vector along
              each axis.
          'mean'
              Pads with the mean value of all or part of the vector along each
              axis.
          'median'
              Pads with the median value of all or part of the vector along
              each axis. (Not Implemented)
          'minimum'
              Pads with the minimum value of all or part of the vector along
              each axis.
          'reflect'
              Pads with the reflection of the vector mirrored on the first and
              last values of the vector along each axis.
          'symmetric'
               Pads with the reflection of the vector mirrored along the edge
               of the array.
          'wrap'
              Pads with the wrap of the vector along the axis. The first
              values are used to pad the end and the end values are used to
              pad the beginning.
          'empty'
              Pads with undefined values.
          <function>
              Padding function, see Notes.
      stat_length(sequence or int, optional): Used in 'maximum', 'mean',
          'median', and 'minimum'.  Number of values at edge of each axis used
          to calculate the statistic value.
          ((before_1, after_1), ... (before_N, after_N)) unique statistic
          lengths for each axis. ((before, after),) yields same before and
          after statistic lengths for each axis. (stat_length,) or int is a
          shortcut for before = after = statistic length for all axes.
          Default is ``None``, to use the entire axis. You cannot specify
          ``cupy.ndarray``.
      constant_values(sequence or scalar, optional): Used in 'constant'. The
          values to set the padded values for each axis.
          ((before_1, after_1), ... (before_N, after_N)) unique pad constants
          for each axis.
          ((before, after),) yields same before and after constants for each
          axis.
          (constant,) or constant is a shortcut for before = after = constant
          for all axes.
          Default is 0. You cannot specify ``cupy.ndarray``.
      end_values(sequence or scalar, optional): Used in 'linear_ramp'. The
          values used for the ending value of the linear_ramp and that will
          form the edge of the padded array.
          ((before_1, after_1), ... (before_N, after_N)) unique end values
          for each axis.
          ((before, after),) yields same before and after end
          values for each axis.
          (constant,) or constant is a shortcut for before = after = constant
          for all axes.
          Default is 0. You cannot specify ``cupy.ndarray``.
      reflect_type({'even', 'odd'}, optional): Used in 'reflect', and
          'symmetric'.  The 'even' style is the default with an unaltered
          reflection around the edge value.  For the 'odd' style, the extended
          part of the array is created by subtracting the reflected values from
          two times the edge value.

    Returns:
      cupy.ndarray: Padded array with shape extended by ``pad_width``.

    .. note::
        For an array with rank greater than 1, some of the padding of later
        axes is calculated from padding of previous axes.  This is easiest to
        think about with a rank 2 array where the corners of the padded array
        are calculated by using padded values from the first axis.

        The padding function, if used, should modify a rank 1 array in-place.
        It has the following signature:

        ``padding_func(vector, iaxis_pad_width, iaxis, kwargs)``

        where

        vector (cupy.ndarray)
            A rank 1 array already padded with zeros.  Padded values are
            ``vector[:iaxis_pad_width[0]]`` and
            ``vector[-iaxis_pad_width[1]:]``.
        iaxis_pad_width (tuple)
            A 2-tuple of ints, ``iaxis_pad_width[0]`` represents the number of
            values padded at the beginning of vector where
            ``iaxis_pad_width[1]`` represents the number of values padded at
            the end of vector.
        iaxis (int)
            The axis currently being calculated.
        kwargs (dict)
            Any keyword arguments the function requires.

    Examples
    --------
    >>> a = cupy.array([1, 2, 3, 4, 5])
    >>> cupy.pad(a, (2, 3), 'constant', constant_values=(4, 6))
    array([4, 4, 1, ..., 6, 6, 6])

    >>> cupy.pad(a, (2, 3), 'edge')
    array([1, 1, 1, ..., 5, 5, 5])

    >>> cupy.pad(a, (2, 3), 'linear_ramp', end_values=(5, -4))
    array([ 5,  3,  1,  2,  3,  4,  5,  2, -1, -4])

    >>> cupy.pad(a, (2,), 'maximum')
    array([5, 5, 1, 2, 3, 4, 5, 5, 5])

    >>> cupy.pad(a, (2,), 'mean')
    array([3, 3, 1, 2, 3, 4, 5, 3, 3])

    >>> a = cupy.array([[1, 2], [3, 4]])
    >>> cupy.pad(a, ((3, 2), (2, 3)), 'minimum')
    array([[1, 1, 1, 2, 1, 1, 1],
           [1, 1, 1, 2, 1, 1, 1],
           [1, 1, 1, 2, 1, 1, 1],
           [1, 1, 1, 2, 1, 1, 1],
           [3, 3, 3, 4, 3, 3, 3],
           [1, 1, 1, 2, 1, 1, 1],
           [1, 1, 1, 2, 1, 1, 1]])

    >>> a = cupy.array([1, 2, 3, 4, 5])
    >>> cupy.pad(a, (2, 3), 'reflect')
    array([3, 2, 1, 2, 3, 4, 5, 4, 3, 2])

    >>> cupy.pad(a, (2, 3), 'reflect', reflect_type='odd')
    array([-1,  0,  1,  2,  3,  4,  5,  6,  7,  8])

    >>> cupy.pad(a, (2, 3), 'symmetric')
    array([2, 1, 1, 2, 3, 4, 5, 5, 4, 3])

    >>> cupy.pad(a, (2, 3), 'symmetric', reflect_type='odd')
    array([0, 1, 1, 2, 3, 4, 5, 5, 6, 7])

    >>> cupy.pad(a, (2, 3), 'wrap')
    array([4, 5, 1, 2, 3, 4, 5, 1, 2, 3])

    >>> def pad_with(vector, pad_width, iaxis, kwargs):
    ...     pad_value = kwargs.get('padder', 10)
    ...     vector[:pad_width[0]] = pad_value
    ...     vector[-pad_width[1]:] = pad_value
    >>> a = cupy.arange(6)
    >>> a = a.reshape((2, 3))
    >>> cupy.pad(a, 2, pad_with)
    array([[10, 10, 10, 10, 10, 10, 10],
           [10, 10, 10, 10, 10, 10, 10],
           [10, 10,  0,  1,  2, 10, 10],
           [10, 10,  3,  4,  5, 10, 10],
           [10, 10, 10, 10, 10, 10, 10],
           [10, 10, 10, 10, 10, 10, 10]])
    >>> cupy.pad(a, 2, pad_with, padder=100)
    array([[100, 100, 100, 100, 100, 100, 100],
           [100, 100, 100, 100, 100, 100, 100],
           [100, 100,   0,   1,   2, 100, 100],
           [100, 100,   3,   4,   5, 100, 100],
           [100, 100, 100, 100, 100, 100, 100],
           [100, 100, 100, 100, 100, 100, 100]])
    iz%`pad_width` must be of integral type.T)r{   r   )r0   rF   Nc              3   ,   K   | ]}|t           fz   V  d S r   )r   )r   inds     r   r#   zpad.<locals>.<genexpr>n  s)      66#C8+%666666r   constant_values
end_valuesstat_lengthreflect_type)
r-   edgewrapr~   linear_rampmaximummeanminimumreflect	symmetriczmode '{}' is not supportedz/unsupported keyword arguments for mode '{}': {}r~   r   g    NA)r   r   r   r-   zGcan't extend empty axis {} using modes other than 'constant' or 'empty'r   r   >   r   r   evenr   Fr   )$ro   rp   Integralru   rr   rs   r
   kind	TypeErrorr}   callabler3   ranger   moveaxisndindexr*   setKeyErrorrw   formatgetrq   r    maxr]   r   r)   r   r:   anyr@   rM   rY   rg   rl   )r   r/   modekwargsfunctionr2   _r   rx   indsr   allowed_kwargsunsupported_kwargsvaluesstat_functionsr   axesr6   r7   roirJ   r   	ramp_pairfunclengthrP   	stat_pairr^   r_   r<   r>   s                                  r   padr     s   ` )W-.. 	D+-
:		M),,	#s**CDDD idCCC	~~ yQ???	 &+&& 	C 	CD
 =r22D =CRC11D66666D C CcIdOT6BBBBC  &'$~!?!?"#$% ND [[3~d/C+D+DD D D D5<<TBBCCCD 
=DD( 
 
 	
 z-q11fgn-- 	7&A++
a5:#3#3ui33A66 88	 N #.eY"?"?F Dz66;//,/i,H,H 	= 	=(D*jF$7>>C#tZ<<<<	= 
	q !$D) 4 4 	 	D*{4 A%%#j//% ,,2F4LL  	 
 #D) 4 4 	< 	<D*F$7>>C"3j99I#tZ;;;;	<
 
		ZZa00
z6;77
,/i,L,L 	< 	<(D*jF$7>>C)#tZLLI#tZ;;;;	<
 
		d#M40066;>>>-0y&-I-I 	< 	<)D*kF$7>>C"3j+tLLI#tZ;;;;	<
 
)	)	)NF33#{22tt/24/C/C 	 	+D+:{{4 A%%:>>[1__ 'vtj+5NOO	D:{";Y   F$7>>Cq..K!OO +<
K8&,+ +'
K	 q..K!OO	& 
/24/C/C 	 	+D+:{F$7>>Cq..K!OO +9
K8+ +'
K	 q..K!OO Ms   )%E -E<r   )F)r~   )rp   rr   r   r   r   r   r3   r:   r@   rM   rY   rg   rl   r}   r   r   r   r   <module>r      s       7 7 7  *' ' ' '@	( 	( 	(! ! !($! $! $!N0! 0! 0!fG G G GT?' ?' ?'D; ; ; ;LN N N N N Nr   