pytorch - 💡(How to fix) Fix [ONNX Export] TransformerEncoderLayer export fails when MHA fastpath is enabled [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#177013Fetched 2026-04-08 00:22:53
View on GitHub
Comments
2
Participants
2
Timeline
16
Reactions
0
Author
Timeline (top)
labeled ×5commented ×2mentioned ×2project_v2_item_status_changed ×2

Error Message

UnsupportedOperatorError: Exporting the operator 'aten::_transformer_encoder_layer_fwd' to ONNX opset version 17 is not supported. Please feel free to request support or submit a pull request on PyTorch GitHub: https://github.com/pytorch/pytorch/issues.

Code Example

import torch
from torch.nn import TransformerEncoderLayer
class Model(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.encoder_layer = TransformerEncoderLayer(
            d_model=512,
            dim_feedforward=2048, 
            nhead=8, 
            norm_first=True,
            batch_first=True
        )

    def forward(self, x):
        return self.encoder_layer(x)

model = Model()
model.eval()
# Without the following line, export may fail
# torch.backends.mha.set_fastpath_enabled(False)
dummy_input = torch.randn(10, 32, 512)  # (S, N, E)
with torch.no_grad():
    torch.onnx.export(
        model,
        dummy_input,
        "encoder.onnx",
        opset_version=17
    )

---

UnsupportedOperatorError: Exporting the operator 'aten::_transformer_encoder_layer_fwd' to ONNX opset version 17 is not supported. Please feel free to request support or submit a pull request on PyTorch GitHub: https://github.com/pytorch/pytorch/issues.

---

torch.backends.mha.set_fastpath_enabled(False)
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Problem

PyTorch's TransformerEncoderLayer fails ONNX export. The following script will produce errors.

import torch
from torch.nn import TransformerEncoderLayer
class Model(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.encoder_layer = TransformerEncoderLayer(
            d_model=512,
            dim_feedforward=2048, 
            nhead=8, 
            norm_first=True,
            batch_first=True
        )

    def forward(self, x):
        return self.encoder_layer(x)

model = Model()
model.eval()
# Without the following line, export may fail
# torch.backends.mha.set_fastpath_enabled(False)
dummy_input = torch.randn(10, 32, 512)  # (S, N, E)
with torch.no_grad():
    torch.onnx.export(
        model,
        dummy_input,
        "encoder.onnx",
        opset_version=17
    )

Error message

UnsupportedOperatorError: Exporting the operator 'aten::_transformer_encoder_layer_fwd' to ONNX opset version 17 is not supported. Please feel free to request support or submit a pull request on PyTorch GitHub: https://github.com/pytorch/pytorch/issues.

Best Practices

Try disable fastpath when export TransformerEncoderLayer to onnx following the below code

torch.backends.mha.set_fastpath_enabled(False)

Versions

Collecting environment information... PyTorch version: 2.5.0+cu124 Is debug build: False CUDA used to build PyTorch: 12.4 ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.3 LTS (x86_64) GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0 Clang version: Could not collect CMake version: version 3.16.3 Libc version: glibc-2.31

Python version: 3.12.9 (main, Mar 17 2025, 21:01:58) [Clang 20.1.0 ] (64-bit runtime) Python platform: Linux-5.11.0-27-generic-x86_64-with-glibc2.31 Is CUDA available: True CUDA runtime version: 12.0.76 CUDA_MODULE_LOADING set to: LAZY GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090 Nvidia driver version: 525.60.13 cuDNN version: Probably one of the following: /usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.7 /usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.9.7 Is XPU available: False HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True Caching allocator config: N/A

The default behavior of TransformerEncoderLayer cannot support onnx export. Should we add more informations for user when export the onnx?

cc @justinchuby @titaiwangms

extent analysis

Fix Plan

Disable Fastpath for ONNX Export

The issue is caused by the fastpath being enabled by default in PyTorch. To fix this, you need to disable the fastpath before exporting the model to ONNX.

Code Changes

import torch
from torch.nn import TransformerEncoderLayer
class Model(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.encoder_layer = TransformerEncoderLayer(
            d_model=512,
            dim_feedforward=2048, 
            nhead=8, 
            norm_first=True,
            batch_first=True
        )

    def forward(self, x):
        return self.encoder_layer(x)

model = Model()
model.eval()
# Disable fastpath for ONNX export
torch.backends.mha.set_fastpath_enabled(False)
dummy_input = torch.randn(10, 32, 512)  # (S, N, E)
with torch.no_grad():
    torch.onnx.export(
        model,
        dummy_input,
        "encoder.onnx",
        opset_version=17
    )

Verification

  1. Run the code with the fastpath disabled.
  2. Check if the ONNX export is successful without any errors.
  3. Verify that the exported ONNX model can be loaded and run without issues.

Extra Tips

  • Make sure to enable the fastpath again after the ONNX export is complete to avoid any potential performance issues.
  • Consider adding a check to disable the fastpath only when exporting the model to ONNX, and re-enable it otherwise.
  • If you're using a specific version of PyTorch that doesn't support disabling the fastpath, consider upgrading to a newer version that supports this feature.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - 💡(How to fix) Fix [ONNX Export] TransformerEncoderLayer export fails when MHA fastpath is enabled [2 comments, 2 participants]