This weekend, I've been working on how to sideload Swift on Google Colab (repo: philipturner/swift-colab). Eventually, this will turn into loading Swift for TensorFlow as a Swift package, pre-compiled as a binary target instead of a toolchain. I got the point where I can pass an arbitrary string of Swift code as a Python string, then compile and run it.
The next step is to install PythonKit into the Colab virtual machine and support SwiftPM. I was exploring the Swift compiler and Swift Package Manager, and came across this tutorial.
After getting it to work on my Mac, I decided to try it out again on Linux. I realized that having the ability to carry out the commands in Swift instead of Shell had some advantages (I could write a string literal to a file without learning the Shell command for that ), and Google Colab is easier to setup than Docker. Also, it was exciting to see Swift code execute something generally useful in Google Colab!
Below is the link to the Colab notebook. Anyone can make a copy and run the program. The first code block is modified to pull from the save-1
branch of the GitHub repository, which will stay stable unlike the main
branch.
import swift
swift.run('''
import Foundation
let fm = FileManager.default
... // do some stuff
func doCommand(args: [String]) throws {
let command = Process()
command.executableURL = .init(fileURLWithPath: "/usr/bin/env")
command.arguments = args
try command.run()
command.waitUntilExit()
}
try doCommand(args: ["swiftc", "-D", "DEBUG", "point.swift", "main.swift", "-o", "point-app"])
try doCommand(args: ["./point-app"])
''')
Output:
debug mode
Hello world! 4 20
I can't guarantee that side-loading will break out of passing strings to Python and support the full Jupyter notebook experience. Regardless, sideloading makes it possible to utilize cloud GPUs and TPUs in the new Swift for TensorFlow. I can validate that the upcoming Metal backend doesn't break CUDA support.
For more context about the Swift for TensorFlow resurrection: