Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
26 changes: 17 additions & 9 deletions src/transformers/masking_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .utils.import_utils import (
is_torch_flex_attn_available,
is_torch_greater_or_equal,
is_torchdynamo_exporting,
is_tracing,
)

Expand Down Expand Up @@ -233,6 +234,19 @@ def fast_all(tensor: torch.BoolTensor) -> torch.BoolTensor:
return tensor.sum() == tensor.numel()


def _cannot_decide_skip_while_tracing(padding_mask: torch.Tensor | None) -> bool:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name is not very descriptive - but anyway I don't think we need this function at all, see other comments

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

"""Whether the skip conditions cannot be evaluated because we are tracing.

Export hard-codes the decision into the exported forward, which is in general wrong (see
https://github.com/pytorch/pytorch/issues/108108). Reading the values of a `padding_mask` is
data-dependent. Without one, the remaining conditions are static and dynamo simply guards on them.

NOTE: until torch 2.14 (pytorch#176499), dynamo also reported exporting under `torch.compile`, so
older versions keep the previous behavior of never skipping while compiling.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is wrong or very badly phrased, it's actually the opposite - dynamo also reported compile under torch.export instead of only export

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring is gone with the helper. The note now sits at the call site.

"""
return is_torchdynamo_exporting() or (padding_mask is not None and is_tracing(padding_mask))


def _ignore_causal_mask_sdpa(
padding_mask: torch.Tensor | None,
q_length: int,
Expand All @@ -253,11 +267,7 @@ def _ignore_causal_mask_sdpa(
mask_indices = torch.arange(kv_length, device=padding_mask.device) + kv_offset
padding_mask = padding_mask[:, mask_indices]

# When using `torch.export` or `torch.onnx.dynamo_export`, we must pass an example input, and `is_causal` behavior is
# hard-coded to the forward. If a user exports a model with query_length > 1, the exported model will hard-code `is_causal=True`
# which is in general wrong (see https://github.com/pytorch/pytorch/issues/108108). Thus, we only set
# `ignore_causal_mask = True` if we are not tracing
Comment on lines -256 to -259

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to keep this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both restored verbatim.

if is_tracing(padding_mask):
if _cannot_decide_skip_while_tracing(padding_mask):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to only call is_exporting here no? I don't think the new helper is useful

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The helper is gone, the condition is inlined at the three call sites.
Calling only is_exporting() does not work, though. A compiled forward that does have a padding mask then reaches fast_all(padding_mask) and branches on the result:

torch._dynamo.exc.Unsupported: Data-dependent branching
  File "src/transformers/masking_utils.py", line 234, in fast_all, code: return tensor.sum() == tensor.numel()

So it reads is_torchdynamo_exporting() or (padding_mask is not None and is_tracing(padding_mask)): always decline under export, and decline under compile only when we would have to read the mask.

return False
# In this case, we need to add special patterns to the mask no matter what, so we cannot use any of the later skip conditions
if local_attention_size is not None and kv_length >= local_attention_size:
Expand Down Expand Up @@ -291,7 +301,7 @@ def _can_skip_bidirectional_mask_xpu(
- Skip if no padding and no local attention constraint
"""

if is_tracing(padding_mask):
if _cannot_decide_skip_while_tracing(padding_mask):
return False

# Check local attention constraint (same as CUDA)
Expand Down Expand Up @@ -324,10 +334,8 @@ def _ignore_bidirectional_mask_sdpa(
# - Skip if no padding and no local attention constraint
return _can_skip_bidirectional_mask_xpu(padding_mask, kv_length, local_attention_size)

# When using `torch.export` or `torch.onnx.dynamo_export`, we need to avoid to check the contents of the mask;
# otherwise, we will encounter dynamic control flows
Comment on lines -327 to -328

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to keep this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if (
not is_tracing(padding_mask)
not _cannot_decide_skip_while_tracing(padding_mask)
and (padding_mask is None or padding_mask.all())
# in this case we need to add special patterns to the mask so cannot be skipped otherwise
and (local_attention_size is None or kv_length < local_attention_size)
Expand Down
46 changes: 46 additions & 0 deletions tests/utils/test_masking_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from transformers.testing_utils import (
is_torch_available,
require_torch,
require_torch_greater_or_equal,
torch_device,
)

Expand Down Expand Up @@ -177,6 +178,51 @@ def test_nonpacked_sequence_mask_skip(self):
# cannot be skipped under compile, should result into a triu mask
self.assertTrue(torch.equal(~torch.ones(*causal_mask.shape).triu(diagonal=1).bool(), causal_mask))

# `torch.compiler.is_exporting()` was hard-coded to `True` by dynamo, i.e. it also reported exporting under
# `torch.compile`, until https://github.com/pytorch/pytorch/pull/176499 (torch 2.14). On older versions we keep
# the conservative behavior of never skipping while tracing, so the mask is still materialized.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong - it's the other way around
If torch.compiler.is_exporting() was hard-coded to True, it would fire all the time

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The True is not in the function, it is in dynamo. When dynamo traces a call to is_exporting(), it does not run it, it replaces the call with the constant True. Eager keeps calling the real function, which is why the mask was skipped in eager but not under compile. The comment now says "dynamo replaced calls to torch.compiler.is_exporting() by a constant True", which is hopefully less ambiguous than "hard-coded".

@require_torch_greater_or_equal("2.14")
def test_mask_skip_without_padding_mask_under_compile(self):
"""
Checks whether the mask creation can still be skipped under `torch.compile` if we have no padding mask at all.
Whether a padding mask is provided is a static property that dynamo guards on - only the checks reading its
values are data-dependent, and have to be skipped while tracing.
"""
config = LlamaConfig()
config._attn_implementation = "sdpa"

batch_size = 2
sequence_length = 10
inputs_embeds = torch.empty((batch_size, sequence_length, 8), dtype=torch.float16, device=torch_device)
padded_mask = torch.ones(batch_size, sequence_length, dtype=torch.long, device=torch_device)
padded_mask[0, :3] = 0

def create_masks(attention_mask):
causal_mask = create_causal_mask(
config=config,
inputs_embeds=inputs_embeds,
attention_mask=attention_mask,
past_key_values=None,
)
bidirectional_mask = create_bidirectional_mask(
config=config,
inputs_embeds=inputs_embeds,
attention_mask=attention_mask,
)
return causal_mask, bidirectional_mask

compiled_create_masks = torch.compile(create_masks, fullgraph=True)

# Without a padding mask, both masks are skipped in eager as well as under compile
self.assertEqual(create_masks(None), (None, None))
self.assertEqual(compiled_create_masks(None), (None, None))

# With a padding mask, the masks are materialized. Under compile, reading its values must be skipped
# instead of raising a data-dependent control flow error
for causal_mask, bidirectional_mask in (create_masks(padded_mask), compiled_create_masks(padded_mask)):
self.assertIsNotNone(causal_mask)
self.assertIsNotNone(bidirectional_mask)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why we loop here tbh, can just keep it simple as above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


def test_chunked_mask_with_left_padding_and_large_prefill(self):
# Make sure we have an attention_chunk_size in the config
config = LlamaConfig(attention_chunk_size=3, attn_implementation="sdpa")
Expand Down