From 8296fa1a401c97a1449ebce867bcd34f98ae5b3d Mon Sep 17 00:00:00 2001 From: Sugarme Date: Fri, 13 Nov 2020 11:21:25 +1100 Subject: [PATCH] Created basic Gophernotes GoTch on Google Colab --- example/basic/gotch.ipynb | 326 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 326 insertions(+) create mode 100644 example/basic/gotch.ipynb diff --git a/example/basic/gotch.ipynb b/example/basic/gotch.ipynb new file mode 100644 index 0000000..551ec56 --- /dev/null +++ b/example/basic/gotch.ipynb @@ -0,0 +1,326 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "accelerator": "GPU", + "colab": { + "name": "gotch.ipynb", + "provenance": [], + "collapsed_sections": [], + "include_colab_link": true + }, + "kernelspec": { + "display_name": "Go", + "name": "gophernotes" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "caT1iMfshw62", + "outputId": "6429d196-5df6-4565-a392-150935f57a33", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "source": [ + "# run this cell first time using python runtime\n", + "!add-apt-repository ppa:longsleep/golang-backports -y\n", + "!apt update\n", + "!apt install golang-go\n", + "%env GOPATH=/root/go\n", + "!go get -u github.com/gopherdata/gophernotes\n", + "!cp ~/go/bin/gophernotes /usr/bin/\n", + "!mkdir /usr/local/share/jupyter/kernels/gophernotes\n", + "!cp ~/go/src/github.com/gopherdata/gophernotes/kernel/* \\\n", + " /usr/local/share/jupyter/kernels/gophernotes\n", + "# then refresh (browser), it will now use gophernotes. Skip to golang in later cells" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "\r0% [Working]\r \rGet:1 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease [3,626 B]\n", + "Ign:2 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease\n", + "Ign:3 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease\n", + "Get:4 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release [697 B]\n", + "Get:5 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release [564 B]\n", + "Get:6 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release.gpg [836 B]\n", + "Get:7 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]\n", + "Get:8 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release.gpg [833 B]\n", + "Get:9 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease [15.9 kB]\n", + "Hit:10 http://archive.ubuntu.com/ubuntu bionic InRelease\n", + "Get:11 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]\n", + "Ign:12 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages\n", + "Get:12 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages [407 kB]\n", + "Get:13 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Packages [58.5 kB]\n", + "Get:14 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease [21.3 kB]\n", + "Get:15 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]\n", + "Get:16 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [222 kB]\n", + "Get:17 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic InRelease [15.4 kB]\n", + "Get:18 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [15.8 kB]\n", + "Get:19 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [1,781 kB]\n", + "Get:20 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [247 kB]\n", + "Get:21 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main Sources [1,688 kB]\n", + "Get:22 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1,365 kB]\n", + "Get:23 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2,129 kB]\n", + "Get:24 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [46.3 kB]\n", + "Get:25 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [2,198 kB]\n", + "Get:26 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main amd64 Packages [864 kB]\n", + "Get:27 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic/main amd64 Packages [46.6 kB]\n", + "Get:28 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 Packages [5,494 B]\n", + "Fetched 11.4 MB in 4s (2,606 kB/s)\n", + "Reading package lists... Done\n", + "Ign:1 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease\n", + "Hit:2 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease\n", + "Ign:3 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease\n", + "Hit:4 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release\n", + "Hit:5 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release\n", + "Hit:6 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease\n", + "Hit:7 http://archive.ubuntu.com/ubuntu bionic InRelease\n", + "Hit:9 http://security.ubuntu.com/ubuntu bionic-security InRelease\n", + "Hit:10 http://archive.ubuntu.com/ubuntu bionic-updates InRelease\n", + "Hit:11 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease\n", + "Hit:13 http://archive.ubuntu.com/ubuntu bionic-backports InRelease\n", + "Hit:14 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic InRelease\n", + "Reading package lists... Done\n", + "Building dependency tree \n", + "Reading state information... Done\n", + "40 packages can be upgraded. Run 'apt list --upgradable' to see them.\n", + "Reading package lists... Done\n", + "Building dependency tree \n", + "Reading state information... Done\n", + "The following additional packages will be installed:\n", + " golang-1.15-go golang-1.15-src golang-src\n", + "Suggested packages:\n", + " bzr | brz mercurial subversion\n", + "The following NEW packages will be installed:\n", + " golang-1.15-go golang-1.15-src golang-go golang-src\n", + "0 upgraded, 4 newly installed, 0 to remove and 40 not upgraded.\n", + "Need to get 61.3 MB of archives.\n", + "After this operation, 358 MB of additional disk space will be used.\n", + "Get:1 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-1.15-src amd64 1.15.2-1longsleep1+bionic [13.8 MB]\n", + "Get:2 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-1.15-go amd64 1.15.2-1longsleep1+bionic [47.4 MB]\n", + "Get:3 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-src amd64 2:1.15-1longsleep1+bionic [3,924 B]\n", + "Get:4 http://ppa.launchpad.net/longsleep/golang-backports/ubuntu bionic/main amd64 golang-go amd64 2:1.15-1longsleep1+bionic [23.0 kB]\n", + "Fetched 61.3 MB in 6s (10.1 MB/s)\n", + "Selecting previously unselected package golang-1.15-src.\n", + "(Reading database ... 144786 files and directories currently installed.)\n", + "Preparing to unpack .../golang-1.15-src_1.15.2-1longsleep1+bionic_amd64.deb ...\n", + "Unpacking golang-1.15-src (1.15.2-1longsleep1+bionic) ...\n", + "Selecting previously unselected package golang-1.15-go.\n", + "Preparing to unpack .../golang-1.15-go_1.15.2-1longsleep1+bionic_amd64.deb ...\n", + "Unpacking golang-1.15-go (1.15.2-1longsleep1+bionic) ...\n", + "Selecting previously unselected package golang-src.\n", + "Preparing to unpack .../golang-src_2%3a1.15-1longsleep1+bionic_amd64.deb ...\n", + "Unpacking golang-src (2:1.15-1longsleep1+bionic) ...\n", + "Selecting previously unselected package golang-go.\n", + "Preparing to unpack .../golang-go_2%3a1.15-1longsleep1+bionic_amd64.deb ...\n", + "Unpacking golang-go (2:1.15-1longsleep1+bionic) ...\n", + "Setting up golang-1.15-src (1.15.2-1longsleep1+bionic) ...\n", + "Setting up golang-1.15-go (1.15.2-1longsleep1+bionic) ...\n", + "Setting up golang-src (2:1.15-1longsleep1+bionic) ...\n", + "Setting up golang-go (2:1.15-1longsleep1+bionic) ...\n", + "Processing triggers for man-db (2.8.3-2ubuntu0.1) ...\n", + "env: GOPATH=/root/go\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "KcanpJSUcIWD", + "outputId": "2ca19b73-2f60-4205-ee75-e0ff8bce3bcb", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "source": [ + "$wget -q --show-progress --progress=bar:force:noscroll -O /tmp/libtorch-cxx11-abi-shared-with-deps-1.7.0%2Bcu101.zip https://download.pytorch.org/libtorch/cu101/libtorch-cxx11-abi-shared-with-deps-1.7.0%2Bcu101.zip" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/tmp/libtorch-cxx11 100%[===================>] 765.61M 26.8MB/s in 24s \n" + ], + "name": "stderr" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "u4nbhDPVcV12" + }, + "source": [ + "$unzip -qq /tmp/libtorch-cxx11-abi-shared-with-deps-1.7.0%2Bcu101.zip -d /usr/local" + ], + "execution_count": 2, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "l6aOKEMarRNH" + }, + "source": [ + "NOTE: `ldconfig` (GLIBC) current version 2.27 is currently broken when linking Libtorch library\n", + "\n", + "see issue: https://discuss.pytorch.org/libtorch-c-so-files-truncated-error-when-ldconfig/46404/6\n", + "\n", + "Google Colab default settings:\n", + "```bash\n", + "LD_LIBRARY_PATH=/usr/lib64-nvidia\n", + "LIBRARY_PATH=/usr/local/cuda/lib64/stubs\n", + "```\n", + "We copy directly `libtorch/lib` to those paths as a hacky way. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "0N_jGbVft2Y7" + }, + "source": [ + "$unzip -qq -j /tmp/libtorch-cxx11-abi-shared-with-deps-1.7.0%2Bcu101.zip libtorch/lib/* -d /usr/lib64-nvidia/\n", + "$unzip -qq -j /tmp/libtorch-cxx11-abi-shared-with-deps-1.7.0%2Bcu101.zip libtorch/lib/* -d /usr/local/cuda/lib64/stubs/" + ], + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "AEL6FswvnLeQ" + }, + "source": [ + "import(\n", + " \"os\"\n", + ")\n", + "\n", + "//os.Setenv(\"LD_LIBRARY_PATH\", \"/usr/lib64-nvidia:/usr/local/libtorch/lib\")\n", + "//os.Setenv(\"LIBRARY_PATH\", \"/usr/local/cuda/lib64/stubs:/usr/local/libtorch/lib\")\n", + "os.Setenv(\"CPATH\", \"usr/local/libtorch/lib:/usr/local/libtorch/include:/usr/local/libtorch/include/torch/csrc/api/include\")" + ], + "execution_count": 4, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "4gfyTd0Pa2dD", + "outputId": "6001b795-153a-4f21-e775-7560a2f64870", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "source": [ + "$go mod init github.com/sugarme/playgo\n", + "$go get github.com/sugarme/gotch@v0.3.2" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "go: creating new go.mod: module github.com/sugarme/playgo\n", + "go: downloading github.com/sugarme/gotch v0.3.2\n" + ], + "name": "stderr" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "pN_I3xFZcnXX" + }, + "source": [ + "import(\n", + " \"fmt\"\n", + " \n", + " \"github.com/sugarme/gotch\"\n", + " ts \"github.com/sugarme/gotch/tensor\"\n", + ")" + ], + "execution_count": 6, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "sYyKlDWchSv3", + "outputId": "bc8b2321-2587-4cd1-db92-38c511fd6b79", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "source": [ + "device := gotch.NewCuda().CudaIfAvailable()\n", + "ts1, err := ts.Rand([]int64{3, 4, 5}, gotch.Double, device)\n", + "if err != nil {fmt.Print(err)}\n", + "ts2, err := ts.Ones([]int64{5, 3}, gotch.Double, device)\n", + "if err != nil { fmt.Print(err)}\n", + "\n", + "mul, err := ts1.Matmul(ts2, false)\n", + "if err != nil { fmt.Print(err)}\n", + "fmt.Printf(\"%.3f\", mul)" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "(1,.,.) =\n", + "4.388 4.388 4.388 \n", + "2.004 2.004 2.004 \n", + "2.285 2.285 2.285 \n", + "2.497 2.497 2.497 \n", + "\n", + "(2,.,.) =\n", + "2.528 2.528 2.528 \n", + "2.284 2.284 2.284 \n", + "2.119 2.119 2.119 \n", + "2.676 2.676 2.676 \n", + "\n", + "(3,.,.) =\n", + "3.737 3.737 3.737 \n", + "1.997 1.997 1.997 \n", + "1.577 1.577 1.577 \n", + "3.629 3.629 3.629 \n", + "\n" + ], + "name": "stdout" + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "297 " + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 7 + } + ] + } + ] +} \ No newline at end of file