o
    -i2                     @   s  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	m
Z
 d dlmZmZmZ d dlmZ d dlmZmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZmZmZmZm Z  d dl!m"Z"m#Z#m$Z$ d dl%m&Z& ddl'm(Z(m)Z) ddl*m+Z+ ddl,m-Z- G dd dej.Z/G dd dej.Z0e-ddG dd deZ1dee2e3ej4f  de5e3 de2ee2e3ej4f  ee2e3ej4f  f fddZ6G d d! d!e1Z7e-ddG d"d# d#ej.e+Z8d$ej4d%ej4d&e9ddfd'd(Z:dS ))    N)Iterable)nn)RobertaConfig)ModelConfigPoolerConfig
VllmConfig)BOSEOSFilterDispatchPoolerPooler)pooler_for_embed)AllPoolpooler_for_token_classifypooler_for_token_embed)VocabParallelEmbedding)DefaultModelLoader)default_weight_loader)TOKEN_TYPE_SHIFTBertEmbeddingModel	BertModel_decode_token_type_ids_encode_token_type_ids)AutoWeightsLoaderWeightsMappermaybe_prefix)IntermediateTensors   )BertWithRopeJinaRobertaModel)SupportsCrossEncoding)default_pooling_typec                	       sL   e Zd Zdef fddZ	ddejdejdejdB dejfd	d
Z  ZS )RobertaEmbeddingconfigc                    s   t    |j| _t|j|j| _|j| _t	j
|j|j| jd| _t	
|j|j| _t	j|j|jd| _| dt|jd d S )N)padding_idx)epsposition_idsr   )super__init__hidden_sizesizer   
vocab_sizeword_embeddingspad_token_idr"   r   	Embeddingmax_position_embeddingsposition_embeddingstype_vocab_sizetoken_type_embeddings	LayerNormlayer_norm_epsregister_buffertorcharange	unsqueeze)selfr!   	__class__ _/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/vllm/model_executor/models/roberta.pyr&   0   s&   
zRobertaEmbedding.__init__N	input_idsr$   inputs_embedsreturnc                 C   sH   t |}|d u r| |}| |}| |}|| | }| |}|S N)r   r*   r.   r0   r1   )r7   r<   r$   r=   token_type_idsr.   r0   
embeddingsr:   r:   r;   forwardF   s   



zRobertaEmbedding.forwardr?   )	__name__
__module____qualname__r   r&   r4   TensorrB   __classcell__r:   r:   r8   r;   r    /   s    r    c                       s8   e Zd ZdZd
 fddZdejdejfdd	Z  ZS )RobertaClassificationHeadz-Head for sentence-level classification tasks.model_configr   c                    sF   t    |j}|j}tj|j|j|d| _tj|j|j|d| _	d S )Ndtype)
r%   r&   	hf_config
head_dtyper   Linearr'   dense
num_labelsout_proj)r7   rI   r!   rM   r8   r:   r;   r&   ]   s   

z"RobertaClassificationHead.__init__xr>   c                 C   s"   |  |}t|}| |}|S r?   )rO   r4   tanhrQ   )r7   rR   r:   r:   r;   rB   f   s   


z!RobertaClassificationHead.forward)rI   r   )	rC   rD   rE   __doc__r&   r4   rF   rB   rG   r:   r:   r8   r;   rH   Z   s    	rH   CLS)seq_pooling_typec                       s   e Zd ZdZdddedef fddZ		dd	ejd
ejde	dB dejdB dejf
ddZ
	ddededeeB fddZdeeeejf  fddZ  ZS )RobertaEmbeddingModelz?A model that uses Roberta to provide embedding functionalities. prefixvllm_configrZ   c                   s    t  j||d |jjj| _d S )Nr[   rZ   )r%   r&   rI   rL   r+   r"   )r7   r[   rZ   r8   r:   r;   r&   r   s   zRobertaEmbeddingModel.__init__Nr<   	positionsintermediate_tensorsr=   r>   c                 C   s"   t ||| jd | j||||dS )Nr<   r$   r"   r<   r]   r=   r^   )replace_roberta_positionsr"   model)r7   r<   r]   r^   r=   r:   r:   r;   rB   v   s   
