Views
No views yet
1# %pip install -qq transformers git+https://github.com/huggingface/peft accelerate bitsandbytes
2from peft import PeftModel, PeftConfig
3from transformers import AutoModelForCausalLM, AutoTokenizer
4peft_model_id = "crumb/FLAN-OPT-6.7b-LoRA"
5
6config = PeftConfig.from_pretrained(peft_model_id)
7model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, load_in_8bit=True, low_cpu_mem_usage=True, device_map='auto')
8model = PeftModel.from_pretrained(model, peft_model_id)
9tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
10
11import torch
12prompt = """
13Q: Answer the following yes/no question by reasoning step-by-step. Could a dandelion suffer from hepatitis?
14A: Hepatitis only affects organisms with livers. Dandelions don’t have a liver. The answer is no.
15
16Q: Answer the following yes/no question by reasoning step-by-step. Can you write a whole Haiku in a single tweet?
17A: A haiku is a japanese three-line poem. That is short enough to fit in 280 characters. The answer is yes.
18
19Q: Answer the following yes/no question by reasoning step-by-step. Can you reach space with a Cessna?
20A:
21""".strip()
22inputs = tokenizer([prompt], return_tensors='pt')
23
24with torch.autocast("cuda", dtype=torch.float16):
25 outputs = model.generate(
26 input_ids=inputs.input_ids.cuda(),
27 attention_mask=inputs.attention_mask.cuda(),
28 max_new_tokens=32,
29 top_p=0.95,
30 temperature=0.5,
31 do_sample=True
32 )
33print("\n".join(tokenizer.decode(outputs[0]).split("\n")[:prompt.count("\n")+1]))
34# A Cessna is a small plane. A small plane can't get into space. The answer is no.