-
Notifications
You must be signed in to change notification settings - Fork 336
Refactor: add model-specific patch registry #966
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
+122
−22
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,83 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """A registry of target model-specific patches on class HFEagleModel. | ||
|
|
||
| The patches are indexed by model type and will be called by HFEagleModel object at the end of modify(). | ||
| """ | ||
|
|
||
| import types | ||
| from collections.abc import Callable | ||
|
|
||
| import transformers | ||
| from packaging.version import Version | ||
| from transformers.utils.quantization_config import CompressedTensorsConfig | ||
|
|
||
| all = ["apply_model_patch"] | ||
|
|
||
| _MODEL_PATCH_REGISTRY: dict[str, Callable] = {} | ||
|
|
||
|
|
||
| def register_model_patch(model_type: str): | ||
| """Decorator to register a patch function for a specific model type.""" | ||
|
|
||
| def decorator(func: Callable): | ||
| _MODEL_PATCH_REGISTRY[model_type] = func | ||
| return func | ||
|
|
||
| return decorator | ||
|
|
||
|
|
||
| def apply_model_patch(module): | ||
| """Apply a registered patch to the given module based on model_type.""" | ||
| model_type = getattr(module.config, "model_type", None) | ||
| if model_type in _MODEL_PATCH_REGISTRY: | ||
| _MODEL_PATCH_REGISTRY[model_type](module) | ||
|
|
||
|
|
||
| @register_model_patch("kimi_k2") | ||
| def patch_for_kimi_k2(module): | ||
| """Patch for Kimi-K2-Thinking as target model. | ||
|
|
||
| - Version check for transformers < 5.0 | ||
| - Avoid quantizing drafter by updating quantization_config | ||
| - Repeat attention mask at batch dimension | ||
| """ | ||
| if Version(transformers.__version__) >= Version("5.0"): | ||
| raise RuntimeError( | ||
| "Kimi K2 is not supported for transformers >= 5.0. \ | ||
| Please install transformers >=4.57, <5.0" | ||
| ) | ||
|
|
||
| if module.eagle_config._attn_implementation == "flex_attention": | ||
| raise ValueError("Kimi K2 does not support flex attention.") | ||
|
|
||
| # Avoid quantizing drafter by updating quantization_config | ||
| quant_config = getattr(module.config, "quantization_config", None) | ||
| if isinstance(quant_config, CompressedTensorsConfig): | ||
| quant_config.quantization_config.ignore.append("re:.*eagle_module.*") | ||
h-guo18 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Kimi K2 assert attention mask shape as (bsz, 1, qlen, kvlen) | ||
| # https://huggingface.co/moonshotai/Kimi-K2-Thinking/blob/main/modeling_deepseek.py#L829 | ||
| # So we repeat the attention mask at batch dimension | ||
| original_func = module._compute_ttt_attention_mask | ||
|
|
||
| def _patched_compute_ttt_attention_mask(self, batch_size, seq_length, ttt_step): | ||
| tensor_mask = original_func(batch_size, seq_length, ttt_step) | ||
| return tensor_mask.repeat(batch_size, 1, 1, 1) | ||
h-guo18 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| module._compute_ttt_attention_mask = types.MethodType( | ||
| _patched_compute_ttt_attention_mask, module | ||
| ) | ||
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.
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.
Typo:
allshould be__all__.The module-level
allvariable has no special meaning in Python. It should be__all__(with double underscores) to properly control what gets exported when usingfrom module import *.Fix
🤖 Prompt for AI Agents