
    +#h                        d dl mZ d dlZd dl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mZ d dlmZmZ  G d d	ej"                        Zy)
    )annotationsN)Callable)
load_model)
save_model)Tensornn)fullnameimport_from_stringc                       e Zd ZdZd ej
                         ddf	 	 	 	 	 	 	 	 	 	 	 d fdZddZddZd Z	dddZ
d	 Zed
        Z xZS )Densea0  
    Feed-forward function with activation function.

    This layer takes a fixed-sized sentence embedding and passes it through a feed-forward layer. Can be used to generate deep averaging networks (DAN).

    Args:
        in_features: Size of the input dimension
        out_features: Output size
        bias: Add a bias vector
        activation_function: Pytorch activation function applied on
            output
        init_weight: Initial value for the matrix of the linear layer
        init_bias: Initial value for the bias of the linear layer
    TNc                Z   t         |           || _        || _        || _        |t        j                         n|| _        t        j                  |||      | _	        |$t        j                  |      | j                  _        |%t        j                  |      | j                  _        y y )N)bias)super__init__in_featuresout_featuresr   r   Identityactivation_functionLinearlinear	Parameterweight)selfr   r   r   r   init_weight	init_bias	__class__s          _/var/www/html/sandstorm/venv/lib/python3.12/site-packages/sentence_transformers/models/Dense.pyr   zDense.__init__   s     	&(	4G4O2;;=Uh ii\E"!#k!:DKK !||I6DKK !    c           	     n    |j                  d| j                  | j                  |d               i       |S )Nsentence_embedding)updater   r   )r   featuress     r   forwardzDense.forward5   s4    -t/G/GT\]qTrHs/tuvr   c                    | j                   S )N)r   r   s    r    get_sentence_embedding_dimensionz&Dense.get_sentence_embedding_dimension9   s       r   c                r    | j                   | j                  | j                  t        | j                        dS )N)r   r   r   r   )r   r   r   r	   r   r%   s    r   get_config_dictzDense.get_config_dict<   s3    ++ --II#+D,D,D#E	
 	
r   c                   t        t        j                  j                  |d      d      5 }t	        j
                  | j                         |       d d d        |r+t        | t        j                  j                  |d             y t        j                  | j                         t        j                  j                  |d             y # 1 sw Y   yxY w)Nconfig.jsonwmodel.safetensorspytorch_model.bin)openospathjoinjsondumpr(   save_safetensors_modeltorchsave
state_dict)r   output_pathsafe_serializationfOuts       r   r6   z
Dense.saveD   s    "'',,{M:C@ 	4DIId**,d3	4 "4kCV)WXJJt("'',,{DW*XY	4 	4s   %CCc                *    d| j                          dS )NzDense())r(   r%   s    r   __repr__zDense.__repr__M   s    ,,./q11r   c                p   t        t        j                  j                  | d            5 }t	        j
                  |      }d d d         t        d                |d<   t        di |}t        j                  j                  t        j                  j                  | d            r,t        |t        j                  j                  | d             |S |j                  t        j
                  t        j                  j                  | d      t        j                  d      d             |S # 1 sw Y   xY w)	Nr*   r   r,   r-   cpuT)map_locationweights_only )r.   r/   r0   r1   r2   loadr
   r   existsload_safetensors_modelload_state_dictr5   device)
input_pathfInconfigmodels       r   rC   z
Dense.loadP   s    "'',,z=9: 	$cYYs^F	$ )Z(:6BW;X(Y([$%77>>"'',,z3FGH"5"'',,zCV*WX  !!

GGLL-@APUP\P\]bPcrv
 	$ 	$s   D,,D5)r   intr   rL   r   boolr   z!Callable[[Tensor], Tensor] | Noner   Tensor | Noner   rN   )r"   zdict[str, Tensor])returnrL   )T)r9   rM   rO   None)__name__
__module____qualname____doc__r   Tanhr   r#   r&   r(   r6   r=   staticmethodrC   __classcell__)r   s   @r   r   r      s    & AH%)#'77 7 	7
 ?7 #7 !7,!
Z2  r   r   )
__future__r   r2   r/   typingr   r5   safetensors.torchr   rE   r   r4   r   r   sentence_transformers.utilr	   r
   Moduler   rB   r   r   <module>r]      s2    "  	   B B  CPBII Pr   