Repository navigation
[Feature] GLM-5.3 internal prefill checkpoints #5048
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
yidingcheng0206
wants to merge
4
commits into
InternLM:main
from
yidingcheng0206:feat/glm53-internal-checkpoint
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cf288d7
fix(pytorch): enforce prefill checkpoint save alignment
yidingcheng0206 ebba3bd
feat(pytorch): add optional native FlashKDA prefill backend
yidingcheng0206 a639759
feat(pytorch): support GLM internal prefill checkpoints
yidingcheng0206 a447f37
feat(pytorch): support MTP internal prefill checkpoints
yidingcheng0206 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # GLM-5.3 internal prefill checkpoints | ||
|
|
||
| IC avoids a model forward used only to reach a checkpoint boundary. It is | ||
| disabled by default and supports non-MTP AR and `deepseek_mtp`. | ||
|
|
||
| ## Enablement | ||
|
|
||
| Use local prefix caching (`--enable-prefix-caching`) and set: | ||
|
|
||
| ```bash | ||
| export LMDEPLOY_KDA_PREFILL_BACKEND=flash_kda | ||
| export LMDEPLOY_INTERNAL_PREFILL_CHECKPOINT=1 | ||
| ``` | ||
|
|
||
| The default backend remains the FLA entry, which may dispatch to FlashKDA. | ||
| Native selection alone does not enable IC or change decode. The native adapter | ||
| requires BF16 inputs, head dimension 128, FP32 recurrent state, a gate lower | ||
| bound, and operator outputs `checkpoint_state` / `checkpoint_offsets`. | ||
| Incompatible enabled configurations fail validation rather than silently fall back. | ||
|
|
||
| Validated FlashKDA revision: `ee0be888cd0e972f9409bf53756f8c38c6652173`, | ||
| Torch 2.10.0+cu128 and FLA 0.5.2 on H200. No kernel sources are modified. | ||
|
|
||
| ## Execution and safety | ||
|
|
||
| The scheduler retains the original reservation at H and, when capacity permits, | ||
| expands execution to prompt end E within the existing prefill budget: | ||
|
|
||
| ```text | ||
| OFF: forward [start,H) -> save S_H -> forward [H,E) | ||
| ON: forward [start,E), exporting S_H internally; runtime advances to S_E | ||
| ``` | ||
|
|
||
| FlashKDA writes recurrent state into the checkpoint slot. Conv state comes from | ||
| the preceding projected-input window. Existing pinning, publication and cleanup | ||
| are reused; endpoint copying must not overwrite the internal state. KPool raw | ||
| tails remain runtime-only. | ||
|
|
||
| For MTP K, operator offset is `H - start`, recurrent destination is `H % (K+1)`, | ||
| and conv window `[H-width,H)` is placed in the `width+K` ring, wrapping if needed. | ||
| Draft shifting, known-prompt lookahead checks and accepted-history recovery are | ||
| unchanged. Predicting a next token does not make a prompt-end MTP checkpoint | ||
| publishable; MTP decode checkpoint saving remains disabled. | ||
|
|
||
| ## Limits and validation | ||
|
|
||
| - Only exclusive single-sequence text prefill with one internal save is supported. | ||
| - Required non-MTP endpoint saves retain the original split; no checkpoint is dropped. | ||
| - Connector/Mooncake, multimodal, scoring/routed-expert output, mixed local | ||
| decode/prefill, insufficient capacity and unsupported layouts do not enter IC. | ||
| - Intervals at or below `piecewise_cudagraph_max_tokens` retain the original path. | ||
| IC currently runs outside prefill PCG; decode graphs remain enabled. | ||
| - `prefill_save_alignment` filters absolute internal and endpoint save positions, | ||
| including with IC OFF; aligned positions may be skipped. Decode intervals are unchanged. | ||
| - Comparisons must fix backend, budget, PCG cap, alignment and requests. Export | ||
| work and larger forwards cost time/memory; IC is not a universal speedup. | ||
| - Tests cover alignment, fallback, restore and K=1/3/5 rings/partial acceptance; | ||
| model checks cover K=3 at TP1/DP8/EP8, not full IC accuracy or production stress. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| """Optional native FlashKDA adapter with explicit intermediate-state outputs.""" | ||
|
|
||
| import torch | ||
|
|
||
|
|
||
| def check_flash_kda(): | ||
| """Fail before inference if the installed native operator lacks IC outputs.""" | ||
| import flash_kda | ||
|
|
||
| arguments = {argument.name for argument in torch.ops.flash_kda.fwd.default._schema.arguments} | ||
| if not {'checkpoint_state', 'checkpoint_offsets'} <= arguments: | ||
| raise RuntimeError('FlashKDA must provide native checkpoint_state and checkpoint_offsets outputs.') | ||
| return flash_kda | ||
|
|
||
|
|
||
| def flash_kda_prefill(query, key, value, gate, beta, a_log, dt_bias, | ||
| initial_state, cu_seqlens, lower_bound, checkpoint=None): | ||
| """Use the same native prefill path with or without checkpoint export.""" | ||
| import flash_kda | ||
|
|
||
| if query.dtype != torch.bfloat16 or query.size(-1) != 128 or initial_state.dtype != torch.float32: | ||
| raise ValueError('Native FlashKDA prefill requires BF16 inputs, D=128 and FP32 recurrent state.') | ||
| if lower_bound is None: | ||
| raise ValueError('Native FlashKDA requires a finite gate lower bound.') | ||
| output = torch.empty_like(query, memory_format=torch.contiguous_format) | ||
| final_state = torch.empty_like(initial_state, memory_format=torch.contiguous_format) | ||
| workspace = torch.empty( | ||
| flash_kda.get_workspace_size(query.size(1), query.size(2), initial_state.size(0)), | ||
| dtype=torch.uint8, device=query.device) | ||
| torch.ops.flash_kda.fwd( | ||
| query.contiguous(), key.contiguous(), value.contiguous(), gate.contiguous(), beta.contiguous(), | ||
| query.size(-1) ** -0.5, output, workspace, a_log, | ||
| dt_bias.reshape(query.size(2), query.size(3)), lower_bound, | ||
| initial_state=initial_state.contiguous(), final_state=final_state, cu_seqlens=cu_seqlens, | ||
| checkpoint_state=None if checkpoint is None else checkpoint.recurrent_state_out, | ||
| checkpoint_offsets=None if checkpoint is None else checkpoint.offsets, | ||
| ) | ||
| return output, final_state |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add zh_cn docs