Skip to content

fix(rknpu2): improve NPU stability with error propagation and IOVA fragmentation fixes - #3

Merged
KHAEntertainment merged 4 commits into
rknpu2from
qwen3-support
Mar 23, 2026
Merged

fix(rknpu2): improve NPU stability with error propagation and IOVA fragmentation fixes#3
KHAEntertainment merged 4 commits into
rknpu2from
qwen3-support

Conversation

@KHAEntertainment

Copy link
Copy Markdown
Owner

Summary

Implements TraycerAI's suggested improvements from issue #2 plus an IOVA fragmentation fix for large models (>9B parameters) on RK3588.

Changes

Error Handling Improvements:

  • rknn_matmul_run error propagation - errors now properly fail instead of silently continuing
  • Memory context failure logging - NPU backend will log clear errors when initialization fails
  • B-matrix memcpy guard assertion - prevents buffer overflow in debug builds

Environment Variables:

  • RKNN_DEVICE - Select device (e.g., "RK3588")
  • RKNN_CORE_MASK - NPU core affinity ("0", "1", "2", "auto")
  • RKNN_SPLIT_FACTOR - Split B-matrix into smaller segments to reduce IOVA allocation size

IOVA Fragmentation Fix:
The RK3588 NPU uses IOMMU for address translation. Large models like Qwen 9B require ~11-17MB contiguous IOVA allocations which fail when other peripherals (RGA, VPU, JPEG) have fragmented the address space.

RKNN_SPLIT_FACTOR=N splits each core's segments into N pieces, reducing the max contiguous allocation by Nx while maintaining parallelization via round-robin core assignment.

Test Commands

# Test with split factor for large models
RKNN_SPLIT_FACTOR=4 ./build/bin/llama-cli -m Qwen3.5-9B-Q8_0.gguf -p 'Hello' -n 64

# Use specific NPU core
RKNN_CORE_MASK=1 ./build/bin/llama-cli -m model.gguf

Commits

  • 36132380a fix(rknpu2): implement TraycerAI suggested improvements
  • bf93a5b01 fix(rknpu2): add RKNN_SPLIT_FACTOR to reduce IOVA allocation size
  • 7c8e767d6 docs: document RKNN environment variables and IOVA exhaustion fix

Test plan

  • Rebuild on Rock 5C: cmake --build build
  • Test Qwen 9B with RKNN_SPLIT_FACTOR=4
  • Verify no IOVA errors in dmesg
  • Test RKNN_CORE_MASK=1 for single-core execution

🤖 Generated with Claude Code

KHAEntertainment and others added 4 commits March 18, 2026 23:21
…ment

- Fixed get_matmul_ctx() to properly map core_id to correct RKNN_NPU_CORE mask
- core_id 0 -> RKNN_NPU_CORE_0 (mask 1)
- core_id 1 -> RKNN_NPU_CORE_1 (mask 2)
- core_id 2 -> RKNN_NPU_CORE_2 (mask 4)
- This enables parallel matrix segmentation across all 3 NPU cores

Docs:
- Add multi-core NPU support note to README
- Add OS compatibility warning for RadxaOS kernel issues
- Recommend Armbian for stable NPU driver support

Fixes: EFAULT errors from spinlock recursion bug in RKNPU 0.9.x

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add rknn_matmul_run error propagation
- Add RKNN_CORE_MASK environment variable for core selection
- Add RKNN_DEVICE environment variable for device selection
- Add memory context failure logging
- Add B-matrix memcpy guard assertion

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Split B-matrix into num_cores * split_factor segments
- Round-robin core assignment (i % num_cores) preserves parallelization
- Reduces max contiguous IOVA requirement per segment
- Helps avoid ENOMEM/"Bad address" errors on fragmented CMA

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add RKNN_DEVICE, RKNN_CORE_MASK, RKNN_SPLIT_FACTOR to README
- Update IOVA_EXHAUSTION.md with solution

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Mar 19, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@KHAEntertainment has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 1 minutes and 9 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3eca7cf1-730f-4424-99f7-30350ffb2c2b

📥 Commits

Reviewing files that changed from the base of the PR and between 2806dc3 and 7c8e767.

📒 Files selected for processing (4)
  • README.md
  • docs/IOVA_EXHAUSTION.md
  • ggml/src/ggml-rknpu2/README.md
  • ggml/src/ggml-rknpu2/ggml-rknpu2.cpp
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch qwen3-support
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot added documentation Improvements or additions to documentation ggml labels Mar 19, 2026
@KHAEntertainment
KHAEntertainment merged commit 9577d6d into rknpu2 Mar 23, 2026
34 of 48 checks passed
@KHAEntertainment
KHAEntertainment deleted the qwen3-support branch March 23, 2026 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation ggml

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant