
    `i*                    R   U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dlm
Z
 d dlmZ d dlZd dlZd dlZd dlZd dlmZ d dlmZ  e            rd dlZej        j        Zded	<   nej        Zd)dZd*dZd+dZeej        dfdZeej        fdZeej        dd dfdZ ej!        j"        ej        ddd fdZ#eej        fdZ$eej        fdddZ%ej&        d             Z' G d  d!ej(                  Z) G d" d#e)          Z* G d$ d%e)          Z+ej&        d&d'd(            Z,e,Z-dS ),    )annotationsN)Callable)PackageNotFoundError)mock)internal)is_availablez-Callable[..., Callable[[Callable], Callable]]_skipifrequirementsstrreturnCallable[[Callable], Callable]c                 d    dd                     |            }t          t          |   |          S )a  Run a test case only when given requirements are satisfied.

    .. admonition:: Example

       This test case runs only when `numpy>=1.18` is installed.

       >>> from cupy import testing
       ...
       ...
       ... class Test(unittest.TestCase):
       ...     @testing.with_requires("numpy>=1.18")
       ...     def test_for_numpy_1_18(self):
       ...         pass

    Args:
        requirements: A list of string representing requirement condition to
            run a given test case.

    z
requires: ,)reason)joinr	   	installed)r
   msgs     h/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupy/testing/_helper.pywith_requiresr      s8    ( 0sxx--
/
/Cy,//<<<<    
specifiersboolc                     ddl m} | D ]h} ||          }	 t          j                            |j                  }n# t          $ r Y  dS w xY w|j        }|r|                    |d          s dS idS )zReturns True if the current environment satisfies the specified
    package requirement.

    Args:
        specifiers: Version specifiers (e.g., `numpy>=1.20.0`).
    r   )RequirementFT)prereleases)	packaging.requirementsr   	importlibmetadataversionnamer   	specifiercontains)r   r   specreqfoundexpecteds         r   r   r   4   s     322222 	 	k$	&..sx88EE# 	 	 	555	= 	X..u$.GG 	554s   $<
A
Aversion_rangec                &    t          d|            S )zReturns True if numpy version satisfies the specified criteria.

    Args:
        version_range: A version specifier (e.g., `>=1.13.0`).
    numpy)r   )r'   s    r   numpy_satisfiesr*   K   s     ,],,---r   Cc                @   t          j        |          }t          j        dt          j        |           dz   d          }|dk    r
|dz  dk    }n|j        dk    r||dz  z   }|                    |                    |                              |           |          S )av  Returns an array with given shape, array module, and dtype.

    Args:
         shape(tuple of int): Shape of returned ndarray.
         xp(numpy or cupy): Array module to use.
         dtype(dtype): Dtype of returned ndarray.
         order({'C', 'F'}): Order of returned ndarray.

    Returns:
         numpy.ndarray or cupy.ndarray:
         The array filled with :math:`1, \cdots, N` with specified dtype
         with given shape, array module. Here, :math:`N` is
         the size of the returned array.
         If ``dtype`` is ``numpy.bool_``, evens (resp. odds) are converted to
         ``True`` (resp. ``False``).

       ?   r   c              ?)order)	r)   dtypearanger   prodkindarrayastypereshape)shapexpr3   r2   as        r   shaped_aranger=   T   s    $ KEQe,,q0!44A||EQJ	s		BJ88AHHUOO++E22%8@@@r   c                :   t          j        |          }t          j        |           }t          j        |dd          }|dk    r
|dz  dk    }n|j        dk    r||dz  z   }|                    |                    |                              |                     S )a3  Returns an array filled with decreasing numbers.

    Args:
         shape(tuple of int): Shape of returned ndarray.
         xp(numpy or cupy): Array module to use.
         dtype(dtype): Dtype of returned ndarray.

    Returns:
         numpy.ndarray or cupy.ndarray:
         The array filled with :math:`N, \cdots, 1` with specified dtype
         with given shape, array module.
         Here, :math:`N` is the size of the returned array.
         If ``dtype`` is ``numpy.bool_``, evens (resp. odds) are converted to
         ``True`` (resp. ``False``).
    r   r.   r/   r0   r1   )	r)   r3   r   r5   r4   r6   r7   r8   r9   )r:   r;   r3   sizer<   s        r   shaped_reverse_arangerA   o   s      KE=DT1b!!A||EQJ	s		BJ88AHHUOO++E22333r   
   c                   t           j                            |           t          j        |          }|dk    r"t           j                            d|           }nQ|j        dk    r0t          j        j        |  dt          j        j        |  z  z   }||z  }nt          j        j        |  |z  }|                    |||          S )a  Returns an array filled with random values.

    Args:
         shape(tuple): Shape of returned ndarray.
         xp(numpy or cupy): Array module to use.
         dtype(dtype): Dtype of returned ndarray.
         scale(float): Scaling factor of elements.
         seed(int): Random seed.

    Returns:
         numpy.ndarray or cupy.ndarray: The array with
         given shape, array module,

    If ``dtype`` is ``numpy.bool_``, the elements are
    independently drawn from ``True`` and ``False``
    with same probabilities.
    Otherwise, the array is filled with samples
    independently and identically drawn
    from uniform distribution over :math:`[0, scale)`
    with specified dtype.
    r.   r/   )r@   r0   r1   )r3   r2   )r)   randomseedr3   randintr6   randasarray)r:   r;   r3   scalerE   r2   r<   s          r   shaped_randomrJ      s    . 
LdKE||L   //	s		Lu%U\->-F(FF	U
Lu%-::auE:222r   g{Gz?cooc                   ddl }| \  }}t          j                            |           |j                            |||                              |          }	|t          j        j        u r%t          j        j                            |	          }	n+||j        ur"t          d
                    |                    |	                    |          S )a  Returns an array filled with random values.

    Args:
        shape (tuple): Shape of returned sparse matrix.
        sp (scipy.sparse or cupyx.scipy.sparse): Sparce matrix module to use.
        dtype (dtype): Dtype of returned sparse matrix.
        density (float): Density of returned sparse matrix.
        format (str): Format of returned sparse matrix.
        seed (int): Random seed.

    Returns:
        The sparse matrix with given shape, array module,
    r   NzUnknown module: {})scipy.sparser)   rD   rE   sparser8   cupyxscipy
coo_matrix
ValueErrorformatasformat)
r:   spr3   densityrS   rE   rP   n_rowsn_colsr<   s
             r   shaped_sparse_randomrY      s      NFF	LdFFG44;;EBBA	U[K))!,,	5<		-44R88999::fr   c                x   t          j        |          }t          j        |          }|dk    r!t          | d          } t	          |d          }n|j        dk    rt          | d          } t          j        | ||          }|                    |                    |          	                    |                    S )ah  Returns an array with given shape, array module, and dtype.

    Args:
        start (int): The starting value.
        stop (int): The end value.
        shape (tuple of int): Shape of returned ndarray.
        xp (numpy or cupy): Array module to use.
        dtype (dtype): Dtype of returned ndarray.

    Returns:
        numpy.ndarray or cupy.ndarray:
    r.   r   r-   u)
r)   r3   r5   maxminr6   linspacer7   r8   r9   )startstopr:   r;   r3   r@   r<   s          r   shaped_linspacera      s     KE:eD||E14||	s		E1udD))A88AHHUOO++E22333r   )singular_valuesc                  t          |           dk    r"t          d                    |                     |t          d          |                    |          }t          j        |          }|j        dvr"t          d                    |                    |                    |          st          d          |dk     	                                rt          d	           |j
        j        |  }|j        d
k    r|d |j
        j        |  z  z   }|j                            |d          \  }}}|                    ||j                  }|                    d|||          }|                    |          S )a  Returns a matrix with specified singular values.

    Generates a random matrix with given singular values.
    This function generates a random NumPy matrix (or a stack of matrices) that
    has specified singular values. It can be used to generate the inputs for a
    test that can be instable when the input value behaves bad.
    Notation: denote the shape of the generated array by :math:`(B..., M, N)`,
    and :math:`K = min\{M, N\}`. :math:`B...` may be an empty sequence.

    Args:
        shape (tuple of int): Shape of the generated array, i.e.,
            :math:`(B..., M, N)`.
        xp (numpy or cupy): Array module to use.
        dtype: Dtype of the generated array.
        singular_values (array-like): Singular values of the generated
            matrices. It must be broadcastable to shape :math:`(B..., K)`.

    Returns:
        numpy.ndarray or cupy.ndarray: A random matrix that has specific
        singular values.
    r-   z.shape {} is invalid for matrices: too few axesNzsingular_values is not givenfczdtype {} is not supportedzsingular_values is not realr   z negative singular value is givenr0   r1   F)full_matricesz...ik,...k,...kj->...ij)lenrR   rS   	TypeErrorrH   r)   r3   r6   	isrealobjanyrD   randnlinalgsvdbroadcast_tor:   einsumr8   )	r:   r;   r3   rb   r<   r[   svhsvs	            r   generate_matrixrr      su   0 5zzQ<CCEJJ
 
 	
 6777jj11OKEz3::5AABBB<<(( 75666!  "" =;<<<
 		AzS_RY_e,,,y}}Qe}44HAq"	!'	2	2B
		+QB77A88E??r   c              #  @   K   t          j        d          5 }t          j        d           d V  d d d            n# 1 swxY w Y   t           fd|D                       rd S 	  j        }n# t
          $ r t                     }Y nw xY wt          d|z            )NT)recordalwaysc              3  B   K   | ]}t          |j                  V  d S N)
isinstancemessage).0mr&   s     r   	<genexpr>zassert_warns.<locals>.<genexpr>  s/      
6
6q:ai**
6
6
6
6
6
6r   z%s not triggerred)warningscatch_warningssimplefilterri   __name__AttributeErrorr   AssertionError)r&   wexc_names   `  r   assert_warnsr     s     		 	-	-	- h'''               
6
6
6
6A
6
6
666 !$ ! ! !x==! ,x7
8
88s!   >AA'A/ /B
Bc                  P    e Zd Zed             Zed             Zed             ZdS )NumpyAliasTestBasec                    t                      rw   )NotImplementedErrorselfs    r   funczNumpyAliasTestBase.func*  s    !###r   c                6    t          t          | j                  S rw   )getattrcupyr   r   s    r   	cupy_funczNumpyAliasTestBase.cupy_func.  s    tTY'''r   c                6    t          t          | j                  S rw   )r   r)   r   r   s    r   
numpy_funczNumpyAliasTestBase.numpy_func2  s    udi(((r   N)r   
__module____qualname__propertyr   r   r    r   r   r   r   (  sc        $ $ X$ ( ( X( ) ) X) ) )r   r   c                      e Zd Zd Zd ZdS )NumpyAliasBasicTestBasec                f    t           j        } || j                   || j                  k    sJ d S rw   )inspect	signaturer   r   )r   fs     r   test_argspecz$NumpyAliasBasicTestBase.test_argspec9  s9    q  AAdo$6$6666666r   c                    | j         }| j        }t          |d          sJ |j        J |j        dk    sJ |j        |j        usJ d S )N__doc__ )r   r   hasattrr   )r   r   r   s      r   test_docstringz&NumpyAliasBasicTestBase.test_docstring=  sf    N	_
y),,,,, ,,, B&&&& 
(:::::::r   N)r   r   r   r   r   r   r   r   r   r   7  s2        7 7 7; ; ; ; ;r   r   c                      e Zd Zd ZdS )NumpyAliasValuesTestBasec                J     | j         | j          | j        | j         k    sJ d S rw   )r   argsr   r   s    r   test_valuesz$NumpyAliasValuesTestBase.test_valuesH  s/    t~ty)_T_di-HHHHHHHr   N)r   r   r   r   r   r   r   r   r   F  s(        I I I I Ir   r   r-   )times_calledc              /     K   t          j        |i |5 }dV  |j        | k    sJ 	 ddd           dS # 1 swxY w Y   dS )a   A handy wrapper for unittest.mock to check if a function is called.

    Args:
        *args: Arguments of `mock.patch`.
        times_called (int): The number of times the function should be
            called. Default is ``1``.
        **kwargs: Keyword arguments of `mock.patch`.

    N)r   patch
call_count)r   r   kwargshandles       r   assert_function_is_calledr   L  s       
T	$V	$	$ 1 L000001 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1s   488)r
   r   r   r   )r   r   r   r   )r'   r   r   r   ).
__future__r   
contextlibimportlib.metadatar   r   unittestr}   collections.abcr   r   r   r)   r   rO   cupyx.scipy.sparse
cupy._corer   cupy.testing._pytest_implr   pytestmarkskipifr	   __annotations__skipIfr   r   r*   float32r=   rA   rJ   rP   rN   rY   ra   rr   contextmanagerr   TestCaser   r   r   r   AssertFunctionIsCalledr   r   r   <module>r      s   " " " " " " "            $ $ $ $ $ $ 3 3 3 3 3 3                    2 2 2 2 2 2<>> MMM=C[=OGOOOOOoG= = = =0   .. . . . !S A A A A6 %) 4 4 4 46 em2AS 3  3  3  3H +$EMU   : ,0u} 4 4 4 42 em3@D3 3 3 3 3l 9 9 9 ) ) ) ) )* ) ) ); ; ; ; ;0 ; ; ;I I I I I1 I I I 23 1 1 1 1 1  3   r   