Skip to content

Commit

Permalink
chore: rename use case and notebook + add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrery committed Sep 9, 2024
1 parent b32475c commit e63afe9
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/refresh-one-notebook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ on:
- FullyConnectedNeuralNetwork \n
- FullyConnectedNeuralNetworkOnMNIST \n
- GLMComparison \n
- gpt2_finetune_hybrid \n
- GPT2FineTuneHybrid \n
- HealthCarePrediction \n
- ImportingFromScikitLearn \n
- KaggleTitanic \n
Expand Down Expand Up @@ -68,7 +68,7 @@ env:
FullyConnectedNeuralNetwork: "docs/advanced_examples/FullyConnectedNeuralNetwork.ipynb"
FullyConnectedNeuralNetworkOnMNIST: "docs/advanced_examples/FullyConnectedNeuralNetworkOnMNIST.ipynb"
GLMComparison: "docs/advanced_examples/GLMComparison.ipynb"
gpt2_finetune_hybrid: "use_case_examples/lora_finetune/gpt2_finetune_hybrid.ipynb"
GPT2FineTuneHybrid: "use_case_examples/lora_finetuning/GPT2FineTuneHybrid.ipynb"
HealthCarePrediction: "use_case_examples/disease_prediction/HealthCarePrediction.ipynb"
ImportingFromScikitLearn: "docs/advanced_examples/ImportingFromScikitLearn.ipynb"
KaggleTitanic: "use_case_examples/titanic/KaggleTitanic.ipynb"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,7 @@
}
],
"source": [
"# Inference using the fine-tuned model with LoRA weights\n",
"# Seed for best reproducibility\n",
"torch.manual_seed(SEED)\n",
"\n",
Expand All @@ -1873,6 +1874,7 @@
}
],
"source": [
"# Original inference without LoRA weights\n",
"# Seed for best reproducibility\n",
"torch.manual_seed(SEED)\n",
"\n",
Expand Down
File renamed without changes.
141 changes: 141 additions & 0 deletions use_case_examples/lora_finetuning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Privacy Preserving GPT2 LoRA

This project demonstrates how to fine-tune GPT-2 using Low-Rank Adaptation (LoRA) weights with Fully Homomorphic Encryption (FHE). The goal is to train a specialized model in a privacy-preserving manner, with minimal memory requirements.

## Overview

Fine-tuning large language models typically requires access to sensitive data, which can raise privacy concerns. By leveraging FHE, we can perform computations on encrypted data, ensuring that the data remains private throughout the training process. In this approach, the LoRA weights are only known to the user who owns the data and the memory hungry foundation model remains on the server.

## Key Features

- **LoRA Fine-Tuning**: Fine-tune GPT-2 by adapting low-rank weights.
- **Fully Homomorphic Encryption**: Perform training and inference on encrypted data.
- **Hybrid Model**: Combine traditional and encrypted computations for optimal performance.
- **Low Memory Requirements**: Minimal client-side memory needed for LoRA weights.

## Setup

### Installation

Install the required packages:

```sh
pip install -r requirements.txt
```

## Usage

### 1. Prepare the Dataset

Replace the dataset in the `data_finetune` directory to the one you want to use for fine-tuning.

### 2. Run the Fine-Tuning Script

Execute the Jupyter notebook `GPT2FineTuneHybrid.ipynb` to start the fine-tuning process. The notebook is structured into several steps:

1. **Load Pre-trained Model and Tokenizer**:

<!--pytest-codeblocks:skip-->

```python
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
```

2. **Freeze Model Weights**:

<!--pytest-codeblocks:skip-->

```python
for param in model.parameters():
param.requires_grad = False
```

3. **Define LoRA Configuration**:

<!--pytest-codeblocks:skip-->

```python
peft_config = LoraConfig(
task_type=TaskType.CAUSAL_LM,
r=4,
lora_alpha=32,
lora_dropout=0.05,
fan_in_fan_out=True,
)
peft_model = get_peft_model(model, peft_config)
```

4. **Prepare Training Arguments and Data**:

<!--pytest-codeblocks:skip-->

```python
training_args = TrainingArguments(
output_dir="./checkpoints",
num_train_epochs=100,
per_device_train_batch_size=8,
gradient_accumulation_steps=2,
use_cpu=True,
learning_rate=5e-4,
logging_strategy="epoch",
optim="adamw_torch",
seed=0,
data_seed=0,
weight_decay=0.0,
warmup_steps=0,
max_grad_norm=1.0,
)
```

6. **Train the Model**:

<!--pytest-codeblocks:skip-->

```python
train_custom_model(hybrid_model, train_dataloader, training_args, fhe="simulate")
```

### 3. Generate Text with the Fine-Tuned Model

After training, you can generate text using the fine-tuned model:

<!--pytest-codeblocks:skip-->

```python
prompt = "What is FHE?"
generated_text = generate_text(prompt, fine_tuned_model, tokenizer)
print(generated_text)
```

## Deployment/Production Scenario

In a deployment or production scenario, the model can be fine-tuned as follows:

1. **Server Setup**: The server hosts a foundation model with generic weights.
1. **Client Setup**: The user (client) has a set of LoRA weights and the sensitive data required for fine-tuning.
1. **Fine-Tuning Process**:
- The client requests inference and backward passes from the server, which uses the generic weights/parameters.
- Any computation that requires the LoRA weights is executed on the client's end.
1. **Storage**: The LoRA weights are stored on the client's end for later inference, ensuring full privacy of both the specialized model and the sensitive data.

## Results

The fine-tuned model can generate specialized text based on the provided dataset while ensuring data privacy through FHE.

After fine-tuning, the model's weights are distributed between the client and server as follows:

- Total weights removed from the server: 68.24%
- LoRA weights kept on the client: 147,456 (approximately 0.12% of the original model's weights)

This distribution significantly reduces the memory requirements on the client side while maintaining the privacy of the fine-tuned model.

## Conclusion

This project showcases the potential of combining LoRA and FHE to fine-tune language models in a privacy-preserving manner. By following the steps outlined in the notebook, you can adapt this approach to your own datasets and use cases.

## References

- [LoRA: Low-Rank Adaptation of Large Language Models](https://arxiv.org/abs/2106.09685)
- [PEFT](https://github.com/huggingface/peft)

0 comments on commit e63afe9

Please sign in to comment.