|
4 | 4 |
|
5 | 5 | 'use strict'; |
6 | 6 |
|
7 | | -validateInputFromAnotherBuilder('softmax'); |
| 7 | +const tests_without_axis = [ |
| 8 | + { |
| 9 | + name: '[softmax] Test building Softmax with float32 input without axis.', |
| 10 | + input: { dataType: 'float32', dimensions: [4, 3] }, |
| 11 | + output: { dataType: 'float32', dimensions: [4, 3] } |
| 12 | + }, |
| 13 | + { |
| 14 | + name: '[softmax] Test building Softmax with float16 input without axis.', |
| 15 | + input: { dataType: 'float16', dimensions: [3, 5] }, |
| 16 | + output: { dataType: 'float16', dimensions: [3, 5] } |
| 17 | + }, |
| 18 | + { |
| 19 | + name: '[softmax] Throw if the input is not a non-floating point data.', |
| 20 | + input: { dataType: 'int32', dimensions: [3, 2] } |
| 21 | + }, |
| 22 | + { |
| 23 | + name: '[softmax] Throw if the input dimensions is not 2.', |
| 24 | + input: { dataType: 'float32', dimensions: [1, 4, 3] } |
| 25 | + } |
| 26 | +]; |
| 27 | + |
| 28 | +tests_without_axis.forEach(test => |
| 29 | + promise_test(async t => { |
| 30 | + let input = builder.input( |
| 31 | + `input`, { dataType: test.input.dataType, dimensions: test.input.dimensions } |
| 32 | + ); |
| 33 | + if (test.output) { |
| 34 | + const output = builder.softmax(input); |
| 35 | + assert_equals(output.dataType(), test.output.dataType); |
| 36 | + assert_array_equals(output.shape(), test.output.dimensions); |
| 37 | + } else { |
| 38 | + assert_throws_js(TypeError, () => builder.softmax(input)); |
| 39 | + } |
| 40 | + }, test.name) |
| 41 | +); |
| 42 | + |
| 43 | +multi_builder_test(async (t, builder, otherBuilder) => { |
| 44 | + const operandDescriptor = { dataType: 'float32', dimensions: [2, 3] }; |
| 45 | + const inputFromOtherBuilder = otherBuilder.input('input', operandDescriptor); |
| 46 | + |
| 47 | + assert_throws_js( |
| 48 | + TypeError, |
| 49 | + () => builder.softmax(inputFromOtherBuilder)); |
| 50 | +}, '[softmax without axis] throw if any input is from another builder'); |
| 51 | + |
| 52 | +const tests = [ |
| 53 | + { |
| 54 | + name: '[softmax] Test building Softmax with float32 input.', |
| 55 | + input: { dataType: 'float32', dimensions: [4, 4, 3] }, |
| 56 | + axis: 1, |
| 57 | + output: { dataType: 'float32', dimensions: [4, 4, 3] } |
| 58 | + }, |
| 59 | + { |
| 60 | + name: '[softmax] Test building Softmax with float16 input.', |
| 61 | + input: { dataType: 'float16', dimensions: [3, 1, 5, 2] }, |
| 62 | + axis: 2, |
| 63 | + output: { dataType: 'float16', dimensions: [3, 1, 5, 2] } |
| 64 | + }, |
| 65 | + { |
| 66 | + name: '[softmax] Throw if the input is not a non-floating-point data.', |
| 67 | + input: { dataType: 'int32', dimensions: [3, 1, 5, 2] }, |
| 68 | + axis: 3 |
| 69 | + }, |
| 70 | + { |
| 71 | + name: '[softmax] Throw if the axis is greater than input rank - 1.', |
| 72 | + input: { dataType: 'float16', dimensions: [3, 1, 5, 2] }, |
| 73 | + axis: 4 |
| 74 | + } |
| 75 | +]; |
| 76 | + |
| 77 | +tests.forEach(test => |
| 78 | + promise_test(async t => { |
| 79 | + let input = builder.input( |
| 80 | + `input`, { dataType: test.input.dataType, dimensions: test.input.dimensions } |
| 81 | + ); |
| 82 | + if (test.output) { |
| 83 | + const output = builder.softmax(input, test.axis); |
| 84 | + assert_equals(output.dataType(), test.output.dataType); |
| 85 | + assert_array_equals(output.shape(), test.output.dimensions); |
| 86 | + } else { |
| 87 | + assert_throws_js(TypeError, () => builder.softmax(input, test.axis)); |
| 88 | + } |
| 89 | + }, test.name) |
| 90 | +); |
| 91 | + |
| 92 | +multi_builder_test(async (t, builder, otherBuilder) => { |
| 93 | + const operandDescriptor = { dataType: 'float32', dimensions: [1, 2, 3] }; |
| 94 | + const inputFromOtherBuilder = otherBuilder.input('input', operandDescriptor); |
| 95 | + const axis = 1; |
| 96 | + |
| 97 | + assert_throws_js( |
| 98 | + TypeError, |
| 99 | + () => builder.softmax(inputFromOtherBuilder, axis)); |
| 100 | +}, '[softmax] throw if any input is from another builder'); |
0 commit comments