is_nan.https.any.js (7151B)
1 // META: title=test WebNN API Element-wise logical isNaN operation 2 // META: global=window 3 // META: variant=?cpu 4 // META: variant=?gpu 5 // META: variant=?npu 6 // META: script=../resources/utils.js 7 // META: timeout=long 8 9 'use strict'; 10 11 // https://www.w3.org/TR/webnn/#api-mlgraphbuilder-logical 12 // Check if the values of the input tensor are invalid numeric representations 13 // (NaN’s), element-wise. 14 // 15 // MLOperand isNaN(MLOperand input); 16 17 const isNaNTests = [ 18 // isNaN tests 19 { 20 'name': 'isNaN float32 positive 0D scalar', 21 'graph': { 22 'inputs': { 23 'isNaNInput': { 24 'data': [1.5], 25 'descriptor': {shape: [], dataType: 'float32'} 26 } 27 }, 28 'operators': [{ 29 'name': 'isNaN', 30 'arguments': [{'input': 'isNaNInput'}], 31 'outputs': 'isNaNOutput' 32 }], 33 'expectedOutputs': { 34 'isNaNOutput': { 35 'data': [0], 36 'descriptor': {shape: [], dataType: 'uint8'} 37 } 38 } 39 } 40 }, 41 { 42 'name': 'isNaN float32 1D tensor', 43 'graph': { 44 'inputs': { 45 'isNaNInput': { 46 'data': [1.5, NaN], 47 'descriptor': {shape: [2], dataType: 'float32'} 48 } 49 }, 50 'operators': [{ 51 'name': 'isNaN', 52 'arguments': [{'input': 'isNaNInput'}], 53 'outputs': 'isNaNOutput' 54 }], 55 'expectedOutputs': { 56 'isNaNOutput': { 57 'data': [0, 1], 58 'descriptor': {shape: [2], dataType: 'uint8'} 59 } 60 } 61 } 62 }, 63 { 64 'name': 'isNaN float32 2D tensor', 65 'graph': { 66 'inputs': { 67 'isNaNInput': { 68 'data': [1.0, NaN, -2.5, 0.0], 69 'descriptor': {shape: [2, 2], dataType: 'float32'} 70 } 71 }, 72 'operators': [{ 73 'name': 'isNaN', 74 'arguments': [{'input': 'isNaNInput'}], 75 'outputs': 'isNaNOutput' 76 }], 77 'expectedOutputs': { 78 'isNaNOutput': { 79 'data': [0, 1, 0, 0], 80 'descriptor': {shape: [2, 2], dataType: 'uint8'} 81 } 82 } 83 } 84 }, 85 { 86 'name': 'isNaN float32 3D tensor', 87 'graph': { 88 'inputs': { 89 'isNaNInput': { 90 'data': [1.0, NaN, -2.5, 0.0, Infinity, -Infinity, 3.14, NaN], 91 'descriptor': {shape: [2, 2, 2], dataType: 'float32'} 92 } 93 }, 94 'operators': [{ 95 'name': 'isNaN', 96 'arguments': [{'input': 'isNaNInput'}], 97 'outputs': 'isNaNOutput' 98 }], 99 'expectedOutputs': { 100 'isNaNOutput': { 101 'data': [0, 1, 0, 0, 0, 0, 0, 1], 102 'descriptor': {shape: [2, 2, 2], dataType: 'uint8'} 103 } 104 } 105 } 106 }, 107 { 108 'name': 'isNaN float32 4D tensor', 109 'graph': { 110 'inputs': { 111 'isNaNInput': { 112 'data': [1.0, NaN, -2.5, 0.0, Infinity, -Infinity, 3.14, NaN, 113 -0.0, 42.0, NaN, -999.99, 1e10, -1e-10, NaN, 100.5], 114 'descriptor': {shape: [2, 2, 2, 2], dataType: 'float32'} 115 } 116 }, 117 'operators': [{ 118 'name': 'isNaN', 119 'arguments': [{'input': 'isNaNInput'}], 120 'outputs': 'isNaNOutput' 121 }], 122 'expectedOutputs': { 123 'isNaNOutput': { 124 'data': [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0], 125 'descriptor': {shape: [2, 2, 2, 2], dataType: 'uint8'} 126 } 127 } 128 } 129 }, 130 { 131 'name': 'isNaN float32 special values', 132 'graph': { 133 'inputs': { 134 'isNaNInput': { 135 'data': [NaN, Infinity, -Infinity, 0.0, -0.0, 1.0, -1.0], 136 'descriptor': {shape: [7], dataType: 'float32'} 137 } 138 }, 139 'operators': [{ 140 'name': 'isNaN', 141 'arguments': [{'input': 'isNaNInput'}], 142 'outputs': 'isNaNOutput' 143 }], 144 'expectedOutputs': { 145 'isNaNOutput': { 146 'data': [1, 0, 0, 0, 0, 0, 0], 147 'descriptor': {shape: [7], dataType: 'uint8'} 148 } 149 } 150 } 151 }, 152 { 153 'name': 'isNaN float32 all NaN values', 154 'graph': { 155 'inputs': { 156 'isNaNInput': { 157 'data': [NaN, NaN, NaN, NaN], 158 'descriptor': {shape: [4], dataType: 'float32'} 159 } 160 }, 161 'operators': [{ 162 'name': 'isNaN', 163 'arguments': [{'input': 'isNaNInput'}], 164 'outputs': 'isNaNOutput' 165 }], 166 'expectedOutputs': { 167 'isNaNOutput': { 168 'data': [1, 1, 1, 1], 169 'descriptor': {shape: [4], dataType: 'uint8'} 170 } 171 } 172 } 173 }, 174 { 175 'name': 'isNaN float32 no NaN values', 176 'graph': { 177 'inputs': { 178 'isNaNInput': { 179 'data': [1.0, 2.5, -3.7, 0.0, Infinity, -Infinity], 180 'descriptor': {shape: [6], dataType: 'float32'} 181 } 182 }, 183 'operators': [{ 184 'name': 'isNaN', 185 'arguments': [{'input': 'isNaNInput'}], 186 'outputs': 'isNaNOutput' 187 }], 188 'expectedOutputs': { 189 'isNaNOutput': { 190 'data': [0, 0, 0, 0, 0, 0], 191 'descriptor': {shape: [6], dataType: 'uint8'} 192 } 193 } 194 } 195 }, 196 197 // float16 tests 198 { 199 'name': 'isNaN float16 positive 0D scalar', 200 'graph': { 201 'inputs': { 202 'isNaNInput': { 203 'data': [1.5], 204 'descriptor': {shape: [], dataType: 'float16'} 205 } 206 }, 207 'operators': [{ 208 'name': 'isNaN', 209 'arguments': [{'input': 'isNaNInput'}], 210 'outputs': 'isNaNOutput' 211 }], 212 'expectedOutputs': { 213 'isNaNOutput': { 214 'data': [0], 215 'descriptor': {shape: [], dataType: 'uint8'} 216 } 217 } 218 } 219 }, 220 { 221 'name': 'isNaN float16 1D tensor', 222 'graph': { 223 'inputs': { 224 'isNaNInput': { 225 'data': [1.5, NaN], 226 'descriptor': {shape: [2], dataType: 'float16'} 227 } 228 }, 229 'operators': [{ 230 'name': 'isNaN', 231 'arguments': [{'input': 'isNaNInput'}], 232 'outputs': 'isNaNOutput' 233 }], 234 'expectedOutputs': { 235 'isNaNOutput': { 236 'data': [0, 1], 237 'descriptor': {shape: [2], dataType: 'uint8'} 238 } 239 } 240 } 241 }, 242 { 243 'name': 'isNaN float16 2D tensor', 244 'graph': { 245 'inputs': { 246 'isNaNInput': { 247 'data': [1.0, NaN, -2.5, 0.0], 248 'descriptor': {shape: [2, 2], dataType: 'float16'} 249 } 250 }, 251 'operators': [{ 252 'name': 'isNaN', 253 'arguments': [{'input': 'isNaNInput'}], 254 'outputs': 'isNaNOutput' 255 }], 256 'expectedOutputs': { 257 'isNaNOutput': { 258 'data': [0, 1, 0, 0], 259 'descriptor': {shape: [2, 2], dataType: 'uint8'} 260 } 261 } 262 } 263 }, 264 { 265 'name': 'isNaN float16 special values', 266 'graph': { 267 'inputs': { 268 'isNaNInput': { 269 'data': [NaN, Infinity, -Infinity, 0.0, -0.0, 1.0, -1.0], 270 'descriptor': {shape: [7], dataType: 'float16'} 271 } 272 }, 273 'operators': [{ 274 'name': 'isNaN', 275 'arguments': [{'input': 'isNaNInput'}], 276 'outputs': 'isNaNOutput' 277 }], 278 'expectedOutputs': { 279 'isNaNOutput': { 280 'data': [1, 0, 0, 0, 0, 0, 0], 281 'descriptor': {shape: [7], dataType: 'uint8'} 282 } 283 } 284 } 285 }, 286 ] 287 288 webnn_conformance_test(isNaNTests, buildAndExecuteGraph, getZeroULPTolerance);