You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[bug] libtorch bug in nn::MultiheadAttention and nn::Transformer #80494
module: correctness (silent)issue that returns an incorrect result silentlymodule: cppRelated to C++ APImodule: nnRelated to torch.nntriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate modulewon't fixValid issue but solving it would be too complex for the benefit
Hey there @walkacross! Thanks for bringing this up. Responding to your points:
This is a problem and I will look to repro and fix this week.
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.
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
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.
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.
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.
module: correctness (silent)issue that returns an incorrect result silentlymodule: cppRelated to C++ APImodule: nnRelated to torch.nntriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate modulewon't fixValid issue but solving it would be too complex for the benefit
Activity
erichan1 commentedon Jul 6, 2022
Hey there @walkacross! Thanks for bringing this up. Responding to your points:
walkacross commentedon Jul 7, 2022
@erichan1 thanks for your response, have a good day.
walkacross commentedon Apr 11, 2023
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
albanD commentedon Apr 11, 2023
Hi,
I'm afraid the c++ API does not have a maintainer at the moment. So development in that area is quite slow.
walkacross commentedon Apr 12, 2023
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.
albanD commentedon Apr 12, 2023
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.
walkacross commentedon Apr 12, 2023
got it, anyway, thanks for your feedback
egeboy35 commentedon Oct 17, 2026
@drisspg
drisspg commentedon Oct 17, 2026
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