Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the CPU RNN implementation to use onnxruntime::narrow<int>() instead of unchecked static_cast<int>() when passing shape-derived values into APIs that take int, aiming to prevent silent truncation/overflow.
Changes:
- Add an explicit include for
core/common/narrow.hinrnn.cc. - Replace several
static_cast<int>(...)conversions withonnxruntime::narrow<int>(...)inCopyVectorandGemmcalls.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot apply changes based on the comments in this thread |
Agent-Logs-Url: https://github.com/microsoft/onnxruntime/sessions/4379d900-9734-4f26-9934-82047daceffc Co-authored-by: tianleiwu <30328909+tianleiwu@users.noreply.github.com>
Applied both changes from the review thread in commit 88cff57:
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Description
Fixes two overflow/underflow bugs in the CPU RNN kernel (
rnn.cc):SafeIntfor GEMM M-dimension:seq_length * batch_sizewas computed as a rawint64_tmultiply beforenarrow<int>(), meaning an overflow would be UB before the check could fire. Replaced withSafeInt<int64_t>(seq_length) * batch_sizefor a checked multiply.seq_length == 0guard inAssign_Y_h: For the forward direction,last_time_step = seq_length - 1underflows to-1whenseq_length == 0, producing a negativey_offsetand out-of-bounds read. Added an early-exit that zero-fills Y_h for the direction and returns. Also handlessequence_lens[batch] == 0(same underflow path), zeroing the affected batch slot and skipping viacontinue.Motivation and Context
Silent UB from integer overflow/underflow in shape-derived index arithmetic can corrupt memory or produce incorrect results without any diagnostic signal. These cases are legal per the ONNX spec (empty sequences, per-batch zero-length sequences) and must be handled explicitly.