slice.https.any.js (3442B)
1 // META: title=validation tests for WebNN API slice 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 multi_builder_test(async (t, builder, otherBuilder) => { 11 const inputFromOtherBuilder = 12 otherBuilder.input('input', {dataType: 'float32', shape: [2, 2]}); 13 14 const starts = [1, 1]; 15 const sizes = [1, 1]; 16 assert_throws_js( 17 TypeError, () => builder.slice(inputFromOtherBuilder, starts, sizes)); 18 }, '[slice] throw if input is from another builder'); 19 20 const tests = [ 21 { 22 name: '[slice] Test with starts=[0, 1, 2] and sizes=[1, 2, 3].', 23 input: {dataType: 'float32', shape: [3, 4, 5]}, 24 starts: [0, 1, 2], 25 sizes: [1, 2, 3], 26 output: {dataType: 'float32', shape: [1, 2, 3]} 27 }, 28 { 29 name: '[slice] Test slicing a scalar with empty starts and sizes.', 30 input: {dataType: 'float32', shape: []}, 31 starts: [], 32 sizes: [], 33 output: {dataType: 'float32', shape: []} 34 }, 35 { 36 name: '[slice] Throw if input is a scalar and starts/sizes are not empty.', 37 input: {dataType: 'float32', shape: []}, 38 starts: [0], 39 sizes: [1] 40 }, 41 { 42 name: 43 '[slice] Throw if the length of sizes is not equal to the rank of the input tensor.', 44 input: {dataType: 'float32', shape: [3, 4, 5]}, 45 starts: [1, 2, 3], 46 sizes: [1, 1] 47 }, 48 { 49 name: 50 '[slice] Throw if the length of starts is not equal to the rank of the input tensor.', 51 input: {dataType: 'float32', shape: [3, 4, 5]}, 52 starts: [1, 2, 1, 3], 53 sizes: [1, 1, 1] 54 }, 55 { 56 name: 57 '[slice] Throw if the starting index is equal to or greater than input size in the same dimension.', 58 input: {dataType: 'float32', shape: [3, 4, 5]}, 59 starts: [0, 4, 4], 60 sizes: [1, 1, 1] 61 }, 62 { 63 name: '[slice] Throw if the number of elements to slice is equal to 0.', 64 input: {dataType: 'float32', shape: [3, 4, 5]}, 65 starts: [1, 2, 3], 66 sizes: [1, 0, 1] 67 }, 68 { 69 name: 70 '[slice] Throw if the ending index to slice is greater than input size in the same dimension.', 71 input: {dataType: 'float32', shape: [3, 4, 5]}, 72 starts: [0, 1, 2], 73 sizes: [3, 4, 1] 74 }, 75 { 76 name: 77 '[slice] Throw if the length of strides is not equal to the rank of the input tensor.', 78 input: {dataType: 'float32', shape: [3, 4, 5]}, 79 starts: [1, 2, 3], 80 sizes: [1, 1, 1], 81 strides: [1, 1, 1, 1] 82 }, 83 { 84 name: '[slice] Throw if the strides are less than 1.', 85 input: {dataType: 'float32', shape: [3, 4, 5]}, 86 starts: [1, 2, 3], 87 sizes: [1, 1, 1], 88 strides: [0, 0, 0] 89 } 90 ]; 91 92 tests.forEach( 93 test => promise_test(async t => { 94 const builder = new MLGraphBuilder(context); 95 const input = builder.input('input', test.input); 96 const options = {}; 97 if (test.strides) { 98 options.strides = test.strides; 99 } 100 101 if (test.output) { 102 const output = builder.slice(input, test.starts, test.sizes); 103 assert_equals(output.dataType, test.output.dataType); 104 assert_array_equals(output.shape, test.output.shape); 105 } else { 106 const label = 'slice_xxx'; 107 options.label = label; 108 const regrexp = new RegExp('\\[' + label + '\\]'); 109 assert_throws_with_label( 110 () => builder.slice(input, test.starts, test.sizes, options), 111 regrexp); 112 } 113 }, test.name));