Commit
·
d5394ff
1
Parent(s):
7e81e5f
Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,71 @@
|
|
1 |
---
|
2 |
license: mit
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: mit
|
3 |
---
|
4 |
+
```python
|
5 |
+
model = LlamaForCausalLM.from_pretrained(
|
6 |
+
'/llama_7b_hf',
|
7 |
+
load_in_8bit=True,
|
8 |
+
torch_dtype=torch.float16,
|
9 |
+
device_map='auto',
|
10 |
+
)
|
11 |
+
|
12 |
+
lora_weights = 'ashwinram472/alpaca-cleaned-lora-7b'
|
13 |
+
|
14 |
+
model = PeftModel.from_pretrained(
|
15 |
+
model,
|
16 |
+
lora_weights,
|
17 |
+
torch_dtype=torch.float16,
|
18 |
+
)
|
19 |
+
|
20 |
+
tokenizer = LlamaTokenizer.from_pretrained("../models/llama_7b_hf")
|
21 |
+
|
22 |
+
def generate_prompt(instruction: str, input_ctxt: str = None) -> str:
|
23 |
+
if input_ctxt:
|
24 |
+
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.
|
25 |
+
|
26 |
+
### Instruction:
|
27 |
+
{instruction}
|
28 |
+
|
29 |
+
### Input:
|
30 |
+
{input_ctxt}
|
31 |
+
|
32 |
+
### Response:"""
|
33 |
+
else:
|
34 |
+
return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
35 |
+
|
36 |
+
### Instruction:
|
37 |
+
{instruction}
|
38 |
+
|
39 |
+
### Response:"""
|
40 |
+
|
41 |
+
generation_config = GenerationConfig(
|
42 |
+
temperature=0.1,
|
43 |
+
top_p=0.75,
|
44 |
+
top_k=40,
|
45 |
+
num_beams=4,
|
46 |
+
max_new_tokens=128,
|
47 |
+
)
|
48 |
+
|
49 |
+
model.eval()
|
50 |
+
|
51 |
+
|
52 |
+
instruction = "Count up from 1 to 500."
|
53 |
+
|
54 |
+
input_ctxt = None # For some tasks, you can provide an input context to help the model generate a better response.
|
55 |
+
|
56 |
+
prompt = generate_prompt(instruction, input_ctxt)
|
57 |
+
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
58 |
+
input_ids = input_ids.to(model.device)
|
59 |
+
|
60 |
+
with torch.no_grad():
|
61 |
+
outputs = model.generate(
|
62 |
+
input_ids=input_ids,
|
63 |
+
generation_config=generation_config,
|
64 |
+
return_dict_in_generate=True,
|
65 |
+
output_scores=True,
|
66 |
+
)
|
67 |
+
|
68 |
+
response = tokenizer.decode(outputs.sequences[0], skip_special_tokens=True)
|
69 |
+
|
70 |
+
print(response.split("### Response:")[1].strip().split("### Instruction")[0])
|
71 |
+
```
|