Fix CUDA BeamSearch fp16 score mapping and add regression test - #31640
Merged
Akshay Sonawane (apsonawane) merged 6 commits intoAug 7, 2026
Conversation
Use ToCudaType<T> in LaunchBeamSearchScoreCopy so MLFloat16 outputs map to half writes instead of float writes.
Cover optional scores output path for tiny_gpt2_beamsearch_fp16 and assert output[2] is float16 with expected rank/leading dims.
Akshay Sonawane (apsonawane)
requested review from
Tianlei Wu (tianleiwu)
and
a lite review from Copilot
August 4, 2026 21:42
Copilot started reviewing on behalf of
Akshay Sonawane (apsonawane)
August 4, 2026 21:42
View session
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes FP16 score output handling in the CUDA BeamSearch path by correcting the device-side output type mapping during score copying, and adds a regression test to validate FP16 scores output type/shape using a tiny GPT-2 beam search model.
Changes:
- Fix CUDA
LaunchBeamSearchScoreCopyto map the output pointer type using the template parameterT(instead of alwaysfloat) so FP16 scores are written with the correct element type. - Add a new BeamSearch test that runs the FP16 GPT-2 beam search model and validates the scores output tensor element type is
FLOAT16and the shape matches expectations.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| onnxruntime/contrib_ops/cuda/transformers/generation_cuda_impl.cu | Corrects the CUDA output type mapping used when converting/copying final beam scores into the output tensor. |
| onnxruntime/test/contrib_ops/beam_search_test.cc | Adds a regression test for FP16 scores output type/shape (CUDA-only execution). |
Suppressed comments (1)
onnxruntime/test/contrib_ops/beam_search_test.cc:290
- Inside the new CUDA-guarded block, the indentation is inconsistent (e.g.,
Ort::SessionOptionsis not indented under theif, and#ifdef USE_CUDAhas leading spaces). This reduces readability and deviates from the pattern used in the precedingGptBeamSearchFp16test.
if (enable_cuda) {
Ort::SessionOptions session_options;
#ifdef USE_CUDA
OrtCUDAProviderOptionsV2 cuda_options;
cuda_options.use_tf32 = false;
session_options.AppendExecutionProvider_CUDA_V2(cuda_options);
#endif
Akshay Sonawane (apsonawane)
enabled auto-merge (squash)
August 5, 2026 17:57
The new fp16 regression test was mixing int32_t and int64_t when checking the expected scores shape. Keep the shape math in int64_t to match the tensor shape API and avoid narrowing in the assertion. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Tianlei Wu (tianleiwu)
approved these changes
Aug 7, 2026
auto-merge was automatically disabled
August 7, 2026 16:31
Pull request was closed
Akshay Sonawane (apsonawane)
enabled auto-merge (squash)
August 7, 2026 16:32
Akshay Sonawane (apsonawane)
deleted the
msrc/cuda-beamsearch-scorecopy-fix
branch
August 7, 2026 18:18
This was referenced Aug 12, 2026
This was referenced Aug 17, 2026
Open
Closed
This was referenced Aug 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces improvements to the Beam Search implementation and its test coverage, specifically for FP16 (half-precision float) support. The main changes include a bug fix in the CUDA implementation and the addition of a new test to verify the output type and shape for FP16 scores.
CUDA implementation fix:
LaunchBeamSearchScoreCopyfrom always usingfloatto using the template parameterT, ensuring correct type handling for FP16 and other types. (onnxruntime/contrib_ops/cuda/transformers/generation_cuda_impl.cu)Test improvements:
GptBeamSearchFp16_ScoresOutputTypeAndShape, which runs the GPT-2 Beam Search model with FP16 outputs and verifies that the output tensor has the correct type (FLOAT16) and expected shape. (onnxruntime/test/contrib_ops/beam_search_test.cc)