o
    Ti                     @   sR   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 G dd de j	Z
dS )    N)Optional)ACCEPTED_MODEL_TYPES	ModelSpecc                   @   sz   e Zd ZdZedejdejfddZdejde	fddZ
					
dde	dee	 dee	 dede	f
ddZejdd Zd	S )	Converterz Base class for model converters.parserreturnc                 C   sF   | j dddd | j dddd | j d	dtd
d | j dddd | S )zAdds common conversion options to the command line parser.

        Arguments:
          parser: Command line argument parser.
        z--output_dirTzOutput model directory.)requiredhelpz--vocab_mappingNz#Vocabulary mapping file (optional).)defaultr	   z--quantizationzWeight quantization type.)r
   choicesr	   z--force
store_truez=Force conversion even if the output directory already exists.)actionr	   )add_argumentr   )r    r   T/home/ubuntu/.local/lib/python3.10/site-packages/ctranslate2/converters/converter.pydeclare_arguments   s$   zConverter.declare_argumentsargsc                 C   s   | j |j|j|j|jdS )a  Helper function to call :meth:`ctranslate2.converters.Converter.convert`
        with the parsed command line options.

        Arguments:
          args: Namespace containing parsed arguments.

        Returns:
          Path to the output directory.
        )vmapquantizationforce)convert
output_dirvocab_mappingr   r   )selfr   r   r   r   convert_from_args(   s   
zConverter.convert_from_argsNFr   r   r   r   c                 C   s   t j|r|std| |  }|du rtd|dur#|| |  |j|d t j|r8t	
| t | || |S )a)  Converts the model to the CTranslate2 format.

        Arguments:
          output_dir: Output directory where the CTranslate2 model is saved.
          vmap: Optional path to a vocabulary mapping file that will be included
            in the converted model directory.
          quantization: Weight quantization scheme (possible values are: int8, int8_float32,
            int8_float16, int8_bfloat16, int16, float16, bfloat16, float32).
          force: Override the output directory if it already exists.

        Returns:
          Path to the output directory.

        Raises:
          RuntimeError: If the output directory already exists and :obj:`force`
            is not set.
          NotImplementedError: If the converter cannot convert this model to the
            CTranslate2 format.
        z;output directory %s already exists, use --force to overrideNz<This model is not supported by CTranslate2 or this converter)r   )ospathexistsRuntimeError_loadNotImplementedErrorregister_vocabulary_mappingvalidateoptimizeshutilrmtreemakedirssave)r   r   r   r   r   
model_specr   r   r   r   9   s(   



zConverter.convertc                 C   s   t  )N)r    )r   r   r   r   r   k   s   zConverter._load)NNF)__name__
__module____qualname____doc__staticmethodargparseArgumentParserr   	Namespacestrr   r   boolr   abcabstractmethodr   r   r   r   r   r      s*    
2r   )r3   r.   r   r$   typingr   ctranslate2.specs.model_specr   r   ABCr   r   r   r   r   <module>   s    