Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 27, 2026

Addresses review feedback that LabelAttentionClassifier was ignoring the attention mask, allowing padding tokens to influence label embeddings and attention visualizations.

Changes

  • Added attention_mask parameter to LabelAttentionClassifier.forward() and passed it from TextEmbedder._get_sentence_embedding()

  • Masked cross-attention computation: Convert mask from (1=real, 0=pad) to boolean format, expand to (B, 1, 1, T) for broadcasting, and pass to F.scaled_dot_product_attention as attn_mask

  • Masked attention matrix for explainability: Apply masked_fill(-inf) before softmax to zero out padding positions in attention weights

# Prepare mask for scaled_dot_product_attention
attn_mask = None
if attention_mask is not None:
    attn_mask = (attention_mask == 0)  # Convert: 0 (pad) -> True (mask out)
    attn_mask = attn_mask.unsqueeze(1).unsqueeze(2)  # (B, T) -> (B, 1, 1, T)

y = F.scaled_dot_product_attention(q, k, v, attn_mask=attn_mask, ...)

# For attention matrix computation
if attention_mask is not None:
    attention_scores = attention_scores.masked_fill(attn_mask, float('-inf'))
attention_matrix = torch.softmax(attention_scores, dim=-1)

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits January 27, 2026 09:50
Co-authored-by: meilame-tayebjee <114609737+meilame-tayebjee@users.noreply.github.com>
Co-authored-by: meilame-tayebjee <114609737+meilame-tayebjee@users.noreply.github.com>
Copilot AI changed the title [WIP] WIP to address feedback on cross attention label masking Implement attention masking in LabelAttentionClassifier cross-attention Jan 27, 2026
@meilame-tayebjee meilame-tayebjee marked this pull request as ready for review January 27, 2026 09:54
@meilame-tayebjee meilame-tayebjee merged commit 7a988c3 into 24-add-cross-attention-labels-text Jan 27, 2026
@meilame-tayebjee meilame-tayebjee deleted the copilot/sub-pr-60 branch January 27, 2026 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants