
    Nj                        d dl mZ d dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZ  ej                  e      Z G d de	      Zy)	    )annotationsN)	Tokenizer)BaseFinetuneable)StaticLightningModule)_DEFAULT_RANDOM_SEEDc                       e Zd ZdZdZdddddddd	dd
		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZddddddddddef	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ xZS )StaticModelForSimilarityval_lossmin   i      r   NFT)	n_layers
hidden_dimout_dimpad_idtoken_mappingweightsfreeze	normalizefreeze_weightsc               :    t         |   |||||||	|||
|       y)z'Initialize a standard similarity model.)vectorsr   r   	tokenizerr   r   r   r   r   r   r   N)super__init__)selfr   r   r   r   r   r   r   r   r   r   r   	__class__s               k/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/model2vec/train/similarity.pyr   z!StaticModelForSimilarity.__init__   s8      	'!) 	 	
    gMbP?   g?autoc                j   t        j                  |       t        j                  d       | j	                  |||
||      \  }}| j                  |t        |            }|j                  j                  d   | _	        | j                          t        | |      }| j                  ||||||||	|	       | S )a  Fit a model.

        This function creates a Lightning Trainer object and fits the model to the data.
        We use early stopping. After training, the weights of the best model are loaded back into the model.

        This function seeds everything with a seed of 42, so the results are reproducible.
        It also splits the data into a train and validation set, again with a random seed.

        If `X_val` and `y_val` are not provided, the function will automatically
        split the training data into a train and validation set using `test_size`.

        :param X: The texts to train on.
        :param y: The vectors to train on.
        :param learning_rate: The learning rate.
        :param batch_size: The batch size. If None, a good batch size is chosen automatically.
        :param min_epochs: The minimum number of epochs to train for.
        :param max_epochs: The maximum number of epochs to train for.
            If this is -1, the model trains until early stopping is triggered.
        :param early_stopping_patience: The patience for early stopping.
            If this is None, early stopping is disabled.
        :param test_size: The test size for the train-test split.
        :param device: The device to train on. If this is "auto", the device is chosen automatically.
        :param X_val: The texts to be used for validation.
        :param y_val: The vectors to be used for validation.
        :param validation_steps: The number of steps to run validation for. If None, validation steps are estimated from the data.
        :param random_seed: The random seed to use. Defaults to 42.
        :return: The fitted model.
        zRe-initializing model.r   )learning_rate)	moduletrain_datasetval_dataset
batch_sizeearly_stopping_patience
min_epochs
max_epochsdevicevalidation_steps)plseed_everythingloggerinfo_create_datasets_determine_batch_sizelentargetsshaper   _initializer   _train)r   Xyr$   r(   r*   r+   r)   	test_sizer,   X_valy_valr-   random_seedr&   r'   cs                    r   fitzStaticModelForSimilarity.fit2   s    X 	;',-%)%:%:1ay%Y"{//
C<NO
$,,2215!$mD'#!$;!!- 	 
	
 r   )r   torch.Tensorr   r   r   intr   rB   r   rB   r   rB   r   zlist[int] | Noner   torch.Tensor | Noner   boolr   rD   r   rD   returnNone)r9   z	list[str]r:   rA   r$   floatr(   
int | Noner*   rH   r+   rH   r)   rH   r;   rG   r,   strr<   zlist[str] | Noner=   rC   r-   rH   r>   rB   rE   r	   )	__name__
__module____qualname__
val_metricearly_stopping_directionr   r   r@   __classcell__)r   s   @r   r	   r	      sj   J$ *.'+$
 
 	

 
 
 
 
 (
 %
 
 
 
 

D  $!%!%!#./"&%)'+/CC C 	C
 C C C ",C C C  C #C %C C 
"Cr   r	   )
__future__r   logging	lightningr.   torch
tokenizersr   model2vec.train.baser   !model2vec.train.lightning_modulesr   model2vec.train.utilsr   	getLoggerrJ   r0   r	    r   r   <module>rZ      s>    "      1 C 6			8	$e/ er   