Skip to content

Commit

Permalink
fix: address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cindyli committed Apr 10, 2024
1 parent 4cf061c commit e10b3d6
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 40 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2023, Inclusive Design Institute
Copyright (c) 2023-2024, Inclusive Design Institute

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down
46 changes: 23 additions & 23 deletions docs/Llama2FineTuning.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ to request the access to its Llama2 model;
mkdir llama2
cd llama2
// Load git-lfs first for downloading via Git large file storage
# Load git-lfs first for downloading via Git large file storage
module load StdEnv/2020
module load git-lfs/3.3.0
git lfs install
Expand All @@ -43,7 +44,7 @@ for setting up the Llama2 models into a new file named `requirements-llama2.txt`

## Use the Original Llama2 Model

In the [`jobs/original_use`](../jobs/Llama2/original_use) directory, there are two scripts:
In the [`jobs/Llama2/original_use`](../jobs/Llama2/original_use) directory, there are two scripts:

* original_use_7b_hf.py: The script that loads the downloaded model and tokenizer to perform text generation,
word predictions and making inferences
Expand All @@ -52,8 +53,8 @@ word predictions and making inferences
Note that the job script must be copied to the user's `scratch` directory and is submitted from there using
the `sbatch` command.

FTP scripts above to the cedar cluster in the users `llama2/original_use` directory. Run the following command to
submit the job.
Use FTP to transfer the above scripts to the cedar cluster in the users `llama2/original_use` directory. Run
the following command to submit the job.

```
cp llama2/original_use/job_original_use_7b_hf.sh scratch/.
Expand All @@ -65,28 +66,27 @@ The result is written to the `llama2/original_use/result.txt`.

## Fine-tune the Llama2 Model

In the [`jobs/finetune`](../jobs/Llama2/finetune) directory, there are these scripts:
In the [`jobs/Llama2/finetune`](../jobs/Llama2/finetune) directory, there are these scripts:

* bliss.json: The dataset that converts English text to the structure in the Conceptual Bliss
* finetune_7b_hf.py: The script that fine-tunes the downloaded model
* job_finetune_7b_hf.sh: The job script submitted to Cedar to run `finetune_7b_hf.py`

FTP scripts above to the cedar cluster in the users `llama2/finetune` directory. Run the following command to
submit the job.
Use FTP to transfer the above scripts to the cedar cluster in the users `llama2/finetune` directory. Run
the following command to submit the job.

```
cp llama2/finetune/job_finetune_7b_hf.sh scratch/.
cd scratch
sbatch job_finetune_7b_hf.sh
```

The fine-tuning script does:
The fine-tuning script:

1. Create an instruction dataset using `bliss.json`. This dataset contains bi-directional conversion between
1. Creates an instruction dataset using `bliss.json`. This dataset contains bi-directional conversion between
English and Conceptual Bliss.
2. Use the dataset to fine-tune the Llama2 model. See `finetune_7b_hf.py` about the fine-tuning parameters.
3. Evaluate the fine-tuned model by giving instructions fine-tuned for, along with a few sentences for language
conversion.
2. Uses the dataset to fine-tune the Llama2 model. See `finetune_7b_hf.py` about the fine-tuning parameters.
3. Evaluates the fine-tuned model by testing a few sentence conversions between the English and the Bliss languages.

Please note that due to the relatively small size of the dataset derived from bliss.json, the fine-tuning script
was run four times, adjusting the epoch number in the script from 1 to 4. As a result, 4 models were generated
Expand All @@ -96,16 +96,16 @@ corresponding to the different epoch counts.

This section describes how to evaluate a fine-tuned model with instructions and input sentences.

In the [`jobs/finetune`](../jobs/Llama2/finetune) directory, there are these scripts:
In the [`jobs/Llama2/finetune`](../jobs/Llama2/finetune) directory, there are these scripts:

* eval_7b_hf.py: The script that fine-tunes the downloaded model. Common variables to adjust:
* `model_dir`: The location of the model directory
* `instruction`: At the bottom of the script, define the instruction part in a prompt
* `input`: At the bottom of the script, define the sentence to be converted
* job_eval_7b_hf.sh: The job script submitted to Cedar to run `eval_7b_hf.py`

FTP scripts above to the cedar cluster in the users `llama2/finetune` directory. Run the following command to
submit the job.
Use FTP to transfer the above scripts to the cedar cluster in the users `llama2/finetune` directory. Run
the following command to submit the job.

```
cp llama2/finetune/job_eval_7b_hf.sh scratch/.
Expand All @@ -115,23 +115,23 @@ sbatch job_eval_7b_hf.sh

## Evaluate the Generated Sentences from the Fine-tuned Model

This section describes how to evaluat the generated sentences and compare them with original or expected sentences.
This section describes how to evaluate the generated sentences and compare them with original or expected sentences.
It evaluates the generated sentence in these aspects:

* Semantic Coherence
* Novelty and Creativity
* Fluency and Readability

In the [`jobs/finetune`](../jobs/Llama2/finetune) directory, there are these scripts:
In the [`jobs/Llama2/finetune`](../jobs/Llama2/finetune) directory, there are these scripts:

* eval_generated_sentence.py: The script that fine-tunes the downloaded model. Common variables to adjust:
* `sentence_orig`: The original sentence
* `sentence_expected`: The expected sentence
* `sentence_generated`: The sentence generated by the fine-tuned model
* job_eval_generated_sentence.sh: The job script submitted to Cedar to run `eval_generated_sentence.py`

FTP scripts above to the cedar cluster in the users `llama2/finetune` directory. Run the following command to
submit the job.
Use FTP to transfer the above scripts to the cedar cluster in the users `llama2/finetune` directory. Run
the following command to submit the job.

```
cp llama2/finetune/job_eval_generated_sentence.sh scratch/.
Expand All @@ -146,7 +146,7 @@ and Conceptual Bliss sentence structure, especially with the two-epochs and thre

## References

[Llama2 in the Facebook Research Github repository](https://github.com/facebookresearch/llama)
[Llama2 fine-tune, inference examples](https://github.com/facebookresearch/llama-recipes)
[Llama2 on Hugging Face](https://huggingface.co/docs/transformers/model_doc/llama2)
[Use Hugging Face Models on Cedar Clusters](https://docs.alliancecan.ca/wiki/Huggingface)
* [Llama2 in the Facebook Research Github repository](https://github.com/facebookresearch/llama)
* [Llama2 fine-tune, inference examples](https://github.com/facebookresearch/llama-recipes)
* [Llama2 on Hugging Face](https://huggingface.co/docs/transformers/model_doc/llama2)
* [Use Hugging Face Models on Cedar Clusters](https://docs.alliancecan.ca/wiki/Huggingface)
8 changes: 8 additions & 0 deletions jobs/Llama2/finetune/eval_7b_hf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Copyright (c) 2023-2024, Inclusive Design Institute
#
# Licensed under the BSD 3-Clause License. You may not use this file except
# in compliance with this License.
#
# You may obtain a copy of the BSD 3-Clause License at
# https://github.com/inclusive-design/baby-bliss-bot/blob/main/LICENSE

import torch
from peft import AutoPeftModelForCausalLM
from transformers import AutoTokenizer
Expand Down
8 changes: 8 additions & 0 deletions jobs/Llama2/finetune/eval_generated_sentence.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Copyright (c) 2023-2024, Inclusive Design Institute
#
# Licensed under the BSD 3-Clause License. You may not use this file except
# in compliance with this License.
#
# You may obtain a copy of the BSD 3-Clause License at
# https://github.com/inclusive-design/baby-bliss-bot/blob/main/LICENSE

import spacy
from sentence_transformers import SentenceTransformer, util
from sklearn.feature_extraction.text import TfidfVectorizer
Expand Down
8 changes: 8 additions & 0 deletions jobs/Llama2/finetune/finetune_7b_hf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Copyright (c) 2023-2024, Inclusive Design Institute
#
# Licensed under the BSD 3-Clause License. You may not use this file except
# in compliance with this License.
#
# You may obtain a copy of the BSD 3-Clause License at
# https://github.com/inclusive-design/baby-bliss-bot/blob/main/LICENSE

import torch
from datasets import load_dataset, concatenate_datasets
from transformers import (
Expand Down
9 changes: 9 additions & 0 deletions jobs/Llama2/finetune/job_eval_7b_hf.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
#!/bin/bash

# Copyright (c) 2023-2024, Inclusive Design Institute
#
# Licensed under the BSD 3-Clause License. You may not use this file except
# in compliance with this License.
#
# You may obtain a copy of the BSD 3-Clause License at
# https://github.com/inclusive-design/baby-bliss-bot/blob/main/LICENSE

#SBATCH --job-name=llama2-finetune-7b-hf
#SBATCH --time 2-00:00
#SBATCH --nodes=1
Expand Down
9 changes: 9 additions & 0 deletions jobs/Llama2/finetune/job_eval_generated_sentence.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
#!/bin/bash

# Copyright (c) 2023-2024, Inclusive Design Institute
#
# Licensed under the BSD 3-Clause License. You may not use this file except
# in compliance with this License.
#
# You may obtain a copy of the BSD 3-Clause License at
# https://github.com/inclusive-design/baby-bliss-bot/blob/main/LICENSE

#SBATCH --job-name=llama2-finetune-7b-hf
#SBATCH --time 2-00:00
#SBATCH --nodes=1
Expand Down
9 changes: 9 additions & 0 deletions jobs/Llama2/finetune/job_finetune_7b_hf.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
#!/bin/bash

# Copyright (c) 2023-2024, Inclusive Design Institute
#
# Licensed under the BSD 3-Clause License. You may not use this file except
# in compliance with this License.
#
# You may obtain a copy of the BSD 3-Clause License at
# https://github.com/inclusive-design/baby-bliss-bot/blob/main/LICENSE

#SBATCH --job-name=llama2-finetune-7b-hf
#SBATCH --time 2-00:00
#SBATCH --nodes=1
Expand Down
9 changes: 9 additions & 0 deletions jobs/Llama2/original_use/job_original_use_7b_hf.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
#!/bin/bash

# Copyright (c) 2023-2024, Inclusive Design Institute
#
# Licensed under the BSD 3-Clause License. You may not use this file except
# in compliance with this License.
#
# You may obtain a copy of the BSD 3-Clause License at
# https://github.com/inclusive-design/baby-bliss-bot/blob/main/LICENSE

#SBATCH --job-name=llama2-orig-use-7b-hf
#SBATCH --time 10-00:00
#SBATCH --nodes=1
Expand Down
8 changes: 8 additions & 0 deletions jobs/Llama2/original_use/original_use_7b_hf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Copyright (c) 2023-2024, Inclusive Design Institute
#
# Licensed under the BSD 3-Clause License. You may not use this file except
# in compliance with this License.
#
# You may obtain a copy of the BSD 3-Clause License at
# https://github.com/inclusive-design/baby-bliss-bot/blob/main/LICENSE

from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline

model_dir = "/home/cindyli/llama2/Llama-2-7b-hf"
Expand Down
4 changes: 1 addition & 3 deletions jobs/stylegan2-ada/ctb-styleGAN2AdaPytorchGenerateBatch.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

# Copyright (c) 2023, Inclusive Design Institute
# Copyright (c) 2023-2024, Inclusive Design Institute
#
# Licensed under the BSD 3-Clause License. You may not use this file except
# in compliance with this License.
Expand Down Expand Up @@ -39,5 +39,3 @@ mkdir -p "$OUTPUT_DIR"

# Generate...
python ~/BlissStyleGAN/StyleGAN2/stylegan2-ada-pytorch/generate.py --outdir="$OUTPUT_DIR" --trunc=0.5 --seeds=200,330,400 --network="$MODEL_FILE"


4 changes: 1 addition & 3 deletions jobs/stylegan2-ada/def-styleGAN2AdaPytorchGenerateBatch.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

# Copyright (c) 2023, Inclusive Design Institute
# Copyright (c) 2023-2024, Inclusive Design Institute
#
# Licensed under the BSD 3-Clause License. You may not use this file except
# in compliance with this License.
Expand Down Expand Up @@ -41,5 +41,3 @@ mkdir -p "$OUTPUT_DIR"
#
# This third command is resuming for another 12 hours, using latest model
python ~/BlissStyleGAN/StyleGAN2/stylegan2-ada-pytorch/generate.py --outdir="$OUTPUT_DIR" --trunc=0.5 --seeds=600-605 --network="$MODEL_FILE"


3 changes: 1 addition & 2 deletions jobs/stylegan2-ada/def-styleGAN2AdaPytorchTrainBatch.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

# Copyright (c) 2023, Inclusive Design Institute
# Copyright (c) 2023-2024, Inclusive Design Institute
#
# Licensed under the BSD 3-Clause License. You may not use this file except
# in compliance with this License.
Expand Down Expand Up @@ -58,4 +58,3 @@ python ~/BlissStyleGAN/StyleGAN2/stylegan2-ada-pytorch/train.py --outdir="$OUTPU
# This third command is resuming for another 15 hours, using latest model.
# Again, the actual values here may differ for different groups of runs.
# python ~/BlissStyleGAN/StyleGAN2/stylegan2-ada-pytorch/train.py --outdir="$OUTPUT_DIR" --data="$DATA_DIR" --snap=10 --resume="$OUTPUT_DIR/00001-preppedBlissSingleCharGrey-auto1-resumecustom/network-snapshot-000440.pkl"

8 changes: 1 addition & 7 deletions jobs/stylegan2-ada/def-styleGan2AdaPytorchDataSetupBatch.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

# Copyright (c) 2023, Inclusive Design Institute
# Copyright (c) 2023-2024, Inclusive Design Institute
#
# Licensed under the BSD 3-Clause License. You may not use this file except
# in compliance with this License.
Expand Down Expand Up @@ -59,9 +59,3 @@ else
echo "dataset_tool.py failed with exit status $STATUS"
fi
echo Done!






2 changes: 1 addition & 1 deletion utils/scale_down_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from PIL import Image

"""
Copyright (c) 2023, Inclusive Design Institute
Copyright (c) 2023-2024, Inclusive Design Institute
Licensed under the BSD 3-Clause License. You may not use this file except
in compliance with this License.
Expand Down

0 comments on commit e10b3d6

Please sign in to comment.