Skip to content

feat(ep): sync KIWI EP with upstream tuning, proxy affinity and fixes - #950

Merged
jhchouuu merged 8 commits into
mainfrom
charlifu/kiwi_sync_upstream
Nov 19, 2026
Merged

jhchouuu merged 8 commits into
mainfrom
charlifu/kiwi_sync_upstream

Conversation

@charlifu

@charlifu charlifu commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Summary

This brings mori.ops.dispatch_combine_kiwi up to date with five KIWI changes made after the import in #860: per-hardware tuning tables, a new way of placing proxy threads on CPUs, wider spacing for readiness flags, and two correctness fixes in the queue/invoke core.

Changes

Tuning tables per GPU arch and NIC family

  • When num_blocks or rcomp_backend is left on automatic, the values now come from a table keyed by GPU arch and NIC family.
    • The tables live in ep_tuning.{hpp,cpp}, with one file per platform under tuning/.
    • The gfx942/CX7 table gives the same results as the old generic rule.
    • The gfx950/Pollara table is new; for example, small token counts now use hybrid with fewer blocks.
    • Any other world size, token count or platform falls back to the generic rule.
  • make_config resolves the NIC name, gpu_arch and nic_family, and still applies the CU-count cap at the same point.
    • The NIC name is resolved by the new rdma::resolve_device_name(), which net_context_t also uses, so the tuning key always describes the NIC the proxies actually open.
  • The automatic policy now depends on hardware each rank detects for itself, so ranks could disagree on whether to set up HIP IPC and then hang in the setup collectives. ep_context_t now allgathers each rank's choice and sets up HIP IPC if any rank needs it.

Proxy thread CPU affinity

  • Previously each rank pinned each proxy thread to a single CPU it chose on its own. Ranks could pick the same CPU and end up busy-polling against each other.
  • Now every proxy thread is bound to the GPU's PCI-local CPUs within the process affinity mask, and the scheduler places threads inside that set. If the two don't overlap, the thread keeps the mask it inherited.
  • The KFD-node CPU-slot logic is removed, which shrinks topology.cpp from 583 to about 180 lines.
  • KIWI_PROXY_AFFINITY_DEBUG=1 prints the chosen set and each thread's binding.

Readiness flag stride

  • Payload readiness flags are now spaced 64 bytes apart (KIWI_EP_READINESS_FLAG_STRIDE). The flag index, heap layout and reset() memsets are updated to match.

Fixes in the queue/invoke core

  • KIWI's throwing HIP_CHECK is renamed to KIWI_HIP_CHECK. It clashed with the EP's aborting HIP_CHECK, and which one a file got depended on include order.
  • invoke::Context now keeps its callback registry on the heap. Before, moving a context left its endpoint dispatchers pointing at the registry in the moved-from object. Its device buffers are now held by unique_ptr, so the default move operations are correct.

Testing

  • Built with BUILD_OPS_KIWI=ON and GPU_TARGETS=gfx942;gfx950;gfx1250. The KIWI sources compile without warnings.
  • On one MI300X/CX7 node, tests/python/ops/test_kiwi_dispatch_combine.py gives 20 passed and 18 skipped, the same as on main.
  • A standalone check of the tuning lookup covered:
    • table hits and fallback to the generic rule;
    • NIC family detection: ionic becomes pollara, and mlx5 with device 0x1021 becomes cx7;
    • block counts never decreasing as token count rises, for every table and world size. Setup sizes everything from the maximum token count, so this must hold.
  • With KIWI_PROXY_AFFINITY_DEBUG=1, every proxy thread was bound to the GPU-local CPUs within the process mask.

Not covered: the gfx950/Pollara table on real hardware, the two-node EP16 entry, and performance measurements for the flag stride and the affinity change.

- Select the automatic num_blocks/rcomp_backend from per-(GPU arch, NIC family) tuning tables (gfx942/CX7, gfx950/Pollara), falling back to the generic rule; make_config resolves the NIC, arch and NIC family, and ranks agree on HIP IPC provisioning
- Bind proxy threads to the GPU's PCI-local CPUs within the process affinity mask instead of pinning each to one core
- Space payload readiness flags 64 bytes apart
- Rename KIWI's throwing HIP_CHECK to KIWI_HIP_CHECK so it no longer clashes with the EP's aborting HIP_CHECK
- Keep invoke::Context's callback registry on the heap so a moved context's endpoints stay valid
@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

GPU CI for f19be760 (run): changed areas: kiwi

lane runs jobs
mori no MI355X intranode, MI355X ROCm 7.14, MI355X JAX, MI355X internode
mi300x yes MI300X intranode, MI300X internode
umbp no UMBP unit test
cco no CCO unit test, CCO EPv2 single-node test, CCO internode
fused no CCO fused GEMM test
build yes MoRI build (compile check)

Test groups (the steps that run in those jobs): kiwi.

Lanes follow the changed files. Adding ci:full re-runs CI with every lane, the full internode sweeps and every fused GEMM case; adding ci:mi300x re-runs it with the MI300X lanes added. /ci-rerun re-runs CI against the current main.

