Views
No views yet

pip install loralib bitsandbytes datasets git+https://github.com/huggingface/peft.git git+https://github.com/huggingface/transformers.git sentencepiece1prompt_template = {
2 "prompt": "### Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response:\n",
3 "response": "### Response:"
4}
5
6def generate_prompt(
7 definition: str,
8 inputs: str,
9 targets: Union[None, str] = None,
10) -> str:
11 """Generate a prompt from instruction and input."""
12 res = prompt_template["prompt"].format(
13 instruction=definition, input=inputs
14 )
15
16 if targets:
17 res = f"{res}{targets}"
18
19 return res
20
21def get_response(output: str) -> str:
22 """Get the response from the output."""
23 return output.split(prompt_template["response"])[1].strip()definition is the instruction describing the task. It's generally a single sentence explaining the expected output and
the reasoning steps to follow.inputs is the input to the task. It can be a single sentence or a paragraph. It's the context used by the model to
generate the response to the task.targets is the expected output of the task. It's used for training the model. It's not required for inference.1from transformers import LlamaTokenizer
2
3tokenizer = LlamaTokenizer.from_pretrained("wordcab/llama-natural-instructions-13b")
4tokenizer.padding_side = "left"
5tokenizer.pad_token_id = (0)1from peft import PeftModel
2from transformers import LlamaForCausalLM
3
4model = LlamaForCausalLM.from_pretrained(
5 "decapoda-research/llama-13b-hf",
6 load_in_8bit=True,
7 torch_dtype=torch.float16,
8 device_map="auto",
9)
10model = PeftModel.from_pretrained(
11 model,
12 "wordcab/llama-natural-instructions-13b",
13 torch_dtype=torch.float16,
14 device_map={"": 0},
15)1model = LlamaForCausalLM.from_pretrained(
2 "wordcab/llama-natural-instructions-13b",
3 load_in_8bit=True,
4 torch_dtype=torch.float16,
5 device_map="auto",
6)1model.eval()
2if torch.__version__ >= "2":
3 model = torch.compile(model)1prompt = generate_prompt(
2 "In this task, you have to analyze the full sentences and do reasoning and quick maths to find the correct answer.",
3 f"You are now a superbowl star. You are the quarterback of the team. Your team is down by 3 points. You are in the last 2 minutes of the game. The other team has a score of 28. What is the score of your team?",
4)
5inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=2048)
6input_ids = inputs["input_ids"].to(model.device)
7
8generation_config = GenerationConfig(
9 temperature=0.2,
10 top_p=0.75,
11 top_k=40,
12 num_beams=4,
13)
14
15with torch.no_grad():
16 gen_outputs = model.generate(
17 input_ids=input_ids,
18 generation_config=generation_config,
19 return_dict_in_generate=True,
20 output_scores=True,
21 max_new_tokens=50,
22 )
23
24s = gen_outputs.sequences[0]
25output = tokenizer.decode(s, skip_special_tokens=True)
26response = prompter.get_response(output)
27print(response)
28>>> 25| BoolQ | PIQA | WinoGrande | OpenBookQA | Precision | Inference time (s) | |
|---|---|---|---|---|---|---|
| Original LLaMA 7B | 76.5 | 79.8 | 70.1 | 57.2 | fp32 | 3 seconds |
| Original LLaMA 13B | 78.1 | 80.1 | 73 | 56.4 | fp32 | >5 seconds |
| LoRA LLaMA 7B | 63.9 | 51.3 | 48.9 | 31.4 | 8bit | 0.65 seconds |
| LoRA LLaMA 13B | 70 | 63.93 | 51.6 | 50.4 | 8bit | 1.2 seconds |