[CMSIS-NN] Fix stateful execution and batch-major striding for CMSIS-NN LSTM#3564
Open
veblush wants to merge 1 commit into
Open
[CMSIS-NN] Fix stateful execution and batch-major striding for CMSIS-NN LSTM#3564veblush wants to merge 1 commit into
veblush wants to merge 1 commit into
Conversation
mansnils
pushed a commit
to ARM-software/CMSIS-NN
that referenced
this pull request
Jun 17, 2026
This PR fixes two critical issues in `arm_lstm_unidirectional_s8` and `s16` that prevent state persistence in streaming models and cause out-of-bounds reads during non-time-major inference. These issues are closely related to in tensorflow/tflite-micro#3564. Problem: - State Wiping: By default, `arm_lstm_unidirectional_*` unconditionally sets `hidden_in` to `NULL` and memsets `cell_state` to 0. This discards the `HiddenStateTensor` and `CellStateTensor` that TFLM relies on to persist state across `Invoke()` calls for streaming models. - Striding Bug: In the `time_major` = `false` block of `arm_lstm_unidirectional_*`, CMSIS-NN attempts to jump between batches by passing `batch_offset` = `params->time_steps` to `arm_nn_lstm_step_*`. However, `arm_nn_lstm_step_*` forwards this `batch_offset` to `arm_nn_vec_mat_mul_result_acc_s8_s16` for both the `data_in` and `hidden_in` pointers. Since the `hidden_state` buffer is contiguous (stride 1) and not strided like `data_in`, passing `batch_offset` = `params->time_steps` causes out-of-bounds reads on the hidden_in buffer at `timestep` t=0. Solution: - Adding a `hidden_state` pointer to `cmsis_nn_lstm_context`. - Forwarding this `hidden_state` as `hidden_in` when present, skipping the `cell_state` wiping if so. - Explicitly iterating over the `batch_size` in the `time_major` = `false` case when computing step sizes, which forces `batch_offset` = 1 and avoids the buggy out-of-bounds stride entirely while writing to the final memory buffer sequentially.
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.
Problem
The current CMSIS-NN LSTM wrapper uses arm_lstm_unidirectional_s8 and arm_lstm_unidirectional_s16. These CMSIS-NN functions are designed for stateless sequence evaluation: they explicitly wipe the cell state at t=0 and ignore any initial hidden state, returning only the sequence outputs.
This breaks TFLM's streaming/embedded ML workloads which rely on stateful LSTMs where the CellStateTensor and HiddenStateTensor persist as variable tensors across Invoke() calls.
Furthermore, CMSIS-NN's internal implementation for batch-major tensors (time_major=false with batch_size > 1) incorrectly jumps memory by time_steps, causing an out-of-bounds read on the contiguous hidden_state buffer.
Solution
BUG=N/A