Spaces:
Running
Running
abhishekkankati9
commited on
Commit
•
1ee19b7
1
Parent(s):
4671487
Upload 2 files
Browse files- Instructions Llama 2 7B.docx +0 -0
- Llama 2 Windows GPU setup.ipynb +203 -0
Instructions Llama 2 7B.docx
ADDED
Binary file (974 kB). View file
|
|
Llama 2 Windows GPU setup.ipynb
ADDED
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"id": "36990086",
|
6 |
+
"metadata": {},
|
7 |
+
"source": [
|
8 |
+
"## This is to set your working path. You can work on your default path but ideally it is always good to have a separate folder and virtual environment for each project."
|
9 |
+
]
|
10 |
+
},
|
11 |
+
{
|
12 |
+
"cell_type": "code",
|
13 |
+
"execution_count": 1,
|
14 |
+
"id": "e5f26a4e",
|
15 |
+
"metadata": {},
|
16 |
+
"outputs": [],
|
17 |
+
"source": [
|
18 |
+
"import os\n",
|
19 |
+
"os.chdir(r\"C:\\Users\\abhis\\Documents\\Llama 2\")"
|
20 |
+
]
|
21 |
+
},
|
22 |
+
{
|
23 |
+
"cell_type": "markdown",
|
24 |
+
"id": "56865eff",
|
25 |
+
"metadata": {},
|
26 |
+
"source": [
|
27 |
+
"## Ensure to have requirements.txt file in the above path and execute the below command. Please use Ctrl+Enter to execute each cell"
|
28 |
+
]
|
29 |
+
},
|
30 |
+
{
|
31 |
+
"cell_type": "code",
|
32 |
+
"execution_count": null,
|
33 |
+
"id": "9cd46858",
|
34 |
+
"metadata": {},
|
35 |
+
"outputs": [],
|
36 |
+
"source": [
|
37 |
+
"pip install -r requirements.txt"
|
38 |
+
]
|
39 |
+
},
|
40 |
+
{
|
41 |
+
"cell_type": "markdown",
|
42 |
+
"id": "176ce467",
|
43 |
+
"metadata": {},
|
44 |
+
"source": [
|
45 |
+
"## Importing the necessary libraries"
|
46 |
+
]
|
47 |
+
},
|
48 |
+
{
|
49 |
+
"cell_type": "code",
|
50 |
+
"execution_count": 2,
|
51 |
+
"id": "cc99e10b",
|
52 |
+
"metadata": {},
|
53 |
+
"outputs": [],
|
54 |
+
"source": [
|
55 |
+
"from langchain import HuggingFacePipeline \n",
|
56 |
+
"from langchain import PromptTemplate, LLMChain\n",
|
57 |
+
"from datetime import datetime\n",
|
58 |
+
"from transformers import pipeline\n",
|
59 |
+
"import os\n",
|
60 |
+
"import torch\n",
|
61 |
+
"import transformers\n",
|
62 |
+
"from transformers import AutoTokenizer, AutoModelForCausalLM \n",
|
63 |
+
"from transformers import LlamaForCausalLM, LlamaTokenizer\n",
|
64 |
+
"from accelerate import init_empty_weights\n",
|
65 |
+
"from accelerate import infer_auto_device_map, init_empty_weights"
|
66 |
+
]
|
67 |
+
},
|
68 |
+
{
|
69 |
+
"cell_type": "code",
|
70 |
+
"execution_count": 3,
|
71 |
+
"id": "2b98fe56",
|
72 |
+
"metadata": {},
|
73 |
+
"outputs": [
|
74 |
+
{
|
75 |
+
"name": "stderr",
|
76 |
+
"output_type": "stream",
|
77 |
+
"text": [
|
78 |
+
"You are using the default legacy behaviour of the <class 'transformers.models.llama.tokenization_llama.LlamaTokenizer'>. If you see this, DO NOT PANIC! This is expected, and simply means that the `legacy` (previous) behavior will be used so nothing changes for you. If you want to use the new behaviour, set `legacy=False`. This should only be set if you understand what it means, and thouroughly read the reason why this was added as explained in https://github.com/huggingface/transformers/pull/24565\n"
|
79 |
+
]
|
80 |
+
},
|
81 |
+
{
|
82 |
+
"name": "stdout",
|
83 |
+
"output_type": "stream",
|
84 |
+
"text": [
|
85 |
+
"Code Execution Start18:00:09\n"
|
86 |
+
]
|
87 |
+
},
|
88 |
+
{
|
89 |
+
"name": "stderr",
|
90 |
+
"output_type": "stream",
|
91 |
+
"text": [
|
92 |
+
"C:\\Users\\abhis\\anaconda3\\lib\\site-packages\\transformers\\modeling_utils.py:2363: FutureWarning: The `use_auth_token` argument is deprecated and will be removed in v5 of Transformers.\n",
|
93 |
+
" warnings.warn(\n"
|
94 |
+
]
|
95 |
+
},
|
96 |
+
{
|
97 |
+
"data": {
|
98 |
+
"application/vnd.jupyter.widget-view+json": {
|
99 |
+
"model_id": "424c4a9efc994b80883a62e52ada6888",
|
100 |
+
"version_major": 2,
|
101 |
+
"version_minor": 0
|
102 |
+
},
|
103 |
+
"text/plain": [
|
104 |
+
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
|
105 |
+
]
|
106 |
+
},
|
107 |
+
"metadata": {},
|
108 |
+
"output_type": "display_data"
|
109 |
+
}
|
110 |
+
],
|
111 |
+
"source": [
|
112 |
+
"print(\"Code Execution Start\"+datetime.now().strftime(\"%H:%M:%S\"))\n",
|
113 |
+
"\n",
|
114 |
+
"model_path=\"./Static/Llama\"\n",
|
115 |
+
"\n",
|
116 |
+
"tokenizer = LlamaTokenizer.from_pretrained(model_path) \n",
|
117 |
+
"model= LlamaForCausalLM.from_pretrained (model_path,device_map='auto',torch_dtype=torch.float32,\n",
|
118 |
+
" use_auth_token=True,offload_folder=\"save_folder\",local_files_only=True)\n",
|
119 |
+
"model.tie_weights()\n",
|
120 |
+
"\n",
|
121 |
+
"if torch.backends.mps.is_available(): \n",
|
122 |
+
" mps_device = torch.device(\"mps\")\n",
|
123 |
+
"\n",
|
124 |
+
"#Please ensure these changes for GPU implementations\n",
|
125 |
+
"os.environ[\"SAFETENSORS FAST_GPU\"]=\"1\"\n",
|
126 |
+
"#torch_dtype=torch.bfloat16 is for GPU implementation only. For CPU, we have to make it 32\n",
|
127 |
+
"pipe = pipeline(\"text-generation\",model=model,tokenizer=tokenizer,torch_dtype=torch.float32,device_map=\"auto\",\n",
|
128 |
+
" max_new_tokens = 40,do_sample=True,top_k=30,num_return_sequences=40,eos_token_id=tokenizer.eos_token_id)"
|
129 |
+
]
|
130 |
+
},
|
131 |
+
{
|
132 |
+
"cell_type": "code",
|
133 |
+
"execution_count": 5,
|
134 |
+
"id": "5f6222ca",
|
135 |
+
"metadata": {},
|
136 |
+
"outputs": [
|
137 |
+
{
|
138 |
+
"name": "stdout",
|
139 |
+
"output_type": "stream",
|
140 |
+
"text": [
|
141 |
+
"[INST]<<SYS>>\n",
|
142 |
+
"You are an advanced assistant that excels at translation that answers query in one word. \n",
|
143 |
+
"<</SYS>>\n",
|
144 |
+
"\n",
|
145 |
+
"Translate the following word from English to french. :\n",
|
146 |
+
"\n",
|
147 |
+
" {text}[/INST]\n",
|
148 |
+
"Inferencing Started18:09:02\n",
|
149 |
+
"OUTPUT>>> Chien\n",
|
150 |
+
"Inferencing Completed18:34:40\n"
|
151 |
+
]
|
152 |
+
}
|
153 |
+
],
|
154 |
+
"source": [
|
155 |
+
"B_INST, E_INST = \"[INST]\", \"[/INST]\"\n",
|
156 |
+
"B_SYS, E_SYS = \"<<SYS>>\\n\", \"\\n<</SYS>>\\n\\n\" \n",
|
157 |
+
"DEFAULT_SYSTEM_PROMPT = \"\"\"\\\n",
|
158 |
+
"You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe.\n",
|
159 |
+
"If a question does not make any sense, or is not factually coherent, explain why instead of answering something\"\"\"\n",
|
160 |
+
"\n",
|
161 |
+
"def get_prompt(instruction, new_system_prompt=DEFAULT_SYSTEM_PROMPT):\n",
|
162 |
+
" SYSTEM_PROMPT = B_SYS + new_system_prompt + E_SYS\n",
|
163 |
+
" prompt_template = B_INST + SYSTEM_PROMPT + instruction + E_INST\n",
|
164 |
+
" return prompt_template\n",
|
165 |
+
"\n",
|
166 |
+
"\n",
|
167 |
+
"llm = HuggingFacePipeline(pipeline = pipe, model_kwargs = {'temperature': 0})\n",
|
168 |
+
"system_prompt = \"You are an advanced assistant that excels at translation that answers query in one word. \" \n",
|
169 |
+
"instruction = \"Translate the following word from English to french. :\\n\\n {text}\" \n",
|
170 |
+
"template = get_prompt(instruction, system_prompt)\n",
|
171 |
+
"print(template)\n",
|
172 |
+
"prompt = PromptTemplate(template=template, input_variables=[\"text\"])\n",
|
173 |
+
"llm_chain = LLMChain(prompt=prompt, llm=llm)\n",
|
174 |
+
"text = \"Dog\"\n",
|
175 |
+
"print(\"Inferencing Started \"+datetime.now().strftime(\"%H:%M:%S\")) \n",
|
176 |
+
"output = llm_chain.run(text)\n",
|
177 |
+
"print(\"OUTPUT>>>\"+ output)\n",
|
178 |
+
"print(\"Inferencing Completed \" +datetime.now().strftime(\"%H:%M:%S\"))"
|
179 |
+
]
|
180 |
+
}
|
181 |
+
],
|
182 |
+
"metadata": {
|
183 |
+
"kernelspec": {
|
184 |
+
"display_name": "Python 3 (ipykernel)",
|
185 |
+
"language": "python",
|
186 |
+
"name": "python3"
|
187 |
+
},
|
188 |
+
"language_info": {
|
189 |
+
"codemirror_mode": {
|
190 |
+
"name": "ipython",
|
191 |
+
"version": 3
|
192 |
+
},
|
193 |
+
"file_extension": ".py",
|
194 |
+
"mimetype": "text/x-python",
|
195 |
+
"name": "python",
|
196 |
+
"nbconvert_exporter": "python",
|
197 |
+
"pygments_lexer": "ipython3",
|
198 |
+
"version": "3.9.13"
|
199 |
+
}
|
200 |
+
},
|
201 |
+
"nbformat": 4,
|
202 |
+
"nbformat_minor": 5
|
203 |
+
}
|