Skip to content

Commit 0dc4bfd

Browse files
authored
Merge pull request #4 from zenml-io/feature/OSSK-521-peft-template
PEFT template
2 parents 2c67d94 + 1156f4b commit 0dc4bfd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+955
-11357
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,5 @@ cython_debug/
160160
#.idea/
161161

162162
.ruff_cache/
163+
.local/
164+
.vscode/

copier.yaml

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,39 +48,49 @@ email:
4848
product_name:
4949
type: str
5050
help: The technical name of the data product you are building
51-
default: llm_lora
51+
default: llm-peft
5252
validator: >-
53-
{% if not (product_name | regex_search('^[a-z][a-z0-9\_]*$')) %}
54-
The product name can only contain alphanumeric characters and underscores and must start with a character.
53+
{% if not (product_name | regex_search('^[a-z][-a-z0-9]*$')) %}
54+
The product name can only contain alphanumeric characters and dashes and must start with a character.
5555
{% endif %}
5656
model_repository:
5757
type: str
5858
help: |
59-
Huggingface repository of the model to finetune. Check out the Lit-GPT docs for the currently available models
60-
here https://github.com/Lightning-AI/litgpt?tab=readme-ov-file#-lit-gpt-1
61-
default: mistralai/Mistral-7B-Instruct-v0.1
62-
from_safetensors:
63-
type: bool
64-
help: |
65-
Whether the Huggingface model repository stores the model weights as safetensors. Check out the Lit-GPT docs
66-
to find the answer for your current selected model {{ model_repository }}:
67-
https://github.com/Lightning-AI/litgpt?tab=readme-ov-file#-lit-gpt-1
68-
default: false
59+
Huggingface repository of the model to finetune.
60+
default: microsoft/phi-2
61+
steps_of_finetuning:
62+
type: int
63+
help: The number of steps of finetuning job.
64+
default: 300
6965
cuda_version:
7066
type: str
7167
help: The available cuda version. (Only relevant when using a remote orchestrator)
7268
choices:
7369
CUDA 11.8: cuda11.8
7470
CUDA 12.1: cuda12.1
7571
default: cuda11.8
76-
huggingface_merged_model_repository:
72+
system_prompt:
7773
type: str
78-
help: The huggingface repository to which the finetuned model should be pushed [Optional]
79-
default: ""
80-
huggingface_adapter_model_repository:
74+
help: |
75+
The system prompt to be used for the finetuning job.
76+
default: |
77+
Given a target sentence construct the underlying meaning representation of the input sentence as a single function with attributes and attribute values.
78+
This function should describe the target string accurately and the function must be one of the following ['inform', 'request', 'give_opinion', 'confirm', 'verify_attribute', 'suggest', 'request_explanation', 'recommend', 'request_attribute'].
79+
The attributes must be one of the following: ['name', 'exp_release_date', 'release_year', 'developer', 'esrb', 'rating', 'genres', 'player_perspective', 'has_multiplayer', 'platforms', 'available_on_steam', 'has_linux_release', 'has_mac_release', 'specifier']
80+
dataset_name:
8181
type: str
82-
help: The huggingface repository to which the finetuned adapter should be pushed [Optional]
83-
default: ""
82+
help: |
83+
The dataset to be used for the finetuning job.
84+
default: gem/viggo
85+
step_operator:
86+
type: str
87+
help: The operator to be used for the finetuning and evaluation jobs.
88+
default: gcp_a100
89+
bf16:
90+
type: bool
91+
help: |
92+
Whether to use bf16 for the finetuning job.
93+
default: true
8494
zenml_server_url:
8595
type: str
8696
help: The URL of the ZenML server [Optional]
@@ -128,10 +138,7 @@ _tasks:
128138
{%- if zenml_server_url %}
129139
echo " zenml connect --url {{ zenml_server_url }}"
130140
{%- endif %}
131-
{%- if (huggingface_merged_model_repository or huggingface_adapter_model_repository) %}
132-
echo " zenml secret create huggingface_credentials --token=<HF_TOKEN>"
133-
{%- endif %}
134-
echo " python run.py --finetuning-pipeline --config finetune-alpaca.yaml"
141+
echo " python run.py --config orchestrator_finetune.yaml"
135142
echo "Next, you should take a look at the '{{ _copier_conf.dst_path }}/README.md' file in the generated project."
136143
echo "Happy coding!"
137144

