File size: 2,102 Bytes
1b47fab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ec2-user/miniconda3/envs/fastembed/lib/python3.10/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",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PyTorch vs ONNX max diff: 8.58306884765625e-06\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "from transformers import AutoModel, AutoTokenizer\n",
    "from onnxruntime import InferenceSession\n",
    "\n",
    "# PyTorch modeli yükle\n",
    "model_name = \"yazge/turkish-colbert-onnx\"\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
    "model = AutoModel.from_pretrained(model_name)\n",
    "\n",
    "text = \"Bu bir test cümlesidir.\"\n",
    "inputs = tokenizer(text, return_tensors=\"pt\")\n",
    "\n",
    "# PyTorch modelinden çıktı al\n",
    "with torch.no_grad():\n",
    "    torch_out = model(**inputs).last_hidden_state.numpy()\n",
    "\n",
    "# ONNX modelini yükle\n",
    "session = InferenceSession(\"onnx/model.onnx\")\n",
    "onnx_inputs = {k: v.numpy() for k, v in inputs.items()}\n",
    "onnx_out = session.run(None, onnx_inputs)[0]\n",
    "\n",
    "# Maksimum farkı hesapla\n",
    "max_diff = np.max(np.abs(torch_out - onnx_out))\n",
    "print(f\"PyTorch vs ONNX max diff: {max_diff}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "fastembed",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}