BEncoderRT commited on
Commit
41633e9
·
verified ·
1 Parent(s): 8e9784f

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +205 -0
README.md CHANGED
@@ -106,9 +106,214 @@ Loss is computed using **standard causal language modeling**, meaning the model
106
  ### 5. Start
107
 
108
  ```python
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  ---
113
 
114
  ## 📊 Why QLoRA?
 
106
  ### 5. Start
107
 
108
  ```python
109
+ import torch
110
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
111
+ from peft import PeftModel, LoraConfig
112
+
113
+ base_model_name = "EleutherAI/pythia-1B-deduped"
114
+ lora_repo = "BEncoderRT/Pythia-QLoRA-Instruction-Tuning"
115
+
116
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name)
117
+ tokenizer.pad_token = tokenizer.eos_token
118
+
119
+
120
+
121
+ # Load the base model with the new quantization configuration
122
+ base_model = AutoModelForCausalLM.from_pretrained(
123
+ base_model_name,
124
+ device_map="auto",
125
+ dtype=torch.bfloat16 # Corrected: Use dtype instead of torch_dtype
126
+ )
127
+
128
+ # Load the PEFT model (LoRA adapters)
129
+ model = PeftModel.from_pretrained(base_model, lora_repo)
130
+
131
+ ```
132
+
133
+ ```python
134
+ import torch
135
+
136
+ # Ensure the model is in evaluation mode
137
+ model.eval()
138
+
139
+ # Function to format prompts consistently with training data
140
+ def format_prompt(instruction, context=None):
141
+ if context:
142
+ 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.\n\n### Instruction:\n{instruction}\n\n### Input:\n{context}\n\n### Response:\n"
143
+ else:
144
+ return f"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Response:\n"
145
+
146
+ # Define a few test prompts
147
+ test_prompts = [
148
+ {
149
+ "instruction": "Explain the concept of photosynthesis in simple terms.",
150
+ "context": None
151
+ },
152
+ {
153
+ "instruction": "What is the capital of France?",
154
+ "context": None
155
+ },
156
+ {
157
+ "instruction": "Summarize the main idea of the following text:",
158
+ "context": "The quick brown fox jumps over the lazy dog. This sentence is often used to display all letters of the English alphabet, making it a pangram."
159
+ },
160
+ {
161
+ "instruction": "List three benefits of regular exercise.",
162
+ "context": None
163
+ }
164
+ ]
165
+
166
+ # Add the new test prompts (assuming `new_test_prompts` is defined as in the previous step)
167
+ new_test_prompts = [
168
+ {
169
+ "instruction": "Write a short, imaginative story about a cat who discovers a secret portal to another dimension under its owner's bed.",
170
+ "context": None
171
+ },
172
+ {
173
+ "instruction": "If a train leaves New York at 10 AM traveling at 60 mph and another train leaves Chicago at 11 AM traveling at 50 mph, and the cities are 800 miles apart, at what time do they meet? (Assume they are traveling towards each other on the same track).",
174
+ "context": None
175
+ },
176
+ {
177
+ "instruction": "What is the capital of Australia?",
178
+ "context": None
179
+ },
180
+ {
181
+ "instruction": "Explain the difference between supervised and unsupervised learning in machine learning, and provide an example of when each would be used.",
182
+ "context": None
183
+ },
184
+ {
185
+ "instruction": "Summarize the following passage:",
186
+ "context": "The advent of artificial intelligence has brought forth a new era of technological advancement, impacting various sectors from healthcare to finance. While AI promises increased efficiency and innovative solutions, it also raises ethical concerns regarding job displacement, privacy, and bias in algorithms. Societies worldwide are grappling with how to regulate and integrate AI responsibly, balancing progress with human values. This calls for a multidisciplinary approach involving policymakers, technologists, ethicists, and the public to shape a future where AI serves humanity's best interests."
187
+ }
188
+ ]
189
+ test_prompts.extend(new_test_prompts)
190
+
191
+ # Generate responses for each test prompt
192
+ print("\n--- Generating Responses ---\n")
193
+ with torch.no_grad():
194
+ for i, prompt_data in enumerate(test_prompts):
195
+ instruction = prompt_data["instruction"]
196
+ context = prompt_data["context"]
197
+
198
+ formatted_input = format_prompt(instruction, context)
199
+
200
+ # Tokenize the input prompt
201
+ inputs = tokenizer(formatted_input, return_tensors="pt").to(model.device)
202
+
203
+ # Generate response, explicitly using both eos_token_id and pad_token_id
204
+ outputs = model.generate(
205
+ **inputs,
206
+ max_new_tokens=150, # Limit the length of the generated response
207
+ do_sample=True, # Enable sampling for more diverse responses
208
+ temperature=0.7, # Control randomness (lower means less random)
209
+ top_k=50, # Consider only top 50 probable tokens
210
+ top_p=0.95, # Nucleus sampling
211
+ eos_token_id=tokenizer.eos_token_id,
212
+ pad_token_id=tokenizer.pad_token_id # Explicitly set pad_token_id
213
+ )
214
+
215
+ # Decode the generated tokens
216
+ # Exclude the input prompt from the decoded text to get only the model's response
217
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
218
+ response = generated_text[len(formatted_input):].strip()
219
+
220
+ print(f"### Test Prompt {i+1}: ###")
221
+ print(f"Instruction: {instruction}")
222
+ if context:
223
+ print(f"Context: {context}")
224
+ print(f"Model Response: {response}\n")
225
 
226
  ```
227
+ ---
228
+ ```
229
+
230
+ --- Generating Responses ---
231
+
232
+ ### Test Prompt 1: ###
233
+ Instruction: Explain the concept of photosynthesis in simple terms.
234
+ Model Response: Photosynthesis is a process in which green plants absorb sunlight and convert it to energy to be used by the living plant. The process of photosynthesis is the process of converting the energy from the sun's rays into an organic compound called a molecule. The process of photosynthesis occurs when the energy from the sun is transformed into chemical energy in the form of energy-rich molecules called pigments. The pigments of the plant are the photosynthetic products and they are absorbed by the plant. The process of photosynthesis is the process of converting the energy from the sun into organic compounds called pigments and absorbing them. In plants, the process of photosynthesis is the process of converting the energy from the sun into organic compounds and converting them into pigments.
235
+
236
+ ### Test Prompt 2: ###
237
+ Instruction: What is the capital of France?
238
+ Model Response: Paris is the capital of France. The city is located on the Mediterranean coast of France and is considered to be the most densely populated city in the world. It has an estimated population of about 8.3 million people. It is the fourth most populous city in Europe and the second most populous city in North America. The city's economy is based on tourism, with most of the tourists coming from around the world. It is also a major international hub for finance and technology. The city has hosted many notable people, including the Pope, who is the longest serving Catholic leader in the world.
239
+
240
+ The city is the seat of the region of Paris, which has its own government and a parliament called the National Assembly. It is the largest
241
+
242
+ ### Test Prompt 3: ###
243
+ Instruction: Summarize the main idea of the following text:
244
+ Context: The quick brown fox jumps over the lazy dog. This sentence is often used to display all letters of the English alphabet, making it a pangram.
245
+ Model Response: The quick brown fox jumps over the lazy dog. This sentence is often used to display all letters of the English alphabet, making it a pangram.
246
+
247
+ In a pangram, the letters of the alphabet are grouped in a set of groups called "pangrams". The word "pangram" means "a pangram" or "a pangram-like structure".
248
+
249
+ In the sentence "The quick brown fox jumps over the lazy dog" there are four groups of characters:
250
+
251
+ The quick brown fox jumps over the lazy dog.
252
 
253
+ The lazy dog is lazy.
254
+
255
+ The quick brown fox jumps over the lazy dog.
256
+
257
+ The lazy dog is not lazy.
258
+
259
+ The quick brown
260
+
261
+ ### Test Prompt 4: ###
262
+ Instruction: List three benefits of regular exercise.
263
+ Model Response: 1. It helps you stay fit.
264
+ 2. It helps you relax.
265
+ 3. It helps you lose weight.
266
+ 4. It helps you lose your bad habits.
267
+ 5. It helps you stay healthy.
268
+ 6. It helps you improve your mood.
269
+ 7. It helps you sleep better.
270
+ 8. It helps you lose your extra weight.
271
+ 9. It helps you lose your bad habits.
272
+ 10. It helps you live longer.
273
+ 11. It helps you live a healthier lifestyle.
274
+ 12. It helps you live a healthier lifestyle.
275
+ 13. It helps you live a healthier lifestyle.
276
+ 14. It helps you live a healthier lifestyle.
277
+ 15. It helps you live a healthier lifestyle.
278
+
279
+ ### Test Prompt 5: ###
280
+ Instruction: Write a short, imaginative story about a cat who discovers a secret portal to another dimension under its owner's bed.
281
+ Model Response: Cat, a cat, was sleeping in her bed when she heard a loud noise. She woke up to find her owner, Mr. J, who was out of bed. He said that he heard a loud noise and wondered where the noise was coming from. He then saw the cat's paw print and realized that he was sleeping under his bed. The cat asked him why he was sleeping there and he explained that he had a secret portal to another dimension where he could escape. The cat then told him that he would have to share this secret with him. Mr. J agreed to share this secret with the cat. The cat explained to Mr. J that he would have to find a portal to get to this dimension. Mr. J looked for
282
+
283
+ ### Test Prompt 6: ###
284
+ Instruction: If a train leaves New York at 10 AM traveling at 60 mph and another train leaves Chicago at 11 AM traveling at 50 mph, and the cities are 800 miles apart, at what time do they meet? (Assume they are traveling towards each other on the same track).
285
+ Model Response: The time of arrival is the same. The time of departure is different because the train leaving New York is traveling faster than the train leaving Chicago. If the train leaving New York is at 10 AM and the train leaving Chicago is at 11 AM, then the time of arrival is at 10:00 AM and the time of departure is at 11:00 AM. If the train leaving New York is at 10 AM and the train leaving Chicago is at 11 AM, then the time of arrival is at 10:00 AM and the time of departure is at 11:00 AM. So, the time of arrival is at 10:00 AM and the time of departure is at 11:00 AM.
286
+
287
+ If the trains are traveling toward each other on
288
+
289
+ ### Test Prompt 7: ###
290
+ Instruction: What is the capital of Australia?
291
+ Model Response: The capital of Australia is Sydney.
292
+
293
+ Sydney is a city in Australia. It is a major city in Australia. It is the most populous city in Australia.
294
+
295
+ Sydney is a major financial centre. It is the world's most populous city by a wide margin. It is the world's most populous city by a wide margin.
296
+
297
+ Sydney is home to the Sydney Opera House and the National Library of Australia.
298
+
299
+ Sydney is home to the Sydney Harbour Bridge and the Sydney Opera House.
300
+
301
+ Sydney is home to the Sydney Cricket Ground and the Sydney Football Stadium.
302
+
303
+ Sydney is home to the Sydney Tower and the Sydney Aquarium.
304
+
305
+ ### Test Prompt 8: ###
306
+ Instruction: Explain the difference between supervised and unsupervised learning in machine learning, and provide an example of when each would be used.
307
+ Model Response: In machine learning, supervised learning requires a dataset that has been labelled by a human. This is often done by manually creating a labelled dataset that has to be annotated with a set of labels that a machine can understand. For example, a machine might be given a dataset of people's names and asked to categorize them. It might be possible for a machine to learn a person's name and then use it to classify the rest of the person's name. It's very easy to train a machine that can do this. However, there are a number of limitations with this approach. For example, a machine that can categorize a person's name may be unable to understand the person's ethnicity, nationality, or any other characteristics that a person might have
308
+
309
+ ### Test Prompt 9: ###
310
+ Instruction: Summarize the following passage:
311
+ Context: The advent of artificial intelligence has brought forth a new era of technological advancement, impacting various sectors from healthcare to finance. While AI promises increased efficiency and innovative solutions, it also raises ethical concerns regarding job displacement, privacy, and bias in algorithms. Societies worldwide are grappling with how to regulate and integrate AI responsibly, balancing progress with human values. This calls for a multidisciplinary approach involving policymakers, technologists, ethicists, and the public to shape a future where AI serves humanity's best interests.
312
+ Model Response: Artificial intelligence promises increased efficiency and innovative solutions, but also raises ethical concerns regarding job displacement, privacy, and bias in algorithms. Societies worldwide are grappling with how to regulate and integrate AI responsibly, balancing progress with human values. This calls for a multidisciplinary approach involving policymakers, technologists, ethicists, and the public to shape a future where AI serves humanity's best interests.
313
+
314
+ Artificial intelligence promises increased efficiency and innovative solutions, but also raises ethical concerns regarding job displacement, privacy, and bias in algorithms. Societies worldwide are grappling with how to regulate and integrate AI responsibly, balancing progress with human values. This calls for a multidisciplinary approach involving policymakers, technologists, ethicists, and the public to shape
315
+
316
+ ```
317
  ---
318
 
319
  ## 📊 Why QLoRA?