summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webnn/validation_tests/clamp.https.any.js
blob: 126fa90e167fb344d2b41f71d85f1c3f4ed8addd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// META: title=validation tests for WebNN API clamp operation
// META: global=window,dedicatedworker
// META: script=../resources/utils_validation.js

'use strict';

validateInputFromAnotherBuilder('clamp');

validateUnaryOperation(
    'clamp', allWebNNOperandDataTypes, /*alsoBuildActivation=*/ true);

promise_test(async t => {
  const options = {minValue: 1.0, maxValue: 3.0};
  const input =
      builder.input('input', {dataType: 'uint32', dimensions: [1, 2, 3]});
  const output = builder.clamp(input, options);
  assert_equals(output.dataType(), 'uint32');
  assert_array_equals(output.shape(), [1, 2, 3]);
}, '[clamp] Test building an operator with options');

promise_test(async t => {
  const options = {minValue: 0, maxValue: 0};
  const input =
      builder.input('input', {dataType: 'int32', dimensions: [1, 2, 3, 4]});
  const output = builder.clamp(input, options);
  assert_equals(output.dataType(), 'int32');
  assert_array_equals(output.shape(), [1, 2, 3, 4]);
}, '[clamp] Test building an operator with options.minValue == options.maxValue');

promise_test(async t => {
  const options = {minValue: 2.0};
  builder.clamp(options);
}, '[clamp] Test building an activation with options');

promise_test(async t => {
  const options = {minValue: 3.0, maxValue: 1.0};
  const input =
      builder.input('input', {dataType: 'uint8', dimensions: [1, 2, 3]});
  assert_throws_js(TypeError, () => builder.clamp(input, options));
}, '[clamp] Throw if options.minValue > options.maxValue when building an operator');

// To be removed once infinite `minValue` is allowed. Tracked in
// https://github.com/webmachinelearning/webnn/pull/647.
promise_test(async t => {
  const options = {minValue: -Infinity};
  const input = builder.input('input', {dataType: 'float16', dimensions: []});
  assert_throws_js(TypeError, () => builder.clamp(input, options));
}, '[clamp] Throw if options.minValue is -Infinity when building an operator');

promise_test(async t => {
  const options = {minValue: 2.0, maxValue: -1.0};
  assert_throws_js(TypeError, () => builder.clamp(options));
}, '[clamp] Throw if options.minValue > options.maxValue when building an activation');

// To be removed once NaN `maxValue` is allowed. Tracked in
// https://github.com/webmachinelearning/webnn/pull/647.
promise_test(async t => {
  const options = {maxValue: NaN};
  assert_throws_js(TypeError, () => builder.clamp(options));
}, '[clamp] Throw if options.maxValue is NaN when building an activation');