Skip to content
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

[Release 0.3.2] Additional patches to enable compatibility with SparseML, UX changes #43

Merged
merged 5 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 1 addition & 6 deletions src/compressed_tensors/compressors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,5 @@

from .base import ModelCompressor
from .dense import DenseCompressor
from .helpers import (
infer_compressor_from_model_config,
load_compressed,
save_compressed,
save_compressed_model,
)
from .helpers import load_compressed, save_compressed, save_compressed_model
from .sparse_bitmask import BitmaskCompressor, BitmaskTensor
26 changes: 25 additions & 1 deletion src/compressed_tensors/compressors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from torch import Tensor
from torch.nn import Module, Parameter
from tqdm import tqdm
from transformers import AutoConfig


__all__ = ["ModelCompressor"]
Expand All @@ -34,6 +35,29 @@ class ModelCompressor(RegistryMixin):
:param config: config specifying compression parameters
"""

@classmethod
def from_pretrained(
cls, pretrained_model_name_or_path: str
) -> Optional["ModelCompressor"]:
"""
Given a path to a model config, extract a sparsity config if it exists and
return the associated ModelCompressor

:param pretrained_model_name_or_path: path to model config on disk or HF hub
:return: matching compressor if config contains a sparsity config
"""
config = AutoConfig.from_pretrained(pretrained_model_name_or_path)
sparsity_config = getattr(config, SPARSITY_CONFIG_NAME, None)
if sparsity_config is None:
return None

format = sparsity_config.get("format")
sparsity_config = CompressionConfig.load_from_registry(
format, **sparsity_config
)
compressor = cls.load_from_registry(format, config=sparsity_config)
return compressor

def __init__(self, config: Optional[CompressionConfig] = None):
self.config = config

Expand All @@ -47,7 +71,7 @@ def compress(self, model_state: Dict[str, Tensor]) -> Dict[str, Tensor]:
raise NotImplementedError()

def decompress(
self, path_to_model_or_tensors: str
self, path_to_model_or_tensors: str, device: str = "cpu"
) -> Generator[Tuple[str, Tensor], None, None]:
"""
Reads a compressed state dict located at path_to_model_or_tensors
Expand Down
2 changes: 1 addition & 1 deletion src/compressed_tensors/compressors/dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ def compress(self, model_state: Dict[str, Tensor]) -> Dict[str, Tensor]:
return model_state

def decompress(
self, path_to_model_or_tensors: str, device: str
self, path_to_model_or_tensors: str, device: str = "cpu"
) -> Generator[Tuple[str, Tensor], None, None]:
return iter([])
24 changes: 0 additions & 24 deletions src/compressed_tensors/compressors/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,21 @@
from typing import Dict, Generator, Optional, Tuple, Union

import torch
from compressed_tensors.base import SPARSITY_CONFIG_NAME
from compressed_tensors.compressors import ModelCompressor
from compressed_tensors.config import CompressionConfig, CompressionFormat
from compressed_tensors.utils.safetensors_load import get_weight_mappings
from safetensors import safe_open
from safetensors.torch import save_file
from torch import Tensor
from transformers import AutoConfig


__all__ = [
"infer_compressor_from_model_config",
"load_compressed",
"save_compressed",
"save_compressed_model",
]


def infer_compressor_from_model_config(
pretrained_model_name_or_path: str,
) -> Optional[ModelCompressor]:
"""
Given a path to a model config, extract a sparsity config if it exists and return
the associated ModelCompressor

:param pretrained_model_name_or_path: path to model config on disk or HF hub
:return: matching compressor if config contains a sparsity config
"""
config = AutoConfig.from_pretrained(pretrained_model_name_or_path)
sparsity_config = getattr(config, SPARSITY_CONFIG_NAME, None)
if sparsity_config is None:
return None

format = sparsity_config.get("format")
sparsity_config = CompressionConfig.load_from_registry(format, **sparsity_config)
compressor = ModelCompressor.load_from_registry(format, config=sparsity_config)
return compressor


def save_compressed(
tensors: Dict[str, Tensor],
save_path: Union[str, Path],
Expand Down
2 changes: 1 addition & 1 deletion src/compressed_tensors/compressors/sparse_bitmask.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def decompress(
) -> Generator[Tuple[str, Tensor], None, None]:
"""
Reads a bitmask compressed state dict located at path_to_model_or_tensors
and returns a generator for sequentially decompressing back to a dense state dict
and returns a generator for sequentially decompressing back to dense state dict

:param model_path: path to compressed safetensors model (directory with
one or more safetensors files) or compressed tensors file
Expand Down
Loading