Commit ·
6008455
1
Parent(s): cc96d09
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,55 @@
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
+
---
|
| 4 |
+
## Usage
|
| 5 |
+
Let's define a function to convert `instruction` and `input` into a single prompt as input to our `model.generate`
|
| 6 |
+
```python
|
| 7 |
+
def generate_prompt(instruction, input=None):
|
| 8 |
+
# Templates used by Stanford Alpaca: https://github.com/tatsu-lab/stanford_alpaca
|
| 9 |
+
if input is not None:
|
| 10 |
+
prompt = 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{input}\n\n### Response:"
|
| 11 |
+
else:
|
| 12 |
+
prompt = f"prompt_no_input": "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Response:"
|
| 13 |
+
return prompt
|
| 14 |
+
```
|
| 15 |
+
Load model and generate prediction
|
| 16 |
+
|
| 17 |
+
```python
|
| 18 |
+
import sys
|
| 19 |
+
import torch
|
| 20 |
+
from transformers import LlamaTokenizer, LlamaForCausalLM
|
| 21 |
+
from peft import PeftModel
|
| 22 |
+
|
| 23 |
+
tokenizer = LlamaTokenizer.from_pretrained("decapoda-research/llama-7b-hf")
|
| 24 |
+
model = LlamaForCausalLM.from_pretrained("decapoda-research/llama-7b-hf",
|
| 25 |
+
load_in_8bit=True,
|
| 26 |
+
dtype=torch.float16,
|
| 27 |
+
device_map="auto")
|
| 28 |
+
model = PeftModel.from_pretrained("Fsoft-AIC/CodeCapybara-LoRA",
|
| 29 |
+
load_in_8bit=True,
|
| 30 |
+
dtype=torch.float16,
|
| 31 |
+
device_map="auto")
|
| 32 |
+
|
| 33 |
+
model.config.pad_token_id = tokenizer.pad_token_id = 0
|
| 34 |
+
model.config.bos_token_id = 1
|
| 35 |
+
model.config.eos_token_id = 2
|
| 36 |
+
|
| 37 |
+
model.eval()
|
| 38 |
+
if torch.__version__ >= "2" and sys.platform != "win32":
|
| 39 |
+
model = torch.compile(model)
|
| 40 |
+
|
| 41 |
+
instruction = "Write a Python program that prints the first 10 Fibonacci numbers"
|
| 42 |
+
prompt = generate_prompt(instruction)
|
| 43 |
+
|
| 44 |
+
input_ids = tokenizer(prompt)["input_ids"]
|
| 45 |
+
|
| 46 |
+
generation_config = GenerationConfig(temperature=0.1,
|
| 47 |
+
top_k=40,
|
| 48 |
+
top_p=0.75)
|
| 49 |
+
with torch.no_grad():
|
| 50 |
+
output_ids = model.generate(inputs,
|
| 51 |
+
generation_config=generation_config,
|
| 52 |
+
max_new_tokens=128)
|
| 53 |
+
output = tokenizer.decode(output_ids, skip_special_tokens=True, ignore_tokenization_space=True)
|
| 54 |
+
print(output)
|
| 55 |
+
```
|