o
    di                     @   sJ  d dl Zd dl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mZmZmZmZmZ d dlZddlmZmZmZmZmZmZ dZdefdd	Zd
d Zdd Zdd Zdd Zdd Zdd Z dd Z!dd Z"dd Z#dd Z$dd Z%d d! Z&	"	#	d-d$ee'ee f d%e(d&e(d'eeegef  d(ef
d)d*Z)d+d, Z*dS ).    N)MutableMapping)AnyCallableDictIterableOptionalTuple   )is_accelerate_availableis_auto_gptq_availableis_datasets_availableis_diffusers_available"is_sentence_transformers_availableis_timm_available__DUMMY_OPTIMUM_USER__
dictionaryc                 C   sN   g }|   D ]\}}|}t|tr|t|   q|||f qt|S )z=
    Flatten a nested dictionaries as a flat dictionary.
    )items
isinstancer   extendflatten_dictappenddict)r   r   kvnew_key r   i/lsinfo/ai/hellotax_ai/llm_service/venv_embed/lib/python3.10/site-packages/optimum/utils/testing_utils.pyr   *   s   
r   c                 C      t t d| S )zu
    Decorator marking a test that requires accelerate. These tests are skipped when accelerate isn't installed.
    ztest requires accelerate)unittest
skipUnlessr
   	test_caser   r   r   require_accelerate8      r"   c                 C   r   )zs
    Decorator marking a test that requires auto-gptq. These tests are skipped when auto-gptq isn't installed.
    ztest requires auto-gptq)r   r   r   r    r   r   r   require_auto_gptq?   r#   r$   c                 C   s&   t j rdnd}t|dkd| S )z8Decorator marking a test that requires CUDA and PyTorch.cudacpuztest requires CUDA)torchr%   is_availabler   r   )r!   torch_devicer   r   r   require_torch_gpuF   s   r*   c                 C   s(   ddl }| }td|d kd| S )zNDecorator marking a test that requires ROCMExecutionProvider for ONNX Runtime.r   NROCMExecutionProviderz#test requires ROCMExecutionProvider)onnxruntimeget_available_providersr   r   )r!   ort	providersr   r   r   require_ort_rocmM   s
   r0   c                 C   s(   t jdd}|du rtd| S | S )zG
    Decorator marking a test that requires huggingface hub token.
    HF_AUTH_TOKENNz>test requires hf token as `HF_AUTH_TOKEN` environment variableosenvirongetr   skip)r!   hf_tokenr   r   r   require_hf_tokenX   s   r8   c                 C   s>   t jdd}t jdd}|du s|du rtd| S | S )zB
    Decorator marking a test that requires sigopt API token.
    SIGOPT_API_TOKENNSIGOPT_PROJECTzMtest requires an environment variable `SIGOPT_API_TOKEN` and `SIGOPT_PROJECT`r2   )r!   sigopt_api_tokenhas_sigopt_projectr   r   r    require_sigopt_token_and_projectd   s   r=   c                  C   sb   t jdd u} t jdd ur-zd}tjtjddgddd W n tjy,   d}Y nw | o0|S )Nzonnxruntime.training	torch_ortTz-mztorch_ort.configureF)shellcheck)	importlibutil	find_spec
subprocessrunsys
executableCalledProcessError)is_ort_train_availableis_torch_ort_configuredr   r   r   is_ort_training_availabler   s   rK   c                 C   r   )z
    Decorator marking a test that requires onnxruntime-training and torch_ort correctly installed and configured.
    These tests are skipped otherwise.
    z:test requires torch_ort correctly installed and configured)r   r   rK   r    r   r   r   require_ort_training   s   rL   c                 C   r   )Nztest requires diffusers)r   r   r   r    r   r   r   require_diffusers      rM   c                 C   r   )Nztest requires timm)r   r   r   r    r   r   r   require_timm   rN   rO   c                 C   r   )Nz#test requires sentence-transformers)r   r   r   r    r   r   r   require_sentence_transformers   rN   rP   c                 C   r   )Nztest requires datasets)r   r   r   r    r   r   r   require_datasets   rN   rQ   FT
parameters
yield_dictadd_test_namefilter_params_funcreturnc           
      c   s    t j|   D ]P}|dur|t|}|du rqddd |D }|du rFi }t|  D ]
\}}|| ||< q/|du rB||d< |V  q|du rQ|gt| nt|}	|	V  qdS )aV  
    Generates an iterable over the grid of all combinations of parameters.

    Args:
        `parameters` (`Dict[str, Iterable[Any]]`):
            Dictionary of multiple values to generate a grid from.
        `yield_dict` (`bool`, defaults to `False`):
            If True, a dictionary with all keys, and sampled values will be returned. Otherwise, return sampled values as a list.
        `add_test_name` (`bool`, defaults to `True`):
            Whether to add the test name in the yielded list or dictionary.
        filter_params_func (`Optional[Callable[[Tuple], Tuple]]`, defaults to `None`):
            A function that can modify or exclude the current set of parameters. The function should take a tuple of the
            parameters and return the same. If a parameter set is to be excluded, the function should return an empty tuple.
    N_c                 S   s   g | ]}t |qS r   )str).0paramr   r   r   
<listcomp>   s    z#grid_parameters.<locals>.<listcomp>T	test_name)	itertoolsproductvalueslistjoin	enumeratekeys)
rR   rS   rT   rU   paramsr\   res_dictikeyreturned_listr   r   r   grid_parameters   s"   ri   c                 C   sL   t j| r"t j| r$t jdkrt d|   dS t|  dS dS dS )z
    Remove a directory and its content.
    This is a cross-platform solution to remove a directory and its content that avoids the use of `shutil.rmtree` on Windows.
    Reference: https://github.com/python/cpython/issues/107408
    ntzrmdir /S /Q N)r3   pathexistsisdirnamesystemshutilrmtree)dirpathr   r   r   remove_directory   s
   
rs   )FTN)+importlib.utilrA   r]   r3   rp   rD   rF   r   collections.abcr   typingr   r   r   r   r   r   r'    r
   r   r   r   r   r   USERr   r"   r$   r*   r0   r8   r=   rK   rL   rM   rO   rP   rQ   rX   boolri   rs   r   r   r   r   <module>   sP     
'