Skip to content

Instantly share code, notes, and snippets.

@johndpope
Forked from choongng/notes.md
Created August 19, 2019 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johndpope/6862b7f440a8f887842b6cc0ffdcd589 to your computer and use it in GitHub Desktop.
Save johndpope/6862b7f440a8f887842b6cc0ffdcd589 to your computer and use it in GitHub Desktop.
Swift for TensorFlow quick start with Docker on Mac

A good way to get a taste of Swift for Tensorflow language and tools is to set it up with Jupyter with the fastai Swift notebooks. I wanted a quick setup, which the Mac install experience currently not, so instead I installed the release binaries in a Ubuntu container via Docker. The setup process for this scenario is not well documented, so here it is for you / future me.

What we're about to do is install the S4TF 0.4 release and the fastai v3 Swift notebooks on Ubuntu 18.04. Generally we follow the swift-jupyter docker file, but install cpu-only release versions of the packages.

Below are some of the references I looked at:

Rationale for S4TF and background reading

https://github.com/tensorflow/swift/blob/master/docs/WhySwiftForTensorFlow.md https://github.com/tensorflow/swift/blob/master/docs/DifferentiableFunctions.md https://github.com/tensorflow/swift/blob/master/docs/PythonInteroperability.md

Google's swift-jupyter readme and Dockerfile, this appears to be used by Google CI:

General Docker guide

Jeremy Howard's gist

James Thompson article from March

Page with links to official prebuilt packages:

Docker setup

If you're already on the supported Ubuntu 18.04 don't really need Docker.

For Docker Hub you'll need to be logged in:

docker login

Pull the Ubuntu image:

docker pull ubuntu:18.04

Output should have some Pull complete messages ending with something like this:

Status: Downloaded newer image for ubuntu:18.04
docker.io/library/ubuntu:18.04

Verify download:

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              18.04               a2a15febcdf3        22 hours ago         64.2MB
$ docker run --rm ubuntu:18.04 uname -a
Linux f0c696b0c42e 4.9.184-linuxkit #1 SMP Tue Jul 2 22:58:16 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Some additional notes on Docker command line options:

Fully qualified path is necessary to get Mac Docker to plumb the mount all the way through to the host. t and i attach to terminal and run as interactive. priv is needed for among other things debuggers to work (necessary for REPL and Jupyter).

$ docker create -t -i --privileged -v $(pwd)/sharedfiles:/shared -p 8889:8888 ubuntu:18.04 bash
4c5c59fb01a2e1c07edf38624acc5f6b541ad3b9c33420e898f1801ade3a2d03
$ export my_s4tf_container=4c5c59fb01a2
$ docker start $my_s4tf_container
4c5c59fb01a2
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                  PORTS                    NAMES
4c5c59fb01a2        ubuntu:18.04        "bash"              57 seconds ago      Up Less than a second   0.0.0.0:8889->8888/tcp   romantic_cartwright

Attach to the running container:

docker attach $my_s4tf_container

To detach: ctrl-p,ctrl-q

Install some the dependencies. Note that we skip graphviz due to its X11 dependency.

$ apt update && apt install -y libvorbis-dev libflac-dev libsndfile-dev cmake build-essential libgflags-dev libgoogle-glog-dev libgtest-dev google-mock zlib1g-dev libeigen3-dev libboost-all-dev libasound2-dev libogg-dev libtool libfftw3-dev libbz2-dev liblzma-dev libgoogle-glog0v5 gcc-6 gfortran-6 g++-6 doxygen libsox-fmt-all parallel exuberant-ctags python-powerline python3-pip curl

Set up Python

apt install python-pip

Install Swift dependencies:

$ apt install -y git cmake ninja-build clang python uuid-dev libicu-dev icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev swig libpython-dev libncurses5-dev pkg-config libblocksruntime-dev libcurl4-openssl-dev systemtap-sdt-dev tzdata rsync

Install the latest binary release of S4TF:

$ cd ~
$ curl -O https://storage.googleapis.com/swift-tensorflow-artifacts/releases/v0.4/rc4/swift-tensorflow-RELEASE-0.4-ubuntu18.04.tar.gz
$ mkdir swift
$ tar zxf swift-tensorflow-RELEASE-0.4-ubuntu18.04.tar.gz --directory swift
$ echo 'export PATH=~/swift/bin:$PATH' >> ~/.bashrc
$ source ~/.bashrc

Run Swift interpreter to verify install is ok (ctrl-D to exit):

$ swift
Welcome to Swift version 5.1-dev (LLVM af1f73e9e9, Swift 7d157f346b).
Type :help for assistance.
  1>  

Set up Swift for Jupyter:

$ pip3 install jupyter matplotlib
$ git clone https://github.com/google/swift-jupyter.git
$ cd swift-jupyter
$ python3 register.py --user --swift-toolchain ~/swift --swift-python-library /usr/lib/x86_64-linux-gnu/libpython3.6m.so --kernel-name "Swift"

You should see some JSON printed to the terminal ending at Registered kernel 'Swift' as 'swift'!

Get fastai course v3 notebooks and launch Jupyter:

$ cd /shared
$ git clone https://github.com/fastai/course-v3.git
$ jupyter notebook --allow-root --ip=0.0.0.0 --port=8888

Point your browser to http://localhost:8889 (the port specified when creating the container) and copy/paste the token=... value ‘ where prompted by the Jupyter login page. You can create a new notebook or look at the fastai notebooks visible here:

Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment