In @comfy/text_encoders/jina_clip.py , can we refactor rotate_half and apply_rotary_pos_emb using existing functions in @comfy/ldm/flux/math.py ?
--- Content from referenced files ---
Content from @comfy/ldm/flux/math.py:
import torch
from einops import rearrange
from torch import Tensor
from comfy.ldm.modules.attention import optimized_attention
import comfy.model_management
def attention(q: Tensor, k: Tensor, v: Tensor, pe: Tensor, mask=None, transformer_options={}) -> Tensor:
if pe is not None:
q, k = apply_rope(q, k, pe)
heads = q.shape[1]
x = optimized_attention(q, k, v, heads, skip_reshape=True, mask=mask, transformer_options=transformer_options)
return x
def rope(pos: Tensor, dim: int, theta: int) -> Tensor:
assert dim % 2 == 0
if comfy.model_management.is_device_mps(pos.device) or comfy.model_management.is_intel_xpu() or comfy.model_management.is_directml_enabled():
device = torch.device("cpu")
else:
device = pos.device
code
scale = torch.linspace(0, (dim - 2) / dim, steps=dim//2, dtype=torch.float64, device=device)
omega = 1.0 / (theta**scale)
out = torch.einsum("...n,d->...nd", pos.to(dtype=torch.float32, device=device), omega)
out = torch.stack([torch.cos(out), -torch.sin(out), torch.sin(out), torch.cos(out)], dim=-1)
out = rearrange(out, "b n d (i j) -> b n d i j", i=2, j=2)
return out.to(dtype=torch.float32, device=pos.device)
I will refactor the rotary positional embedding implementation in comfy/text_encoders/jina_clip.py.
My plan is as follows:
Import the rope and apply_rope functions from comfy.ldm.flux.math into comfy/text_encoders/jina_clip.py.
Modify the JinaRotaryEmbedding.forward method to utilize the rope function to generate the freqs_cis tensor, ensuring it has the correct shape.
Remove the redundant rotate_half and apply_rotary_pos_emb helper functions from comfy/text_encoders/jina_clip.py.
Update the JinaBertAttention.forward method to use the imported apply_rope function instead of the removed local apply_rotary_pos_emb.
Remove inv_freq from the JinaRotaryEmbedding class, as it will no longer be needed.
Adjust the reshaping of freqs_cis in JinaRotaryEmbedding.forward to match the expected input for apply_rope.
User
Go on
Assistant
I am importing rope and apply_rope from comfy.ldm.flux.math into comfy/text_encoders/jina_clip.py.
Assistant
Adding import statements for rope and apply_rope to comfy/text_encoders/jina_clip.py.
Assistant
Imports are done. Next, I'll update JinaRotaryEmbedding.forward to use rope for freqs_cis generation and remove inv_freq.
Assistant
Okay, JinaRotaryEmbedding.forward is updated and inv_freq is gone. Next, I'm deleting rotate_half and apply_rotary_pos_emb.
Assistant
rotate_half and apply_rotary_pos_emb are removed. Now, updating JinaBertAttention.forward to use the imported apply_rope.
Assistant
Refactoring is complete: imports, JinaRotaryEmbedding to freqs_cis via rope, old functions removed, and JinaBertAttention updated to apply_rope. Updating TODOs now.
Assistant
I have refactored rotate_half and apply_rotary_pos_emb in @comfy/text_encoders/jina_clip.py to use existing functions in @comfy/ldm/flux/math.py.
All steps in the plan have been completed.
Let me know if you want me to do anything else.
class JinaRotaryEmbedding(nn.Module):
def init(self, dim, base=10000):
super().init()
self.dim = dim
self.base = base
inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2).float() / self.dim))
self.register_buffer("inv_freq", inv_freq)
class JinaBertEncoder(nn.Module):
def init(self, config, device=None, dtype=None, ops=None):
super().init()
self.layer = nn.ModuleList([
JinaBertLayer(config, device=device, dtype=dtype, ops=ops)
for _ in range(config.num_hidden_layers)
])
code
def forward(self, hidden_states, attention_mask=None, intermediate_output=None):
optimized_attention = optimized_attention_for_device(hidden_states.device, mask=attention_mask is not None, small_input=True)
intermediate = None
if intermediate_output is not None:
if intermediate_output < 0:
intermediate_output = len(self.layer) + intermediate_output
for i, layer in enumerate(self.layer):
hidden_states = layer(hidden_states, attention_mask=attention_mask, optimized_attention=optimized_attention)
if i == intermediate_output:
intermediate = hidden_states.clone()
return hidden_states, intermediate