-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.fsx
130 lines (78 loc) · 6.86 KB
/
install.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#r "nuget: DiffSharp-lite,1.0.7"
(**
[](https://colab.research.google.com/github/DiffSharp/diffsharp.github.io/blob/master/install.ipynb) 
[](https://mybinder.org/v2/gh/diffsharp/diffsharp.github.io/master?filepath=install.ipynb) 
[](install.fsx) 
[](install.ipynb)
# Installing
DiffSharp runs on [dotnet](https://dotnet.microsoft.com/), a cross-platform, open-source platform supported on Linux, macOS, and Windows.
There are various ways in which you can run DiffSharp, the main ones being: [interactive notebooks](https://github.com/dotnet/interactive) supporting [Visual Studio Code](https://code.visualstudio.com/) and [Jupyter](https://jupyter.org/); running in a [REPL](https://github.com/jonsequitur/dotnet-repl); running [script files](https://docs.microsoft.com/en-us/dotnet/fsharp/tools/fsharp-interactive/); and [compiling, packing, and publishing](https://docs.microsoft.com/en-us/dotnet/core/introduction) performant binaries.
## Interactive Notebooks and Scripts
You can use DiffSharp in [dotnet interactive](https://github.com/dotnet/interactive) notebooks in [Visual Studio Code](https://code.visualstudio.com/) or [Jupyter](https://jupyter.org/), or in F# scripts (`.fsx` files), by referencing the package as follows:
// Use one of the following three lines
#r "nuget: DiffSharp-cpu" // Use the latest version
#r "nuget: DiffSharp-cpu, *-*" // Use the latest pre-release version
#r "nuget: DiffSharp-cpu, 1.0.1" // Use a specific version
open DiffSharp
*)
</br>
<img src="img/anim-intro-1.gif" width="85%" />
(**
## Dotnet Applications
You can add DiffSharp to your dotnet application using the [dotnet](https://dotnet.microsoft.com/) command-line interface (CLI).
For example, the following creates a new F# console application and adds the latest pre-release version of the `DiffSharp-cpu` package as a dependency.
dotnet new console -lang "F#" -o src/app
cd src/app
dotnet add package --prerelease DiffSharp-cpu
dotnet run
## Packages
We provide several package bundles for a variety of use cases.
* [DiffSharp-cpu](https://www.nuget.org/packages/DiffSharp-cpu)</br>
Includes LibTorch CPU binaries for Linux, macOS, and Windows.
* [DiffSharp-cuda-linux](https://www.nuget.org/packages/DiffSharp-cuda-linux) / [DiffSharp-cuda-windows](https://www.nuget.org/packages/DiffSharp-cuda-windows)</br>
Include LibTorch CPU and CUDA GPU binaries for Linux and Windows. Large download.
* [DiffSharp-lite](https://www.nuget.org/packages/DiffSharp-lite)</br>
Includes the Torch backend but not the LibTorch binaries.
### Using local LibTorch binaries (optional)
You can combine the `DiffSharp-lite` package bundle with existing local native binaries of LibTorch for your OS (Linux, Mac, or Windows) installed through other means.
LibTorch is the main tensor computation core implemented in C++/CUDA and it is used by PyTorch in Python and by other projects in various programming languages. The following are two common ways of having LibTorch in your system.
* If you use Python and have [PyTorch](https://pytorch.org/) installed, this comes with LibTorch as a part of the PyTorch distribution. If your GPU works in this PyTorch installation without any issues, it will also work in DiffSharp.
* You can download the native LibTorch package without Python by following the [get started](https://pytorch.org/get-started/locally/) instructions in the PyTorch website, and extracting the downloaded archive to a folder in your system.
Before using the `Torch` backend in DiffSharp, you will have to add an explicit load of the LibTorch native library, which you can do as follows. In order to find the location of LibTorch binaries, searching for `libtorch.so` in your system might be helpful. Note that this file is called `libtorch.so` in Linux, `libtorch.dylib` in macOS, and `torch.dll` in Windows.
open System.Runtime.InteropServices
NativeLibrary.Load("/home/user/anaconda3/lib/python3.8/site-packages/torch/lib/libtorch.so")
## Backends and Devices
DiffSharp currently provides two computation backends.
* The `Torch` backend is the default and recommended backend based on [LibTorch](https://pytorch.org/cppdocs/), using the same C++ and CUDA implementations for tensor computations that power [PyTorch](https://pytorch.org/). On top of these raw tensors (LibTorch's ATen, excluding autograd), DiffSharp implements its own computation graph and differentiation capabilities. This backend requires platform-specific binaries of LibTorch, which we provide and test on Linux, macOS, and Windows.
* The `Reference` backend is implemented purely in F# and can run on any hardware platform where [dotnet](https://dotnet.microsoft.com/) can run (for example iOS, Android, Raspberry Pi). This backend has reasonable performance for use cases dominated by scalar and small tensor operations, and is not recommended for use cases involving large tensor operations (such as machine learning). This backend is always available.
### Configuration of Default Backend, Device, and Tensor Type
Selection of the default backend, device, and tensor type is done using [dsharp.config](https://diffsharp.github.io/reference/diffsharp-dsharp.html#config).
* [Dtype](https://diffsharp.github.io/reference/diffsharp-dtype.html) choices available: `BFloat16`, `Bool`, `Byte`, `Float16`, `Float32`, `Float64`, `Int16`, `Int32`, `Int64`, `Int8`
* [Device](https://diffsharp.github.io/reference/diffsharp-device.html) choices available: `CPU`, `GPU`
* [Backend](https://diffsharp.github.io/reference/diffsharp-backend.html) choices available: `Reference`, `Torch`
For example, the following selects the `Torch` backend with single precision tensors as the default tensor type and GPU (CUDA) execution.
*)
open DiffSharp
dsharp.config(dtype=Dtype.Float32, device=Device.GPU, backend=Backend.Torch)
(**
The following selects the `Reference` backend.
*)
dsharp.config(backend=Backend.Reference)
(**
A tensor's backend and device can be inspected as follows.
*)
let t = dsharp.tensor [ 0 .. 10 ]
let device = t.device
let backend = t.backend
(**
Tensors can be moved between devices (for example from CPU to GPU) using [Tensor.move](https://diffsharp.github.io/reference/diffsharp-tensor.html#move). For example:
*)
let t2 = t.move(Device.GPU)
(**
## Developing DiffSharp Libraries
To develop libraries built on DiffSharp, you can use the following guideline to reference the various packages.
* Reference `DiffSharp.Core` and `DiffSharp.Data` in your library code.
* Reference `DiffSharp.Backends.Reference` in your correctness testing code.
* Reference `DiffSharp.Backends.Torch` and `libtorch-cpu` in your CPU testing code.
* Reference `DiffSharp.Backends.Torch` and `libtorch-cuda-linux` or `libtorch-cuda-windows` in your (optional) GPU testing code.
*)