def forward(self, x, *, mask=None):
        """
        Forward pass for the block.

        Args:
            x (Tensor): Input tensor of shape (batch, sequence length, dim).
            mask (Tensor, optional): Attention mask. Default is None.

        Returns:
            Tensor: Output tensor after attention and feedforward operations.
        """

        # Save input as residual for skip connection
        residual = x

        # Optional token shifting
        if self.shift_tokens:
            x_shift, x_pass = x.chunk(2, dim=-1)
            x_shift = F.pad(x_shift, (0, 0, 1, -1), value=0.)  # Shift tokens
            x = torch.cat((x_shift, x_pass), dim=-1)

        # Projections for value and gating
        v, u = self.to_hidden(x).chunk(2, dim=-1)  # Split into two branches: v and u
        qk = self.to_qk(x)  # Query and key projections

        # Offset and scale for queries and keys
        quad_q, lin_q, quad_k, lin_k = self.qk_offset_scale(qk)

        # Calculate attention output
        att_v, att_u = self.cal_attention(x, quad_q, lin_q, quad_k, lin_k, v, u, mask)

        # Gated interaction between attention outputs and gating mechanism
        out = (att_u * v) * self.gateActivate(att_v * u)

        # Residual connection and output projection
        x = residual + self.to_out(out)
        return x

        Forward pass for the block.

        Args:
            x (Tensor): Input tensor of shape (batch, sequence length, dim).
            mask (Tensor, optional): Attention mask. Default is None.

        Returns:
            Tensor: Output tensor after attention and feedforward operations.
        
clearvoice/clearvoice/models/mossformer2_ss/mossformer2_block.py