pad.https.any.js (4508B)
1 // META: title=validation tests for WebNN API pad 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 label = 'pad_xxx'; 11 const regrexp = new RegExp('\\[' + label + '\\]'); 12 13 multi_builder_test(async (t, builder, otherBuilder) => { 14 const inputFromOtherBuilder = 15 otherBuilder.input('input', {dataType: 'float32', shape: [2, 2]}); 16 17 const beginningPadding = [1, 1]; 18 const endingPadding = [1, 1]; 19 assert_throws_js( 20 TypeError, 21 () => 22 builder.pad(inputFromOtherBuilder, beginningPadding, endingPadding)); 23 }, '[pad] throw if input is from another builder'); 24 25 promise_test(async t => { 26 const builder = new MLGraphBuilder(context); 27 28 const input = builder.input('input', { 29 dataType: 'float32', 30 shape: [1, context.opSupportLimits().maxTensorByteLength / 4]}); 31 32 const options = {}; 33 options.value = 0; 34 options.mode = 'constant'; 35 options.label = label; 36 const beginningPadding = [1, 2]; 37 const endingPadding = [1, 2]; 38 assert_throws_with_label( 39 () => builder.pad(input, beginningPadding, endingPadding, options), regrexp); 40 }, '[pad] throw if the output tensor byte length exceeds limit'); 41 42 const tests = [ 43 { 44 name: 45 '[pad] Test with default options, beginningPadding=[1, 2] and endingPadding=[1, 2].', 46 input: {dataType: 'float32', shape: [2, 3]}, 47 beginningPadding: [1, 2], 48 endingPadding: [1, 2], 49 options: { 50 mode: 'constant', 51 value: 0, 52 }, 53 output: {dataType: 'float32', shape: [4, 7]} 54 }, 55 { 56 name: 57 '[pad] Test pad for scalar input with empty beginningPadding and endingPadding.', 58 input: {dataType: 'float32', shape: []}, 59 beginningPadding: [], 60 endingPadding: [], 61 output: {dataType: 'float32', shape: []} 62 }, 63 { 64 name: 65 '[pad] Throw if the length of beginningPadding is not equal to the input rank.', 66 input: {dataType: 'float32', shape: [2, 3]}, 67 beginningPadding: [1], 68 endingPadding: [1, 2], 69 options: { 70 mode: 'edge', 71 value: 0, 72 label: label, 73 }, 74 }, 75 { 76 name: 77 '[pad] Throw if the length of endingPadding is not equal to the input rank.', 78 input: {dataType: 'float32', shape: [2, 3]}, 79 beginningPadding: [1, 0], 80 endingPadding: [1, 2, 0], 81 options: { 82 mode: 'reflection', 83 label: label, 84 }, 85 }, 86 { 87 name: 88 '[pad] Throw if beginningPadding[index] is equal to inputShape[index] on reflection mode.', 89 input: {dataType: 'float32', shape: [2, 3]}, 90 beginningPadding: [2, 0], 91 endingPadding: [1, 2], 92 options: { 93 mode: 'reflection', 94 label: label, 95 }, 96 }, 97 { 98 name: 99 '[pad] Throw if beginningPadding[index] is greater than inputShape[index] on reflection mode.', 100 input: {dataType: 'float32', shape: [2, 3]}, 101 beginningPadding: [3, 0], 102 endingPadding: [1, 2], 103 options: { 104 mode: 'reflection', 105 label: label, 106 }, 107 }, 108 { 109 name: 110 '[pad] Throw if endingPadding[index] is equal to inputShape[index] on reflection mode.', 111 input: {dataType: 'float32', shape: [2, 3]}, 112 beginningPadding: [1, 0], 113 endingPadding: [1, 3], 114 options: { 115 mode: 'reflection', 116 label: label, 117 }, 118 }, 119 { 120 name: 121 '[pad] Throw if endingPadding[index] is greater than inputShape[index] on reflection mode.', 122 input: {dataType: 'float32', shape: [2, 3]}, 123 beginningPadding: [1, 0], 124 endingPadding: [1, 4], 125 options: { 126 mode: 'reflection', 127 label: label, 128 }, 129 }, 130 { 131 name: '[pad] Throw if the padding of one dimension is too large.', 132 input: {dataType: 'float32', shape: [2, 3]}, 133 beginningPadding: [2294967295, 0], 134 endingPadding: [3294967295, 2], 135 options: { 136 mode: 'reflection', 137 label: label, 138 }, 139 }, 140 ]; 141 142 tests.forEach( 143 test => promise_test(async t => { 144 const builder = new MLGraphBuilder(context); 145 const input = builder.input('input', test.input); 146 if (test.output) { 147 const output = builder.pad( 148 input, test.beginningPadding, test.endingPadding, test.options); 149 assert_equals(output.dataType, test.output.dataType); 150 assert_array_equals(output.shape, test.output.shape); 151 } else { 152 assert_throws_with_label( 153 () => builder.pad( 154 input, test.beginningPadding, test.endingPadding, test.options), 155 regrexp); 156 } 157 }, test.name));