
    ^j                        d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
mZmZ dd	lmZmZ dd
lmZmZ eeedZeeedZ ej*                  e      Z G d d      Z G d d      ZddZddZddZy)z0Auto exporter factory for HuggingFace exporters.    )annotations   )logging   )
HfExporter)ExportConfigMixinExportFormat)DynamoConfigDynamoExporter)ExecutorchConfigExecutorchExporter)
OnnxConfigOnnxExporter)
executorchdynamoonnxc                  "    e Zd ZdZedd       Zy)AutoExportConfigz
    The Auto-HF export config class that takes care of automatically dispatching to the correct
    export config given an export config stored in a dictionary.
    c           	        |j                  d      }|t        d      t        |t              r|j                  }n|}|t
        vr,t        d| dt        t
        j                                      t
        |   }|j                  |      S )Nexport_formatzHexport_config_dict must contain key 'export_format' set to exporter namezUnknown exporter type, got z - supported exporters are: )	get
ValueError
isinstancer	   valueAUTO_EXPORT_CONFIG_MAPPINGlistkeys	from_dict)clsexport_config_dictr   name
target_clss        f/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/exporters/auto.pyr   zAutoExportConfig.from_dict0   s    *..? ghh m\2 &&D D11-dV3OPTUoUtUtUvPwOxy  05
##$677    N)r    dict)__name__
__module____qualname____doc__classmethodr    r$   r#   r   r   *   s    
 8 8r$   r   c                  F    e Zd ZdZedd       Zedd       Zedd       Zy)	AutoHfExporterz
    The Auto-HF expoerter class that takes care of automatically instantiating to the correct
    `HfExporter` given the `ExportConfig`.
    c                   t        |t              r|j                         n|}| j                  |      st	        d|dt        t               d      |d   }t        |t              r|j                  n|}t        |   di |S )NzUnsupported export config: z. Registered exporters: .r   r+   )	r   r   to_dictsupports_export_formatr   sortedAUTO_EXPORTER_MAPPINGr	   r   )r   export_configkwargsr    r   r!   s         r#   from_configzAutoHfExporter.from_configL   s     9C=Rc8d]224jw))*<=-.@-C D))/0E)F(GqJ 
 +?;&0&M}""S`$T*4V44r$   c                    t        d      )uC  
        Load an exporter instance from a pretrained model/checkpoint that ships an export config.

        **Not implemented yet** — placeholder for a first-class "export recipe" workflow.

        The idea: model owners publish an ``export_config.json`` (or an ``export_config`` field in
        ``config.json``) alongside their weights on the Hub. That file captures the settings the
        owner has already validated for their architecture — the target format (``dynamo`` /
        ``onnx`` / ``executorch``), exact dynamic-shape specs (e.g. ``text_ids`` dynamic to 4096,
        image tiles fixed at 448, ``batch=1`` for edge deployment), ``strict`` flag, ONNX opset,
        prefill vs. decode layout, ExecuTorch backend choice, and any other knob that today lives
        as tribal knowledge in a README or a private notebook.

        Consumers then get the owner-validated export in one call::

            exporter = AutoHfExporter.from_pretrained("org/model-name")
            program = exporter.export(model, inputs)

        Composes with the [`register_export_input_preparer`] registry: the owner supplies the
        shape spec via ``export_config.json``, transformers supplies the data-dependent
        precomputations (``cu_seqlens``, vision position ids, window indices, …) for that
        architecture. Together they cover the two hard parts of exporting new models — knowing
        the right shape contract and preparing the right inputs — so downstream users don't
        re-derive either from scratch (and don't break in production when they get it wrong).
        zAutoHfExporter.from_pretrained is not implemented yet. Load/export configs explicitly and call AutoHfExporter.from_config(...) instead.)NotImplementedError)r   pretrained_model_name_or_pathr5   s      r#   from_pretrainedzAutoHfExporter.from_pretrainedZ   s    6 "_
 	
r$   c                   | j                  d      }|'t        j                  dt        t               d       yt        |t              r|j                  n|}|t        v }|t        v }|sE|sCt        j                  d|dt        t        t              t        t              z         d       y|st        j                  d|d|d	       y|st        j                  d|d
|d	       yy)a   Return True if the provided dict describes an ``export_format`` that has both a
        registered config class and a registered exporter class. Warns with an actionable message
        when the format is missing entirely, unknown, or only half-registered.r   uB   No 'export_format' key in export config — supported values are: z. Skipping.FzUnknown export format u    — supported values are: zExport format z[ has a registered exporter but no config class. Register one via ``@register_export_config(z)``. Skipping.zV has a registered config class but no exporter. Register one via ``@register_exporter(T)
r   loggerwarningr2   r3   r   r	   r   r   set)r    
export_fmtr!   
has_confighas_exporters        r#   r1   z%AutoHfExporter.supports_export_formatz   s   
 (++O<
NNT/01> #-j,#GzZ77
44,NN(6Q#34s;U7VVWXXce NN  )>>BX^U NN  )99=P r$   N)r4   zExportConfigMixin | dictreturnr   )rB   zHfExporter | None)r    r%   rB   bool)	r&   r'   r(   r)   r*   r6   r:   staticmethodr1   r+   r$   r#   r-   r-   F   sC    
 5 5 
 
> " "r$   r-   c                      fd}|S )Nc                    t         v rt        j                  d d       t        | t              st        d      | t         <   | S )Nz
Exporter '0' is already registered and will be overwritten.zExporter must extend HfExporter)r3   r<   r=   
issubclassr   	TypeErrorr   r!   s    r#   register_exporter_fnz/register_exporter.<locals>.register_exporter_fn   sH    ((NNZv-]^_#z*=>>&)d#
r$   r+   )r!   rK   s   ` r#   register_exporterrL      s      r$   c                      fd}|S )Nc                    t         v rt        j                  d d       t        | t              st        d      | t         <   | S )NzExport config 'rG   z+Export config must extend ExportConfigMixin)r   r<   r=   rH   r   rI   rJ   s    r#   register_export_config_fnz9register_export_config.<locals>.register_export_config_fn   sI    --NN_TF2bcd#01IJJ+."4(
r$   r+   )r!   rO   s   ` r#   register_export_configrP      s     %$r$   c                ,    t         j                  |       S )N)r-   r6   )r4   s    r#   get_hf_exporterrR      s    %%m44r$   N)r!   str)rB   r   )r)   
__future__r   utilsr   baser   configsr   r	   exporter_dynamor
   r   exporter_executorchr   r   exporter_onnxr   r   r3   r   
get_loggerr&   r<   r   r-   rL   rP   rR   r+   r$   r#   <module>r\      s    7 "   4 9 E 3 %  #  
		H	%8 88W Wt	 	%5r$   