FastaiNotebooks on Xcode (?!)

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! :upside_down_face:

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 run swift package generate-xcodeproj to create a FastaiNotebooks.xcodeproj. (Make sure you’ve set your PATH so that swift 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 because Package.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 of arc4random())
  • more puzzlingly, the compiler generates a few build errors which you don’t see in the notebooks, related to protocol conformance with TensorGroup and TensorArrayProtocol. 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.


The notebooks will be updated in the next day or so to adapt to the breaking changes; there are a lot of bugfixes in the new toolchain that you’ll definitely want to have. @rxwei may have more info about the build errors (which could simply be related to bugs in the old toolchain); he has the most experience with S4TF on macOS.

Great idea! :slight_smile:

That should be fixed in the next 24 hours.

A PR to fix them would be most welcome.

If you could provide the details then hopefully the s4tf folks can help us fix them! Although we’re pretty focused on getting the Linux stuff working so it might have to wait until after the course.

1 Like

These calls could use the standard random APIs on the integer types, e.g. Int.random(in: 0 ..< 10) which are portable.

Nice. I’ll do a PR as Jeremy suggested.

1 Like