
    rhQ                        d dl 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	 d dlZd dlmZ d dlmZ d dlmZ d d	lmZmZ  G d
 deej.                  j0                        Zy# e
$ r	 d dlm	Z	 Y Lw xY w)    )annotationsN)ABCabstractmethod)Any)Self)	load_file)
load_model)
save_model)load_dir_pathload_file_pathc                      e Zd ZU dZdZded<   	 g Zded<   	 dZded	<   	  e       Z	d
ed<   	  fdZ
edd       ZddZe	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 d d       Ze	 	 	 	 	 	 d!	 	 	 	 	 	 	 	 	 	 	 	 	 d"d       Zeddd#d       Zd$d%dZd&d#dZ xZS )'Modulea	  
    Base class for all modules in the Sentence Transformers library.

    This class provides a common interface for all modules, including methods for loading and saving the module's
    configuration and weights. It also provides a method for performing the forward pass of the module.

    Two abstract methods are defined in this class, which must be implemented by subclasses:

    - :meth:`sentence_transformers.models.Module.forward`: The forward pass of the module.
    - :meth:`sentence_transformers.models.Module.save`: Save the module to disk.

    Optionally, you may also have to override:

    - :meth:`sentence_transformers.models.Module.load`: Load the module from disk.

    To assist with loading and saving the module, several utility methods are provided:

    - :meth:`sentence_transformers.models.Module.load_config`: Load the module's configuration from a JSON file.
    - :meth:`sentence_transformers.models.Module.load_file_path`: Load a file from the module's directory, regardless of whether the module is saved locally or on Hugging Face.
    - :meth:`sentence_transformers.models.Module.load_dir_path`: Load a directory from the module's directory, regardless of whether the module is saved locally or on Hugging Face.
    - :meth:`sentence_transformers.models.Module.load_torch_weights`: Load the PyTorch weights of the module, regardless of whether the module is saved locally or on Hugging Face.
    - :meth:`sentence_transformers.models.Module.save_config`: Save the module's configuration to a JSON file.
    - :meth:`sentence_transformers.models.Module.save_torch_weights`: Save the PyTorch weights of the module.
    - :meth:`sentence_transformers.models.Module.get_config_dict`: Get the module's configuration as a dictionary.

    And several class variables are defined to assist with loading and saving the module:

    - :attr:`sentence_transformers.models.Module.config_file_name`: The name of the configuration file used to save the module's configuration.
    - :attr:`sentence_transformers.models.Module.config_keys`: A list of keys used to save the module's configuration.
    - :attr:`sentence_transformers.models.Module.save_in_root`: Whether to save the module's configuration in the root directory of the model or in a subdirectory named after the module.
    zconfig.jsonstrconfig_file_namez	list[str]config_keysFboolsave_in_rootzset[str]forward_kwargsc                $    t        |   |i | y N)super__init__)selfargskwargs	__class__s      v/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/sentence_transformers/models/Module.pyr   zModule.__init__J   s    $)&)    c                     y)a  
        Forward pass of the module. This method should be overridden by subclasses to implement the specific behavior of the module.

        The forward method takes a dictionary of features as input and returns a dictionary of features as output.
        The keys in the ``features`` dictionary depend on the position of the module in the model pipeline, as
        the ``features`` dictionary is passed from one module to the next. Common keys in the ``features`` dictionary
        are:

            - ``input_ids``: The input IDs of the tokens in the input text.
            - ``attention_mask``: The attention mask for the input tokens.
            - ``token_type_ids``: The token type IDs for the input tokens.
            - ``token_embeddings``: The token embeddings for the input tokens.
            - ``sentence_embedding``: The sentence embedding for the input text, i.e. pooled token embeddings.

        Optionally, the ``forward`` method can accept additional keyword arguments (``**kwargs``) that can be used to
        pass additional information from ``model.encode`` to this module.

        Args:
            features (dict[str, torch.Tensor | Any]): A dictionary of features to be processed by the module.
            **kwargs: Additional keyword arguments that can be used to pass additional information from ``model.encode``.

        Returns:
            dict[str, torch.Tensor | Any]: A dictionary of features after processing by the module.
        N )r   featuresr   s      r   forwardzModule.forwardM       r   c                V    | j                   D ci c]  }|t        | |       c}S c c}w )a  
        Returns a dictionary of the configuration parameters of the module.

        These parameters are used to save the module's configuration when saving the model to disk, and again used
        to initialize the module when loading it from a pre-trained model. The keys used in the dictionary are defined in the
        ``config_keys`` class variable.

        Returns:
            dict[str, Any]: A dictionary of the configuration parameters of the module.
        )r   getattr)r   keys     r   get_config_dictzModule.get_config_dicth   s*     483C3CDCWT3''DDDs   &c                @    | j                  ||||||      } | di |S )a  
        Load this module from a model checkpoint. The checkpoint can be either a local directory or a model id on Hugging Face.

        Args:
            model_name_or_path (str): The path to the model directory or the name of the model on Hugging Face.
            subfolder (str, optional): The subfolder within the model directory to load from, e.g. ``"1_Pooling"``.
                Defaults to ``""``.
            token (bool | str | None, optional): The token to use for authentication when loading from Hugging Face.
                If None, tries to use a token saved using ``huggingface-cli login`` or the ``HF_TOKEN`` environment variable.
                Defaults to None.
            cache_folder (str | None, optional): The folder to use for caching the model files.
                If None, uses the default cache folder for Hugging Face, ``~/.cache/huggingface``. Defaults to None.
            revision (str | None, optional): The revision of the model to load.
                If None, uses the latest revision. Defaults to None.
            local_files_only (bool, optional): Whether to only load local files. Defaults to False.
            **kwargs: Additional module-specific arguments used in an overridden ``load`` method, such as ``trust_remote_code``,
                ``model_kwargs``, ``tokenizer_kwargs``, ``config_kwargs``, ``backend``, etc.

        Returns:
            Self: The loaded module.
        	subfoldertokencache_folderrevisionlocal_files_onlyr    )load_config)	clsmodel_name_or_pathr*   r+   r,   r-   r.   r   configs	            r   loadzModule.loadu   s8    @ %- ! 
 }V}r   c           	         t        ||xs | j                  |||||      }|i S t        |d      5 }	t        j                  |	      }
ddd       |
S # 1 sw Y   
S xY w)al  
        Load the configuration of the module from a model checkpoint. The checkpoint can be either a local directory or a model id on Hugging Face.
        The configuration is loaded from a JSON file, which contains the parameters used to initialize the module.

        Args:
            model_name_or_path (str): The path to the model directory or the name of the model on Hugging Face.
            subfolder (str, optional): The subfolder within the model directory to load from, e.g. ``"1_Pooling"``.
                Defaults to ``""``.
            config_filename (str | None, optional): The name of the configuration file to load.
                If None, uses the default configuration file name defined in the ``config_file_name`` class variable.
                Defaults to None.
            token (bool | str | None, optional): The token to use for authentication when loading from Hugging Face.
                If None, tries to use a token saved using ``huggingface-cli login`` or the ``HF_TOKEN`` environment variable.
                Defaults to None.
            cache_folder (str | None, optional): The folder to use for caching the model files.
                If None, uses the default cache folder for Hugging Face, ``~/.cache/huggingface``. Defaults to None.
            revision (str | None, optional): The revision of the model to load.
                If None, uses the latest revision. Defaults to None.
            local_files_only (bool, optional): Whether to only load local files. Defaults to False.

        Returns:
            dict[str, Any]: A dictionary of the configuration parameters of the module.
        r1   filenamer*   r+   r,   r-   r.   Nutf-8encoding)r   r   openjsonr3   )r0   r1   r*   config_filenamer+   r,   r-   r.   config_pathfr2   s              r   r/   zModule.load_config   so    D %1$<(<(<%-
 I+0 	"AYYq\F	"	"s   AAc           	     &    t        | ||||||      S )a  
        A utility function to load a file from a model checkpoint. The checkpoint can be either a local directory or a model id on Hugging Face.
        The file is loaded from the specified subfolder within the model directory.

        Args:
            model_name_or_path (str): The path to the model directory or the name of the model on Hugging Face.
            filename (str): The name of the file to load.
            subfolder (str, optional): The subfolder within the model directory to load from, e.g. ``"1_Pooling"``.
                Defaults to ``""``.
            token (bool | str | None, optional): The token to use for authentication when loading from Hugging Face.
                If None, tries to use a token saved using ``huggingface-cli login`` or the ``HF_TOKEN`` environment variable.
                Defaults to None.
            cache_folder (str | None, optional): The folder to use for caching the model files.
                If None, uses the default cache folder for Hugging Face, ``~/.cache/huggingface``. Defaults to None.
            revision (str | None, optional): The revision of the model to load.
                If None, uses the latest revision. Defaults to None.
            local_files_only (bool, optional): Whether to only load local files. Defaults to False.

        Returns:
            str | None: The path to the loaded file, or None if the file was not found.
        r5   )r   r5   s          r   r   zModule.load_file_path   s&    > 1%-
 	
r   c                $    t        | |||||      S )a  
        A utility function to load a directory from a model checkpoint. The checkpoint can be either a local directory or a model id on Hugging Face.

        Args:
            model_name_or_path (str): The path to the model directory or the name of the model on Hugging Face.
            subfolder (str, optional): The subfolder within the model directory to load from, e.g. ``"1_Pooling"``.
                Defaults to ``""``.
            token (bool | str | None, optional): The token to use for authentication when loading from Hugging Face.
                If None, tries to use a token saved using ``huggingface-cli login`` or the ``HF_TOKEN`` environment variable.
                Defaults to None.
            cache_folder (str | None, optional): The folder to use for caching the model files.
                If None, uses the default cache folder for Hugging Face, ``~/.cache/huggingface``. Defaults to None.
            revision (str | None, optional): The revision of the model to load.
                If None, uses the latest revision. Defaults to None.
            local_files_only (bool, optional): Whether to only load local files. Defaults to False.

        Returns:
            str: The path to the loaded directory.
        r1   r*   r+   r,   r-   r.   )r   rA   s         r   r   zModule.load_dir_path   s#    8 1%-
 	
