Skip to content

[Hotfix] Implement method on dense compressor #345

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

Merged
merged 2 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions src/compressed_tensors/compressors/sparse_compressors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
# limitations under the License.

import logging
from typing import TYPE_CHECKING, Dict, Generator, Optional, Set, Tuple
from typing import Dict, Generator, Optional, Set, Tuple

import torch
from compressed_tensors.compressors.base import BaseCompressor
from compressed_tensors.utils import (
get_nested_mappings_from_state_dict,
Expand All @@ -27,10 +26,6 @@
from tqdm import tqdm


if TYPE_CHECKING:
from compressed_tensors.quantization import QuantizationScheme


__all__ = ["BaseSparseCompressor"]

_LOGGER: logging.Logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -205,16 +200,3 @@ def should_compress(name: str, expanded_targets: Optional[Set[str]] = None) -> b
return (
name.endswith(".weight") and name[: -(len(".weight"))] in expanded_targets
)

def decompress_module_from_state_dict(
self,
prefix: str,
state_dict: Dict[str, torch.Tensor],
scheme: "QuantizationScheme",
) -> Dict[str, torch.Tensor]:
"""
This function is implemented as a workaround because of how
`ModelCompressor.quantization_compressor` can be set to either
an instance of `BaseQuantizationCompressor` or `BaseSparseCompressor`.
"""
return state_dict.copy()
20 changes: 19 additions & 1 deletion src/compressed_tensors/compressors/sparse_compressors/dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Dict, Generator, Tuple
from typing import TYPE_CHECKING, Dict, Generator, Tuple

import torch
from compressed_tensors.compressors.base import BaseCompressor
from compressed_tensors.config import CompressionFormat
from torch import Tensor


if TYPE_CHECKING:
from compressed_tensors.quantization import QuantizationScheme


@BaseCompressor.register(name=CompressionFormat.dense.value)
class DenseCompressor(BaseCompressor):
"""
Expand Down Expand Up @@ -47,3 +52,16 @@ def decompress_from_state_dict(
) -> Generator[Tuple[str, Dict[str, Tensor]], None, None]:
for key, value in state_dict.items():
yield key, value

def decompress_module_from_state_dict(
self,
prefix: str,
state_dict: Dict[str, torch.Tensor],
scheme: "QuantizationScheme",
) -> Dict[str, torch.Tensor]:
"""
This function is implemented as a workaround because of how
`ModelCompressor.quantization_compressor` can be set to either
an instance of `BaseQuantizationCompressor` or `DenseCompressor`.
"""
return state_dict.copy()