Fix CPU LSTM input validation and refactor ValidateInputs to frontends - #28653
Closed
Youngsik Yang (vacu9708) wants to merge 2 commits into
Closed
Youngsik Yang (vacu9708) wants to merge 2 commits into
Youngsik Yang (vacu9708) wants to merge 2 commits into
Conversation
Author
|
@microsoft-github-policy-service agree |
ONNX shape inference (RNNShapeInference) explicitly validates X rank = 3,
but does not validate W or R shapes. The CPU LSTM path had several gaps:
Changes:
- Move ValidateInputs() call from LSTMBase::ComputeImpl() to each frontend
since W's third dimension must equal X's input_size at runtime.
- Compress ValidateInputs() declaration to eliminate
redundant input fetching at call sites.
- Reorder X rank check in ValidateInputs() to run before any dimension reads.
- Add W and R shape validation in DeepCpuLstmOp::Compute() and
DynamicQuantizeLSTM::Compute() before entering the compute pipeline.
- Note: DynamicQuantizeLSTM uses transposed W layout [num_directions,
input_size, 4*hidden_size]
Tests:
- Add LSTMTest.InvalidInputShapes covering malformed W and R inputs. The X
rank sub-case is a regression test for ONNX shape inference behavior; its
expected error string comes from the ONNX library, not ORT kernel code.
- Add DynamicQuantLSTMTest.InvalidInputShapes covering malformed W and R
inputs for the quantized LSTM path, including the transposed W layout.
Youngsik Yang (vacu9708)
force-pushed
the
main
branch
from
May 23, 2026 15:33
86469de to
ad1de47
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR closes CPU-side validation gaps for LSTM kernels by moving common input validation to the frontend (Compute()), fixing an out-of-bounds dimension-read hazard in ValidateInputs(), and adding missing runtime shape validation for W and R (including the transposed layout used by DynamicQuantizeLSTM).
Changes:
- Refactor
LSTMBase::ValidateInputsto takeOpKernelContext&, fetch inputs internally, and guardXrank before reading dimensions. - Move
ValidateInputs(*context)calls into CPU LSTM frontends and add explicitW/Rshape checks inDeepCpuLstmOpandDynamicQuantizeLSTM. - Add negative tests covering invalid
W/Rranks and dimensions for both float and dynamic-quantized CPU paths.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
onnxruntime/core/providers/cpu/rnn/lstm_base.h |
Changes ValidateInputs API to accept OpKernelContext& for frontend-driven validation. |
onnxruntime/core/providers/cpu/rnn/lstm_base.cc |
Implements the refactor; fixes rank-check ordering to avoid OOB reads; removes validation from ComputeImpl. |
onnxruntime/core/providers/cpu/rnn/deep_cpu_lstm.cc |
Calls ValidateInputs up-front and adds missing W/R runtime shape validation for CPU LSTM. |
onnxruntime/contrib_ops/cpu/quantization/dynamic_quantize_lstm.cc |
Calls ValidateInputs up-front and adds W/R runtime shape validation for the transposed quantized layout. |
onnxruntime/test/providers/cpu/rnn/deep_cpu_lstm_op_test.cc |
Adds CPU LSTM invalid-shape tests (including an X-rank regression case). |
onnxruntime/test/contrib_ops/quantize_lstm_op_test.cc |
Adds DynamicQuantizeLSTM invalid-shape tests for W/R. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.
Summary
Follow-up to #27737, which fixed out-of-bounds shape reads in the CUDA LSTM path but explicitly deferred the CPU counterpart. This PR addresses CPU LSTM validation gaps.
Problems fixed
1. Structural: validation in the backend instead of the frontend
W's third dimension
input_sizemust equal X's third dimensioninput_sizeat runtime,Validating W therefore depends on X. Beyond this data dependency,
input validation is not the responsibility of the computation.
ComputeImpl()should receive already-validated inputs, not validate them itself.
In addition,
ValidateInputs()no longer receives six individual tensors from the caller.2. Bug: out-of-bounds dimension reads before rank check
In
ValidateInputs(),X.Shape()[0]andX.Shape()[1]were read before theNumDimensions() != 3guard:This mirrors the fix applied to the CUDA path in #27737.
3. Missing: CPU LSTM never validated W or R shapes
Neither
DeepCpuLstmOp::Compute()norDynamicQuantizeLSTM::Compute()validated W or R shapes.Changes
lstm_base.hValidateInputs()declaration:(OpKernelContext& context)lstm_base.ccValidateInputs()fetches inputs internally.2. Rank check reordered before dimension reads.
3. call removed from
ComputeImpldeep_cpu_lstm.ccValidateInputs(*context)+ W/R validation called at top ofCompute()dynamic_quantize_lstm.cc[num_directions, input_size, 4*hidden_size])deep_cpu_lstm_op_test.ccLSTMTest.InvalidInputShapes: wrong-rank W, wrong-dim W, wrong-rank R, wrong-dim R; plus X-rank regression test against ONNX shape inferencequantize_lstm_op_test.ccDynamicQuantLSTMTest.InvalidInputShapes: same four cases for the quantized path