o
    hP                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	 d dl
Zd dlZd dlmZ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 eZG dd dejjZG dd dejjZ dS )    N)Path)sleep)CallableOptionalUnion)
Repositorycreate_repo)parse   )IntervalStrategyPreTrainedTokenizerBase)TrainingSummary)kerasc                       s   e Zd ZdZ						ddedeejje	j
ejeef deee  deee  dee d	ed
edee f fddZedddZdd ZdddZ  ZS )KerasMetricCallbacka[  
    Callback to compute metrics at the end of every epoch. Unlike normal Keras metrics, these do not need to be
    compilable by TF. It is particularly useful for common NLP metrics like BLEU and ROUGE that require string
    operations or generation loops that cannot be compiled. Predictions (or generations) will be computed on the
    `eval_dataset` before being passed to the `metric_fn` in `np.ndarray` format. The `metric_fn` should compute
    metrics and return a dict mapping metric names to metric values.

    We provide an example of a suitable metric_fn that computes ROUGE scores for a summarization model below. Note that
    this example skips some post-processing for readability and simplicity, and should probably not be used as-is!

    ```py
    from datasets import load_metric

    rouge_metric = load_metric("rouge")


    def rouge_fn(predictions, labels):
        decoded_predictions = tokenizer.batch_decode(predictions, skip_special_tokens=True)
        decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
        result = rouge_metric.compute(predictions=decoded_predictions, references=decoded_labels)
        return {key: value.mid.fmeasure * 100 for key, value in result.items()}
    ```

    The above function will return a dict containing values which will be logged like any other Keras metric:

    ```
    {'rouge1': 37.4199, 'rouge2': 13.9768, 'rougeL': 34.361, 'rougeLsum': 35.0781
    ```

    Args:
        metric_fn (`Callable`):
            Metric function provided by the user. It will be called with two arguments - `predictions` and `labels`.
            These contain the model's outputs and matching labels from the dataset. It should return a dict mapping
            metric names to numerical values.
        eval_dataset (`tf.data.Dataset` or `dict` or `tuple` or `np.ndarray` or `tf.Tensor`):
            Validation data to be used to generate predictions for the `metric_fn`.
        output_cols (`List[str], *optional*):
            A list of columns to be retained from the model output as the predictions. Defaults to all.
        label_cols ('`List[str]`, *optional*'):
            A list of columns to be retained from the input dataset as the labels. Will be autodetected if this is not
            supplied.
        batch_size (`int`, *optional*):
            Batch size. Only used when the data is not a pre-batched `tf.data.Dataset`.
        predict_with_generate (`bool`, *optional*, defaults to `False`):
            Whether we should use `model.generate()` to get outputs for the model.
        use_xla_generation (`bool`, *optional*, defaults to `False`):
            If we're generating, whether to compile model generation with XLA. This can massively increase the speed of
            generation (up to 100X speedup) but will require a new XLA compilation for each input shape. When using XLA
            generation, it's a good idea to pad your inputs to the same size, or to use the `pad_to_multiple_of`
            argument in your `tokenizer` or `DataCollator`, which will reduce the number of unique input shapes and
            save a lot of compilation time. This option has no effect is `predict_with_generate` is `False`.
        generate_kwargs (`dict`, *optional*):
            Keyword arguments to pass to `model.generate()` when generating. Has no effect if `predict_with_generate`
            is `False`.

    NF	metric_fneval_datasetoutput_cols
label_cols
batch_sizepredict_with_generateuse_xla_generationgenerate_kwargsc	                    sp  t    || _|| _t|tjjs&|d u rtdtjj	|j
|dd}|| _|| _|| _t|jtrBt|jdkrB|j\}	}
n|j}	d }
|d urc|D ]}||	vr[td| dqM|| _d| _n6|
d urnd | _d| _n+d|	v rdg| _d| _td	 nd
|	v rd|	v rd
dg| _d| _td ntdttjtdk rtd || _|d u ri n|| _d | _d S )NzwWhen passing data to KerasMetricCallback that is not a pre-batched tf.data.Dataset the batch_size argument must be set.F)drop_remainder   zLabel z? is in label_cols but could not be found in the dataset inputs!TlabelszTNo label_cols specified for KerasMetricCallback, assuming you want the 'labels' key.start_positionsend_positionsznNo label_cols specified for KerasMetricCallback, assuming you want the start_positions and end_positions keys.zMCould not autodetect label_cols for KerasMetricCallback, please specify them!z2.7zHTF versions less than 2.7 may encounter issues with KerasMetricCallback!)super__init__r   r   
isinstancetfdataDataset
ValueErrorfrom_tensor_slicesbatchr   r   r   element_spectuplelenr   use_keras_labelloggingwarningr	   __version__r   r   generation_function)selfr   r   r   r   r   r   r   r   
input_spec
label_speclabel	__class__ p/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/keras_callbacks.pyr   N   sT   



zKerasMetricCallback.__init__c                    s    d j dkst fdd D rtj ddS tdd  D }tdd  D }tj d |||gt d jd	d   d
}d} D ]}||||t	| d |jd f< |t	|7 }qF|S )Nr   r
   c                 3   s(    | ]}|j d   d j d  kV  qdS )r
   r   Nshape.0r%   batchesr4   r5   	<genexpr>   s   & z;KerasMetricCallback._concatenate_batches.<locals>.<genexpr>)axisc                 S      g | ]}|j d  qS )r
   r7   r9   r4   r4   r5   
<listcomp>       z<KerasMetricCallback._concatenate_batches.<locals>.<listcomp>c                 S   r?   )r   r7   r9   r4   r4   r5   r@      rA   r   )
fill_valuer8   )
ndimallnpconcatenatemaxsum	full_likelistr8   r(   )r<   padding_indexmax_lennum_samplesoutputir%   r4   r;   r5   _concatenate_batches   s   $$"z(KerasMetricCallback._concatenate_batchesc                    s  t |d tr0i }|d  D ] |  fdd|D | < qt|dkr.t| d }|S t |d ts>t |d tr[g }t| D ]
}|	| | qDt|dkrY|d }|S t |d t
jrj| |}|S t |d tjr~| dd |D }|S tdt|d  d)Nr   c                    s   g | ]}|  qS r4   r4   r9   keyr4   r5   r@          zJKerasMetricCallback._postprocess_predictions_or_labels.<locals>.<listcomp>r
   c                 S      g | ]}|  qS r4   numpy)r:   tensorr4   r4   r5   r@      rS   zCouldn't handle batch of type !)r   dictkeysrP   r(   rJ   valuesr'   zipappendrE   ndarrayr    Tensor	TypeErrortype)r.   inputsoutputs
input_listr4   rQ   r5   "_postprocess_predictions_or_labels   s*   
z6KerasMetricCallback._postprocess_predictions_or_labelsc                    sP  t jdrtjjdg ng d }jrFt jdr)t jjdr)jjj}ntjdd}jrFjd u rFfdd}t	j
|dd	_g }g }jD ] t trY \ }nd }jrt trn | } d
d }	n }d }	jr}j||	dn8jj|fd
|	ijn*j ttrtjd urfddjD nfdd D | jsȇ fddjD }n5t|trdd | D }n&t|tst|trdd |D }nt|t	jr| }n	tdt| || qM|}
|}|
|f}t|ts!td| || d S )Nconfigkeys_to_ignore_at_inferenceencodermain_input_name	input_idsc                    s    j j| fd|i jS )Nattention_mask)modelgenerater   )rb   rk   )r.   r4   r5   r-      s   z=KerasMetricCallback.on_epoch_end.<locals>.generation_functionT)jit_compilerk   )rk   c                    s   i | ]}| | qS r4   r4   r:   rR   )predictionsr4   r5   
<dictcomp>   rA   z4KerasMetricCallback.on_epoch_end.<locals>.<dictcomp>c                    s$   i | ]\}}| d g vr||qS )lossr4   )r:   rR   val)ignore_keysr4   r5   rq      s    c                    s   i | ]	}| |   qS r4   rU   ro   )r%   r4   r5   rq          c                 S   s   i | ]	\}}||  qS r4   rU   )r:   rR   arrayr4   r4   r5   rq      ru   c                 S   rT   r4   rU   )r:   rv   r4   r4   r5   r@      rS   z4KerasMetricCallback.on_epoch_end.<locals>.<listcomp>zConfused by labels of type zSmetric_fn should return a dict mapping metric names to values but instead returned ) hasattrrl   getattrrf   r   rh   ri   r   r-   r    functionr   r   r'   rY   getrm   r   predict_on_batchr   itemsr]   r)   r   rJ   r_   rV   r`   ra   re   r   update)r.   epochlogsri   r-   prediction_list
label_listr   generation_inputsrk   	all_preds
all_labelsmetric_outputr4   )r%   rt   rp   r.   r5   on_epoch_end   sv   