@charlifu
charlifu requested a review from jhchouuu November 16, 2026 15:34

@jhchouuu jhchouuu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These match upstream KIWI d7bb0d1 (apps/ep, apps/common/topology, invoke.hpp). CI on c4aedef ran the KIWI single-node test and all six EP16 backend/dtype runs on MI300X_MLX, and they pass, so the two-node entry is covered. A few comments inline.

if (hca_name.empty()) {
return {};
}
std::string prefix = hca_name.substr(0, hca_name.find('_'));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The family comes from the device name, so a NIC renamed by rdma-core's persistent naming (for example an ionic device named rocep9s0) falls back to the generic rule without any message. net_context_t already opens the device through mori's RdmaContext, and RdmaDevice::GetDeviceAttr() has vendor_id / vendor_part_id (Pensando 0x1dd8; Mellanox 0x15b3, part 0x1021 for CX7). Could the family come from those instead?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, agreed. A renamed device misses both prefixes and silently gets the generic rule, and a renamed CX7 also loses the cx7 check. I now key the family off the PCI vendor/device IDs instead of the name. They are read from /sys/class/infiniband//device/{vendor,device}, which exist whatever the device is called: 0x1dd8 is pollara, and 0x15b3 with 0x1021 is cx7. The name prefix stays only as a fallback. I didn't go through RdmaContext / GetDeviceAttr() for two reasons. First, ibv_device_attr.vendor_id is whatever the driver reports, and on CX7 that is the OUI 0x02c9, not the PCI vendor 0x15b3 (vendor_part_id is 4129 = 0x1021). Second, the family is needed in make_config() to size the queues, before ep_comm_context_t creates net_context_t. So using the attributes would mean opening the device a second time or moving net_context_t creation ahead of the EP context. I also added a one-time warning when no tuning table matches the GPU arch and NIC family, so a fallback is visible.

Comment on lines +34 to +37
{8, 4, 32, rcomp_backend_t::RDMA_ONLY}, {8, 8, 32, rcomp_backend_t::HYBRID},
{8, 16, 32, rcomp_backend_t::HYBRID}, {8, 32, 32, rcomp_backend_t::HYBRID},
{8, 64, 64, rcomp_backend_t::HYBRID}, {16, 1, 12, rcomp_backend_t::RDMA_ONLY},
{16, 2, 20, rcomp_backend_t::RDMA_ONLY}, {16, 4, 32, rcomp_backend_t::HYBRID},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description says this table matches the old generic rule, but it does only at power-of-two token counts: at world size 8, 5-7 tokens now pick HYBRID (generic: RDMA_ONLY below 8), and at world size 16/32/64, 3 tokens now pick HYBRID (generic: below 4). Is that intended?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now I only measured power-of-two token counts. We can always refine this table if necessary.

// capacity selects hybrid; explicit backend selection remains fixed.
// capacity selects hybrid, which the capacity bucket does if any does;
// explicit backend selection remains fixed.
bool rcomp_ipc_resources_required() const {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

provisioned_num_blocks() and this read only the capacity bucket, so they rely on every table being monotonic (blocks never decreasing, HYBRID at capacity if any smaller bucket is HYBRID). Nothing in mori checks that: a decreasing block row aborts at launch (dispatch.hip.cpp:744-749), and a HYBRID row below an RDMA_ONLY capacity row launches dispatch<true> without IPC resources. Upstream has the check in apps/ep/tests/test_ep_block_count.hip.cpp (require_monotonic). Could it come along, or could these two take the max / any over the rules up to capacity?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we should also add the same check from kiwi here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I went with the second option: make_config now scans every token count up to capacity once and stores the max num_blocks and whether any count selects HYBRID. provisioned_num_blocks() and rcomp_ipc_resources_required() read that instead of the capacity bucket, so provisioning no longer depends on the tables being monotonic, and the per-launch check is still a field read. For the current tables the result is identical to the capacity bucket (checked at world sizes 2-64, capacity 1-4096).

} // namespace

