clamp.https.any.js (3363B)
1 // META: title=validation tests for WebNN API clamp 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 validateInputFromAnotherBuilder('clamp'); 11 12 const label = '123_clamp'; 13 14 validateSingleInputOperation('clamp', label); 15 16 promise_test(async t => { 17 const builder = new MLGraphBuilder(context); 18 const options = {minValue: 1.0, maxValue: 3.0}; 19 const input = builder.input('input', {dataType: 'float32', shape: [1, 2, 3]}); 20 const output = builder.clamp(input, options); 21 assert_equals(output.dataType, 'float32'); 22 assert_array_equals(output.shape, [1, 2, 3]); 23 }, '[clamp] Build with options'); 24 25 promise_test(async t => { 26 const builder = new MLGraphBuilder(context); 27 const options = {minValue: 0, maxValue: 0}; 28 const input = 29 builder.input('input', {dataType: 'float32', shape: [1, 2, 3, 4]}); 30 const output = builder.clamp(input, options); 31 assert_equals(output.dataType, 'float32'); 32 assert_array_equals(output.shape, [1, 2, 3, 4]); 33 }, '[clamp] Build with options.minValue == options.maxValue'); 34 35 promise_test(async t => { 36 const builder = new MLGraphBuilder(context); 37 const options = { 38 minValue: 3.0, 39 maxValue: 1.0, 40 label: label, 41 }; 42 const input = builder.input('input', {dataType: 'float32', shape: [1, 2, 3]}); 43 const regrexp = new RegExp('\\[' + label + '\\]'); 44 assert_throws_with_label(() => builder.clamp(input, options), regrexp); 45 }, '[clamp] Throw if options.minValue > options.maxValue'); 46 47 promise_test(async t => { 48 const builder = new MLGraphBuilder(context); 49 const options = {minValue: 3n, maxValue: 1n}; 50 if (!context.opSupportLimits().input.dataTypes.includes('int64')) { 51 assert_throws_js( 52 TypeError, 53 () => builder.input('input', {dataType: 'int64', shape: [1, 2, 3]})); 54 return; 55 } 56 const input = builder.input('input', {dataType: 'int64', shape: [1, 2, 3]}); 57 assert_throws_js(TypeError, () => builder.clamp(input, options)); 58 }, '[clamp] Throw if options.minValue BigInt > options.maxValue BigInt'); 59 60 promise_test(async t => { 61 const builder = new MLGraphBuilder(context); 62 const options = {minValue: 3, maxValue: 1n}; 63 if (!context.opSupportLimits().input.dataTypes.includes('int64')) { 64 assert_throws_js( 65 TypeError, 66 () => builder.input('input', {dataType: 'int64', shape: [1, 2, 3]})); 67 return; 68 } 69 const input = builder.input('input', {dataType: 'int64', shape: [1, 2, 3]}); 70 assert_throws_js(TypeError, () => builder.clamp(input, options)); 71 }, '[clamp] Throw if options.minValue > options.maxValue BigInt'); 72 73 promise_test(async t => { 74 const builder = new MLGraphBuilder(context); 75 const options = { minValue: 1n, maxValue: 3n }; 76 const data_types = ['float32', 'float16', 'int32', 'uint32', 'int8', 'uint8', 'int4', 'uint4']; 77 for (const data_type of data_types) { 78 if (!context.opSupportLimits().input.dataTypes.includes(data_type)) { 79 assert_throws_js( 80 TypeError, 81 () => builder.input('input', { dataType: data_type, shape: [1, 2, 3] })); 82 return; 83 } 84 const input = builder.input('input', { dataType: data_type, shape: [1, 2, 3] }); 85 assert_throws_js(TypeError, () => builder.clamp(input, options)); 86 } 87 }, '[clamp] Throw if BigInt is used for data types other than int64 and uint64');