Sandiago21 commited on
Commit
2f7732f
·
1 Parent(s): c709ea2

Update README.md with improved way to load and use the model

Browse files
Files changed (1) hide show
  1. README.md +35 -34
README.md CHANGED
@@ -98,49 +98,50 @@ def generate_prompt(instruction: str, input_ctxt: str = None) -> str:
98
  Use the code below to get started with the model.
99
 
100
  ```python
101
- from transformers import LlamaTokenizer, LlamaForCausalLM
102
- from peft import PeftModel
103
-
104
- MODEL_NAME = "decapoda-research/llama-7b-hf"
105
- tokenizer = LlamaTokenizer.from_pretrained(MODEL_NAME, add_eos_token=True)
106
- tokenizer.pad_token_id = 0
 
 
 
 
 
 
 
 
 
 
 
107
 
108
- model = LlamaForCausalLM.from_pretrained(MODEL_NAME, load_in_8bit=True, device_map="auto")
109
- model = PeftModel.from_pretrained(model, "Sandiago21/llama-7b-hf")
 
110
  ```
111
 
112
  ### Example of Usage
113
  ```python
114
- from transformers import GenerationConfig
115
-
116
- PROMPT = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\nWhich is the capital city of Greece and with which countries does Greece border?\n\n### Input:\nQuestion answering\n\n### Response:\n"""
117
- DEVICE = "cuda"
118
 
119
- inputs = tokenizer(
120
- PROMPT,
121
- return_tensors="pt",
122
- )
123
 
124
- input_ids = inputs["input_ids"].to(DEVICE)
 
 
 
 
 
 
125
 
126
- generation_config = GenerationConfig(
127
- temperature=0.1,
128
- top_p=0.95,
129
- repetition_penalty=1.2,
130
- )
131
 
132
- print("Generating Response ... ")
133
- with torch.no_grad():
134
- generation_output = model.generate(
135
- input_ids=input_ids,
136
- generation_config=generation_config,
137
- return_dict_in_generate=True,
138
- output_scores=True,
139
- max_new_tokens=256,
140
- )
141
-
142
- for s in generation_output.sequences:
143
- print(tokenizer.decode(s))
144
  ```
145
 
146
  ### Example Output
 
98
  Use the code below to get started with the model.
99
 
100
  ```python
101
+ import torch
102
+ from transformers import GenerationConfig, LlamaTokenizer, LlamaForCausalLM
103
+
104
+ tokenizer = LlamaTokenizer.from_pretrained("chainyo/alpaca-lora-7b")
105
+ model = LlamaForCausalLM.from_pretrained(
106
+ "chainyo/alpaca-lora-7b",
107
+ load_in_8bit=True,
108
+ torch_dtype=torch.float16,
109
+ device_map="auto",
110
+ )
111
+ generation_config = GenerationConfig(
112
+ temperature=0.2,
113
+ top_p=0.75,
114
+ top_k=40,
115
+ num_beams=4,
116
+ max_new_tokens=128,
117
+ )
118
 
119
+ model.eval()
120
+ if torch.__version__ >= "2":
121
+ model = torch.compile(model)
122
  ```
123
 
124
  ### Example of Usage
125
  ```python
126
+ instruction = "What is the capital city of Greece and with which countries does Greece border?"
127
+ input_ctxt = None # For some tasks, you can provide an input context to help the model generate a better response.
 
 
128
 
129
+ prompt = generate_prompt(instruction, input_ctxt)
130
+ input_ids = tokenizer(prompt, return_tensors="pt").input_ids
131
+ input_ids = input_ids.to(model.device)
 
132
 
133
+ with torch.no_grad():
134
+ outputs = model.generate(
135
+ input_ids=input_ids,
136
+ generation_config=generation_config,
137
+ return_dict_in_generate=True,
138
+ output_scores=True,
139
+ )
140
 
141
+ response = tokenizer.decode(outputs.sequences[0], skip_special_tokens=True)
142
+ print(response)
 
 
 
143
 
144
+ >>> The capital city of Greece is Athens and it borders Albania, Macedonia, Bulgaria and Turkey.
 
 
 
 
 
 
 
 
 
 
 
145
  ```
146
 
147
  ### Example Output