Validate DynamicQuantizeLSTM recurrence quantization parameter shapes - #29254
Conversation
…er shapes The recurrence zero-point (R_zero_point) and scale (R_scale) inputs were not run through the kernel's shape validation: R_zp_shape was bound to w_zp->Shape() instead of r_zp->Shape(), and the R_scale WeightCheck was passed W_scale_shape instead of R_scale_shape. As a result a malformed r_zp/r_scale (e.g. a shape inconsistent with R) was never rejected and downstream code iterated using the input parameter's element count over the recurrence parameter's buffer. Bind R_zp_shape to r_zp->Shape() and validate R_scale against R_scale_shape so the recurrence parameters are checked symmetrically with the input (W) ones. Add two expect-failure tests that supply R_zero_point / R_scale shapes whose first dimension does not match num_directions and assert the specific INVALID_ARGUMENT diagnostics. Status-based validation, so the tests need no ORT_NO_EXCEPTIONS guard. CPU-only op; no CUDA implementation exists. Agent-signed-off: Developer (0b207188) [claude-opus-4.8 via copilot] Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in the CPU DynamicQuantizeLSTM kernel’s input validation where recurrence quantization parameters (R_scale/R_zero_point) were mistakenly validated using the input-weight (W) parameter shapes. It also adds unit tests to ensure malformed recurrence quantization parameter shapes are rejected with a clear error.
Changes:
- Correct
R_zero_pointshape validation to user_zp->Shape()(instead ofw_zp->Shape()). - Correct
R_scaleshape validation to useR_scale_shape(instead ofW_scale_shape). - Add two negative tests that expect failures for inconsistent
R_zero_point/R_scaleshapes.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
onnxruntime/contrib_ops/cpu/quantization/dynamic_quantize_lstm.cc |
Fixes the recurrence quantization-parameter shape checks to validate against the actual R_* tensor shapes. |
onnxruntime/test/contrib_ops/quantize_lstm_op_test.cc |
Adds expect-failure tests that confirm incorrect R_scale/R_zero_point shapes are rejected with the intended error message. |
|
Missing Tests
|
Add the maintainer-requested test coverage for the recurrence quantization-parameter validation: - Per-channel (2D) recurrence shape mismatch on the second dim (correct num_directions first dim, wrong 4*hidden_size second dim), exercising the per-channel branch of the shape check. - Positive mixed-granularity case (per-tensor W with per-channel R and vice versa) that runs to completion, confirming valid mixed configs are not falsely rejected. - Bidirectional (num_directions == 2) recurrence shape mismatch. - Per-channel recurrence zero-point with correct shape but non-constant values, exercising the per-element zero-point validation against R's own shape and values. Generalize RunQuantLSTM/ComputeRefOutput to accept independent W and R quantization granularity (the convenience wrapper preserves the existing symmetric behavior) and extend the expect-failure helper with num_directions and optional recurrence zero-point values. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
682936c
into
microsoft:main
Description
The
R_zero_point/R_scale(recurrence) quantization-parameter shape validation inDynamicQuantizeLSTMwas inadvertently checking theW(input) quantization parameters instead of theRones. This change validates theRparameters' own shapes symmetrically withW, so malformed recurrence quantization parameters are rejected with a clear error.Changes
R_zero_pointandR_scaleare validated against theRtensor's expected shape (previously bound to theWtensor).Motivation
Improves input validation and error diagnostics for malformed
DynamicQuantizeLSTMrecurrence quantization parameters. CPU-only; no behavior change for valid inputs.Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com