Skip to content

Disable fused causal attention - #14732

Merged
tianleiwu merged 3 commits into
mainfrom
tlwu/disable_fused_causal_att
Feb 21, 2023
Merged

Disable fused causal attention#14732
tianleiwu merged 3 commits into
mainfrom
tlwu/disable_fused_causal_att

Conversation

@tianleiwu

@tianleiwu tianleiwu commented Feb 17, 2023

Copy link
Copy Markdown
Contributor

Description

There is accuracy regression in GPT-2 model. Top1 match rate (vs PyTorch model) drops about 1%. The cause is the fused causal attention uses fp16 accumulation. Disable it by default and user could use an environment variable ORT_ENABLE_FUSED_CAUSAL_ATTENTION=1 to turn on it manually.

It also updated the GPT-2 parity test script to generate left side padding to reflect the actual usage.

To test:

python -m onnxruntime.transformers.models.gpt2.convert_to_onnx -m gpt2 --output gpt2.onnx -o -p fp16 --use_gpu

The top1-match-rate in the output is on-par with ORT 1.13.1.

Motivation and Context

gh-yewang
gh-yewang previously approved these changes Feb 17, 2023
@yufenglee

Copy link
Copy Markdown
Member

I'm not sure it is a good idea to disable fMHA by defaut.
The accuracy check is based on dummy inputs which are meaningless. Intuitively, it tends to generate logits that are more neural to all tokens, i.e., no/less preference on next token. I would say it'd better to randomly select 1000 real(meaning) sentences as the test data set.
In addition, fMHA is only enabled for the context input, not for all iterations.

@tianleiwu

Copy link
Copy Markdown
Contributor Author

I'm not sure it is a good idea to disable fMHA by defaut. The accuracy check is based on dummy inputs which are meaningless. Intuitively, it tends to generate logits that are more neural to all tokens, i.e., no/less preference on next token. I would say it'd better to randomly select 1000 real(meaning) sentences as the test data set. In addition, fMHA is only enabled for the context input, not for all iterations.

Good suggestion. We will improve the test with real sentences, and re-evaluate this later.
Based on current test result, I think it is better to turn off it by default. Even though 1% drop is small, it is still regression on 1.13.

@tianleiwu
tianleiwu merged commit c0d2472 into main Feb 21, 2023
@tianleiwu
tianleiwu deleted the tlwu/disable_fused_causal_att branch February 21, 2023 17:53
PatriceVignola pushed a commit that referenced this pull request Feb 22, 2023
There is accuracy regression in GPT-2 model. Top1 match rate (vs PyTorch
model) drops about 1%. The cause is the fused causal attention uses fp16
accumulation. Disable it by default and add an environment variable 
ORT_ENABLE_FUSED_CAUSAL_ATTENTION=1 to turn on it manually.

It also updated the GPT-2 parity test script to generate left side
padding to reflect the actual usage.

To test:
```
python -m onnxruntime.transformers.models.gpt2.convert_to_onnx -m gpt2 --output gpt2.onnx -o -p fp16 --use_gpu
```
The top1-match-rate in the output is on-par with ORT 1.13.1.
PatriceVignola pushed a commit that referenced this pull request Feb 22, 2023
There is accuracy regression in GPT-2 model. Top1 match rate (vs PyTorch
model) drops about 1%. The cause is the fused causal attention uses fp16
accumulation. Disable it by default and add an environment variable 
ORT_ENABLE_FUSED_CAUSAL_ATTENTION=1 to turn on it manually.

It also updated the GPT-2 parity test script to generate left side
padding to reflect the actual usage.

To test:
```
python -m onnxruntime.transformers.models.gpt2.convert_to_onnx -m gpt2 --output gpt2.onnx -o -p fp16 --use_gpu
```
The top1-match-rate in the output is on-par with ORT 1.13.1.
kunal-vaishnavi pushed a commit that referenced this pull request Jun 19, 2026
### Description

This PR removes the TensorRT fused **causal** attention kernels (the
`fmha_v2_*_Causal_*` and `fmha_v2_flash_attention_*_Causal_*` cubins)
and all of the code paths that selected them from the CUDA `Attention`
operator.

