Skip to content

[bug] libtorch bug in nn::MultiheadAttention and nn::Transformer #80494

Open

Description

@walkacross

🐛 Describe the bug

the attn_mask does not work in nn::MultiheadAttention

#include <torch/torch.h>
namespace nn = torch::nn;

// seq_length x batch_size x feature_size
torch::Tensor x = torch::randn({3,1,4});

torch::Tensor attn_mask = nn::TransformerImpl::generate_square_subsequent_mask(3);
torch::Tensor attn_mask_bool = attn_mask.to(torch::kBool);

nn::MultiheadAttention multihead_attention(4,1);
std::tuple<torch::Tensor, torch::Tensor> output_with_attn_mask = multihead_attention ->forward(x,x,x,{},true,attn_mask_bool);

torch::Tensor attn_output, attn_output_weights;
std::tie(attn_output, attn_output_weights) = output_with_attn_mask; //unpacking tuple into variables

std::cout << attn_output_weights << std::endl;
attn_mask:
/*
 0  1  1
 0  0  1
 0  0  0
[ CPUBoolType{3,3} ]
*/


attn_output_weights:

(1,.,.) = 
  0.1918  0.5302  0.2780
  0.2074  0.1919  0.6007
  0.1948  0.5092  0.2960
[ CPUFloatType{1,3,3} ]

Versions

libtorch: 1.11.0+cu113

cc @jbschlosser @albanD @mruberry @walterddr @mikaylagawarecki @kshitij12345 @saketh-are @bhosmer @cpuhrsch @erichan1

Activity

  1. added
    module: cpp
    module: nn
    triaged
    module: correctness (silent)
    on Jun 29, 2022
  2. erichan1 commented on Jul 6, 2022

    @erichan1
    Contributor

    Hey there @walkacross! Thanks for bringing this up. Responding to your points:

    1. This is a problem and I will look to repro and fix this week.
    2. Unfortunately the cpp api is not as well developed as the python api and the lag will probably continue. We will do our best to add features like batch_first but cannot guarantee.
  3. walkacross commented on Jul 7, 2022

    @walkacross
    Author

    @erichan1 thanks for your response, have a good day.

  4. walkacross commented on Apr 11, 2023

    @walkacross
    Author

    It's been 9 months, and this bug still exists. why?? (everyone who use libtorch to inference experiences the wrong code)
    @ezyang @erichan1 @soumith @albanD @malfet

  5. albanD commented on Apr 11, 2023

    @albanD
    Collaborator

    Hi,

    I'm afraid the c++ API does not have a maintainer at the moment. So development in that area is quite slow.

  6. walkacross commented on Apr 12, 2023

    @walkacross
    Author

    hi @albanD, thanks for your kind response.

    this situation - c++ API does not have a maintainer at the moment - seems stay a long time, does pytorch team has any plan to change this situation? considering every version release involves in libtorch, but unfortunately, little improvement happens.

    Besides, nn::MultiheadAttention is a key part of Transformer model, but the code is wrong, it has affacted many researchers.

  7. albanD commented on Apr 12, 2023

    @albanD
    Collaborator

    We are very much open to onboarding a maintainer for the C++ API! Unfortunately, we don't have anyone interested at the moment.
    Also since the C++ API is not used as much as other features, we tend to focus more on these other features (while maintaining releases for libtorch of course). Broadly, we don't expect the C++ API to bring a lot of benefits compared to the python one.

  8. walkacross commented on Apr 12, 2023

    @walkacross
    Author

    got it, anyway, thanks for your feedback

  9. egeboy35 commented on Oct 17, 2026

    @egeboy35
    Contributor

    @drisspg

    Drafted by Claude (AI) for @egeboy35.

    This still reproduces on current main. In the C++ frontend's F::detail::multi_head_attention_forward (torch/csrc/api/include/torch/nn/functional/activation.h), attn_mask is applied with attn_output_weights += attn_mask_, so a bool mask adds 1.0 at the masked positions instead of -inf and has almost no effect. The Python version converts a bool mask to -inf first (F._canonical_mask), and key_padding_mask in the same C++ function already uses masked_fill(..., -inf).

    The fix would do that conversion for attn_mask before the bias_k concatenation, reject masks that are neither bool nor floating point as Python does, and add a test in test/cpp/api/modules.cpp that a bool mask gives the same output as the equivalent -inf float mask. Could this be labelled actionable? egeboy35 would send the PR.

  10. added
    won't fix
    on Oct 17, 2026
  11. drisspg commented on Oct 17, 2026

    @drisspg
    Contributor

    Hey marking wont fix; this code is frozen, I agree this is a real bug but we dont encourage people to use this code any more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: correctness (silent)module: cppmodule: nntriagedwon't fix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions