Skip to content
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
7 changes: 5 additions & 2 deletions src/llmcompressor/modifiers/quantization/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
QuantizationStrategy,
)
from compressed_tensors.quantization.lifecycle.forward import forward_quantize
from compressed_tensors.utils import align_module_device, update_offload_parameter
from compressed_tensors.utils import (
align_module_device,
getattr_chain,
update_offload_parameter,
)
from loguru import logger
from torch.nn import Module

from llmcompressor.observers import Observer
from llmcompressor.utils.helpers import getattr_chain

__all__ = [
"initialize_observer",
Expand Down
2 changes: 1 addition & 1 deletion src/llmcompressor/pipelines/independent/pipeline.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import TYPE_CHECKING

import torch
from compressed_tensors.utils import patch_attr
from loguru import logger
from torch.utils.data.dataloader import DataLoader

from llmcompressor.core import active_session
from llmcompressor.pipelines.registry import CalibrationPipeline
from llmcompressor.utils.helpers import patch_attr

if TYPE_CHECKING:
from llmcompressor.args.dataset_arguments import DatasetArguments
Expand Down
2 changes: 1 addition & 1 deletion src/llmcompressor/pipelines/sequential/ast_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from typing import List

import torch
from compressed_tensors.utils import patch_attr

from llmcompressor.pipelines.sequential.ast_utils.auto_wrapper import AutoWrapper
from llmcompressor.utils import patch_attr

__all__ = ["autowrap_forwards", "append_autowrap_source_on_fail"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import builtins
from typing import Set, Tuple

from llmcompressor.utils import patch_attr
from compressed_tensors.utils import patch_attr


class NameAnalyzer(ast.NodeVisitor):
Expand Down
3 changes: 2 additions & 1 deletion src/llmcompressor/pipelines/sequential/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from compressed_tensors.utils import (
has_offloaded_params,
offloaded_dispatch,
patch_attr,
remove_dispatch,
)
from compressed_tensors.utils.match import match_targets
Expand All @@ -24,7 +25,7 @@
from llmcompressor.modifiers import Modifier
from llmcompressor.modifiers.utils.hooks import HooksMixin
from llmcompressor.pipelines.sequential.transformers_helpers import HFTracer
from llmcompressor.utils.helpers import calibration_forward_context, patch_attr
from llmcompressor.utils.helpers import calibration_forward_context
from llmcompressor.utils.pytorch.module import get_no_split_params

from .ast_helpers import append_autowrap_source_on_fail, autowrap_forwards
Expand Down
4 changes: 1 addition & 3 deletions src/llmcompressor/utils/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
import torch
from accelerate import dispatch_model, infer_auto_device_map
from accelerate.utils import get_balanced_memory
from compressed_tensors.utils import remove_dispatch
from compressed_tensors.utils import patch_attr, remove_dispatch
from huggingface_hub import snapshot_download
from safetensors.torch import save_file
from transformers import AutoModelForCausalLM, PreTrainedModel
from transformers.modeling_utils import TORCH_INIT_FUNCTIONS
from transformers.utils import SAFE_WEIGHTS_INDEX_NAME, WEIGHTS_INDEX_NAME

from llmcompressor.utils.helpers import patch_attr

__all__ = [
"skip_weights_download",
"patch_transformers_logger_level",
Expand Down
31 changes: 31 additions & 0 deletions src/llmcompressor/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import numpy
import torch
from compressed_tensors.quantization import disable_quantization, enable_quantization
from compressed_tensors.utils import deprecated
from loguru import logger
from transformers import PreTrainedModel

Expand Down Expand Up @@ -88,6 +89,7 @@
##############################


@deprecated()
def flatten_iterable(li: Iterable):
"""
:param li: a possibly nested iterable of items to be flattened
Expand All @@ -105,6 +107,7 @@ def _flatten_gen(_li):
return list(_flatten_gen(li))


@deprecated()
def convert_to_bool(val: Any):
"""
:param val: the value to be converted to a bool,
Expand All @@ -119,6 +122,7 @@ def convert_to_bool(val: Any):
)


@deprecated()
def validate_str_iterable(
val: Union[str, Iterable[str]], error_desc: str = ""
) -> Union[str, Iterable[str]]:
Expand All @@ -144,6 +148,7 @@ def validate_str_iterable(
raise ValueError("unsupported type ({}) given in {}".format(val, error_desc))


@deprecated()
def bucket_iterable(
val: Iterable[Any],
num_buckets: int = 3,
Expand Down Expand Up @@ -191,6 +196,7 @@ def bucket_iterable(
INTERPOLATION_FUNCS = ["linear", "cubic", "inverse_cubic"]


@deprecated(future_name="torch.lerp")
def interpolate(
x_cur: float, x0: float, x1: float, y0: Any, y1: Any, inter_func: str = "linear"
) -> Any:
Expand Down Expand Up @@ -243,6 +249,7 @@ def interpolate(
return y_per * (y1 - y0) + y0


@deprecated(future_name="torch.lerp")
def interpolate_list_linear(
measurements: List[Tuple[float, float]], x_val: Union[float, List[float]]
) -> List[Tuple[float, float]]:
Expand Down Expand Up @@ -279,6 +286,7 @@ def interpolate_list_linear(
return interpolated


@deprecated(future_name="torch.lerp")
def interpolated_integral(measurements: List[Tuple[float, float]]):
"""
Calculate the interpolated integal for a group of measurements of the form
Expand Down Expand Up @@ -308,6 +316,7 @@ def interpolated_integral(measurements: List[Tuple[float, float]]):
return integral


@deprecated()
def clean_path(path: str) -> str:
"""
:param path: the directory or file path to clean
Expand All @@ -316,6 +325,7 @@ def clean_path(path: str) -> str:
return os.path.abspath(os.path.expanduser(path))


@deprecated()
def create_dirs(path: str):
"""
:param path: the directory path to try and create
Expand All @@ -332,6 +342,7 @@ def create_dirs(path: str):
raise


@deprecated()
def create_parent_dirs(path: str):
"""
:param path: the file path to try to create the parent directories for
Expand All @@ -340,6 +351,7 @@ def create_parent_dirs(path: str):
create_dirs(parent)


@deprecated()
def create_unique_dir(path: str, check_number: int = 0) -> str:
"""
:param path: the file path to create a unique version of
Expand All @@ -355,6 +367,7 @@ def create_unique_dir(path: str, check_number: int = 0) -> str:
return create_unique_dir(path, check_number + 1)


@deprecated()
def path_file_count(path: str, pattern: str = "*") -> int:
"""
Return the number of files that match the given pattern under the given path
Expand All @@ -368,6 +381,7 @@ def path_file_count(path: str, pattern: str = "*") -> int:
return len(fnmatch.filter(os.listdir(path), pattern))


@deprecated()
def path_file_size(path: str) -> int:
"""
Return the total size, in bytes, for a path on the file system
Expand Down Expand Up @@ -405,6 +419,7 @@ def path_file_size(path: str) -> int:
return total_size


@deprecated()
def is_url(val: str):
"""
:param val: value to check if it is a url or not
Expand All @@ -429,6 +444,7 @@ def is_url(val: str):
NDARRAY_KEY = "ndarray"


@deprecated()
def load_numpy(file_path: str) -> Union[numpy.ndarray, Dict[str, numpy.ndarray]]:
"""
Load a numpy file into either an ndarray or an OrderedDict representing what
Expand All @@ -449,6 +465,7 @@ def load_numpy(file_path: str) -> Union[numpy.ndarray, Dict[str, numpy.ndarray]]
return array


@deprecated()
def save_numpy(
array: Union[numpy.ndarray, Dict[str, numpy.ndarray], Iterable[numpy.ndarray]],
export_dir: str,
Expand Down Expand Up @@ -488,6 +505,7 @@ def save_numpy(
return export_path


@deprecated()
def _fix_loaded_numpy(array) -> Union[numpy.ndarray, Dict[str, numpy.ndarray]]:
if not isinstance(array, numpy.ndarray):
tmp_arrray = array
Expand All @@ -498,6 +516,7 @@ def _fix_loaded_numpy(array) -> Union[numpy.ndarray, Dict[str, numpy.ndarray]]:
return array


@deprecated()
def load_numpy_from_tar(
path: str,
) -> List[Union[numpy.ndarray, Dict[str, numpy.ndarray]]]:
Expand All @@ -522,6 +541,7 @@ def load_numpy_from_tar(
return data


@deprecated()
def load_numpy_list(
data: Union[str, Iterable[Union[str, numpy.ndarray, Dict[str, numpy.ndarray]]]],
) -> List[Union[numpy.ndarray, Dict[str, numpy.ndarray]]]:
Expand Down Expand Up @@ -553,6 +573,7 @@ def load_numpy_list(
return loaded


@deprecated()
def load_labeled_data(
data: Union[str, Iterable[Union[str, numpy.ndarray, Dict[str, numpy.ndarray]]]],
labels: Union[
Expand Down Expand Up @@ -631,6 +652,7 @@ def __len__(self):

return len(self._items[list(self._items.keys())[0]])

@deprecated()
def append(self, item: Union[numpy.ndarray, Dict[str, numpy.ndarray]]):
"""
Append a new item into the current batch.
Expand Down Expand Up @@ -684,6 +706,7 @@ def append(self, item: Union[numpy.ndarray, Dict[str, numpy.ndarray]]):

self._items[key].append(val)

@deprecated()
def stack(self) -> Dict[str, numpy.ndarray]:
"""
Stack the current items into a batch along a new, zeroed dimension
Expand All @@ -698,6 +721,7 @@ def stack(self) -> Dict[str, numpy.ndarray]:
return batch_dict


@deprecated()
def tensor_export(
tensor: Union[numpy.ndarray, Dict[str, numpy.ndarray], Iterable[numpy.ndarray]],
export_dir: str,
Expand Down Expand Up @@ -734,6 +758,7 @@ def tensor_export(
return export_path


@deprecated()
def tensors_export(
tensors: Union[numpy.ndarray, Dict[str, numpy.ndarray], Iterable[numpy.ndarray]],
export_dir: str,
Expand Down Expand Up @@ -765,6 +790,7 @@ def tensors_export(
return exported_paths


@deprecated()
def _tensors_export_recursive(
tensors: Union[numpy.ndarray, Iterable[numpy.ndarray]],
export_dir: str,
Expand Down Expand Up @@ -799,6 +825,7 @@ def _tensors_export_recursive(
)


@deprecated()
def _tensors_export_batch(
tensors: Union[numpy.ndarray, Dict[str, numpy.ndarray], Iterable[numpy.ndarray]],
export_dir: str,
Expand Down Expand Up @@ -845,6 +872,7 @@ def _tensors_export_batch(
)


@deprecated()
def json_to_jsonl(json_file_path: str, overwrite: bool = True):
"""
Converts a json list file to jsonl file format (used for sharding efficienty)
Expand Down Expand Up @@ -876,6 +904,7 @@ def json_to_jsonl(json_file_path: str, overwrite: bool = True):
jsonl_file.write("\n") # newline


@deprecated()
def deprecation_warning(message: str):
warnings.simplefilter("always", DeprecationWarning)
warnings.warn(
Expand Down Expand Up @@ -946,6 +975,7 @@ def import_from_path(path: str) -> str:
raise AttributeError(f"Cannot find {class_name} in {_path}")


@deprecated()
def getattr_chain(obj: Any, chain_str: str, *args, **kwargs) -> Any:
"""
Chain multiple getattr calls, separated by `.`
Expand Down Expand Up @@ -1063,6 +1093,7 @@ def calibration_forward_context(model: torch.nn.Module):
yield


@deprecated()
@contextlib.contextmanager
def disable_lm_head(model: torch.nn.Module):
"""
Expand Down
Loading