§
    ‚Štj-"  ã                  óò   — d Z ddlmZ ddlmZmZ ddlm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 d
dlmZ  ej        e¦  «        Ze	r e¦   «         rddlZddlmZ ddlmZ  G d„ de¦  «        ZdS )z3Abstract base class for all Transformers exporters.é    )Úannotations)ÚABCÚabstractmethod)ÚMutableMapping)ÚTYPE_CHECKING)Úversioné   )Úlogging)Ú_is_package_availableÚis_torch_availableé   )ÚExportConfigMixin)Údecompose_for_generationN)ÚCache)ÚPreTrainedModelc                  ój   — e Zd ZU dZg Zded<   i Zded<   i Zded<   d„ Zd„ Z	e
dd„¦   «         Zdd„ZdS )Ú
HfExporterz�
    Abstract base class for all Transformers exporters.

    Subclass and implement [`~HfExporter.export`] to add a new export backend.
    z	list[str]Úrequired_packageszdict[str, str]Úmin_versionsÚtested_versionsc                ó.   — |                       ¦   «          d S )N)Úvalidate_environment)Úselfs    úY/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/exporters/base.pyÚ__init__zHfExporter.__init__7   s   € Ø×!Ò!Ñ#Ô#Ð#Ð#Ð#ó    c           	     óZ  ‡ — g g }}‰ j         D ]¦}t          |d¬¦  «        \  }}|s|                     |¦  «         Œ.‰ j                             |¦  «        }|�\|dk    rV|                     dd¦  «        d         }	|                     dd¦  «        d         }
|	|
k    r|                     ||	|
f¦  «         Œ§|rHd                     ˆ fd	„|D ¦   «         ¦  «        }t          d
t          ‰ ¦  «        j	        › d|› �¦  «        ‚g }‰ j
                             ¦   «         D ]�\  }}t          |d¬¦  «        \  }}|dk    sDt          j        |                     dd¦  «        d         ¦  «        t          j        |¦  «        k     r|                     |› d|› d|› d�¦  «         Œ‚|r9t          t          ‰ ¦  «        j	        › dd                     |¦  «        › �¦  «        ‚|rSd                     d„ |D ¦   «         ¦  «        }t                               t          ‰ ¦  «        j	        › d|› d�¦  «         dS dS )zYCheck `required_packages` are installed and warn on version drift from `tested_versions`.T)Úreturn_versionNzN/Aú+r   r   z, c              3  óR   •K  — | ]!}|‰j         v r|› d ‰j         |         › �n|V — Œ"dS )z==N)r   )Ú.0Úpkgr   s     €r   ú	<genexpr>z2HfExporter.validate_environment.<locals>.<genexpr>N   s]   øè è € ð ð Øbe¸ÀÔ@TÐ9TÐ9T�3Ð5Ð5˜$Ô.¨sÔ3Ð5Ð5Ð5ÐZ]ðð ð ð ð ð r   zTo use z-, please install the following dependencies: z>=z (found ú)z requires newer versions of: c              3  ó2   K  — | ]\  }}}|› d |› d|› �V — ŒdS )z: installed z	, tested N© )r!   r"   ÚgotÚwants       r   r#   z2HfExporter.validate_environment.<locals>.<genexpr>]   s@   è è € ÐeÐeÉ^ÈSÐRUÐW[ 3Ð HÐ H°CÐ HÐ HÀ$Ð HÐ HÐeÐeÐeÐeÐeÐer   u~    is experimental and patches many backend internals; behaviour may differ from what was validated. Version drift detected â€” z-. If you hit issues, try the tested versions.)r   r   Úappendr   ÚgetÚsplitÚjoinÚImportErrorÚtypeÚ__name__r   Úitemsr   ÚparseÚloggerÚwarning)r   ÚargsÚkwargsÚmissingÚdriftr"   ÚexistsÚ	installedÚtestedÚinstalled_baseÚtested_baseÚspecsÚoutdatedÚminimumÚ_Údetailss   `               r   r   zHfExporter.validate_environment:   s£  ø€ ð ˜R�ˆØÔ)ð 
	Eð 
	EˆCÝ 5°cÈ$Ð OÑ OÔ OÑˆF�IØð Ø—’˜sÑ#Ô#Ð#ØØÔ)×-Ò-¨cÑ2Ô2ˆFØÐ! i°5Ò&8Ð&8Ø!*§¢°°aÑ!8Ô!8¸Ô!;�Ø$Ÿlšl¨3°Ñ2Ô2°1Ô5�Ø! [Ò0Ð0Ø—L’L # ~°{Ð!CÑDÔDÐDøàð 	sØ—I’Ið ð ð ð Øipðñ ô ñ ô ˆEõ Ðq­¨T©
