LLM Compressor with Alauda AI
This document describes how to use the LLM Compressor integration with the Alauda AI platform to perform model compression workflows. The Alauda AI integration of LLM Compressor provides two example workflows:
- A workbench image and the data-free-compressor.ipynb that demonstrate how to compress a model.
- A workbench image and the calibration-compressor.ipynb that demonstrate how to compress a model using a calibration dataset.
notebook
TOC
Supported Model Compression WorkflowsCreate a WorkbenchPrepare the Model Artifact(Optional) Prepare and Upload a Dataset(Optional) Upload Dataset into S3-Compatible Object Storage(Optional) Use Dataset from S3-Compatible Object StorageDownload Models and Datasets in JupyterLabCreate and Run Compression NotebooksRegister the Compressed ModelDeploy and Use the Compressed Model for InferenceSupported Model Compression Workflows
On the Alauda AI platform, you can use the Workbench feature to run LLM Compressor on models stored in S3, a PVC, or an OCI registry. The following workflow outlines the typical steps for compressing a model.
Create a Workbench
Follow the instructions in Create Workbench to create a new Workbench instance. When creating the Workbench, make sure to select the image odh-workbench-jupyter-pytorch-llmcompressor-cuda-py312-ubi9, which includes the required LLM Compressor environment. Model compression is currently supported only within JupyterLab.
If this image is not available in your environment, refer to the linked article for instructions on how to synchronize the image and patch the WorkspaceKind resource so that the image becomes available in the Workbench creation options.
Prepare the Model Artifact
Download the model into the Workbench or stage it on a PVC/S3 location accessible from the Workbench. After compression, upload the output to a supported storage backend. See Upload Models Using a Workbench. Register the resulting Model URI as a new Model Registry version using Model Registry. The example notebooks in this guide use the TinyLlama-1.1B-Chat-v1.0 model.
- Model to compress. You can modify this line if you want to use your own
model. 2. This recipe will quantize all Linear layers except those in the
lm_head, which is often sensitive to quantization. TheW4A16scheme compresses weights to4-bitintegers while retaining16-bitactivations.
(Optional) Prepare and Upload a Dataset
If you plan to use the data-free compressor notebook, you can skip this step.
To use the calibration compressor notebook, you must prepare and upload a calibration dataset. Prepare your dataset using the same process described in Upload Models Using Notebook. The example calibration notebook uses the ultrachat_200k dataset.
- Create the calibration dataset, using Huggingface datasets API. You can modify this line if you want to use your own dataset. 2. Select number of samples. 512 samples is a good place to start. Increasing the number of samples can improve accuracy. 3. Load dataset. 4. Shuffle and grab only the number of samples we need. 5. Preprocess and tokenize into format the model uses.
(Optional) Upload Dataset into S3-Compatible Object Storage
If you want to upload datasets into S3-compatible object storage, you can run the following code in JupyterLab. Alauda AI supports S3-compatible storage access, and in typical product deployments the storage implementation is Ceph object storage, so you can use the standard boto3 client.
- You can modify this line if you want to use your own dataset. 2. Configure multipart upload with 100 MB chunks and a maximum of 10 concurrent threads.
(Optional) Use Dataset from S3-Compatible Object Storage
If you want to use datasets stored in S3-compatible object storage, first install the s3fs tool and then modify the dataset loading section in the example as shown below. In Alauda AI environments, this S3-compatible storage is typically backed by Ceph object storage.
- Set environment variables (as a backup, some underlying components will use them). Replace
<access-key>and<secret-key>with the credentials of the object storage account. - Define storage configuration; you must explicitly specify the
endpoint_urlfor your S3-compatible object storage service, such as a Ceph object storage endpoint. - If the dataset is split, this is equivalent to
split="train_sft"in the example.
Download Models and Datasets in JupyterLab
In the JupyterLab terminal, use the hf command-line tool or your approved
object-storage client to download the model and dataset into the workspace. The
data-free compressor notebook does not require a dataset.
We recommend using the Hugging Face (hf) command-line tool to download models and datasets directly when network access allows it. This approach is usually faster and simpler than cloning repositories manually.
If you are in a restricted network environment, you can use the Hugging Face mirror for accelerated access:
You can use the hf command-line tool to download models and datasets directly. For example, to download the TinyLlama model:
For calibration datasets, download them similarly:
Create and Run Compression Notebooks
Download the appropriate example notebook for your use case: the calibration compressor notebook if you are using a dataset, or the data-free compressor notebook otherwise. Click the upward arrow button on the JupyterLab page to upload the downloaded notebook file.
Register the Compressed Model
Once compression is complete, upload the compressed model to S3, a PVC, or an OCI registry using Upload Models Using a Workbench. Then register its Model URI as a new Model Registry version using Model Registry.
- Save model and tokenizer. You can modify this line if you want to change the name of output.
Deploy and Use the Compressed Model for Inference
Quantized and sparse models that you create with LLM Compressor are saved using the compressed-tensors library (an extension of Safetensors).
The compression format matches the model's quantization or sparsity type. These formats are natively supported in vLLM, enabling fast inference through optimized deployment kernels by using Alauda AI Inference Server.
Follow the instructions in create inference service to complete this step.