Views
No views yet
!pip install -q -U huggingface_hub transformers torch accelerate1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig1model = AutoModelForCausalLM.from_pretrained(
2 "MBZUAI-LLM/LLaMA2-7B-GLoRA-ShareGPT",
3 use_auth_token=True,
4 torch_dtype=torch.bfloat16,
5 device_map="auto",
6)
7tokenizer = AutoTokenizer.from_pretrained("MBZUAI-LLM/LLaMA2-7B-GLoRA-ShareGPT")1def llama_generate(
2 model: AutoModelForCausalLM,
3 tokenizer: AutoTokenizer,
4 prompt: str,
5 max_new_tokens: int = 128,
6 temperature: float = 0.92,
7) -> str:
8 """
9 Initialize the pipeline
10 Uses Hugging Face GenerationConfig defaults
11 https://huggingface.co/docs/transformers/v4.29.1/en/main_classes/text_generation#transformers.GenerationConfig
12 Args:
13 model (transformers.AutoModelForCausalLM): Model for text generation
14 tokenizer (transformers.AutoTokenizer): Tokenizer for model
15 prompt (str): Prompt for text generation
16 max_new_tokens (int, optional): Max new tokens after the prompt to generate. Defaults to 128.
17 temperature (float, optional): The value used to modulate the next token probabilities.
18 Defaults to 1.0
19 """
20 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
21 inputs = tokenizer(
22 [prompt],
23 return_tensors="pt",
24 return_token_type_ids=False,
25 ).to(
26 device
27 ) # tokenize inputs, load on device
28 # when running Torch modules in lower precision, it is best practice to use the torch.autocast context manager.
29 with torch.autocast("cuda", dtype=torch.bfloat16):
30 response = model.generate(
31 **inputs,
32 max_new_tokens=max_new_tokens,
33 temperature=temperature,
34 return_dict_in_generate=True,
35 eos_token_id=tokenizer.eos_token_id,
36 pad_token_id=tokenizer.pad_token_id,
37 )
38 decoded_output = tokenizer.decode(
39 response["sequences"][0],
40 skip_special_tokens=True,
41 ) # grab output in natural language
42 return decoded_output[len(prompt) :] # remove prompt from output1prompt = "You are a helpful assistant. Tell me a recipe for vegan banana bread.\n"
2response = llama_generate(
3 model,
4 tokenizer,
5 prompt,
6 max_new_tokens=500,
7 temperature=0.92,
8)
9print(response)@misc{chavan2023oneforall,
title={One-for-All: Generalized LoRA for Parameter-Efficient Fine-tuning},
author={Arnav Chavan and Zhuang Liu and Deepak Gupta and Eric Xing and Zhiqiang Shen},
year={2023},
eprint={2306.07967},
archivePrefix={arXiv},
primaryClass={cs.LG}
}