Skip to content

Commit 688a5ca

Browse files
authored
fix: transformers library deprecated
transformers library was deprecated, and was replaced with optimum changed notebook and Google Colab
1 parent 0e7c0fd commit 688a5ca

3 files changed

Lines changed: 85 additions & 68 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ build/
77
### User Defined ###
88
*.onnx
99
raw-files
10+
onnx/
1011

1112
### IntelliJ IDEA ###
1213
.idea/modules.xml

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ These files are required to run the project:
2020

2121
### Convert the ONNX Model
2222

23-
To convert the Hugging Face NER model to ONNX, open this [Google Colaboratory Notebook](https://colab.research.google.com/drive/1kZx9XOnExVfPoAGHhHRUrdQnioiLloBW#revisionId=0BwKss6yztf4KS0NKaWRiQjc0RGRvQkd6ZFp3OUFhR1lTclBNPQ), run the code as shown in the image below, and follow all the steps.
23+
To convert the Hugging Face NER model to ONNX, open this [Google Colaboratory Notebook](https://colab.research.google.com/drive/1kZx9XOnExVfPoAGHhHRUrdQnioiLloBW#revisionId=0BwKss6yztf4KeTFwdHBrOWJMQVlvdUkyby9WaEt0LzVnbkdZPQ), run the code as shown in the image below, and follow all the steps.
2424

2525
![run colab code cell](images/run-colab.jpeg)
2626

Lines changed: 83 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,91 @@
11
{
2-
"cells": [
3-
{
4-
"cell_type": "markdown",
5-
"metadata": {
6-
"id": "xJckl99IHePQ"
7-
},
8-
"source": [
9-
"## Hugging Face Model to ONNX\n"
10-
]
11-
},
12-
{
13-
"cell_type": "code",
14-
"execution_count": null,
15-
"metadata": {
16-
"id": "IWvxTyMSHnFo"
17-
},
18-
"outputs": [],
19-
"source": [
20-
"%pip install -q transformers[onnx] transformers[sentencepiece] torch"
21-
]
22-
},
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "xJckl99IHePQ"
7+
},
8+
"source": [
9+
"## Hugging Face Model to ONNX\n"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 3,
15+
"metadata": {
16+
"id": "IWvxTyMSHnFo"
17+
},
18+
"outputs": [
2319
{
24-
"cell_type": "code",
25-
"execution_count": 2,
26-
"metadata": {
27-
"colab": {
28-
"base_uri": "https://localhost:8080/"
29-
},
30-
"id": "aWB2G_kLHou2",
31-
"outputId": "b66c9ef4-8536-40de-cf47-fe2aeb9410c7"
32-
},
33-
"outputs": [
34-
{
35-
"name": "stdout",
36-
"output_type": "stream",
37-
"text": [
38-
"Validating ONNX model...\n",
39-
"\t-[✓] ONNX model output names match reference model ({'logits'})\n",
40-
"\t- Validating ONNX Model output \"logits\":\n",
41-
"\t\t-[✓] (3, 9, 8) matches (3, 9, 8)\n",
42-
"\t\t-[✓] all values close (atol: 0.005)\n",
43-
"All good, model saved at: onnx/model.onnx\n"
44-
]
45-
}
46-
],
47-
"source": [
48-
"!python -m transformers.onnx --opset 16 --atol 0.005 \\\n",
49-
" --feature=token-classification \\\n",
50-
" --model=xlm-roberta-large-finetuned-conll03-english onnx/"
51-
]
20+
"name": "stdout",
21+
"output_type": "stream",
22+
"text": [
23+
"\n",
24+
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m26.0.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m26.1.2\u001b[0m\n",
25+
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
26+
"Note: you may need to restart the kernel to use updated packages.\n"
27+
]
5228
}
53-
],
54-
"metadata": {
29+
],
30+
"source": [
31+
"%pip install optimum-onnx onnx onnxruntime -q"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 1,
37+
"metadata": {
5538
"colab": {
56-
"provenance": []
57-
},
58-
"kernelspec": {
59-
"display_name": "Python 3.8.0 64-bit",
60-
"language": "python",
61-
"name": "python3"
62-
},
63-
"language_info": {
64-
"name": "python",
65-
"version": "3.8.0"
39+
"base_uri": "https://localhost:8080/"
6640
},
67-
"vscode": {
68-
"interpreter": {
69-
"hash": "437c6a4c2ab8ad564298253974ee7794b68bcbeea462aed8eaaa05b6d7c57f73"
70-
}
41+
"id": "aWB2G_kLHou2",
42+
"outputId": "b66c9ef4-8536-40de-cf47-fe2aeb9410c7"
43+
},
44+
"outputs": [
45+
{
46+
"name": "stdout",
47+
"output_type": "stream",
48+
"text": [
49+
"`torch_dtype` is deprecated! Use `dtype` instead!\n",
50+
"Some weights of the model checkpoint at xlm-roberta-large-finetuned-conll03-english were not used when initializing XLMRobertaForTokenClassification: ['roberta.pooler.dense.bias', 'roberta.pooler.dense.weight']\n",
51+
"- This IS expected if you are initializing XLMRobertaForTokenClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
52+
"- This IS NOT expected if you are initializing XLMRobertaForTokenClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n",
53+
"/home/codespace/.python/current/lib/python3.12/site-packages/transformers/modeling_attn_mask_utils.py:196: TracerWarning: torch.tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.\n",
54+
" inverted_mask = torch.tensor(1.0, dtype=dtype) - expanded_mask\n",
55+
"\u001b[0;93m2026-06-05 18:41:16.243880067 [W:onnxruntime:Default, device_discovery.cc:133 GetPciBusId] Skipping pci_bus_id for PCI path at \"/sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A03:00/device:07/VMBUS:01/5620e0c7-8062-4dce-aeb7-520c7ef76171\" because filename \"5620e0c7-8062-4dce-aeb7-520c7ef76171\" did not match expected pattern of [0-9a-f]+:[0-9a-f]+:[0-9a-f]+[.][0-9a-f]+\u001b[m\n",
56+
"Weight deduplication check in the ONNX export requires accelerate. Please install accelerate to run it.\n"
57+
]
7158
}
59+
],
60+
"source": [
61+
"!optimum-cli export onnx --opset 18 --atol 0.005 \\\n",
62+
" --task=token-classification \\\n",
63+
" --model=xlm-roberta-large-finetuned-conll03-english onnx/"
64+
]
65+
}
66+
],
67+
"metadata": {
68+
"colab": {
69+
"provenance": []
70+
},
71+
"kernelspec": {
72+
"display_name": "Python 3",
73+
"language": "python",
74+
"name": "python3"
7275
},
73-
"nbformat": 4,
74-
"nbformat_minor": 0
76+
"language_info": {
77+
"codemirror_mode": {
78+
"name": "ipython",
79+
"version": 3
80+
},
81+
"file_extension": ".py",
82+
"mimetype": "text/x-python",
83+
"name": "python",
84+
"nbconvert_exporter": "python",
85+
"pygments_lexer": "ipython3",
86+
"version": "3.12.1"
87+
}
88+
},
89+
"nbformat": 4,
90+
"nbformat_minor": 0
7591
}

0 commit comments

Comments
 (0)