Repository navigation
feat(pytorch): add opt-in unified compact state caching - #5057
Draft
RunningLeon wants to merge 4 commits into
Draft
RunningLeon wants to merge 4 commits into
RunningLeon wants to merge 4 commits into
Conversation
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
Reduce redundant recurrent/convolution state storage and checkpoint copies in the PyTorch engine for Qwen3.5 and GLM-5.3 Flash, while preserving speculative decoding, prefix caching, and asynchronous Mooncake Store lifetimes.
The legacy paths maintain runtime state and checkpoint storage separately and may copy state during checkpoint save/restore. They can also split prefill solely to reach a checkpoint boundary. A unified compact state pool lets runtime execution and checkpoints reference the same physical state, with explicit ownership preventing premature reuse.
This implementation is opt-in: set
LMDEPLOY_ENABLE_COMPACT_STATE=1before starting the Python process. Unset or0preserves legacy execution.Modification
Legacy execution remains the default. The environment switch derives compact capacity from the existing batch/checkpoint/Store budgets and MTP width; no test hook is required. Unsupported compact execution modes fail explicitly rather than silently falling back.
The current scope remains CUDA Qwen3.5 / GLM-5.3 Flash AR or matching single-layer MTP with UniExecutor/RayExecutor. Existing guards are unchanged: an explicit positive
num_gpu_blocksis required; Mooncake Store also requires immutablecompact_model_identityand, for MTP,compact_draft_identityinkv_connector_extra_config. PD, MemDecode, deprecated MP, other connectors, and Store with CPU KV paging are not supported.Implementation references
3802ee441.8401699c9.lmdeploy/pytorch/engine/cache_engine/README.md.Performance results
GLM-5.3 Flash, TP4: upstream/main
aba987ad2266d2fd2204e9d22d1ce43a5984dc91versus compact8401699c92a289441d1ba79aeab1f63a1c5b3a45.prefill_save_alignment=512; inputs share a 50% prefix.All 16 cases completed: 16,384/16,384 measured requests passed exact-length and termination checks, with zero request errors.
Observed throughput improvements are 0.64%–22.00% with MTP5 and 9.46%–38.64% with target-only decoding in this matrix.
Interpretation and measurement boundaries
(E2E − TTFT) / (output_tokens − 1), not per-token ITL.ignore_eos=True; this performance workload is not an accuracy evaluation. Maximum batch size is a cap, not guaranteed active occupancy.Validation and remaining scope
8401699c9): 224 tests passed, including boolean parsing/worker-environment forwarding, AR/MTP capacity selection with APC/Store roles, fail-closed guards, and real CUDA allocation. This does not rerun full-model quality or serving performance.Use cases (Optional)
The following examples use GLM-5.3 Flash, TP4, compact state ON, prefix caching ON, and Mooncake Store OFF. Run them separately on four available GPUs with sufficient memory. Use the branch containing the environment-switch follow-up and replace the model path with your local checkpoint directory.
Python pipeline
OpenAI-compatible API server
Using the same environment variables above:
lmdeploy serve api_server "$MODEL_PATH" \ --backend pytorch \ --tp 4 \ --distributed-executor-backend ray \ --dtype bfloat16 \ --language-model-only \ --max-batch-size 8 \ --session-len 8192 \ --num-gpu-blocks 2048 \ --max-prefill-token-num 2048 \ --enable-prefix-caching \ --prefix-cache-state-budget 8 \ --prefill-save-alignment 512 \ --cache-block-seq-len 64 \ --model-name glm53-flash \ --server-name 127.0.0.1 \ --server-port 23333 \ --log-level INFOSend a request from another terminal:
Optional MTP3: for the pipeline, import
SpeculativeConfigfromlmdeploy.messagesand passspeculative_config=SpeculativeConfig(method='deepseek_mtp', num_speculative_tokens=3)topipeline(...). For the server, append--speculative-algorithm deepseek_mtp --speculative-num-draft-tokens 3to the launch command. Leave these options out for target-only decoding.num_gpu_blocks=2048is an explicit example capacity, not a universal hardware recommendation or the performance-table configuration. Compact currently requires a positive explicit KV block count; adjust it to the available memory. To use legacy execution, setLMDEPLOY_ENABLE_COMPACT_STATE=0and restart the process. The startup log reportsEnable unified compact statewhen the compact path is selected.Checklist