
    #i/                    H   d dl mZ d dlZd dlmZ d dlmZ d dlZd dlmZ d dl	m
Z
mZmZ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 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$ d dl%m&Z&  e$       r
d dl'm(Z(m)Z)m*Z*  ejV                  e,      Z- G d de      Z.y)    )annotationsN)Callable)Any)nn)EvalPredictionFeatureExtractionMixinPreTrainedTokenizerBaseProcessorMixinTrainerCallback)BaseImageProcessor)BaseEvaluator)BaseTrainer)(SpladeRegularizerWeightSchedulerCallback)SparseEncoderDataCollator)"SparseMultipleNegativesRankingLoss
SpladeLoss)SparseEncoder)SparseEncoderModelCardCallbackSparseEncoderModelCardData)SparseEncoderTrainingArguments)is_datasets_available)deprecated_kwargs)DatasetDatasetDictIterableDatasetc                       e Zd ZdZeZeZeZ	e
ZeZ ed      	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd       Zd	dZ	 	 	 	 	 	 d
 fdZ xZS )SparseEncoderTraineru  
    SparseEncoderTrainer is a simple but feature-complete training and eval loop for PyTorch
    based on the SentenceTransformerTrainer that based on 🤗 Transformers :class:`~transformers.Trainer`.

    This trainer integrates support for various :class:`transformers.TrainerCallback` subclasses, such as:

    - :class:`~transformers.integrations.WandbCallback` to automatically log training metrics to W&B if `wandb` is installed
    - :class:`~transformers.integrations.TensorBoardCallback` to log training metrics to TensorBoard if `tensorboard` is accessible.
    - :class:`~transformers.integrations.CodeCarbonCallback` to track the carbon emissions of your model during training if `codecarbon` is installed.

        - Note: These carbon emissions will be included in your automatically generated model card.

    See the Transformers `Callbacks <https://huggingface.co/docs/transformers/main/en/main_classes/callback>`_
    documentation for more information on the integrated callbacks and how to write your own callbacks.

    Args:
        model (:class:`~sentence_transformers.sparse_encoder.model.SparseEncoder`, *optional*):
            The model to train, evaluate or use for predictions. If not provided, a `model_init` must be passed.
        args (:class:`~sentence_transformers.sparse_encoder.training_args.SparseEncoderTrainingArguments`, *optional*):
            The arguments to tweak for training. Will default to a basic instance of
            :class:`~sentence_transformers.sparse_encoder.training_args.SparseEncoderTrainingArguments` with the
            `output_dir` set to a directory named *tmp_trainer* in the current directory if not provided.
        train_dataset (Union[:class:`datasets.Dataset`, :class:`datasets.DatasetDict`, :class:`datasets.IterableDataset`, Dict[str, :class:`datasets.Dataset`]], *optional*):
            The dataset to use for training. Must have a format accepted by your loss function, see
            `Training Overview > Dataset Format <../../../docs/sparse_encoder/training_overview.html#dataset-format>`_.
        eval_dataset (Union[:class:`datasets.Dataset`, :class:`datasets.DatasetDict`, :class:`datasets.IterableDataset`, Dict[str, :class:`datasets.Dataset`]], *optional*):
            The dataset to use for evaluation. Must have a format accepted by your loss function, see
            `Training Overview > Dataset Format <../../../docs/sparse_encoder/training_overview.html#dataset-format>`_.
        loss (Optional[Union[:class:`torch.nn.Module`, Dict[str, :class:`torch.nn.Module`],            Callable[[:class:`~sentence_transformers.sparse_encoder.model.SparseEncoder`], :class:`torch.nn.Module`],            Dict[str, Callable[[:class:`~sentence_transformers.sparse_encoder.model.SparseEncoder`]]]], *optional*):
            The loss function to use for training. Can either be a loss class instance, a dictionary mapping
            dataset names to loss class instances, a function that returns a loss class instance given a model,
            or a dictionary mapping dataset names to functions that return a loss class instance given a model.
            In practice, the latter two are primarily used for hyper-parameter optimization. Will default to
            :class:`~sentence_transformers.sparse_encoder.losses.SpladeLoss` with :class:`~sentence_transformers.sparse_encoder.losses.SparseMultipleNegativesRankingLoss` if no ``loss`` is provided.
        evaluator (Union[:class:`~sentence_transformers.base.evaluation.BaseEvaluator`,            List[:class:`~sentence_transformers.base.evaluation.BaseEvaluator`]], *optional*):
            The evaluator instance for useful evaluation metrics during training. You can use an ``evaluator`` with
            or without an ``eval_dataset``, and vice versa. Generally, the metrics that an ``evaluator`` returns
            are more useful than the loss value returned from the ``eval_dataset``. A list of evaluators will be
            wrapped in a :class:`~sentence_transformers.base.evaluation.SequentialEvaluator` to run them sequentially.
        callbacks (List of [:class:`transformers.TrainerCallback`], *optional*):
            A list of callbacks to customize the training loop. Will add those to the list of default callbacks
            detailed in [here](callback).

            If you want to remove one of the default callbacks used, use the [`Trainer.remove_callback`] method.
        optimizers (`Tuple[:class:`torch.optim.Optimizer`, :class:`torch.optim.lr_scheduler.LambdaLR`]`, *optional*, defaults to `(None, None)`):
            A tuple containing the optimizer and the scheduler to use. Will default to an instance of :class:`torch.optim.AdamW`
            on your model and a scheduler given by :func:`transformers.get_linear_schedule_with_warmup` controlled by `args`.

    Important attributes:

        - **model** -- Always points to the core model. If using a transformers model, it will be a [`PreTrainedModel`]
          subclass.
        - **model_wrapped** -- Always points to the most external model in case one or more other modules wrap the
          original model. This is the model that should be used for the forward pass. For example, under `DeepSpeed`,
          the inner model is wrapped in `DeepSpeed` and then again in `torch.nn.DistributedDataParallel`. If the inner
          model hasn't been wrapped, then `self.model_wrapped` is the same as `self.model`.
        - **is_model_parallel** -- Whether or not a model has been switched to a model parallel mode (different from
          data parallelism, this means some of the model layers are split on different GPUs).
        - **place_model_on_device** -- Whether or not to automatically place the model on the device - it will be set
          to `False` if model parallel or deepspeed is used, or if the default
          `TrainingArguments.place_model_on_device` is overridden to return `False` .
        - **is_in_train** -- Whether or not a model is currently running `train` (e.g. when `evaluate` is called while
          in `train`)
    processing_class)	tokenizerc                L    t         |   |||||||||	|
||||       |  |  |  y )N)modelargstrain_dataseteval_datasetloss	evaluatordata_collatorr   
model_initcompute_metrics	callbacks
optimizersoptimizer_cls_and_kwargspreprocess_logits_for_metrics)super__init__)selfr!   r"   r#   r$   r%   r&   r'   r   r(   r)   r*   r+   r,   r-   	__class__s                  }/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/sentence_transformers/sparse_encoder/trainer.pyr/   zSparseEncoderTrainer.__init__n   sL    4 	'%'-!+!%=*G 	 	
  	    c                ^    t         j                  d       t        |t        |      dd      S )NzNo `loss` passed, using `SpladeLoss` with `SparseMultipleNegativesRankingLoss` as the default. Note: `query_regularizer_weight` and `document_regularizer_weight` are sensitive parameters and should be tuned for your task.)r!   g-C6
?giUMu>)r!   r%   query_regularizer_weightdocument_regularizer_weight)loggerinfor   r   )r0   r!   s     r2   get_default_lossz%SparseEncoderTrainer.get_default_loss   s3    1	

 3%@%)(,	
 	
r3   c                   t         |   ||      }|t        |t              nd}d }t	        | j
                  j                        D ]  \  }}t        |t              s|} n |rv||dkD  ro|&| j
                  j                  j                  |      }n!t        j                  d       t        |      }| j
                  j                  j                  d|       |S )NF   zSpladeLoss detected without SpladeRegularizerWeightSchedulerCallback. Adding default SpladeRegularizerWeightSchedulerCallback to gradually increase weight values from 0 to their maximum.)r%   )r.   prepare_loss
isinstancer   	enumeratecallback_handlerr*   r   popr7   warninginsert)	r0   r%   r!   is_splade_losssplade_scheduler_callback_indexidxcallbacksplade_callbackr1   s	           r2   r<   z!SparseEncoderTrainer.prepare_loss   s    
 w#D%09=9ID*5u*.'&t'<'<'F'FG 	MC($LM25/	 >FJilmJm.:"&"7"7"A"A"E"EFe"f K #KPT"U!!++221oFr3   )NNNNNNNNNNN)NNNN)r!   zSparseEncoder | Noner"   z%SparseEncoderTrainingArguments | Noner#   CDataset | DatasetDict | IterableDataset | dict[str, Dataset] | Noner$   rH   r%   znn.Module | dict[str, nn.Module] | Callable[[SparseEncoder], torch.nn.Module] | dict[str, Callable[[SparseEncoder], torch.nn.Module]] | Noner&   z*BaseEvaluator | list[BaseEvaluator] | Noner'   z SparseEncoderDataCollator | Noner   z]PreTrainedTokenizerBase | BaseImageProcessor | FeatureExtractionMixin | ProcessorMixin | Noner(   z"Callable[[], SparseEncoder] | Noner)   z'Callable[[EvalPrediction], dict] | Noner*   zlist[TrainerCallback] | Noner+   z?tuple[torch.optim.Optimizer, torch.optim.lr_scheduler.LambdaLR]r,   z9tuple[type[torch.optim.Optimizer], dict[str, Any]] | Noner-   z;Callable[[torch.Tensor, torch.Tensor], torch.Tensor] | NonereturnNone)r!   r   rI   torch.nn.Module)r%   z<Callable[[SparseEncoder], torch.nn.Module] | torch.nn.Moduler!   r   rI   rK   )__name__
__module____qualname____doc__r   model_classr   model_card_data_classr   model_card_callback_classr   data_collator_classr   training_args_classr   r/   r9   r<   __classcell__)r1   s   @r2   r   r   #   s7   BH  K6 >38!34 '+6:]a\`
 @D:>
 9=CG26Vb^bei/+6#+6 4+6 [	+6
 Z+6+6 >+6 8+6+6$ 7%+6& A'+6( 0)+6* T++6, #\-+6. (c/+60 
1+6 5+6Z
J  
	 r3   r   )/
__future__r   loggingcollections.abcr   typingr   torchr   transformersr   r   r	   r
   r   #transformers.image_processing_utilsr   %sentence_transformers.base.evaluationr   "sentence_transformers.base.trainerr   ?sentence_transformers.sparse_encoder.callbacks.splade_callbacksr   2sentence_transformers.sparse_encoder.data_collatorr   +sentence_transformers.sparse_encoder.lossesr   r   *sentence_transformers.sparse_encoder.modelr   /sentence_transformers.sparse_encoder.model_cardr   r   2sentence_transformers.sparse_encoder.training_argsr   sentence_transformers.utilr   %sentence_transformers.util.decoratorsr   datasetsr   r   r   	getLoggerrL   r7   r    r3   r2   <module>rj      sq    "  $     C ? : t X f D v ] < C>>			8	$c; cr3   