|
| 1 | +package wasi:nn |
| 2 | + |
| 3 | +world inference { |
| 4 | + import tensor |
| 5 | + import graph |
| 6 | + import execution |
| 7 | + import errors |
| 8 | +} |
| 9 | + |
| 10 | +interface tensor { |
| 11 | + type tensor-dimensions = list<u32> |
| 12 | + type tensor-data = list<u8> |
| 13 | + |
| 14 | + enum tensor-type { |
| 15 | + fp16, |
| 16 | + fp32, |
| 17 | + bf16, |
| 18 | + up8, |
| 19 | + ip32 |
| 20 | + } |
| 21 | + |
| 22 | + record tensor { |
| 23 | + // Describe the size of the tensor (e.g., 2x2x2x2 -> [2, 2, 2, 2]). To represent a tensor |
| 24 | + // containing a single value, use `[1]` for the tensor dimensions. |
| 25 | + dimensions: tensor-dimensions, |
| 26 | + |
| 27 | + // Describe the type of element in the tensor (e.g., f32). |
| 28 | + tensor-type: tensor-type, |
| 29 | + |
| 30 | + // Contains the tensor data. |
| 31 | + data: tensor-data, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +interface graph { |
| 36 | + use errors.{error} |
| 37 | + type graph-builder = list<u8> |
| 38 | + type graph-builder-array = list<graph-builder> |
| 39 | + use tensor.{tensor} |
| 40 | + |
| 41 | + type graph = u32 |
| 42 | + |
| 43 | + enum graph-encoding { |
| 44 | + openvino, |
| 45 | + onnx, |
| 46 | + tensorflow, |
| 47 | + pytorch, |
| 48 | + tensorflowlite, |
| 49 | + autodetect, |
| 50 | + } |
| 51 | + |
| 52 | + enum execution-target { |
| 53 | + cpu, |
| 54 | + gpu, |
| 55 | + tpu |
| 56 | + } |
| 57 | + |
| 58 | + load: func(builder: graph-builder-array, encoding: graph-encoding, target: execution-target) -> result<graph, error> |
| 59 | + load-named-model: func(name: string) -> result<graph, error> |
| 60 | +} |
| 61 | + |
| 62 | +interface execution { |
| 63 | + use errors.{error} |
| 64 | + use tensor.{tensor, tensor-data} |
| 65 | + use graph.{graph} |
| 66 | + |
| 67 | + type graph-execution-context = u32 |
| 68 | + init-execution-context: func(graph: graph) -> result<graph-execution-context, error> |
| 69 | + set-input: func(ctx: graph-execution-context, index: u32, tensor: tensor) -> result<_, error> |
| 70 | + set-input-by-name: func(ctx: graph-execution-context, name: string, tensor: tensor) -> result<_, error> |
| 71 | + compute: func(ctx: graph-execution-context) -> result<_, error> |
| 72 | + get-output: func(ctx: graph-execution-context, index: u32) -> result<list<tensor-data>, error> |
| 73 | + eval: func(tensors: list<tensor>) -> result<list<tensor>, error> |
| 74 | + |
| 75 | +} |
| 76 | + |
| 77 | +interface errors { |
| 78 | + enum error { |
| 79 | + // Caller module passed an invalid argument. |
| 80 | + invalid-argument, |
| 81 | + // Invalid encoding. |
| 82 | + invalid-encoding, |
| 83 | + busy, |
| 84 | + // Runtime Error. |
| 85 | + runtime-error, |
| 86 | + // Unsupported operation |
| 87 | + unsupported-operation, |
| 88 | + // Model too large |
| 89 | + model-too-large, |
| 90 | + // Model not found |
| 91 | + model-not-found |
| 92 | + } |
| 93 | +} |
0 commit comments