Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Support async/sync functions #172

Merged

Conversation

BruceDai
Copy link
Collaborator

In first commit, I just updated add op tests using Async function. @huningxin PTAL, thanks.

Copy link
Contributor

@huningxin huningxin left a comment

Choose a reason for hiding this comment

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

@BruceDai , thanks for the work, left some comments, please take a look.

package.json Outdated Show resolved Hide resolved
src/nn/graph.ts Show resolved Hide resolved
src/nn/graph.ts Outdated Show resolved Hide resolved
@BruceDai BruceDai force-pushed the support_context_based_graph branch from 95c9af5 to 7061d3a Compare June 28, 2022 08:31
@BruceDai
Copy link
Collaborator Author

Thanks @huningxin.
I updated commit to address your comments, please take another look.
My second commit of updated other tests will be summitted later to fix CI failure.

  createContext() / createContextSync()
  build() / buildSync()
  compute() / computeSync()
@BruceDai BruceDai force-pushed the support_context_based_graph branch from c0e1419 to 886b278 Compare July 1, 2022 07:58
src/nn/graph.ts Outdated Show resolved Hide resolved
src/nn/context.ts Show resolved Hide resolved
src/nn/graph_builder.ts Show resolved Hide resolved
@BruceDai
Copy link
Collaborator Author

BruceDai commented Jul 5, 2022

Thanks @huningxin !
I've added these checks, and verified by running local WPT IDL tests in worker with WebNN-Polyfill.
Please take another look, thanks.

src/ml.ts Outdated Show resolved Hide resolved
src/ml.ts Outdated Show resolved Hide resolved
src/nn/context.ts Outdated Show resolved Hide resolved
src/nn/graph.ts Outdated Show resolved Hide resolved
test/api/graph_builder.js Outdated Show resolved Hide resolved
test/api/graph_builder.js Outdated Show resolved Hide resolved

it('builder.build should throw for invalid parameters', async () => {
try {
await builder.build();
Copy link
Contributor

Choose a reason for hiding this comment

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

Should it reject with an error or throw?

test/api/graph_builder.js Outdated Show resolved Hide resolved
@BruceDai BruceDai force-pushed the support_context_based_graph branch from ef227fc to d635418 Compare July 11, 2022 13:35
@BruceDai
Copy link
Collaborator Author

@huningxin I've update this patch to address your comments, and added a sample by relu op test for invoking sync functions in worker, please take another look, thanks.

Copy link
Contributor

@Honry Honry left a comment

Choose a reason for hiding this comment

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

@BruceDai, leave a few comments, PTAL.

src/nn/graph.ts Outdated
utils.assert(
utils.isTypedArray(resource),
'Only resource of ArrayBufferView type is supported.');
utils.validateTypedArray(
resource as ArrayBufferView, inputOperand.desc.type, dimensions);
resource , inputOperand.desc.type, dimensions);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit:

Suggested change
resource , inputOperand.desc.type, dimensions);
resource, inputOperand.desc.type, dimensions);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed, thanks.

@@ -245,10 +199,18 @@ export class MLGraph {
}

/** @internal */
static buildAndCompile(outputs?: MLNamedOperands): MLGraph {
static async buildAndCompile(outputs?: MLNamedOperands): Promise<MLGraph> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can try merger buildAndCompile and buildAndCompileSync into one and declare the return types with |.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried to merge them to one function, but there's message The return type of an async function or method must be the global Promise<T> type., so I leave it as is.

return new Promise((resolve, reject) => {
let graph;
try {
graph = MLGraph.buildAndCompile(outputs);
Copy link
Contributor

Choose a reason for hiding this comment

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

buildAndCompile is async function, missing await keyword.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed, please take another look at new commit, thanks.

expect(() => navigator.ml.createContext({
powerPreference: 'invalid',
})).to.throw(Error);
describe('test MLContext.compute', function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be better to use ES6 expression for consistency purpose.

Suggested change
describe('test MLContext.compute', function() {
describe('test MLContext.compute', () => {

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated, thanks.

@BruceDai BruceDai force-pushed the support_context_based_graph branch 2 times, most recently from 0acebaa to d18b99c Compare November 9, 2022 06:50
@BruceDai
Copy link
Collaborator Author

BruceDai commented Nov 9, 2022

@Honry I've update PR, please take another review, thanks.

Copy link
Contributor

@Honry Honry left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

src/nn/graph.ts Outdated Show resolved Hide resolved
src/nn/graph.ts Outdated
compute(inputs: MLNamedInputs, outputs: MLNamedOutputs): void {
/** @internal */
async compute(
inputs:MLNamedArrayBufferViews, outputs: MLNamedArrayBufferViews,
Copy link
Contributor

@huningxin huningxin Nov 10, 2022

Choose a reason for hiding this comment

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

nit: a blank space between inputs: and MLNamedArraybufferViews

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated, thanks.

Copy link
Contributor

@huningxin huningxin left a comment

Choose a reason for hiding this comment

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

Getting better, thanks for the update.

src/nn/graph.ts Outdated Show resolved Hide resolved
src/nn/graph.ts Outdated Show resolved Hide resolved
src/nn/graph.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@huningxin huningxin left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the great work.

@BruceDai
Copy link
Collaborator Author

Thanks @Honry and @huningxin for your reviewing. I will merge this PR.

@BruceDai BruceDai merged commit abaf8ba into webmachinelearning:master Nov 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants