ggml : fix SpaceMit IME array out-of-bounds in task assignment #16629
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.
Fix: SpaceMit IME backend array out-of-bounds access
Description
This PR fixes a critical bug in the SpaceMit IME (Intelligent Matrix Engine) backend that causes out-of-bounds array access during quantization phase, leading to undefined behavior and potential crashes.
Problem
Root Cause:
The task-to-batch index mapping calculation was incorrect. The code was dividing
compute_idxbyblock_size_minstead ofper_gemm_block_count_m, causing incorrectgemm_idxvalues that exceed the array bounds.Example scenario:
Impact:
qnbitgemm_args[gemm_idx]with invalid index reads uninitialized memory0x20451)gemm_mbeing a multiple of 4)Solution
Fix the task assignment calculation to properly map tasks to batches:
This ensures:
gemm_idxbatch_feature=1, all tasks map togemm_idx=0m_idxcorrectly ranges over the blocks within each batchTesting
Tested on SpaceMit K1 RISC-V64 board with:
gemm_m=30,batch_feature=10x20451Files Changed
ggml/src/ggml-cpu/spacemit/ime.cpp: Fix task-to-batch index calculation (lines 488-490)Related Issues
This bug was discovered while integrating the SpaceMit backend into Ollama, where the Go runtime's thread scheduling exposed the out-of-bounds access more readily than in llama.cpp's native threading model.
Verification: