Fix/dsv4 native transformers warmup - #4878
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates LMDeploy’s DeepSeek-V4 integration to align with native Transformers config/schema and improves the CUDA V4 indexer warmup and metadata handling, while removing LMDeploy’s custom HF config registration paths.
Changes:
- Switch DeepSeek-V4 compression config handling to native
layer_types/compress_ratesand force V4 cache block sizing to a fixed 256. - Extend the V4 indexer backend interface to include
num_heads/head_dim, and fix DeepGEMM warmup + empty-sequence handling via newtopk_seqlens. - Remove LMDeploy custom DeepSeek (v4/v32) HF config classes and manual config registration/export hooks.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/pytorch/config/test_model_config.py | Updates tests to reflect native V4 layer schema and fixed block sizing expectations. |
| lmdeploy/pytorch/transformers/configuration_deepseek_v4.py | Removes re-export shim for the custom DeepSeek-V4 HF config. |
| lmdeploy/pytorch/transformers/configuration_deepseek_v32.py | Removes re-export shim for the custom DeepSeek-V32 HF config. |
| lmdeploy/pytorch/transformers/init.py | Drops register_config from the public pytorch transformers helper exports. |
| lmdeploy/pytorch/nn/v4_indexer.py | Updates V4 indexer wrapper to pass num_heads/head_dim into backend builder. |
| lmdeploy/pytorch/models/deepseek_v4.py | Adapts DeepSeek-V4 model wiring to native config fields and new compression-ratio translation. |
| lmdeploy/pytorch/configurations/deepseek_v4.py | Adds native layer schema translation, forces V4_BLOCK_SIZE=256, updates cache config finalization. |
| lmdeploy/pytorch/backends/indexer.py | Extends BaseV4IndexerBuilder.build() signature to include num_heads/head_dim. |
| lmdeploy/pytorch/backends/cuda/v4_indexer.py | Fixes DeepGEMM warmup shapes/keys and requires topk_seqlens for packed scoring path. |
| lmdeploy/pytorch/backends/cuda/attention/v4.py | Adds topk_seqlens to index-score metadata and clamps scheduler inputs safely for empty rows. |
| lmdeploy/hf_configs/configuration_deepseek_v4.py | Deletes LMDeploy’s custom DeepSeek-V4 PretrainedConfig implementation. |
| lmdeploy/hf_configs/configuration_deepseek_v32.py | Deletes LMDeploy’s custom DeepSeek-V32 PretrainedConfig implementation. |
| lmdeploy/hf_configs/init.py | Simplifies config loading to a direct AutoConfig.from_pretrained() call. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| from transformers import AutoConfig | ||
|
|
||
| from lmdeploy.utils import get_logger | ||
|
|
||
| logger = get_logger('lmdeploy') | ||
|
|
||
|
|
||
| @lru_cache | ||
| def register_config(model_type: str): | ||
| if model_type == 'deepseek_v32': | ||
| from .configuration_deepseek_v32 import DeepseekV32Config | ||
| AutoConfig.register(DeepseekV32Config.model_type, DeepseekV32Config) | ||
| elif model_type == 'deepseek_v4': | ||
| from .configuration_deepseek_v4 import DeepseekV4Config | ||
| AutoConfig.register(DeepseekV4Config.model_type, DeepseekV4Config) | ||
| else: | ||
| logger.debug(f'Can not register config for model_type: {model_type}') | ||
|
|
||
|
|
||
| def config_from_pretrained(pretrained_model_name_or_path: str, **kwargs): | ||
| try: | ||
| return AutoConfig.from_pretrained(pretrained_model_name_or_path, **kwargs) | ||
| except Exception as e: | ||
| logger.debug(f'AutoConfig.from_pretrained failed: {e}, try register config manually.') | ||
| # some models do not provide auto map for config | ||
| from transformers import PretrainedConfig | ||
| trust_remote_code = kwargs.pop('trust_remote_code', None) | ||
| config_dict, _ = PretrainedConfig.get_config_dict(pretrained_model_name_or_path, **kwargs) | ||
| model_type = config_dict.get('model_type', None) | ||
| if trust_remote_code is not None: | ||
| kwargs['trust_remote_code'] = trust_remote_code | ||
| register_config(model_type) | ||
| try: | ||
| return AutoConfig.from_pretrained(pretrained_model_name_or_path, **kwargs) | ||
| except Exception as e: | ||
| return PretrainedConfig.from_pretrained(pretrained_model_name_or_path, **kwargs) | ||
| return AutoConfig.from_pretrained(pretrained_model_name_or_path, **kwargs) |
| compress_rates = hf_config.compress_rates | ||
| compressed_layer_types = set(layer_types).difference({'sliding_attention'}) | ||
| missing_rates = sorted(compressed_layer_types.difference(compress_rates)) | ||
| if missing_rates: | ||
| raise ValueError(f'DeepSeek-V4 compress_rates is missing layer types: {missing_rates}.') | ||
|
|
||
| compress_ratios = [ | ||
| 0 if layer_type == 'sliding_attention' else compress_rates[layer_type] | ||
| for layer_type in layer_types | ||
| ] |
|
May put "pip install tile-kernels" in docker/install.sh |
The response log shows: This does not appear to be a valid tool_call response. Could you please help investigate whether this is caused by the model, the chat template, the inference pipeline, or something else? |
Co-Authored-By: Claude <noreply@anthropic.com>
tile-kernels depends on a cu13 tilelang build, so gate the pip install on CUDA_VERSION_SHORT == cu13* instead of installing it unconditionally. Co-Authored-By: Claude <noreply@anthropic.com>
The _hc_post_expand_kernel received *comb.stride() for a contiguous [n, src, out] tensor, assigning comb_stride_out_h=stride(1) (the SRC axis) and comb_stride_src_h=stride(2) (the OUT axis). The kernel indexes weight as (out_h, src_h) and sums over src_h, so this loaded comb[out, src] and computed matmul(comb, residual) -- the transpose of the intended residual mix. DeepSeek-V4's comb is produced by hc_split_sinkhorn as [n, src, out], identical to native transformers / official inference / vllm, all of which apply matmul(comb.T, residual) == sum_src comb[src, out]*residual[src]. The Sinkhorn comb is doubly-stochastic but non-symmetric (real weights ~2-11% off-diagonal), so the transposed application accumulated systematic error across 86 applications (2 per layer x 43 layers) and flipped low-margin closing-tag tokens in DSML tool-call output, e.g. </|DSML|tool> instead of </|DSML|parameter> and </|DSML|inv> instead of </|DSML|invoke>, leaving tool_calls unparsed. Pass comb.stride(0), comb.stride(2), comb.stride(1) so out_h strides the OUT axis (stride(2)) and src_h the SRC axis (stride(1)), yielding matmul(comb.T, residual). Verified end-to-end: tool_choice='auto' now returns parsed tool_calls with correct closing tags. The test reference _reference_post_expand encoded the same transposed mix (matmul(comb, residual)); corrected to matmul(comb.T, residual). Co-Authored-By: Claude <noreply@anthropic.com>
fixed |
|
The root cause of test_docker_image failure is brought by #4853 #4853 added
cc @CUHKSZzxy |
Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily receiving feedbacks. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.
Motivation
Please describe the motivation of this PR and the goal you want to achieve through this PR.
Modification
Please briefly describe what modification is made in this PR.
BC-breaking (Optional)
Does the modification introduce changes that break the backward-compatibility of the downstream repositories?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.
Use cases (Optional)
If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.
Checklist