o
    5ti                     @   sf   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m	Z	 e
eZedG dd de	ZdS )	    N)	find_spec)Path)register_model)HFLMopenvinoc                       sB   e Zd ZdZ	d	d fddZ				dd
eddfddZ  ZS )	OptimumLMu/  
    Optimum Intel provides a simple interface to optimize Transformer models and convert them to     OpenVINO™ Intermediate Representation (IR) format to accelerate end-to-end pipelines on     Intel® architectures using OpenVINO™ runtime.

    To use an OpenVINO config, use `--model_args ov_config` to point to a json file with an OpenVINO config:
    `lm_eval --model openvino --model_args pretrained=gpt2,ov_config=config.json --task lambada_openai`
    Example json file contents: {"INFERENCE_PRECISION_HINT": "f32", "CACHE_DIR": "model_cache"}
    cpureturnNc                    sH   d|v r|d dv sJ d|| _ t jd| j |ddd| d S )Nbackend)causalseq2seqzHCurrently, only OVModelForCausalLM or OVModelForSeq2SeqLM are supported.r   )devicer
    )openvino_devicesuper__init__pop)selfr   kwargs	__class__r   M/home/ubuntu/.local/lib/python3.10/site-packages/lm_eval/models/optimum_lm.pyr      s   

zOptimumLM.__init__mainautoF
pretrainedc                 K   s  t dstdddlm}m} |r|ni }d|v rNt|d  s&tdt|d }	t	
|	|d< td|d   W d    n1 sHw   Y  ni |d< |d dd	 d
|v rh|d
 rhd|d d< | jdkro|n|}
|
j|f||| j d|| _d S )NoptimumzYpackage `optimum` is not installed. Please install it via `pip install optimum[openvino]`r   )OVModelForCausalLMOVModelForSeq2SeqLM	ov_configzDov_config should point to a .json file containing an OpenVINO configzUsing custom OpenVINO config: 	CACHE_DIR pipeline_parallelPIPELINE_PARALLELMODEL_DISTRIBUTION_POLICYr   )revisiontrust_remote_coder   )r   ModuleNotFoundErroroptimum.intel.openvinor   r   r   exists
ValueErroropenjsonloadeval_loggerinfo
setdefaultr
   from_pretrainedr   upper_model)r   r   r$   dtyper%   r   r   r   model_kwargsf	model_clsr   r   r   _create_model+   sF   
zOptimumLM._create_model)r   )r	   N)r   r   F)__name__
__module____qualname____doc__r   strr7   __classcell__r   r   r   r   r      s    r   )r+   loggingimportlib.utilr   pathlibr   lm_eval.api.registryr   lm_eval.models.huggingfacer   	getLoggerr8   r-   r   r   r   r   r   <module>   s    
