o
    diY-                     @   s  d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	 ddl
mZ ddlmZ e r2d dlmZ nd dlmZ G d	d
 d
ZG dd deeZG dd deeZG dd deeZdd ZeeG dd dZeeG dd dZeeG dd dZeeG dd dZeeG dd dZeG dd dZeG dd  d ZeG d!d" d"ZeG d#d$ d$eZeG d%d& d&eeZeeG d'd( d(eeeZ dS ))    N)field)Enum)DictListOptionalUnion   )is_pydantic_available)generate_doc_dataclass)	dataclassc                   @   s<   e Zd Zg dZededefddZedefddZdS )	APIFeaturesManager)text-classificationztoken-classificationzquestion-answeringzimage-classification
model_typetaskc              	   C   sn   |   } | tjvrt|  dttj  d|  d|tj|  vr5t| d|  dtj|   d| dd S )N is not supported yet. Only ' are supported. If you want to support ) please propose a PR or open up an issue.z  is not supported yet for model z. Only )lowerr   _SUPPORTED_MODEL_TYPEKeyErrorlistkeys)r   r    r   `/lsinfo/ai/hellotax_ai/llm_service/venv_embed/lib/python3.10/site-packages/optimum/utils/runs.pycheck_supported_model_task_pair"   s$   
z2APIFeaturesManager.check_supported_model_task_pairc                 C   s*   | t jvrt|  dt j d|  dd S )Nr   r   r   )r   _SUPPORTED_TASKSr   r   r   r   r   check_supported_task2   s   
z'APIFeaturesManager.check_supported_taskN)__name__
__module____qualname__r   staticmethodstrr   r   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZdS )
FrameworksonnxruntimeN)r   r   r    r$   r   r   r   r   r#   <   s    r#   c                   @   s   e Zd ZdZdZdZdS )CalibrationMethodsminmax
percentileentropyN)r   r   r    r&   r'   r(   r   r   r   r   r%   @   s    r%   c                   @   s   e Zd ZdZdZdS )QuantizationApproachstaticdynamicN)r   r   r    r*   r+   r   r   r   r   r)   F   s    r)   c                   C   s(   t  dkrdS t  dkrdS td)NLinuxlscpuDarwinzsysctl -a | grep machdep.cpuzOS not supported.)platformsystemNotImplementedErrorr   r   r   r   cpu_info_commandK   s
   r2   c                   @   s   e Zd ZU dZeddidZeed< eddidZe	ed< eddd	id
Z
ee ed< edddid
Zee ed< edddid
Zee ed< dS )CalibrationzBParameters for post-training calibration with static quantization.descriptionzDCalibration method used, either "minmax", "entropy" or "percentile".metadatamethodzVNumber of examples to use for the calibration step resulting from static quantization.num_calibration_samplesNz:The percentile used for the percentile calibration method.defaultr6    calibration_histogram_percentilezjWhether to compute the moving average of the minimum and maximum values for the minmax calibration method.calibration_moving_averagezConstant smoothing factor to use when computing the moving average of the minimum and maximum values. Effective only when the selected calibration method is minmax and `calibration_moving_average` is set to True.#calibration_moving_average_constant)r   r   r    __doc__r   r7   r%   __annotations__r8   intr;   r   floatr<   boolr=   r   r   r   r   r3   T   s,   
 r3   c                   @   sN   e Zd ZU edddidZee ed< edddidZee ed< d	d
 Z	dS )FrameworkArgs   r4   z,ONNX opset version to export the model with.r9   opsetr   zONNX optimization level.optimization_levelc                 C   s8   | j dksJ d| j  | jdv sJ d| j d S )N   zUnsupported OnnxRuntime opset: )r   r      c   z,Unsupported OnnxRuntime optimization level: )rE   rF   selfr   r   r   __post_init__x   s   
zFrameworkArgs.__post_init__N)
r   r   r    r   rE   r   r@   r?   rF   rL   r   r   r   r   rC   r   s   
 rC   c                   @   s   e Zd ZU dZeddidZeed< eddidZeed< eddidZ	e
eed	ef f ed
< eddidZee ed< ed	ddidZee ed< ed	ddidZee ed< d	S )DatasetArgsz"Parameters related to the dataset.r4   z9Path to the dataset, as in `datasets.load_dataset(path)`.r5   pathz0Dataset split used for evaluation (e.g. "test").
eval_splitzZDataset columns used as input data. At most two, indicated with "primary" and "secondary".N	data_keysz5Dataset column used for references during evaluation.ref_keysz?Name of the dataset, as in `datasets.load_dataset(path, name)`.r9   namez2Dataset split used for calibration (e.g. "train").calibration_split)r   r   r    r>   r   rN   r"   r?   rO   rP   r   r   rQ   r   rR   r   rS   r   r   r   r   rM      s   
 rM   c                   @   s.   e Zd ZU dZedddidZee ed< dS )TaskArgszTask-specific parameters.Nr4   zVText classification specific. Set whether the task is regression (output = one float).r9   is_regression)	r   r   r    r>   r   rU   r   rB   r?   r   r   r   r   rT      s   
 rT   c                   @   sJ   e Zd ZU dZedddidZee ed< edddidZ	ee ed	< d
S )BenchmarkTimeArgs%Parameters related to time benchmark.   r4   z+Duration in seconds of the time evaluation.r9   duration
   z?Number of warmup calls to forward() before the time evaluation.warmup_runsN)
r   r   r    r>   r   rY   r   r@   r?   r[   r   r   r   r   rV      s   
 rV   c                   @   s   e Zd ZU eddidZeed< eddidZeed< eddidZe	ed< edd	idZ
eed
< eddidZeed< eddidZeed< dS )_RunBaser4   z7Name of the model hosted on the Hub to use for the run.r5   model_name_or_pathzTask performed by the model.r   z.Whether to use dynamic or static quantization.quantization_approachzDDataset to use. Several keys must be set on top of the dataset name.datasetz0Name of the framework used (e.g. "onnxruntime").	frameworkzFramework-specific arguments.framework_argsN)r   r   r    r   r]   r"   r?   r   r^   r)   r_   rM   r`   r#   ra   rC   r   r   r   r   r\      s   
 r\   c                   @   s   e Zd ZU edd ddidZeee  ed< edd ddidZ	eee  ed	< ed
ddidZ
ee ed< edddidZee ed< edddidZee ed< ed
ddidZee ed< edddidZee ed< ee ddidZee ed< dS )_RunDefaultsc                   C      ddgS )NAddMatMulr   r   r   r   r   <lambda>       z_RunDefaults.<lambda>r4   zWOperators to quantize, doing no modifications to others (default: `["Add", "MatMul"]`).default_factoryr6   operators_to_quantizec                   C   s   g dS )N)	layernormgeluresidualgathersoftmaxr   r   r   r   r   rf      rg   zsSpecific nodes to exclude from being quantized (default: `['layernorm', 'gelu', 'residual', 'gather', 'softmax']`).node_exclusionFz3Whether to quantize per channel (default: `False`).r9   per_channelNz<Calibration parameters, in case static quantization is used.calibrationz*Task-specific arguments (default: `None`).	task_argszXWhether the quantization is to be done with Quantization-Aware Training (not supported).aware_trainingzLMaximum number of samples to use from the evaluation dataset for evaluation.max_eval_samplesrW   time_benchmark_args)r   r   r    r   rj   r   r   r"   r?   rp   rq   rB   rr   r3   rs   rT   rt   ru   r@   rV   rv   r   r   r   r   rb      s@   
 
rb   c                   @   s,   e Zd ZU dZeddidZee ed< dS )_RunConfigBasezParameters defining a run. A run is an evaluation of a triplet (model, dataset, metric) coupled with optimization parameters, allowing to compare a transformers baseline and a model optimized with Optimum.r4   zList of metrics to evaluate on.r5   metricsN)	r   r   r    r>   r   rx   r   r"   r?   r   r   r   r   rw      s   
 rw   c                   @   sV   e Zd ZU edd ddidZeee  ed< edd ddidZ	eee  ed	< d
S )_RunConfigDefaultsc                   C   rc   )N      r   r   r   r   r   rf      rg   z_RunConfigDefaults.<lambda>r4   z:Batch sizes to include in the run to measure time metrics.rh   batch_sizesc                   C   s   dgS )N   r   r   r   r   r   rf      s    z<Input lengths to include in the run to measure time metrics.input_lengthsN)
r   r   r    r   r|   r   r   r@   r?   r~   r   r   r   r   ry      s   
 ry   c                   @   s   e Zd Zdd ZdS )Runc                 C   s   t j| jd | jdkr"d}| jd usJ || jd d us"J || jdkr0| jd s0J d| jdkr<| js<J d| jd	u sEJ d
d S )Nr   r   zyFor text classification, whether the task is regression should be explicity specified in the task_args.is_regression key.rU   r*   rS   z`Calibration split should be passed for static quantization in the dataset.calibration_split key.zWCalibration parameters should be passed for static quantization in the calibration key.Fz*Quantization-Aware Training not supported.)r   r   r   rs   r^   r_   rr   rt   )rK   messager   r   r   rL      s"   


zRun.__post_init__N)r   r   r    rL   r   r   r   r   r      s    r   c                       s    e Zd ZdZ fddZ  ZS )	RunConfigz-Class holding the parameters to launch a run.c                    s   t    t| jtrtdi | j| _t| jtr#tdi | j| _t| jtr2t	di | j| _t| j
trAtdi | j
| _
t| jtrRtdi | j| _d S d S )Nr   )superrL   
isinstancer_   dictrM   ra   rC   rr   r3   rs   rT   rv   rV   rJ   	__class__r   r   rL     s   
zRunConfig.__post_init__)r   r   r    r>   rL   __classcell__r   r   r   r   r     s    r   )!r/   dataclassesr   enumr   typingr   r   r   r    r	   docr
   pydantic.dataclassesr   r   r"   r#   r%   r)   r2   r3   rC   rM   rT   rV   r\   rb   rw   ry   r   r   r   r   r   r   <module>   sT   	%