where.https.any.js (6259B)
1 // META: title=validation tests for WebNN API where operation 2 // META: global=window 3 // META: variant=?cpu 4 // META: variant=?gpu 5 // META: variant=?npu 6 // META: script=../resources/utils_validation.js 7 8 'use strict'; 9 10 const kExampleConditionDescriptor = { 11 dataType: 'uint8', 12 shape: [2, 4] 13 }; 14 const kExampleInputDescriptor = { 15 dataType: 'float32', 16 shape: [2, 4] 17 }; 18 const label = 'where_123'; 19 const regrexp = new RegExp('\\[' + label + '\\]'); 20 21 const tests = [ 22 { 23 name: '[where] Throw if the condition data type is not uint8.', 24 condition: {dataType: 'float32', shape: [2, 4]}, 25 trueValue: {dataType: 'float32', shape: [2, 4]}, 26 falseValue: {dataType: 'float32', shape: [2, 4]}, 27 }, 28 { 29 name: 30 '[where] Throw if the data types of trueValue and falseValue do not match', 31 condition: {dataType: 'uint8', shape: [2, 4]}, 32 trueValue: {dataType: 'float16', shape: [2, 4]}, 33 falseValue: {dataType: 'float32', shape: [2, 4]}, 34 }, 35 { 36 name: 37 '[where] Throw if the shapes of trueValue and falseValue are not broadcastable', 38 condition: {dataType: 'uint8', shape: [2, 4]}, 39 trueValue: {dataType: 'float32', shape: [2, 4]}, 40 falseValue: {dataType: 'float32', shape: [2, 3]}, 41 }, 42 { 43 name: '[where] Throw if the condition shape is not broadcastable', 44 condition: {dataType: 'uint8', shape: [2, 4]}, 45 trueValue: {dataType: 'float32', shape: [2, 3]}, 46 falseValue: {dataType: 'float32', shape: [2, 1]}, 47 }, 48 { 49 name: 50 '[where] Test building where with 2-D condition, 2-D trueValue and 2-D falseValue using broadcast', 51 condition: {dataType: 'uint8', shape: [2, 1]}, 52 trueValue: {dataType: 'float32', shape: [2, 4]}, 53 falseValue: {dataType: 'float32', shape: [2, 4]}, 54 output: {dataType: 'float32', shape: [2, 4]}, 55 }, 56 { 57 name: 58 '[where] Test building where with 2-D condition, 2-D trueValue and 3-D falseValue using broadcast', 59 condition: {dataType: 'uint8', shape: [1, 4]}, 60 trueValue: {dataType: 'float16', shape: [3, 4]}, 61 falseValue: {dataType: 'float16', shape: [2, 3, 4]}, 62 output: {dataType: 'float16', shape: [2, 3, 4]}, 63 }, 64 { 65 name: 66 '[where] Test building where with 3-D condition, 3-D trueValue and 2-D falseValue using broadcast', 67 condition: {dataType: 'uint8', shape: [2, 1, 4]}, 68 trueValue: {dataType: 'int32', shape: [2, 3, 4]}, 69 falseValue: {dataType: 'int32', shape: [1, 4]}, 70 output: {dataType: 'int32', shape: [2, 3, 4]}, 71 }, 72 { 73 name: 74 '[where] Test building where with 4-D condition, 3-D trueValue and 2-D falseValue using broadcast', 75 condition: {dataType: 'uint8', shape: [2, 3, 4, 5]}, 76 trueValue: {dataType: 'uint32', shape: [3, 4, 5]}, 77 falseValue: {dataType: 'uint32', shape: [4, 5]}, 78 output: {dataType: 'uint32', shape: [2, 3, 4, 5]}, 79 } 80 ]; 81 82 tests.forEach( 83 test => promise_test(async t => { 84 const builder = new MLGraphBuilder(context); 85 for (let operand of [test.condition, test.trueValue, test.falseValue]) { 86 if (!context.opSupportLimits().input.dataTypes.includes( 87 operand.dataType)) { 88 assert_throws_js(TypeError, () => builder.input('input', operand)); 89 return; 90 } 91 } 92 93 const condition = builder.input('condition', test.condition); 94 const trueValue = builder.input('trueValue', test.trueValue); 95 const falseValue = builder.input('falseValue', test.falseValue); 96 if (test.output && 97 context.opSupportLimits().where.condition.dataTypes.includes( 98 test.condition.dataType) && 99 context.opSupportLimits().where.trueValue.dataTypes.includes( 100 test.trueValue.dataType) && 101 context.opSupportLimits().where.falseValue.dataTypes.includes( 102 test.falseValue.dataType)) { 103 const output = builder.where(condition, trueValue, falseValue); 104 assert_equals(output.dataType, test.output.dataType); 105 assert_array_equals(output.shape, test.output.shape); 106 } else { 107 const options = {label}; 108 assert_throws_with_label( 109 () => builder.where(condition, trueValue, falseValue, options), 110 regrexp); 111 } 112 }, test.name)); 113 114 multi_builder_test(async (t, builder, otherBuilder) => { 115 const conditionFromOtherBuilder = 116 otherBuilder.input('condition', kExampleConditionDescriptor); 117 118 const trueValue = builder.input('trueValue', kExampleInputDescriptor); 119 const falseValue = builder.input('falseValue', kExampleInputDescriptor); 120 assert_throws_js( 121 TypeError, 122 () => builder.where(conditionFromOtherBuilder, trueValue, falseValue)); 123 }, '[where] throw if condition is from another builder'); 124 125 multi_builder_test(async (t, builder, otherBuilder) => { 126 const trueValueFromOtherBuilder = 127 otherBuilder.input('trueValue', kExampleInputDescriptor); 128 129 const condition = builder.input('condition', kExampleConditionDescriptor); 130 const falseValue = builder.input('falseValue', kExampleInputDescriptor); 131 assert_throws_js( 132 TypeError, 133 () => builder.where(condition, trueValueFromOtherBuilder, falseValue)); 134 }, '[where] throw if trueValue is from another builder'); 135 136 multi_builder_test(async (t, builder, otherBuilder) => { 137 const falseValueFromOtherBuilder = 138 otherBuilder.input('falseValue', kExampleInputDescriptor); 139 140 const condition = builder.input('condition', kExampleConditionDescriptor); 141 const trueValue = builder.input('trueValue', kExampleInputDescriptor); 142 assert_throws_js( 143 TypeError, 144 () => builder.where(condition, trueValue, falseValueFromOtherBuilder)); 145 }, '[where] throw if falseValue is from another builder'); 146 147 148 promise_test(async t => { 149 const builder = new MLGraphBuilder(context); 150 151 const condition = builder.input('condition', {dataType: 'uint8', shape: [1, 4]}); 152 const trueValue = builder.input('trueValue', { 153 dataType: 'float32', 154 shape: [context.opSupportLimits().maxTensorByteLength / 4, 1]}); 155 const falseValue = builder.input('falseValue', {dataType: 'float32', shape: [1, 4]}); 156 157 const options = {label}; 158 assert_throws_with_label( 159 () => builder.where(condition, trueValue, falseValue, options), regrexp); 160 }, '[where] throw if the output tensor byte length exceeds limit');