r   c                J   |||||d} | j                   |fddi|}	|	|t        ||	       |S t        |	      }
|
S  | j                   |fddi|}|t        d| d      t	        j
                  |t	        j                  d      d	      }
||j                  |
       |S |
S )
a  
        A utility function to load the PyTorch weights of a model from a checkpoint. The checkpoint can be either a
        local directory or a model id on Hugging Face. The weights are loaded from either a ``model.safetensors``
        file or a ``pytorch_model.bin`` file, depending on which one is available. This method either loads the
        weights into the model or returns the weights as a state dictionary.

        Args:
            model_name_or_path (str): The path to the model directory or the name of the model on Hugging Face.
            subfolder (str, optional): The subfolder within the model directory to load from, e.g. ``"2_Dense"``.
                Defaults to ``""``.
            token (bool | str | None, optional): The token to use for authentication when loading from Hugging Face.
                If None, tries to use a token saved using ``huggingface-cli login`` or the ``HF_TOKEN`` environment variable.
                Defaults to None.
            cache_folder (str | None, optional): The folder to use for caching the model files.
                If None, uses the default cache folder for Hugging Face, ``~/.cache/huggingface``. Defaults to None.
            revision (str | None, optional): The revision of the model to load.
                If None, uses the latest revision. Defaults to None.
            local_files_only (bool, optional): Whether to only load local files. Defaults to False.
            model (Self | None, optional): The model to load the weights into. If None, returns the weights as a state
                dictionary. Defaults to None.

        Raises:
            ValueError: If neither a ``model.safetensors`` file nor a ``pytorch_model.bin`` file is found in the model
                checkpoint in the ``subfolder``.

        Returns:
            Self | dict[str, torch.Tensor]: The model with the loaded weights or the weights as a state dictionary,
                depending on the value of the ``model`` argument.
        r)   r6   model.safetensorspytorch_model.binz=Could not find 'model.safetensors' or 'pytorch_model.bin' in .cpuT)map_locationweights_only)r   load_safetensors_modelload_safetensors_file
ValueErrortorchr3   deviceload_state_dict)r0   r1   r*   r+   r,   r-   r.   model
hub_kwargssafetensors_pathweightspytorch_model_paths               r   load_torch_weightszModule.load_torch_weights  s    R #(  0

 .3--.@mK^mblm' &u.>?/0@A 0S//0BoM`odno%\]o\ppqrss**/ell5>Q`de!!'*Lr   T)safe_serializationc                    y)ao  
        Save the module to disk. This method should be overridden by subclasses to implement the specific behavior of the module.

        Args:
            output_path (str): The path to the directory where the module should be saved.
            *args: Additional arguments that can be used to pass additional information to the save method.
            safe_serialization (bool, optional): Whether to use the safetensors format for saving the model weights.
                Defaults to True.
            **kwargs: Additional keyword arguments that can be used to pass additional information to the save method.
        Nr    )r   output_pathrU   r   r   s        r   savezModule.savee  r#   r   c                    | j                         }t        j                  j                  ||xs | j                        }t        |dd      5 }t        j                  ||d       ddd       y# 1 sw Y   yxY w)a  
        Save the configuration of the module to a JSON file.

        Args:
            output_path (str): The path to the directory where the configuration file should be saved.
            filename (str | None, optional): The name of the configuration file. If None, uses the default configuration
                file name defined in the ``config_file_name`` class variable. Defaults to None.

        Returns:
            None
        wr7   r8      )indentN)r'   ospathjoinr   r:   r;   dump)r   rW   r6   r2   config_output_pathr>   s         r   save_configzModule.save_configr  sf     %%'WW\\+x7X4CXCXY$cG< 	+IIfa*	+ 	+ 	+s   A//A8c                    |r+t        | t        j                  j                  |d             yt	        j
                  | j                         t        j                  j                  |d             y)af  
        Save the PyTorch weights of the module to disk.

        Args:
            output_path (str): The path to the directory where the weights should be saved.
            safe_serialization (bool, optional): Whether to use the safetensors format for saving the model weights.
                Defaults to True.

        Returns:
            None
        rC   rD   N)save_safetensors_modelr]   r^   r_   rL   rX   
state_dict)r   rW   rU   s      r   save_torch_weightszModule.save_torch_weights  sF     "4kCV)WXJJt("'',,{DW*XYr   )r!   dict[str, torch.Tensor | Any]returnrg   )rh   dict[str, Any]) NNNF)r1   r   r*   r   r+   bool | str | Noner,   
str | Noner-   rl   r.   r   rh   r   )rj   NNNNF)r1   r   r*   r   r<   rl   r+   rk   r,   rl   r-   rl   r.   r   rh   ri   )r1   r   r6   r   r*   r   r+   rk   r,   rl   r-   rl   r.   r   rh   rl   )r1   r   r*   r   r+   rk   r,   rl   r-   rl   r.   r   rh   r   )rj   NNNFN)r1   r   r*   r   r+   rk   r,   rl   r-   rl   r.   r   rO   zSelf | None)rW   r   rU   r   rh   Noner   )rW   r   r6   rl   rh   rm   )T)__name__
__module____qualname____doc__r   __annotations__r   r   setr   r   r   r"   r'   classmethodr3   r/   staticmethodr   r   rT   rX   rb   rf   __classcell__)r   s   @r   r   r      s   @ *c)  K L$  #uNH$
*  4E  #'#'#!&'' ' !	'
 !' ' ' 
' 'R  &*#'#'#!&// / $	/
 !/ !/ / / 
/ /b  #'#'#!&&
&
&
 &
 !	&

 !&
 &
 &
 
&
 &
P  #'#'#!&"
"
"
 !"
 !	"

 "
 "
 
"
 "
H  #'#'#!&!CC C !	C
 !C C C C CJ GK 
 
+"Z Zr   r   )
__future__r   r;   r]   abcr   r   typingr   r   ImportErrortyping_extensionsrL   safetensors.torchr   rJ   r	   rI   r
   rd   sentence_transformers.utilr   r   nnr   r    r   r   <module>r      s[    "  	 # '  @ B B D}ZS%((// }Z  '&'s   A# #A10A1