Skip to content

feat(kt): add --kt-skip-gpu-expert-cpu-copy to reclaim host RAM for GPU experts#64

Open
gvr13n wants to merge 1 commit into
kvcache-ai:mainfrom
gvr13n:feat/kt-skip-gpu-expert-cpu-copy
Open

feat(kt): add --kt-skip-gpu-expert-cpu-copy to reclaim host RAM for GPU experts#64
gvr13n wants to merge 1 commit into
kvcache-ai:mainfrom
gvr13n:feat/kt-skip-gpu-expert-cpu-copy

Conversation

@gvr13n

@gvr13n gvr13n commented Jul 12, 2026

Copy link
Copy Markdown

Companion to the kt-kernel PR (kvcache-ai/ktransformers#2085); see kvcache-ai/ktransformers#2084 for the full rationale and numbers.

Adds the user-facing --kt-skip-gpu-expert-cpu-copy server arg and resolves it into an effective flag passed to KTMoEWrapper:

_mask_static = method == "MXFP4" or (method == "MXFP8" and not kt_enable_dynamic_expert_update)
skip = kt_skip_gpu_expert_cpu_copy and _mask_static

Only paths whose full-GPU prefill fallback is mask-aware may skip: MXFP4's _prepare_weight_mxfp4_prepare_weight_fp8 copies GPU experts device-to-device from original_layer and never reads the CPU copies; MXFP4's dynamic-update mask rewrite is unconditionally skipped, so its mask is static even with --kt-enable-dynamic-expert-update on. If the flag cannot be honored (e.g. MXFP8 + dynamic update), it warns once at layer 0 and falls back to stock behavior — safe by construction.

Measured on DeepSeek-V4-Flash (MXFP4, 100–110 GPU experts, RTX PRO 6000 + 123GB RAM): scheduler RSS ~172GB → ~99GB, decode swap io → 0, identical output.

Diff is pure additions (+29, 2 files: server_args.py, kt_ep_wrapper.py), rebased on current main.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the --kt-skip-gpu-expert-cpu-copy option to skip allocating CPU copies of GPU-resident experts, reclaiming host RAM. The feedback suggests normalizing the method string to uppercase to ensure the case-insensitive comparison works correctly and prevents the option from being silently ignored.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +2663 to +2665
_method = self.kt_config.method
_dyn = self.kt_config.kt_enable_dynamic_expert_update
_mask_static = _method == "MXFP4" or (_method == "MXFP8" and not _dyn)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The comparison of _method with "MXFP4" and "MXFP8" is case-sensitive. If a user specifies --kt-method mxfp4 or --kt-method mxfp8 in lowercase, the condition _mask_static will evaluate to False, and the --kt-skip-gpu-expert-cpu-copy flag will be silently ignored (or a warning will be logged).

To ensure robustness and consistency with other parts of the codebase where self.kt_config.method is normalized using .upper(), we should normalize _method to uppercase.

Suggested change
_method = self.kt_config.method
_dyn = self.kt_config.kt_enable_dynamic_expert_update
_mask_static = _method == "MXFP4" or (_method == "MXFP8" and not _dyn)
_method = (self.kt_config.method or "").upper()
_dyn = self.kt_config.kt_enable_dynamic_expert_update
_mask_static = _method == "MXFP4" or (_method == "MXFP8" and not _dyn)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in d9a8b1c — --kt-method has no argparse choices constraint, so lowercase input was indeed possible and would silently disable the flag. The method string is now normalized with .upper() before the comparison.

…PU experts

In a hybrid GPU/CPU MoE split, kt-kernel keeps a CPU copy of every expert,
including the ones placed on and computed by the GPU. The companion kt-kernel
change adds an opt-in MOEConfig.skip_gpu_expert_cpu_copy that drops those
redundant host copies (~63GB at 110 GPU experts for DeepSeek-V4-Flash). Wire it
through from the server:

- server_args: new --kt-skip-gpu-expert-cpu-copy flag (off by default)
- kt_ep_wrapper: resolve the public opt-in into an effective flag, honored only
  where the GPU/CPU expert mask is static so no runtime path reads a skipped
  buffer -- MXFP4 (dynamic expert update is a no-op there, so the skip is safe
  even with --kt-enable-dynamic-expert-update on), or MXFP8 with dynamic update
  off. If it can't be honored the wrapper warns once and falls back to stock.
@gvr13n
gvr13n force-pushed the feat/kt-skip-gpu-expert-cpu-copy branch from f375568 to d9a8b1c Compare July 12, 2026 17:23
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.

1 participant