hanzceo commited on
Commit
ea7c1c0
·
verified ·
1 Parent(s): 2049257

Upload train.ipynb

Browse files
Files changed (1) hide show
  1. train.ipynb +370 -0
train.ipynb ADDED
@@ -0,0 +1,370 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "id": "d2eea17d-499a-418a-b19c-541ed2af2166",
7
+ "metadata": {},
8
+ "outputs": [
9
+ {
10
+ "name": "stdout",
11
+ "output_type": "stream",
12
+ "text": [
13
+ "Python 3.13 is already installed\n",
14
+ "Pinned `\u001b[36m/.python-version\u001b[39m` to `\u001b[32m3.13\u001b[39m`\n"
15
+ ]
16
+ }
17
+ ],
18
+ "source": [
19
+ "!uv python install 313\n",
20
+ "!uv python pin 313"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 3,
26
+ "id": "fc531160-8579-4899-87ca-040eade771f5",
27
+ "metadata": {},
28
+ "outputs": [
29
+ {
30
+ "name": "stdout",
31
+ "output_type": "stream",
32
+ "text": [
33
+ "Using CPython \u001b[36m3.13.11\u001b[39m\u001b[36m\u001b[39m\n",
34
+ "Creating virtual environment at: \u001b[36m.venv\u001b[39m\n",
35
+ "Activate with: \u001b[32msource .venv/bin/activate\u001b[39m\n"
36
+ ]
37
+ }
38
+ ],
39
+ "source": [
40
+ "!uv venv --clear\n",
41
+ "!uv pip install ipykernel\n",
42
+ "!uv run python -m ipykernel install --user --name=uv-kernel --display-name \"Python (uv venv)\""
43
+ ]
44
+ },
45
+ {
46
+ "cell_type": "code",
47
+ "execution_count": 20,
48
+ "id": "0ef9c787-54f1-42f5-b6f9-1a18aa694b68",
49
+ "metadata": {
50
+ "scrolled": true
51
+ },
52
+ "outputs": [
53
+ {
54
+ "name": "stdout",
55
+ "output_type": "stream",
56
+ "text": [
57
+ "\u001b[2K\u001b[2mResolved \u001b[1m80 packages\u001b[0m \u001b[2min 32ms\u001b[0m\u001b[0m \u001b[0m\n",
58
+ "\u001b[2K\u001b[2mInstalled \u001b[1m1 package\u001b[0m \u001b[2min 24ms\u001b[0m\u001b[0m \u001b[0m\n",
59
+ " \u001b[32m+\u001b[39m \u001b[1maccelerate\u001b[0m\u001b[2m==1.14.0\u001b[0m\n"
60
+ ]
61
+ }
62
+ ],
63
+ "source": [
64
+ "!uv pip install sentence-transformers huggingface_hub datasets tokenizers accelerate>=1.1.0"
65
+ ]
66
+ },
67
+ {
68
+ "cell_type": "code",
69
+ "execution_count": 1,
70
+ "id": "b50e7888-4d6b-48f5-a38f-82cd6df53da7",
71
+ "metadata": {},
72
+ "outputs": [
73
+ {
74
+ "name": "stderr",
75
+ "output_type": "stream",
76
+ "text": [
77
+ "/.venv/lib/python3.13/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
78
+ " from .autonotebook import tqdm as notebook_tqdm\n"
79
+ ]
80
+ }
81
+ ],
82
+ "source": [
83
+ "from datasets import load_dataset, DatasetDict\n",
84
+ "from sentence_transformers import SentenceTransformer, SentenceTransformerTrainer, SentenceTransformerTrainingArguments, SentenceTransformerModelCardData\n",
85
+ "from sentence_transformers.sentence_transformer.modules import StaticEmbedding\n",
86
+ "from sentence_transformers.sentence_transformer.losses import MultipleNegativesRankingLoss, MatryoshkaLoss\n",
87
+ "from sentence_transformers.sentence_transformer.training_args import BatchSamplers, MultiDatasetBatchSamplers\n",
88
+ "from tokenizers import Tokenizer\n",
89
+ "from huggingface_hub import notebook_login"
90
+ ]
91
+ },
92
+ {
93
+ "cell_type": "code",
94
+ "execution_count": 2,
95
+ "id": "8f7eaae2-8f37-4f4c-90d5-a35a2d258e55",
96
+ "metadata": {},
97
+ "outputs": [],
98
+ "source": [
99
+ "notebook_login()"
100
+ ]
101
+ },
102
+ {
103
+ "cell_type": "code",
104
+ "execution_count": 3,
105
+ "id": "7cfd6110-7cac-4c3d-be2a-592c24e6fb73",
106
+ "metadata": {},
107
+ "outputs": [],
108
+ "source": [
109
+ "def process__FinModernBERT_pairs(batch):\n",
110
+ "\tanchors = []\n",
111
+ "\tpositives = []\n",
112
+ "\n",
113
+ "\tfor i, score in enumerate(batch[\"score\"]):\n",
114
+ "\t\tif float(score) >= 0.9:\n",
115
+ "\t\t\tanchors.append(batch[\"sentence_a\"][i])\n",
116
+ "\t\t\tpositives.append(batch[\"sentence_b\"][i])\n",
117
+ "\n",
118
+ "\treturn dict(anchor=anchors, positive=positives)\n",
119
+ "FinModernBERT_pairs = load_dataset(\"BatuhanECB/FinModernBERT-pairs-sec-synthetic-v1\", data_dir=\"sts\", split=\"train\", revision=\"refs/convert/parquet\")\n",
120
+ "FinModernBERT_pairs = FinModernBERT_pairs.map(process__FinModernBERT_pairs, batched=True, remove_columns=FinModernBERT_pairs.column_names)\n",
121
+ "\n",
122
+ "def process__Quora_Question_Pairs(batch):\n",
123
+ "\tanchors = []\n",
124
+ "\tpositives = []\n",
125
+ "\n",
126
+ "\tfor i, score in enumerate(batch[\"is_duplicate\"]):\n",
127
+ "\t\tif score:\n",
128
+ "\t\t\tanchors.append(batch[\"question1\"][i])\n",
129
+ "\t\t\tpositives.append(batch[\"question2\"][i])\n",
130
+ "\n",
131
+ "\treturn dict(anchor=anchors, positive=positives)\n",
132
+ "Quora_Question_Pairs = load_dataset(\"Heliosoph/Quora-Question-Pairs\", split=\"train\", revision=\"refs/convert/parquet\")\n",
133
+ "Quora_Question_Pairs = Quora_Question_Pairs.map(process__Quora_Question_Pairs, batched=True, remove_columns=Quora_Question_Pairs.column_names)\n",
134
+ "\n",
135
+ "def process__wildchat_paraphrases(batch):\n",
136
+ "\tanchors = []\n",
137
+ "\tpositives = []\n",
138
+ "\n",
139
+ "\tfor i, prompt in enumerate(batch[\"prompt\"]):\n",
140
+ "\t\tfor para in batch[\"paraphrases\"][i]:\n",
141
+ "\t\t\tanchors.append(prompt)\n",
142
+ "\t\t\tpositives.append(para)\n",
143
+ "\n",
144
+ "\treturn dict(anchor=anchors, positive=positives)\n",
145
+ "wildchat_paraphrases = load_dataset(\"owenkaplinsky/wildchat-paraphrases\", split=\"train\", revision=\"refs/convert/parquet\")\n",
146
+ "wildchat_paraphrases = wildchat_paraphrases.map(process__wildchat_paraphrases, batched=True, remove_columns=wildchat_paraphrases.column_names)\n",
147
+ "\n",
148
+ "def process__ogce1(batch):\n",
149
+ "\tanchors = []\n",
150
+ "\tpositives = []\n",
151
+ "\n",
152
+ "\tfor i, signal in enumerate(batch[\"signal_type\"]):\n",
153
+ "\t\tif signal.startswith(\"gradient_\") or signal in \"word_synonym|definition_example|query_alternate|query_same_concept\":\n",
154
+ "\t\t\tif float(batch[\"label\"][i]) >= 0.85:\n",
155
+ "\t\t\t\tanchors.append(batch[\"text_a\"][i])\n",
156
+ "\t\t\t\tpositives.append(batch[\"text_b\"][i])\n",
157
+ "\n",
158
+ "\treturn dict(anchor=anchors, positives=positives)\n",
159
+ "ogce1 = load_dataset(\"mjbommar/ogbert-v1-contrastive\", split=\"train\", revision=\"refs/convert/parquet\")\n",
160
+ "ogce1 = ogce1.map(process__ogce1, batched=True, remove_columns=ogce1.column_names)\n",
161
+ "\n",
162
+ "def process__ogce13(batch):\n",
163
+ "\tanchors = []\n",
164
+ "\tpositives = []\n",
165
+ "\tnegatives_1 = []\n",
166
+ "\tnegatives_2 = []\n",
167
+ "\n",
168
+ "\tfor i, anchor in enumerate(batch[\"source_sentence\"]):\n",
169
+ "\t\tanchors.append(anchor)\n",
170
+ "\t\tpositives.append(batch[\"near_synonym_sentence\"][i])\n",
171
+ "\t\tnegatives_1.append(batch[\"near_antonym_sentence\"][i])\n",
172
+ "\t\tnegatives_2.append(batch[\"antonym_sentence\"][i])\n",
173
+ "\n",
174
+ "\treturn dict(anchor=anchors, positives=positives, negatives_1=negatives_1, negatives_2=negatives_2)\n",
175
+ "ogce13 = load_dataset(\"mjbommar/opengloss-v1.3-contrastive-examples\", split=\"train\", revision=\"refs/convert/parquet\")\n",
176
+ "ogce13 = ogce13.map(process__ogce13, batched=True, remove_columns=ogce13.column_names)\n",
177
+ "\n",
178
+ "ds_train = DatasetDict({\n",
179
+ "\t\"BatuhanECB/FinModernBERT-pairs-sec-synthetic-v1\": FinModernBERT_pairs,\n",
180
+ "\t\"Heliosoph/Quora-Question-Pairs\": Quora_Question_Pairs,\n",
181
+ "\t\"owenkaplinsky/wildchat-paraphrases\": wildchat_paraphrases,\n",
182
+ " \"mjbommar/ogbert-v1-contrastive\": ogce1,\n",
183
+ "\t\"mjbommar/opengloss-v1.3-contrastive-examples\": ogce13\n",
184
+ "})"
185
+ ]
186
+ },
187
+ {
188
+ "cell_type": "code",
189
+ "execution_count": 4,
190
+ "id": "15a9927e-faa5-426a-b7bb-67b9f6ad1902",
191
+ "metadata": {},
192
+ "outputs": [],
193
+ "source": [
194
+ "tokenizer = Tokenizer.from_pretrained(\"poolside/Laguna-S-2.1\")\n",
195
+ "static_embedding = StaticEmbedding(tokenizer, embedding_dim=1024)\n",
196
+ "\n",
197
+ "PurpleStatic = SentenceTransformer(\n",
198
+ "\tmodules=[static_embedding],\n",
199
+ "\tmodel_card_data=SentenceTransformerModelCardData(\n",
200
+ "\t\tlanguage=\"en\",\n",
201
+ "\t\tlicense=\"wtfpl\",\n",
202
+ "\t\tmodel_name=\"PurpleStatic: Static Embeddings\"\n",
203
+ "\t)\n",
204
+ ")\n",
205
+ "\n",
206
+ "base_loss = MultipleNegativesRankingLoss(PurpleStatic)\n",
207
+ "loss = MatryoshkaLoss(PurpleStatic, base_loss, matryoshka_dims=[1024, 768, 512, 256, 128, 64, 32])"
208
+ ]
209
+ },
210
+ {
211
+ "cell_type": "code",
212
+ "execution_count": null,
213
+ "id": "ec6ecbba-0d42-4189-9067-b5f92b4637c7",
214
+ "metadata": {},
215
+ "outputs": [
216
+ {
217
+ "data": {
218
+ "text/html": [
219
+ "\n",
220
+ " <div>\n",
221
+ " \n",
222
+ " <progress value='21371' max='21400' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
223
+ " [21371/21400 2:03:29 < 00:12, 2.34 it/s, Epoch 19.97/20]\n",
224
+ " </div>\n",
225
+ " <table border=\"1\" class=\"dataframe\">\n",
226
+ " <thead>\n",
227
+ " <tr style=\"text-align: left;\">\n",
228
+ " <th>Step</th>\n",
229
+ " <th>Training Loss</th>\n",
230
+ " </tr>\n",
231
+ " </thead>\n",
232
+ " <tbody>\n",
233
+ " <tr>\n",
234
+ " <td>5000</td>\n",
235
+ " <td>40.791266</td>\n",
236
+ " </tr>\n",
237
+ " <tr>\n",
238
+ " <td>6000</td>\n",
239
+ " <td>40.154305</td>\n",
240
+ " </tr>\n",
241
+ " <tr>\n",
242
+ " <td>7000</td>\n",
243
+ " <td>38.176074</td>\n",
244
+ " </tr>\n",
245
+ " <tr>\n",
246
+ " <td>8000</td>\n",
247
+ " <td>38.800340</td>\n",
248
+ " </tr>\n",
249
+ " <tr>\n",
250
+ " <td>9000</td>\n",
251
+ " <td>38.206828</td>\n",
252
+ " </tr>\n",
253
+ " <tr>\n",
254
+ " <td>10000</td>\n",
255
+ " <td>37.408781</td>\n",
256
+ " </tr>\n",
257
+ " <tr>\n",
258
+ " <td>11000</td>\n",
259
+ " <td>36.580504</td>\n",
260
+ " </tr>\n",
261
+ " <tr>\n",
262
+ " <td>12000</td>\n",
263
+ " <td>37.152621</td>\n",
264
+ " </tr>\n",
265
+ " <tr>\n",
266
+ " <td>13000</td>\n",
267
+ " <td>36.002770</td>\n",
268
+ " </tr>\n",
269
+ " <tr>\n",
270
+ " <td>14000</td>\n",
271
+ " <td>35.222508</td>\n",
272
+ " </tr>\n",
273
+ " <tr>\n",
274
+ " <td>15000</td>\n",
275
+ " <td>36.238773</td>\n",
276
+ " </tr>\n",
277
+ " <tr>\n",
278
+ " <td>16000</td>\n",
279
+ " <td>35.199824</td>\n",
280
+ " </tr>\n",
281
+ " <tr>\n",
282
+ " <td>17000</td>\n",
283
+ " <td>35.261895</td>\n",
284
+ " </tr>\n",
285
+ " <tr>\n",
286
+ " <td>18000</td>\n",
287
+ " <td>35.194262</td>\n",
288
+ " </tr>\n",
289
+ " <tr>\n",
290
+ " <td>19000</td>\n",
291
+ " <td>36.039648</td>\n",
292
+ " </tr>\n",
293
+ " <tr>\n",
294
+ " <td>20000</td>\n",
295
+ " <td>34.257383</td>\n",
296
+ " </tr>\n",
297
+ " <tr>\n",
298
+ " <td>21000</td>\n",
299
+ " <td>35.113922</td>\n",
300
+ " </tr>\n",
301
+ " </tbody>\n",
302
+ "</table><p>"
303
+ ],
304
+ "text/plain": [
305
+ "<IPython.core.display.HTML object>"
306
+ ]
307
+ },
308
+ "metadata": {},
309
+ "output_type": "display_data"
310
+ }
311
+ ],
312
+ "source": [
313
+ "# we are running on RTX 5090\n",
314
+ "args = SentenceTransformerTrainingArguments(\n",
315
+ "\tnum_train_epochs=20,\n",
316
+ "\tper_device_train_batch_size=4096,\n",
317
+ "\tlearning_rate=1e-4,\n",
318
+ "\twarmup_steps=0.1,\n",
319
+ "\tfp16=False,\n",
320
+ "\tbf16=True,\n",
321
+ "\tbatch_sampler=BatchSamplers.NO_DUPLICATES,\n",
322
+ "\tmulti_dataset_batch_sampler=MultiDatasetBatchSamplers.PROPORTIONAL,\n",
323
+ "\n",
324
+ "\tlogging_steps=1000,\n",
325
+ " logging_first_step=True,\n",
326
+ " save_strategy=\"no\"\n",
327
+ ")\n",
328
+ "\n",
329
+ "trainer = SentenceTransformerTrainer(\n",
330
+ "\tmodel=PurpleStatic,\n",
331
+ "\targs=args,\n",
332
+ "\ttrain_dataset=ds_train,\n",
333
+ "\tloss=loss\n",
334
+ ")\n",
335
+ "trainer.train(resume_from_checkpoint=\"trainer_output/checkpoint-4000/\")"
336
+ ]
337
+ },
338
+ {
339
+ "cell_type": "code",
340
+ "execution_count": null,
341
+ "id": "389fbdee-c86d-48a6-837b-6df7a2ec75cb",
342
+ "metadata": {},
343
+ "outputs": [],
344
+ "source": [
345
+ "PurpleStatic.push_to_hub(\"LocalWisdom/PurpleStatic\")"
346
+ ]
347
+ }
348
+ ],
349
+ "metadata": {
350
+ "kernelspec": {
351
+ "display_name": "Python (uv venv)",
352
+ "language": "python",
353
+ "name": "uv-kernel"
354
+ },
355
+ "language_info": {
356
+ "codemirror_mode": {
357
+ "name": "ipython",
358
+ "version": 3
359
+ },
360
+ "file_extension": ".py",
361
+ "mimetype": "text/x-python",
362
+ "name": "python",
363
+ "nbconvert_exporter": "python",
364
+ "pygments_lexer": "ipython3",
365
+ "version": "3.13.11"
366
+ }
367
+ },
368
+ "nbformat": 4,
369
+ "nbformat_minor": 5
370
+ }