Skip to content

llmcompressor.pytorch.model_load.helpers

fallback_to_cpu(device)

Takes in a device string and forces it to cpu if cuda is not available

Parameters:

Name Type Description Default
device str

device id to check

required

Returns:

Type Description
str

device modified for CUDA status

Source code in src/llmcompressor/pytorch/model_load/helpers.py
def fallback_to_cpu(device: str) -> str:
    """
    Takes in a device string and forces it to cpu if cuda is not available

    :param device: device id to check
    :return: device modified for CUDA status
    """
    if "cuda" in device and not torch.cuda.is_available():
        logger.warning(
            f"Requested {device} but CUDA is not available, falling back to CPU"
        )
        return "cpu"

    return device

get_completed_stages(checkpoint_dir)

Given a checkpoint directory for a staged run, get the list of stages that have completed in a prior run if the checkpoint_dir is a string

Parameters:

Name Type Description Default
checkpoint_dir Any

path to staged checkpoint

required

Returns:

Type Description
List[str]

list of completed stage names

Source code in src/llmcompressor/pytorch/model_load/helpers.py
def get_completed_stages(checkpoint_dir: Any) -> List[str]:
    """
    Given a checkpoint directory for a staged run, get the list of stages that
    have completed in a prior run if the checkpoint_dir is a string

    :param checkpoint_dir: path to staged checkpoint
    :return: list of completed stage names
    """
    if isinstance(checkpoint_dir, str):
        stage_path = os.path.join(checkpoint_dir, COMPLETED_STAGES_FILENAME)
        if os.path.exists(stage_path):
            with open(stage_path) as stage_file:
                stage_data = json.load(stage_file)
                return stage_data["completed"]

    return []

get_session_model()

Returns:

Type Description
Optional[Module]

pytorch module stored by the active CompressionSession, or None if no session is active

Source code in src/llmcompressor/pytorch/model_load/helpers.py
def get_session_model() -> Optional[Module]:
    """
    :return: pytorch module stored by the active CompressionSession,
        or None if no session is active
    """
    session = active_session()
    if not session:
        return None

    active_model = session.state.model
    return active_model

load_safetensors_state_dict(file_path)

Load a safetensors file from disk

Parameters:

Name Type Description Default
file_path str

path to the safetensors file

required

Returns:

Type Description
Dict[str, Tensor]

dictionary of safetensors data

Source code in src/llmcompressor/pytorch/model_load/helpers.py
def load_safetensors_state_dict(file_path: str) -> Dict[str, torch.Tensor]:
    """
    Load a safetensors file from disk

    :param file_path: path to the safetensors file
    :return: dictionary of safetensors data
    """
    with safe_open(file_path, framework="pt", device="cpu") as f:
        return {key: f.get_tensor(key) for key in f.keys()}

parse_dtype(dtype_arg)

Parameters:

Name Type Description Default
dtype_arg Union[str, dtype]

dtype or string to parse

required

Returns:

Type Description
dtype

torch.dtype parsed from input string

Source code in src/llmcompressor/pytorch/model_load/helpers.py
def parse_dtype(dtype_arg: Union[str, torch.dtype]) -> torch.dtype:
    """
    :param dtype_arg: dtype or string to parse
    :return: torch.dtype parsed from input string
    """
    dtype_arg = str(dtype_arg)
    dtype = "auto"  # get precision from model by default
    if dtype_arg in ("half", "float16", "torch.float16"):
        dtype = torch.float16
    elif dtype_arg in ("torch.bfloat16", "bfloat16"):
        dtype = torch.bfloat16
    elif dtype_arg in ("full", "float32", "torch.float32"):
        dtype = torch.float32

    return dtype

save_checkpoint(save_path, model, processor=None, save_safetensors=True, save_compressed=True, skip_sparsity_compression_stats=False)

Save a model, processor, and recipe

Parameters:

Name Type Description Default
save_path str

Path used to save model and processor

required
model PreTrainedModel

model to save

required
processor Optional[Processor]

processor to save

None
save_safetensors bool

save model checkpoint using safetensors file type

True
save_compressed bool

save model checkpoint using compressed-tensors format

True
Source code in src/llmcompressor/pytorch/model_load/helpers.py
def save_checkpoint(
    save_path: str,
    model: PreTrainedModel,
    processor: Optional[Processor] = None,
    save_safetensors: bool = True,
    save_compressed: bool = True,
    skip_sparsity_compression_stats: bool = False,
):
    """
    Save a model, processor, and recipe

    :param save_path: Path used to save model and processor
    :param model: model to save
    :param processor: processor to save
    :param save_safetensors: save model checkpoint using safetensors file type
    :param save_compressed: save model checkpoint using compressed-tensors format
    """
    # saving the model also saves the recipe
    model.save_pretrained(
        save_path,
        save_safetensors=save_safetensors,
        save_compressed=save_compressed,
        skip_sparsity_compression_stats=skip_sparsity_compression_stats,
    )
    if processor is not None:
        processor.save_pretrained(save_path)

save_completed_stages(checkpoint_dir, completed_stages)

Save a list of completed stages to a checkpoint directory

Parameters:

Name Type Description Default
checkpoint_dir str

model checkpoint directory to save stages to

required
completed_stages List[str]

list of stage names that have been run

required
Source code in src/llmcompressor/pytorch/model_load/helpers.py
def save_completed_stages(checkpoint_dir: str, completed_stages: List[str]):
    """
    Save a list of completed stages to a checkpoint directory

    :param checkpoint_dir: model checkpoint directory to save stages to
    :param completed_stages: list of stage names that have been run
    """
    stage_path = os.path.join(checkpoint_dir, COMPLETED_STAGES_FILENAME)
    with open(stage_path, "w") as out_file:
        json.dump({"completed": completed_stages}, out_file)