These causal fused kernels were disabled by default (since
[#14732](#14732)) and were
only reachable via the opt-in `ORT_ENABLE_FUSED_CAUSAL_ATTENTION`
environment variable / `TRT_CAUSAL_ATTENTION` backend bit. They used
fp16 accumulation, which can cause accuracy drops, and have been
superseded by flash attention, memory-efficient attention, and cuDNN
SDPA. Removing them deletes ~1.27M lines of generated cubin source and
simplifies the attention dispatch logic.

### Key Changes

- **Removed cubin sources**: Deleted all `causal/fmha_v2_fp16_Causal_*`
and `flash_attention/fmha_v2_flash_attention_fp16_Causal_*` generated
cubin files (70+ files).
- **Dispatch simplification**
([attention.cc](onnxruntime/contrib_ops/cuda/bert/attention.cc)):
Removed the `is_unidirectional_` / causal fused-runner branch in
`ComputeInternal`; the fused runner path now only handles the BERT
(non-causal) case.
- **Kernel options**
([attention_kernel_options.cc](onnxruntime/contrib_ops/cuda/bert/attention_kernel_options.cc),
[attention_kernel_options.h](onnxruntime/contrib_ops/cuda/bert/attention_kernel_options.h)):
Removed `use_trt_causal_attention_`, `UseTrtCausalAttention()`, the
`TRT_CAUSAL_ATTENTION` debug print, and the `causal` argument of
`SetTrtFusedKernel`.
- **QKV format**
([attention_common.h](onnxruntime/contrib_ops/cpu/bert/attention_common.h),
[attention_prepare_qkv.cu](onnxruntime/contrib_ops/cuda/bert/attention_prepare_qkv.cu)):
Removed the `Q_K_V_BNSH_QKV_BS3NH` format and the fused-causal
gemm-buffer-with-bias preparation path.
- **Runner API**
([mha_runner.cu](onnxruntime/contrib_ops/cuda/bert/tensorrt_fused_multihead_attention/mha_runner.cu),
[mha_runner.h](onnxruntime/contrib_ops/cuda/bert/tensorrt_fused_multihead_attention/mha_runner.h),
[fused_multihead_attention_v2.h](onnxruntime/contrib_ops/cuda/bert/tensorrt_fused_multihead_attention/fused_multihead_attention_v2.h)):
Dropped the `causal` parameter from `FusedMHARunnerFP16v2::Create` /
`IsSupported` and removed the causal kernel metadata.
- **Env var removed**: `ORT_ENABLE_FUSED_CAUSAL_ATTENTION`
(`kEnableFusedCausalAttention`) is no longer recognized.
- **Callers updated**:
[multihead_attention.cc](onnxruntime/contrib_ops/cuda/bert/multihead_attention.cc),
[packed_attention.cc](onnxruntime/contrib_ops/cuda/bert/packed_attention.cc),
[packed_multihead_attention.cc](onnxruntime/contrib_ops/cuda/bert/packed_multihead_attention.cc),
[attention_impl.cu](onnxruntime/contrib_ops/cuda/bert/attention_impl.cu)
updated to the new no-causal signatures.
- **Python helpers**: Removed stale `ORT_ENABLE_FUSED_CAUSAL_ATTENTION`
references from the transformers benchmark helper and stable diffusion
benchmark.
- **Tests updated**:
[attention_op_test.cc](onnxruntime/test/contrib_ops/attention_op_test.cc)
and
[attention_kernel_options_test.cc](onnxruntime/test/providers/cuda/test_cases/attention_kernel_options_test.cc)
no longer set/assert the causal-fused option.

### Motivation and Context

The fused causal kernels were off by default, carried potential
fp16-accumulation accuracy risk, and added a large amount of generated
cubin source to the repo. Causal attention is already well covered by
flash attention, memory-efficient attention, and cuDNN SDPA, so these
kernels can be safely removed to reduce binary size and simplify
maintenance.

### Testing

- Build the CUDA EP and run the attention contrib op tests
(`ContribOpAttentionTest.*`, including `Causal_EmptyPastState`).
- Run `AttentionKernelOptionsTest.*` to verify the kernel-option parsing
no longer references the causal backend.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants