
    #i"                      d dl mZ d dlZd dlZd dlZd dlZd dlZd dl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mZ d dlmZmZ d dlZd dlZd dlm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#m$Z$m%Z% d dl&m'Z'm(Z( d dl)m*Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1m2Z2 d dl3m4Z4m5Z5m6Z6 d dl7m8Z8m9Z9 d dl:m;Z;m<Z<m=Z= d dl>m?Z? d dl@mAZAmBZBmCZCmDZDmEZE d dlFmGZG  e+j                  eI      ZJ G d de"j                  e?e      ZLy)    )annotationsN)ABCabstractmethod)OrderedDict)Queue)Path)AnyLiteral)CardDataHfApi)version)Tensornn)PreTrainedModelis_datasets_availableis_torch_npu_available)get_class_from_dynamic_moduleget_relative_import_files)logging)__version__)BaseEvaluator)format_modalityinfer_batch_modality)Modality	PairInputSingleInput)BaseModelCardDatagenerate_model_card)ModuleRouterTransformer)PeftAdapterMixin)get_device_nameimport_from_stringload_dir_pathload_file_pathsave_to_hub_args_decorator)ORIGINAL_TRANSFORMER_MODELSc                  F    e Zd ZU dZeZdZded<   i Zded<   dZ	ded	<   	 d?dddddd
dd
ddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d@ fdZ
dAdZdBdZdCdZedDd       ZdEdZdFdZdGdZdHdZdHdZdIdZedJd       ZedKd       ZdLdZ	 d?	 	 	 	 	 dMdZdNdZdOdZ	 	 	 	 dP	 	 	 	 	 	 	 	 	 	 	 dQdZdRdZ	 	 	 	 dP	 	 	 	 	 	 	 	 	 	 	 dQd ZdSd!Z 	 dT	 	 	 	 	 	 	 dUd"Z!e"	 	 	 	 	 	 	 	 	 dV	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dWd#       Z#	 	 	 	 	 	 	 	 	 	 dX	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dYd$Z$	 	 	 	 	 	 dZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d[d%Z%e&	 	 	 	 	 	 dZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d[d&       Z'	 	 	 	 	 	 dZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d[d'Z(d\d(Z)	 	 	 	 	 	 	 d]	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d^d)Z*	 	 d_	 	 	 	 	 	 	 	 	 	 	 d`d*Z+	 	 	 	 	 	 	 	 	 	 	 	 dad+Z,d?dbd,Z-d?dcd-Z.eddd.       Z/	 d?	 	 	 ded/Z0edfd0       Z1d1 Z2e	 	 	 	 	 	 	 	 	 	 dgd2       Z3edhd3       Z4e4jj                  dAd4       Z4edhd5       Z6edGd6       Z7e7jj                  dAd7       Z7edid8       Z8eddd9       Z9e9jj                  d?djd:       Z9edkd;       Z:edFd<       Z;edFd=       Z<dld>Z= xZ>S )m	BaseModela  
    Base class for SentenceTransformer, SparseEncoder, and CrossEncoder models.

    This class provides common functionality for:

    - Model loading (from Hub, local paths, or creating new models)
    - Model saving (to disk and Hub)
    - Device management
    - Module architecture (sequential composition)
    - Configuration management
    - Tokenizer/processor access

    All models inherit from nn.Sequential and are composed of a sequence of modules
    that are called sequentially in the forward pass.
    N
str | None default_huggingface_organizationzdict[str, str | None]_default_promptssentence_transformers_model_idstr _model_card_model_id_placeholderFtorch)modulesdevicepromptsdefault_prompt_namecache_foldertrust_remote_coderevisionlocal_files_onlytokenmodel_kwargsprocessor_kwargsconfig_kwargsmodel_card_databackendc                  t        | j                        }|r|j                  |       || _        || _        || _        |xs | j                  |	      | _        d| _        i | _	        d| _
        | j                  j                  | _        || _        |t        j                   d      }|"t#               }t$        j'                  d|        |dk(  r,t(        j*                  j-                  d      ddlm}  |        |rt        j2                  j5                  |      sqt        j6                  d	k(  rd	|v s|j9                  d
      dkD  rt;        d| d      | j<                  +d
|vr'|j?                         t@        vr| j<                   d
| }|r"| jC                  ||
||||	|||	      \  }| _        |BtE        |tF              s2tG        tI        |      D cg c]  \  }}tK        |      |f c}}      }|0tM        |      dk(  r"tO        dtQ        |       j                   d      tR        |   |       tW        | d   jY                         d      }|>|jZ                  }t]        | j_                               dd D ]  }|ja                  |        | ja                  |       d| _1        | je                          | j                  jg                  |        yc c}}w )a/  
        Initialize a BaseModel instance.

        Args:
            model_name_or_path (str, optional): If a filepath on disk, loads the model from that path. Otherwise,
                tries to download a pre-trained model. If that fails, tries to construct a model from the Hugging
                Face Hub with that name. Defaults to None.
            modules (list[nn.Module], optional): A list of torch modules that are called sequentially. Can be used
                to create custom models from scratch. Defaults to None.
            device (str, optional): Device (like ``"cuda"``, ``"cpu"``, ``"mps"``, ``"npu"``) that should be used
                for computation. If None, checks if a GPU can be used. Defaults to None.
            prompts (dict[str, str], optional): A dictionary with prompts for the model. The key is the prompt
                name, the value is the prompt text. The prompt text will be prepended before any text during inference.
                For example: ``{"query": "query: ", "passage": "passage: "}``. If a model has saved prompts, you
                can override them by passing your own, or pass ``{"query": "", "document": ""}`` to disable them.
                Defaults to None.
            default_prompt_name (str, optional): The name of the prompt that should be used by default. If not
                set, no prompt will be applied. Defaults to None.
            cache_folder (str, optional): Path to store models. Can also be set by the
                ``SENTENCE_TRANSFORMERS_HOME`` environment variable. Defaults to None.
            trust_remote_code (bool, optional): Whether to allow for custom models defined on the Hub in their
                own modeling files. Only set to ``True`` for repositories you trust and in which you have read the
                code, as it will execute code present on the Hub on your local machine. Defaults to False.
            revision (str, optional): The specific model version to use. It can be a branch name, a tag name, or
                a commit id, for a stored model on Hugging Face. Defaults to None.
            local_files_only (bool, optional): Whether to only look at local files (i.e., do not try to download
                the model). Defaults to False.
            token (bool or str, optional): Hugging Face authentication token to download private models.
                Defaults to None.
            model_kwargs (dict[str, Any], optional): Keyword arguments passed to the underlying Hugging Face
                Transformers model via ``AutoModel.from_pretrained``. Particularly useful options include:

                - ``torch_dtype``: Override the default ``torch.dtype`` and load the model under a specific
                  dtype. Can be ``torch.float16``, ``torch.bfloat16``, ``torch.float32``, or ``"auto"`` to
                  use the dtype from the model's ``config.json``.
                - ``attn_implementation``: The attention implementation to use. For example ``"eager"``,
                  ``"sdpa"``, or ``"flash_attention_2"``. If you ``pip install kernels``, then
                  ``"flash_attention_2"`` should work without having to install ``flash_attn``. It is
                  frequently the fastest option. Defaults to ``"sdpa"`` when available (torch>=2.1.1).
                - ``device_map``: Device map for model parallelism, e.g. ``"auto"``.
                - ``provider``: For ``backend="onnx"``, the ONNX execution provider
                  (e.g. ``"CUDAExecutionProvider"``).
                - ``file_name``: For ``backend="onnx"`` or ``"openvino"``, the filename to load
                  (e.g. for optimized or quantized models).
                - ``export``: For ``backend="onnx"`` or ``"openvino"``, whether to export the model to the
                  backend format. Also set automatically if the exported file doesn't exist.

                See the `PreTrainedModel.from_pretrained
                <https://huggingface.co/docs/transformers/en/main_classes/model#transformers.PreTrainedModel.from_pretrained>`_
                documentation for more details. Defaults to None.
            processor_kwargs (dict[str, Any], optional): Keyword arguments passed to the Hugging Face Transformers
                processor/tokenizer via ``AutoProcessor.from_pretrained``. See the
                `AutoTokenizer.from_pretrained
                <https://huggingface.co/docs/transformers/en/model_doc/auto#transformers.AutoTokenizer.from_pretrained>`_
                documentation for more details. Defaults to None.
            config_kwargs (dict[str, Any], optional): Keyword arguments passed to the Hugging Face Transformers
                config via ``AutoConfig.from_pretrained``. See the `AutoConfig.from_pretrained
                <https://huggingface.co/docs/transformers/en/model_doc/auto#transformers.AutoConfig.from_pretrained>`_
                documentation for more details. Defaults to None.
            model_card_data (CardData, optional): A model card data object that contains information about the
                model. Used to generate a model card when saving the model. If not set, a default model card data
                object is created. Defaults to None.
            backend (str, optional): The backend to use for inference. Can be ``"torch"`` (default), ``"onnx"``,
                or ``"openvino"``. Defaults to ``"torch"``.
        )r9   NSENTENCE_TRANSFORMERS_HOMEzNo device provided, using hpuoptimumr   )adapt_transformers_to_gaudi\/   zPath z
 not foundr:   r6   r8   r7   r9   r;   r<   r=   z$An empty modules list was passed to z@. Please provide at least one module, e.g. a Transformer module.F)4dictr-   updater4   r5   r7   model_card_data_classr>   module_kwargs_model_card_vars_model_card_text	__class____name__
