You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/The Design and Philosphy of spidr's Tensor.md
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -15,11 +15,11 @@ limitations under the License.
15
15
-->
16
16
# The Design and Philosophy of spidr's Tensor
17
17
18
-
spidr was born out of a desire to explore what is possible when we have the opportunity to use the latest technologies available in engineering machine learning systems. This philosophy has led many central decision in spidr's design. In this tutorial, we'll explore these, detailing spidr's architecture and limitations as we go.
18
+
spidr was born out of a desire to explore what is possible when we have the opportunity to use the latest technologies available in engineering machine learning systems. This philosophy has led many central decisions in spidr's design. In this tutorial, we'll explore these, detailing spidr's architecture, strengths and limitations as we go.
19
19
20
20
## Dependent types: precisely describing Tensors
21
21
22
-
The first technology that users will likely notice is that spidr utilises the dependent types offered by Idris. Dependent types allow you to, among other things, include values at type level. Here's an introductory [talk](https://www.youtube.com/watch?v=mOtKD7ml0NU) on dependent types, and a [book](https://www.manning.com/books/type-driven-development-with-idris) if you want a deeper dive. Let's have a quick look at an example of relevance to spidr. A `List Int` is a list of integers. It's not a dependent type, and it can have zero, five or two hundred elements. which can have any number of elements
22
+
The first technology that users will likely notice is that spidr utilises the dependent types offered by Idris. Dependent types allow you to, among other things, include values at type level. Here's an introductory [talk](https://www.youtube.com/watch?v=mOtKD7ml0NU) on dependent types, and a [book](https://www.manning.com/books/type-driven-development-with-idris) if you want a deeper dive. Let's have a quick look at an example of relevance to spidr. A `List Int` is a list of integers. It's not a dependent type, and it can have any number of elements
23
23
<!-- idris
24
24
import Data.Vect
25
25
-->
@@ -36,12 +36,12 @@ head : List Int -> Int
36
36
head [] = ?hmmm
37
37
head (x :: _) = x
38
38
```
39
-
we can't implement `head` for empty lists, at least not as a total function. Let's turn to another type. A `Vect 3 Int` is also a list, but it's a dependent type which always contains precisely three integers
39
+
we can't implement `head` for empty lists, at least not as a total function. Let's turn to another type that will help us address this. A `Vect 3 Int` is also a list, but it's a dependent type which always contains precisely three integers
40
40
```idris
41
41
ys : Vect 3 Int
42
42
ys = [0, 1, 2]
43
43
```
44
-
If we tried to implement this with `ys = [0]`, it wouldn't compile, as this is a `Vect 1 Nat`. If we try to define `head` for `Vect`, we find that we can use the types to require that the argument isn't empty
44
+
If we tried to implement this with `ys = [0]`, it wouldn't compile, as `[0]` is a `Vect 1 Nat`. If we try to define `head` for `Vect`, we find that we can use the types to require that the argument isn't empty
45
45
```idris
46
46
namespace Vect
47
47
head : Vect (S n) Int -> Int
@@ -62,21 +62,21 @@ It's exactly this kind of extra precision that we use throughout spidr when work
62
62
63
63
## Running Tensors with XLA
64
64
65
-
When writing spidr's `Tensor` API, we had the option to implement `Tensor` within spidr, or use a third party offering. We chose to opt for a third party tool for a number of reasons:
65
+
When writing spidr's `Tensor` API, we had the option to implement `Tensor` within spidr, or use a third party tool. We chose to opt for a third party tool for a number of reasons:
66
66
67
67
* it allowed us to start working on higher-level aspects of spidr sooner, such as the probabilistic modelling API
68
68
* many frameworks have been highly optimized and offer impressive performance
69
69
* many frameworks offer acceleration on hardware beyond CPUs
70
70
71
-
We were drawn to smaller third party tools that offered only what we needed and nothing more, and especially those that were newer as they would be more likely to have learnt from older frameworks. The first candidate was Graphcore's Poplar. We ruled this out because IPUs are difficult to access for individuals, and other accelerators are either not emphaised or not supported at all. XLA was the next candidate. It supports a number of accelerators of interest, and it is currently being used by Google's JAX, which implies it will remain active for the forseeable future. It also offers a C++ API which allows us to efficiently call into it from Idris. In retrospect, progress has been slower due to the fact that XLA does not include automatic differentiation. We're unsure if this would have affected our decisions had we factored this into our decision.
71
+
We were drawn to smaller third party tools that offered only what we needed and nothing more, and especially those that were newer as they would be more likely to have learnt from older frameworks. The first candidate was Graphcore's Poplar. While the speed of Graphcore's IPU was attractive, we ruled this out because IPUs are difficult to access for individuals, and other accelerators are either not emphaised or not supported at all. XLA was the next candidate. It supports a number of accelerators of interest (including the IPU), and it is currently being used by Google's JAX, which suggests it will remain active for the forseeable future. It also offers a C++ API which allows us to efficiently call into it from Idris. In retrospect, progress has been slower due to the fact that XLA does not include automatic differentiation. We're unsure if this would have affected our decisions had we considered this at the time.
72
72
73
73
As mentioned, XLA has a C++ API. In order to call this from Idris, we had two options. The first is to write a C++ backend for Idris. Apparently the Idris core language is small, which means writing new backends is less work than one may expect. The other option is to wrap XLA in a pure C wrapper and use Idris' FFI capabilities to call into this wrapper. We chose this second option for a number of reasons:
74
74
75
75
* we had more familiarity with how to FFI into C than we did in writing a backend
76
76
* all Idris backends support C FFI, which means that spidr would be compatible with all Idris backends
77
77
* the C wrapper may be useful for other projects as many languages support FFI to C
78
78
79
-
We decided to keep the C wrapper as close to the C++ interface as possible, within the limitations of what would work with Idris' FFI. We did this so that the C wrapper made few assumptions about how it would be used, and also so that we could implement as much of the logic as possible within Idris, simply because we find Idris easier to work with. Of course, this means there is a layer in Idris of very XLA-specific code that looks only approximately like spidr's tensor API. Thus, spidr is structured as
79
+
We decided to keep the C wrapper as close to the C++ interface as possible, within the limitations of what would work with Idris' FFI. We did this so that the C wrapper made few assumptions about how it would be used, and also so that we could implement as much of the logic as possible within Idris. Naturally, to do this we need a layer in Idris of very XLA-specific code that looks only approximately like spidr's tensor API. Thus, spidr is structured as
80
80
```
81
81
Idris tensor API ------> XLA-specific Idris layer ------> C wrapper for XLA ------> XLA C++ interface
0 commit comments