Blog: Fast.ai Lesson 14 notes — Part 2 v3
These notes are from Part 2 v3 lessons which are not released yet. Some of the material might not make sense without the notebooks and therefore I don’t recommend to read these if you are not in the course. I tried to add all things that weren’t clear from v2 or that didn’t exist then.





Even making two-dimensional matrix calculation to go fast is really hard. In our case, we need to do more dimensions more different kind of operations and that is even harder. There is a compiler doing this and it is called XLA. This only works with graphs which is kind of a problem right now. There are two ways to transfer the normal code into graphs, tracing and staging. These methods although have weird side effects and currently there is research happening around this area to make these work. Probably when this video is published in June there will be a solution for this.
All these things are coming in the future but because we don’t have those yet now we should focus on things we have. C is a fast programming language and next, we will learn how to use it in Swift.
c_interop_examples.ipynb
There are no great audio processing libraries in Swift. There are C libraries that are doing this job well so why not use those.
To use C library Jeremy did the following things: First, he created a directory called SwiftSox, then inside that directory, he created a file called Package.swift that has the following code.
// swift-tools-version:4.2
import PackageDescription
let package = Package(
name: "SwiftSox",
products: [ .library( name: "SwiftSox", targets: ["SwiftSox"]), ],
targets: [
.target( name: "SwiftSox", dependencies: ["sox"]),
.testTarget( name: "SwiftSoxTests", dependencies: ["SwiftSox"]),
.systemLibrary( name: "sox", pkgConfig: "sox")
]
)
Use this as a base when you create another C library folder because the structure is the same.
Then Jeremy also created the following directories ./Sources/sox
and a file called module.modulemap
inside sox
directory.
module sox [system] {
umbrella header "soxu.h"
link "sox"
export *
}
Again these lines are mostly the same for every C library.
The final thing is soxu.h
file inside the same directory than previous.
#include <sox.h>
After all these steps you can just import it in Swift by writing import sox
Now you can use all of the functions that are defined in the library.
Jeremy showed techniques to use different languages in Swift. What’s the point of all this? He showed that sometimes using e.g. C library can make the code faster. It’s not just about what we can do twice as fast computing in some things but more like that this opens us new doors that were closed previously. Chris highlighted that this is not something rookies should do because it’s kind of complicated and there is other things you should learn first. He even said that ignoring all this (using other languages) stuff could be a good idea.
Datablock API
08c_data_block_generic.ipynb
There is a couple of problems in the current implementation. Firstly we need to run everything in a particular order. Also if the code misses a step it might cause an error.








The rest of the notebook shows how data block API can be implemented to Swift.
Protocols in Swift
aka interfaces, aka type classes similar to abstract classes














Fully connected model
02_fully_connected.ipynb










Basic Architecture












I didn’t want to just copy the full notebook so go check rest of it to understand how loss and the backward pass is working.
Classes in Swift
Previously we have used a struct that looks like class.
value (struct) vs. reference (class)
























Automatic Differentiation in Swift
































This is not something we should do every time but in some cases, gradient can be really complex and using approximation can speed up the calculation.
In the end, Jeremy showed how to build image classifier from scratch. There wasn’t any new theory but just using the old things we already should know.
~Lankinen
Leave a Reply