|
| 1 | +# Introducing Swift for TensorFlow |
| 2 | + |
| 3 | +> Swift for TensorFlow: No boundaries. |
| 4 | +
|
| 5 | +Swift for TensorFlow is a next-generation platform for machine learning, |
| 6 | +incorporating the latest research across machine learning, compilers, |
| 7 | +differentiable programming, systems design, and beyond. This is an early-stage |
| 8 | +project: it is not feature-complete nor production-ready, but it is ready for |
| 9 | +_pioneers_ to try in projects, give feedback, and help shape the future! |
| 10 | + |
| 11 | +The Swift for TensorFlow project is currently focusing on 2 kinds of users: |
| 12 | + |
| 13 | +1. **Advanced ML researchers** who are limited by current ML frameworks. Swift |
| 14 | + for TensorFlow's advantages include seamless integration with a modern |
| 15 | + general-purpose language, allowing for more dynamic and sophisticated |
| 16 | + models. Fast abstractions can be developed in "user-space" (as opposed to in |
| 17 | + C/C++, aka "framework-space"), resulting in modular APIs that can be easily |
| 18 | + customized. |
| 19 | + |
| 20 | +2. **ML learners** who are just getting started with machine learning. Thanks |
| 21 | + to Swift's support for quality tooling (e.g. context-aware autocompletion), |
| 22 | + Swift for TensorFlow can be one of the most productive ways to start |
| 23 | + learning the fundamentals of machine learning. |
| 24 | + |
| 25 | +## Swift |
| 26 | + |
| 27 | +Swift is an [open source](https://swift.org/) general-purpose programming language, which has a |
| 28 | +large and growing user base. We chose Swift because it has an |
| 29 | +[open language design process](https://github.com/apple/swift-evolution) and for specific technical |
| 30 | +reasons detailed in the |
| 31 | +"[Why *Swift* for TensorFlow](https://github.com/BradLarson/swift/blob/main/docs/WhySwiftForTensorFlow.md)" |
| 32 | +document. We assume that most readers are unfamiliar with it, so we’ll briefly touch on some |
| 33 | +additional important things about it here. |
| 34 | + |
| 35 | +The development of Swift started in 2010, and aimed to bring the best practices in programming |
| 36 | +language design together into one system rather than trying for academic novelty or to religiously |
| 37 | +propagate programming methodologies. As a result, it supports multi-paradigm development (e.g. |
| 38 | +functional, OOP, generic, procedural, etc) all in one system, and brings many well-known concepts |
| 39 | +from academic languages (e.g. |
| 40 | +[pattern matching](http://alisoftware.github.io/swift/pattern-matching/2016/03/27/pattern-matching-1/), |
| 41 | +[algebraic data types](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html), |
| 42 | +and type classes) into the forefront. Instead of strongly encouraging developers to rewrite all |
| 43 | +their code in Swift, it pragmatically focuses on interoperability with other languages, e.g., |
| 44 | +allowing you to directly import C header files and use them without an |
| 45 | +[FFI](https://en.wikipedia.org/wiki/Foreign_function_interface) and (now) the ability to use Python |
| 46 | +APIs without wrappers. |
| 47 | + |
| 48 | +Swift has the audacious goal of spanning all the way from low-level systems programming to |
| 49 | +high-level scripting, with a focus on being |
| 50 | +[easy to learn and use](https://www.apple.com/swift/playgrounds/). |
| 51 | +Because Swift needs to be easy to learn and use but also powerful, it relies on the principle of |
| 52 | +[progressive disclosure of complexity](https://www.nngroup.com/articles/progressive-disclosure/), |
| 53 | +which aggressively factors the cost of complexity onto the people who benefit from that complexity. |
| 54 | +The "scripting language feel" combined with high performance is very useful for machine learning. |
| 55 | + |
| 56 | +A final pertinent aspect of the design of Swift is that much of the Swift language is actually |
| 57 | +implemented in its standard library. "Builtin" types like |
| 58 | +[Int](https://developer.apple.com/documentation/swift/int) and |
| 59 | +[Bool](https://developer.apple.com/documentation/swift/bool) are actually just structs defined in |
| 60 | +the standard library that wrap magic types and operations. As such, sometimes we joke that Swift |
| 61 | +is just "syntactic sugar for LLVM". |
| 62 | + |
| 63 | +There is a lot more that is cool about Swift and a ton of content available online. If you are |
| 64 | +interested in learning more about general Swift programming concepts, here are a few links to get |
| 65 | +started: |
| 66 | + |
| 67 | + - [A Swift Tour](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html) is a skimmable tour of the high level syntax and feel of Swift, and is part of the larger "The Swift Programming Language" book. |
| 68 | + - Value semantics are powerful and play an important role in Swift code, as explained in "[Building Better Apps with Value Types in Swift](https://developer.apple.com/videos/play/wwdc2015/414/)" [[YouTube](https://www.youtube.com/watch?v=av4i3x-aZbM)]. |
| 69 | + - Swift supports classic OOP, but has adapted ideas from the Haskell type system. This is explained in "[Protocol-Oriented Programming in Swift](https://developer.apple.com/videos/play/wwdc2015/408/)" [[YouTube](https://www.youtube.com/watch?v=g2LwFZatfTI)]. |
| 70 | + |
| 71 | +One warning: Swift evolved rapidly in its early years, so you should be careful with anything |
| 72 | +before Swift 3 (released in 2016). |
| 73 | + |
| 74 | +## Why Swift for TensorFlow? |
| 75 | + |
| 76 | +Swift for TensorFlow is a new way to develop machine learning models. It |
| 77 | +gives you the power of |
| 78 | +[TensorFlow](https://www.tensorflow.org) directly integrated into the |
| 79 | +[Swift programming language](https://swift.org/about). We believe that |
| 80 | +machine learning paradigms are so important that they deserve |
| 81 | +**first-class language and compiler support**. |
| 82 | + |
| 83 | +A fundamental primitive in machine learning is gradient-based optimization: |
| 84 | +computing function derivatives to optimize parameters. With Swift for |
| 85 | +TensorFlow, you can easily differentiate functions using differential |
| 86 | +operators like [`gradient(of:)`](https://www.tensorflow.org/swift/api_docs/Functions#/s:10TensorFlow8gradient2of15CotangentVectorQzxcq_xc_tAA14DifferentiableRzSFR_AaFR_AdaFPQy_Rs_r0_lF), or differentiate with respect to an entire |
| 87 | +model by calling method [`gradient(in:)`](https://www.tensorflow.org/swift/api_docs/Protocols/Differentiable#/s:10TensorFlow14DifferentiablePAAE8gradient2in15CotangentVectorQzqd__xXE_tSFRd__AaBRd__AfCQyd__Rsd__lF). These differentiation APIs |
| 88 | +are not just available for `Tensor`-related concepts—they are |
| 89 | +generalized for all types that conform to the [`Differentiable`](https://www.tensorflow.org/swift/api_docs/Protocols/Differentiable) |
| 90 | +protocol, including `Float`, `Double`, SIMD vectors, and your own data |
| 91 | +structures. |
| 92 | + |
| 93 | +```swift |
| 94 | +// Custom differentiable type. |
| 95 | +struct Model: Differentiable { |
| 96 | + var w: Float |
| 97 | + var b: Float |
| 98 | + func applied(to input: Float) -> Float { |
| 99 | + return w * input + b |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// Differentiate using `gradient(at:_:in:)`. |
| 104 | +let model = Model(w: 4, b: 3) |
| 105 | +let input: Float = 2 |
| 106 | +let (𝛁model, 𝛁input) = gradient(at: model, input) { model, input in |
| 107 | + model.applied(to: input) |
| 108 | +} |
| 109 | + |
| 110 | +print(𝛁model) // Model.TangentVector(w: 2.0, b: 1.0) |
| 111 | +print(𝛁input) // 4.0 |
| 112 | +``` |
| 113 | + |
| 114 | +Beyond derivatives, the Swift for TensorFlow project comes with a sophisticated toolchain |
| 115 | +to make users more productive. You can run Swift interactively in a Jupyter |
| 116 | +notebook, and get helpful autocomplete suggestions to help you explore the |
| 117 | +massive API surface of a modern deep learning library. You can [get started |
| 118 | +right in your browser in |
| 119 | +seconds](https://colab.research.google.com/github/tensorflow/swift/blob/main/docs/site/tutorials/model_training_walkthrough.ipynb)! |
| 120 | + |
| 121 | +Migrating to Swift for TensorFlow is really easy thanks to Swift's powerful |
| 122 | +Python integration. You can incrementally migrate your Python code over (or |
| 123 | +continue to use your favorite Python libraries), because you can easily call |
| 124 | +your favorite Python library with a familiar syntax: |
| 125 | + |
| 126 | +```swift |
| 127 | +import TensorFlow |
| 128 | +import Python |
| 129 | + |
| 130 | +let np = Python.import("numpy") |
| 131 | + |
| 132 | +let array = np.arange(100).reshape(10, 10) // Create a 10x10 numpy array. |
| 133 | +let tensor = Tensor<Float>(numpy: array) // Seamless integration! |
| 134 | +``` |
0 commit comments