std::vector<int> current_allowed_cpus() {
std::vector<int> proxy_thread_cpu_set() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #860 @JiakunYan asked for a per-thread vs per-GPU-set pinning comparison before changing this, and the description lists the affinity change as not measured. Do you have before/after numbers? It now reads the same local_cpulist as mori's GpuLocalCpuList (include/mori/application/utils/cpu_affinity.hpp:47); could it reuse that helper and honor MORI_IGNORE_CPU_AFFINITY (#935)? It also still aborts if sched_setaffinity fails (L166), where mori's helper warns.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I measured per-thread and per-GPU-set pinning and I didn't see meaningful difference, so I just changed it in kiwi-ep to do it the mori way. Yes it should reuse the mori helper here.

// cache line, and writes from different QPs into that shared line appear to
// serialize at the NIC/memory controller. Padding every flag out to its own
// cache line removes that false sharing.
inline constexpr size_t KIWI_EP_READINESS_FLAG_STRIDE = 64;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes both readiness regions 64x larger (2 x world_size x max tokens x 64 B, e.g. 32 MiB at EP64 with 4096 tokens, 512 KiB before), and reset() clears all of it. Are there upstream measurements of the gain?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I measured it. It makes 64 tokens/GPU 3-8% faster at 2/4/8 nodes. I don't think the memory usage matters since it is negligible compared the the memory usage of the token data.

// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// MI355X with Pollara NICs: BF16, hidden 7168, top-k 8, eight ranks per node.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KIWI isn't built in the MI355X CI jobs, and the description says this table wasn't run on hardware here. Was it measured upstream? The key has no dtype or hidden size, so it also applies to shapes other than BF16 / 7168 / top-8.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was measured upstream on MI355X + Pollara at EP8/16/32, 1–64 tokens/rank, with BF16 / 7168 / top-8. We can always add dtype or hidden size in the future when we have more data points.

Comment thread src/kiwi/topology/topology.cpp Outdated
CPU_ZERO(&actual);
sched_getaffinity(0, sizeof(actual), &actual);
std::fprintf(stderr, "KIWI_PROXY_AFFINITY_DEBUG pid=%ld tid=%ld bound=%s\n",
static_cast<long>(getpid()), static_cast<long>(gettid()),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: gettid() needs glibc 2.30, and the wheel image is manylinux_2_28 (glibc 2.28), so this file won't compile there if KIWI is ever enabled in wheels. syscall(SYS_gettid) avoids that.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, fixed: it now uses syscall(SYS_gettid), so the file builds on manylinux_2_28 (glibc 2.28) if KIWI is ever enabled in wheels.


// Check HIP call and throw on failure
#define HIP_CHECK(call) \
#define KIWI_HIP_CHECK(call) \

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: with this renamed, the #undef HIP_CHECK in src/ops/dispatch_combine_kiwi/hip_check.hpp:54 is no longer needed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, removed the #undef; this is now the only HIP_CHECK definition.

rdma-core's persistent naming can rename RDMA devices (e.g. an ionic NIC
to rocep9s0), so the name prefix missed both ionic_* and mlx5_* and the
tuning lookup silently fell back to the generic rule. Read the PCI
vendor/device IDs under /sys/class/infiniband/<name>/device for every
device (0x1dd8 is pollara, 0x15b3:0x1021 is cx7) and keep the name rule
only as a fallback. Log at debug level when no tuning table matches.
gettid() needs glibc 2.30, and the wheel image is manylinux_2_28
(glibc 2.28), so topology.cpp would not build there if KIWI were
enabled in wheels.
The KIWI queue header now defines KIWI_HIP_CHECK, so nothing else defines HIP_CHECK before this header.

@jhchouuu jhchouuu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fixes. 89c4c59 (NIC family from the PCI vendor/device IDs), 93322a4 (syscall(SYS_gettid)) and da1a530 (#undef removed) look good, and the answers on affinity, flag stride and the tuning tables make sense.

A correction to my earlier comment: ibv_device_attr.vendor_id is the OUI (0x02c9 for Mellanox, as in mori's RdmaDeviceVendorId), not the PCI vendor ID, so reading the PCI IDs from sysfs is the right approach.

On the monotonic table check: will you add it in this PR or in a follow-up? Either works for me.

The MI300X_MLX internode failure on da1a530 is node2 being unreachable (No route to host), not this change; it needs a rerun once the runner is back.

provisioned_num_blocks() and rcomp_ipc_resources_required() read only the
capacity bucket, so they relied on every tuning table being monotonic. A
table with a larger block count below capacity would abort at launch, and
a HYBRID row below an RDMA_ONLY capacity row would launch dispatch<true>
without IPC resources. make_config now scans token counts 1..capacity
once (provisioned_runtime_defaults) and stores the max num_blocks and
whether any count selects HYBRID; both functions read that. Results are
unchanged for the current tables.
@charlifu

charlifu commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator Author

Thanks for the fixes. 89c4c59 (NIC family from the PCI vendor/device IDs), 93322a4 (syscall(SYS_gettid)) and da1a530 (#undef removed) look good, and the answers on affinity, flag stride and the tuning tables make sense.

A correction to my earlier comment: ibv_device_attr.vendor_id is the OUI (0x02c9 for Mellanox, as in mori's RdmaDeviceVendorId), not the PCI vendor ID, so reading the PCI IDs from sysfs is the right approach.

On the monotonic table check: will you add it in this PR or in a follow-up? Either works for me.

The MI300X_MLX internode failure on da1a530 is node2 being unreachable (No route to host), not this change; it needs a rerun once the runner is back.

Thanks, and thanks for the OUI correction. For the monotonic table check I took the second option from your comment instead of porting require_monotonic: 097731d makes provisioned_num_blocks() take the maximum block count over every token count from 1 to capacity, and rcomp_ipc_resources_required() is true if any of those counts selects HYBRID. Provisioning no longer depends on the tables being monotonic, so a decreasing block row can't exceed what was provisioned, and a HYBRID row below an RDMA_ONLY capacity row still gets IPC resources. With that, I don't think the check is needed in this PR; I can add it as a follow-up if you'd still like it as a guard on the tuning tables.

BTW, the two-node test is still broken.

@jhchouuu
jhchouuu merged commit 23e7353 into main Nov 19, 2026
30 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants