Views
No views yet
1import torch
2import transformers
3from transformers import LlamaTokenizer, LlamaForCausalLM
4tokenizer = LlamaTokenizer.from_pretrained("Fan21/Llama-mt-lora")
5mdoel = LlamaForCausalLM.from_pretrained(
6 "Fan21/Llama-mt-lora",
7 load_in_8bit=False,
8 torch_dtype=torch.float16,
9 device_map="auto",
10 )
11def generate_prompt(instruction, input=None):
12 if input:
13 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.
14### Instruction:
15{instruction}
16### Input:
17{input}
18### Response:"""
19 else:
20 return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
21### Instruction:
22{instruction}
23### Response:"""
24
25def evaluate(
26 instruction,
27 input=None,
28 temperature=0.1,
29 top_p=0.75,
30 top_k=40,
31 num_beams=4,
32 max_new_tokens=128,
33 **kwargs,
34):
35 prompt = generate_prompt(instruction, input)
36 inputs = tokenizer(prompt, return_tensors="pt")
37 input_ids = inputs["input_ids"].to(device)
38 generation_config = GenerationConfig(
39 temperature=temperature,
40 top_p=top_p,
41 top_k=top_k,
42 num_beams=num_beams,
43 **kwargs,
44 )
45 with torch.no_grad():
46 generation_output = model.generate(
47 input_ids=input_ids,
48 generation_config=generation_config,
49 return_dict_in_generate=True,
50 output_scores=True,
51 max_new_tokens=max_new_tokens,
52 )
53 s = generation_output.sequences[0]
54 output = tokenizer.decode(s)
55 return output.split("### Response:")[1].strip()
56instruction = 'write your instruction here'
57inputs = 'write your inputs here'
58output= evaluate(instruction,
59 input=inputs,
60 temperature=0.1,#change the parameters by yourself
61 top_p=0.75,
62 top_k=40,
63 num_beams=4,
64 max_new_tokens=128,)