Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding alternate method to apply mask to allow XLA to detect MHA pattern #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion praxis/layers/attentions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,7 @@ class DotProductAttention(base_layer.BaseLayer):
decode_cache: bool = True
attention_mask_summary: bool = False
zero_fully_masked: bool = False
mha_mask_addition_pattern: bool = True
qk_einsum_tpl: LayerTpl = template_field(base_ops.EinsumOp)
pv_einsum_tpl: LayerTpl = template_field(base_ops.EinsumOp)

Expand Down Expand Up @@ -1342,8 +1343,14 @@ def _dot_atten(
logits = self._cap_logits(logits)
# Attention softmax is always carried out in fp32.
logits = logits.astype(jnp.float32)

# Apply attention masking
padded_logits = py_utils.apply_mask_to_logits(logits, atten_mask)
if self.mha_mask_addition_pattern:
padded_logits = logits + atten_mask.astype(jnp.float32)
else:
padded_logits = py_utils.apply_mask_to_logits(logits, atten_mask)


if self.attention_mask_summary:
self.add_summary('attention_mask', atten_mask)
if self.attention_extra_logit is None:
Expand Down