Oleg
commited on
Commit
·
7982df2
1
Parent(s):
ebc8af3
Monor improvement
Browse files- safetensors_to_onnx.ipynb +32 -196
safetensors_to_onnx.ipynb
CHANGED
|
@@ -2,11 +2,7 @@
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"metadata": {
|
| 5 |
-
"collapsed": true
|
| 6 |
-
"ExecuteTime": {
|
| 7 |
-
"end_time": "2026-02-12T12:52:46.678786554Z",
|
| 8 |
-
"start_time": "2026-02-12T12:52:43.490350354Z"
|
| 9 |
-
}
|
| 10 |
},
|
| 11 |
"cell_type": "code",
|
| 12 |
"source": [
|
|
@@ -20,15 +16,10 @@
|
|
| 20 |
],
|
| 21 |
"id": "2b3977272abf14d9",
|
| 22 |
"outputs": [],
|
| 23 |
-
"execution_count":
|
| 24 |
},
|
| 25 |
{
|
| 26 |
-
"metadata": {
|
| 27 |
-
"ExecuteTime": {
|
| 28 |
-
"end_time": "2026-02-12T12:52:46.726391124Z",
|
| 29 |
-
"start_time": "2026-02-12T12:52:46.691717774Z"
|
| 30 |
-
}
|
| 31 |
-
},
|
| 32 |
"cell_type": "code",
|
| 33 |
"source": [
|
| 34 |
"# MODEL_SOURCE_ID = \"sergeyzh/BERTA\"\n",
|
|
@@ -41,25 +32,11 @@
|
|
| 41 |
"MODEL_TARGET_PATH.mkdir(parents=True, exist_ok=True)"
|
| 42 |
],
|
| 43 |
"id": "494fc15203b0fb89",
|
| 44 |
-
"outputs": [
|
| 45 |
-
|
| 46 |
-
"name": "stdout",
|
| 47 |
-
"output_type": "stream",
|
| 48 |
-
"text": [
|
| 49 |
-
"==================================================\n",
|
| 50 |
-
"Подготовка директории: onnx/berta-onnx\n"
|
| 51 |
-
]
|
| 52 |
-
}
|
| 53 |
-
],
|
| 54 |
-
"execution_count": 2
|
| 55 |
},
|
| 56 |
{
|
| 57 |
-
"metadata": {
|
| 58 |
-
"ExecuteTime": {
|
| 59 |
-
"end_time": "2026-02-12T12:52:46.862603179Z",
|
| 60 |
-
"start_time": "2026-02-12T12:52:46.739714466Z"
|
| 61 |
-
}
|
| 62 |
-
},
|
| 63 |
"cell_type": "code",
|
| 64 |
"source": [
|
| 65 |
"# 1. Загружаем модель и токенизатор\n",
|
|
@@ -86,56 +63,22 @@
|
|
| 86 |
"print(dummy_inputs)"
|
| 87 |
],
|
| 88 |
"id": "4f9f5febc6f07769",
|
| 89 |
-
"outputs": [
|
| 90 |
-
|
| 91 |
-
"name": "stdout",
|
| 92 |
-
"output_type": "stream",
|
| 93 |
-
"text": [
|
| 94 |
-
"Загрузка модели и токенизатора из '../BERTA'...\n",
|
| 95 |
-
"Создание тестовых входных данных...\n",
|
| 96 |
-
"{'input_ids': tensor([[ 2, 570, 11028, ..., 0, 0, 0],\n",
|
| 97 |
-
" [ 2, 3007, 67, ..., 0, 0, 0],\n",
|
| 98 |
-
" [ 2, 46369, 998, ..., 0, 0, 0]]), 'token_type_ids': tensor([[0, 0, 0, ..., 0, 0, 0],\n",
|
| 99 |
-
" [0, 0, 0, ..., 0, 0, 0],\n",
|
| 100 |
-
" [0, 0, 0, ..., 0, 0, 0]]), 'attention_mask': tensor([[1, 1, 1, ..., 0, 0, 0],\n",
|
| 101 |
-
" [1, 1, 1, ..., 0, 0, 0],\n",
|
| 102 |
-
" [1, 1, 1, ..., 0, 0, 0]])}\n"
|
| 103 |
-
]
|
| 104 |
-
}
|
| 105 |
-
],
|
| 106 |
-
"execution_count": 3
|
| 107 |
},
|
| 108 |
{
|
| 109 |
-
"metadata": {
|
| 110 |
-
"ExecuteTime": {
|
| 111 |
-
"end_time": "2026-02-12T12:52:46.899958136Z",
|
| 112 |
-
"start_time": "2026-02-12T12:52:46.868506089Z"
|
| 113 |
-
}
|
| 114 |
-
},
|
| 115 |
"cell_type": "code",
|
| 116 |
"source": [
|
| 117 |
"# print(model)\n",
|
| 118 |
"print(signature(model.forward))"
|
| 119 |
],
|
| 120 |
"id": "8bdce4e5bc593383",
|
| 121 |
-
"outputs": [
|
| 122 |
-
|
| 123 |
-
"name": "stdout",
|
| 124 |
-
"output_type": "stream",
|
| 125 |
-
"text": [
|
| 126 |
-
"(input_ids: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, past_key_values: Optional[transformers.cache_utils.Cache] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None) -> Union[tuple[torch.Tensor], transformers.modeling_outputs.BaseModelOutputWithPoolingAndCrossAttentions]\n"
|
| 127 |
-
]
|
| 128 |
-
}
|
| 129 |
-
],
|
| 130 |
-
"execution_count": 4
|
| 131 |
},
|
| 132 |
{
|
| 133 |
-
"metadata": {
|
| 134 |
-
"ExecuteTime": {
|
| 135 |
-
"end_time": "2026-02-12T12:52:56.427369911Z",
|
| 136 |
-
"start_time": "2026-02-12T12:52:46.902043777Z"
|
| 137 |
-
}
|
| 138 |
-
},
|
| 139 |
"cell_type": "code",
|
| 140 |
"source": [
|
| 141 |
"# 3. Экспорт с двумя входами\n",
|
|
@@ -153,20 +96,21 @@
|
|
| 153 |
"# }\n",
|
| 154 |
"\n",
|
| 155 |
"# In case of issues use dynamo_export instead of dynamo=True\n",
|
| 156 |
-
"torch.
|
| 157 |
-
"
|
| 158 |
-
"
|
| 159 |
-
"
|
| 160 |
-
"
|
| 161 |
-
"
|
| 162 |
-
"
|
| 163 |
-
"
|
| 164 |
-
"
|
| 165 |
-
"
|
| 166 |
-
"
|
| 167 |
-
"
|
| 168 |
-
"
|
| 169 |
-
"
|
|
|
|
| 170 |
"# 4. Сохраняем токенизатор\n",
|
| 171 |
"print(f\"Сохранение токенизатора в '{MODEL_TARGET_PATH}'...\")\n",
|
| 172 |
"tokenizer.save_pretrained(MODEL_TARGET_PATH)\n",
|
|
@@ -174,97 +118,11 @@
|
|
| 174 |
"print(\"Конвертация завершена успешно!\")"
|
| 175 |
],
|
| 176 |
"id": "87d59bf71ed545dc",
|
| 177 |
-
"outputs": [
|
| 178 |
-
|
| 179 |
-
"name": "stdout",
|
| 180 |
-
"output_type": "stream",
|
| 181 |
-
"text": [
|
| 182 |
-
"Экспорт модели в ONNX формат: onnx/berta-onnx/BERTA.onnx\n"
|
| 183 |
-
]
|
| 184 |
-
},
|
| 185 |
-
{
|
| 186 |
-
"name": "stderr",
|
| 187 |
-
"output_type": "stream",
|
| 188 |
-
"text": [
|
| 189 |
-
"W0212 14:52:47.799000 19280 torch/onnx/_internal/exporter/_schemas.py:455] Missing annotation for parameter 'input' from (input, boxes, output_size: 'Sequence[int]', spatial_scale: 'float' = 1.0, sampling_ratio: 'int' = -1, aligned: 'bool' = False). Treating as an Input.\n",
|
| 190 |
-
"W0212 14:52:47.800000 19280 torch/onnx/_internal/exporter/_schemas.py:455] Missing annotation for parameter 'boxes' from (input, boxes, output_size: 'Sequence[int]', spatial_scale: 'float' = 1.0, sampling_ratio: 'int' = -1, aligned: 'bool' = False). Treating as an Input.\n",
|
| 191 |
-
"W0212 14:52:47.801000 19280 torch/onnx/_internal/exporter/_schemas.py:455] Missing annotation for parameter 'input' from (input, boxes, output_size: 'Sequence[int]', spatial_scale: 'float' = 1.0). Treating as an Input.\n",
|
| 192 |
-
"W0212 14:52:47.802000 19280 torch/onnx/_internal/exporter/_schemas.py:455] Missing annotation for parameter 'boxes' from (input, boxes, output_size: 'Sequence[int]', spatial_scale: 'float' = 1.0). Treating as an Input.\n"
|
| 193 |
-
]
|
| 194 |
-
},
|
| 195 |
-
{
|
| 196 |
-
"name": "stdout",
|
| 197 |
-
"output_type": "stream",
|
| 198 |
-
"text": [
|
| 199 |
-
"[torch.onnx] Obtain model graph for `BertModel([...]` with `torch.export.export(..., strict=False)`...\n",
|
| 200 |
-
"[torch.onnx] Obtain model graph for `BertModel([...]` with `torch.export.export(..., strict=False)`... ✅\n",
|
| 201 |
-
"[torch.onnx] Run decomposition...\n"
|
| 202 |
-
]
|
| 203 |
-
},
|
| 204 |
-
{
|
| 205 |
-
"name": "stderr",
|
| 206 |
-
"output_type": "stream",
|
| 207 |
-
"text": [
|
| 208 |
-
"/home/lavrentiy/Projects/FRIDA-transformed/.venv/lib/python3.13/site-packages/torch/cuda/__init__.py:435: UserWarning: \n",
|
| 209 |
-
" Found GPU0 NVIDIA GeForce GTX 1060 6GB which is of cuda capability 6.1.\n",
|
| 210 |
-
" Minimum and Maximum cuda capability supported by this version of PyTorch is\n",
|
| 211 |
-
" (7.0) - (12.0)\n",
|
| 212 |
-
" \n",
|
| 213 |
-
" queued_call()\n",
|
| 214 |
-
"/home/lavrentiy/Projects/FRIDA-transformed/.venv/lib/python3.13/site-packages/torch/cuda/__init__.py:435: UserWarning: \n",
|
| 215 |
-
" Please install PyTorch with a following CUDA\n",
|
| 216 |
-
" configurations: 12.6 following instructions at\n",
|
| 217 |
-
" https://pytorch.org/get-started/locally/\n",
|
| 218 |
-
" \n",
|
| 219 |
-
" queued_call()\n",
|
| 220 |
-
"/home/lavrentiy/Projects/FRIDA-transformed/.venv/lib/python3.13/site-packages/torch/cuda/__init__.py:435: UserWarning: \n",
|
| 221 |
-
"NVIDIA GeForce GTX 1060 6GB with CUDA capability sm_61 is not compatible with the current PyTorch installation.\n",
|
| 222 |
-
"The current PyTorch install supports CUDA capabilities sm_70 sm_75 sm_80 sm_86 sm_90 sm_100 sm_120.\n",
|
| 223 |
-
"If you want to use the NVIDIA GeForce GTX 1060 6GB GPU with PyTorch, please check the instructions at https://pytorch.org/get-started/locally/\n",
|
| 224 |
-
"\n",
|
| 225 |
-
" queued_call()\n",
|
| 226 |
-
"/home/lavrentiy/.local/share/uv/python/cpython-3.13.11-linux-x86_64-gnu/lib/python3.13/copyreg.py:99: FutureWarning: `isinstance(treespec, LeafSpec)` is deprecated, use `isinstance(treespec, TreeSpec) and treespec.is_leaf()` instead.\n",
|
| 227 |
-
" return cls.__new__(cls, *args)\n"
|
| 228 |
-
]
|
| 229 |
-
},
|
| 230 |
-
{
|
| 231 |
-
"name": "stdout",
|
| 232 |
-
"output_type": "stream",
|
| 233 |
-
"text": [
|
| 234 |
-
"[torch.onnx] Run decomposition... ✅\n",
|
| 235 |
-
"[torch.onnx] Translate the graph into ONNX...\n",
|
| 236 |
-
"[torch.onnx] Translate the graph into ONNX... ✅\n"
|
| 237 |
-
]
|
| 238 |
-
},
|
| 239 |
-
{
|
| 240 |
-
"name": "stderr",
|
| 241 |
-
"output_type": "stream",
|
| 242 |
-
"text": [
|
| 243 |
-
"/home/lavrentiy/Projects/FRIDA-transformed/.venv/lib/python3.13/site-packages/torch/onnx/_internal/exporter/_onnx_program.py:460: UserWarning: # The axis name: batch_size will not be used, since it shares the same shape constraints with another axis: batch_size.\n",
|
| 244 |
-
" rename_mapping = _dynamic_shapes.create_rename_mapping(\n",
|
| 245 |
-
"/home/lavrentiy/Projects/FRIDA-transformed/.venv/lib/python3.13/site-packages/torch/onnx/_internal/exporter/_onnx_program.py:460: UserWarning: # The axis name: sequence_length will not be used, since it shares the same shape constraints with another axis: sequence_length.\n",
|
| 246 |
-
" rename_mapping = _dynamic_shapes.create_rename_mapping(\n"
|
| 247 |
-
]
|
| 248 |
-
},
|
| 249 |
-
{
|
| 250 |
-
"name": "stdout",
|
| 251 |
-
"output_type": "stream",
|
| 252 |
-
"text": [
|
| 253 |
-
"Applied 68 of general pattern rewrite rules.\n",
|
| 254 |
-
"Сохранение токенизатора в 'onnx/berta-onnx'...\n",
|
| 255 |
-
"Конвертация завершена успешно!\n"
|
| 256 |
-
]
|
| 257 |
-
}
|
| 258 |
-
],
|
| 259 |
-
"execution_count": 5
|
| 260 |
},
|
| 261 |
{
|
| 262 |
-
"metadata": {
|
| 263 |
-
"ExecuteTime": {
|
| 264 |
-
"end_time": "2026-02-12T12:52:56.931194388Z",
|
| 265 |
-
"start_time": "2026-02-12T12:52:56.428745759Z"
|
| 266 |
-
}
|
| 267 |
-
},
|
| 268 |
"cell_type": "code",
|
| 269 |
"source": [
|
| 270 |
"# 5. Тестирование и сравнение результатов\n",
|
|
@@ -330,30 +188,8 @@
|
|
| 330 |
"print(f\"Путь к модели: {MODEL_TARGET_PATH.resolve()}\")"
|
| 331 |
],
|
| 332 |
"id": "91a5740805f8e829",
|
| 333 |
-
"outputs": [
|
| 334 |
-
|
| 335 |
-
"name": "stdout",
|
| 336 |
-
"output_type": "stream",
|
| 337 |
-
"text": [
|
| 338 |
-
"\n",
|
| 339 |
-
"==================================================\n",
|
| 340 |
-
"ТЕСТИРОВАНИЕ РЕЗУЛЬТАТОВ\n",
|
| 341 |
-
"Тестирование оригинальной модели...\n",
|
| 342 |
-
"Тестирование ONNX модели...\n",
|
| 343 |
-
"\n",
|
| 344 |
-
"Cosine similarity между оригинальной и ONNX моделью:\n",
|
| 345 |
-
" Текст 1: 1.000000\n",
|
| 346 |
-
" Текст 2: 1.000000\n",
|
| 347 |
-
" Текст 3: 1.000000\n",
|
| 348 |
-
"Средняя схожесть: 1.000000\n",
|
| 349 |
-
"\n",
|
| 350 |
-
"==================================================\n",
|
| 351 |
-
"ГОТОВО! Модель успешно конвертирована и протестирована.\n",
|
| 352 |
-
"Путь к модели: /home/lavrentiy/Projects/BERTA-transformed/onnx/berta-onnx\n"
|
| 353 |
-
]
|
| 354 |
-
}
|
| 355 |
-
],
|
| 356 |
-
"execution_count": 6
|
| 357 |
}
|
| 358 |
],
|
| 359 |
"metadata": {
|
|
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"metadata": {
|
| 5 |
+
"collapsed": true
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
},
|
| 7 |
"cell_type": "code",
|
| 8 |
"source": [
|
|
|
|
| 16 |
],
|
| 17 |
"id": "2b3977272abf14d9",
|
| 18 |
"outputs": [],
|
| 19 |
+
"execution_count": null
|
| 20 |
},
|
| 21 |
{
|
| 22 |
+
"metadata": {},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"cell_type": "code",
|
| 24 |
"source": [
|
| 25 |
"# MODEL_SOURCE_ID = \"sergeyzh/BERTA\"\n",
|
|
|
|
| 32 |
"MODEL_TARGET_PATH.mkdir(parents=True, exist_ok=True)"
|
| 33 |
],
|
| 34 |
"id": "494fc15203b0fb89",
|
| 35 |
+
"outputs": [],
|
| 36 |
+
"execution_count": null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
},
|
| 38 |
{
|
| 39 |
+
"metadata": {},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
"cell_type": "code",
|
| 41 |
"source": [
|
| 42 |
"# 1. Загружаем модель и токенизатор\n",
|
|
|
|
| 63 |
"print(dummy_inputs)"
|
| 64 |
],
|
| 65 |
"id": "4f9f5febc6f07769",
|
| 66 |
+
"outputs": [],
|
| 67 |
+
"execution_count": null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
},
|
| 69 |
{
|
| 70 |
+
"metadata": {},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
"cell_type": "code",
|
| 72 |
"source": [
|
| 73 |
"# print(model)\n",
|
| 74 |
"print(signature(model.forward))"
|
| 75 |
],
|
| 76 |
"id": "8bdce4e5bc593383",
|
| 77 |
+
"outputs": [],
|
| 78 |
+
"execution_count": null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
},
|
| 80 |
{
|
| 81 |
+
"metadata": {},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
"cell_type": "code",
|
| 83 |
"source": [
|
| 84 |
"# 3. Экспорт с двумя входами\n",
|
|
|
|
| 96 |
"# }\n",
|
| 97 |
"\n",
|
| 98 |
"# In case of issues use dynamo_export instead of dynamo=True\n",
|
| 99 |
+
"with torch.no_grad():\n",
|
| 100 |
+
" torch.onnx.export(\n",
|
| 101 |
+
" model,\n",
|
| 102 |
+
" (dummy_inputs[\"input_ids\"], dummy_inputs[\"attention_mask\"]),\n",
|
| 103 |
+
" onnx_model_path.as_posix(),\n",
|
| 104 |
+
" input_names=[\"input_ids\", \"attention_mask\"],\n",
|
| 105 |
+
" output_names=[\"last_hidden_state\"],\n",
|
| 106 |
+
" opset_version=20, # Maybe update\n",
|
| 107 |
+
" dynamic_shapes = {\n",
|
| 108 |
+
" \"input_ids\": {0: batch_size, 1: sequence_length},\n",
|
| 109 |
+
" \"attention_mask\": {0: batch_size, 1: sequence_length}\n",
|
| 110 |
+
" },\n",
|
| 111 |
+
" verbose=True,\n",
|
| 112 |
+
" dynamo=True\n",
|
| 113 |
+
" )\n",
|
| 114 |
"# 4. Сохраняем токенизатор\n",
|
| 115 |
"print(f\"Сохранение токенизатора в '{MODEL_TARGET_PATH}'...\")\n",
|
| 116 |
"tokenizer.save_pretrained(MODEL_TARGET_PATH)\n",
|
|
|
|
| 118 |
"print(\"Конвертация завершена успешно!\")"
|
| 119 |
],
|
| 120 |
"id": "87d59bf71ed545dc",
|
| 121 |
+
"outputs": [],
|
| 122 |
+
"execution_count": null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
},
|
| 124 |
{
|
| 125 |
+
"metadata": {},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
"cell_type": "code",
|
| 127 |
"source": [
|
| 128 |
"# 5. Тестирование и сравнение результатов\n",
|
|
|
|
| 188 |
"print(f\"Путь к модели: {MODEL_TARGET_PATH.resolve()}\")"
|
| 189 |
],
|
| 190 |
"id": "91a5740805f8e829",
|
| 191 |
+
"outputs": [],
|
| 192 |
+
"execution_count": null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
}
|
| 194 |
],
|
| 195 |
"metadata": {
|