zRobertaEmbeddingModel.forwardc                 C   sH   |j j}t||d}t|dddkrtdi |dtiS tdi |S )Nr\   position_embedding_typeabsoluteembedding_classr:   )rI   rL   dictgetattrr   r    r   )r7   r[   rZ   rL   kwargsr:   r:   r;   _build_model   s
   z"RobertaEmbeddingModel._build_modelweightsc                 C   sX   t |}tdd |D }|rtddid}ntddid}t| dgd}|j||d	S )
Nc                 s   s    | ]
\}}| d V  qdS )roberta.N
startswith).0name_r:   r:   r;   	<genexpr>   s    
z5RobertaEmbeddingModel.load_weights.<locals>.<genexpr>rk   zmodel.)orig_to_new_prefixrX   zlm_head.)skip_prefixesmapper)listanyr   r   load_weights)r7   rj   weights_listhas_roberta_prefixru   loaderr:   r:   r;   rx      s   z"RobertaEmbeddingModel.load_weights)NN)rX   )rC   rD   rE   rT   r   strr&   r4   rF   r   rB   r   r   ri   r   tuplerx   rG   r:   r:   r8   r;   rW   n   s2    

$
rW   all_weightssecondary_weightsr>   c                    s>   t | \}}fdd  fdd|D  fdd|D fS )Nc                    s   t  fddD S )Nc                 3       | ]}  |V  qd S r?   rl   )rn   fnr:   r;   rq      s    z=filter_secondary_weights.<locals>.filtered.<locals>.<genexpr>)rw   r   )r   r   r;   filtered   s   z*filter_secondary_weights.<locals>.filteredc                 3   s$    | ]\}} |r||fV  qd S r?   r:   rn   r   wr   r:   r;   rq      s   " z+filter_secondary_weights.<locals>.<genexpr>c                 3   s$    | ]\}} |s||fV  qd S r?   r:   r   r   r:   r;   rq      s    
)	itertoolstee)r~   r   all_weights1all_weights2r:   )r   r   r;   filter_secondary_weights   s
   r   c                       sb   e Zd ZdZdddedef fddZded	efd
dZ	de
eeejf  f fddZ  ZS )BgeM3EmbeddingModelzA model that extends RobertaEmbeddingModel with sparse embeddings.

    This class supports loading an additional sparse_linear.pt file
    to create sparse embeddings as described in https://arxiv.org/abs/2402.03216
    rX   rY   r[   rZ   c                   s|    j jj| _ j }|j| _|jj| _|jj| _t j |d ddg| _dd | jD | _	 fddt
| j	| jD | _d S )Nr\   zsparse_linear.zcolbert_linear.c                 S   s   g | ]}|d  qS )ptr:   rn   rZ   r:   r:   r;   
<listcomp>   s    z0BgeM3EmbeddingModel.__init__.<locals>.<listcomp>c                    s(   g | ]\}}t j jjd ||gdqS )N)model_or_pathrevisionrZ   allow_patterns_overrides)r   SourcerI   rb   )rn   filenamerZ   r[   r:   r;   r      s    )rI   rL   r'   rM   bos_token_ideos_token_idr%   r&   secondary_weight_prefixessecondary_weight_fileszipr   )r7   r[   rZ   rI   r8   r   r;   r&      s   



zBgeM3EmbeddingModel.__init__pooler_configr>   c              
   C   sp   t j| jd| jd| _t j| j| j| jd| _tt|tt	|| j| j
tt|t | jtjd| j
| jdS )Nr   rJ   )pooling
classifieract_fn)embedtoken_embedtoken_classify)r   rN   r'   rM   sparse_linearcolbert_linearr	   r   r   r   r   r   r   r4   relur   )r7   r   r:   r:   r;   _build_pooler   s,   
z!BgeM3EmbeddingModel._build_poolerr~   c                    sp   t || j\}}t | t|  }|D ]\ }t fdd| jD r5|  }t|dt}||| qd S )Nc                 3   r   r?   rl   r   ro   r:   r;   rq      s    

z3BgeM3EmbeddingModel.load_weights.<locals>.<genexpr>weight_loader)	r   r   r%   rx   rf   named_parametersrw   rg   r   )r7   r~   	secondaryrj   params_dictloaded_weightparamr   r8   r   r;   rx      s   
z BgeM3EmbeddingModel.load_weights)rC   rD   rE   rT   r   r|   r&   r   r
   r   r   r}   r4   rF   rx   rG   r:   r:   r8   r;   r      s
    (r   c                       s   e Zd ZdZdZeddddddd	d
ddZdddedef fddZ	de
eeejf  fddZdejdejfddZ			d!dejdB dejdedB dejdB dejdB dejfdd Z  ZS )" RobertaForSequenceClassificationad  A model that uses Roberta to provide embedding functionalities.

    This class encapsulates the BertModel and provides an interface for
    embedding operations and customized pooling functions.

    Attributes:
        roberta: An instance of BertModel used for forward operations.
        _pooler: An instance of Pooler used for pooling operations.
    Tzembeddings.LayerNormlayerzattention.self.qkv_projzattention.output.densezattention.output.LayerNormzintermediate.densezoutput.densezoutput.LayerNorm)emb_lnlayersz
mixer.Wqkvzmixer.out_projnorm1zmlp.fc1zmlp.fc2norm2)orig_to_new_substrrX   rY   r[   rZ   c                   sr   t    |jj}|jjj| _|j| _t|t|dt	d| _
t|j| _|jj}|d us.J tj|| jd| _d S )Nbert)r[   rZ   re   )r   )r%   r&   rI   rL   r+   r"   rP   r   r   r    robertarH   r   r   r	   for_seq_clspooler)r7   r[   rZ   r!   r   r8   r:   r;   r&     s    
z)RobertaForSequenceClassification.__init__rj   c                 C   s   t | }|j|| jdS )Nrt   )r   rx   jina_to_vllm_mapper)r7   rj   r{   r:   r:   r;   rx   2  s   z-RobertaForSequenceClassification.load_weightsr<   r>   c                 C   s   | j |S r?   )r   embed_input_ids)r7   r<   r:   r:   r;   r   6  s   z0RobertaForSequenceClassification.embed_input_idsNr]   r^   r=   r@   c                 C   sV   t ||| jd |d ur"| jjjdt> k sJ |d usJ t|| | j||||dS )Nr_   r   r`   )ra   r"   r   r!   r)   r   r   )r7   r<   r]   r^   r=   r@   r:   r:   r;   rB   9  s   
z(RobertaForSequenceClassification.forward)NNN)rC   rD   rE   rT   is_pooling_modelr   r   r   r|   r&   r   r}   r4   rF   rx   r   r   rB   rG   r:   r:   r8   r;   r     sB    
r   r<   r$   r"   c                 C   s   ||d 7 }d S )Nr   r:   r_   r:   r:   r;   ra   P  s   
ra   );r   collections.abcr   r4   r   transformersr   vllm.configr   r   r   !vllm.model_executor.layers.poolerr   r	   r
   )vllm.model_executor.layers.pooler.seqwiser   )vllm.model_executor.layers.pooler.tokwiser   r   r   3vllm.model_executor.layers.vocab_parallel_embeddingr   /vllm.model_executor.model_loader.default_loaderr   -vllm.model_executor.model_loader.weight_utilsr   vllm.model_executor.models.bertr   r   r   r   r    vllm.model_executor.models.utilsr   r   r   vllm.sequencer   bert_with_roper   r   
interfacesr   interfaces_baser   Moduler    rH   rW   r}   r|   rF   rv   r   r   r   intra   r:   r:   r:   r;   <module>   sR   +8&
NL