Compiling torch.nn.MultiheadAttention with key_padding_mask fails in F. _canonical_mask
When compiling torch.nn.MultiheadAttention with a key_padding_mask, the compilation fails because the mask tensor gets converted from dtype=torch.bool to thunder.dtypes.bool8. F._canonical_mask then throws an exception.
From my explain output, it looks like multi_head_attention_forward has a registered thunder module, but the _canonical_mask check happens outside of that function.
Code snippet:
self.attn = torch.nn.MultiheadAttention(
embed_dim=latent_dim, num_heads=latent_dim // kv_dim, batch_first=True, dropout=dropout
)
...
key_padding_mask = ~attention_mask.bool() if attention_mask is not None else None
attn_out = self.attn(q, kv, kv, key_padding_mask=key_padding_mask)[0]
...
model = thunder.compile(model) # or model = thunder.jit(model)
Runtime messages:
Compiling model Backend tkagg is interactive backend. Turning interactive mode on. Found 22 distinct operations, of which 22 (100.0%) are supported Note 3 operators are automatically registered: _VariableFunctionsClass.conv_transpose1d of torch multi_head_attention_forward of torch.nn.functional TensorBase.bool
Stack Trace:
File "/home/wags/dev/lcm/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1762, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^
File "/home/wags/dev/lcm/.venv/lib/python3.12/site-packages/thunder/core/interpreter.py", line 6840, in _impl
return fn.__func__(fn.__self__, *args, **kwargs)
^^^^^^^^^^^^^^^^^^
File "/home/wags/dev/lcm/.venv/lib/python3.12/site-packages/torch/nn/modules/activation.py", line 1220, in forward
key_padding_mask = F._canonical_mask(
^^^^^^^^^^^^^^^^^^
File "/home/wags/dev/lcm/.venv/lib/python3.12/site-packages/torch/nn/functional.py", line 5957, in _canonical_mask
raise AssertionError(
^^^^^^^^^^^^^^^^^^
AssertionError: only bool and floating types of key_padding_mask are supported.
type is thunder.dtypes.bool8 at the time of the exception.
I tried a key_padding_mask as dtype=float
key_padding_mask = ~attention_mask.bool() if attention_mask is not None else None
if key_padding_mask is not None:
key_padding_mask = torch.zeros_like(key_padding_mask, dtype=q.dtype).masked_fill_(key_padding_mask, float("-inf"))
key_padding_mask.requires_grad = False
But that fails in backward_adaptor with:
if any(map(_is_differentiable, tree_flatten(inp_kwargs)[0])):
raise NotImplementedError(
f"Exception encountered when doing automatic registration for {torch_func} because there is keyword argument that requires gradient, please use manual registration."
)
Because key_padding_mask is a kwarg:
attn_output, attn_output_weights = F.multi_head_attention_forward(
...
key_padding_mask=key_padding_mask,
And float is considered differentiable:
def _is_differentiable(arg: Any) -> bool:
from torch._subclasses.fake_tensor import FakeTensor
if isinstance(arg, (torch.Tensor, FakeTensor, TensorProxy)):
return dtypes.is_inexact_dtype(to_dtype(arg.dtype))
return False