Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Residence proposal #70

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions wit/wasi-nn.wit
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@ world ml {
import errors;
}

/// Inference is performed on a specific `device`.
interface device {
/// Define where tensors reside and graphs execute.
record device {
name: string
}

/// List the available devices for a given backend.
available-devices: func(backend: backend) -> list<device>;
}

/// All inputs and outputs to an ML inference are represented as `tensor`s.
interface tensor {
use device.{device};

/// The dimensions of a tensor.
///
/// The array length matches the tensor rank and each element in the array describes the size of
Expand Down Expand Up @@ -44,6 +57,7 @@ interface tensor {
type tensor-data = list<u8>;

resource tensor {
/// Construct a tensor that lives on the host CPU.
constructor(dimensions: tensor-dimensions, ty: tensor-type, data: tensor-data);

// Describe the size of the tensor (e.g., 2x2x2x2 -> [2, 2, 2, 2]). To represent a tensor
Expand All @@ -53,7 +67,15 @@ interface tensor {
// Describe the type of element in the tensor (e.g., `f32`).
ty: func() -> tensor-type;

// Return the tensor data.
// Describe where the tensor is currently located (e.g., `cpu`, `gpu`, `tpu`).
location: func() -> device;

// Move the tensor to a different device. This operation may result in an expensive data
// copy.
move-to: func(device: device) -> result<tensor, error>;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtr's suggestion: rename to copy-to since that more accurately reflects the semantics here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mingqiusun suggests to.


// Return the tensor data. If the tensor is located on a device other than the CPU, this
// operation may result in an expensive data copy operation.
data: func() -> tensor-data;
}
}
Expand All @@ -62,8 +84,9 @@ interface tensor {
/// framework (e.g., TensorFlow):
interface graph {
use errors.{error};
use tensor.{tensor};
use device.{device};
use inference.{graph-execution-context};
use tensor.{tensor};

/// An execution graph for performing inference (i.e., a model).
resource graph {
Expand All @@ -81,21 +104,15 @@ interface graph {
autodetect,
}

/// Define where the graph should be executed.
enum execution-target {
cpu,
gpu,
tpu
}

/// The graph initialization data.
///
/// This gets bundled up into an array of buffers because implementing backends may encode their
/// graph IR in parts (e.g., OpenVINO stores its IR and weights separately).
type graph-builder = list<u8>;

/// Load a `graph` from an opaque sequence of bytes to use for inference.
load: func(builder: list<graph-builder>, encoding: graph-encoding, target: execution-target) -> result<graph, error>;
/// Load a `graph` from an opaque sequence of bytes to use for inference on the specified device
/// `location`.
load: func(builder: list<graph-builder>, encoding: graph-encoding, location: device) -> result<graph, error>;

/// Load a `graph` by name.
///
Expand All @@ -116,6 +133,11 @@ interface inference {
/// TODO: this may no longer be necessary in WIT
/// (https://github.com/WebAssembly/wasi-nn/issues/43)
resource graph-execution-context {
/// Load a tensor using the graph context. Unlike the `tensor` constructor, this function
/// will co-locate the tensor data on a specific device using the graph's underlying
/// backend; this may avoid some copies, improving performance.
load-tensor: func(dimensions: tensor-dimensions, ty: tensor-type, data: tensor-data) -> result<tensor, error>;

/// Define the inputs to use for inference.
set-input: func(name: string, tensor: tensor) -> result<_, error>;

Expand Down
Loading