o
    h<P                     @   s  d dl Z d dlZd dlmZmZ d dlmZ d dlZd dlZ	d dl
mZ ddlmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZmZmZ dd	lmZ e r^d dlZej j!"d
 e red dl#Z#e rnd dl$m%Z% dZ&defddZ'G dd deZ(dS )    N)ArgumentParser	Namespace)import_module)version   )FEATURE_EXTRACTOR_MAPPINGIMAGE_PROCESSOR_MAPPINGPROCESSOR_MAPPINGTOKENIZER_MAPPING
AutoConfigAutoFeatureExtractorAutoImageProcessorAutoProcessorAutoTokenizeris_datasets_availableis_tf_availableis_torch_available)TF2_WEIGHTS_INDEX_NAMETF2_WEIGHTS_NAMElogging   )BaseTransformersCLICommandF)load_datasetg-C6
?argsc              	   C   s&   t | j| j| j| j| j| j| j| jS )z~
    Factory function used to convert a model PyTorch checkpoint in a TensorFlow 2 checkpoint.

    Returns: ServeCommand
    )	PTtoTFCommand
model_name	local_dir	max_errornew_weightsno_prpushextra_commit_descriptionoverride_model_class)r    r#   T/var/www/html/ai/venv/lib/python3.10/site-packages/transformers/commands/pt_to_tf.pyconvert_command_factory9   s   r%   c                   @   sd   e Zd ZedefddZedd Zdededed	e	d
e	de	dedefddZ
dd Zdd ZdS )r   parserc                 C   s   | j ddd}|jdtddd |jdtd	d
d |jdttdt dd |jdddd |jdddd |jdddd |jdtd	dd |jdtddd |jtd dS )z
        Register this command to argparse so it's available for the transformer-cli

        Args:
            parser: Root parser to register command-specific arguments
        zpt-to-tfzCLI tool to run convert a transformers model from a PyTorch checkpoint to a TensorFlow checkpoint. Can also be used to validate existing weights without opening PRs, with --no-pr.)helpz--model-nameTzAThe model name, including owner/organization, as seen on the hub.)typerequiredr'   z--local-dir zOOptional local directory of the model repository. Defaults to /tmp/{model_name})r(   defaultr'   z--max-errorz%Maximum error tolerance. Defaults to z4. This flag should be avoided, use at your own risk.z--new-weights
store_truezKOptional flag to create new TensorFlow weights, even if they already exist.)actionr'   z--no-prz6Optional flag to NOT open a PR with converted weights.z--pushzKOptional flag to push the weights directly to `main` (requires permissions)z--extra-commit-descriptionzXOptional additional commit description to use when opening a PR (e.g. to tag the owner).z--override-model-classNzIf you think you know better than the auto-detector, you can specify the model class here. Can be either an AutoModel class or a specific model class like BertForSequenceClassification.)func)
add_parseradd_argumentstrfloat	MAX_ERRORset_defaultsr%   )r&   train_parserr#   r#   r$   register_subcommandL   s`   
z!PTtoTFCommand.register_subcommandc                    sP   t |  }t | }||krtd| d| dd fdd	  | |i S )	zr
        Compares the TensorFlow and PyTorch outputs, returning a dictionary with all tensor differences.
        zAThe model outputs have different attributes, aborting. (Pytorch: z, TensorFlow: )r*   c           
         s   t | tjrtt|  |  }|||< |S |}t| D ])\}}t |tr6|| }|| }	| | }n|d| d }|| }	 ||	||}q |S )N[])	
isinstancetorchTensornpmaxabsnumpy	enumerater1   )
pt_outtf_outdifferences	attr_nametensor_difference	root_nameipt_itembranch_nametf_item_find_pt_tf_differencesr#   r$   rM      s   

zEPTtoTFCommand.find_pt_tf_differences.<locals>._find_pt_tf_differencesN)r*   )setkeys
ValueError)
pt_outputs
tf_outputspt_out_attrstf_out_attrsr#   rL   r$   find_pt_tf_differences   s   z$PTtoTFCommand.find_pt_tf_differencesr   r   r   r   r   r    r!   r"   c	           
      G   sR   t d| _|| _|r|ntjd|| _|| _|| _	|| _
|| _|| _|| _d S )Nztransformers-cli/pt_to_tfz/tmp)r   
get_logger_logger_model_nameospathjoin
_local_dir
_max_error_new_weights_no_pr_push_extra_commit_description_override_model_class)
selfr   r   r   r   r   r    r!   r"   r   r#   r#   r$   __init__   s   
zPTtoTFCommand.__init__c                 C   s*  dd }t |j}|tv r$t| j}|tv r#|jjdu r#|jj	|j_n2|t
v r/t| j}n'|tv r:t| j}n|tv rNt| j}|jdu rM|j	|_ntd| dtt|jj }i }d|v rr|ddgd	d	d
 d|v rtdddddd d }	|d|	i d|v rt|jj}
d|
v r|
d j}|dur|dur|}nd	}nd	}|| |d d|v r|| d	d |d!i |ddi}|d!i |ddi}|jst|drt|dsd|v rtjdgdggtd|jjpd  }|dt !|i |dt"#|i ||fS )"zQ
        Returns the right inputs for the model, based on its signature.
        c                  S   s@   t dddd} | dtdd d d }dd	 |D }|S )
Nz)hf-internal-testing/librispeech_asr_dummyclean
validationsplitidr   audioc                 S   s   g | ]}|d  qS )arrayr#   ).0xr#   r#   r$   
<listcomp>   s    zFPTtoTFCommand.get_inputs.<locals>._get_audio_input.<locals>.<listcomp>)r   sortselectrange)dsspeech_samplesraw_samplesr#   r#   r$   _get_audio_input   s    z2PTtoTFCommand.get_inputs.<locals>._get_audio_inputNz1Unknown data processing type (model config type: r7   	input_idsz	Hi there!z@I am a batch with more than one row and different input lengths.T)textpadding
truncationpixel_valuescifar10
plain_texttestrg   r   imgimagesinput_featuresrx   F)rj   rx   input_valuesreturn_tensorspttfencoderdecoderdecoder_input_idsr   )dtyper   r#   )$r(   configr	   r   from_pretrainedr\   r
   	tokenizer	pad_token	eos_tokenr   r   r   r   r   rP   rN   inspect	signatureforward
parametersrO   updater   feature_extractorr+   is_encoder_decoderhasattrr=   asarrayintdecoder_start_token_idr;   tensorr   convert_to_tensor)rc   pt_modeltf_dummy_inputsr   ru   model_config_class	processormodel_forward_signatureprocessor_inputssample_imagesfeature_extractor_signaturedefault_strategypadding_strategypt_inputtf_inputr   r#   r#   r$   
get_inputs   sj   



"zPTtoTFCommand.get_inputsc           !   	      s  t tjt dk rtdddlm}m} ddlm} | j	 j
d}t j	}|j} jd ur jdrB jdd  g}n jg}zttd	|d }W n tya   td
 j dw zttd	d|d  }W nj ty   td j dw |d u rttd	d}ttd	d} jd nAt|dkrtd| d jd|d   ttd	|d }zttd	d|d  }W n ty   td|d  dw ||}	|	j}
~	| j	}|   ||
|\}}t  |dBi |ddi}W d    n	1 sw   Y  ~|j j	dd}	|	dBi |ddd} ||}dd | D }dd | D }t|dkrW|d urWtd| d|r`t|  nd }t|  }| j!kst| j!krtd!d" j! d# d$" fd%d&| D  d' j! d# d$" fd(d&| D  t#j$" j	t%}t#j$" j	t&}t#j$'|st#j$'|rĈ j(r|	) j	 ~	| j	}|dBi |ddi} ||}d)d | D }d*d | D }t|dkr|d urtd| d|rt|  nd }t|  }| j!ks%| j!krStd+d" j! d# d$" fd,d&| D  d' j! d# d$" fd-d&| D   j(rYd.nd/} j*r||j+dd0 |,| |j-dd1  jd2 j
  d S  j.s jd3 d4|d5d6|d5d7|d5d8|d5d9	} j!t/kr|d: j! d;7 } j0r|d< j0 7 }t#j$'|r|t&|d=g}t1j2j34 j	d> D ]}||t#j$5||d=g7 }qn|t%|d=g}| j
|||d?dd@j6}  jdA|   d S d S )CNz0.9.0zrThe huggingface_hub version must be >= 0.9.0 to use this command. Please update your huggingface_hub installation.r   )
Repositorycreate_commit)CommitOperationAdd)r   
clone_fromTFr   transformerszModel class z not found in transformers.zTF model class TF	AutoModelTFAutoModelz5No detected architecture, using AutoModel/TFAutoModelr   zAMore than one architecture was found, aborting. (architectures = r7   zDetected architecture: zThe TensorFlow equivalent of z doesn't exist in transformers.output_hidden_statesT)from_ptF)r   trainingc                 S      i | ]\}}d |vr||qS hiddenr#   rl   kvr#   r#   r$   
<dictcomp>N      z%PTtoTFCommand.run.<locals>.<dictcomp>c                 S      i | ]\}}d |v r||qS r   r#   r   r#   r#   r$   r   O  r   z;Something went wrong -- the config file has architectures (zF), but no model head output was found. All outputs start with 'hidden'g        zOThe cross-loaded TensorFlow model has different outputs, something went wrong!
z9
List of maximum output differences above the threshold (z):

c                    *   g | ]\}}| j kr| d |dqS z: .3er]   r   rc   r#   r$   rn   [     * z%PTtoTFCommand.run.<locals>.<listcomp>z@

List of maximum hidden layer differences above the threshold (c                    r   r   r   r   r   r#   r$   rn   ]  r   c                 S   r   r   r#   r   r#   r#   r$   r   k  r   c                 S   r   r   r#   r   r#   r#   r$   r   l  r   zLThe converted TensorFlow model has different outputs, something went wrong!
c                    r   r   r   r   r   r#   r$   rn   x  r   c                    r   r   r   r   r   r#   r$   rn   z  r   zUpdate TF weightszAdd TF weights)auto_lfs_track)blockingzTF weights pushed into z&Uploading the weights into a new PR...a  Model converted by the [`transformers`' `pt_to_tf` CLI](https://github.com/huggingface/transformers/blob/main/src/transformers/commands/pt_to_tf.py). All converted model outputs and hidden layers were validated against its PyTorch counterpart.

Maximum crossload output difference=r   z,; Maximum crossload hidden layer difference=z';
Maximum conversion output difference=z-; Maximum conversion hidden layer difference=z;
zB

CAUTION: The maximum admissible error was manually increased to !z

)path_in_repopath_or_fileobjz/tf_model-*.h5model)repo_id
operationscommit_messagecommit_description	repo_type	create_przPR open in r#   )7r   parsehuggingface_hub__version__ImportErrorr   r   huggingface_hub._commit_apir   r\   rX   r   r   architecturesrb   
startswithgetattrr   AttributeErrorrP   rW   warninglenfrom_configdummy_inputsevalr   r;   no_gradrU   itemsr>   valuesr]   r[   rY   rZ   r   r   existsr^   save_pretrainedr`   git_add
git_commitgit_pushr_   r3   ra   r   iogfileglobbasenamepr_url)!rc   r   r   r   repor   r   pt_classtf_classtf_from_pt_modelr   r   r   r   rQ   tf_from_pt_outputscrossload_differencesoutput_differenceshidden_differencesmax_crossload_output_diffmax_crossload_hidden_difftf_weights_pathtf_weights_index_pathtf_modelrR   conversion_differencesmax_conversion_output_diffmax_conversion_hidden_diffr   commit_descritionr   
shard_path
hub_pr_urlr#   r   r$   run  s  



	$

	

zPTtoTFCommand.runN)__name__
__module____qualname__staticmethodr   r6   rU   r1   r2   boolrd   r   r   r#   r#   r#   r$   r   K   s0    >
%	
Fr   ))r   rY   argparser   r   	importlibr   r   r@   r=   	packagingr   r*   r   r   r	   r
   r   r   r   r   r   r   r   r   utilsr   r   r   r   
tensorflowr   r   experimental enable_tensor_float_32_executionr;   datasetsr   r3   r%   r   r#   r#   r#   r$   <module>   s(   8