Skip to content

Commit

Permalink
Context-based graph execution methods for different threading models. (
Browse files Browse the repository at this point in the history
…#257)

* Context-based graph execution methods for different threading models.

* Fixing multiple refs on Promise type.

* Fix up over-aggressive search/replace.

* Remove `MLCommandEncoder` and simplify `MLContext` creation with params.

* Fix missing graph param

* Fix build break due to merge conflict.

* Fix build break: Disambiguate the WebIDL link to {{Promise}} definition.

* GPU support on sync execution (limited to worker thread). Default context only supports CPU inputs and outputs (automatic upload/download). Reintroduce MLCommandEncoder for WebGPU interop.

* Remove undefined `MLResource` type.

* Remove references to undefined MLInput type.

* Remove outdated note section in the computeAsync description. Further clarify graph initialization stage and remove the unnecessary second param.

* Add a note section describing when graph initialization occurs on a default GPU context.

* Adjust some wordings on the note section of `MLGraphBuilder.build` method.
  • Loading branch information
wchao1115 authored Apr 30, 2022
1 parent c7c0b85 commit 4c5b70b
Show file tree
Hide file tree
Showing 2 changed files with 449 additions and 206 deletions.
7 changes: 4 additions & 3 deletions explainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const bufferB = new Float32Array(4).fill(0.8);
const bufferC = new Float32Array(4);
const inputs = {'A': bufferA, 'B': bufferB};
const outputs = {'C': bufferC};
graph.compute(inputs, outputs);
context.compute(graph, inputs, outputs);
// The computed result of [[1, 1], [1, 1]] is in the buffer associated with
// the output operand.
console.log('Output value: ' + bufferC);
Expand Down Expand Up @@ -99,12 +99,13 @@ There are many important [application use cases](https://webmachinelearning.gith
export class NSNet2 {
constructor() {
this.graph = null;
this.context = null;
this.frameSize = 161;
this.hiddenSize = 400;
}

async build(baseUrl, batchSize, frames) {
const context = navigator.ml.createContext();
this.context = navigator.ml.createContext();
const builder = new MLGraphBuilder(context);
// Create constants by loading pre-trained data from .npy files.
const weight172 = await buildConstantByNpy(builder, baseUrl + '172.npy');
Expand Down Expand Up @@ -153,7 +154,7 @@ export class NSNet2 {
'gru94': gru94Buffer,
'gru157': gru157Buffer
};
return this.graph.compute(inputs, outputs);
return this.context.compute(this.graph, inputs, outputs);
}
}
```
Expand Down
Loading

0 comments on commit 4c5b70b

Please sign in to comment.