Commit
·
12c4100
1
Parent(s):
52b5206
Update README.md
Browse files
README.md
CHANGED
|
@@ -30,9 +30,10 @@ import torch
|
|
| 30 |
from transformers import LlamaForCausalLM, LlamaTokenizer
|
| 31 |
|
| 32 |
# the previewed version of OpenAlpaca
|
| 33 |
-
model_path = r'openllmplayground/openalpaca_7b_700bt_preview'
|
| 34 |
tokenizer = LlamaTokenizer.from_pretrained(model_path)
|
| 35 |
model = LlamaForCausalLM.from_pretrained(model_path).cuda()
|
|
|
|
| 36 |
|
| 37 |
# same prompt as provided in https://crfm.stanford.edu/2023/03/13/alpaca.html
|
| 38 |
instruction = r'What is an alpaca? How is it different from a llama?'
|
|
@@ -42,16 +43,15 @@ instruction = r'What is the capital of Tanzania?'
|
|
| 42 |
instruction = r'Write a well-thought out abstract for a machine learning paper that proves that 42 is the optimal seed for training neural networks.'
|
| 43 |
'''
|
| 44 |
|
| 45 |
-
prompt_no_input =
|
| 46 |
tokens = tokenizer.encode(prompt_no_input)
|
| 47 |
-
|
| 48 |
-
tokens =
|
| 49 |
-
tokens = torch.LongTensor(tokens[-1024:]).unsqueeze(0).cuda()
|
| 50 |
instance = {'input_ids': tokens,
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
length = len(tokens[0])
|
| 56 |
with torch.no_grad():
|
| 57 |
rest = model.generate(
|
|
@@ -62,14 +62,12 @@ with torch.no_grad():
|
|
| 62 |
top_p=instance['top_p'],
|
| 63 |
top_k=instance['top_k']
|
| 64 |
)
|
| 65 |
-
|
| 66 |
output = rest[0][length:]
|
| 67 |
-
string = tokenizer.decode(output, skip_special_tokens=
|
| 68 |
-
string = string.replace('<s>', '').replace('</s>', '').strip()
|
| 69 |
print(f'[!] Generation results: {string}')
|
| 70 |
```
|
| 71 |
|
| 72 |
-
|
| 73 |
# License and Usage
|
| 74 |
|
| 75 |
OpenAlpaca is permissively licensed under the Apache 2.0 license and can be used freely for academic/commercial purposes.
|
|
|
|
| 30 |
from transformers import LlamaForCausalLM, LlamaTokenizer
|
| 31 |
|
| 32 |
# the previewed version of OpenAlpaca
|
| 33 |
+
model_path = r'openllmplayground/openalpaca_7b_700bt_preview'
|
| 34 |
tokenizer = LlamaTokenizer.from_pretrained(model_path)
|
| 35 |
model = LlamaForCausalLM.from_pretrained(model_path).cuda()
|
| 36 |
+
tokenizer.bos_token_id, tokenizer.eos_token_id = 1,2 # see https://github.com/openlm-research/open_llama#preview-weights-release-and-usage
|
| 37 |
|
| 38 |
# same prompt as provided in https://crfm.stanford.edu/2023/03/13/alpaca.html
|
| 39 |
instruction = r'What is an alpaca? How is it different from a llama?'
|
|
|
|
| 43 |
instruction = r'Write a well-thought out abstract for a machine learning paper that proves that 42 is the optimal seed for training neural networks.'
|
| 44 |
'''
|
| 45 |
|
| 46 |
+
prompt_no_input = 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:'
|
| 47 |
tokens = tokenizer.encode(prompt_no_input)
|
| 48 |
+
|
| 49 |
+
tokens = torch.LongTensor(tokens).unsqueeze(0)
|
|
|
|
| 50 |
instance = {'input_ids': tokens,
|
| 51 |
+
'top_k': 50,
|
| 52 |
+
'top_p': 0.9,
|
| 53 |
+
'generate_len': 128}
|
| 54 |
+
|
| 55 |
length = len(tokens[0])
|
| 56 |
with torch.no_grad():
|
| 57 |
rest = model.generate(
|
|
|
|
| 62 |
top_p=instance['top_p'],
|
| 63 |
top_k=instance['top_k']
|
| 64 |
)
|
| 65 |
+
|
| 66 |
output = rest[0][length:]
|
| 67 |
+
string = tokenizer.decode(output, skip_special_tokens=True)
|
|
|
|
| 68 |
print(f'[!] Generation results: {string}')
|
| 69 |
```
|
| 70 |
|
|
|
|
| 71 |
# License and Usage
|
| 72 |
|
| 73 |
OpenAlpaca is permissively licensed under the Apache 2.0 license and can be used freely for academic/commercial purposes.
|