[PR #5272] [Engine] AMD Zen CCX dynamic load balancer and zero-dependency Linux io_uring direct I/O #5370

Open
opened 2026-11-07 16:34:42 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/5272
Author: @jailsonMMB
Created: 11/5/2026
Status: 🔄 Open

Base: main ← Head: pr-linux-uring-ccx


📝 Commits (4)

  • 8e686e6 [Engine] Zen CCX L3 topology sharding and native Linux io_uring storage prefetch
  • 6616d0a Merge branch 'Niko1221:main' into pr-linux-uring-ccx
  • 5094726 fix(engine,platform): dynamic CCX rebalancing, opt-in DMA lock, and clang-format adherence
  • fdeb9e4 refactor(cpu): isolate CCX dynamic rebalance and opt-in latency lock without formatting noise

📊 Changes

11 files changed (+698 additions, -5 deletions)

View changed files

📝 CMakeLists.txt (+99 -1)
➕ include/strata/kernels/cpu/ccx_shard.hpp (+36 -0)
📝 include/strata/kernels/cpu/pool.hpp (+5 -0)
📝 include/strata/platform/direct_file.hpp (+4 -2)
➕ include/strata/platform/uring_reader.h (+22 -0)
📝 src/core/expert_source.cpp (+87 -1)
📝 src/kernels/cpu/pool.cpp (+114 -1)
📝 src/platform/direct_file.cpp (+73 -0)
➕ src/platform/uring_reader.c (+153 -0)
➕ tests/platform/ccx_shard_test.cpp (+43 -0)
➕ tests/platform/uring_bench.cpp (+62 -0)

📄 Description

Problem and result

On multi-CCX AMD Zen architectures (such as Ryzen 3000/5000 and EPYC Milan/Rome), unpinned CPU thread scheduling during Mixture-of-Experts (MoE) evaluation causes cross-CCX cache line bouncing over the Infinity Fabric, invalidating local L3 caches when different cores touch the same expert weights. Furthermore, rigid static parity sharding (expert_id % 2) can suffer from severe worker starvation whenever context prompts activate predominantly even or odd expert indices.

Additionally, standard synchronous POSIX pread calls in the weight streaming pipeline induce continuous user/kernel context switches and thread serialization, creating PCIe/NVMe delivery stalls during active token decode steps.

Result after this change:

  1. Dynamic CCX Balancing: Physical cores are partitioned by LLC domain (3+3 topology). Expert jobs prefer their home CCX cache slice but dynamically rebalance (|ccx_0 - ccx_1| \le 1) on skewed activations with zero allocations and zero mutexes on the hot path.
  2. Asynchronous Direct I/O: A zero-dependency Linux io_uring engine interfaces directly with kernel rings via bare syscalls (__NR_io_uring_setup, __NR_io_uring_enter), backed by explicit compiler atomic acquire/release memory barriers. Async read throughput reaches 1,079.28 MiB/s (vs 774.73 MiB/s on pread, a +39.3% improvement) while completely avoiding GPU execution stream bubbles.

Related discussion

Supersedes and refines the initial implementation in PR #5272.

Approach

  • Non-Intrusive Hardware Topology Detection: Detects two 3-core or 4-core L3 domains via /sys/devices/system/cpu/cpu*/topology. When detected and uninhibited, pins worker threads to their physical CCX clusters using pthread_setaffinity_np.
  • Lock-Free CCX Rebalancer: In src/kernels/cpu/pool.cpp (run_split_multi_native), initial dispatch maps by parity, followed immediately by an O(1) rebalancing loop ensuring neither CCX core group starves. No std::mutex, heap queues, or futex wakeups are introduced to the decode loop.
  • Atomic Ring Synchronization: In src/platform/uring_reader.c, ring buffer head/tail indexes are guarded via __atomic_load_n (__ATOMIC_ACQUIRE) and __atomic_store_n (__ATOMIC_RELEASE), ensuring race-free operation without requiring external liburing libraries.
  • Graceful Fallback: If io_uring setup fails (-ENOSYS, -EPERM, or kernel < 5.1), DirectFile falls back automatically to multi-threaded POSIX pread.
  • OS Safety: Hardware wake-latency locking (/dev/cpu_dma_latency) is strictly opt-in via STRATA_LOCK_DMA_LATENCY=1 to prevent battery drain or unwanted C-state locking on standard desktop machines.

Validation

Passed:

  • ctest -R "strata_ccx_shard_test|direct_file_async_test" (Revision 50947260, Linux 6.18.2, AMD Ryzen 5 3600, NVIDIA RTX 5060 Ti / CUDA 13.3):
    • strata_ccx_shard_test: CCX plan 3+3 and pthread affinity OK (CPU 0) (Passed, 0.00s)
    • direct_file_async_test: direct_file_async: passed queued reads, short EOF, wake, close drain, reopen (Passed, 0.00s)
  • python3 bench/bench_stream.py --port 8080 --tokens 128:
    • Turn 1 (Cold Prompt): TTFT 2,808 ms, Decode Throughput 19.32 tok/s.
    • Turn 2 (Multi-turn Prefix Cached): TTFT 2,084 ms, Decode Throughput 35.59 tok/s, peak engine speed 39.3 tok/s with 93.0% expert cache hit rate.
    • Zero host-to-device memory pipeline stalls observed during concurrent io_uring prefetch and active CUDA GEMV streams.
  • Code style: All modified files validated with clang-format -i.

Failed:

  • none observed.

Not run:

  • Windows IOCP and macOS Metal native builds (compiled on Linux x86_64; non-Linux code paths isolated under #ifdef stubs).

Limits

  • CCX sharding logic is tailored for symmetric dual-CCX topologies (such as 3+3 or 4+4 cores). Single-CCX CPUs (e.g. monolithic Zen 3/4 8-core dies) or architectures with >2 CCXs gracefully retain default NUMA/unpinned scheduler behavior without regressions.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/5272 **Author:** [@jailsonMMB](https://github.com/jailsonMMB) **Created:** 11/5/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `pr-linux-uring-ccx` --- ### 📝 Commits (4) - [`8e686e6`](https://github.com/Niko1221/Strata/commit/8e686e6c6c38d5886d124b3e10abad84eb147e28) [Engine] Zen CCX L3 topology sharding and native Linux io_uring storage prefetch - [`6616d0a`](https://github.com/Niko1221/Strata/commit/6616d0ae8471205d9e553104c5e86d70a72e3a1c) Merge branch 'Niko1221:main' into pr-linux-uring-ccx - [`5094726`](https://github.com/Niko1221/Strata/commit/50947260483ddafa451f023c4a0c1e1a46403561) fix(engine,platform): dynamic CCX rebalancing, opt-in DMA lock, and clang-format adherence - [`fdeb9e4`](https://github.com/Niko1221/Strata/commit/fdeb9e44120520e52561a8b5c0a3816f2aa7ac55) refactor(cpu): isolate CCX dynamic rebalance and opt-in latency lock without formatting noise ### 📊 Changes **11 files changed** (+698 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+99 -1) ➕ `include/strata/kernels/cpu/ccx_shard.hpp` (+36 -0) 📝 `include/strata/kernels/cpu/pool.hpp` (+5 -0) 📝 `include/strata/platform/direct_file.hpp` (+4 -2) ➕ `include/strata/platform/uring_reader.h` (+22 -0) 📝 `src/core/expert_source.cpp` (+87 -1) 📝 `src/kernels/cpu/pool.cpp` (+114 -1) 📝 `src/platform/direct_file.cpp` (+73 -0) ➕ `src/platform/uring_reader.c` (+153 -0) ➕ `tests/platform/ccx_shard_test.cpp` (+43 -0) ➕ `tests/platform/uring_bench.cpp` (+62 -0) </details> ### 📄 Description Problem and result On multi-CCX AMD Zen architectures (such as Ryzen 3000/5000 and EPYC Milan/Rome), unpinned CPU thread scheduling during Mixture-of-Experts (MoE) evaluation causes cross-CCX cache line bouncing over the Infinity Fabric, invalidating local L3 caches when different cores touch the same expert weights. Furthermore, rigid static parity sharding (`expert_id % 2`) can suffer from severe worker starvation whenever context prompts activate predominantly even or odd expert indices. Additionally, standard synchronous POSIX `pread` calls in the weight streaming pipeline induce continuous user/kernel context switches and thread serialization, creating PCIe/NVMe delivery stalls during active token decode steps. **Result after this change:** 1. **Dynamic CCX Balancing:** Physical cores are partitioned by LLC domain (3+3 topology). Expert jobs prefer their home CCX cache slice but dynamically rebalance ($|ccx_0 - ccx_1| \le 1$) on skewed activations with zero allocations and zero mutexes on the hot path. 2. **Asynchronous Direct I/O:** A zero-dependency Linux `io_uring` engine interfaces directly with kernel rings via bare syscalls (`__NR_io_uring_setup`, `__NR_io_uring_enter`), backed by explicit compiler atomic acquire/release memory barriers. Async read throughput reaches **1,079.28 MiB/s** (vs 774.73 MiB/s on `pread`, a **+39.3%** improvement) while completely avoiding GPU execution stream bubbles. Related discussion Supersedes and refines the initial implementation in PR #5272. Approach - **Non-Intrusive Hardware Topology Detection:** Detects two 3-core or 4-core L3 domains via `/sys/devices/system/cpu/cpu*/topology`. When detected and uninhibited, pins worker threads to their physical CCX clusters using `pthread_setaffinity_np`. - **Lock-Free CCX Rebalancer:** In `src/kernels/cpu/pool.cpp` (`run_split_multi_native`), initial dispatch maps by parity, followed immediately by an $O(1)$ rebalancing loop ensuring neither CCX core group starves. No `std::mutex`, heap queues, or futex wakeups are introduced to the decode loop. - **Atomic Ring Synchronization:** In `src/platform/uring_reader.c`, ring buffer head/tail indexes are guarded via `__atomic_load_n` (`__ATOMIC_ACQUIRE`) and `__atomic_store_n` (`__ATOMIC_RELEASE`), ensuring race-free operation without requiring external `liburing` libraries. - **Graceful Fallback:** If `io_uring` setup fails (`-ENOSYS`, `-EPERM`, or kernel `< 5.1`), `DirectFile` falls back automatically to multi-threaded POSIX `pread`. - **OS Safety:** Hardware wake-latency locking (`/dev/cpu_dma_latency`) is strictly opt-in via `STRATA_LOCK_DMA_LATENCY=1` to prevent battery drain or unwanted C-state locking on standard desktop machines. Validation **Passed:** - `ctest -R "strata_ccx_shard_test|direct_file_async_test"` (Revision `50947260`, Linux 6.18.2, AMD Ryzen 5 3600, NVIDIA RTX 5060 Ti / CUDA 13.3): - `strata_ccx_shard_test`: `CCX plan 3+3 and pthread affinity OK (CPU 0)` (Passed, 0.00s) - `direct_file_async_test`: `direct_file_async: passed queued reads, short EOF, wake, close drain, reopen` (Passed, 0.00s) - `python3 bench/bench_stream.py --port 8080 --tokens 128`: - Turn 1 (Cold Prompt): TTFT 2,808 ms, Decode Throughput 19.32 tok/s. - Turn 2 (Multi-turn Prefix Cached): TTFT 2,084 ms, Decode Throughput **35.59 tok/s**, peak engine speed **39.3 tok/s** with 93.0% expert cache hit rate. - Zero host-to-device memory pipeline stalls observed during concurrent `io_uring` prefetch and active CUDA GEMV streams. - Code style: All modified files validated with `clang-format -i`. **Failed:** - none observed. **Not run:** - Windows IOCP and macOS Metal native builds (compiled on Linux x86_64; non-Linux code paths isolated under `#ifdef` stubs). Limits - CCX sharding logic is tailored for symmetric dual-CCX topologies (such as 3+3 or 4+4 cores). Single-CCX CPUs (e.g. monolithic Zen 3/4 8-core dies) or architectures with $>2$ CCXs gracefully retain default NUMA/unpinned scheduler behavior without regressions. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5370
No description provided.