• Stars
    star
    449
  • Rank 97,328 (Top 2 %)
  • Language
  • Created over 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Neural Network proposal for WASI

wasi-nn

A proposed WebAssembly System Interface API for machine learning (ML).

Current Phase

wasi-nn is currently in Phase 2.

Champions

Phase 4 Advancement Criteria

wasi-nn must have at least two complete independent implementations.

Table of Contents

Introduction

wasi-nn is a WASI API for performing ML inference. Its name derives from the fact that ML models are also known as neural networks (nn). ML models are typically trained using a large data set, resulting in one or more files that describe the model's weights. The model is then used to compute an "inference," e.g., the probabilities of classifying an image as a set of tags. This API is concerned initially with inference, not training.

Why expose ML inference as a WASI API? Though the functionality of inference can be encoded into WebAssembly, there are two primary motivations for wasi-nn:

  1. ease of use: an entire ecosystem already exists to train and use models (e.g., Tensorflow, ONNX, OpenVINO, etc.); wasi-nn is designed to make it easy to use existing model formats as-is
  2. performance: the nature of ML inference makes it amenable to hardware acceleration of various kinds; without this hardware acceleration, inference can suffer slowdowns of several hundred times. Hardware acceleration for ML is very diverse β€” SIMD (e.g., AVX512), GPUs, TPUs, FPGAs β€” and it is unlikely (impossible?) that all of these would be supported natively in WebAssembly

WebAssembly programs that want to use a host's ML capabilities can access these capabilities through wasi-nn's core abstractions: backends, graphs, and tensors. A user selects a backend for inference and loads a model, instantiated as a graph, to use in the backend. Then, the user passes tensor inputs to the graph, computes the inference, and retrieves the tensor outputs.

wasi-nn backends correspond to existing ML frameworks, e.g., Tensorflow, ONNX, OpenVINO, etc. wasi-nn places no requirements on hosts to support specific backends; the API is purposefully designed to allow the largest number of ML frameworks to implement it. wasi-nn graphs can be passed as opaque byte sequences to support any number of model formats. This makes the API framework- and format-agnostic, since we expect device vendors to provide the ML backend and support for their particular graph format.

Users can find language bindings for wasi-nn at the wasi-nn bindings repository; request additional language support there. More information about wasi-nn can be found at:

Goals

The primary goal of wasi-nn is to allow users to perform ML inference from WebAssembly using existing models (i.e., ease of use) and with maximum performance. Though the primary focus is inference, we plan to leave open the possibility to perform ML training in the future (request training in an issue!).

Another design goal is to make the API framework- and model-agnostic; this allows for implementing the API with multiple ML frameworks and model formats. The load method will return an error message when an unsupported model encoding scheme is passed in. This approach is similar to how a browser deals with image or video encoding.

Non-goals

wasi-nn is not designed to provide support for individual ML operations (a "model builder" API). The ML field is still evolving rapidly, with new operations and network topologies emerging continuously. It would be a challenge to define an evolving set of operations to support in the API. Instead, our approach is to start with a "model loader" API, inspired by WebNN’s model loader proposal.

API walk-through

The following example describes how a user would use wasi-nn:

// Load the model.
let encoding = wasi_nn::GRAPH_ENCODING_...;
let target = wasi_nn::EXECUTION_TARGET_CPU;
let graph = wasi_nn::load(&[bytes, more_bytes], encoding, target);

// Configure the execution context.
let context = wasi_nn::init_execution_context(graph);
let tensor = wasi_nn::Tensor { ... };
wasi_nn::set_input(context, 0, tensor);

// Compute the inference.
wasi_nn::compute(context);
wasi_nn::get_output(context, 0, &mut output_buffer, output_buffer.len());

Note that the details above will depend on the model and backend used; the pseudo-Rust simply illustrates the general idea, minus any error-checking. Consult the AssemblyScript and Rust bindings for more detailed examples.

Detailed design discussion

For the details of the API, see wasi-nn.wit.md.

Should wasi-nn support training models?

Ideally, yes. In the near term, however, exposing (and implementing) the inference-focused API is sufficiently complex to postpone a training-capable API until later. Also, models are typically trained offline, prior to deployment, and it is unclear why training models using WASI would be an advantage over training them natively. (Conversely, the inference API does make sense: performing ML inference in a Wasm deployment is a known use case). See associated discussion here and feel free to open pull requests or issues related to this that fit within the goals above.

Should wasi-nn support inspecting models?

Ideally, yes. The ability to inspect models would allow users to determine, at runtime, the tensor shapes of the inputs and outputs of a model. As with ML training (above), this can be added in the future.

Considered alternatives

There are other ways to perform ML inference from a WebAssembly program:

  1. a user could specify a custom host API for ML tasks; this is similar to the approach taken here. The advantages and disadvantages are in line with other "spec vs. custom" trade-offs: the user can precisely tailor the API to their use case, etc., but will not be able to switch easily between implementations.
  2. a user could compile a framework and/or model to WebAssembly; this is similar to here and here. The primary disadvantage to this approach is performance: WebAssembly, even with the recent addition of 128-bit SIMD, does not have optimized primitives for performing ML inference or accessing ML-optimized hardware. The performance loss can be of several orders of magnitude.

Stakeholder Interest & Feedback

TODO before entering Phase 3.

References & acknowledgements

Many thanks for valuable feedback and advice from:

More Repositories

1

design

WebAssembly Design Documents
11,261
star
2

binaryen

Optimizer and compiler/toolchain library for WebAssembly
WebAssembly
7,376
star
3

wabt

The WebAssembly Binary Toolkit
C++
6,778
star
4

WASI

WebAssembly System Interface
Rust
4,810
star
5

spec

WebAssembly specification, reference interpreter, and test suite.
WebAssembly
3,148
star
6

wasi-sdk

WASI-enabled WebAssembly C/C++ toolchain
CMake
1,224
star
7

gc

Branch of the spec repo scoped to discussion of GC integration in WebAssembly
WebAssembly
982
star
8

component-model

Repository for design and specification of the Component Model
Python
945
star
9

proposals

Tracking WebAssembly proposals
849
star
10

wasi-libc

WASI libc implementation for WebAssembly
C
836
star
11

interface-types

WebAssembly
641
star
12

threads

Threads and Atomics in WebAssembly
WebAssembly
577
star
13

wasm-c-api

Wasm C API prototype
C++
539
star
14

simd

Branch of the spec repo scoped to discussion of SIMD in WebAssembly
WebAssembly
531
star
15

meetings

WebAssembly meetings (VC or in-person), agendas, and notes
HTML
468
star
16

esm-integration

ECMAScript module integration
WebAssembly
370
star
17

wasm-jit-prototype

Standalone VM using LLVM JIT
C++
309
star
18

tool-conventions

Conventions supporting interoperatibility between tools working with WebAssembly.
297
star
19

website

WebAssembly website
CSS
270
star
20

memory64

Memory with 64-bit indexes
WebAssembly
210
star
21

testsuite

Mirror of the spec testsuite
WebAssembly
191
star
22

wasi-filesystem

Filesystem API for WASI
167
star
23

reference-types

Proposal for adding basic reference types (anyref)
WebAssembly
163
star
24

wasi-crypto

WASI Cryptography API Proposal
Makefile
162
star
25

exception-handling

Proposal to add exception handling to WebAssembly
WebAssembly
156
star
26

wasi-sockets

WASI API proposal for managing sockets
Rust
155
star
27

wasi-http

142
star
28

wasi-threads

WebAssembly
136
star
29

wasi-io

I/O Types proposal for WASI
132
star
30

multi-memory

Multiple per-module memories for Wasm
WebAssembly
127
star
31

stack-switching

A repository for the stack switching proposal.
WebAssembly
124
star
32

module-linking

Proposal for allowing modules to define, import and export modules and instances
WebAssembly
120
star
33

tail-call

Proposal to add tail calls to WebAssembly
WebAssembly
110
star
34

wasp

WebAssembly module decoder in C++
C++
106
star
35

function-references

Proposal for Typed Function References
WebAssembly
99
star
36

debugging

Design documents and discussions about debug support in WebAssembly
98
star
37

wasmint

Library for interpreting / debugging wasm code
C++
94
star
38

bulk-memory-operations

Bulk memory operations
WebAssembly
74
star
39

wasi-webgpu

72
star
40

js-promise-integration

JavaScript Promise Integration
WebAssembly
69
star
41

multi-value

Proposal to add multi-values to WebAssembly
WebAssembly
66
star
42

wasi-testsuite

WASI Testsuite
Rust
51
star
43

wasi-cloud-core

Rust
50
star
44

flexible-vectors

Vector operations for WebAssembly
WebAssembly
48
star
45

waterfall

Build and test bots
45
star
46

js-types

Proposal for adding type reflection to the JS API
WebAssembly
44
star
47

relaxed-simd

Relax the strict determinism requirements of SIMD operations.
WebAssembly
43
star
48

content-security-policy

WebAssembly
38
star
49

shared-everything-threads

A draft proposal for spawning threads in WebAssembly
WebAssembly
38
star
50

stringref

WebAssembly
37
star
51

JS-BigInt-integration

JavaScript BigInt to WebAssembly i64 integration
WebAssembly
36
star
52

wasi-keyvalue

35
star
53

wasi-libc-old

Precursor to WASI libc.
C
35
star
54

wasi-clocks

Clocks API for WASI
35
star
55

wasi-random

Entropy source API for WASI
29
star
56

benchmarks

Resources for collaborative benchmarking
JavaScript
27
star
57

wasi-sql

25
star
58

wasi-sql-embed

23
star
59

memory-control

A proposal to introduce finer grained control of WebAssembly memory.
WebAssembly
21
star
60

annotations

Proposal for Custom Annotation Syntax in the Text Format
WebAssembly
20
star
61

proposal-type-imports

Proposal for Type Imports & Exports
WebAssembly
20
star
62

constant-time

Constant-time WebAssembly
WebAssembly
20
star
63

wasi-proposal-template

Starter template for proposing a new WASI API
19
star
64

wasi-messaging

Messaging proposal for WASI
18
star
65

wasi-tools

WASI tools
Rust
17
star
66

funclets

Proposal for adding funclets - flexible intraprocedural control flow
WebAssembly
17
star
67

wasi-parallel

wasi-parallel is a proposal to add a parallel for construct to WASI.
Shell
17
star
68

sign-extension-ops

Sign-extension opcodes
WebAssembly
17
star
69

mutable-global

Import & export of mutable globals
WebAssembly
16
star
70

wasi-poll

16
star
71

wasi-cli

Command-Line Interface (CLI) World for WASI
15
star
72

extended-const

Proposal for extended constant expressions
WebAssembly
15
star
73

wasi-observe

Observability World for WASI
Just
15
star
74

instrument-tracing

Proposal to add instrumentation and tracing instructions to WebAssembly
WebAssembly
14
star
75

nontrapping-float-to-int-conversions

Proposal to add non-trapping float-to-int conversions to WebAssembly
WebAssembly
14
star
76

wasi-blobstore

14
star
77

conditional-sections

WebAssembly
13
star
78

wasi-grpc

13
star
79

profiles

Profiles proposal
WebAssembly
12
star
80

wasi-logging

WASI logging API
11
star
81

wasi-i2c

I2C API for WASI
Rust
11
star
82

extended-name-section

WebAssembly
11
star
83

feature-detection

WebAssembly
10
star
84

wasi-config

10
star
85

webassembly.github.io

Redirect to webassembly.org
HTML
10
star
86

testsuite-js

WebAssembly testsuite tests converted to single-file JavaScript tests
JavaScript
9
star
87

lld

Staging repository for upstreaming WebAssembly support into lld
C++
9
star
88

branch-hinting

Proposal to add branch hinting functionality to WebAssembly
WebAssembly
9
star
89

decompressor-prototype

C++
8
star
90

wat-numeric-values

Proposal to add numeric values to data segment definitions in the text format
WebAssembly
7
star
91

gc-js-customization

WebAssembly
7
star
92

js-string-builtins

JS String Builtins
WebAssembly
6
star
93

wasi-classic-command

6
star
94

wasi-url

6
star
95

half-precision

Proposal to introduce half precision operations
WebAssembly
6
star
96

wit-abi-up-to-date

5
star
97

wasi-spi

Rust
5
star
98

wg-charter

Proposed WebAssembly Working Group charter
HTML
5
star
99

call-tags

WebAssembly
4
star
100

cg-charter

Proposed WebAssembly Community Group charter
HTML
4
star