model_typer?   osgetenvr#   loggerinfo	importlibutil	find_spec*optimum.habana.transformers.modeling_utilsrD   pathexistssepcountFileNotFoundErrorr,   lowerr(   _load_modules
isinstancer   	enumerater/   len
ValueErrortypesuper__init__next
parametersdtypelistchildrentois_hpu_graph_enabled_validate_promptsregister_model)selfmodel_name_or_pathr2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   default_promptsrD   idxmodulefirst_paramfirst_dtyperO   s                         q/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/sentence_transformers/base/model.pyrg   zBaseModel.__init__J   s   h t445""7+&#6 !2.o$2L2L^n2L2o! " $..1199%ABL >$&FKK4VH=>U?y~~77	BN^') bggnn5G&H$4+=#=BTBZBZ[^B_bcBc'%0B/C:(NOO 55A11&,,.6QQ(,(M(M'NaPbOc%d"*.*<*<")!"3!1)!1+ += 
+'GT' z';'G!SZI["\+#vCHf#5"\]G3w<1#46tDz7J7J6K LQ Q 
 	!
 47--/6"%++Kt}}/3 '		+&' 	$)! 	  	++D15 #]s   K
c           	     d   | j                   j                         D ]  \  }}|	d| j                   |<    | j                  U| j                  | j                   vr=t        d| j                   dt	        | j                   j                               d      | j                   j                         D cg c]  \  }}|dk7  s| c}}x}r0t        |      }t        j                  d| d|dkD  rd	nd d
|        | j                  r$t        j                  d| j                   d       yyc c}}w )z9Validate prompt configuration and log prompt information.N zDefault prompt name ';' not found in the configured prompts dictionary with keys .zLoaded z promptrG   sz with these keys: zDefault prompt name is set to 'zs'. This prompt will be applied to all inference calls, except if a `prompt` or `prompt_name` parameter is provided.)
r4   itemsr5   rd   rk   keysrc   rT   rU   warning_once)rq   keyvaluekvnon_empty_keysns          rx   ro   zBaseModel._validate_prompts   s;    ,,,,. 	'JC}$&S!	' ##/D4L4LTXT`T`4`'(@(@'A B((,T\\->->-@(A'DAG 
 -1LL,>,>,@LDAqAGaLL>LN#AKK'!G1q5Cb+AASTbScde##1$2J2J1K LE F $ Ms   3D,D,c           
     H   |H|	 | j                   |   }|S | j
                  &| j                   j                  | j
                  d      }|S |t        j                  d       |S # t        $ r4 t        d| dt        | j                   j	                               d      w xY w)a&  Resolve a prompt from a prompt name or the default prompt name.

        Args:
            prompt: An explicit prompt string, or None.
            prompt_name: A key into ``self.prompts``, or None.

        Returns:
            The resolved prompt string, or None if no prompt applies.
        NzPrompt name 'r{   r|   zvProvide either a `prompt`, a `prompt_name`, or neither, but not both. Ignoring the `prompt_name` in favor of `prompt`.)	r4   KeyErrorrd   rk   r   r5   getrT   warning)rq   promptprompt_names      rx   _resolve_promptzBaseModel._resolve_prompt  s     >&!\\+6F  ))5))$*B*BDI  $NNC    $'}4optuy  vB  vB  vG  vG  vI  qJ  pM  MN  O s   A$ $=B!c                    | j                   S )zReturn the backend used for inference, which can be one of "torch", "onnx", or "openvino".

        Returns:
            str: The backend used for inference.
        )r?   rq   s    rx   get_backendzBaseModel.get_backend   s     ||    c                $    t        | d   ddg      S )zmReturn the list of modalities supported by this model, e.g. ``["text"]`` or ``["text", "image", "message"]``.r   
