update readme
Browse files
README.md
CHANGED
@@ -5,4 +5,149 @@ datasets:
|
|
5 |
language:
|
6 |
- en
|
7 |
library_name: transformers
|
8 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
language:
|
6 |
- en
|
7 |
library_name: transformers
|
8 |
+
---
|
9 |
+
# Instruction Tuned GPTJ LoRA using the Alpaca Dataset
|
10 |
+
|
11 |
+
The repo with all the training code can be found at https://github.com/entrpn/serving-model-cards/tree/main/instruct-tune-gptj
|
12 |
+
|
13 |
+
This model contains the LoRA weights for GPTJ-6B. The model was fine tuned on a T4 GPU using 8-bit quantization, gradient checkpointing and gradient accumulation.
|
14 |
+
|
15 |
+
This was trained in a Google Cloud Platform Compute Engine spot VM for 3k steps, costing less than $2 dollars.
|
16 |
+
|
17 |
+
The license should follow the same as [Stanford Alpaca](https://crfm.stanford.edu/2023/03/13/alpaca.html). However, you can use this method with your own dataset and not have the same restrictions.
|
18 |
+
|
19 |
+
Also want to shout out to @tloen as I used his code to generate prompts for training and inference. Please check out the author's repo https://github.com/tloen/alpaca-lora
|
20 |
+
|
21 |
+
## Generations
|
22 |
+
|
23 |
+
First let's look at some results
|
24 |
+
|
25 |
+
```text
|
26 |
+
Instruction : How can I make friends?
|
27 |
+
Response : The best way to make friends is to be friendly and approachable. Show interest in other people by asking them about their lives, interests, and hobbies. Make an effort to get to know people better by inviting them to social events and inviting them to join you in activities. Make an effort to get to know people better by asking them about their lives, interests, and hobbies. Show interest in other people by asking them about their lives, interests, and hobbies. Make an effort to get to know people better by inviting them to social events and inviting them to join you in activities. Make an effort to get to know people better by
|
28 |
+
```
|
29 |
+
|
30 |
+
```text
|
31 |
+
Instruction : Generate an example of what a resume should like for an engineering professional
|
32 |
+
Response : An example of a resume for an engineering professional should include the following information: name, contact information, education, professional experience, skills, and references. The education section should include a list of degrees, certifications, and professional experience. The professional experience section should include a list of projects, responsibilities, and accomplishments. The skills section should include a list of technical and non-technical skills. Finally, the references section should include a list of professional references.
|
33 |
+
```
|
34 |
+
|
35 |
+
```text
|
36 |
+
Instruction : Tell me about lions.
|
37 |
+
Response : Lions are one of the largest cats in the world. They are found in Africa, Asia, and parts of the Middle East. They are known for their powerful jaws, sharp claws, and large manes. They are also known for their ability to roar, which can be heard up to a mile away. They are also known for their ability to hunt and kill large prey such as antelopes, zebras, and wildebeests. They are also known for their ability to defend their territory and their cubs. They are also known for their ability to roar, which can be heard up to a mile away
|
38 |
+
```
|
39 |
+
|
40 |
+
```text
|
41 |
+
Instruction : What are alcapas? How are alpacas different from llamas?
|
42 |
+
Response : Alpacas and llamas are both members of the camelid family. Alpacas are smaller than llamas and have shorter legs. Alpacas are native to South America, while llamas are native to the Andes Mountains of South America. Alpacas and llamas are similar in appearance, but alpacas are smaller and have shorter legs than llamas. Alpacas and llamas are both used as pack animals, but alpacas are smaller and have shorter legs than llamas. Alpacas and llamas are both used for milk production, but alpacas are smaller and have shorter legs than
|
43 |
+
```
|
44 |
+
|
45 |
+
```text
|
46 |
+
Instruction: What is a neural network?
|
47 |
+
Response: A neural network is a type of artificial intelligence (AI) system that is inspired by the structure and function of the human brain. It is a type of machine learning algorithm that is used to learn from data and make predictions. Neural networks can be used for a variety of tasks, such as image recognition, speech recognition, natural language processing, and computer vision. They can also be used to solve problems in areas such as robotics, autonomous vehicles, and medical diagnosis. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [
|
48 |
+
```
|
49 |
+
|
50 |
+
## Examples
|
51 |
+
|
52 |
+
Install dependencies.
|
53 |
+
|
54 |
+
```shell
|
55 |
+
conda create -n py39 python=3.9
|
56 |
+
conda activate py39
|
57 |
+
pip install transformers tensorboard accelerate datasets torch bitsandbytes git+https://github.com/huggingface/peft.git
|
58 |
+
```
|
59 |
+
|
60 |
+
To generate, use the following
|
61 |
+
|
62 |
+
```python
|
63 |
+
import torch
|
64 |
+
from peft import PeftModel, PeftConfig
|
65 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
66 |
+
|
67 |
+
def generate_prompt(instruction, input=None):
|
68 |
+
if input:
|
69 |
+
return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
70 |
+
### Instruction:
|
71 |
+
{instruction}
|
72 |
+
### Input:
|
73 |
+
{input}
|
74 |
+
### Response:"""
|
75 |
+
else:
|
76 |
+
return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
77 |
+
### Instruction:
|
78 |
+
{instruction}
|
79 |
+
### Response:"""
|
80 |
+
|
81 |
+
def evaluate(
|
82 |
+
model,
|
83 |
+
tokenizer,
|
84 |
+
instruction,
|
85 |
+
input=None,
|
86 |
+
temperature=0.1,
|
87 |
+
top_p=0.75,
|
88 |
+
top_k=40,
|
89 |
+
num_beams=4,
|
90 |
+
max_new_tokens=128,
|
91 |
+
**kwargs,
|
92 |
+
):
|
93 |
+
prompt = generate_prompt(instruction, input)
|
94 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
95 |
+
input_ids = inputs["input_ids"].to("cuda")
|
96 |
+
generation_config = GenerationConfig(
|
97 |
+
temperature=temperature,
|
98 |
+
top_p=top_p,
|
99 |
+
top_k=top_k,
|
100 |
+
num_beams=num_beams,
|
101 |
+
**kwargs,
|
102 |
+
)
|
103 |
+
with torch.no_grad():
|
104 |
+
generation_output = model.generate(
|
105 |
+
input_ids=input_ids,
|
106 |
+
generation_config=generation_config,
|
107 |
+
return_dict_in_generate=True,
|
108 |
+
output_scores=True,
|
109 |
+
max_new_tokens=max_new_tokens,
|
110 |
+
)
|
111 |
+
s = generation_output#.sequences[0]
|
112 |
+
s = s.sequences[0]
|
113 |
+
output = tokenizer.decode(s)
|
114 |
+
return output.split("### Response:")[1].strip()
|
115 |
+
|
116 |
+
peft_model_id = "jfacevedo/gptj-alpaca"
|
117 |
+
model_name = "EleutherAI/gpt-j-6B"
|
118 |
+
config = PeftConfig.from_pretrained(peft_model_id)
|
119 |
+
print("loading model")
|
120 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto",revision="float16", load_in_8bit=True)
|
121 |
+
print("loading tokenizer")
|
122 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
123 |
+
|
124 |
+
# Padding token should not be required for inference, but adding it since it was added during training
|
125 |
+
# Add pad token
|
126 |
+
new_tokens = [""]
|
127 |
+
# check if the tokens are already in the vocabulary
|
128 |
+
new_tokens = set(new_tokens) - set(tokenizer.vocab.keys())
|
129 |
+
# add the tokens to the tokenizer vocabulary
|
130 |
+
tokenizer.add_tokens(list(new_tokens))
|
131 |
+
# add new, random embeddings for the new tokens
|
132 |
+
model.resize_token_embeddings(len(tokenizer))
|
133 |
+
tokenizer.pad_token = ""
|
134 |
+
|
135 |
+
# Load the Lora model
|
136 |
+
|
137 |
+
instruction = "Describe the structure of an atom."
|
138 |
+
instruction = "Tell me about alpacas"
|
139 |
+
instruction = "Generate an example of what a resume should like for an engineering professional"
|
140 |
+
instruction = "How can I make friends?"
|
141 |
+
print("instruction: ",instruction)
|
142 |
+
|
143 |
+
# Testing the original model
|
144 |
+
# print("Not finetuned")
|
145 |
+
# print("Response:", evaluate(model, tokenizer, instruction))
|
146 |
+
# print("\n\n")
|
147 |
+
|
148 |
+
print("loading lora model")
|
149 |
+
model = PeftModel.from_pretrained(model, peft_model_id).to("cuda")
|
150 |
+
|
151 |
+
print("Finetuned model")
|
152 |
+
print("Response:", evaluate(model, tokenizer, instruction))
|
153 |
+
```
|