def cal_attention(self, x, quad_q, lin_q, quad_k, lin_k, v, u, mask=None):
        """
        Computes attention using quadratic and linear mechanisms.

        Args:
            x (Tensor): Input tensor of shape (batch, sequence length, dim).
            quad_q (Tensor): Quadratic query.
            lin_q (Tensor): Linear query.
            quad_k (Tensor): Quadratic key.
            lin_k (Tensor): Linear key.
            v (Tensor): Value tensor.
            u (Tensor): Gating tensor.
            mask (Tensor, optional): Attention mask. Default is None.

        Returns:
            Tuple[Tensor]: Attention outputs for value and gating.
        """
        b, n, device, g = x.shape[0], x.shape[-2], x.device, self.group_size

        # Mask for linear attention (if provided)
        if exists(mask):
            lin_mask = rearrange(mask, '... -> ... 1')
            lin_k = lin_k.masked_fill(~lin_mask, 0.)

        # Rotary embeddings for queries and keys (if provided)
        if exists(self.rotary_pos_emb):
            quad_q, lin_q, quad_k, lin_k = map(self.rotary_pos_emb.rotate_queries_or_keys, (quad_q, lin_q, quad_k, lin_k))

        # Padding to match group size
        padding = padding_to_multiple_of(n, g)
        if padding > 0:
            quad_q, quad_k, lin_q, lin_k, v, u = map(lambda t: F.pad(t, (0, 0, 0, padding), value=0.), (quad_q, quad_k, lin_q, lin_k, v, u))

            mask = default(mask, torch.ones((b, n), device=device, dtype=torch.bool))
            mask = F.pad(mask, (0, padding), value=False)

        # Group inputs along sequence
        quad_q, quad_k, lin_q, lin_k, v, u = map(lambda t: rearrange(t, 'b (g n) d -> b g n d', n=self.group_size), (quad_q, quad_k, lin_q, lin_k, v, u))

        if exists(mask):
            mask = rearrange(mask, 'b (g j) -> b g 1 j', j=g)

        # Quadratic attention
        sim = einsum('... i d, ... j d -> ... i j', quad_q, quad_k) / g
        attn = F.relu(sim) ** 2
        attn = self.dropout(attn)

        if exists(mask):
            attn = attn.masked_fill(~mask, 0.)

        if self.causal:
            causal_mask = torch.ones((g, g), dtype=torch.bool, device=device).triu(1)
            attn = attn.masked_fill(causal_mask, 0.)

        quad_out_v = einsum('... i j, ... j d -> ... i d', attn, v)
        quad_out_u = einsum('... i j, ... j d -> ... i d', attn, u)

        # Linear attention (with cumulative sum for causal mode)
        if self.causal:
            lin_kv = einsum('b g n d, b g n e -> b g d e', lin_k, v) / g
            lin_kv = lin_kv.cumsum(dim=1)
            lin_kv = F.pad(lin_kv, (0, 0, 0, 0, 1, -1), value=0.)
            lin_out_v = einsum('b g d e, b g n d -> b g n e', lin_kv, lin_q)

            lin_ku = einsum('b g n d, b g n e -> b g d e', lin_k, u) / g
            lin_ku = lin_ku.cumsum(dim=1)
            lin_ku = F.pad(lin_ku, (0, 0, 0, 0, 1, -1), value=0.)
            lin_out_u = einsum('b g d e, b g n d -> b g n e', lin_ku, lin_q)
        else:
            lin_kv = einsum('b g n d, b g n e -> b d e', lin_k, v) / n
            lin_out_v = einsum('b g n d, b d e -> b g n e', lin_q, lin_kv)

            lin_ku = einsum('b g n d, b g n e -> b d e', lin_k, u) / n
            lin_out_u = einsum('b g n d, b d e -> b g n e', lin_q, lin_ku)

        # Fold groups back into full sequence
        return map(lambda t: rearrange(t, 'b g n d -> b (g n) d')[:, :n], (quad_out_v + lin_out_v, quad_out_u + lin_out_u))

        Computes attention using quadratic and linear mechanisms.

        Args:
            x (Tensor): Input tensor of shape (batch, sequence length, dim).
            quad_q (Tensor): Quadratic query.
            lin_q (Tensor): Linear query.
            quad_k (Tensor): Quadratic key.
            lin_k (Tensor): Linear key.
            v (Tensor): Value tensor.
            u (Tensor): Gating tensor.
            mask (Tensor, optional): Attention mask. Default is None.

        Returns:
            Tuple[Tensor]: Attention outputs for value and gating.
        
clearvoice/clearvoice/models/mossformer2_ss/mossformer2_block.py