Last active
September 10, 2020 10:05
-
-
Save nkpro2000sr/386b2af9a04450f6c46d9ce0ef0c4453 to your computer and use it in GitHub Desktop.
To build TensorFlow from source in colab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "build-tf-src-colab.ipynb", | |
"provenance": [], | |
"collapsed_sections": [] | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"# To Build TensorFlow from source" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Installing Python and dependencies" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! sudo apt update\n", | |
"! sudo apt install python3-dev python3-pip\n", | |
"! sudo apt autoremove\n", | |
"\n", | |
"! pip install six 'numpy<1.19.0' wheel setuptools mock 'future>=0.17.1' 'gast==0.3.3' typing_extensions\n", | |
"! pip install keras_applications --no-deps\n", | |
"! pip install keras_preprocessing --no-deps" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Setting Target CPU flags" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"run `gcc -march=native -Q --help=target > target.txt` \n", | |
"in your machine and upload **target.txt** below." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"from google.colab import files\n", | |
"files.upload() # target.txt" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# target.txt -> target.arg\n", | |
"\n", | |
"! wget https://gist.githubusercontent.com/nkpro2000sr/386b2af9a04450f6c46d9ce0ef0c4453/raw/get_target_args.py\n", | |
"! python get_target_args.py /content/target.txt /content/target.arg" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### To set target manually >" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"%%writefile /content/target.arg\n", | |
"\n", | |
"--copt=-mtune=generic" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## To get [TensorFlow](https://www.tensorflow.org/ \"HomePage\") [source](https://github.com/tensorflow/tensorflow/blob/master/.bazelversion \"from GitHub\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! git clone https://github.com/tensorflow/tensorflow.git /content/tensorflow\n", | |
"%cd /content/tensorflow" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## To install [Bazel](https://www.bazel.build/ \"HomePage\")\n", | |
"\n", | |
"https://github.com/bazelbuild/bazel" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# https://docs.bazel.build/versions/master/install-ubuntu.html\n", | |
"\n", | |
"! sudo apt install curl gnupg\n", | |
"! curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg\n", | |
"! sudo mv bazel.gpg /etc/apt/trusted.gpg.d/\n", | |
"! echo \"deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8\" | sudo tee /etc/apt/sources.list.d/bazel.list" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! sudo apt update && sudo apt install bazel-$(cat /content/tensorflow/.bazelversion)\n", | |
"! sudo ln -s /usr/bin/bazel-$(cat /content/tensorflow/.bazelversion) /usr/bin/bazel" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## GPU Support" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! sudo apt autoremove" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"%env NVIDIA_DRIVER = 390\n", | |
"%env CUDA = 9.0-1\n", | |
"%env CUDNN = 7.1.4-2\n", | |
"\n", | |
"print('\\njust a sanitary check :]\\n')\n", | |
"! echo libnvidia-gl-${NVIDIA_DRIVER}\n", | |
"! echo nvidia-driver-${NVIDIA_DRIVER}\n", | |
"! echo cuda-${CUDA}\n", | |
"! echo libcudnn${CUDNN%%[.|-]*}=${CUDNN}+cuda${CUDA%%[.|-]*}.${CUDA##*-}\n", | |
"! echo libcudnn${CUDNN%%[.|-]*}-dev=${CUDNN}+cuda${CUDA%%[.|-]*}.${CUDA##*-}" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### CUDA" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# Install development and runtime libraries (~4GB)\n", | |
"! sudo apt-get install --no-install-recommends cuda-${CUDA}\n", | |
"! sudo apt autoremove" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"#### CUDA from archive" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! wget https://developer.nvidia.com/compute/cuda/9.0/Prod/local_installers/cuda-repo-ubuntu1704-9-0-local_9.0.176-1_amd64-deb -P /content/" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! sudo dpkg -i /content/cuda-repo*\n", | |
"! sudo apt-key add /var/cuda-repo-*-local/*.pub\n", | |
"! sudo apt-get update\n", | |
"! sudo apt-get install --no-install-recommends cuda-${CUDA%%-*}\n", | |
"! sudo apt-get autoremove" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### CUDNN" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! sudo apt-get install --no-install-recommends libcudnn${CUDNN%%[.|-]*}=${CUDNN}+cuda${CUDA%%[.|-]*}.${CUDA##*-}\n", | |
"! sudo apt-get install --no-install-recommends libcudnn${CUDNN%%[.|-]*}-dev=${CUDNN}+cuda${CUDA%%[.|-]*}.${CUDA##*-}" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"#### CUDNN from archive\n", | |
"https://developer.nvidia.com/cudnn" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! wget http://developer.download.nvidia.com/compute/redist/cudnn/v7.1.4/cudnn-9.0-linux-x64-v7.1.tgz -P /content" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! tar -xzvf /content/cudnn* -C /content/\n", | |
"\n", | |
"! sudo cp /content/cuda/include/* /usr/local/cuda/include/\n", | |
"! sudo cp /content/cuda/lib64/* /usr/local/cuda/lib64/" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### Nvidia Drivers" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# Install NVIDIA driver\n", | |
"! sudo apt-get install --no-install-recommends libnvidia-gl-${NVIDIA_DRIVER}\n", | |
"! sudo apt-get install --no-install-recommends nvidia-driver-${NVIDIA_DRIVER}\n", | |
"! sudo apt-get install --no-install-recommends nvidia-${NVIDIA_DRIVER}" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Configuring" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! cd /content/tensorflow && ./configure" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Building PIP Package" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! bazel build --config=cuda $(cat /content/target.arg) //tensorflow/tools/pip_package:build_pip_package" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"! mv /tmp/tensorflow_pkg/tensorflow*.whl " | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
with open(sys.argv[1], 'r') as target_txt: | |
targets = target_txt.read() | |
targets = targets.partition('\n\n')[0] | |
targets = targets.splitlines()[1:] | |
copts = [] | |
for target in targets: | |
arg = target.split() | |
if len(arg) == 1: | |
continue | |
elif arg[1] == '[disabled]': | |
continue | |
elif arg[1] == '[default]': | |
continue | |
elif arg[1] == '[enabled]': | |
copts.append(arg[0]) | |
else: | |
copts.append(''.join(arg)) | |
with open(sys.argv[2], 'w') as target_arg: | |
target_arg.write(' '.join(['--copt='+copt for copt in copts])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try Now 😉