Skip to content

perf(kimi-k2): compact local top-1 readback into one pinned transfer#734

Open
LanluZ wants to merge 2 commits into
openinfer-project:mainfrom
LanluZ:perf-kimi-k2-top1-readback
Open

perf(kimi-k2): compact local top-1 readback into one pinned transfer#734
LanluZ wants to merge 2 commits into
openinfer-project:mainfrom
LanluZ:perf-kimi-k2-top1-readback

Conversation

@LanluZ

@LanluZ LanluZ commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #716

Kimi's batched local top-1 readback synchronized the whole compute stream and then copied the full-capacity ID and value buffers through two pageable D2Hs, although only active_rows are consumed. This adds a full-stream barrier and two host transfers to the decode tail.

This change:

  • Adds a dedicated pack_top1_packets_cuda kernel that interleaves each active row's {i32 id, bf16 value} into one compact 8-byte device packet (shared argmax kernels are untouched).
  • Keeps a persistent device packet buffer (CudaSlice<u8>) + pinned host landing buffer (PinnedHostSlice<u8>) in SamplingScratch.
  • Issues one active-prefix D2H (active_rows * 8 bytes) into the pinned buffer instead of two full-capacity pageable copies.
  • Waits on the transfer's completion event (cudarc PinnedHostSlice event) instead of cudaStreamSynchronize — no full-stream barrier solely for top-1 readback.

Token IDs, values, and cross-rank selection semantics are unchanged; the row range checks are preserved verbatim.

I was unable to complete end-to-end testing locally. Requesting the reviewer / maintainer to verify.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Checklist

  • My code follows the style guidelines of this project (see docs/conventions/coding-style.md).
  • I have performed a self-review of my own code.
  • I have formatted my commits according to Commitizen conventions.
  • I have run the local test suite and all tests pass (see CLAUDE.md).

LanluZ added 2 commits July 21, 2026 17:20
Pack each active row's top-1 id and value into one compact device packet
via a dedicated pack kernel, land it in a persistent pinned host buffer
with a single active-prefix D2H, and wait on that transfer's completion
event instead of synchronizing the whole compute stream.

Refs openinfer-project#716

Signed-off-by: LanluZ <lanluz@users.noreply.github.com>
Signed-off-by: LanluZ <lanluz@users.noreply.github.com>
@LanluZ
LanluZ force-pushed the perf-kimi-k2-top1-readback branch from c53ccb7 to fe565d8 Compare July 21, 2026 09:20
@xiaguan

xiaguan commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

Thanks for working on this. The implementation direction matches #716, and a little runtime evidence would help us validate both correctness and the intended improvement. The current description notes that end-to-end testing was not completed; the generic CI checks also do not exercise the Kimi-K2 runtime path.

Could you please add results from the same hardware/software/configuration for:

  1. Kimi-K2 E2E correctness on the affected greedy local-top1 path, including the multi-rank/cross-rank selection path if that is the production configuration.
  2. A before/after trace showing D2H call count, D2H bytes, and synchronization/event behavior per decode step, as requested in perf(kimi-k2): compact local top-1 readback into one pinned transfer #716.
  3. A before/after TPOT benchmark with the exact command/configuration and representative active-row counts. Please include neutral or negative cases as well.

These results will let us confirm that the runtime behavior is preserved and evaluate the optimization in its intended context. Once they are attached, we can continue with the detailed review.

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.

perf(kimi-k2): compact local top-1 readback into one pinned transfer

2 participants