{ "cells": [ { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": ["// Google Colab only: uncomment and run the following to install dotnet and the F# kernel\n", "// !bash \u003c(curl -Ls https://raw.githubusercontent.com/gbaydin/scripts/main/colab_dotnet6.sh)\n"] } , { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": ["// Import DiffSharp package\n", "#r \"nuget: DiffSharp-lite,1.0.7\"\n", "\n", "// Set dotnet interactive formatter to plaintext\n", "Formatter.SetPreferredMimeTypesFor(typeof\u003cobj\u003e, \"text/plain\")\n", "Formatter.Register(fun (x:obj) (writer: TextWriter) -\u003e fprintfn writer \"%120A\" x )\n"] } , { "cell_type": "markdown", "metadata": {}, "source": ["[![Binder](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DiffSharp/diffsharp.github.io/blob/master/install.ipynb)\u0026emsp;\n", "[![Binder](img/badge-binder.svg)](https://mybinder.org/v2/gh/diffsharp/diffsharp.github.io/master?filepath=install.ipynb)\u0026emsp;\n", "[![Script](img/badge-script.svg)](install.fsx)\u0026emsp;\n", "[![Script](img/badge-notebook.svg)](install.ipynb)\n", "\n", "# Installing\n", "\n", "DiffSharp runs on [dotnet](https://dotnet.microsoft.com/), a cross-platform, open-source platform supported on Linux, macOS, and Windows.\n", "\n", "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.\n", "\n", "## Interactive Notebooks and Scripts\n", "\n", "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:\n", "\n", " // Use one of the following three lines\n", " #r \"nuget: DiffSharp-cpu\" // Use the latest version\n", " #r \"nuget: DiffSharp-cpu, *-*\" // Use the latest pre-release version\n", " #r \"nuget: DiffSharp-cpu, 1.0.1\" // Use a specific version\n", "\n", " open DiffSharp\n", "\n"] } , { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": ["\u003c/br\u003e\n", "\u003cimg src=\"img/anim-intro-1.gif\" width=\"85%\" /\u003e\n"] } , { "cell_type": "markdown", "metadata": {}, "source": ["## Dotnet Applications\n", "\n", "You can add DiffSharp to your dotnet application using the [dotnet](https://dotnet.microsoft.com/) command-line interface (CLI).\n", "\n", "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.\n", "\n", " dotnet new console -lang \"F#\" -o src/app\n", " cd src/app\n", " dotnet add package --prerelease DiffSharp-cpu\n", " dotnet run\n", "\n", "## Packages\n", "\n", "We provide several package bundles for a variety of use cases.\n", "\n", "* [DiffSharp-cpu](https://www.nuget.org/packages/DiffSharp-cpu)\u003c/br\u003e\n", "Includes LibTorch CPU binaries for Linux, macOS, and Windows.\n", "\n", "* [DiffSharp-cuda-linux](https://www.nuget.org/packages/DiffSharp-cuda-linux) / [DiffSharp-cuda-windows](https://www.nuget.org/packages/DiffSharp-cuda-windows)\u003c/br\u003e\n", "Include LibTorch CPU and CUDA GPU binaries for Linux and Windows. Large download.\n", "\n", "* [DiffSharp-lite](https://www.nuget.org/packages/DiffSharp-lite)\u003c/br\u003e\n", "Includes the Torch backend but not the LibTorch binaries.\n", "\n", "### Using local LibTorch binaries (optional)\n", "\n", "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.\n", "\n", "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.\n", "\n", "* 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.\n", "\n", "* 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.\n", "\n", "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.\n", "\n", " open System.Runtime.InteropServices\n", " NativeLibrary.Load(\"/home/user/anaconda3/lib/python3.8/site-packages/torch/lib/libtorch.so\")\n", "\n", "\n", "## Backends and Devices\n", "\n", "DiffSharp currently provides two computation backends.\n", "\n", "* 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\u0027s 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.\n", " \n", "\n", "* 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.\n", " \n", "\n", "### Configuration of Default Backend, Device, and Tensor Type\n", "\n", "Selection of the default backend, device, and tensor type is done using [dsharp.config](https://diffsharp.github.io/reference/diffsharp-dsharp.html#config).\n", "\n", "* [Dtype](https://diffsharp.github.io/reference/diffsharp-dtype.html) choices available: `BFloat16`, `Bool`, `Byte`, `Float16`, `Float32`, `Float64`, `Int16`, `Int32`, `Int64`, `Int8`\n", " \n", "\n", "* [Device](https://diffsharp.github.io/reference/diffsharp-device.html) choices available: `CPU`, `GPU`\n", " \n", "\n", "* [Backend](https://diffsharp.github.io/reference/diffsharp-backend.html) choices available: `Reference`, `Torch`\n", " \n", "\n", "For example, the following selects the `Torch` backend with single precision tensors as the default tensor type and GPU (CUDA) execution.\n", "\n"] } , { "cell_type": "code", "metadata": {}, "execution_count": 2, "outputs": [], "source": ["open DiffSharp\n", "\n", "dsharp.config(dtype=Dtype.Float32, device=Device.GPU, backend=Backend.Torch)\n"] } , { "cell_type": "markdown", "metadata": {}, "source": ["The following selects the `Reference` backend.\n", "\n"] } , { "cell_type": "code", "metadata": {}, "execution_count": 3, "outputs": [], "source": ["dsharp.config(backend=Backend.Reference)\n"] } , { "cell_type": "markdown", "metadata": {}, "source": ["A tensor\u0027s backend and device can be inspected as follows.\n", "\n"] } , { "cell_type": "code", "metadata": {}, "execution_count": 4, "outputs": [], "source": ["let t = dsharp.tensor [ 0 .. 10 ]\n", "\n", "let device = t.device\n", "let backend = t.backend\n"] } , { "cell_type": "markdown", "metadata": {}, "source": ["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:\n", "\n"] } , { "cell_type": "code", "metadata": {}, "execution_count": 5, "outputs": [], "source": ["let t2 = t.move(Device.GPU)\n"] } , { "cell_type": "markdown", "metadata": {}, "source": ["## Developing DiffSharp Libraries\n", "\n", "To develop libraries built on DiffSharp, you can use the following guideline to reference the various packages.\n", "\n", "* Reference `DiffSharp.Core` and `DiffSharp.Data` in your library code.\n", "\n", "* Reference `DiffSharp.Backends.Reference` in your correctness testing code.\n", "\n", "* Reference `DiffSharp.Backends.Torch` and `libtorch-cpu` in your CPU testing code.\n", "\n", "* Reference `DiffSharp.Backends.Torch` and `libtorch-cuda-linux` or `libtorch-cuda-windows` in your (optional) GPU testing code.\n", "\n"] }], "metadata": { "kernelspec": {"display_name": ".NET (F#)", "language": "F#", "name": ".net-fsharp"}, "langauge_info": { "file_extension": ".fs", "mimetype": "text/x-fsharp", "name": "C#", "pygments_lexer": "fsharp", "version": "4.5" } }, "nbformat": 4, "nbformat_minor": 1 }