¬
Ô(;ÐqÐqÐjoÐqÐqÑrÔrÐrð ˆØ Ô-×3Ò3Ñ5Ô5ð 	Ið 	I‰LˆC�Ý0°ÀTÐJÑJÔJ‰LˆAˆyØ˜EÒ!Ð!¥W¤]°9·?²?À3ÈÑ3JÔ3JÈ1Ô3MÑ%NÔ%NÕQXÔQ^Ð_fÑQgÔQgÒ%gÐ%gØ—’ 3Ð GÐ G¨'Ð GÐ G¸9Ð GÐ GÐ GÑHÔHÐHøØð 	jÝ¥ d¡¤Ô!4ÐhÐhÐSW×S\ÒS\Ð]eÑSfÔSfÐhÐhÑiÔiÐiàð 	Ø—i’iÐeÐeÐ_dÐeÑeÔeÑeÔeˆGÝ�NŠNÝ˜‘:”:Ô&ð ?ð ?Ø\cð?ð ?ð ?ñô ð ð ð ð	ð 	r   Úmodelr   Úsample_inputsú)MutableMapping[str, torch.Tensor | Cache]Úconfigr   c                óJ   — t          t          | ¦  «        j        › d�¦  «        ‚)uˆ  
        Export the model and return the backend-specific program object.

        Args:
            model ([`PreTrainedModel`]):
                The model to export.
            sample_inputs (`dict[str, torch.Tensor | Cache]`):
                **Forward** kwargs â€” what you'd pass to `model(**sample_inputs)`. These are used
                directly as the example inputs during tracing. For an autoregressive decode-step
                export, this means you need to include `past_key_values`, `cache_position`, etc.
                If you only have generation-style inputs, use [`~HfExporter.export_for_generation`]
                instead â€” it runs `model.generate` for you and exports each stage.
            config ([`~transformers.exporters.configs.ExportConfigMixin`]):
                Backend-specific configuration.

        Returns:
            Backend-specific export artifact.
        zø does not implement `export`. Pick a concrete exporter (`DynamoExporter`, `OnnxExporter`, `ExecutorchExporter`), or override `export` in your subclass with a backend-specific tracing pipeline that consumes `config` and returns the runtime artifact.)ÚNotImplementedErrorr.   r/   )r   rB   rC   rE   s       r   ÚexportzHfExporter.exportd   s1   € õ2 "Ý�D‰zŒzÔ"ð 0ð 0ð 0ñ
ô 
ð 	
r   ú0ExportConfigMixin | dict[str, ExportConfigMixin]Úreturnúdict[str, object]c                ón  — t          ||¦  «        }t          |t          ¦  «        rTt          |¦  «        t          |¦  «        z
  }|r0t	          dt          |¦  «        › dt          |¦  «        › d�¦  «        ‚|}nt                               ||¦  «        }i }|                     ¦   «         D ]‰\  }\  }	}
	 |                      |	|
||         ¬¦  «        ||<   Œ+# t          $ rR}t          t          | ¦  «        j        › d|› dt          |	¦  «        j        › dt          |
¦  «        › d�¦  «        |‚d	}~ww xY w|S )
ué  
        Decompose a generative model and export each component independently.

        Thin wrapper around [`~exporters.utils.decompose_for_generation`] that calls
        [`~HfExporter.export`] on every returned `(submodel, forward_inputs)` pair. If you need
        the intermediate `(submodel, forward_inputs)` pairs (for verification, custom inputs,
        skipping a stage, â€¦), call [`~exporters.utils.decompose_for_generation`] directly.

        Args:
            model ([`PreTrainedModel`]):
                The generative model to export. Must support `model.generate(**sample_inputs)`.
            sample_inputs (`dict[str, torch.Tensor | Cache]`):
                **Generate** kwargs â€” what you'd pass to `model.generate(**sample_inputs)`
                (typically `input_ids` + `attention_mask`, plus any modality inputs like
                `pixel_values` / `input_features` for multi-modal models). Per-stage forward
                kwargs are captured internally.
            config ([`~transformers.exporters.configs.ExportConfigMixin`] or `dict[str, ExportConfigMixin]`):
                Backend-specific configuration. Pass a single config to apply to every
                component, or a `dict` keyed by component name (e.g. `"image_encoder"`,
                `"language_model"`, `"lm_head"`, `"decode"`) to override per-component â€”
                all component names must be present in the dict.

        Returns:
            `dict[str, Any]`: `{component_name: backend_specific_artifact}` â€” same keys as
            [`~exporters.utils.decompose_for_generation`]. Values are whatever
            [`~HfExporter.export`] returns for the concrete backend (`ExportedProgram`,
            `ONNXProgram`, `ExecutorchProgramManager`).
        z4Per-component `config` dict is missing entries for: z$. Expected one entry per component: ú.)rE   z.export failed on component 'z' (submodel=z, input keys=z).N)r   Ú
isinstanceÚdictÚsetÚ
ValueErrorÚsortedÚfromkeysr0   rH   Ú	ExceptionÚRuntimeErrorr.   r/   Úlist)r   rB   rC   rE   Ú
componentsr6   ÚconfigsÚexportedÚnameÚsubmodelÚ	subinputsÚes               r   Úexport_for_generationz HfExporter.export_for_generation„   s‘  € õD .¨e°]ÑCÔCˆ
Ý�f�dÑ#Ô#ð 		8Ý˜*‘o”o­¨F©¬Ñ3ˆGØð Ý ðOÍ6ÐRYÉ?Ì?ð Oð OÝ9?À
Ñ9KÔ9KðOð Oð Oñô ð ð ˆGˆGå—m’m J°Ñ7Ô7ˆGØ&(ˆØ+5×+;Ò+;Ñ+=Ô+=ð 	ð 	Ñ'ˆDÑ'�8˜YðØ!%§¢¨X°yÈÐQUÌ Ñ!WÔ!W�˜‘�øÝð ð ð Ý"Ý˜D‘z”zÔ*ð [ð [Èð [ð [Ý!% h¡¤Ô!8ð[ð [ÝGKÈIÁÄð[ð [ð [ñô ð ðøøøøðøøøð
 ˆs   Â4!CÃ
D2Ã AD-Ä-D2N)rB   r   rC   rD   rE   r   )rB   r   rC   rD   rE   rI   rJ   rK   )r/   Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   r   r   r   r   rH   r^   r&   r   r   r   r   *   s©   € € € € € € ðð ð $&ÐÐ%Ð%Ð%Ñ%à#%€LÐ%Ð%Ð%Ñ%à&(€OÐ(Ð(Ð(Ñ(ð$ð $ð $ð(ð (ð (ðT ð
ð 
ð 
ñ „^ð
ð>6ð 6ð 6ð 6ð 6ð 6r   r   )ra   Ú
__future__r   Úabcr   r   Úcollections.abcr   Útypingr   Ú	packagingr   Úutilsr
   Úutils.import_utilsr   r   rX   r   r   Ú
get_loggerr/   r2   ÚtorchÚcache_utilsr   Úmodeling_utilsr   r   r&   r   r   ú<module>rn      sU  ðð :Ð 9à "Ð "Ð "Ð "Ð "Ð "à #Ð #Ð #Ð #Ð #Ð #Ð #Ð #Ø *Ð *Ð *Ð *Ð *Ð *Ø  Ð  Ð  Ð  Ð  Ð  à Ð Ð Ð Ð Ð à Ð Ð Ð Ð Ð Ø JÐ JÐ JÐ JÐ JÐ JÐ JÐ JØ &Ð &Ð &Ð &Ð &Ð &Ø +Ð +Ð +Ð +Ð +Ð +ð 
ˆÔ	˜HÑ	%Ô	%€ð ð 5ØÐÑÔð 5Øˆˆˆà'Ð'Ð'Ð'Ð'Ð'Ø4Ð4Ð4Ð4Ð4Ð4ðPð Pð Pð Pð P�ñ Pô Pð Pð Pð Pr   