PEFT
Safetensors
French
eltorio commited on
Commit
83b9995
·
verified ·
1 Parent(s): d797920

Add sample notebook

Browse files
Files changed (1) hide show
  1. InferenceTest.ipynb +270 -0
InferenceTest.ipynb ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "ea62ee81-8904-492e-a840-3664cf27e8fb",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Autoeval inference testing"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": 1,
14
+ "id": "fa3d9de4-4e59-468f-92f0-b5f2ec55858d",
15
+ "metadata": {},
16
+ "outputs": [
17
+ {
18
+ "name": "stderr",
19
+ "output_type": "stream",
20
+ "text": [
21
+ "2024-11-27 10:33:08.171106: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n",
22
+ "WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n",
23
+ "E0000 00:00:1732703588.241296 2164 cuda_dnn.cc:8310] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n",
24
+ "E0000 00:00:1732703588.262674 2164 cuda_blas.cc:1418] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n",
25
+ "2024-11-27 10:33:08.443688: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n",
26
+ "To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n"
27
+ ]
28
+ }
29
+ ],
30
+ "source": [
31
+ "from transformers import AutoProcessor, AutoTokenizer, AutoModelForCausalLM\n",
32
+ "import torch\n",
33
+ "import os\n",
34
+ "\n",
35
+ "if os.environ.get('HF_TOKEN') is None:\n",
36
+ " raise ValueError(\"You must set the HF_TOKEN environment variable to use this script, you also need to have access to the Llama 3.2 model family\")\n",
37
+ "\n",
38
+ "hugging_face_model_id = \"eltorio/Llama-3.2-3B-appreciation\"\n",
39
+ "base_model_path = \"meta-llama/Llama-3.2-3B-Instruct\"\n",
40
+ "device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n"
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "markdown",
45
+ "id": "a7696fc5-7c8e-4c3c-a5e5-8b88dcdaa2de",
46
+ "metadata": {},
47
+ "source": [
48
+ "## Load the model"
49
+ ]
50
+ },
51
+ {
52
+ "cell_type": "code",
53
+ "execution_count": 2,
54
+ "id": "a0668894-d42e-4e56-8448-4b83af04b213",
55
+ "metadata": {},
56
+ "outputs": [
57
+ {
58
+ "data": {
59
+ "application/vnd.jupyter.widget-view+json": {
60
+ "model_id": "1c7303ddd88143a99b18d04f0def5efc",
61
+ "version_major": 2,
62
+ "version_minor": 0
63
+ },
64
+ "text/plain": [
65
+ "Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
66
+ ]
67
+ },
68
+ "metadata": {},
69
+ "output_type": "display_data"
70
+ }
71
+ ],
72
+ "source": [
73
+ "processor = AutoProcessor.from_pretrained(\n",
74
+ " base_model_path,\n",
75
+ " do_image_splitting=False\n",
76
+ ")\n",
77
+ "\n",
78
+ "model = AutoModelForCausalLM.from_pretrained(\n",
79
+ " base_model_path,\n",
80
+ " torch_dtype=torch.float16,\n",
81
+ " low_cpu_mem_usage=True,\n",
82
+ ").to(device)\n",
83
+ "model.load_adapter(hugging_face_model_id)\n",
84
+ "tokenizer = AutoTokenizer.from_pretrained(hugging_face_model_id)"
85
+ ]
86
+ },
87
+ {
88
+ "cell_type": "markdown",
89
+ "id": "75ed038d-649d-4c91-8804-ad9bbe3c5963",
90
+ "metadata": {},
91
+ "source": [
92
+ "## Define a function for getting a multiturn conversation"
93
+ ]
94
+ },
95
+ {
96
+ "cell_type": "code",
97
+ "execution_count": null,
98
+ "id": "07cb81ed-7190-405a-8aea-be139cf24bc9",
99
+ "metadata": {},
100
+ "outputs": [],
101
+ "source": [
102
+ "# Define a function to infer a evaluation from the incoming parameters\n",
103
+ "def infere(trimestre: str, moyenne_1: float,moyenne_2: float,moyenne_3: float, comportement: float, participation: float, travail: float) -> str:\n",
104
+ "\n",
105
+ " if trimestre == \"1\":\n",
106
+ " trimestre_full = \"premier trimestre\"\n",
107
+ " user_question = f\"Veuillez rédiger une appréciation en moins de 40 mots pour le {trimestre_full} pour cet élève qui a eu {moyenne_1} de moyenne, j'ai évalué son comportement à {comportement}/10, sa participation à {participation}/10 et son travail à {travail}/10. Les notes ne doivent pas apparaître dans l'appréciation.\"\n",
108
+ " elif trimestre == \"2\":\n",
109
+ " trimestre_full = \"deuxième trimestre\"\n",
110
+ " user_question = f\"Veuillez rédiger une appréciation en moins de 40 mots pour le {trimestre_full} pour cet élève qui a eu {moyenne_2} de moyenne ce trimestre et {moyenne_1} au premier trimestre, j'ai évalué son comportement à {comportement}/10, sa participation à {participation}/10 et son travail à {travail}/10. Les notes ne doivent pas apparaître dans l'appréciation.\"\n",
111
+ " elif trimestre == \"3\":\n",
112
+ " trimestre_full = \"troisième trimestre\"\n",
113
+ " user_question= f\"Veuillez rédiger une appréciation en moins de 40 mots pour le {trimestre_full} pour cet élève qui a eu {moyenne_3} de moyenne ce trimestre, {moyenne_2} au deuxième trimestre et {moyenne_1} au premier trimestre, j'ai évalué son comportement à {comportement}/10, sa participation à {participation}/10 et son travail à {travail}/10. Les notes ne doivent pas apparaître dans l'appréciation.\"\n",
114
+ " messages = [\n",
115
+ " {\n",
116
+ " \"role\": \"system\",\n",
117
+ " \"content\": \"Vous êtes une IA assistant les enseignants d'histoire-géographie en rédigeant à leur place une appréciation personnalisée pour leur élève en fonction de ses performances. Votre appreciation doit être en français, et doit aider l'élève à comprendre ses points forts et les axes d'amélioration. Votre appréciation doit comporter de 1 à 40 mots. Votre appréciation ne doit jamais comporter la valeur de la note. Votre appréciation doit utiliser le style impersonnel.Attention l'élément le plus important de votre analyse doit rester la moyenne du trimestre\"},\n",
118
+ " {\n",
119
+ " \"role\": \"user\",\n",
120
+ " \"content\": user_question},\n",
121
+ " ]\n",
122
+ " return messages"
123
+ ]
124
+ },
125
+ {
126
+ "cell_type": "code",
127
+ "execution_count": 4,
128
+ "id": "0874967f-ddcc-4b3f-9625-e934cff38d44",
129
+ "metadata": {},
130
+ "outputs": [],
131
+ "source": [
132
+ "messages = infere(\"1\", 3, float('nan'), float('nan'), 10, 10, 10)"
133
+ ]
134
+ },
135
+ {
136
+ "cell_type": "markdown",
137
+ "id": "2686f92a-2de6-420e-a36a-3581f4df3ed8",
138
+ "metadata": {},
139
+ "source": [
140
+ "## Tokenize the input"
141
+ ]
142
+ },
143
+ {
144
+ "cell_type": "code",
145
+ "execution_count": 5,
146
+ "id": "b58c8308-f5df-48d1-b872-2b539f1eef19",
147
+ "metadata": {},
148
+ "outputs": [
149
+ {
150
+ "data": {
151
+ "text/plain": [
152
+ "tensor([[128000, 128006, 9125, 128007, 271, 38766, 1303, 33025, 2696,\n",
153
+ " 25, 6790, 220, 2366, 18, 198, 15724, 2696, 25,\n",
154
+ " 220, 1627, 5887, 220, 2366, 19, 271, 43273, 62299,\n",
155
+ " 6316, 44190, 18328, 3625, 68061, 625, 1821, 294, 6,\n",
156
+ " 90446, 2427, 978, 3257, 648, 665, 9517, 67, 7404,\n",
157
+ " 519, 3869, 28130, 2035, 6316, 917, 43711, 5979, 367,\n",
158
+ " 97252, 35965, 8047, 5019, 28130, 33013, 79351, 665, 34501,\n",
159
+ " 409, 15907, 24601, 13, 650, 52262, 35996, 42182, 23761,\n",
160
+ " 665, 55467, 11, 1880, 42182, 91878, 326, 6, 19010,\n",
161
+ " 79351, 3869, 60946, 265, 15907, 3585, 75652, 1880, 3625,\n",
162
+ " 25776, 294, 58591, 73511, 7769, 13, 650, 52262, 917,\n",
163
+ " 43711, 5979, 367, 42182, 52962, 261, 409, 220, 16,\n",
164
+ " 3869, 220, 1272, 78199, 13, 650, 52262, 917, 43711,\n",
165
+ " 5979, 367, 841, 42182, 56316, 52962, 261, 1208, 51304,\n",
166
+ " 409, 1208, 5296, 13, 650, 52262, 917, 43711, 5979,\n",
167
+ " 367, 42182, 75144, 514, 1742, 60849, 8301, 47472, 3012,\n",
168
+ " 326, 6, 29982, 479, 514, 5636, 3062, 409, 15265,\n",
169
+ " 49586, 42182, 2800, 261, 1208, 52138, 26193, 3930, 75110,\n",
170
+ " 265, 128009, 128006, 882, 128007, 271, 53, 89025, 9517,\n",
171
+ " 67, 7420, 6316, 917, 43711, 5979, 367, 665, 40970,\n",
172
+ " 409, 220, 1272, 78199, 5019, 514, 21134, 75110, 265,\n",
173
+ " 5019, 42067, 33013, 79351, 7930, 264, 15925, 220, 18,\n",
174
+ " 409, 52138, 26193, 11, 503, 34155, 4046, 26591, 978,\n",
175
+ " 4538, 52962, 1133, 3869, 220, 605, 14, 605, 11,\n",
176
+ " 829, 20852, 3869, 220, 605, 14, 605, 1880, 4538,\n",
177
+ " 42775, 3869, 220, 605, 14, 605, 13, 11876, 8554,\n",
178
+ " 841, 97569, 6502, 917, 5169, 66014, 7010, 326, 6,\n",
179
+ " 391, 652, 978, 5979, 367, 13, 128009, 128006, 78191,\n",
180
+ " 128007, 271]], device='cuda:0')"
181
+ ]
182
+ },
183
+ "execution_count": 5,
184
+ "metadata": {},
185
+ "output_type": "execute_result"
186
+ }
187
+ ],
188
+ "source": [
189
+ "inputs = tokenizer.apply_chat_template(\n",
190
+ " messages,\n",
191
+ " tokenize = True,\n",
192
+ " add_generation_prompt = True,\n",
193
+ " return_tensors = \"pt\",).to(device)\n",
194
+ "inputs"
195
+ ]
196
+ },
197
+ {
198
+ "cell_type": "markdown",
199
+ "id": "49d35c86-4d5d-4aaa-8e49-6b6e7386d4d6",
200
+ "metadata": {},
201
+ "source": [
202
+ "## Generate the output"
203
+ ]
204
+ },
205
+ {
206
+ "cell_type": "code",
207
+ "execution_count": 6,
208
+ "id": "94723775-3774-4e5f-b9bb-53b6f16fb432",
209
+ "metadata": {},
210
+ "outputs": [
211
+ {
212
+ "name": "stderr",
213
+ "output_type": "stream",
214
+ "text": [
215
+ "The attention mask is not set and cannot be inferred from input because pad token is same as eos token. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.\n"
216
+ ]
217
+ },
218
+ {
219
+ "data": {
220
+ "text/plain": [
221
+ "\"Quel changement (positif) par rapport à l'an passé! X travaille plus sérieusement, il fait davantage d'effort, il participe. Son redoublement lui permettra d'avoir une deuxième année plus confortable. Continuer ainsi devrait payer au final.\""
222
+ ]
223
+ },
224
+ "execution_count": 6,
225
+ "metadata": {},
226
+ "output_type": "execute_result"
227
+ }
228
+ ],
229
+ "source": [
230
+ "outputs = model.generate(input_ids = inputs, \n",
231
+ " max_new_tokens = 90, \n",
232
+ " use_cache = True,\n",
233
+ " temperature = 1.5,\n",
234
+ " min_p = 0.1,\n",
235
+ " pad_token_id=tokenizer.eos_token_id,)\n",
236
+ "decoded_sequences = tokenizer.batch_decode(outputs[:, inputs.shape[1]:],skip_special_tokens=True)[0]\n",
237
+ "decoded_sequences"
238
+ ]
239
+ },
240
+ {
241
+ "cell_type": "code",
242
+ "execution_count": null,
243
+ "id": "5d1ba3fb-2e62-4486-9c45-3567d4d3a6f0",
244
+ "metadata": {},
245
+ "outputs": [],
246
+ "source": []
247
+ }
248
+ ],
249
+ "metadata": {
250
+ "kernelspec": {
251
+ "display_name": "Python 3 (ipykernel)",
252
+ "language": "python",
253
+ "name": "python3"
254
+ },
255
+ "language_info": {
256
+ "codemirror_mode": {
257
+ "name": "ipython",
258
+ "version": 3
259
+ },
260
+ "file_extension": ".py",
261
+ "mimetype": "text/x-python",
262
+ "name": "python",
263
+ "nbconvert_exporter": "python",
264
+ "pygments_lexer": "ipython3",
265
+ "version": "3.12.7"
266
+ }
267
+ },
268
+ "nbformat": 4,
269
+ "nbformat_minor": 5
270
+ }