template/.assets/model.png

-189 KB
Loading

template/.assets/pipeline.png

45.3 KB
Loading

template/.dockerignore

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
*
2+
!/materializers/**
23
!/pipelines/**
34
!/steps/**
4-
!/materializers/**
5-
!/evaluate/**
6-
!/finetune/**
7-
!/generate/**
8-
!/lit_gpt/**
9-
!/scripts/**
5+
!/utils/**

template/README.md

Lines changed: 32 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# ☮️ Fine-tuning open source LLMs using MLOps pipelines
1+
# ☮️ Fine-tuning open source LLMs using MLOps pipelines with PEFT
22

3-
Welcome to your newly generated "{{project_name}}" project! This is
3+
Welcome to your newly generated "ZenML LLM PEFT Finetuning project" project! This is
44
a great way to get hands-on with ZenML using production-like template.
55
The project contains a collection of ZenML steps, pipelines and other artifacts
66
and useful resources that can serve as a solid starting point for finetuning open-source LLMs using ZenML.
@@ -15,13 +15,13 @@ Using these pipelines, we can run the data-preparation and model finetuning with
1515
<br/>
1616
</div>
1717

18-
## :earth_americas: Inspiration and Credit
18+
## 🌎 Inspiration and Credit
1919

20-
This project heavily relies on the [Lit-GPT project](https://github.com/Lightning-AI/litgpt) of the amazing people at Lightning AI. We used [this blogpost](https://lightning.ai/pages/community/lora-insights/#toc14) to get started with LoRA and QLoRA and modified the commands they recommend to make them work using ZenML.
20+
This project heavily relies on the [PEFT project](https://huggingface.co/docs/peft/en/index) by the amazing people at Hugging Face and the [`{{ model_repository }}`](https://huggingface.co/{{ model_repository }}) model from the amazing people at {{ model_repository.split("/") | first }}.
2121

2222
## 🏃 How to run
2323

24-
In this project we provide a few predefined configuration files for finetuning models on the [Alpaca](https://huggingface.co/datasets/tatsu-lab/alpaca) dataset. Before we're able to run any pipeline, we need to set up our environment as follows:
24+
In this project, we provide a predefined configuration file to finetune models on the [{{ dataset_name }}](https://huggingface.co/datasets/{{ dataset_name }}) dataset. Before we're able to run any pipeline, we need to set up our environment as follows:
2525

2626
```bash
2727
# Set up a Python virtual environment, if you haven't already
@@ -30,60 +30,32 @@ source .venv/bin/activate
3030

3131
# Install requirements
3232
pip install -r requirements.txt
33-
34-
{%- if zenml_server_url %}
35-
# Connect to your remote ZenML server
36-
zenml connect --url {{ zenml_server_url }}
37-
{%- endif %}
38-
{%- if (huggingface_merged_model_repository or huggingface_adapter_model_repository) %}
39-
# Create a secret with your Huggingface token to push the finetuned model/adapter
40-
zenml secret create huggingface_credentials --token=<HF_TOKEN>
41-
{%- endif %}
42-
```
43-
44-
### Combined feature engineering and finetuning pipeline
45-
46-
The easiest way to get started with just a single command is to run the finetuning pipeline with the `finetune-alpaca.yaml` configuration file, which will do both feature engineering and finetuning:
47-
48-
```shell
49-
python run.py --finetuning-pipeline --config finetune-alpaca.yaml
50-
```
51-
52-
When running the pipeline like this, the trained adapter will be stored in the ZenML artifact store. You can optionally upload the adapter, the merged model or both by specifying the `adapter_output_repo` and `merged_output_repo` parameters in the configuration file.
53-
54-
55-
### Evaluation pipeline
56-
57-
Before running this pipeline, you will need to fill in the `adapter_repo` in the `eval.yaml` configuration file. This should point to a huggingface repository that contains the finetuned adapter you got by running the finetuning pipeline.
58-
59-
```shell
60-
python run.py --eval-pipeline --config eval.yaml
6133
```
6234

63-
### Merging pipeline
35+
### 👷 Combined feature engineering and finetuning pipeline
6436

65-
In case you have trained an adapter using the finetuning pipeline, you can merge it with the base model by filling in the `adapter_repo` and `output_repo` parameters in the `merge.yaml` file, and then running:
37+
The easiest way to get started with just a single command is to run the finetuning pipeline with the `orchestrator_finetune.yaml` configuration file, which will do data preparation, model finetuning, evaluation with [Rouge](https://huggingface.co/spaces/evaluate-metric/rouge) and promotion:
6638

6739
```shell
68-
python run.py --merge-pipeline --config merge.yaml
40+
python run.py --config orchestrator_finetune.yaml
6941
```
7042

71-
### Feature Engineering followed by Finetuning
72-
73-
If you want to finetune your model on a different dataset, you can do so by running the feature engineering pipeline followed by the finetuning pipeline. To define your dataset, take a look at the `scripts/prepare_*` scripts and set the dataset name in the `feature-alpaca.yaml` config file.
43+
When running the pipeline like this, the trained model will be stored in the ZenML artifact store.
7444

75-
```shell
76-
python run.py --feature-pipeline --config feature-alpaca.yaml
77-
python run.py --finetuning-pipeline --config finetune-from-dataset.yaml
78-
```
45+
<div align="center">
46+
<br/>
47+
<a href="https://cloud.zenml.io">
48+
<img alt="Model version metadata" src=".assets/pipeline.png">
49+
</a>
50+
<br/>
51+
</div>
7952

80-
## ☁️ Running with a remote stack
53+
## ☁️ Running with a step operator in the stack
8154

8255
To finetune an LLM on remote infrastructure, you can either use a remote orchestrator or a remote step operator. Follow these steps to set up a complete remote stack:
8356
- Register the [orchestrator](https://docs.zenml.io/stacks-and-components/component-guide/orchestrators) (or [step operator](https://docs.zenml.io/stacks-and-components/component-guide/step-operators)) and make sure to configure it in a way so that the finetuning step has access to a GPU with at least 24GB of VRAM. Check out our docs for more [details](https://docs.zenml.io/stacks-and-components/component-guide).
8457
- To access GPUs with this amount of VRAM, you might need to increase your GPU quota ([AWS](https://docs.aws.amazon.com/servicequotas/latest/userguide/request-quota-increase.html), [GCP](https://console.cloud.google.com/iam-admin/quotas), [Azure](https://learn.microsoft.com/en-us/azure/machine-learning/how-to-manage-quotas?view=azureml-api-2#request-quota-and-limit-increases)).
8558
- The GPU instance that your finetuning will be running on will have CUDA drivers of a specific version installed. If that CUDA version is not compatible with the one provided by the default Docker image of the finetuning pipeline, you will need to modify it in the configuration file. See [here](https://hub.docker.com/r/pytorch/pytorch/tags) for a list of available PyTorch images.
86-
- If you're running out of memory, you can experiment with quantized LoRA (QLoRA) by setting a different value for the `quantize` parameter in the configuration, or reduce the `global_batch_size`/`micro_batch_size`.
8759
- Register a remote [artifact store](https://docs.zenml.io/stacks-and-components/component-guide/artifact-stores) and [container registry](https://docs.zenml.io/stacks-and-components/component-guide/container-registries).
8860
- Register a stack with all these components
8961
```shell
@@ -93,43 +65,30 @@ To finetune an LLM on remote infrastructure, you can either use a remote orchest
9365
[-s <STEP_OPERATOR_NAME>]
9466
```
9567

96-
## 💾 Running with custom data
97-
98-
To finetune a model with your custom data, you will need to convert it to a CSV file with the columns described
99-
[here](https://github.com/Lightning-AI/litgpt/blob/main/tutorials/prepare_dataset.md#preparing-custom-datasets-from-a-csv-file).
100-
101-
Next, update the `configs/feature-custom.yaml` file and set the value of the `csv_path` parameter to that CSV file.
102-
With all that in place, you can now run the feature engineering pipeline to convert your CSV into the correct format for training and then run the finetuning pipeline as follows:
103-
```shell
104-
python run.py --feature-pipeline --config feature-custom.yaml
105-
python run.py --finetuning-pipeline --config finetune-from-dataset.yaml
106-
```
107-
10868
## 📜 Project Structure
10969

11070
The project loosely follows [the recommended ZenML project structure](https://docs.zenml.io/user-guide/starter-guide/follow-best-practices):
11171

11272
```
11373
.
11474
├── configs # pipeline configuration files
115-
│ ├── eval.yaml # configuration for the evaluation pipeline
116-
│ ├── feature-alpaca.yaml # configuration for the feature engineering pipeline
117-
│ ├── feature-custom.yaml # configuration for the feature engineering pipeline
118-
│ ├── finetune-alpaca.yaml # configuration for the finetuning pipeline
119-
│ ├── finetune-from-dataset.yaml # configuration for the finetuning pipeline
120-
│ └── merge.yaml # configuration for the merging pipeline
75+
│ ├── orchestrator_finetune.yaml # default local or remote orchestrator
76+
│ └── remote_finetune.yaml # default step operator configuration
77+
├── materializers
78+
│ └── directory_materializer.py # custom materializer to push whole directories to the artifact store and back
12179
├── pipelines # `zenml.pipeline` implementations
122-
│ ├── evaluate.py # Evaluation pipeline
123-
│ ├── feature_engineering.py # Feature engineering pipeline
124-
│ ├── finetuning.py # Finetuning pipeline
125-
│ └── merge.py # Merging pipeline
80+
│ └── train.py # Finetuning and evaluation pipeline
12681
├── steps # logically grouped `zenml.steps` implementations
127-
│ ├── evaluate.py # evaluate model performance
128-
│ ├── feature_engineering.py # preprocess data
129-
│ ├── finetune.py # finetune a model
130-
│ ├── merge.py # merge model and adapter
131-
│ ├── params.py # shared parameters for steps
132-
│ └── utils.py # utility functions
82+
│ ├── evaluate_model.py # evaluate base and finetuned models using Rouge metrics
83+
│ ├── finetune.py # finetune the base model
84+
│ ├── prepare_datasets.py # load and tokenize dataset
85+
│ └── promote.py # promote good models to target environment
86+
├── utils # utility functions
87+
│ ├── callbacks.py # custom callbacks
88+
│ ├── cuda.py # helpers for CUDA
89+
│ ├── loaders.py # loaders for models and data
90+
│ ├── logging.py # logging helpers
91+
│ └── tokenizer.py # load and tokenize
13392
├── .dockerignore
13493
├── README.md # this file
13594
├── requirements.txt # extra Python dependencies

template/configs/eval.yaml

Lines changed: 0 additions & 25 deletions
This file was deleted.

template/configs/feature-alpaca.yaml

Lines changed: 0 additions & 16 deletions
This file was deleted.

template/configs/feature-custom.yaml

Lines changed: 0 additions & 19 deletions
This file was deleted.

template/configs/finetune-alpaca.yaml

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)