
    wfhe                        d dl mZ d dlZd dlmZ d dlmZmZmZ d dl	m
Z
 d dlmZ  ej                  e      Z G d de      Z G d	 d
e      Zy)    )annotationsN)Enum)TrainerCallbackTrainerControlTrainerState)
SpladeLoss)SparseEncoderTrainingArgumentsc                      e Zd ZdZdZdZy)SchedulerTypez7Types of schedulers for weight parameters in SpladeLosslinear	quadraticN)__name__
__module____qualname____doc__LINEAR	QUADRATIC     }/home/chris/cleankitchens-env/lib/python3.12/site-packages/sentence_transformers/sparse_encoder/callbacks/splade_callbacks.pyr   r      s    AFIr   r   c                  |     e Zd Zej                  df	 	 	 	 	 d fdZ	 	 	 	 	 	 ddZd	dZ	 	 	 	 	 	 ddZd
dZ	 xZ
S )(SpladeRegularizerWeightSchedulerCallbackUUUUUU?c                   t         |           t        |t              r	 t	        |j                               }|| _        d|cxk  rdk  sn t        j                  d| d       d}t        |t              s7t        j                  dt        |      j                   d	       t        d
      || _        | j                  j                   | _        | j                  j$                  | _        || _        | j&                  dnd| _        d| _        d| _        d| _        y# t        $ rG t        j                  d| dt        j                  j                          t        j                  }Y 4w xY w)a7  
        Callback that updates the query_regularizer_weight and document_regularizer_weight parameters of SpladeLoss
        based on a schedule.

        The scheduler gradually increases the weight values from 0 to their max value
        within the specified warmup ratio of the total training steps.

        Args:
            loss (SpladeLoss): SpladeLoss instance to be updated
            scheduler_type (str): Type of scheduler ('linear' or 'quadratic')
            warmup_ratio (float): Ratio of total steps to reach max weight values (default: 1/3)
        zInvalid scheduler_type: z. Using default: r      z,warmup_ratio should be between 0 and 1, got z. Setting to default 1/3.r   zUSpladeRegularizerWeightSchedulerCallback is only compatible with SpladeLoss, but got z&. This callback won't have any effect.z&loss must be an instance of SpladeLossNg        )super__init__
isinstancestrr   lower
ValueErrorloggerwarningr   valuescheduler_typer   typer   lossdocument_regularizer_weightmax_document_regularizer_weightquery_regularizer_weightmax_query_regularizer_weightwarmup_ratio!_current_query_regularizer_weight$_current_document_regularizer_weighttotal_stepswarmup_steps)selfr'   r%   r,   	__class__s       r   r   z1SpladeRegularizerWeightSchedulerCallback.__init__   sV   $ 	nc*9!.~/C/C/E!F - <$1$NNI,Wpqr L $
+NN:..//UW EFF	/3yy/T/T,,0II,N,N)(8<8Y8Y8eko.471 7  9.~.>>OP]PgPgPmPmOno "/!8!8	9s   D AE/.E/c                   t        |d      r!|j                  dkD  r|j                  | _        nMt        |d      r+t        |d      r|j                  |j                  z  | _        nt
        j                  d       yt        | j                  | j                  z        | _	        | j                  dk  rd| _	        | j                  | j                  _        | j                  | j                  _        y)z6Initialize the scheduler at the beginning of training.	max_stepsr   num_train_epochsnum_update_steps_per_epochzXCannot determine total steps from TrainerState. Weight scheduling may not work properly.Nr   )hasattrr4   r/   r6   r5   r"   r#   intr,   r0   r-   r'   r*   r.   r(   )r1   argsstatecontrolkwargss        r   on_train_beginz7SpladeRegularizerWeightSchedulerCallback.on_train_beginJ   s     5+&5??Q+>$DU./GEC_4`$??%BXBXXDNNuv 0 043D3D DE! !D .2-S-S		*040Y0Y		-r   c                V   | j                   || j                   k\  s||S |t        | j                   d      z  }| j                  t        j                  k(  r||z  S | j                  t        j
                  k(  r||dz  z  S t        j                  d| j                   d       ||dz  z  S )zHCalculate the weight value based on the current step and scheduler type.r      zUnknown scheduler type: z. Using quadratic.)r0   maxr%   r   r   r   r"   r#   )r1   step	max_valueratios       r   _calculate_weight_valuez@SpladeRegularizerWeightSchedulerCallback._calculate_weight_valuec   s    $0A0A(AYEVs4,,a00-"6"66u$$  M$;$;;q))NN5d6I6I5JJ\]^q))r   c                \   | j                   | j                  y|j                  }| j                  || j                        }| j                  || j
                        }|| j                  k7  s|| j                  k7  r1|| j                  _	        || j                  _
        || _        || _        yy)z-Update weight values at the end of each step.N)r/   r0   global_steprD   r+   r)   r-   r.   r'   r*   r(   )r1   r9   r:   r;   r<   rA   new_query_regularizer_weightnew_document_regularizer_weights           r   on_step_beginz6SpladeRegularizerWeightSchedulerCallback.on_step_beginr   s     #t'8'8'@    (,'C'CD$JkJk'l$*.*F*FtTMqMq*r' )D,R,RR.$2[2[[1MDII.4SDII1 6RD28WD5 \r   c                Z    | j                   |d<   | j                  | j                  |d<   yy)zLog the current weight values.r(   Nr*   )r.   r-   )r1   r9   r:   r;   modellogsr<   s          r   on_logz/SpladeRegularizerWeightSchedulerCallback.on_log   s5    .2.W.W*+11=/3/U/UD+, >r   )r'   r   r%   zstr | SchedulerTyper,   float)r9   r	   r:   r   r;   r   )rA   r8   rB   rN   returnrN   )NN)r   r   r   r   r   r   r=   rD   rI   rM   __classcell__)r2   s   @r   r   r      s     /<.E.E#	2!2! ,2! 	2!hZ,Z Z  	Z2*X,X X  	X<Vr   r   )
__future__r   loggingenumr   transformers.trainer_callbackr   r   r   6sentence_transformers.sparse_encoder.losses.SpladeLossr   2sentence_transformers.sparse_encoder.training_argsr	   	getLoggerr   r"   r   r   r   r   r   <module>rX      sH    "   W W M ]			8	$D V Vr   