Skip to content

Instantly share code, notes, and snippets.

@motokimura
Last active September 17, 2024 01:04
Show Gist options
  • Save motokimura/1a90c0b8c5628914b99a81cd91369636 to your computer and use it in GitHub Desktop.
Save motokimura/1a90c0b8c5628914b99a81cd91369636 to your computer and use it in GitHub Desktop.
Quantization error simulation of SiLU (Swish) activation
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def fake_quantize(x):\n",
" min_ = np.percentile(x, 0)\n",
" max_ = np.percentile(x, 100)\n",
" x_clip = np.clip(x, a_min=min_, a_max=max_)\n",
" x_int8 = ((x_clip - min_) / (max_ - min_) * 255).astype(np.uint8)\n",
" return (x_int8.astype(float) / 255) * (max_ - min_) + min_"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def relu(x, quantize=False):\n",
" quantize_fn = fake_quantize if quantize else (lambda x: x)\n",
" h = quantize_fn(x) # quantize input\n",
" h = h.clip(min=0) # relu operation\n",
" return quantize_fn(h) # quantize output\n",
"\n",
"\n",
"def sigmoid(x):\n",
" return 1 / (1 + np.exp(-x))\n",
"\n",
"\n",
"def silu(x, quantize=False):\n",
" quantize_fn = fake_quantize if quantize else (lambda x: x)\n",
" h1 = quantize_fn(x)\n",
" h2 = quantize_fn(sigmoid(h1))\n",
" return quantize_fn(h1 * h2)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def compute_sqnr(x, q):\n",
" return 20 * np.log10(np.linalg.norm(x) / np.linalg.norm(x - q))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ReLU SQNR[dB]: 34.1037243352085\n",
"SiLU SQNR[dB]: 31.78201463229342\n"
]
},
{
"data":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BwUAgDpg586d2rx5s5o3by5JiouL07/+9S/Nnz9frVq10vr16/WnP/1JgYGB6t69ew3X9vIIKGUoz3nHip4rBADA1VauXClfX1+dO3dO+fn5cnd315tvvqn8/Hy98MIL+vrrrxUZGSlJuu6667Rx40a9/fbbBBQAAFB9br/9dr311ls6ffq0Zs+eLU9PT8XExGjXrl06c+aM7rrrLqfyBQUFuvnmm2uothVDQAEAoJZq2LChbrjhBknS+++/r44dO+q9997TTTfdJEn67LPPdO211zot4+3tfdn12mw2SVJubm6pq3hycnJkt9tdUPtLI6AAAFAHuLu769lnn9W4ceO0f/9+eXt7Ky0trVKnc1q1aiV3d3clJyebY1ok6aefflJubq5at27tyqqXqUJX8UyfPl1ubm5Oj7Zt25rzz549q9jYWDVq1Ei+vr6KiYlRVlaW0zrS0tLUp08fNWjQQE2aNNGECRN07tw517QGAICr2P333y8PDw+9/fbbGj9+vMaOHauFCxfq4MGD+u677/TGG2+Y904psW/fPqWkpDg96tevr0cffVRPP/20PvnkE6Wmpmr9+vUaNGiQunbtqm7dulV7Wyrcg3LjjTfq66+//v8VeP7/KsaOHavPPvtMS5culd1u16hRozRgwABt2rRJ0vkfZOrTp48cDoc2b96sjIwMDRkyRPXq1dMLL7zgguYAAOAatfFiCE9PT40aNUqzZs1SamqqAgMDFRcXp59++kn+/v7q3Lmznn32WadlBg4cWGo96enpmjNnjmbOnKmJEyfq8OHDcjgcuuuuu/T3v//9ivxukZthGEZ5C0+fPl0rVqxQSkpKqXm5ubkKDAzUhx9+qPvuu0+StHfvXrVr106JiYnq2rWrvvjiC91zzz06cuSIgoKCJEnz58/XxIkTdfTo0XL/kmReXp7sdrtyc3PN82SuxFU8AHD1OHv2rFJTU9WyZcty3YEVl3ap17Min98VvlHbgQMHFBISouuuu06DBg1SWlqaJCk5OVmFhYWKiooyy7Zt21bNmjVTYmKiJCkxMVHt27c3w4kkRUdHKy8vT7t27apoVQAAQB1VoVM8ERERio+PV5s2bZSRkaHnnntOt912m3bu3KnMzE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o1SoVUGJjY9WnTx9FRUU5TU9OTlZhYaHT9LZt26pZs2ZKTEyUJCUmJqp9+/YKCgoyy0RHRysvL0+7du0qc3v5+fnKy8tzegAAgLrLs6ILLF68WN999522bt1aal5mZqa8vLzk7+/vND0oKEiZmZlmmQvDScn8knlliYuL03PPPVfRqgIAgFqqQj0o6enpeuqpp/TBBx+ofv361VWnUiZNmqTc3FzzkZ6efsW2DQAArrwKBZTk5GRlZ2erc+fO8vT0lKenp9atW6fXX39dnp6eCgoKUkFBgXJycpyWy8rKksPhkCQ5HI5SV/WUPC8p81ve3t6y2WxODwAAUHdVKKDceeed2rFjh1JSUszHLbfcokGDBpn/r1evnhISEsxl9u3bp7S0NEVGRkqSIiMjtWPHDmVnZ5tlVq9eLZvNprCwMBc1CwAA1GYVGoPi5+enm266yWlaw4YN1ahRI3P68OHDNW7cOAUEBMhms2n06NGKjIxU165dJUk9e/ZUWFiYBg8erFmzZikzM1OTJ09WbGysvL29XdQsAABQm1V4kOzlzJ49W+7u7oqJiVF+fr6io6M1b948c76Hh4dWrlypJ554QpGRkWrYsKGGDh2qGTNmuLoqAACglnIzDMOo6UpUVF5enux2u3Jzc6tlPMqkZTsuWyZuQHuXbxcAgLqsIp/f/BYPAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwHAIKAACwH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ILjRo1SitXrtT69evVtGlTc7rD4VBBQYFycnKcelGysrLkcDjMMlu2bHFaX8lVPiVlfsvb21ve3t6VqSoAAKiFKtSDYhiGRo0apeXLl+ubb75Ry5YtneaHh4erXr16SkhIMKft27dPaWlpioyMlCRFRkZqx44dys7ONsusXr1aNptNYWFhVWkLAACoIyrUgxIbG6sPP/xQH3/8sfz8/MwxI3a7XT4+PrLb7Ro+fLjGjRungIAA2Ww2jR49WpGRkerataskqWfPngoLC9PgwYM1a9YsZWZmavLkyYqNjaWXBAAASKpgQHnrrbckST169HCavmDBAg0bNkySNHv2bLm7uysmJkb5+fmKjo7WvHnzzLIeHh5auXKlnnjiCUVGRqphw4YaOnSoZsyYUbWWAACAOqNK90GpKdwHBQCA2ueK3QcFAACgOhBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5RBQAACA5VQ4oKxfv159+/ZVSEiI3NzctGLFCqf5hmFo6tSpCg4Olo+Pj6KionTgwAGnMidOnNCgQYNks9nk7++v4cOH69SpU1VqCAAAqDsqHFBOnz6tjh07au7cuWXOnzVrll5//XXNnz9fSUlJatiwoaKjo3X27FmzzKBBg7Rr1y6tXr1aK1eu1Pr16zVy5MjKtwIAANQpnhVdoHfv3urdu3eZ8wzD0GuvvabJkyerX79+kqR//vOfCgoK0ooVKzRw4E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VatWslut8tms+nUqVOl1jF27FglJSVp/fr1uvbaa83p5T1WylLyGoWHh5vT2rZtW6mrZubOnavw8HAFBgbK19dX77zzTqk23HjjjfLw8DCfV+V9/c477+i9997TJ598osDAQEnnX4sff/xRfn5+5msREBCgs2fP6uDBg8rNzVVGRobTMeHp6Vnm+8jHx0eSKvT3ANWLgIIqcXNzK3U+98LTCImJiRo0aJDuvvturVy5Utu3b9df//rXUgM869WrV2q9lRm0uXv3bg0cOFAzZ85Uz549zemHDh3SPffcow4dOuh///d/lZycrLlz50oqe7BpVVW0PadOndJjjz2mlJQU8/H999/rwIEDuv76681yDRs2LLWsj49PpQYKV2a506dPKzo6WjabTR988IG2bt1qdpdX5HV89NFH9dNPP2nw4MHasWOHbrnlFr3xxhuSpMaNG0s6P57E1V566SXNmTNHEydO1Jo1a5SSkqLo6OgKHwOPP/64074KCQkp97JDhw5VSkqK5syZo82bNyslJUWNGjUqVYe77rpL//3vf7Vq1Sqn6eU9VirL3f38x8KF7+sL39PS+TFC48eP1/Dhw/XVV18pJSVFjzzySLW9r9esWWOe6rowVJ86dUrh4eFOr0VKSor279+vhx9+uELbOHHihCSZ4Qc1z/rD4FEjrr/+etWrV09JSUlq1qyZpPMfGPv37ze/MUvn38wXnjs+cOCA0zeQzZs3q3nz5k6Xix4+fLjC9fHy8lJRUdElyxw7dkx9+/ZVTEyMxo4d6zQvOTlZxcXFeuWVV8w/wL89X16ebbRr107p6elKT083e1F2796tnJwchYWFVbRZps6dO2v37t264YYbKr2OC+t47tw5JSUlqVu3bpLODw7dt29fleooSXv37tXx48c1c+ZMs/3btm0rs+y3335b6thp166dOT80NFSPP/64Hn/8cU2aNEnvvvuuRo8ereuvv142m027d+926gG7nPK0e9OmTerXr5/+9Kc/STo/lmL//v1Or0t5joOAgAAFBASUu24X2rRpk+bNm6e7775b0vlBnseOHStV7t5771Xfvn318MMPy8PDQwMHDpRUtWOlbdu2OnfunJKTk/W73/1OkrRv3z6ne9aUfEBnZGTo5ptvliS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fv367bbbtPNN9+sqVOnVujUwYUWLFig8PBw3XPPPYqMjJRhGPr8889Ldbn/1rFjxy75TTgwMFDx8fFaunSpwsLCNHPmTL388stllp05c6aeeuophYeHKzMzU59++qm8vLwknR8LEhsbq3bt2qlXr15q3bq15s2bZy776KOPavHixRUOB5dr9+TJk9W5c2dFR0erR48ecjgcTncUlcp3rFXFe++9p19++UWdO3fW4MGD9eSTT17yvh/33XefFi5cqMGDB2vZsmVVPlYWLFigkJAQde/eXQMGDNDIkSNLbf/999/XuXPnFB4erjFjxuhvf/ub0/zHHntMAwYM0IMPPqiIiAgdP37cqTelvGbMmKFDhw7p+uuvL/PUSlZWlvbu3auEhASFhIQ4va8lqUGDBlq/fr2aNWumAQMGqF27dho+fLjOnj1r9qg8/fTTGjx4sIYOHarIyEj5+fnpj3/8o9N2zp49qxUrVmjEiBEVbgOqj5vx2wEEwCX06NFDnTp14tbvqFaGYSgiIkJjx47VQw89VNPVqfNatGihMWPGWP5nA6rLW2+9peXLl+urr76q6argAvSgALAcNzc3vfPOOzp37lxNVwVXgXr16pmDtGEdDJIFYEmdOnVSp06daroatcqGDRsu+Xs/JXdOhrNHH320pquAMnCKBwDqiF9//VX//e9/LzrfFVeJAVcKAQUAAFgOY1AAAIDlEFAAAIDlEFAAAIDlEFAAAIDlEFAAAIDlEFAAAIDlEFAAAIDlEFAAAIDl/B+6rI3pvhW2QQAAAABJRU5ErkJggg==",
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mu = 0\n",
"sigma = 1.0\n",
"x = np.random.normal(mu, sigma, 1000)\n",
"\n",
"bins = np.linspace(0, 0.045, 50)\n",
"plt.hist(np.abs(relu(x) - relu(x, quantize=True)), bins, label='ReLU', alpha=0.6)\n",
"plt.hist(np.abs(silu(x) - silu(x, quantize=True)), bins, label='SiLU', alpha=0.6)\n",
"plt.legend()\n",
"plt.xlabel('quantization error: abs(float - fake_quantized)');\n",
"\n",
"print('ReLU SQNR[dB]:', compute_sqnr(relu(x), relu(x, quantize=True)))\n",
"print('SiLU SQNR[dB]:', compute_sqnr(silu(x), silu(x, quantize=True)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.9.17"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment