Hi, I thought it’d be interesting to try to compile and run the FastaiNotebooks Swift code on Xcode, to see how that went.
Why? Well, Xcode is familiar to the vast majority of existing Swift developers, it has good documentation lookup, and it can provide autocomplete, intelligent contextual error message, fix it hints, etc… Perhaps a mature IDE like Xcode can show the benefits of Swift’s type system better than notebooks? On the other hand, all the fastai Swift code has been written targeting Jupyter notebooks on Ubuntu, which you need for a GPU anyway, so perhaps this was a fool’s errand. So I tried!
To summarize, it doesn’t build in Xcode right now. But it’s probably not too far from working. And the exercise points out some compatibility issues that may be worth fixing.
Here are some quick observations for anyone who wants to try the same thing.
Things to do get started:
- The S4TF page has instructions for installing an Xcode S4TF nightly as a toolchain, and then configuring Xcode to use that toolchain. For now, I’d use the 2019-03-26 release, not the one released today at 2019-04-16.
- In the
FastaiNotebooks
directory, you can runswift package generate-xcodeproj
to create aFastaiNotebooks.xcodeproj
. (Make sure you’ve set your PATH so thatswift
points to the one binary installed in your selected toolchain.) - Open Xcode and go File / Project Settings / Build System: Legacy Build System
- In Xcode, in the Project Navigator (CMD-1), select the project bundle, then select the
FastaiNotebooks
target, then select Build Settings, and then scroll down to the bottom of the many build settings and change “Swift Language Version” from “Swift 4” to “Swift 5”. (I suspect this is necessary becausePackage.swift
does not specify version 5) - You may also want to set “macOS Deployment Target” to 10.14 in order to suppress some irrelevant warnings about older versions of macOS.
This leads to the problems you then encounter:
- the latest Xcode S4TF nightly (released today on 2019-04-16) introduces breaking changes to the TensorFlow module’s API, versus the FastaiNotebooks right now. (E.g., changes to the initializer for
Context
.) - The Fastai code has a few Linux-only snippets here and there (such as importing Glibc and using
random()
instead ofarc4random()
) - more puzzlingly, the compiler generates a few build errors which you don’t see in the notebooks, related to protocol conformance with
TensorGroup
andTensorArrayProtocol
. I’d guess these these are a result of difference between the macOS and Linux toolchain versions I’m using, but I suppose it’s possible that Xcode’s build system treats some conditions as errors that Jupyter (via lldb) treats as warnings.
In sum, it might be worth shimming the Linux-only call sites, and looking more closely at the protocol conformances.