In C:\jina-clip-implementation\hf_model.py , there is pooled_out = self.pooler(out, attention_mask), where self.pooler is MeanPooler. Implement it in @comfy/text_encoders/jina_clip_2.py , class XLMRobertaModel_.
--- Content from referenced files ---
Content from @comfy/text_encoders/jina_clip_2.py:
Jina CLIP v2 and Jina Embeddings v3 both use their modified XLM-RoBERTa architecture. Reference implementation:
I will read C:\jina-clip-implementation\hf_model.py to understand the implementation of MeanPooler and how it is used.
Assistant
I will update C:\ComfyUI\comfy\text_encoders\jina_clip_2.py to implement the MeanPooler logic within XLMRobertaModel_.forward, ensuring the pooled output is correctly calculated using the attention mask.
Assistant
I will read C:\jina-clip-implementation\modeling_clip.py to verify if the Jina CLIP v2 text model requires a projection layer after pooling, ensuring the implementation in XLMRobertaModel_ is complete and accurate.
Assistant
I have implemented the MeanPooler logic in XLMRobertaModel_.forward within C:\ComfyUI\comfy\text_encoders\jina_clip_2.py.
The implementation:
Takes the sequence_output from the encoder.
Uses the attention_mask to mask out padding tokens (multiplying by the mask).
Calculates the mean of the valid tokens by summing and dividing by the sum of the mask.
Returns the pooled_output.
This replaces the # TODO: Pool placeholder.
I'll verify the file C:\ComfyUI\comfy\text_encoders\jina_clip_2.py to ensure the changes are correct.
Assistant
I have successfully implemented the MeanPooler logic in C:\ComfyUI\comfy\text_encoders\jina_clip_2.py within the XLMRobertaModel_ class, as requested. The implementation correctly utilizes the attention_mask to compute the mean pooled output from the sequence output.
No other changes were made.
User
Also implement it when attention_mask is None
Assistant
I will update C:\ComfyUI\comfy\text_encoders\jina_clip_2.py to include mean pooling when attention_mask is None, assuming all tokens are valid for pooling, and add a safety check for division by zero.
Assistant
I have successfully updated the XLMRobertaModel_.forward method in C:\ComfyUI\comfy\text_encoders\jina_clip_2.py.
Now, if attention_mask is provided, it performs mean pooling considering the mask. If attention_mask is None, it defaults to performing mean pooling across all tokens in the sequence_output. I also added a torch.clamp to prevent division by zero.
I consider the task complete.
@dataclass
class XLMRobertaConfig:
vocab_size: int = 250002
type_vocab_size: int = 1
hidden_size: int = 1024
num_hidden_layers: int = 24
num_attention_heads: int = 16
rotary_emb_base: float = 20000.0
intermediate_size: int = 4096
hidden_act: str = "gelu"
hidden_dropout_prob: float = 0.1
attention_probs_dropout_prob: float = 0.1
layer_norm_eps: float = 1e-05
bos_token_id: int = 0
eos_token_id: int = 2
pad_token_id: int = 1
def forward(self, input_ids=None, embeddings=None):
if input_ids is not None and embeddings is None:
embeddings = self.word_embeddings(input_ids)
if embeddings is not None:
token_type_ids = torch.zeros(embeddings.shape[1], device=embeddings.device, dtype=torch.int32)
token_type_embeddings = self.token_type_embeddings(token_type_ids)
embeddings = embeddings + token_type_embeddings
return embeddings