Skip to content

Commit

Permalink
webnn: implement logicalNot on coreml
Browse files Browse the repository at this point in the history
Also fix WPT helpers to check opSupportLimits for output type, and
cast to supported output type if current op's output type is not
supported.

Fixing WPTs causes it to take longer to run elementwise-logical tests
that it needs to be splitted up to individual tests to avoid timeouts.

Bug: 41481333
Change-Id: I33882af4264bb2f5594224d495e511e2ad6ce3a1
Cq-Include-Trybots: luci.chromium.try:win11-blink-rel,mac14.arm64-blink-rel,mac14-blink-rel, linux-blink-rel
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5719110
Auto-Submit: Phillis Tang <[email protected]>
Reviewed-by: Reilly Grant <[email protected]>
Commit-Queue: Phillis Tang <[email protected]>
Commit-Queue: David Baron <[email protected]>
Reviewed-by: David Baron <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1329923}
  • Loading branch information
philloooo authored and chromium-wpt-export-bot committed Jul 19, 2024
1 parent 1054674 commit f4f0bf7
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 33 deletions.
26 changes: 0 additions & 26 deletions webnn/conformance_tests/elementwise_logical.https.any.js

This file was deleted.

13 changes: 13 additions & 0 deletions webnn/conformance_tests/equal.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// META: title=test WebNN API element-wise binary operations
// META: global=window,dedicatedworker
// META: variant=?cpu
// META: variant=?gpu
// META: variant=?npu
// META: script=../resources/utils.js
// META: timeout=long

'use strict';

// https://webmachinelearning.github.io/webnn/#dom-mlgraphbuilder-equal

runWebNNConformanceTests('equal', buildOperationWithTwoInputs);
13 changes: 13 additions & 0 deletions webnn/conformance_tests/greater.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// META: title=test WebNN API element-wise binary operations
// META: global=window,dedicatedworker
// META: variant=?cpu
// META: variant=?gpu
// META: variant=?npu
// META: script=../resources/utils.js
// META: timeout=long

'use strict';

// https://webmachinelearning.github.io/webnn/#dom-mlgraphbuilder-greater

runWebNNConformanceTests('greater', buildOperationWithTwoInputs);
13 changes: 13 additions & 0 deletions webnn/conformance_tests/greater_or_equal.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// META: title=test WebNN API element-wise binary operations
// META: global=window,dedicatedworker
// META: variant=?cpu
// META: variant=?gpu
// META: variant=?npu
// META: script=../resources/utils.js
// META: timeout=long

'use strict';

// https://webmachinelearning.github.io/webnn/#dom-mlgraphbuilder-greaterorequal

runWebNNConformanceTests('greaterOrEqual', buildOperationWithTwoInputs);
13 changes: 13 additions & 0 deletions webnn/conformance_tests/lesser.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// META: title=test WebNN API element-wise binary operations
// META: global=window,dedicatedworker
// META: variant=?cpu
// META: variant=?gpu
// META: variant=?npu
// META: script=../resources/utils.js
// META: timeout=long

'use strict';

// https://webmachinelearning.github.io/webnn/#dom-mlgraphbuilder-lesser

runWebNNConformanceTests('lesser', buildOperationWithTwoInputs);
13 changes: 13 additions & 0 deletions webnn/conformance_tests/lesser_or_equal.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// META: title=test WebNN API element-wise binary operations
// META: global=window,dedicatedworker
// META: variant=?cpu
// META: variant=?gpu
// META: variant=?npu
// META: script=../resources/utils.js
// META: timeout=long

'use strict';

// https://webmachinelearning.github.io/webnn/#dom-mlgraphbuilder-lesserorequal

runWebNNConformanceTests('lesserOrEqual', buildOperationWithTwoInputs);
13 changes: 13 additions & 0 deletions webnn/conformance_tests/logical_not.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// META: title=test WebNN API element-wise binary operations
// META: global=window,dedicatedworker
// META: variant=?cpu
// META: variant=?gpu
// META: variant=?npu
// META: script=../resources/utils.js
// META: timeout=long

'use strict';

// https://webmachinelearning.github.io/webnn/#dom-mlgraphbuilder-logicalnot

runWebNNConformanceTests('logicalNot', buildOperationWithSingleInput);
34 changes: 27 additions & 7 deletions webnn/resources/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const kIntTypes = ['uint8', 'int8', 'uint32', 'int32', 'uint64', 'int64'];
const kFloatTypes = ['float16', 'float32'];

const findCompatibleType =
(dataType, context) => {
for (let supportedType of context.opSupportLimits().input.dataTypes) {
(dataType, supportedTypes) => {
for (let supportedType of supportedTypes) {
if (kIntTypes.includes(dataType)) {
if (kIntTypes.indexOf(supportedType) > kIntTypes.indexOf(dataType)) {
return supportedType;
Expand Down Expand Up @@ -672,8 +672,8 @@ const createSingleInputOperand =
// a supported type to pass the data, then cast back to original type.
if (!context.opSupportLimits().input.dataTypes.includes(
inputResources.type)) {
const compatible_type =
findCompatibleType(inputResources.type, context);
const compatible_type = findCompatibleType(
inputResources.type, context.opSupportLimits().input.dataTypes);
if (compatible_type) {
inputResources.castedType = compatible_type;
dataType = compatible_type;
Expand Down Expand Up @@ -727,9 +727,17 @@ const buildOperationWithSingleInput =
const namedOutputOperand = {};
const inputOperand =
createSingleInputOperand(context, builder, resources);
const outputOperand = resources.options ?
let outputOperand = resources.options ?
builder[operationName](inputOperand, resources.options) :
builder[operationName](inputOperand);
if (!context.opSupportLimits().output.dataTypes.includes(
resources.expected.type)) {
const compatibleType = findCompatibleType(
resources.expected.type,
context.opSupportLimits().output.dataTypes);
outputOperand = builder.cast(outputOperand, compatibleType);
resources.expected.castedType = compatibleType;
}
namedOutputOperand[resources.expected.name] = outputOperand;
return namedOutputOperand;
};
Expand All @@ -748,10 +756,18 @@ const buildOperationWithTwoInputs =
const namedOutputOperand = {};
const [inputOperandA, inputOperandB] =
createMultiInputOperands(context, builder, resources);
const outputOperand = resources.options ?
let outputOperand = resources.options ?
builder[operationName](
inputOperandA, inputOperandB, resources.options) :
builder[operationName](inputOperandA, inputOperandB);
if (!context.opSupportLimits().output.dataTypes.includes(
resources.expected.type)) {
const compatibleType = findCompatibleType(
resources.expected.type,
context.opSupportLimits().output.dataTypes);
outputOperand = builder.cast(outputOperand, compatibleType);
resources.expected.castedType = compatibleType;
}
namedOutputOperand[resources.expected.name] = outputOperand;
return namedOutputOperand;
};
Expand Down Expand Up @@ -979,7 +995,11 @@ const buildGraph = (operationName, context, builder, resources, buildFunc) => {
} else {
// matmul 1D with 1D produces a scalar which doesn't have its shape
const shape = resources.expected.shape ? resources.expected.shape : [1];
outputs[resources.expected.name] = new TypedArrayDict[resources.expected.type](sizeOfShape(shape));
const expectedType = resources.expected.castedType ?
resources.expected.castedType :
resources.expected.type;
outputs[resources.expected.name] =
new TypedArrayDict[expectedType](sizeOfShape(shape));
}
return [namedOperands, inputs, outputs];
};
Expand Down

0 comments on commit f4f0bf7

Please sign in to comment.