modalitiestextgetattrr   s    rx   r   zBaseModel.modalities(  s     tAwvh77r   c                x    | j                   |v ryt        |t              rdv rt        fd|D              S y)a  Check if the model supports the given modality.

        A modality is supported if:

        1. It is directly listed in :attr:`modalities` (including tuple modalities that
           are explicitly listed), or
        2. It is a tuple of modalities (e.g. ``("image", "text")``) where each part is
           individually supported and the model also supports ``"message"`` format, which
           is used to combine multiple modalities into a single input.

        Args:
            modality: A single modality string (e.g. ``"text"``, ``"image"``) or a tuple
                of modality strings (e.g. ``("image", "text")``).

        Returns:
            bool: Whether the model supports the given modality.

        Example::

            >>> from sentence_transformers import SentenceTransformer
            >>> model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
            >>> model.supports("text")
            True
            >>> model.supports("image")
            False
        Tmessagec              3  &   K   | ]  }|v  
 y wN ).0part	supporteds     rx   	<genexpr>z%BaseModel.supports.<locals>.<genexpr>L  s     >Tty(>s   F)r   ra   tupleall)rq   modalityr   s     @rx   supportszBaseModel.supports-  s>    6 OO	y h&9	+A>X>>>r   c                   t        | j                               }t               }|r|j                         \  }}t	        |t
              rG|j                  j                         D ]*  }|j                  t        |j                                      , | j                  r,|| j                  v r|j                  | j                  |          t        |d      r|j                  |j                         |rt        |      S )a  
        Get the keyword arguments specific to this model for inference methods like `encode` or `predict`.

        Example:

            >>> from sentence_transformers import SentenceTransformer, SparseEncoder
            >>> SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2").get_model_kwargs()
            []
            >>> SentenceTransformer("jinaai/jina-embeddings-v4", trust_remote_code=True).get_model_kwargs()
            ['task', 'truncate_dim']
            >>> SparseEncoder("opensearch-project/opensearch-neural-sparse-encoding-doc-v3-distill").get_model_kwargs()
            ['task']

        Returns:
            list[str]: A list of keyword arguments for the forward pass.
        forward_kwargs)rk   named_childrensetpopra   r    sub_modulesvaluesextendrL   rJ   hasattrr   )rq   r2   r   module_nameru   route_moduless         rx   get_model_kwargszBaseModel.get_model_kwargsO  s    " t**,-")++-K&&)%+%7%7%>%>%@ IMNN4(D(D(F#GHI!!kT5G5G&G%%d&8&8&EFv/0%%f&;&;<  N##r   c                    | j                   S )a_  
        .. deprecated::
            Use the :attr:`max_seq_length` property instead.

        Returns the maximal sequence length that the first module of the model accepts.
        Longer inputs will be truncated.

        Returns:
            Optional[int]: The maximal sequence length that the model accepts, or None if it is not defined.
        max_seq_lengthr   s    rx   get_max_seq_lengthzBaseModel.get_max_seq_lengthm  s     """r   c                X    | j                   t        t        | j                                  S )z4Returns the first module of this sequential embedder)_modulesrh   iterr   s    rx   _first_modulezBaseModel._first_modulez  s    }}T$t}}"5677r   c                X    | j                   t        t        | j                                  S )z3Returns the last module of this sequential embedder)r   rh   reversedr   s    rx   _last_modulezBaseModel._last_module~  s    }}T(4=="9:;;r   c                P   | d   }t        |t              r|j                  S t        |t              ru|j                  j                         D ]C  }t        t        |j                               d      }t        |t              r|j                  rC y t        |j                        S y)a  Check if the first module (Transformer or Router containing Transformers) supports flattened text-only inputs.

        For Router models, this returns True only if ALL routes support flattening, since we cannot
        know at this point which route will be used.
        r   NF)
ra   r!   can_flatten_inputsr    r   r   rh   r   rl   bool)rq   input_moduleroutefirst_in_routes       rx   _can_flatten_inputszBaseModel._can_flatten_inputs  s     AwlK0222lF+%1188: !!%d5>>+;&<d!C!.+>nFgFg 	!
 0011r   c                    t        |       }t        j                  || j                        }| d|dz   dz   |ddd<   | |dz
  |dz
  dz  d   |ddd<   |S )aJ  Interleave a largest-to-smallest sorted index array so that each consecutive batch
        contains a mix of long and short inputs.

        When text-only inputs are flattened via flash attention, there is no padding. Total tokens per
        batch equals the sum of actual lengths. Grouping all long inputs together creates peak-memory
        batches, while grouping all short inputs together under-utilises the GPU. Interleaving
        (largest, smallest, 2nd largest, 2nd smallest, ...) balances the total token count
        across batches for more uniform memory usage.
        )rj   NrG      r   )rc   npemptyrj   )
sorted_idxr   interleaveds      rx   _interleave_sorted_indicesz$BaseModel._interleave_sorted_indices  sp     
Ohhq
(8(89&~!a%A6ADqD&q1uA!|b'@AADqDr   c                   t        | t              rt        |       S t        | t        t        f      r.| r,t        | d   t
              rd| d   v st        d | D              S t        | t
              rd| v rt        j                  | d         S d| v rC| d   }t        |t              rt        |      S t        |t              rt        d |D              S yt        d | j                         D              S t        | t              r'| r%t        | d   t
              rt        d	 | D              S t        | t        j                  t        j                  f      r6t        | t        j                        r| j                         S | j                  S 	 dd
lm} t        | |      r| j                  d   | j                  d   z  S 	 y# t$        $ r Y yw xY w)zEstimate the "size" of an input sample for length-based batch sorting.

        The exact value doesn't matter, it's only used to group similarly sized
        inputs together so that padding waste is minimised within each batch.
        r   rolec              3  F   K   | ]  }t         j                  |        y wr   r*   _input_length)r   r}   s     rx   r   z*BaseModel._input_length.<locals>.<genexpr>  s     F!92215F   !arraycontentc           	   3     K   | ]9  }t        |t              st        t        |j	                  d d                   ; yw)r   rz   N)ra   rI   rc   r/   r   )r   items     rx   r   z*BaseModel._input_length.<locals>.<genexpr>  s1     l$U_`dfjUks3txx';#<=ls
   A)ArG   c              3  F   K   | ]  }t         j                  |        y wr   r   )r   r   s     rx   r   z*BaseModel._input_length.<locals>.<genexpr>  s     Kay..q1Kr   c              3  F   K   | ]  }t         j                  |        y wr   r   )r   msgs     rx   r   z*BaseModel._input_length.<locals>.<genexpr>  s     Fy..s3Fr   )Image)ra   r/   rc   r   rk   rI   sumr*   r   r   r   ndarrayr1   r   nelementsize	PIL.Imager   ImportError)sampler   PILImages      rx   r   zBaseModel._input_length  s    fc"v; fudm,z&)T:vPQ?RFvFFF fd#&  ..vg??F" +gs+w<'gt,l'lllK6==?KKK fd#:fQi3NFvFFF frzz5<<89(265<<(H6??$YfkkY	3&(+{{1~A66 ,
   		s   0F> >	G
	G
c           	     T   | j                         D ]  \  }}i }t        |t              r|}nkg }| j                  | j                  j	                  |g       }|j                         D ci c]&  \  }}||v st        |d      r||j                  v r||( }}} ||fi |} |S c c}}w )z.Forward pass through all modules in the model.r   )r   ra   r    rL   r   r~   r   r   )	rq   inputkwargsr   ru   rL   module_kwarg_keysr   r   s	            rx   forwardzBaseModel.forward  s    #'#6#6#8 	3KM&&) &$&!%%1(,(:(:(>(>{B(O% '-lln!"U//GFDT4UZ]agavavZv J! !
 52M2E	3 !s   )+B$c                    |si S d}	 t        | j                        }| j	                  |      sdj                  d  j                  D              }dt        |       dt               j                   d| }t        |t              r,t         fd|D              r|d	d
j                  |       dz  }t        |      	   d   j                  |fd|i|}|S # t        t        f$ r Y w xY w# t        $ rX |r<|dk(  r7|D cg c]&  }t        |t              r||d   z   f|dd z   n||z   ( nc c}w }}  d   j                  |fi |}Y |S t        $ r~ |r<|dk(  r7|D cg c]&  }t        |t              r||d   z   f|dd z   n||z   ( nc c}w }}	   d   j                  |fi |}Y |S # t        $ r  d   j                  |      }Y Y |S w xY ww xY w)a  
        Preprocesses the inputs for the model.

        Args:
            inputs (list[SingleInput | PairInput]): A list of inputs to be preprocessed. Each input can be a
                string, dict, tuple, PIL Image, numpy array, torch Tensor, or other supported modality.
                If a single input is provided, it must be wrapped in a list.
            prompt (str, optional): A prompt string to prepend to text inputs. Defaults to None.
                If the model supports the ``message`` modality, the prompt will be added as a system message to the
                input messages instead of being prepended to text.

        Returns:
            dict[str, Tensor | Any]: A dictionary of tensors with the preprocessed inputs.
        N)supported_modalities, c              3  2   K   | ]  }t        |        y wr   )r   )r   ms     rx   r   z'BaseModel.preprocess.<locals>.<genexpr>
  s     !N/!"4!Ns   z
Modality 'z' is not supported by this z model. Supported modalities: c              3  :   K   | ]  }|j                   v   y wr   )r   )r   r   rq   s     rx   r   z'BaseModel.preprocess.<locals>.<genexpr>  s     2`t44??3J2`s   z
This model supports z and zR individually, but not in the same input. Please process each modality separately.r   r   r   rG   )r   r   rd   	TypeErrorr   joinr   re   rP   ra   r   r   
preprocessAttributeErrortokenize)	rq   inputsr   r   r   r   r   preprocessedinps	   `        rx   r   zBaseModel.preprocess  sJ   ( I
 	+FYH h(?		!Ndoo!NNI_X677RSWX\S]SfSfRg h))25  (E*s2`W_2`/`,W\\(-C,D EZ Z W%%
	8-47--fNVNvNL C I& 		*  	@(f,ntugj*S%:P6CF?,s12w6V\_bVbbuuu-47--f??L   	8(f,ntugj*S%:P6CF?,s12w6V\_bVbbuuu8/tAw//A&A   8#Aw//78	8sY   C C3 C0/C03G+D32GG&+FGF33GGGGc                T    t         j                  d        | j                  dd|i|S )za
        .. deprecated::
            `tokenize` is deprecated. Use `preprocess` instead.
        zEThe `tokenize` method is deprecated, please use `preprocess` instead.r   r   )rT   r   r   )rq   textsr   s      rx   r   zBaseModel.tokenize)  s,     	cdt6e6v66r   c                   t         t        f}t               r	 ddlm} ||fz  }t        ||      ryt        |t        j                        r(|j                  dk\  r|j                  j                  dv ryy# t
        $ r Y [w xY w)z
        Check if the input represents a single example or a batch of examples.

        Args:
            inputs: The input to check.
        Returns:
            bool: True if the input is a single example, False if it is a batch.
        r   )ColumnFrG   )UOT)rk   r   r   datasetsr   r   ra   r   r   ndimrj   kind)rq   r   
list_typesr   s       rx   is_singular_inputzBaseModel.is_singular_input2  s{     E]
 "+vi'
 fj)fbjj)fkkQ.>6<<CTCTXbCb  s   A4 4	B ?B c                   |yt        j                  |d       t        j                  d|        g }| j	                         }t        t         j                  j                  |d      dd      5 }t        j                  ||d	d
       ddd       t        | j                        D ]-  \  }	}
| j                  |
   }|	dk(  r9t        |d      r-|j                  r!t         j                  j                  |d      }nBt         j                  j                  |t        |	      dz   t        |      j                   z         }t        j                  |d       	 |j#                  ||       t        |      j&                  }|j)                  d      rt*        j,                  |   j.                  }t1        |      t1        |      j2                  z  }t5        j6                  ||       t9        |      D ]9  }t1        |      t1        |      j2                  z  }t5        j6                  ||       ; |j;                  d      d    dt        |      j                    }n| dt        |      j                    }|	|
t         j                  j=                  |      |d}| j>                  r$|
| j>                  v r| j>                  |
   x}r||d<   |jA                  |       0 t        t         j                  j                  |d      dd      5 }t        j                  ||d	       ddd       |r| jC                  |||       yy# 1 sw Y   xY w# t$        $ r |j#                  |       Y w xY w# 1 sw Y   NxY w)  
        Saves a model and its configuration files to a directory, so that it can be loaded again.

        Args:
            path (str): Path on disk where the model will be saved.
            model_name (str, optional): Optional model name.
            create_model_card (bool, optional): If True, create a README.md with basic information about this model.
            train_datasets (List[str], optional): Optional list with the names of the datasets used to train the model.
            safe_serialization (bool, optional): If True, save the model using safetensors. If False, save the model
                the traditional (but unsafe) PyTorch way.
        NTexist_okzSaving model to !config_sentence_transformers.jsonwutf8encodingr   )indent	sort_keysr   save_in_rootrz   _)safe_serializationztransformers_modules.r|   r   )rt   namerZ   re   r   modules.json)r  )"rR   makedirsrT   rU   _get_model_configopenrZ   r   jsondumprb   r   r   r	  r/   re   rP   saver   
__module__
startswithsysr2   __file__r   r  shutilcopyr   splitbasenamerL   append_create_model_card)rq   rZ   
model_namecreate_model_cardtrain_datasetsr  modules_configconfigfOutrt   r  ru   
model_path	class_ref
class_file	dest_fileneeded_filemodule_configrL   s                      rx   r  zBaseModel.saveJ  s   & <
D4(&tf-. '')"'',,t%HI3Y_` 	>dhIIfd1=	> #4==1 '	1IC!]]40FqWV^<ATATWW\\$3
WW\\$C3fAVAV0VW
KK
T2(J;MN V//I##$;< [[3<<
 !,Z0@0E0EF	J	2 $=Z#H 8K $Z 0D4E4J4J KIKKY78  )s3B78$v,:O:O9PQ	(k4<+@+@*AB	$'rww?O?OPZ?[enoM!!dd.@.@&@W[WiWijnWoFomFo*7h'!!-0O'	1R "'',,t^4cFK 	6tIInd15	6 ##D*nE a	> 	>"  (J'(8	6 	6s*   1L=M
M+=M
M('M(+M4c                    | j                   t        t        j                  t        j                  d| j                  | j
                  dS )N)sentence_transformerstransformerspytorch)rQ   r   r4   r5   )rQ   r   r,  r1   r4   r5   r   s    rx   r  zBaseModel._get_model_config  s?    //)4 , 8 8 ,,
 ||#'#;#;	
 		
r   c                0    | j                  |||||       y)r   r  r  r   r  N)r  )rq   rZ   r  r  r   r  s         rx   save_pretrainedzBaseModel.save_pretrained  s$    & 			!/)1 	 	
r   c                    | j                   j                  rf|j                  d| j                  j                   d| j
                   dd| j                  j                   d| j                   j                   d      }|S )z.Update the default model ID in the model card.zmodel = z("")r>   model_idreplacerO   rP   r0   )rq   
model_cards     rx   _update_default_model_idz"BaseModel._update_default_model_id  sy    ((#++4>>2232d6[6[5\\]^4>>2232d6J6J6S6S5TTUVJ r   c                b   |rBt        |      }|j                         s'| j                  j                  s|| j                  _        || j                  _        | j
                  r6d| j                  j                  vr| j
                  }| j                  |      }n	 t        |       }t        t        j                   j#                  |d      dd      5 }|j%                  |       ddd       y# t        $ r. t        j                  dt        j                          d       Y yw xY w# 1 sw Y   yxY w)	a  
        Create an automatic model card and store it in the specified path.

        Args:
            path (str): The path where the model card will be stored.
            model_name (Optional[str], optional): The name of the model. Defaults to None.
            train_datasets (Optional[List[str]], optional): Deprecated argument, ignored.

        Returns:
            None
        generated_from_trainerz#Error while generating model card:
zConsider opening an issue on https://github.com/huggingface/sentence-transformers/issues with this traceback.
Skipping model card creation.N	README.mdr  r  r  )r   r[   r>   r3  save_dirrN   tagsr6  r   	ExceptionrT   error	traceback
format_excr  rR   rZ   r   write)rq   rZ   r  r   r$  r5  r#  s          rx   r  zBaseModel._create_model_card  s    j)J$$&t/C/C/L/L0:$$- )-%   %=TEYEYE^E^%^..J66zBJ06
 "'',,t[13H 	#DJJz"	# 	#  :9;O;O;Q:R44
 	# 	#s   C+ D%+4D"!D"%D.c                *   t         j                  d       |rcd|vr$t         j                  d| d| d       | d| }n;|j                  d      d   |k7  rt        d      t         j                  d| d       | j	                  ||||||||	|
	      S )aY  
        DEPRECATED, use `push_to_hub` instead.

        Uploads all elements of this model to a new HuggingFace Hub repository.

        Args:
            repo_id (str): Repository name for your model in the Hub, including the user or organization.
            token (str, optional): An authentication token (See https://huggingface.co/settings/token)
            private (bool, optional): Set to true, for hosting a private model
            safe_serialization (bool, optional): If true, save the model using safetensors. If false, save the model the traditional PyTorch way
            commit_message (str, optional): Message to commit while pushing.
            local_model_path (str, optional): Path of the model locally. If set, this file path will be uploaded. Otherwise, the current model will be uploaded
            exist_ok (bool, optional): If true, saving to an existing repository is OK. If false, saving only to a new repository is possible
            replace_model_card (bool, optional): If true, replace an existing model card in the hub with the automatically created model card
            train_datasets (List[str], optional): Datasets used to train the model. If set, the datasets will be added to the model card in the Hub.

        Returns:
            str: The url of the commit of your model in the repository on the Hugging Face Hub.
        zThe `save_to_hub` method is deprecated and will be removed in a future version of SentenceTransformers. Please use `push_to_hub` instead for future model uploads.rF   zQProviding an `organization` to `save_to_hub` is deprecated. Please use `repo_id="z"` instead.r   zYProviding an `organization` to `save_to_hub` is deprecated. Please use `repo_id` instead.)	repo_idr:   privater  commit_messagelocal_model_pathr  replace_model_cardr   )rT   r   r  rd   push_to_hub)rq   rB  organizationr:   rC  r  rD  rE  r  rF  r   s              rx   save_to_hubzBaseModel.save_to_hub  s    B 	J	

 '!ghtguuvw~v  @K  L *N!G95s#A&,6 o  ghogpp{| 1)-1)   

 
	
r   c           	     b   t        |      }|j                  ||d|xs |      }|j                  }| j                  j	                  |       |
|j                  ||
d       |M| j                         }|dk(  rd| j                  j                   d}nd| j                  j                   d	| d
}d}|r.d| j                  j                   d|  d| j                  |       }|r|j                  ||||r|nd||
      }ngt        j                         5 }|rd}nt        |d||
d      }|du }| j                  ||||	|       |j                  ||||r|nd||
      }ddd       |r.t        j!                  dj"                          |j"                  S j$                  S # 1 sw Y   ExY w)az  
        Uploads all elements of this model to a HuggingFace Hub repository, creating it if it doesn't exist.

        Args:
            repo_id (str): Repository name for your model in the Hub, including the user or organization.
            token (str, optional): An authentication token (See https://huggingface.co/settings/token)
            private (bool, optional): Set to true, for hosting a private model
            safe_serialization (bool, optional): If true, save the model using safetensors. If false, save the model the traditional PyTorch way
            commit_message (str, optional): Message to commit while pushing.
            local_model_path (str, optional): Path of the model locally. If set, this file path will be uploaded. Otherwise, the current model will be uploaded
            exist_ok (bool, optional): If true, saving to an existing repository is OK. If false, saving only to a new repository is possible
            replace_model_card (bool, optional): If true, replace an existing model card in the hub with the
                automatically created model card. If false (default), keep the existing model card if one exists
                in the repository.
            train_datasets (List[str], optional): Datasets used to train the model. If set, the datasets will be added to the model card in the Hub.
            revision (str, optional): Branch to push the uploaded files to
            create_pr (bool, optional): If True, create a pull request instead of pushing directly to the main branch

        Returns:
            str: The url of the commit of your model in the repository on the Hugging Face Hub.
        )r:   N)rB  rC  	repo_typer  T)rB  branchr  r1   zAdd new z modelz model with an z backendrz   zBHello!

This pull request has been automatically generated to add z1 compatibility.

## Full Model Architecture:
```
z
```

)rB  folder_pathrD  commit_description	create_prr8   r9  F)r:   r8   r9   r/  z#A pull request has been created at )r   create_reporB  r>   set_model_idcreate_branchr   rO   rP   _push_to_hub_usage_tipupload_foldertempfileTemporaryDirectoryr&   r  rT   rU   pr_url
commit_url)rq   rB  r:   rC  r  rD  rE  r  rF  r   r8   rO  apirepo_urlr?   rN  
folder_urltmp_dircreate_model_card_for_pathexisting_readmes                       rx   rG  zBaseModel.push_to_hub4  s   F % ??*	 # 
 ""))'2ghN!&&(G'!#+DNN,C,C+DF!K#+DNN,C,C+DOT[S\\d!e&; <@>>;R;R:S T   W% &
"* **,-9B#5#! + J ,,. '%15. '5EH_d'O 2AD1H.		&&@#1'9   !..# '#1=F'9D'% / 
!2 KK=j>O>O=PQR$$$$$$; s   AF%%F.c
           	        ||||||||	d}
t        |d||||      }|9t        j                  d| d| j                   d        | j                  |fi |
S | j                  |||||      }|| j                  k(  r9t        j                  d| j                   d| d	        | j                  |fi |
S t        j                  d
| d| d| j                   d	        | j                  |fi |
d|iS )NrH   r  r:   r6   r8   r9   zNo modules.json found for z, initializing a new z model.zLoading z model from r|   zConverting z model z to rQ   )r&   rT   rU   rQ   _load_default_modules_get_model_type_load_config_modules_load_converted_modules)rq   rr   r:   r6   r8   r7   r9   r;   r<   r=   load_kwargsmodules_json_pathmodel_type_being_loadeds                rx   r`   zBaseModel._load_modules  sH    ( !2 0( 0*	
 +%-
 $KK45G4HH]^b^m^m]nnuvw-4--.@PKPP"&"6"6%- #7 #
 #doo5KK(4??"3<@R?SSTUV,4,,-?O;OOk"9!:'BTAUUYZ^ZiZiYjjklm+t++,>r+rZqrrr   c
                     y)a  

        Args:
            model_name_or_path (str): The name or path of the pre-trained model.
            token (Optional[Union[bool, str]]): The token to use for the model.
            cache_folder (Optional[str]): The folder to cache the model.
            revision (Optional[str], optional): The revision of the model. Defaults to None.
            trust_remote_code (bool, optional): Whether to trust remote code. Defaults to False.
            local_files_only (bool, optional): Whether to use only local files. Defaults to False.
            model_kwargs (Optional[Dict[str, Any]], optional): Additional keyword arguments for the model. Defaults to None.
            processor_kwargs (Optional[Dict[str, Any]], optional): Additional keyword arguments for the processor/tokenizer. Defaults to None.
            config_kwargs (Optional[Dict[str, Any]], optional): Additional keyword arguments for the config. Defaults to None.

        Returns:
            List[nn.Module]: A list containing the transformer model and the pooling model.
        Nr   )
rq   rr   r:   r6   r8   r7   r9   r;   r<   r=   s
             rx   ra  zBaseModel._load_default_modules  s    r   c
                L   t        |d||||      }
|
t        |
d      5 }t        j                  |      }ddd       dv rbd|d   v r[t	        j
                  |d   d         t	        j
                  t              kD  r&t        j                  d|d   d    d	t         d
       | j                  |       t        |d||||      }|,	 t        |d      5 }|j                         | _        ddd       t        |d||||      }t        |d      5 }t        j                  |      }ddd       t               }t               }D ]  }|d   }| j                  |||||      }t        j                   |j                        }t#        |j$                        dk(  rt        j                   |j&                        }t)        |j$                        }h d|z  }ddh|k  }|s|rt+        j,                  ||d   ||||||||	| j.                        }dddd}|r3|s1|j1                         D ]  \  }}||v s|j3                  |      ||<     ddh}|D ]  }||v s||vs|j3                  |         ||fi |}nOt5        ||d   ||||      } |j                  |       }n)|j                  ||d   ||||||||	| j.                        }|||d   <   |j7                  dg       ||d   <    |]t9        |      }!t#        |!j:                        dk\  r:t9        |      j:                  d   }"t#        |"      dk(  rt=        d  |"D              r|"}|s| j>                  jA                  ||!       ||fS # 1 sw Y   :xY w# 1 sw Y   xY w# t        $ r Y w xY w# 1 sw Y   rxY w)"a  
        Loads a full model using the modules.json file.

        Args:
            model_name_or_path (str): The name or path of the pre-trained model.
            token (Optional[Union[bool, str]]): The token to use for the model.
            cache_folder (Optional[str]): The folder to cache the model.
            revision (Optional[str], optional): The revision of the model. Defaults to None.
            trust_remote_code (bool, optional): Whether to trust remote code. Defaults to False.
            local_files_only (bool, optional): Whether to use only local files. Defaults to False.
            model_kwargs (Optional[Dict[str, Any]], optional): Additional keyword arguments for the model. Defaults to None.
            processor_kwargs (Optional[Dict[str, Any]], optional): Additional keyword arguments for the processor/tokenizer. Defaults to None.
            config_kwargs (Optional[Dict[str, Any]], optional): Additional keyword arguments for the config. Defaults to None.

        Returns:
            Tuple[OrderedDict[str, nn.Module], OrderedDict[str, Any]]: An ordered dictionary containing the modules of the model and their kwargs.
        r  r`  Nr  r  r   r+  z:This model was created with Sentence Transformers version z, but you're using version zD. Consider updating to the latest version to avoid potential issues.r9  r  re   rG   >   
model_argsconfig_argstokenizer_argsr;   r=   rZ   )
	subfolderr:   r6   r8   r9   r7   r;   r<   r=   r?   rj  rl  rk  )r;   r<   r=   modality_configmodule_output_name)rr   rm  r:   r6   r8   r9   r  r   r   (   c              3  $   K   | ]  }|d v  
 yw)0123456789abcdefNr   )r   cs     rx   r   z1BaseModel._load_config_modules.<locals>.<genexpr>  s     8mUV>P9P8ms   )r8   )!r&   r  r  loadr   parser   rT   r   _parse_model_configreadrN   r<  r   _load_module_class_from_refinspect	signaturerc   ri   rg   r   r!   _load_init_kwargsr?   r~   r   r%   r   r   partsr   r>   set_base_model)#rq   rr   r:   r6   r8   r7   r9   r;   r<   r=   &config_sentence_transformers_json_pathfInmodel_configmodel_card_pathrf  r!  r2   rL   r)  r%  module_classload_signaturer{  init_paramsuses_old_namesuses_new_namesinit_kwargsnew_to_old_name_mappingnew_nameold_nameoptional_paramsru   
local_path
path_partsrevision_path_parts#                                      rx   rc  zBaseModel._load_config_modules  sj   < 2@/%-2
. 2=<vN .RU#yy~. -+|M/JJMM,}"=>U"VWZaZgZghsZttPQ]^kQl  nE  RF  QG G00;}  =AB
 $$\2 )%-
 &/F; 7s,/HHJD)7 +%-
 #f5 	,!YYs^N	, -#+ Y	SM%f-I#'#C#C-/@(L$L %..|/@/@AN >,,-2#--l.C.CD	 ")"6"67!PS^!^"0/!Bk!Q!^"-"?"?*"/"7#%1!))9*;%1)9&3 $#K$ )5,<)6/+
 &n2I2O2O2Q R.Hh';68C8QH 5R
 (9:N&OO$3 6#{2x{7R'OOH56 **<LLF "/+="/"7#%1!))9"J *..z:F
 &**&+F3!-%%5&7!-%5"/ LL +   .4GM&)*3@3D3DXr3RM-/0sY	Sv /0J:##$)%)*;%<%B%B2%F")*b0S8mZl8m5m1H  //0BX/V%%a. .47 7 	, 	,sA   M/
N	 M<-N	 N/M9<NN	 		NNN#c                    |j                  di       j                         D ]2  \  }}|| j                  vs| j                  |   $|| j                  |<   4 | j                  |j                  dd      | _        yy)a  Parse model configuration and merge saved prompts/defaults with user-provided values.

        User-provided prompts and default_prompt_name take precedence over saved config values.
        Saved prompts are only used for keys not already present in ``self.prompts``, or where the
        current value is ``None`` (i.e. a default placeholder, not yet filled by the user or config).
        Empty string ``""`` is treated as an intentional user-provided value and will not be overwritten.
        r4   Nr5   )r   r~   r4   r5   )rq   r  r   prompt_texts       rx   rw  zBaseModel._parse_model_config  s     )5(8(8B(G(M(M(O 	8$K$,,.$,,{2K2S,7[)	8 ##+'3'7'78Mt'TD$ ,r   c                6    | j                  |||||||||		      S )NrH   )ra  )rq   rr   r:   r6   r8   r7   r9   r;   r<   r=   rQ   s              rx   rd  z!BaseModel._load_converted_modules  s7     ))%/-%-' * 

 
	
r   c                    t        |d||||      }|yt        |d      5 }t        j                  |      }|j	                  dd      cddd       S # 1 sw Y   yxY w)at  
        Retrieves the model_type from the config_sentence_transformers.json file.

        This is used to determine whether the model being loaded matches the current class
        (e.g., a SentenceTransformer model loaded with SentenceTransformer, or a SparseEncoder model
        loaded with SparseEncoder). When the model type doesn't match, we switch to a converted
        loading method to ensure compatibility.

        Defaults to "SentenceTransformer" if the config file is missing or has no "model_type" key,
        for backwards compatibility with older models.

        Args:
            model_name_or_path (str): The name or path of the pre-trained model.
            token (Optional[Union[bool, str]]): The token to use for the model.
            cache_folder (Optional[str]): The folder to cache the model.
            revision (Optional[str], optional): The revision of the model. Defaults to None.
            local_files_only (bool, optional): Whether to use only local files. Defaults to False.

        Returns:
            str: The model type, e.g. "SentenceTransformer", "SparseEncoder", or "CrossEncoder".
        r  r`  NSentenceTransformerr  r  rQ   )r&   r  r  ru  r   )	rq   rr   r:   r6   r8   r9   r  r  r"  s	            rx   rb  zBaseModel._get_model_type  so    : 2@/%-2
. 29(86J 	CcYYs^F::l,AB	C 	C 	Cs   'AAc                   |j                  d      rt        |      S |st        j                  j	                  |      r&|r|j                  dd      nd}	 t        ||||      S t        |      S # t        t        f$ r Y t        |      S w xY w)a  
        Load a module class from a class reference string.

        Args:
            class_ref: The class reference string (e.g., "sentence_transformers.sentence_transformer.modules.Pooling")
            model_name_or_path: The model name or path
            trust_remote_code: Whether to trust remote code
            revision: The model revision
            model_kwargs: Additional model kwargs

        Returns:
            The module class
        zsentence_transformers.code_revisionN)r8   r  )	r  r$   rR   rZ   r[   r   r   OSErrorrd   )rq   r%  rr   r7   r8   r;   r  s          rx   ry  z%BaseModel._load_module_class_from_ref   s    .  89%i00/A BGSL,,_dCY]M	4&%"/	  "),,	 Z( !),,	s   A/ /B
Bc                F    |t        j                  |d        || |      S )a?  
        Evaluate the model based on an evaluator

        Args:
            evaluator (BaseEvaluator): The evaluator used to evaluate the model.
            output_path (str, optional): The path where the evaluator can write the results. Defaults to None.

        Returns:
            The evaluation results.
        Tr   )rR   r  )rq   	evaluatoroutput_paths      rx   evaluatezBaseModel.evaluate)  s%     "KKd3{++r   c                    | j                         D ]&  }|| ust        |d      s	 |j                  |       ( y# t        $ r |j                          Y Ew xY w)z,Enable gradient checkpointing for the model.gradient_checkpointing_enableN)r2   r   r  r   )rq   gradient_checkpointing_kwargsru   s      rx   r  z'BaseModel.gradient_checkpointing_enable8  s\     lln 	;FT!gf6U&V;889VW	; ! ;88:;s   ;AAc                   | j                   x}t        |d      r|j                  S t        | j                        rAt        | d   d      r2t        | d   j
                  d      r| d   j
                  j                  S 	 t        | j                               j                  S # t        $ rZ dd}| j                  |      }	 t        |      }|d   j                  cY S # t        $ r t        j                  d      cY cY S w xY ww xY w)	z
        Get torch.device from module, assuming that the whole module has one device.
        In case there are no PyTorch parameters, fall back to CPU.
        r3   r   
auto_modelc                    | j                   j                         D cg c]  \  }}t        j                  |      s||f! }}}|S c c}}w r   )__dict__r~   r1   	is_tensor)ru   r   r   tupless       rx   find_tensor_attributesz0BaseModel.device.<locals>.find_tensor_attributesS  sA    -3__-B-B-D[TQXYHZ1a&[[ \s
   AA)get_members_fnrG   cpu)ru   	nn.Modulereturnzlist[tuple[str, Tensor]])transformers_modelr   r3   rc   r   r  rh   ri   StopIteration_named_membersr1   )rq   r  r  genfirst_tuples        rx   r3   zBaseModel.deviceB  s     #'"9"99F7SegoKp%,,,t}}'$q'<"@WTRSWM_M_aiEj7%%,,,	+)*111 	+ %%5K%LC+"3i"1~,,,  +||E**+	+s0   >"B! !DCDD ;D?D  Dc           	     N   |t         j                  j                         r8t        t         j                  j	                               D cg c]  }d| 	 }}n]t               r8t        t         j                  j	                               D cg c]  }d| 	 }}nt        j                  d       dgdz  }t        j                  ddj                  t        t        |                    | j                  d       | j                          t        j                  d      }|j!                         }|j!                         }g }|D ]O  }|j#                  | j$                  j&                  || ||fd	
      }|j)                          |j+                  |       Q |||dS c c}w c c}w )aT  
        Starts a multi-process pool to infer with several independent processes.

        This method is recommended if you want to predict on multiple GPUs or CPUs. It is advised
        to start only one process per GPU. This method works together with predict and
        stop_multi_process_pool.

        Args:
            target_devices (List[str], optional): PyTorch target devices, e.g. ["cuda:0", "cuda:1", ...],
                ["npu:0", "npu:1", ...], or ["cpu", "cpu", "cpu", "cpu"]. If target_devices is None and CUDA/NPU
                is available, then all available CUDA/NPU devices will be used. If target_devices is None and
                CUDA/NPU is not available, then 4 CPU devices will be used.

        Returns:
            Dict[str, Any]: A dictionary with the target processes, an input queue, and an output queue.
        zcuda:znpu:z1CUDA/NPU is not available. Starting 4 CPU workersr     z(Starting multi-process pool on devices: r   spawnT)targetargsdaemon)r   output	processes)r1   cudais_availablerangedevice_countr   npurT   rU   r   mapr/   rm   share_memorympget_contextr   ProcessrO   _multi_process_workerstartr  )	rq   target_devicesictxinput_queueoutput_queuer  	device_idps	            rx   start_multi_process_poolz"BaseModel.start_multi_process_pool^  se   & !zz&&(7<UZZ=T=T=V7W!X!E!+!X!X')6;EII<R<R<T6U!VD*!V!VOP"'1>tyySR`Ia?b>cde 	nnW%iikyy{	' 	 I~~;;{LA  A
 GGIQ	  %9UU7 "Y!Vs   
FF"c                    | d   D ]  }|j                           | d   D ]"  }|j                          |j                          $ | d   j                          | d   j                          y)z
        Stops all processes started with start_multi_process_pool.

        Args:
            pool (Dict[str, object]): A dictionary containing the input queue, output queue, and process list.

        Returns:
            None
        r  r   r  N)	terminater   close)poolr  s     rx   stop_multi_process_poolz!BaseModel.stop_multi_process_pool  sk     k" 	AKKM	 k" 	AFFHGGI	 	WXr   c                    t        d      )N0This method should be implemented in subclasses.NotImplementedError)rq   r  r   s      rx   _multi_processzBaseModel._multi_process  s    !"TUUr   c                    t        d      )af  Worker function for multi-process inference. Must be overridden by subclasses.

        This is called as the target function in each spawned process by
        :meth:`start_multi_process_pool`. Subclasses should implement this to
        read from ``input_queue``, run inference on ``target_device``, and write
        results to ``results_queue``.
        r  r  )target_devicemodelr  results_queues       rx   r  zBaseModel._multi_process_worker  s     ""TUUr   c                     | d   j                   S )zJ
        Property to get the tokenizer that is used by this model
        r   )	tokenizerr   s    rx   r  zBaseModel.tokenizer      
 Aw   r   c                    	 || d   _         y# t        $ r' t        dt        | d         j                   d      dw xY w)zQ
        Property to set the tokenizer that should be used by this model
        r   zThe first module (z() does not have a 'tokenizer' attribute.N)r  r   re   rP   rq   r   s     rx   r  zBaseModel.tokenizer  sN    
	 %DG 	 $T$q']%;%;$<<de	s   
 0=c                     | d   j                   S )zJ
        Property to get the processor that is used by this model
        r   )	processorr   s    rx   r  zBaseModel.processor  r  r   c                "    t        | d   dd      S )z
        Returns the maximal input sequence length for the model. Longer inputs will be truncated.

        Returns:
            Optional[int]: The maximal input sequence length, or None if not defined.
        r   r   Nr   r   s    rx   r   zBaseModel.max_seq_length  s     tAw 0$77r   c                    || d   _         y)zs
        Property to set the maximal input sequence length for the model. Longer inputs will be truncated.
        r   Nr   r  s     rx   r   zBaseModel.max_seq_length  s    
 "'Qr   c                    | j                         D ]4  }t        |t              r|j                  c S t        |t              s2|c S  y)a  
        Property to get the underlying transformers PreTrainedModel instance, if it exists.
        Note that it's possible for a model to have multiple underlying transformers models, but this property
        will return the first one it finds in the module hierarchy.

        .. note::

            This property can also return e.g. ORTModelForFeatureExtraction or OVModelForFeatureExtraction instances
            from the optimum-intel and optimum-onnx libraries, if the model is loaded using ``backend="onnx"`` or
            ``backend="openvino"``.

        Returns:
            PreTrainedModel or None: The underlying transformers model or None if not found.
        N)r2   ra   r!   r  r   )rq   ru   s     rx   r  zBaseModel.transformers_model  sA      lln 	F&+.||#&/2	 r   c                    t         j                  d| j                  j                   d| j                  j                   d       | j                  S )N`z2._target_device` has been deprecated. Please use `z.device` instead.)rT   r   rO   rP   r3   r   s    rx   _target_devicezBaseModel._target_device  sN    ''((Z[_[i[i[r[rZs  tE  F	
 {{r   c                    t         j                  d| j                  j                   d       | j	                  |       y )Nr  zF._target_device` has been deprecated. Please use `to(device)` instead.)rT   r   rO   rP   rm   )rq   r3   s     rx   r  zBaseModel._target_device  s3    ''((no	
 	r   c                h    	 t        | j                               j                  S # t        $ r Y yw xY w)zw
        `torch.dtype`: The dtype of the module (assuming that all the module parameters have the same dtype).
        N)rh   ri   rj   r  r   s    rx   rj   zBaseModel.dtype	  s1    
	)*000 		s   "% 	11c                    g S )zc
        Return the list of modules that should not be split when using model parallelism.
        r   r   s    rx   _no_split_moduleszBaseModel._no_split_modules  	    
 	r   c                    g S )zJ
        Return the list of keys to ignore when saving the model.
        r   r   s    rx   _keys_to_ignore_on_savez!BaseModel._keys_to_ignore_on_save  r  r   c           	     l    | j                   j                  }| j                         }d| d| d| d| d	S )zReturn a usage tip snippet for the push_to_hub PR description.

        Subclasses can override this to provide model-type-specific example code.
        z## Testing this pull request
You can test this pull request before merging by loading the model from this PR with the `revision` argument:
```python
from sentence_transformers import zO

# NOTE: Update this to the number of your pull request
pr_number = 2
model = z(
    "z5",
    revision=f"refs/pr/{pr_number}",
    backend="z	",
)
```
)rO   rP   r   )rq   rB  
class_namer?   s       rx   rS  z BaseModel._push_to_hub_usage_tip!  s\    
 ^^,,
""$# $., /	 
 Y Y  	r   r   ) rr   r+   r2   z4list[nn.Module] | OrderedDict[str, nn.Module] | Noner3   r+   r4   zdict[str, str] | Noner5   r+   r6   r+   r7   r   r8   r+   r9   r   r:   bool | str | Noner;   dict[str, Any] | Noner<   r  r=   r  r>   zCardData | Noner?   $Literal['torch', 'onnx', 'openvino']r  None)r  r  )r   r+   r   r+   r  r+   )r  r  )r  zlist[Modality])r   r   r  r   )r  z	list[str])r  z
int | None)r  ztorch.nn.Module)r  r   )r   
np.ndarrayr  r  )r  int)r   dict[str, Tensor]r  r  )r   zlist[SingleInput | PairInput]r   r+   r  zdict[str, Tensor | Any])r   z.list[str] | list[dict] | list[tuple[str, str]]r  r  )r   r	   r  r   )NTNT)rZ   r/   r  r+   r  r   r   list[str] | Noner  r   r  r  )r  dict[str, Any])r5  r/   r  r/   )N
deprecated)rZ   r/   r  r+   r   r  r  r  )	NNNTzAdd new model.NFFN)rB  r/   rH  r+   r:   r+   rC  bool | Noner  r   rD  r/   rE  r+   r  r   rF  r   r   r  r  r/   )
NNTNNFFNNF)rB  r/   r:   r+   rC  r  r  r   rD  r+   rE  r+   r  r   rF  r   r   r  r8   r+   rO  r   r  r/   )NFFNNN)rr   r/   r:   r  r6   r+   r8   r+   r7   r   r9   r   r;   r  r<   r  r=   r  r  Dtuple[list[nn.Module] | OrderedDict[str, nn.Module], dict[str, Any]])r  r  r  r  )NFFNNNN)rr   r/   r:   r  r6   r+   r8   r+   r7   r   r9   r   r;   r  r<   r  r=   r  rQ   r+   r  r  )NF)rr   r/   r:   r  r6   r+   r8   r+   r9   r   r  r/   )r%  r/   rr   r/   r7   r   r8   r+   r;   r  r  r  )r  r   r  r+   r  zdict[str, float] | float)r  r  r  r  )r  ztorch.device)r  r  r  2dict[Literal['input', 'output', 'processes'], Any])r  r  r  r  )
r  r/   r  r*   r  r   r  r   r  r  )r  r	   )r  zPreTrainedModel | None)r3   zint | str | torch.device | Noner  r  )r  ztorch.dtype | None)rB  r/   r  r/   )?rP   r  __qualname____doc__r   rK   r,   __annotations__r-   r0   rg   ro   r   r   propertyr   r   r   r   r   r   r   staticmethodr   r   r   r   r   r   r  r  r0  r6  r  r'   rI  rG  r`   r   ra  rc  rw  rd  rb  ry  r  r  r3   r  r  r  r  r  setterr  r   r  r  rj   r  r  rS  __classcell__)rO   s   @rx   r*   r*   /   s7   " .37$j7 /1+0,L$cL *.c2 IM!)-*.#'"'#!&#'.226/3+/8?#c2&c2 F	c2
 c2 'c2 (c2 !c2  c2 c2 c2 !c2 ,c2 0c2 -c2  )!c2" 6#c2$ 
%c2J,6 8 8 D$<#8<$    / /b* ">-> >
 
!>@76 "&"&+/#'NFNF NF  	NF
 )NF !NF 
NF`

 "&"&+/#'

 
  	

 )
 !
 

6 \h'#'#%/'#HX'#	'#R   $( ##'.'+#(+/>
>
 !>
 	>

 >
 !>
 >
 %>
 >
 !>
 )>
 
>
  >
F !##'%)'+#(+/#k%k% k% 	k%
 !k% #k% %k% k% !k% )k% k% k% 
k%d  $"'!&.226/30s0s !0s !	0s
 0s  0s 0s ,0s 00s -0s 
N0sd   $"'!&.226/3 ! !	
     , 0 - 
N D  $"'!&.226/3w&w& !w& !	w&
 w&  w& w& ,w& 0w& -w& 
Nw&rU(  $"'!&.226/3!%

 !
 !	

 
  
 
 ,
 0
 -
 
 
N
<  $!&,C,C !,C !	,C
 ,C ,C 
,C\'-'-  '-  	'-
 '- ,'- 
'-R,; + +8 260V.0V	;0Vd  (V VVV V 	V
 
V V ! ! 	 	 ! ! 8 8 ' '  .          r   r*   )M
__future__r   rV   rz  r  rR   r  r  rU  r>  abcr   r   collectionsr   multiprocessingr   pathlibr   typingr	   r
   numpyr   r1   torch.multiprocessingr  r,  huggingface_hubr   r   	packagingr   r   r   r   r   r   !transformers.dynamic_module_utilsr   r   transformers.utilsr   transformers_loggingr+  r   %sentence_transformers.base.evaluationr   #sentence_transformers.base.modalityr   r   )sentence_transformers.base.modality_typesr   r   r   %sentence_transformers.base.model_cardr   r   "sentence_transformers.base.modulesr   r    r!   %sentence_transformers.base.peft_mixinr"   sentence_transformers.utilr#   r$   r%   r&   r'   sentence_transformers.util.miscr(   
get_loggerrP   rT   
Sequentialr*   r   r   rx   <module>r     s    "    	  
   # # !     "  +   W W f > - ? U V V X J J B  H	(		(	(	2G/ Gr   