Swift-Colab Development Repository

April 6, 2022 · View on GitHub

This is a repository for designing the new Swift-Colab. It will be rebuilt from the ground up to rely less on Python libraries, eliminate all Python code from the GitHub repository, and improve launch speed.

Snapshot of the previous README:

Swift-Colab

In March 2021, Google ended built-in Swift support on Colaboratory as part of their attempt to end S4TF. Less than a year later, the open-source community is resurrecting S4TF, and Colab support is a vital component of that effort. It allows testing on TPUs and ensuring new modifications don't break existing hardware acceleration.

Furthermore, Colab is a very accessible way to do programming with Swift. It runs instantly without downloading an IDE, and it can even run on a Chromebook or mobile device.

How to run Swift on Google Colaboratory

Copy this template of a Swift Colab notebook. Do not create one directly from Google Drive, as notebooks are configured for Python by default. Copy the following command into the first code cell and run it:

!curl "https://raw.githubusercontent.com/philipturner/swift-colab/release/latest/install_swift.sh" --output "install_swift.sh" && bash "install_swift.sh" "5.5.2" #// Replace 5.5.2 with newest Swift version
#// After this command finishes, go to Runtime > Restart runtime.

In the output stream, you will see:

=== Downloading Swift ===
...
=== Swift successfully downloaded ===
...
=== Swift successfully installed ===

You will be instructed to restart the runtime. This is necessary because it shuts down the Python kernel and starts the Swift kernel.

Tip: If you factory reset the runtime or exceed the time limit, Colab will restart in Python mode. Just re-run the first code cell to return to Swift mode.

Type the following code into the second code cell:

Int.bitWidth

After running it, the following output appears:

64

For further guidance on how to use Swift on Google Colab, check out the usage instructions on google/swift-jupyter. You must use Swift on Google Colab in a slightly different way than described on google/swift-jupyter. The differences are explained in the rest of this document.

Installing packages

To use Python interop or automatic differentiation, you must explicitly import their packages in first cell executed in Swift mode. Also, you cannot include EnableJupyterDisplay.swift (include EnableIPythonDisplay.swift instead).

%install '.package(url: "https://github.com/pvieito/PythonKit.git", .branch("master"))' PythonKit
%install '.package(url: "https://github.com/philipturner/differentiation", .branch("main"))' _Differentiation
%include "EnableIPythonDisplay.swift"
import PythonKit
import Differentiation

Sample code

The code on the README of google/swift-jupyter about SwiftPlot will not compile. Replace it with the following:

import Foundation
import SwiftPlot
import AGGRenderer

func function(_ x: Float) -> Float {
    return 1.0 / x
}

var aggRenderer = AGGRenderer()
var lineGraph = LineGraph<Float, Float>()
lineGraph.addFunction(
    function,
    minX: -5.0,
    maxX: 5.0,
    numberOfSamples: 400,
    clampY: -50...50,
    label: "1/x",
    color: .orange)
lineGraph.plotTitle.title = "FUNCTION"
lineGraph.drawGraph(renderer: aggRenderer)
display(base64EncodedPNG: aggRenderer.base64Png())

And add these statements to the bottom of the code cell that imports PythonKit and Differentiation:

%install-swiftpm-flags -Xcc -isystem/usr/include/freetype2 -Xswiftc -lfreetype
%install '.package(url: "https://github.com/KarthikRIyer/swiftplot", .branch("master"))' SwiftPlot AGGRenderer

Testing

To test Swift-Colab against recent Swift toolchains and ensure support is never dropped from Colab again, you can run the following tests. These Colab notebooks originated from Python unit tests in google/swift-jupyter:

TestPassingLast Tested
kernel_testsSwift 5.5.3 (March 2022)
own_kernel_testsSwift 5.5.3 (March 2022)
simple_notebook_testsSwift 5.5.3 (March 2022)
swiftplot testsSwift 5.5.3 (March 2022)

You can also test some tutorial notebooks on tensorflow/swift that don't require TensorFlow. Paste the contents of Swift-Template into the top of each S4TF tutorial.

Tutorial (excluding TensorFlow cells)PassingLast Tested
A Swift TourSwift 5.5.2 (December 2021)
Protocol-Oriented Programming & GenericsSwift 5.5.2 (December 2021)
Python Interoperablity❌*Swift 5.5.2 (December 2021)
Custom DifferentiationSwift 5.5.2 (December 2021)
Sharp Edges In Differentiability❌*Swift 5.5.2 (December 2021)

*"Python Interoperability" and "Sharp Edges in Differentiability" can pass with minor changes.