-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: rename use case and notebook + add readme
- Loading branch information
Showing
9 changed files
with
145 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.