{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "torch test.ipynb", "version": "0.3.2", "views": {}, "default_view": {}, "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "accelerator": "GPU" }, "cells": [ { "metadata": { "id": "w7X8BE2Rz_RT", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 94 }, "outputId": "37075520-3657-42a7-ae0a-bc2282395bca", "executionInfo": { "status": "ok", "timestamp": 1528841886286, "user_tz": -180, "elapsed": 28281, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "# http://pytorch.org/\n", "from os import path\n", "from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag\n", "platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())\n", "\n", "accelerator = 'cu80' if path.exists('/opt/bin/nvidia-smi') else 'cpu'\n", "\n", "!pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.4.0-{platform}-linux_x86_64.whl torchvision" ], "execution_count": 63, "outputs": [ { "output_type": "stream", "text": [ "tcmalloc: large alloc 1073750016 bytes == 0x5bc2e000 @ 0x7f16d1ff21c4 0x46d6a4 0x5fcbcc 0x4c494d 0x54f3c4 0x553aaf 0x54e4c8 0x54f4f6 0x553aaf 0x54efc1 0x54f24d 0x553aaf 0x54efc1 0x54f24d 0x553aaf 0x54efc1 0x54f24d 0x551ee0 0x54e4c8 0x54f4f6 0x553aaf 0x54efc1 0x54f24d 0x551ee0 0x54efc1 0x54f24d 0x551ee0 0x54e4c8 0x54f4f6 0x553aaf 0x54e4c8\r\n" ], "name": "stdout" } ] }, { "metadata": { "id": "sFPFKKP696Jc", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "48ae9ae9-c37c-4ed4-c48e-662b07fb4639", "executionInfo": { "status": "ok", "timestamp": 1528841915996, "user_tz": -180, "elapsed": 7753, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "import torch\n", "torch.__version__" ], "execution_count": 1, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'0.4.0'" ] }, "metadata": { "tags": [] }, "execution_count": 1 } ] }, { "metadata": { "id": "zPOw0zHPzvHX", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "b0bb75be-85d1-4624-c78c-25f1d493be3e", "executionInfo": { "status": "ok", "timestamp": 1528841916889, "user_tz": -180, "elapsed": 866, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "torch.cuda.is_available()" ], "execution_count": 2, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 2 } ] }, { "metadata": { "id": "3kyjp_kl0MJv", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "1e404914-1d21-4e9c-8f78-88a5810b99be", "executionInfo": { "status": "ok", "timestamp": 1528837500935, "user_tz": -180, "elapsed": 798, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "torch.cuda.device_count()" ], "execution_count": 6, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1" ] }, "metadata": { "tags": [] }, "execution_count": 6 } ] }, { "metadata": { "id": "K3aagsQ2zwl_", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "905fb756-f001-4719-9021-af8912662ccc", "executionInfo": { "status": "ok", "timestamp": 1528840049268, "user_tz": -180, "elapsed": 1766, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "dev = torch.cuda.current_device()\n", "dev" ], "execution_count": 4, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0" ] }, "metadata": { "tags": [] }, "execution_count": 4 } ] }, { "metadata": { "id": "QFgbjh7KzzMF", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "cb416436-4cfc-4d84-a0d2-6c50c6edc81d", "executionInfo": { "status": "ok", "timestamp": 1528840050259, "user_tz": -180, "elapsed": 783, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "torch.cuda.get_device_name(dev)" ], "execution_count": 5, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Tesla K80'" ] }, "metadata": { "tags": [] }, "execution_count": 5 } ] }, { "metadata": { "id": "QQBG2fyy0Jtz", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "b0084cf5-29c2-4a1d-e0c4-ef905dcc7cf7", "executionInfo": { "status": "ok", "timestamp": 1528841918133, "user_tz": -180, "elapsed": 1158, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "import numpy as np\n", "np.__version__" ], "execution_count": 3, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'1.14.3'" ] }, "metadata": { "tags": [] }, "execution_count": 3 } ] }, { "metadata": { "id": "GmgvJy4r0kM5", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "n = 1000\n", "dtype = 'float32'" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "bntIHmNA1Be9", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 136 }, "outputId": "62f6b21d-d357-4227-da7f-25cdc6b1b416", "executionInfo": { "status": "ok", "timestamp": 1528838781984, "user_tz": -180, "elapsed": 1039, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "x = np.random.rand(n, n).astype(dtype)\n", "y = np.random.rand(n, n).astype(dtype)\n", "print(x)" ], "execution_count": 44, "outputs": [ { "output_type": "stream", "text": [ "[[0.64993143 0.54426557 0.4271511 ... 0.63773596 0.35977545 0.47273144]\n", " [0.10438471 0.4131443 0.4897633 ... 0.25540075 0.650905 0.9069924 ]\n", " [0.2869643 0.6781323 0.76321834 ... 0.55297124 0.2495685 0.347869 ]\n", " ...\n", " [0.65938854 0.9135049 0.55126035 ... 0.3452831 0.37183738 0.70037305]\n", " [0.75186837 0.8833698 0.67125374 ... 0.15030158 0.11645979 0.15519282]\n", " [0.8473455 0.7309286 0.38008705 ... 0.7080732 0.81976247 0.6249228 ]]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "lBI8NDdG1DMd", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 153 }, "outputId": "0d122439-5023-4e18-bba6-7ca141517dc4", "executionInfo": { "status": "ok", "timestamp": 1528838783468, "user_tz": -180, "elapsed": 1328, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "z = x @ y\n", "print(z)\n", "print(type(z))" ], "execution_count": 45, "outputs": [ { "output_type": "stream", "text": [ "[[262.6432 268.94064 249.42052 ... 257.21527 254.57413 257.5146 ]\n", " [254.75009 264.89435 244.59337 ... 255.7371 248.91745 254.29904]\n", " [259.03632 264.5193 246.06383 ... 254.04399 246.53589 254.04001]\n", " ...\n", " [266.98166 266.02438 251.76076 ... 258.45422 256.73703 264.81305]\n", " [249.16425 253.55284 240.94025 ... 245.26656 239.66714 249.19298]\n", " [253.71408 257.41022 238.84543 ... 248.72 242.68701 252.66364]]\n", "\n" ], "name": "stdout" } ] }, { "metadata": { "id": "4Y2-Tv4r1FRC", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 187 }, "outputId": "30e0fb22-0998-4ff2-9045-608f5273d32d", "executionInfo": { "status": "ok", "timestamp": 1528838784498, "user_tz": -180, "elapsed": 891, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "a = torch.from_numpy(x).cuda()\n", "b = torch.from_numpy(y).cuda()\n", "print(a)" ], "execution_count": 46, "outputs": [ { "output_type": "stream", "text": [ "\n", " 6.4993e-01 5.4427e-01 4.2715e-01 ... 6.3774e-01 3.5978e-01 4.7273e-01\n", " 1.0438e-01 4.1314e-01 4.8976e-01 ... 2.5540e-01 6.5091e-01 9.0699e-01\n", " 2.8696e-01 6.7813e-01 7.6322e-01 ... 5.5297e-01 2.4957e-01 3.4787e-01\n", " ... ⋱ ... \n", " 6.5939e-01 9.1350e-01 5.5126e-01 ... 3.4528e-01 3.7184e-01 7.0037e-01\n", " 7.5187e-01 8.8337e-01 6.7125e-01 ... 1.5030e-01 1.1646e-01 1.5519e-01\n", " 8.4735e-01 7.3093e-01 3.8009e-01 ... 7.0807e-01 8.1976e-01 6.2492e-01\n", "[torch.cuda.FloatTensor of size 1000x1000 (GPU 0)]\n", "\n" ], "name": "stdout" } ] }, { "metadata": { "id": "7l6jkp171HOM", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 204 }, "outputId": "0130699c-c74c-490e-deae-2927f0347f97", "executionInfo": { "status": "ok", "timestamp": 1528838785522, "user_tz": -180, "elapsed": 874, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "c = a @ b\n", "print(c)\n", "print(type(c))" ], "execution_count": 47, "outputs": [ { "output_type": "stream", "text": [ "\n", " 262.6434 268.9405 249.4204 ... 257.2153 254.5742 257.5147\n", " 254.7500 264.8945 244.5935 ... 255.7372 248.9176 254.2992\n", " 259.0364 264.5194 246.0638 ... 254.0441 246.5359 254.0401\n", " ... ⋱ ... \n", " 266.9815 266.0244 251.7610 ... 258.4541 256.7370 264.8131\n", " 249.1642 253.5528 240.9402 ... 245.2666 239.6672 249.1931\n", " 253.7141 257.4101 238.8456 ... 248.7199 242.6871 252.6635\n", "[torch.cuda.FloatTensor of size 1000x1000 (GPU 0)]\n", "\n", "\n" ], "name": "stdout" } ] }, { "metadata": { "id": "1KJJ7OXG1Ik1", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "88cde71e-b334-413e-de29-6600e813d399", "executionInfo": { "status": "ok", "timestamp": 1528838786760, "user_tz": -180, "elapsed": 1046, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "np.allclose(c.cpu().numpy(), z)" ], "execution_count": 48, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 48 } ] }, { "metadata": { "id": "1Ocu0Z3R1KIU", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 51 }, "outputId": "c2fe936f-a9cb-4430-800d-023b417da94e", "executionInfo": { "status": "ok", "timestamp": 1528838803983, "user_tz": -180, "elapsed": 17080, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "%timeit z = x @ y; z /= z.sum()\n", "%timeit c = a @ b; c /= c.sum()" ], "execution_count": 49, "outputs": [ { "output_type": "stream", "text": [ "100 loops, best of 3: 29.7 ms per loop\n", "1000 loops, best of 3: 1.22 ms per loop\n" ], "name": "stdout" } ] }, { "metadata": { "id": "FFJCrCTR2UON", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "72131aea-7d3d-411c-dfeb-57824e5edfb2", "executionInfo": { "status": "ok", "timestamp": 1528838844466, "user_tz": -180, "elapsed": 2221, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "(29.7 * 1e-3) / (1.22 * 1e-3)" ], "execution_count": 51, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "24.34426229508197" ] }, "metadata": { "tags": [] }, "execution_count": 51 } ] }, { "metadata": { "id": "BtggWw956_Ph", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "N = int(1e6)\n", "n = 10\n", "dtype = 'float32'\n", "\n", "x = np.random.random(n).astype(dtype)\n", "y = torch.from_numpy(x).cuda()" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "r_zwzqjM7BDC", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 51 }, "outputId": "8c6fc65b-435e-419a-a335-e701b0c0758c", "executionInfo": { "status": "ok", "timestamp": 1528842056059, "user_tz": -180, "elapsed": 4306, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "%%timeit\n", "i = 0\n", "np.random.multinomial(N, x/x.sum())" ], "execution_count": 15, "outputs": [ { "output_type": "stream", "text": [ "The slowest run took 118.90 times longer than the fastest. This could mean that an intermediate result is being cached.\n", "100000 loops, best of 3: 8.22 µs per loop\n" ], "name": "stdout" } ] }, { "metadata": { "id": "7SYf8KglFu5x", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "base_uri": "https://localhost:8080/", "height": 34 }, "outputId": "770af45c-d3bd-4f37-b89c-f3084fd81a5e", "executionInfo": { "status": "ok", "timestamp": 1528842092175, "user_tz": -180, "elapsed": 802, "user": { "displayName": "Yoav Ram", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128", "userId": "117191630927154602006" } } }, "cell_type": "code", "source": [ "\n", "np.random.multinomial(N, x/x.sum()).shape" ], "execution_count": 18, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(10,)" ] }, "metadata": { "tags": [] }, "execution_count": 18 } ] }, { "metadata": { "id": "92Sc6Nh721K2", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "%%timeit -n 10 -r 3\n", "m = torch.distributions.multinomial.Multinomial(N, y)\n", "m.sample() / N" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "uPUS3mfIDltz", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "cell_type": "code", "source": [ "" ], "execution_count": 0, "outputs": [] } ] }