|
1 |
| -from typing import TYPE_CHECKING |
| 1 | +from typing import TYPE_CHECKING, Union |
2 | 2 |
|
3 | 3 | import torch
|
4 | 4 | import tqdm
|
|
7 | 7 |
|
8 | 8 | from llmcompressor.core import LifecycleCallbacks
|
9 | 9 | from llmcompressor.modifiers.utils.pytorch_helpers import apply_pad_mask_to_batch
|
| 10 | +from llmcompressor.pipelines.registry import CalibrationPipeline |
10 | 11 | from llmcompressor.pytorch.utils.helpers import tensors_to_device
|
11 | 12 | from llmcompressor.utils.helpers import calibration_forward_context
|
12 | 13 |
|
13 | 14 | if TYPE_CHECKING:
|
14 | 15 | from llmcompressor.args.dataset_arguments import DatasetArguments
|
15 | 16 |
|
16 |
| -__all__ = ["run_pipeline"] |
| 17 | +__all__ = ["BasicPipeline", "run_calibration"] |
17 | 18 |
|
18 | 19 |
|
19 |
| -def run_pipeline( |
20 |
| - model: torch.nn.Module, |
21 |
| - dataloader: DataLoader, |
22 |
| - dataset_args: "DatasetArguments", |
23 |
| -): |
24 |
| - """ |
25 |
| - Run a basic data pipeline. |
| 20 | +@CalibrationPipeline.register("basic") |
| 21 | +class BasicPipeline(CalibrationPipeline): |
| 22 | + @staticmethod |
| 23 | + def __call__( |
| 24 | + model: torch.nn.Module, |
| 25 | + dataloader: DataLoader, |
| 26 | + dataset_args: Union["DatasetArguments", None], |
| 27 | + ): |
| 28 | + """ |
| 29 | + Run a basic data pipeline. |
26 | 30 |
|
27 |
| - Batches are fetched from the data loader and are used to perform forward passes |
28 |
| - through the model. This pipeline is typically used for basic model calibration |
29 |
| - and, unlike the sequential pipelines, does not propagate compression error when |
30 |
| - used to calibrate model compression |
| 31 | + Batches are fetched from the data loader and are used to perform forward passes |
| 32 | + through the model. This pipeline is typically used for basic model calibration |
| 33 | + and, unlike the sequential pipelines, does not propagate compression error when |
| 34 | + used to calibrate model compression |
31 | 35 |
|
32 |
| - :param model: model being calibrated |
33 |
| - :param dataloader: loads data for calibration |
34 |
| - :param dataset_args: dataset arguments relevant to pipelines |
35 |
| - """ |
36 |
| - model_device = get_execution_device(model) |
| 36 | + :param model: model being calibrated |
| 37 | + :param dataloader: loads data for calibration |
| 38 | + :param dataset_args: dataset arguments relevant to pipelines |
| 39 | + """ |
| 40 | + model_device = get_execution_device(model) |
37 | 41 |
|
38 |
| - LifecycleCallbacks.calibration_epoch_start() |
| 42 | + LifecycleCallbacks.calibration_epoch_start() |
39 | 43 |
|
40 |
| - with calibration_forward_context(model): |
41 |
| - for batch in tqdm.tqdm(dataloader, desc="Calibrating"): |
42 |
| - batch = apply_pad_mask_to_batch(batch) |
43 |
| - batch = tensors_to_device(batch, model_device) |
44 |
| - model(**batch) |
| 44 | + with calibration_forward_context(model): |
| 45 | + for batch in tqdm.tqdm(dataloader, desc="Calibrating"): |
| 46 | + batch = apply_pad_mask_to_batch(batch) |
| 47 | + batch = tensors_to_device(batch, model_device) |
| 48 | + model(**batch) |
45 | 49 |
|
46 |
| - LifecycleCallbacks.calibration_epoch_end() |
| 50 | + LifecycleCallbacks.calibration_epoch_end() |
| 51 | + |
| 52 | + |
| 53 | +def run_calibration(model: torch.nn.Module, dataloader: DataLoader): |
| 54 | + pipeline = BasicPipeline() |
| 55 | + pipeline(model, dataloader, None) |
0 commit comments