DiffSharp runs on dotnet, 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 supporting Visual Studio Code and Jupyter; running in a REPL; running script files; and compiling, packing, and publishing performant binaries.
You can use DiffSharp in dotnet interactive notebooks in Visual Studio Code or Jupyter, 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
You can add DiffSharp to your dotnet application using the dotnet 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
We provide several package bundles for a variety of use cases.
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.
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")
DiffSharp currently provides two computation backends.
The Torch
backend is the default and recommended backend based on LibTorch, using the same C++ and CUDA implementations for tensor computations that power PyTorch. 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 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.
Selection of the default backend, device, and tensor type is done using dsharp.config.
Dtype choices available: BFloat16
, Bool
, Byte
, Float16
, Float32
, Float64
, Int16
, Int32
, Int64
, Int8
Device choices available: CPU
, GPU
Backend 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. For example:
let t2 = t.move(Device.GPU)
To develop libraries built on DiffSharp, you can use the following guideline to reference the various packages.
DiffSharp.Core
and DiffSharp.Data
in your library code.DiffSharp.Backends.Reference
in your correctness testing code.DiffSharp.Backends.Torch
and libtorch-cpu
in your CPU testing code.DiffSharp.Backends.Torch
and libtorch-cuda-linux
or libtorch-cuda-windows
in your (optional) GPU testing code.© Copyright 2021, DiffSharp Contributors.