Skip to content

[Fix] Avoid CUDA sync in DeepSeek-V4 prefill token metadata - #5016

Open
adenzhou1350 wants to merge 1 commit into
InternLM:mainfrom
adenzhou1350:codex/v4-prefill-host-token-count
Open

adenzhou1350 wants to merge 1 commit into
InternLM:mainfrom
adenzhou1350:codex/v4-prefill-host-token-count

Conversation

@adenzhou1350

Copy link
Copy Markdown
Contributor

Motivation

build_prefill_token_meta takes its arange size from cu_q_seqlens[-1]. The CUDA prefill caller builds these cumulative lengths on the device, so that scalar conversion synchronizes even though the helper claims not to. The flattened input's token count is already available from its shape.

Modification

Pass step_ctx.input_ids.numel() through _precompute_prefill to an optional keyword-only total_tokens argument. Calls that omit it retain the existing behavior. Token mappings and dtypes are unchanged; the docstring now describes the fallback accurately.

Validation

  • Native RTX 4060 Laptop, PyTorch 2.11.0+cu128: the unchanged helper triggers called a synchronizing CUDA operation in sync-debug mode, both with and without supplied cumulative lengths. The host-count path passes that check.
  • pytest tests/pytorch/kernel/test_v4_utils.py -q: 26 passed, covering CPU/CUDA, empty/mixed sequence lengths, optional cumulative lengths/count, output dtype/device, and the sync regression.
  • Ruff and git diff --check pass.

The tests directly load the actual standalone helper with real Torch operations, not mocked tensor behavior. This is component validation, not a full DeepSeek-V4 model benchmark or a whole-model speedup claim. Full package import was blocked by missing mmengine locally; pre-commit environment bootstrap was blocked by Windows application control. Upstream package/CI coverage is still needed.

Signed-off-by: Xucheng Zhou <aden1350@outlook.com>

This branch has not been deployed

No deployments
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