z KerasMetricCallback.on_epoch_end)NNNFFN)r6   N)__name__
__module____qualname____doc__r   r   r    r!   r"   rE   r^   r_   r'   rY   r   rJ   strintboolr   staticmethodrP   re   r   __classcell__r4   r4   r2   r5   r      s:    =

	Ar   c                       s   e Zd ZdZ						ddeeef deeef dee	 dee
 d	ee d
ee def fddZdddZdddZdddZdddZ  ZS )PushToHubCallbackaK	  
    Callback that will save and push the model to the Hub regularly. By default, it pushes once per epoch, but this can
    be changed with the `save_strategy` argument. Pushed models can be accessed like any other model on the hub, such
    as with the `from_pretrained` method.

    ```py
    from transformers.keras_callbacks import PushToHubCallback

    push_to_hub_callback = PushToHubCallback(
        output_dir="./model_save",
        tokenizer=tokenizer,
        hub_model_id="gpt5-7xlarge",
    )

    model.fit(train_dataset, callbacks=[push_to_hub_callback])
    ```

    Args:
        output_dir (`str`):
            The output directory where the model predictions and checkpoints will be written and synced with the
            repository on the Hub.
        save_strategy (`str` or [`~trainer_utils.IntervalStrategy`], *optional*, defaults to `"epoch"`):
            The checkpoint save strategy to adopt during training. Possible values are:

                - `"no"`: Save is done at the end of training.
                - `"epoch"`: Save is done at the end of each epoch.
                - `"steps"`: Save is done every `save_steps`
        save_steps (`int`, *optional*):
            The number of steps between saves when using the "steps" `save_strategy`.
        tokenizer (`PreTrainedTokenizerBase`, *optional*):
            The tokenizer used by the model. If supplied, will be uploaded to the repo alongside the weights.
        hub_model_id (`str`, *optional*):
            The name of the repository to keep in sync with the local `output_dir`. It can be a simple model ID in
            which case the model will be pushed in your namespace. Otherwise it should be the whole repository name,
            for instance `"user_name/model"`, which allows you to push to an organization you are a member of with
            `"organization_name/model"`.

            Will default to the name of `output_dir`.
        hub_token (`str`, *optional*):
            The token to use to push the model to the Hub. Will default to the token in the cache folder obtained with
            `huggingface-cli login`.
        checkpoint (`bool`, *optional*, defaults to `False`):
            Whether to save full training checkpoints (including epoch and optimizer state) to allow training to be
            resumed. Only usable when `save_strategy` is `"epoch"`.
    r~   NF
output_dirsave_strategy
save_steps	tokenizerhub_model_id	hub_token
checkpointc           	         s   t    |r|dkrtdt|trt| }|| _| jtjkr0t|t	r,|dkr0td|| _
t|}|d u r@| j}t|d|dj| _|| _tt| j| j|d| _|| _d | _|| _d | _|| _d S )Nr~   z:Cannot save checkpoints when save_strategy is not 'epoch'!r   zWPlease supply a positive integer argument for save_steps when save_strategy == 'steps'!T)repo_idexist_oktoken)
clone_fromr   )r   r   r#   r   r   r   lowerr   STEPSr   r   r   absolutenamer   r   r   r   r   repor   last_jobr   training_historymodel_card_args)	r.   r   r   r   r   r   r   r   r   r2   r4   r5   r   ;  s(   



zPushToHubCallback.__init__c                 C   s
   g | _ d S r   )r   )r.   r   r4   r4   r5   on_train_begin_  s   
z PushToHubCallback.on_train_beginc                 C   s   | j tjkr=|d | j dkr?| jd ur| jjsd S | j| j | j	d ur-| j	| j | j
jd| dd\}| _d S d S d S )Nr
   r   zTraining in progress steps Fcommit_messageblocking)r   r   r   r   r   is_donerl   save_pretrainedr   r   r   push_to_hub)r.   r%   r   _r4   r4   r5   on_train_batch_endd  s   

z$PushToHubCallback.on_train_batch_endc                 C   s  |  }d|vr||d< | j| | jtjkr| jd ur#| jjs#d S | j	| j
 | jd ur6| j	| j
 | jrHtj| j
d}| j|| tjd	| j| j| jd| j}| }| j
d d}|| W d    n1 stw   Y  | jjd| dd\}| _d S d S )
Nr~   r   rl   
model_namekeras_history	README.mdwzTraining in progress epoch Fr   r4   )copyr   r]   r   r   EPOCHr   r   rl   r   r   r   r   ospathjoin_save_checkpointr   
from_kerasr   r   to_model_cardopenwriter   r   )r.   r~   r   checkpoint_dirtrain_summary
model_cardfr   r4   r4   r5   r   o  s8   

zPushToHubCallback.on_epoch_endc                 C   s   | j d ur| j jstd | j jstd | j jrd S d S | j| j | jd ur1| j| j t	j
d	| j| j| jd| j}| }| jd d}|| W d    n1 s]w   Y  | jjddd d S )
Nz;Pushing the last epoch to the Hub, this may take a while...r
   r   r   r   zEnd of trainingTr   r4   )r   r   r*   infor   rl   r   r   r   r   r   r   r   r   r   r   r   r   r   )r.   r   r   r   r   r4   r4   r5   on_train_end  s(   

zPushToHubCallback.on_train_end)r~   NNNNFr   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r4   r4   r2   r5   r     s6    1


$

r   )!r*   r   pathlibr   timer   typingr   r   r   rV   rE   
tensorflowr    huggingface_hubr   r   packaging.versionr	    r   r   	modelcardr   modeling_tf_utilsr   	getLoggerr   logger	callbacksCallbackr   r   r4   r4   r4   r5   <module>   s     
 y