-
Notifications
You must be signed in to change notification settings - Fork 599
Add DASC recurrent state sparsity policy #2375
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
Closed
Changes from all commits
Commits
Show all changes
2 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
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,39 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 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. | ||
|
|
||
| """Decay-aware sparsity policies for persisted recurrent state.""" | ||
|
|
||
| from . import mode # imported for mode-registration side effects | ||
| from .api import calibrate, export_policy | ||
| from .config import ( | ||
| DASCCalibrationMeasurement, | ||
| DASCConfig, | ||
| DASCLayerPolicy, | ||
| DASCPolicy, | ||
| DASCQualityMeasurement, | ||
| ) | ||
| from .policy import analyze_gdn_decay, compute_gdn_decay_horizons | ||
|
|
||
| __all__ = [ | ||
| "DASCCalibrationMeasurement", | ||
| "DASCConfig", | ||
| "DASCLayerPolicy", | ||
| "DASCPolicy", | ||
| "DASCQualityMeasurement", | ||
| "analyze_gdn_decay", | ||
| "calibrate", | ||
| "compute_gdn_decay_horizons", | ||
| "export_policy", | ||
| ] |
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,88 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 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. | ||
|
|
||
| """Public DASC state-sparsity APIs.""" | ||
|
|
||
| import copy | ||
| from collections.abc import Iterable | ||
| from typing import Any | ||
|
|
||
| from torch import nn | ||
|
|
||
| from modelopt.torch.opt.conversion import ModeloptStateManager, apply_mode | ||
| from modelopt.torch.utils import unwrap_model | ||
|
|
||
| from .config import DASCCalibrationMeasurement, DASCConfig | ||
| from .conversion import get_attached_dasc_policy, replace_dasc_mode | ||
| from .mode import DASCModeRegistry | ||
| from .policy import validate_dasc_decay_parameters, validate_dasc_model_structure | ||
|
|
||
| __all__ = ["calibrate", "export_policy"] | ||
|
|
||
|
|
||
| def calibrate( | ||
| model: nn.Module, | ||
| config: dict[str, Any] | DASCConfig, | ||
| measurements: Iterable[DASCCalibrationMeasurement | dict], | ||
| ) -> nn.Module: | ||
| """Calibrate and attach a DASC policy without changing model execution. | ||
|
|
||
| ``measurements`` must contain exactly one entry for every configured ``Wmax`` candidate. | ||
| The largest candidate passing every quality, lifecycle, and storage gate is selected. | ||
| Recalibrating replaces the existing DASC mode-state entry in place. | ||
|
|
||
| Example:: | ||
|
|
||
| import modelopt.torch.sparsity.state_sparsity as mtss | ||
|
|
||
| model = mtss.calibrate(model, config, measurements) | ||
| deployment_policy = mtss.export_policy(model) | ||
|
|
||
| Args: | ||
| model: Model containing GatedDeltaNet modules with one-dimensional ``A_log`` and | ||
| ``dt_bias`` tensors. | ||
| config: Checkpoint provenance, candidate windows, and quality gates. | ||
| measurements: Quality and checkpoint-storage results produced by the caller's paired | ||
| dense-versus-DASC calibration workflow. | ||
|
|
||
| Returns: | ||
| The input model with a serializable DASC policy attached through ModelOpt state. | ||
| """ | ||
| model = unwrap_model(model, force_unwrap=True) | ||
| config_object = config if isinstance(config, DASCConfig) else DASCConfig(**config) | ||
| if ModeloptStateManager.is_converted(model, is_root=True) and any( | ||
| mode == "dasc" for mode, _ in ModeloptStateManager(model).state_dict() | ||
| ): | ||
| return replace_dasc_mode(model, config_object, measurements) | ||
|
|
||
| return apply_mode( | ||
| model, | ||
| mode=[("dasc", config_object.model_dump())], | ||
| registry=DASCModeRegistry, | ||
| mode_kwargs={"measurements": measurements}, | ||
| ) | ||
|
|
||
|
|
||
| def export_policy(model: nn.Module) -> dict[str, Any]: | ||
| """Export a JSON-safe DASC policy after validating model structure and decay parameters. | ||
|
|
||
| This policy does not implement checkpoint packing or recovery. A serving backend must preserve | ||
| convolution state, store retained complete GDN heads, recover omitted heads according to the | ||
| declared variant, and materialize the ordinary dense runtime state before continuation. | ||
| """ | ||
| policy = get_attached_dasc_policy(model) | ||
| validate_dasc_model_structure(model, policy) | ||
| validate_dasc_decay_parameters(model, policy) | ||
| return copy.deepcopy(policy.model_dump(mode="json")) |
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Show the default quality gates in the example.
Omitting these fields is valid, but
DASCConfigapplies defaults of0.995,0.98, and0.2._candidate_passesuses these values to select candidates, so the example hides its active selection criteria. Add the three fields with their default values.🤖 Prompt for AI Agents