llmcompressor.utils
General utility functions used throughout llmcompressor
DisableKVCache
Temporarily disable the key-value cache for transformer models. Used to prevent excess memory use in one-shot cases where the model only performs the prefill phase and not the generation phase.
Example:
model = AutoModel.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0") input = torch.randint(0, 32, size=(1, 32)) with DisableKVCache(model): ... output = model(input)
Source code in src/llmcompressor/utils/helpers.py
NumpyArrayBatcher
Bases: object
Batcher instance to handle taking in dictionaries of numpy arrays, appending multiple items to them to increase their batch size, and then stack them into a single batched numpy array for all keys in the dicts.
Source code in src/llmcompressor/utils/helpers.py
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 |
|
append(item)
Append a new item into the current batch. All keys and shapes must match the current state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item | Union[ndarray, Dict[str, ndarray]] | the item to add for batching | required |
Source code in src/llmcompressor/utils/helpers.py
stack()
Stack the current items into a batch along a new, zeroed dimension
Returns:
Type | Description |
---|---|
Dict[str, ndarray] | the stacked items |
Source code in src/llmcompressor/utils/helpers.py
DisableQuantization(module)
Disable quantization during forward passes after applying a quantization config
Source code in src/llmcompressor/utils/helpers.py
bucket_iterable(val, num_buckets=3, edge_percent=0.05, sort_highest=True, sort_key=None)
Bucket iterable into subarray consisting of the first top percentage followed by the rest of the iterable sliced into equal sliced groups.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val | Iterable[Any] | The iterable to bucket | required |
num_buckets | int | The number of buckets to group the iterable into, does not include the top bucket | 3 |
edge_percent | float | Group the first percent into its own bucket. If sort_highest, then this is the top percent, else bottom percent. If <= 0, then will not create an edge bucket | 0.05 |
sort_highest | bool | True to sort such that the highest percent is first and will create buckets in descending order. False to sort so lowest is first and create buckets in ascending order. | True |
sort_key | Callable[[Any], Any] | The sort_key, if any, to use for sorting the iterable after converting it to a list | None |
Returns:
Type | Description |
---|---|
List[Tuple[int, Any]] | a list of each value mapped to the bucket it was sorted into |
Source code in src/llmcompressor/utils/helpers.py
calibration_forward_context(model)
Context in which all calibration forward passes should occur.
- Remove gradient calculations
- Disable the KV cache
- Disable train mode and enable eval mode
Source code in src/llmcompressor/utils/helpers.py
clean_path(path)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path | str | the directory or file path to clean | required |
Returns:
Type | Description |
---|---|
str | a cleaned version that expands the user path and creates an absolute path |
convert_to_bool(val)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val | Any | the value to be converted to a bool, supports logical values as strings ie True, t, false, 0 | required |
Returns:
Type | Description |
---|---|
the boolean representation of the value, if it can't be determined, falls back on returning True |
Source code in src/llmcompressor/utils/helpers.py
create_dirs(path)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path | str | the directory path to try and create | required |
Source code in src/llmcompressor/utils/helpers.py
create_parent_dirs(path)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path | str | the file path to try to create the parent directories for | required |
create_unique_dir(path, check_number=0)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path | str | the file path to create a unique version of (append numbers until one doesn't exist) | required |
check_number | int | the number to begin checking for unique versions at | 0 |
Returns:
Type | Description |
---|---|
str | the unique directory path |
Source code in src/llmcompressor/utils/helpers.py
flatten_iterable(li)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
li | Iterable | a possibly nested iterable of items to be flattened | required |
Returns:
Type | Description |
---|---|
a flattened version of the list where all elements are in a single list flattened in a depth first pattern |
Source code in src/llmcompressor/utils/helpers.py
getattr_chain(obj, chain_str, *args, **kwargs)
Chain multiple getattr calls, separated by .
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj | Any | base object whose attributes are being retrieved | required |
chain_str | str | attribute names separated by | required |
default | default value, throw error otherwise | required |
Source code in src/llmcompressor/utils/helpers.py
import_from_path(path)
Import the module and the name of the function/class separated by : Examples: path = "/path/to/file.py:func_or_class_name" path = "/path/to/file:focn" path = "path.to.file:focn"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path | str | path including the file path and object name | required |
Source code in src/llmcompressor/utils/helpers.py
interpolate(x_cur, x0, x1, y0, y1, inter_func='linear')
note, caps values at their min of x0 and max x1, designed to not work outside of that range for implementation reasons
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_cur | float | the current value for x, should be between x0 and x1 | required |
x0 | float | the minimum for x to interpolate between | required |
x1 | float | the maximum for x to interpolate between | required |
y0 | Any | the minimum for y to interpolate between | required |
y1 | Any | the maximum for y to interpolate between | required |
inter_func | str | the type of function to interpolate with: linear, cubic, inverse_cubic | 'linear' |
Returns:
Type | Description |
---|---|
Any | the interpolated value projecting x into y for the given interpolation function |
Source code in src/llmcompressor/utils/helpers.py
interpolate_list_linear(measurements, x_val)
interpolate for input values within a list of measurements linearly
Parameters:
Name | Type | Description | Default |
---|---|---|---|
measurements | List[Tuple[float, float]] | the measurements to interpolate the output value between | required |
x_val | Union[float, List[float]] | the target values to interpolate to the second dimension | required |
Returns:
Type | Description |
---|---|
List[Tuple[float, float]] | a list of tuples containing the target values, interpolated values |
Source code in src/llmcompressor/utils/helpers.py
interpolated_integral(measurements)
Calculate the interpolated integal for a group of measurements of the form [(x0, y0), (x1, y1), ...]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
measurements | List[Tuple[float, float]] | the measurements to calculate the integral for | required |
Returns:
Type | Description |
---|---|
the integral or area under the curve for the measurements given |
Source code in src/llmcompressor/utils/helpers.py
is_package_available(package_name, return_version=False)
A helper function to check if a package is available and optionally return its version. This function enforces a check that the package is available and is not just a directory/file with the same name as the package.
inspired from: https://github.com/huggingface/transformers/blob/965cf677695dd363285831afca8cf479cf0c600c/src/transformers/utils/import_utils.py#L41
Parameters:
Name | Type | Description | Default |
---|---|---|---|
package_name | str | The package name to check for | required |
return_version | bool | True to return the version of the package if available | False |
Returns:
Type | Description |
---|---|
Union[Tuple[bool, str], bool] | True if the package is available, False otherwise or a tuple of (bool, version) if return_version is True |
Source code in src/llmcompressor/utils/helpers.py
is_url(val)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val | str | value to check if it is a url or not | required |
Returns:
Type | Description |
---|---|
True if value is a URL, False otherwise |
Source code in src/llmcompressor/utils/helpers.py
json_to_jsonl(json_file_path, overwrite=True)
Converts a json list file to jsonl file format (used for sharding efficienty) e.x. [{"a": 1}, {"a": 1}] would convert to: {"a": 1}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_file_path | str | file path to a json file path containing a json list of objects | required |
overwrite | bool | If True, the existing json file will be overwritten, if False, the file will have the same name but with a .jsonl extension | True |
Source code in src/llmcompressor/utils/helpers.py
load_labeled_data(data, labels, raise_on_error=True)
Load labels and data from disk or from memory and group them together. Assumes sorted ordering for on disk. Will match between when a file glob is passed for either data and/or labels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data | Union[str, Iterable[Union[str, ndarray, Dict[str, ndarray]]]] | the file glob, file path to numpy data tar ball, or list of arrays to use for data | required |
labels | Union[None, str, Iterable[Union[str, ndarray, Dict[str, ndarray]]]] | the file glob, file path to numpy data tar ball, or list of arrays to use for labels, if any | required |
raise_on_error | bool | True to raise on any error that occurs; False to log a warning, ignore, and continue | True |
Returns:
Type | Description |
---|---|
List[Tuple[Union[ndarray, Dict[str, ndarray]], Union[None, ndarray, Dict[str, ndarray]]]] | a list containing tuples of the data, labels. If labels was passed in as None, will now contain a None for the second index in each tuple |
Source code in src/llmcompressor/utils/helpers.py
load_numpy(file_path)
Load a numpy file into either an ndarray or an OrderedDict representing what was in the npz file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path | str | the file_path to load | required |
Returns:
Type | Description |
---|---|
Union[ndarray, Dict[str, ndarray]] | the loaded values from the file |
Source code in src/llmcompressor/utils/helpers.py
patch_attr(base, attr, value)
Patch the value of an object attribute. Original value is restored upon exit
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base | object | object which has the attribute to patch | required |
attr | str | name of the the attribute to patch | required |
value | Any | used to replace original value Usage: >>> from types import SimpleNamespace >>> obj = SimpleNamespace() >>> with patch_attr(obj, "attribute", "value"): ... assert obj.attribute == "value" >>> assert not hasattr(obj, "attribute") | required |
Source code in src/llmcompressor/utils/helpers.py
path_file_count(path, pattern='*')
Return the number of files that match the given pattern under the given path
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path | str | the path to the directory to look for files under | required |
pattern | str | the pattern the files must match to be counted | '*' |
Returns:
Type | Description |
---|---|
int | the number of files matching the pattern under the directory |
Source code in src/llmcompressor/utils/helpers.py
path_file_size(path)
Return the total size, in bytes, for a path on the file system
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path | str | the path (directory or file) to get the size for | required |
Returns:
Type | Description |
---|---|
int | the size of the path, in bytes, as stored on disk |
Source code in src/llmcompressor/utils/helpers.py
save_numpy(array, export_dir, name, npz=True)
Save a numpy array or collection of numpy arrays to disk
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array | Union[ndarray, Dict[str, ndarray], Iterable[ndarray]] | the array or collection of arrays to save | required |
export_dir | str | the directory to export the numpy file into | required |
name | str | the name of the file to export to (without extension) | required |
npz | bool | True to save as an npz compressed file, False for standard npy. Note, npy can only be used for single numpy arrays | True |
Returns:
Type | Description |
---|---|
the saved path |
Source code in src/llmcompressor/utils/helpers.py
tensor_export(tensor, export_dir, name, npz=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor | Union[ndarray, Dict[str, ndarray], Iterable[ndarray]] | tensor to export to a saved numpy array file | required |
export_dir | str | the directory to export the file in | required |
name | str | the name of the file, .npy will be appended to it | required |
npz | bool | True to export as an npz file, False otherwise | True |
Returns:
Type | Description |
---|---|
str | the path of the numpy file the tensor was exported to |
Source code in src/llmcompressor/utils/helpers.py
tensors_export(tensors, export_dir, name_prefix, counter=0, break_batch=False)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensors | Union[ndarray, Dict[str, ndarray], Iterable[ndarray]] | the tensors to export to a saved numpy array file | required |
export_dir | str | the directory to export the files in | required |
name_prefix | str | the prefix name for the tensors to save as, will append info about the position of the tensor in a list or dict in addition to the .npy file format | required |
counter | int | the current counter to save the tensor at | 0 |
break_batch | bool | treat the tensor as a batch and break apart into multiple tensors | False |
Returns:
Type | Description |
---|---|
List[str] | the exported paths |
Source code in src/llmcompressor/utils/helpers.py
validate_str_iterable(val, error_desc='')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val | Union[str, Iterable[str]] | the value to validate, check that it is a list (and flattens it), otherwise checks that it's an ALL or ALL_PRUNABLE string, otherwise raises a ValueError | required |
error_desc | str | the description to raise an error with in the event that the val wasn't valid | '' |
Returns:
Type | Description |
---|---|
Union[str, Iterable[str]] | the validated version of the param |