Repository where I mostly put random python scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

407 lines
147 KiB

  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 20,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "name": "stdout",
  10. "output_type": "stream",
  11. "text": [
  12. "Wed Jul 1 11:16:12 2020 \n",
  13. "+-----------------------------------------------------------------------------+\n",
  14. "| NVIDIA-SMI 440.82 Driver Version: 440.82 CUDA Version: 10.2 |\n",
  15. "|-------------------------------+----------------------+----------------------+\n",
  16. "| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\n",
  17. "| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\n",
  18. "|===============================+======================+======================|\n",
  19. "| 0 GeForce GTX 106... Off | 00000000:01:00.0 On | N/A |\n",
  20. "| 0% 49C P2 26W / 120W | 2808MiB / 3016MiB | 2% Default |\n",
  21. "+-------------------------------+----------------------+----------------------+\n",
  22. " \n",
  23. "+-----------------------------------------------------------------------------+\n",
  24. "| Processes: GPU Memory |\n",
  25. "| GPU PID Type Process name Usage |\n",
  26. "|=============================================================================|\n",
  27. "| 0 1972 G /usr/libexec/Xorg 59MiB |\n",
  28. "| 0 2361 G /usr/libexec/Xorg 280MiB |\n",
  29. "| 0 2485 G /usr/bin/gnome-shell 231MiB |\n",
  30. "| 0 3214 G ...uest-channel-token=15065411588967843042 23MiB |\n",
  31. "| 0 4090 G /usr/lib64/firefox/firefox 12MiB |\n",
  32. "| 0 4151 G /usr/lib64/firefox/firefox 2MiB |\n",
  33. "| 0 4194 G /usr/lib64/firefox/firefox 2MiB |\n",
  34. "| 0 4917 G /usr/lib64/firefox/firefox 2MiB |\n",
  35. "| 0 5777 G /usr/lib64/firefox/firefox 2MiB |\n",
  36. "| 0 33033 G /usr/lib64/firefox/firefox 4MiB |\n",
  37. "| 0 37575 G /usr/lib64/firefox/firefox 167MiB |\n",
  38. "| 0 37626 G /usr/lib64/firefox/firefox 2MiB |\n",
  39. "| 0 50906 G ...quest-channel-token=3573384613177380021 121MiB |\n",
  40. "| 0 90844 C /home/jeff/Documents/python/ml/bin/python 1881MiB |\n",
  41. "+-----------------------------------------------------------------------------+\n"
  42. ]
  43. }
  44. ],
  45. "source": [
  46. "!nvidia-smi"
  47. ]
  48. },
  49. {
  50. "cell_type": "code",
  51. "execution_count": 1,
  52. "metadata": {},
  53. "outputs": [
  54. {
  55. "name": "stdout",
  56. "output_type": "stream",
  57. "text": [
  58. "True\n"
  59. ]
  60. }
  61. ],
  62. "source": [
  63. "import torch\n",
  64. "\n",
  65. "print(torch.cuda.is_available())"
  66. ]
  67. },
  68. {
  69. "cell_type": "code",
  70. "execution_count": 2,
  71. "metadata": {},
  72. "outputs": [],
  73. "source": [
  74. "import time # times in seconds\n",
  75. "def time_torch(size):\n",
  76. " x = torch.rand(size, size, device=torch.device(\"cuda\"))\n",
  77. " start = time.time()\n",
  78. " x.sin_()\n",
  79. " end = time.time()\n",
  80. " return(end - start)\n",
  81. "\n",
  82. "def time_cpu(size):\n",
  83. " x = torch.rand(size, size, device=torch.device(\"cpu\"))\n",
  84. " start = time.time()\n",
  85. " x.sin_()\n",
  86. " end = time.time()\n",
  87. " return(end - start)"
  88. ]
  89. },
  90. {
  91. "cell_type": "code",
  92. "execution_count": 3,
  93. "metadata": {},
  94. "outputs": [
  95. {
  96. "name": "stdout",
  97. "output_type": "stream",
  98. "text": [
  99. "([0.00028634071350097656, 1.049041748046875e-05, 4.291534423828125e-06, 4.0531158447265625e-06], [3.9577484130859375e-05, 1.0728836059570312e-05, 1.0013580322265625e-05, 1.0013580322265625e-05])\n"
  100. ]
  101. }
  102. ],
  103. "source": [
  104. "def get_cuda_cpu_times(sizes):\n",
  105. " cpuTimes = []\n",
  106. " cudaTimes = []\n",
  107. "\n",
  108. " for s in sizes:\n",
  109. " cpuTimes += [time_cpu(s)]\n",
  110. " cudaTimes += [time_torch(s)]\n",
  111. " return cpuTimes, cudaTimes\n",
  112. "\n",
  113. "print(get_cuda_cpu_times(range(2, 10, 2)))"
  114. ]
  115. },
  116. {
  117. "cell_type": "code",
  118. "execution_count": 122,
  119. "metadata": {},
  120. "outputs": [],
  121. "source": [
  122. "import matplotlib.pyplot as plt\n",
  123. "\n",
  124. "\n",
  125. "def plot_cuda_vs_cpu(cpuTimes, cudaTimes, sizes, xLab=\"Matrix Width\"):\n",
  126. " plt.title(\"CUDA vs CPU\")\n",
  127. " cpu_plot = plt.plot(sizes, cpuTimes, label=\"CPU\")\n",
  128. " cuda_plot = plt.plot(sizes, cudaTimes, label=\"CUDA\")\n",
  129. " plt.legend(bbox_to_anchor=(0.8, 0.98), loc='upper left', borderaxespad=0.)\n",
  130. " # plt.legend(handles=[cpu_plot, cuda_plot])\n",
  131. " plt.xlabel(xLab)\n",
  132. " plt.ylabel('Execution Time (Seconds)')\n",
  133. " plt.show()"
  134. ]
  135. },
  136. {
  137. "cell_type": "code",
  138. "execution_count": 62,
  139. "metadata": {},
  140. "outputs": [
  141. {
  142. "data": {
  143. "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXgAAAEWCAYAAABsY4yMAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nO3deXxU9bn48c+TZCbJTEKAJOxgICCrgIK4gVVcqha3W9tqbWu9bb29t4v2tvZXf7e31t7urdb215Wqt7ZarG1F7WYVRXFF9n2HAAECCQGyTrZ5fn+cM9lIJifJTBJmnvfrNa+ZOefMmedkeeY7z/me71dUFWOMMYknpb8DMMYYEx+W4I0xJkFZgjfGmARlCd4YYxKUJXhjjElQluCNMSZBWYI3xpgEZQneDCgi8mERWS0iVSJyRET+ISLz3XW/EZFvttu+QERURNLc50UiUisilSJyUkTeEpFPi8hpf+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WnLuzsscFNTE5s2bWLq1Kls2rSJXbt2cdVVV1FQUMBTTz3FkiVLehxjPEs0VwL7VLVUVRuAZ4CL22+kqotVda6qzs3Pz+/RGzV3k7QSjTGmj3Q2XPDx48c9Dwvc0NDAfffdx9ixY5k5cyZLlizh61//OkVFRRQVFXH48GEOHz7M/v37exRjPBP8AeBCEQmI893kCmBbPN6ouZukteCNMX0kMlzwsmXLKCwsZPr06dx3332MGjWqeVjg2bNnc8stt5w2LPDtt9/OzJkzmTFjBtXV1Tz33HOAU3+/+eab27zPzTff3Dypd3fFswa/UkT+BKwFGoF1wOL
  144. "text/plain": [
  145. "<Figure size 432x288 with 1 Axes>"
  146. ]
  147. },
  148. "metadata": {
  149. "needs_background": "light"
  150. },
  151. "output_type": "display_data"
  152. }
  153. ],
  154. "source": [
  155. "sizes = range(1, 50, 1)\n",
  156. "cpu_t, cuda_t = get_cuda_cpu_times(sizes)\n",
  157. "plot_cuda_vs_cpu(cpu_t, cuda_t, sizes)"
  158. ]
  159. },
  160. {
  161. "cell_type": "code",
  162. "execution_count": 64,
  163. "metadata": {},
  164. "outputs": [
  165. {
  166. "data": {
  167. "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZIAAAEWCAYAAABMoxE0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOzdeXxU1d348c93JishhGzsSwIEZUcIiAvuWLQqWrFibUVra7X6tD4+bR/9PdaqXbW1i61Lca9WUakL7hu4CxKUfQ17ErYsZF9nvr8/7g0MIclMSCYb3/frNa/c7dw5ZzK539xzzj1HVBVjjDHmaHk6OgPGGGO6NgskxhhjWsUCiTHGmFaxQGKMMaZVLJAYY4xpFQskxhhjWsUCiTHGmFaxQGKOCSLyHRHJEpEyEdktIm+JyKnuvidF5DcNjk8TERW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1/O73//e/bu3dvseT0i/P7+eSz6fBmrVq0iOjqaWbMOf177ueeeY8qUKbz00ktBStwyVrVljDFNeetWdM8qEmt8pHoEbzON4RFAhs9PTZ2fukgPEZ7G/0+vSBpF6eTbGdg7lh6NDPseOIx8vQkTJlBUVMTSpUubzW6kV6iu9RPVK4Z7772XESNGsHLlSiZMmMCWLVsoKyvjwQcf5Le//S3XXHNNaJ9BCOyOxBhjmlHnV1QJqRE90it4BGp8/sN6dx06l5+KGh+JPaJIaqKarH4Y+aMRFeGlyu0C7PV6mTBhAhs2bABg/vz5zJkzh+nTp7Nx48agdzgtYXckxhjTlPP+wM59ZdT5lZF9e0KQ3lAC1FbWsKOggoG9Y0nueagRvab
  168. "text/plain": [
  169. "<Figure size 432x288 with 1 Axes>"
  170. ]
  171. },
  172. "metadata": {
  173. "needs_background": "light"
  174. },
  175. "output_type": "display_data"
  176. }
  177. ],
  178. "source": [
  179. "sizes = range(1, 5000, 100)\n",
  180. "cpu_t, cuda_t = get_cuda_cpu_times(sizes)\n",
  181. "plot_cuda_vs_cpu(cpu_t, cuda_t, sizes)"
  182. ]
  183. },
  184. {
  185. "cell_type": "code",
  186. "execution_count": 73,
  187. "metadata": {},
  188. "outputs": [
  189. {
  190. "data": {
  191. "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZ4AAAEGCAYAAABVSfMhAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOzdeXhU1fnA8e+byTIBAiEhYYcECCAoiwbcsCJuaFW0tRW7qbXaRbran9UuWrXWauvSRdvi0rpU0briVjdwVyCI7FuAICEQQnYge97fH/dOmCSTmUmYSUJ4P8+Th5kz9545NyR555zz3nNEVTHGGGM6S0xXN8AYY8yRxQKPMcaYTmWBxxhjTKeywGOMMaZTWeAxxh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F9Jp1eEuM81Bt6dTGGBNQqPt4jm1RNMq9+98E4Y2LobreOoTGGBNIqB7PXe6/XuA4YBXO3M4kIAc4MXpNO3zZDaTGGNO2oHM8qnqaqp4G7AKOc1dvPg6YShirQh+pfDeQhrMckTHGHGnCTS4Yp6qrfU9UdQ1wVHSadPhLcLdGqLHhNmOMaSXc5IJVIvIg8Lj7/Os4w24mgES/zeB8G8MZY4xxhBt4rgC+D/zYff4e8PeotKgHSIz3bQbXQHIXt8UYY7qbsAKPqlYD97hfJoSDPR4bajPGmJbCCjwicjLwW2Ck/zmqOio6zTq8eePcXUgts80YY1oJd6jtIeCnwHLA/pqG4JvXsdULjDGmtXADT7mqvhbVlvQg/skFxhhjmgs38CwWkT8CzwE1vkJV/TQqrTrMeS3wGGN
  192. "text/plain": [
  193. "<Figure size 432x288 with 1 Axes>"
  194. ]
  195. },
  196. "metadata": {
  197. "needs_background": "light"
  198. },
  199. "output_type": "display_data"
  200. }
  201. ],
  202. "source": [
  203. "# cpu_plot = plt.plot(sizes, cpuTimes, label=\"CPU\")\n",
  204. "cuda_plot = plt.plot(sizes, cuda_t, label=\"CUDA\")\n",
  205. "plt.legend(bbox_to_anchor=(0.8, 0.98), loc='upper left', borderaxespad=0.)\n",
  206. "# plt.legend(handles=[cpu_plot, cuda_plot])\n",
  207. "plt.xlabel('Matrix Size')\n",
  208. "plt.ylabel('Execution Time (Seconds)')\n",
  209. "plt.show()"
  210. ]
  211. },
  212. {
  213. "cell_type": "code",
  214. "execution_count": 85,
  215. "metadata": {},
  216. "outputs": [
  217. {
  218. "data": {
  219. "text/plain": [
  220. "0.00036406517028808594"
  221. ]
  222. },
  223. "execution_count": 85,
  224. "metadata": {},
  225. "output_type": "execute_result"
  226. }
  227. ],
  228. "source": [
  229. "import time # times in seconds\n",
  230. "def time_torch_copy(size):\n",
  231. " x = torch.rand(size, size, device=torch.device(\"cpu\"))\n",
  232. " start = time.time()\n",
  233. " x = x.cuda()\n",
  234. " x.sin_()\n",
  235. " end = time.time()\n",
  236. " return(end - start)\n",
  237. "time_torch_copy(2)"
  238. ]
  239. },
  240. {
  241. "cell_type": "code",
  242. "execution_count": 86,
  243. "metadata": {},
  244. "outputs": [
  245. {
  246. "data": {
  247. "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZIAAAEWCAYAAABMoxE0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOzdd3gWVfbA8e9JpwYIRXroVYgSqaKo2AtiA0VFRbCxa9nV1f25tl37Wta6IlYUEJCqsCgIdoHQqxAgQBJCSUISQnrO74+ZYAgpLyTvm5Ccz/O8T/LO3Jk5Y+Ewc+89V1QVY4wx5mT5VXYAxhhjTm2WSIwxxpSLJRJjjDHlYonEGGNMuVgiMcYYUy6WSIwxxpSLJRJjjDHlYonE1AgicpOIRInIYRHZKyILRORsd9/HIvKvIu3DRURFJMD9HiMiGSKSJiKHROQXEblbRI77f0hElopIsogE++jemovIB+59pYnIFhF5WkTquPtVRNLde48TkVdFxL/Qvo5FzveUiHzmi9hN9WCJxFR7IvIQ8DrwHNAMaAO8Aww7wVNdqar1gLbAC8DfgA+KXCscGAwocFV54vaEiDQCfgVqAQPc+C4EGgAdCjXtrap1gQuAm4Cx3o7N1ByWSEy1JiKhwDPAfao6U1XTVTVHVeep6sMnc05VTVHVucAIYLSI9Cy0+1bgN+BjYHQpcY0Qkagi2x4Ukbnu75eJyCb3CSNORP5awqkeAtKAm1U1xo1vj6rer6rriol9C/Aj0LPoPmNOliUSU90NAEKAWRV9YlVdDsTiPIEUuBX43P1cLCLNSjh8HtBFRDoV2nYTMNn9/QPgLvcJoyfwXQnnGQrMVNV8T2IWke5uvKs9aW+MJyyRmOouDDioqrleOn880AjA7XNpC0xT1ZXAdpzkcBxVPQLMAW50j+0EdAX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9ZIIeLGPxO3zGA8sxBn++6GqbhSRZ4AoVZ0LfABMEpFoIAkn2QCcDTwqIjlAPnCvqh4UkfbALDcjBwCTVfV/3roHY4ypbM7TiB9hdYIrO5QSeXUeiarOB+YX2fZEod8zgeuLOW4SMKmY7TuA3hUfqTHG+E6+KmmZudQLCcCvlFdVBU8jp4WGVNmnEajCne3GGFNdHTycxa7EdOIPlV7m/VR4GgFLJMYY41OqStLhbPxFSErP5kBa1nFtEhISuO6GEZzTpyc3Xj6EK6+4nAkTJhxTLh7gtttuOzoLfsiQIXTp0oVevXrRtWtXxo8fz6FDh45p//rrrxMSEkJKSkqF3pMlEmOM8aG0zFyy8/Jp1bAWDWoHkZCayaEj2Uf3F5SRjzhrIAt/XcuqlSt5/vnn2bdvX5nn/vzzz1m3bh3r1q0jODiYYcOGHbN/ypQpnHXWWcycObNC78mKNhpjTEkWPAoJ6yv0lBrahcABT1G/ViD1agWSk5fPnuQMAvz9qBscwJIlS/DzD+CqG0fTpF4Q/n5C7969SU5OZtmyZR5dIygoiJdeeomOHTuydu1aevfuzfbt2zl8+DDvvPMOzz77LLfffnuF3ZM9kRhjjI/kq5Kdp4TVCUJE8BOhbaPaBPn7sSsxncycPDZs2ECXnr0J8POjUTn6Rvz9/enduzdbtmwBYOrUqYwcOZLBgwfz+++/e/SE4yl7IjHGmJJc+kLZbU5AwqEMEtOz6Von6Oi2AH8/2jWuTfT+dGIOppORk0tObv7Rp5ECJU1ELG2CYuGO/ClTpjBr1iz8/Py49tprmT59OuPHj6+Au7JEYowxPpGXrySnZ9OgViCBRRaeCgrwJ7xxbXYcSKdRyw5snv7lcU8jYWFhJCcnH7OttHLxeXl5rF+/nm7durF+/Xq2bdvGhRdeCEB2djbt2rWrsERir7aMMcY
  248. "text/plain": [
  249. "<Figure size 432x288 with 1 Axes>"
  250. ]
  251. },
  252. "metadata": {
  253. "needs_background": "light"
  254. },
  255. "output_type": "display_data"
  256. }
  257. ],
  258. "source": [
  259. "def get_cuda_cpu_times_with_copy(sizes):\n",
  260. " cpuTimes = []\n",
  261. " cudaTimes = []\n",
  262. " for s in sizes:\n",
  263. " cpuTimes += [time_cpu(s)]\n",
  264. " cudaTimes += [time_torch_copy(s)]\n",
  265. " return cpuTimes, cudaTimes\n",
  266. "\n",
  267. "sizes = range(1, 5000, 100)\n",
  268. "cpu_t, cuda_t = get_cuda_cpu_times_with_copy(sizes)\n",
  269. "plot_cuda_vs_cpu(cpu_t, cuda_t, sizes)"
  270. ]
  271. },
  272. {
  273. "cell_type": "code",
  274. "execution_count": 88,
  275. "metadata": {},
  276. "outputs": [
  277. {
  278. "data": {
  279. "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYsAAAEWCAYAAACXGLsWAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOzdd3xV9f348dc7OwGSQJgJI8jeESJupa7iBJy4tVbqt7XV2qXf+rWOtta2WutPOxBsFRRUXGhR3FJRgbA3hBFI2Nk7ubnv3x/nBC8hyb1Abm7G+/l43Me955zPOfd9AuTN+UxRVYwxxpjGhIU6AGOMMS2fJQtjjDF+WbIwxhjjlyULY4wxflmyMMYY45clC2OMMX5ZsjDGGOOXJQvTpojIDSKSISIlIrJXRN4XkbPcY/8Wkd/WKZ8qIioiEe72ThEpF5FiESkQka9E5C4ROerfioh8LiL5IhLdTPfWS0RmuvdVLCKbROQREengHlcRKXXvPUdEnhKRcJ9jA+tc72ERmd0csZvWz5KFaTNE5D7gaeD3QA+gL/A3YNIxXupyVe0E9AP+APwKmFnnu1KBswEFrjiRuAMhIl2Ar4FY4HQ3vguBRGCAT9ExqtoROB+4Abgz2LGZ9sGShWkTRCQBeBT4kaq+qaqlqlqtqu+q6i+O55qqWqiq84HrgFtFZKTP4VuAb4B/A7c2Etd1IpJRZ99P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YmJijuk8G2dhjGl3evfuTXZ2NgcPHmyeL1R1lkStLIaoDhAbDQe2NM931yMmJobevetd0bp
  280. "text/plain": [
  281. "<Figure size 432x288 with 1 Axes>"
  282. ]
  283. },
  284. "metadata": {
  285. "needs_background": "light"
  286. },
  287. "output_type": "display_data"
  288. }
  289. ],
  290. "source": [
  291. "import time # times in seconds\n",
  292. "def time_torch_copy_and_back(size):\n",
  293. " x = torch.rand(size, size, device=torch.device(\"cpu\"))\n",
  294. " start = time.time()\n",
  295. " x = x.cuda()\n",
  296. " x.sin_()\n",
  297. " x = x.cpu()\n",
  298. " end = time.time()\n",
  299. " return(end - start)\n",
  300. "time_torch_copy(2)\n",
  301. "\n",
  302. "def get_cuda_cpu_times_with_copy(sizes):\n",
  303. " cpuTimes = []\n",
  304. " cudaTimes = []\n",
  305. " for s in sizes:\n",
  306. " cpuTimes += [time_cpu(s)]\n",
  307. " cudaTimes += [time_torch_copy_and_back(s)]\n",
  308. " return cpuTimes, cudaTimes\n",
  309. "\n",
  310. "sizes = range(1, 5000, 100)\n",
  311. "cpu_t, cuda_t = get_cuda_cpu_times_with_copy(sizes)\n",
  312. "plot_cuda_vs_cpu(cpu_t, cuda_t, sizes)"
  313. ]
  314. },
  315. {
  316. "cell_type": "code",
  317. "execution_count": 95,
  318. "metadata": {},
  319. "outputs": [],
  320. "source": [
  321. "def time_torch_operation_repetition(size, iterations):\n",
  322. " x = torch.rand(size, size, device=torch.device(\"cpu\"))\n",
  323. " start = time.time()\n",
  324. " x = x.cuda()\n",
  325. " for _ in range(0, iterations): \n",
  326. " x.sin()\n",
  327. " end = time.time()\n",
  328. " return(end - start)"
  329. ]
  330. },
  331. {
  332. "cell_type": "code",
  333. "execution_count": 101,
  334. "metadata": {},
  335. "outputs": [],
  336. "source": [
  337. "def time_cpu_operation_repetition(size, iterations):\n",
  338. " x = torch.rand(size, size, device=torch.device(\"cpu\"))\n",
  339. " start = time.time()\n",
  340. " for _ in range(0, iterations): \n",
  341. " x.sin()\n",
  342. " end = time.time()\n",
  343. " return(end - start)"
  344. ]
  345. },
  346. {
  347. "cell_type": "code",
  348. "execution_count": 126,
  349. "metadata": {},
  350. "outputs": [],
  351. "source": [
  352. "def get_cuda_cpu_times_with_iterations(iterations):\n",
  353. " cpuTimes = []\n",
  354. " cudaTimes = []\n",
  355. " for i in iterations:\n",
  356. " cpuTimes += [time_cpu_operation_repetition(300, i)]\n",
  357. " cudaTimes += [time_torch_operation_repetition(300, i)]\n",
  358. " return cpuTimes, cudaTimes"
  359. ]
  360. },
  361. {
  362. "cell_type": "code",
  363. "execution_count": 125,
  364. "metadata": {},
  365. "outputs": [
  366. {
  367. "data": {
  368. "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYgAAAEWCAYAAAB8LwAVAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOzdd3yV9dn48c91Tk72IglhQwJhT2U5wIEbUZwVbV21ov1Vq22f9qmtbbV92mpbrba1VSuoRQVH3aNWVKxaQEDZM0CAACEhe52TM76/P+47k4xDyMk4ud6v13lxzr1y3QHu63y3GGNQSimlmnJ0dQBKKaW6J00QSimlmqUJQimlVLM0QSillGqWJgillFLN0gShlFKqWZoglFJKNUsThAorInKdiKwVkQoROSwi74nILHvfMyLyf02OzxARIyIR9uccEakWkXIRKRGR/4rI7SJyzP8VEVkhIsUiEtVJ9zZARBbZ91UuIttF5H4RibP3GxGptO/9oIg8LCLOBvuymlzvPhF5rjNiVz2TJggVNkTk+8AjwG+AfsBQ4K/A/OO81CXGmARgGPAA8L/AoiY/KwOYDRjg0hOJOxgikgKsBGKAU+34zgOSgRENDp1sjIkHzgGuA24NdWwqfGmCUGFBRJKAXwLfMca8aoypNMZ4jTFvGWN+2J5rGmNKjTFvAtcAN4rIhAa7bwBWAc8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t5tqwa2hvE+y9a4JQSnWq+hKEo8uqmKKjoyksLOyVScIYQ2FhIdHR0W0eq2tSK6U6lcfrJzohimhX11UxDR48mNzcXHrr+Kn
  369. "text/plain": [
  370. "<Figure size 432x288 with 1 Axes>"
  371. ]
  372. },
  373. "metadata": {
  374. "needs_background": "light"
  375. },
  376. "output_type": "display_data"
  377. }
  378. ],
  379. "source": [
  380. "iterations = range(1, 500, 5)\n",
  381. "cpu_t, cuda_t = get_cuda_cpu_times_with_iterations(iterations)\n",
  382. "plot_cuda_vs_cpu(cpu_t, cuda_t, iterations, xLab=\"Number of Operations\")"
  383. ]
  384. }
  385. ],
  386. "metadata": {
  387. "kernelspec": {
  388. "display_name": "ml",
  389. "language": "python",
  390. "name": "ml"
  391. },
  392. "language_info": {
  393. "codemirror_mode": {
  394. "name": "ipython",
  395. "version": 3
  396. },
  397. "file_extension": ".py",
  398. "mimetype": "text/x-python",
  399. "name": "python",
  400. "nbconvert_exporter": "python",
  401. "pygments_lexer": "ipython3",
  402. "version": "3.8.3"
  403. }
  404. },
  405. "nbformat": 4,
  406. "nbformat_minor": 4
  407. }