diff --git a/AWQ/README.md b/AWQ/README.md index f9c0496..fa27dbf 100644 --- a/AWQ/README.md +++ b/AWQ/README.md @@ -6,14 +6,32 @@ In order to target weight and activation scaling locations within the model, the ## Installations +## LUMI + +To run AWQ quantization scripts on LUMI, you can use the `gptqmodel` library that already ships inside the LUMI AI Factory container — no extra packages or virtual environment are required for AWQ. `torch` , `transformers`, and `datasets` are also already provided in the container. + +The script loads the `Singularity` container environment and sets the container image path: + +```bash +module purge +module use /appl/local/laifs/modules +module load lumi-aif-singularity-bindings + +export SIF=/appl/local/laifs/containers/lumi-multitorch-u24r70f21m50t210-20260731_122833/lumi-multitorch-full-u24r70f21m50t210-20260731_122833.sif + +``` + +--- + +## Roihu + The CSC preinstalled PyTorch module covers most of the libraries needed to run these examples -(torch, transformers, datasets, accelerate). The rest can be installed on top of the module in a virtual environment. +(torch, transformers, datasets, accelerate). Llmcompressor can be installed on top of the module in a virtual environment. ### Load the module ```bash module purge -module use /appl/local/csc/modulefiles -module load pytorch/2.7 +module load python-pytorch/2.10 ``` ### Create and activate a virtual environment using system packages ```bash @@ -21,18 +39,18 @@ python3 -m venv --system-site-packages venv source venv/bin/activate ``` ### Install packages + ```bash -pip install optimum==1.27.0 llmcompressor==0.7.1 --cache-dir ./.pip-cache -``` -The flag --cache-dir points the pip cache to the current (scratch) folder instead of the default (home directory), to avoid filling up home directory quota. +(venv)> pip install llmcompressor==0.12.0 --cache-dir ./.pip-cache +``` +--- ## Usage The launch scripts are: -- `run-awq-modifier-lumi.sh` - quantizes model on LUMI with 1 GPU -- `run-awq-modifier-mahti.sh` - quantizes model on Mahti with 1 GPU -- `run-awq-modifier-puhti.sh` - quantizes model on Puhti with 1 GPU +- `run-awq-modifier-lumi.sh` - quantizes model on LUMI with 1 GPU +- `run-awq-modifier-roihu.sh` - quantizes model on Roihu with 1 GPU **Note:** the scripts are made to be run on `gputest` or `dev-g` partition with a 30 minutes time-limit. You have to select the proper partition for longer jobs for your real runs. Additionally, change the `--account` parameter to your own project code. @@ -55,6 +73,6 @@ Meaning the script quantizes the model’s linear layers using a mixed-precision - Model size (MB) before and after quantization. ## Notes -- The current scripts use **Falcon-RW-1B** for fast experimentation. You can replace `model_name` with a larger model. In this case, you might want to disable saving the full model. +- The current scripts use **TinyLlama-1.1B-Chat-v1.0** for fast experimentation. You can replace `model_name` with a larger model. In this case, you might want to disable saving the full model. - For large models, `device_map="auto"` allows the model modules to be moved between the CPU and GPU for quantization. - Feel free to experiment with different values for `num_calibration_samples` and `max_seq_lenght` and to modify the quantization recipe. diff --git a/AWQ/awq-modifier.py b/AWQ/awq-modifier.py index fe70eea..3ea2cf2 100644 --- a/AWQ/awq-modifier.py +++ b/AWQ/awq-modifier.py @@ -11,7 +11,7 @@ from llmcompressor.modifiers.awq import AWQModifier from llmcompressor.utils import dispatch_for_generation -model_name = "tiiuae/falcon-rw-1b" +model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" dataset_name = "HuggingFaceH4/ultrachat_200k" dataset_split = "train_sft" num_calibration_samples = 256 diff --git a/AWQ/run-awq-modifier-lumi.sh b/AWQ/run-awq-modifier-lumi.sh index 61cfd1c..dc86524 100644 --- a/AWQ/run-awq-modifier-lumi.sh +++ b/AWQ/run-awq-modifier-lumi.sh @@ -11,15 +11,16 @@ # Load the module module purge -module use /appl/local/csc/modulefiles -module load pytorch/2.7 +module use /appl/local/laifs/modules +module load lumi-aif-singularity-bindings -# Activate the virtual environment from your current directory or change to the appropriate path -source venv/bin/activate +# export path to used container image +export SIF=/appl/local/laifs/containers/lumi-multitorch-u24r70f21m50t210-20260731_122833/lumi-multitorch-full-u24r70f21m50> +# Activate the virtual environment from your current directory or change to th # This will store all the Hugging Face cache such as downloaded models # and datasets in the project's scratch folder export HF_HOME=/scratch/${SLURM_JOB_ACCOUNT}/${USER}/hf-cache mkdir -p $HF_HOME -srun python3 awq-modifier.py +srun singularity exec "$SIF" python3 awq-modifier.py diff --git a/AWQ/run-awq-modifier-mahti.sh b/AWQ/run-awq-modifier-mahti.sh deleted file mode 100644 index d131122..0000000 --- a/AWQ/run-awq-modifier-mahti.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -#SBATCH --account=xxxxxxxx -#SBATCH --partition=gputest -#SBATCH --ntasks=1 -#SBATCH --cpus-per-task=6 -#SBATCH --nodes=1 -#SBATCH --mem=32G -#SBATCH --time=0:15:00 -#SBATCH --gres=gpu:a100:1 -#SBATCH --output=slurm-%j.out -#SBATCH --error=slurm-%j.err - -# Load the module -module purge -module use /appl/local/csc/modulefiles -module load pytorch/2.7 - -# Activate the virtual environment from your current directory or change to the appropriate path -source venv/bin/activate - -# This will store all the Hugging Face cache such as downloaded models -# and datasets in the project's scratch folder -export HF_HOME=/scratch/${SLURM_JOB_ACCOUNT}/${USER}/hf-cache -mkdir -p $HF_HOME - -srun python3 awq-modifier.py diff --git a/AWQ/run-awq-modifier-puhti.sh b/AWQ/run-awq-modifier-puhti.sh deleted file mode 100644 index 1e40cff..0000000 --- a/AWQ/run-awq-modifier-puhti.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -#SBATCH --account=xxxxxxxx -#SBATCH --partition=gputest -#SBATCH --ntasks=1 -#SBATCH --cpus-per-task=6 -#SBATCH --nodes=1 -#SBATCH --mem=32G -#SBATCH --time=0:15:00 -#SBATCH --gres=gpu:v100:1 -#SBATCH --output=slurm-%j.out -#SBATCH --error=slurm-%j.err - -# Load the module -module purge -module use /appl/local/csc/modulefiles -module load pytorch/2.7 - -# Activate the virtual environment from your current directory or change to the appropriate path -source venv/bin/activate - -# This will store all the Hugging Face cache such as downloaded models -# and datasets in the project's scratch folder -export HF_HOME=/scratch/${SLURM_JOB_ACCOUNT}/${USER}/hf-cache -mkdir -p $HF_HOME - -srun python3 awq-modifier.py diff --git a/AWQ/run-awq-modifier-roihu.sh b/AWQ/run-awq-modifier-roihu.sh new file mode 100644 index 0000000..eeeb5e1 --- /dev/null +++ b/AWQ/run-awq-modifier-roihu.sh @@ -0,0 +1,22 @@ +#!/bin/bash +#SBATCH --account=project_xxxxxxx +#SBATCH --partition=gputest +#SBATCH --time=00:15:00 +#SBATCH --nodes=1 +#SBATCH --tasks-per-node=1 +#SBATCH --cpus-per-task=72 +#SBATCH --gres=gpu:gh200:1 +#SBATCH --mem=32G + +module purge +module load python-pytorch/2.10 + +# Activate the virtual environment from your current directory or change to the appropriate path +source venv/bin/activate + +# Set hf cache to the project's scratch +export HF_HOME=/scratch/$SLURM_JOB_ACCOUNT/$USER/hf-cache/ +mkdir -p $HF_HOME + +srun python3 awq-modifier.py + diff --git a/BitsAndBytes/README.md b/BitsAndBytes/README.md index 86d0443..629b126 100644 --- a/BitsAndBytes/README.md +++ b/BitsAndBytes/README.md @@ -4,15 +4,14 @@ This example demonstrates quantizing the **OPT-125M** model using the [bitsandby ## Running the script -All of the libraries needed to run this example (transformers, bitsandbytes, accelerate) are covered by the CSC preinstalled PyTorch module. +All of the libraries needed to run this example (transformers, bitsandbytes, accelerate) are covered by the AI Factory provided Container on LUMI or the CSC preinstalled PyTorch module on Roihu. The script `bnb-quantization.py` will quantize the OPT-125M model to nf4 or NormalFloat 4-bit, introduced to use with QLoRA technique, a parameter efficient fine-tuning technique. It can be used with QLoRA for fine-tuning, or without just for reducing model size. The launch scripts are: -- `run-bnb-quantization-lumi.sh` - quantizes model on LUMI with 1 GPU -- `run-bnb-quantization-mahti.sh` - quantizes model on Mahti with 1 GPU -- `run-bnb-quantization-puhti.sh` - quantizes model on Puhti with 1 GPU +- `run-bnb-quantization-lumi.sh` - quantizes model on LUMI with 1 GPU +- `run-bnb-quantization-roihu.sh` - quantizes model on Roihu with 1 GPU **Note:** the scripts are made to be run on `gputest` or `dev-g` partition with a 30 minutes time-limit. You have to select the proper partition for longer jobs for your real runs. Additionally, change the `--account` parameter to your own project code. diff --git a/BitsAndBytes/bnb-quantization.py b/BitsAndBytes/bnb-quantization.py index d182d84..24ba45c 100644 --- a/BitsAndBytes/bnb-quantization.py +++ b/BitsAndBytes/bnb-quantization.py @@ -27,6 +27,7 @@ def benchmark(model, tokenizer, prompt): **inputs, max_new_tokens=50, do_sample=True, + use_cache=False, temperature=0.7, ) diff --git a/BitsAndBytes/run-bnb-quantization-lumi.sh b/BitsAndBytes/run-bnb-quantization-lumi.sh index 8492c78..5ea4e29 100644 --- a/BitsAndBytes/run-bnb-quantization-lumi.sh +++ b/BitsAndBytes/run-bnb-quantization-lumi.sh @@ -11,12 +11,16 @@ # Load the module module purge -module use /appl/local/csc/modulefiles -module load pytorch/2.7 +module use /appl/local/laifs/modules +module load lumi-aif-singularity-bindings + +# export path to used container image +export SIF=/appl/local/laifs/containers/lumi-multitorch-u24r70f21m50t210-20260731_122833/lumi-multitorch-full-u24r70f21m50t210-20260731_122833.sif # This will store all the Hugging Face cache such as downloaded models # and datasets in the project's scratch folder -export HF_HOME=/scratch/${SLURM_JOB_ACCOUNT}/${USER}/hf-cache +export HF_HOME=/scratch/$SLURM_JOB_ACCOUNT/$USER/llm-quantization-scripts/BitsAndBytes/hf-cache mkdir -p $HF_HOME +export SINGULARITYENV_HF_HOME=$HF_HOME -srun python3 bnb-quantization.py +srun singularity exec "$SIF" bash -c 'python3 bnb-quantization.py' diff --git a/BitsAndBytes/run-bnb-quantization-mahti.sh b/BitsAndBytes/run-bnb-quantization-mahti.sh deleted file mode 100644 index 6c683e7..0000000 --- a/BitsAndBytes/run-bnb-quantization-mahti.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash -#SBATCH --account=xxxxxxxx -#SBATCH --partition=gputest -#SBATCH --ntasks=1 -#SBATCH --cpus-per-task=6 -#SBATCH --nodes=1 -#SBATCH --mem=32G -#SBATCH --time=0:15:00 -#SBATCH --gres=gpu:a100:1 -#SBATCH --output=slurm-%j.out -#SBATCH --error=slurm-%j.err - -# Load the module -module purge -module use /appl/local/csc/modulefiles -module load pytorch/2.7 - -# This will store all the Hugging Face cache such as downloaded models -# and datasets in the project's scratch folder -export HF_HOME=/scratch/${SLURM_JOB_ACCOUNT}/${USER}/hf-cache -mkdir -p $HF_HOME - -srun python3 bnb-quantization.py diff --git a/BitsAndBytes/run-bnb-quantization-puhti.sh b/BitsAndBytes/run-bnb-quantization-puhti.sh deleted file mode 100644 index 30d4626..0000000 --- a/BitsAndBytes/run-bnb-quantization-puhti.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash -#SBATCH --account=xxxxxxxx -#SBATCH --partition=gputest -#SBATCH --ntasks=1 -#SBATCH --cpus-per-task=6 -#SBATCH --nodes=1 -#SBATCH --mem=32G -#SBATCH --time=0:15:00 -#SBATCH --gres=gpu:v100:1 -#SBATCH --output=slurm-%j.out -#SBATCH --error=slurm-%j.err - -# Load the module -module purge -module use /appl/local/csc/modulefiles -module load pytorch/2.7 - -# This will store all the Hugging Face cache such as downloaded models -# and datasets in the project's scratch folder -export HF_HOME=/scratch/${SLURM_JOB_ACCOUNT}/${USER}/hf-cache -mkdir -p $HF_HOME - -srun python3 bnb-quantization.py diff --git a/BitsAndBytes/run-bnb-quantization-roihu.sh b/BitsAndBytes/run-bnb-quantization-roihu.sh new file mode 100644 index 0000000..e96799c --- /dev/null +++ b/BitsAndBytes/run-bnb-quantization-roihu.sh @@ -0,0 +1,21 @@ +#!/bin/bash +#SBATCH --account=project_xxxxxxx +#SBATCH --partition=gputest +#SBATCH --time=00:15:00 +#SBATCH --nodes=1 +#SBATCH --tasks-per-node=1 +#SBATCH --cpus-per-task=72 +#SBATCH --gres=gpu:gh200:1 +#SBATCH --mem=32G + +module purge +module load python-pytorch/2.10 + +# Activate the virtual environment from your current directory or change to the appropriate path +source venv/bin/activate + +# Set hf cache to the project's scratch +export HF_HOME=/scratch/$SLURM_JOB_ACCOUNT/$USER/hf-cache/ +mkdir -p $HF_HOME + +srun python3 bnb-quantization.py diff --git a/GPTQ/README.md b/GPTQ/README.md index 4d2cad6..c570722 100644 --- a/GPTQ/README.md +++ b/GPTQ/README.md @@ -3,12 +3,35 @@ This repository contains two practical examples of applying GPTQ quantization to LLMs. Both examples currently use the small **OPT-125M** model for demonstration, but the code is written so you can swap in larger models. -1. **GPTQConfig** — Uses Hugging Face `transformers` and [`GPTQConfig`](https://huggingface.co/docs/transformers/en/quantization/gptq) to quantize the **OPT-125M** model to 4-bit precision. +1. **GPTQModel** — Uses [GPTQModel](https://github.com/modelcloud/gptqmodel) with `QuantizeConfig` to quantize the **OPT-125M** model to 4-bit precision. 2. **GPTQModifier** — Uses [LLM Compressor](https://github.com/vllm-project/llm-compressor) with a GPTQ recipe to quantize the **OPT-125M** model to mixed precision W4A16. --- -## Installations +## LUMI + +To run gptq scripts on LUMI, you have to install `gptqmodel` on top of the LUMI AI Factory container in a virtual environment. `Llmcompressor` and other libraries needed are already in the container. + +Load the `Singularity` container environment and set the container image path. Later, create virtual environment inside the container and install the packages. + +```bash +module purge +module use /appl/local/laifs/modules +module load lumi-aif-singularity-bindings + +export SIF=/appl/local/laifs/containers/lumi-multitorch-u24r70f21m50t210-20260731_122833/lumi-multitorch-full-u24r70f21m50t210-20260731_122833.sif + +singularity shell "$SIF" + +Apptainer> python -m venv venv --system-site-packages +Apptainer> source venv/bin/activate +(venv) Apptainer> pip install gptqmodel==7.1.0 --no-build-isolation --cache-dir ./.pip-cache + +``` +The flag --cache-dir points the pip cache to the current (scratch) folder instead of the default (home directory), to avoid filling up home directory quota. + +--- +## Roihu The CSC preinstalled PyTorch module covers most of the libraries needed to run these examples (torch, transformers, datasets, accelerate). The rest can be installed on top of the module in a virtual environment. @@ -16,8 +39,7 @@ The CSC preinstalled PyTorch module covers most of the libraries needed to run t ### Load the module ```bash module purge -module use /appl/local/csc/modulefiles -module load pytorch/2.7 +module load python-pytorch/2.10 ``` ### Create and activate a virtual environment using system packages ```bash @@ -26,44 +48,29 @@ source venv/bin/activate ``` ### Install packages ```bash -pip install optimum==1.27.0 --cache-dir ./.pip-cache +(venv)> pip install gptqmodel==7.1.0 --no-build-isolation --cache-dir ./.pip-cache ``` -The flag --cache-dir points the pip cache to the current (scratch) folder instead of the default (home directory), to avoid filling up home directory quota. - -The GPTQmodel library is needed for the **gptq-config** example. To install it on Puhti or Mahti, you need to use a GPU interactively when installing, or set the following environment variable: +This version of gptqmodel is compatible with python-pytorch/2.10. -- For Puhti:`export TORCH_CUDA_ARCH_LIST="7.0"` -- For Mahti: `export TORCH_CUDA_ARCH_LIST="8.0"` +For the **gptq-modifier** example, you need to install the llmcompressor library. -Then install with: ```bash -pip install gptqmodel==4.0.0 --no-build-isolation --cache-dir ./.pip-cache -``` - -This version of gptqmodel is compatible with PyTorch 2.7. - -Troubleshooting: -- If you get an AssetionError from pip's resolver when installing gptqmodel, upgrade pip, setuptools and wheel: `python -m pip install --upgrade pip setuptools wheel` -- When quantizing models that use Rotary Positional Embeddings (RoPE), such as LlaMA, you might encounter runtime errors related to rotary dimensions. The current fix is to downgrade transformers to version 4.51.3. +(venv)> pip install llmcompressor==0.12.0 --cache-dir ./.pip-cache +``` -For the **gptq-modifier** example, you need to install the llmcompressor library. +--- -```bash -pip install llmcompressor==0.7.1 --cache-dir ./.pip-cache -``` ## Usage The launch scripts for gptq-config are: -- `run-gptq-config-lumi.sh` - quantizes model on LUMI with 1 GPU -- `run-gptq-config-mahti.sh` - quantizes model on Mahti with 1 GPU -- `run-gptq-config-puhti.sh` - quantizes model on Puhti with 1 GPU +- `run-gptq-config-lumi.sh` - quantizes model on LUMI with 1 GPU +- `run-gptq-config-roihu.sh` - quantizes model on Roihu with 1 GPU Similarly, for gptq-modifier: -- `run-gptq-modifier-lumi.sh` - quantizes model on LUMI with 1 GPU -- `run-gptq-modifier-mahti.sh` - quantizes model on Mahti with 1 GPU -- `run-gptq-modifier-puhti.sh` - quantizes model on Puhti with 1 GPU +- `run-gptq-modifier-lumi.sh` - quantizes model on LUMI with 1 GPU +- `run-gptq-modifier-roihu.sh` - quantizes model on Roihu with 1 GPU **Note:** the scripts are made to be run on `gputest` or `dev-g` partition with a 30 minute time-limit. You have to select the proper partition for longer jobs for your real runs. Additionally, change the `--account` parameter to your own project code. @@ -75,8 +82,9 @@ sbatch run-gptq-config-lumi.sh You can also increase the memory if you decide to run quantization on larger models. Setting `device_map="auto"` automatically offloads the model to a CPU to help fit the model in memory, and allow the model modules to be moved between the CPU and GPU for quantization. ## `gptq-config.py` -- Uses Hugging Face 🤗 `transformers` with [`GPTQConfig`](https://huggingface.co/docs/transformers/en/quantization/gptq). -- This example quantizes the model to 4-bit precision, supported precisions are 2-bit, 3-bit*, 4-bit and 8-bit. +- Uses [`gptqmodel`](https://github.com/modelcloud/gptqmodel)(`GPTQModel.load`, `QuantizeConfig`) with `QuantizeConfig` recipe. +- Runs explicit **calibration** on a subset of the [aleenai/c4](https://huggingface.co/datasets/allenai/c4) dataset. +- This example quantizes the model to 4-bit precision, supported precisions are 2-bit, 3-bit, 4-bit and 8-bit. - Saves both the full-precision and quantized models. - Compares outputs, inference latency, and model size. @@ -88,8 +96,6 @@ You can also increase the memory if you decide to run quantization on larger mod - Compares outputs, inference latency, and model size. - Provides finer control over quantization schemes (e.g. `W4A16`, `ignore=["lm_head"]`). -*3-bit quantization is not currently supported on LUMI with PyTorch 2.7, if you wish to use 3-bit quantization you can use the PyTorch 2.5 module. - ## Output Includes - Generated text before and after quantization. - Inference time comparison. diff --git a/GPTQ/gptq-config.py b/GPTQ/gptq-config.py index 628ccf6..3571197 100644 --- a/GPTQ/gptq-config.py +++ b/GPTQ/gptq-config.py @@ -1,15 +1,14 @@ import os import time import torch -from transformers import ( - AutoTokenizer, - AutoModelForCausalLM, - GPTQConfig, -) +from transformers import AutoTokenizer, AutoModelForCausalLM +from gptqmodel import GPTQModel, QuantizeConfig +from datasets import load_dataset model_name = "facebook/opt-125m" prompt = "The future of AI is" + # Measure model inference time and generate sample output for a given prompt def benchmark(model, tokenizer, prompt, max_new_tokens=50): inputs = tokenizer(prompt, return_tensors="pt").to(model.device) @@ -17,82 +16,93 @@ def benchmark(model, tokenizer, prompt, max_new_tokens=50): # Warm-up run (to remove cold start effects) with torch.no_grad(): _ = model.generate(**inputs, max_new_tokens=5) - if torch.cuda.is_available(): torch.cuda.synchronize() - start = time.time() + start = time.time() with torch.no_grad(): output_ids = model.generate( **inputs, - max_new_tokens=50, + max_new_tokens=max_new_tokens, do_sample=True, temperature=0.7, -) - + ) if torch.cuda.is_available(): torch.cuda.synchronize() end = time.time() elapsed_time = end - start decoded_text = tokenizer.decode(output_ids[0], skip_special_tokens=True) - return decoded_text, elapsed_time -# Load base model and tokenizer + +def get_folder_size(path): + total = 0 + for dirpath, _, filenames in os.walk(path): + for f in filenames: + total += os.path.getsize(os.path.join(dirpath, f)) + return total / (1024 * 1024) # MB + + +# --------------------------------------------------------------------------- +# 1. Load base model and tokenizer, run benchmark, save full model +# --------------------------------------------------------------------------- model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto") tokenizer = AutoTokenizer.from_pretrained(model_name) -# Run benchmark on full model initial_output, initial_time = benchmark(model, tokenizer, prompt) -# Save full model before quantization save_dir_full = model_name.split("/")[-1] + "-full" model.save_pretrained(save_dir_full, safe_serialization=True) tokenizer.save_pretrained(save_dir_full) -# Set GPTQ-config -# Optionally set 'model_seqlen' for models -# where GPTQ cannot automatically infer the max sequence length -gptq_config = GPTQConfig( +# Free memory before quantization +del model +if torch.cuda.is_available(): + torch.cuda.empty_cache() + +# --------------------------------------------------------------------------- +# 2. Prepare calibration dataset for GPTQ +# --------------------------------------------------------------------------- +calibration_dataset = load_dataset( + "allenai/c4", + data_files="en/c4-train.00001-of-01024.json.gz", + split="train", +).select(range(256))["text"] + +# --------------------------------------------------------------------------- +# 3. Quantize the model with gptqmodel (no optimum/transformers.GPTQConfig) +# --------------------------------------------------------------------------- +quantize_config = QuantizeConfig( bits=4, - dataset="c4", # Use a standard text dataset for calibration - tokenizer=tokenizer - # model_seqlen (int, optional) — The maximum sequence length that the model can take. + group_size=128, ) -# Quantize the model with GPTQ -quantized_model = AutoModelForCausalLM.from_pretrained( - model_name, - quantization_config=gptq_config, - device_map="auto") +quant_model = GPTQModel.load(model_name, quantize_config) +quant_model.quantize(calibration_dataset, batch_size=2) -# Save quantized model save_dir_quant = model_name.split("/")[-1] + "-gptq-config" -# Move model to a CPU for saving -quantized_model.to("cpu") -quantized_model.save_pretrained(save_dir_quant, safe_serialization=True) +quant_model.save(save_dir_quant) tokenizer.save_pretrained(save_dir_quant) -# Reload quantized model and tokenizer -quant_model = AutoModelForCausalLM.from_pretrained(save_dir_quant, device_map="auto") -quant_tokenizer = AutoTokenizer.from_pretrained(save_dir_quant) +del quant_model +if torch.cuda.is_available(): + torch.cuda.empty_cache() -# Run benchmark on quantized model -quant_output, quant_time = benchmark(quant_model, quant_tokenizer, prompt) +# --------------------------------------------------------------------------- +# 4. Reload quantized model and benchmark +# --------------------------------------------------------------------------- +reloaded_quant_model = GPTQModel.load(save_dir_quant) +quant_tokenizer = AutoTokenizer.from_pretrained(save_dir_quant) -# Compare model sizes -def get_folder_size(path): - total = 0 - for dirpath, _, filenames in os.walk(path): - for f in filenames: - total += os.path.getsize(os.path.join(dirpath, f)) - return total / (1024 * 1024) # MB +quant_output, quant_time = benchmark(reloaded_quant_model, quant_tokenizer, prompt) +# --------------------------------------------------------------------------- +# 5. Compare model sizes and print results +# --------------------------------------------------------------------------- initial_size = get_folder_size(save_dir_full) quant_size = get_folder_size(save_dir_quant) -# Print results print("=== Full Model ===") print(f" Output: {initial_output}") print(f" Size: {initial_size:.2f} MB") diff --git a/GPTQ/run-gptq-config-lumi.sh b/GPTQ/run-gptq-config-lumi.sh index 812ab59..da970f8 100644 --- a/GPTQ/run-gptq-config-lumi.sh +++ b/GPTQ/run-gptq-config-lumi.sh @@ -9,17 +9,17 @@ #SBATCH --output=slurm-%j.out #SBATCH --error=slurm-%j.err -# Load the module +#Load the module module purge -module use /appl/local/csc/modulefiles -module load pytorch/2.7 +module use /appl/local/laifs/modules +module load lumi-aif-singularity-bindings -# Activate the virtual environment from your current directory or change to the appropriate path -source venv/bin/activate +# export path to used container image +export SIF=/appl/local/laifs/containers/lumi-multitorch-u24r70f21m50t210-20260731_122833/lumi-multitorch-full-u24r70f21m50t210-20260731_122833.sif # This will store all the Hugging Face cache such as downloaded models # and datasets in the project's scratch folder export HF_HOME=/scratch/${SLURM_JOB_ACCOUNT}/${USER}/hf-cache mkdir -p $HF_HOME -srun python3 gptq-config.py +srun singularity exec "$SIF" bash -c 'source venv/bin/activate && python3 gptq-config.py' diff --git a/GPTQ/run-gptq-config-mahti.sh b/GPTQ/run-gptq-config-mahti.sh deleted file mode 100644 index a9af25a..0000000 --- a/GPTQ/run-gptq-config-mahti.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -#SBATCH --account=xxxxxxxx -#SBATCH --partition=gputest -#SBATCH --ntasks=1 -#SBATCH --cpus-per-task=6 -#SBATCH --nodes=1 -#SBATCH --mem=32G -#SBATCH --time=0:15:00 -#SBATCH --gres=gpu:a100:1 -#SBATCH --output=slurm-%j.out -#SBATCH --error=slurm-%j.err - -# Load the module -module purge -module use /appl/local/csc/modulefiles -module load pytorch/2.7 - -# Activate the virtual environment from your current directory or change to the appropriate path -source venv/bin/activate - -# This will store all the Hugging Face cache such as downloaded models -# and datasets in the project's scratch folder -export HF_HOME=/scratch/${SLURM_JOB_ACCOUNT}/${USER}/hf-cache -mkdir -p $HF_HOME - -srun python3 gptq-config.py diff --git a/GPTQ/run-gptq-config-puhti.sh b/GPTQ/run-gptq-config-puhti.sh deleted file mode 100644 index 70f7961..0000000 --- a/GPTQ/run-gptq-config-puhti.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -#SBATCH --account=xxxxxxxx -#SBATCH --partition=gputest -#SBATCH --ntasks=1 -#SBATCH --cpus-per-task=6 -#SBATCH --nodes=1 -#SBATCH --mem=32G -#SBATCH --time=0:15:00 -#SBATCH --gres=gpu:v100:1 -#SBATCH --output=slurm-%j.out -#SBATCH --error=slurm-%j.err - -# Load the module -module purge -module use /appl/local/csc/modulefiles -module load pytorch/2.7 - -# Activate the virtual environment from your current directory or change to the appropriate path -source venv/bin/activate - -# This will store all the Hugging Face cache such as downloaded models -# and datasets in the project's scratch folder -export HF_HOME=/scratch/${SLURM_JOB_ACCOUNT}/${USER}/hf-cache -mkdir -p $HF_HOME - -srun python3 gptq-config.py diff --git a/GPTQ/run-gptq-config-roihu.sh b/GPTQ/run-gptq-config-roihu.sh new file mode 100644 index 0000000..865e5e2 --- /dev/null +++ b/GPTQ/run-gptq-config-roihu.sh @@ -0,0 +1,21 @@ +#!/bin/bash +#SBATCH --account=project_xxxxxxx +#SBATCH --partition=gputest +#SBATCH --time=00:15:00 +#SBATCH --nodes=1 +#SBATCH --tasks-per-node=1 +#SBATCH --cpus-per-task=72 +#SBATCH --gres=gpu:gh200:1 +#SBATCH --mem=32G + +module purge +module load python-pytorch/2.10 + +# Activate the virtual environment from your current directory or change to the appropriate path +source venv/bin/activate + +# Set hf cache to the project's scratch +export HF_HOME=/scratch/$SLURM_JOB_ACCOUNT/$USER/hf-cache/ +mkdir -p $HF_HOME + +srun python3 gptq-config.py diff --git a/GPTQ/run-gptq-modifier-lumi.sh b/GPTQ/run-gptq-modifier-lumi.sh index 2dee594..c033dc0 100644 --- a/GPTQ/run-gptq-modifier-lumi.sh +++ b/GPTQ/run-gptq-modifier-lumi.sh @@ -11,15 +11,15 @@ # Load the module module purge -module use /appl/local/csc/modulefiles -module load pytorch/2.7 +module use /appl/local/laifs/modules +module load lumi-aif-singularity-bindings -# Activate the virtual environment from your current directory or change to the appropriate path -source venv/bin/activate +# export path to used container image +export SIF=/appl/local/laifs/containers/lumi-multitorch-u24r70f21m50t210-20260731_122833/lumi-multitorch-full-u24r70f21m50t210-20260731_122833.sif # This will store all the Hugging Face cache such as downloaded models # and datasets in the project's scratch folder export HF_HOME=/scratch/${SLURM_JOB_ACCOUNT}/${USER}/hf-cache mkdir -p $HF_HOME -srun python3 gptq-modifier.py +srun singularity exec "$SIF" python3 gptq-modifier.py diff --git a/GPTQ/run-gptq-modifier-mahti.sh b/GPTQ/run-gptq-modifier-mahti.sh deleted file mode 100644 index 7d8b51f..0000000 --- a/GPTQ/run-gptq-modifier-mahti.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -#SBATCH --account=xxxxxxxx -#SBATCH --partition=gputest -#SBATCH --ntasks=1 -#SBATCH --cpus-per-task=6 -#SBATCH --nodes=1 -#SBATCH --mem=32G -#SBATCH --time=0:15:00 -#SBATCH --gres=gpu:a100:1 -#SBATCH --output=slurm-%j.out -#SBATCH --error=slurm-%j.err - -# Load the module -module purge -module use /appl/local/csc/modulefiles -module load pytorch/2.7 - -# Activate the virtual environment from your current directory or change to the appropriate path -source venv/bin/activate - -# This will store all the Hugging Face cache such as downloaded models -# and datasets in the project's scratch folder -export HF_HOME=/scratch/${SLURM_JOB_ACCOUNT}/${USER}/hf-cache -mkdir -p $HF_HOME - -srun python3 gptq-modifier.py diff --git a/GPTQ/run-gptq-modifier-puhti.sh b/GPTQ/run-gptq-modifier-puhti.sh deleted file mode 100644 index 41abf50..0000000 --- a/GPTQ/run-gptq-modifier-puhti.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -#SBATCH --account=xxxxxxxx -#SBATCH --partition=gputest -#SBATCH --ntasks=1 -#SBATCH --cpus-per-task=6 -#SBATCH --nodes=1 -#SBATCH --mem=32G -#SBATCH --time=0:15:00 -#SBATCH --gres=gpu:v100:1 -#SBATCH --output=slurm-%j.out -#SBATCH --error=slurm-%j.err - -# Load the module -module purge -module use /appl/local/csc/modulefiles -module load pytorch/2.7 - -# Activate the virtual environment from your current directory or change to the appropriate path -source venv/bin/activate - -# This will store all the Hugging Face cache such as downloaded models -# and datasets in the project's scratch folder -export HF_HOME=/scratch/${SLURM_JOB_ACCOUNT}/${USER}/hf-cache -mkdir -p $HF_HOME - -srun python3 gptq-modifier.py diff --git a/GPTQ/run-gptq-modifier-roihu.sh b/GPTQ/run-gptq-modifier-roihu.sh new file mode 100644 index 0000000..0869a8b --- /dev/null +++ b/GPTQ/run-gptq-modifier-roihu.sh @@ -0,0 +1,21 @@ +#!/bin/bash +#SBATCH --account=project_xxxxxxx +#SBATCH --partition=gputest +#SBATCH --time=00:15:00 +#SBATCH --nodes=1 +#SBATCH --tasks-per-node=1 +#SBATCH --cpus-per-task=72 +#SBATCH --gres=gpu:gh200:1 +#SBATCH --mem=32G + +module purge +module load python-pytorch/2.10 + +# Activate the virtual environment from your current directory or change to the appropriate path +source venv/bin/activate + +# Set hf cache to the project's scratch +export HF_HOME=/scratch/$SLURM_JOB_ACCOUNT/$USER/hf-cache/ +mkdir -p $HF_HOME + +srun python3 gptq-modifier.py diff --git a/README.md b/README.md index 9c80b50..520560f 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,6 @@ We provide examples and experiments for: 3. **Guides & Utilities** - Helper functions for measuring model size, timing inference, and testing the quantized model. - - Notes on how to run the examples on Puhti, Mahti and LUMI. + - Notes on how to run the examples on LUMI and Roihu. ---