// META: title=test WebNN API element-wise cos operation // META: global=window // META: variant=?cpu // META: variant=?gpu // META: variant=?npu // META: script=../resources/utils.js // META: timeout=long 'use strict'; // https://www.w3.org/TR/webnn/#api-mlgraphbuilder-unary // Compute the cosine of the input tensor, element-wise. // // MLOperand cos(MLOperand input); const getCosPrecisionTolerance = (graphResources) => { // Use the float accuracy for WGSL for float16: // https://gpuweb.github.io/gpuweb/wgsl/#concrete-float-accuracy const toleranceValueDict = {float32: 2 ** -10, float16: 2 ** -7}; const expectedDataType = getExpectedDataTypeOfSingleOutput(graphResources.expectedOutputs); return {metricType: 'ATOL', value: toleranceValueDict[expectedDataType]}; }; const cosTests = [ { 'name': 'cos float32 0D scalar', 'graph': { 'inputs': { 'cosInput': { 'data': [-2.4009041149673322], 'descriptor': {shape: [], dataType: 'float32'} } }, 'operators': [{ 'name': 'cos', 'arguments': [{'input': 'cosInput'}], 'outputs': 'cosOutput' }], 'expectedOutputs': { 'cosOutput': { 'data': [-0.7380040884017944], 'descriptor': {shape: [], dataType: 'float32'} } } } }, { 'name': 'cos float32 1D constant tensor', 'graph': { 'inputs': { 'cosInput': { 'data': [ -2.4009041149673322, -1.1175411065788339, -0.429725885470269, 1.808886416821629, 0.04802187281199366, 1.0653891722940827, -2.3977027575912615, -1.3261746248294415, -1.3153583208192998, -1.1028532187407407, 1.8888955433741899, -1.4786912441727864, 1.8273826281967303, -2.851855166821629, -1.6838571072098958, -0.743461799684809, -2.51020827298138, 1.9872438431418402, 1.8580675760061922, -0.7797077497373408, 1.7245051702074363, 2.0227612337161602, 1.1678468704855902, 2.4311166127101274 ], 'descriptor': {shape: [24], dataType: 'float32'}, 'constant': true } }, 'operators': [{ 'name': 'cos', 'arguments': [{'input': 'cosInput'}], 'outputs': 'cosOutput' }], 'expectedOutputs': { 'cosOutput': { 'data': [ -0.7380040884017944, 0.43789437413215637, 0.9090799689292908, -0.23584702610969543, 0.9988471865653992, 0.48416373133659363, -0.7358400821685791, 0.24218930304050446, 0.25266921520233154, 0.4510514736175537, -0.31276169419288635, 0.09197491407394409, -0.2537800967693329, -0.9583188891410828, -0.11282006651163101, 0.736129879951477, -0.80721116065979, -0.4045141637325287, -0.283336341381073, 0.7111190557479858, -0.1531042903661728, -0.43673399090766907, 0.39213326573371887, -0.7580515146255493 ], 'descriptor': {shape: [24], dataType: 'float32'} } } } }, { 'name': 'cos float32 1D tensor', 'graph': { 'inputs': { 'cosInput': { 'data': [ -2.4009041149673322, -1.1175411065788339, -0.429725885470269, 1.808886416821629, 0.04802187281199366, 1.0653891722940827, -2.3977027575912615, -1.3261746248294415, -1.3153583208192998, -1.1028532187407407, 1.8888955433741899, -1.4786912441727864, 1.8273826281967303, -2.851855166821629, -1.6838571072098958, -0.743461799684809, -2.51020827298138, 1.9872438431418402, 1.8580675760061922, -0.7797077497373408, 1.7245051702074363, 2.0227612337161602, 1.1678468704855902, 2.4311166127101274 ], 'descriptor': {shape: [24], dataType: 'float32'} } }, 'operators': [{ 'name': 'cos', 'arguments': [{'input': 'cosInput'}], 'outputs': 'cosOutput' }], 'expectedOutputs': { 'cosOutput': { 'data': [ -0.7380040884017944, 0.43789437413215637, 0.9090799689292908, -0.23584702610969543, 0.9988471865653992, 0.48416373133659363, -0.7358400821685791, 0.24218930304050446, 0.25266921520233154, 0.4510514736175537, -0.31276169419288635, 0.09197491407394409, -0.2537800967693329, -0.9583188891410828, -0.11282006651163101, 0.736129879951477, -0.80721116065979, -0.4045141637325287, -0.283336341381073, 0.7111190557479858, -0.1531042903661728, -0.43673399090766907, 0.39213326573371887, -0.7580515146255493 ], 'descriptor': {shape: [24], dataType: 'float32'} } } } }, { 'name': 'cos float32 2D tensor', 'graph': { 'inputs': { 'cosInput': { 'data': [ -2.4009041149673322, -1.1175411065788339, -0.429725885470269, 1.808886416821629, 0.04802187281199366, 1.0653891722940827, -2.3977027575912615, -1.3261746248294415, -1.3153583208192998, -1.1028532187407407, 1.8888955433741899, -1.4786912441727864, 1.8273826281967303, -2.851855166821629, -1.6838571072098958, -0.743461799684809, -2.51020827298138, 1.9872438431418402, 1.8580675760061922, -0.7797077497373408, 1.7245051702074363, 2.0227612337161602, 1.1678468704855902, 2.4311166127101274 ], 'descriptor': {shape: [4, 6], dataType: 'float32'} } }, 'operators': [{ 'name': 'cos', 'arguments': [{'input': 'cosInput'}], 'outputs': 'cosOutput' }], 'expectedOutputs': { 'cosOutput': { 'data': [ -0.7380040884017944, 0.43789437413215637, 0.9090799689292908, -0.23584702610969543, 0.9988471865653992, 0.48416373133659363, -0.7358400821685791, 0.24218930304050446, 0.25266921520233154, 0.4510514736175537, -0.31276169419288635, 0.09197491407394409, -0.2537800967693329, -0.9583188891410828, -0.11282006651163101, 0.736129879951477, -0.80721116065979, -0.4045141637325287, -0.283336341381073, 0.7111190557479858, -0.1531042903661728, -0.43673399090766907, 0.39213326573371887, -0.7580515146255493 ], 'descriptor': {shape: [4, 6], dataType: 'float32'} } } } }, { 'name': 'cos float32 3D tensor', 'graph': { 'inputs': { 'cosInput': { 'data': [ -2.4009041149673322, -1.1175411065788339, -0.429725885470269, 1.808886416821629, 0.04802187281199366, 1.0653891722940827, -2.3977027575912615, -1.3261746248294415, -1.3153583208192998, -1.1028532187407407, 1.8888955433741899, -1.4786912441727864, 1.8273826281967303, -2.851855166821629, -1.6838571072098958, -0.743461799684809, -2.51020827298138, 1.9872438431418402, 1.8580675760061922, -0.7797077497373408, 1.7245051702074363, 2.0227612337161602, 1.1678468704855902, 2.4311166127101274 ], 'descriptor': {shape: [2, 3, 4], dataType: 'float32'} } }, 'operators': [{ 'name': 'cos', 'arguments': [{'input': 'cosInput'}], 'outputs': 'cosOutput' }], 'expectedOutputs': { 'cosOutput': { 'data': [ -0.7380040884017944, 0.43789437413215637, 0.9090799689292908, -0.23584702610969543, 0.9988471865653992, 0.48416373133659363, -0.7358400821685791, 0.24218930304050446, 0.25266921520233154, 0.4510514736175537, -0.31276169419288635, 0.09197491407394409, -0.2537800967693329, -0.9583188891410828, -0.11282006651163101, 0.736129879951477, -0.80721116065979, -0.4045141637325287, -0.283336341381073, 0.7111190557479858, -0.1531042903661728, -0.43673399090766907, 0.39213326573371887, -0.7580515146255493 ], 'descriptor': {shape: [2, 3, 4], dataType: 'float32'} } } } }, { 'name': 'cos float32 4D tensor', 'graph': { 'inputs': { 'cosInput': { 'data': [ -2.4009041149673322, -1.1175411065788339, -0.429725885470269, 1.808886416821629, 0.04802187281199366, 1.0653891722940827, -2.3977027575912615, -1.3261746248294415, -1.3153583208192998, -1.1028532187407407, 1.8888955433741899, -1.4786912441727864, 1.8273826281967303, -2.851855166821629, -1.6838571072098958, -0.743461799684809, -2.51020827298138, 1.9872438431418402, 1.8580675760061922, -0.7797077497373408, 1.7245051702074363, 2.0227612337161602, 1.1678468704855902, 2.4311166127101274 ], 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'} } }, 'operators': [{ 'name': 'cos', 'arguments': [{'input': 'cosInput'}], 'outputs': 'cosOutput' }], 'expectedOutputs': { 'cosOutput': { 'data': [ -0.7380040884017944, 0.43789437413215637, 0.9090799689292908, -0.23584702610969543, 0.9988471865653992, 0.48416373133659363, -0.7358400821685791, 0.24218930304050446, 0.25266921520233154, 0.4510514736175537, -0.31276169419288635, 0.09197491407394409, -0.2537800967693329, -0.9583188891410828, -0.11282006651163101, 0.736129879951477, -0.80721116065979, -0.4045141637325287, -0.283336341381073, 0.7111190557479858, -0.1531042903661728, -0.43673399090766907, 0.39213326573371887, -0.7580515146255493 ], 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'} } } } }, { 'name': 'cos float32 5D tensor', 'graph': { 'inputs': { 'cosInput': { 'data': [ -2.4009041149673322, -1.1175411065788339, -0.429725885470269, 1.808886416821629, 0.04802187281199366, 1.0653891722940827, -2.3977027575912615, -1.3261746248294415, -1.3153583208192998, -1.1028532187407407, 1.8888955433741899, -1.4786912441727864, 1.8273826281967303, -2.851855166821629, -1.6838571072098958, -0.743461799684809, -2.51020827298138, 1.9872438431418402, 1.8580675760061922, -0.7797077497373408, 1.7245051702074363, 2.0227612337161602, 1.1678468704855902, 2.4311166127101274 ], 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float32'} } }, 'operators': [{ 'name': 'cos', 'arguments': [{'input': 'cosInput'}], 'outputs': 'cosOutput' }], 'expectedOutputs': { 'cosOutput': { 'data': [ -0.7380040884017944, 0.43789437413215637, 0.9090799689292908, -0.23584702610969543, 0.9988471865653992, 0.48416373133659363, -0.7358400821685791, 0.24218930304050446, 0.25266921520233154, 0.4510514736175537, -0.31276169419288635, 0.09197491407394409, -0.2537800967693329, -0.9583188891410828, -0.11282006651163101, 0.736129879951477, -0.80721116065979, -0.4045141637325287, -0.283336341381073, 0.7111190557479858, -0.1531042903661728, -0.43673399090766907, 0.39213326573371887, -0.7580515146255493 ], 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float32'} } } } }, // float16 tests { 'name': 'cos float16 0D scalar', 'graph': { 'inputs': { 'cosInput': {'data': [85.5625], 'descriptor': {shape: [], dataType: 'float16'}} }, 'operators': [{ 'name': 'cos', 'arguments': [{'input': 'cosInput'}], 'outputs': 'cosOutput' }], 'expectedOutputs': { 'cosOutput': { 'data': [-0.73876953125], 'descriptor': {shape: [], dataType: 'float16'} } } } }, { 'name': 'cos float16 1D constant tensor', 'graph': { 'inputs': { 'cosInput': { 'data': [ -2.402, -1.111, -0.4397, 1.818, 0.04092, 1.072, -2.41, -1.318, -1.301, -1.115, 1.891, -1.482, 1.848, -2.88, -1.67, -0.7266, -2.514, 1.977, 1.844, -0.793, 1.727, 2.006, 1.164, 2.422 ], 'descriptor': {shape: [24], dataType: 'float16'}, 'constant': true } }, 'operators': [{ 'name': 'cos', 'arguments': [{'input': 'cosInput'}], 'outputs': 'cosOutput' }], 'expectedOutputs': { 'cosOutput': { 'data': [ -0.73876953125, 0.443359375, 0.90478515625, -0.2452392578125, 0.9990234375, 0.47802734375, -0.744140625, 0.24951171875, 0.266845703125, 0.43994140625, -0.314208984375, 0.08807373046875, -0.273681640625, -0.96630859375, -0.09912109375, 0.74755859375, -0.8095703125, -0.39501953125, -0.269287109375, 0.70166015625, -0.1551513671875, -0.421630859375, 0.3955078125, -0.751953125 ], 'descriptor': {shape: [24], dataType: 'float16'} } } } }, { 'name': 'cos float16 1D tensor', 'graph': { 'inputs': { 'cosInput': { 'data': [ -2.402, -1.111, -0.4397, 1.818, 0.04092, 1.072, -2.41, -1.318, -1.301, -1.115, 1.891, -1.482, 1.848, -2.88, -1.67, -0.7266, -2.514, 1.977, 1.844, -0.793, 1.727, 2.006, 1.164, 2.422 ], 'descriptor': {shape: [24], dataType: 'float16'} } }, 'operators': [{ 'name': 'cos', 'arguments': [{'input': 'cosInput'}], 'outputs': 'cosOutput' }], 'expectedOutputs': { 'cosOutput': { 'data': [ -0.73876953125, 0.443359375, 0.90478515625, -0.2452392578125, 0.9990234375, 0.47802734375, -0.744140625, 0.24951171875, 0.266845703125, 0.43994140625, -0.314208984375, 0.08807373046875, -0.273681640625, -0.96630859375, -0.09912109375, 0.74755859375, -0.8095703125, -0.39501953125, -0.269287109375, 0.70166015625, -0.1551513671875, -0.421630859375, 0.3955078125, -0.751953125 ], 'descriptor': {shape: [24], dataType: 'float16'} } } } }, { 'name': 'cos float16 2D tensor', 'graph': { 'inputs': { 'cosInput': { 'data': [ -2.402, -1.111, -0.4397, 1.818, 0.04092, 1.072, -2.41, -1.318, -1.301, -1.115, 1.891, -1.482, 1.848, -2.88, -1.67, -0.7266, -2.514, 1.977, 1.844, -0.793, 1.727, 2.006, 1.164, 2.422 ], 'descriptor': {shape: [4, 6], dataType: 'float16'} } }, 'operators': [{ 'name': 'cos', 'arguments': [{'input': 'cosInput'}], 'outputs': 'cosOutput' }], 'expectedOutputs': { 'cosOutput': { 'data': [ -0.73876953125, 0.443359375, 0.90478515625, -0.2452392578125, 0.9990234375, 0.47802734375, -0.744140625, 0.24951171875, 0.266845703125, 0.43994140625, -0.314208984375, 0.08807373046875, -0.273681640625, -0.96630859375, -0.09912109375, 0.74755859375, -0.8095703125, -0.39501953125, -0.269287109375, 0.70166015625, -0.1551513671875, -0.421630859375, 0.3955078125, -0.751953125 ], 'descriptor': {shape: [4, 6], dataType: 'float16'} } } } }, { 'name': 'cos float16 3D tensor', 'graph': { 'inputs': { 'cosInput': { 'data': [ -2.402, -1.111, -0.4397, 1.818, 0.04092, 1.072, -2.41, -1.318, -1.301, -1.115, 1.891, -1.482, 1.848, -2.88, -1.67, -0.7266, -2.514, 1.977, 1.844, -0.793, 1.727, 2.006, 1.164, 2.422 ], 'descriptor': {shape: [2, 3, 4], dataType: 'float16'} } }, 'operators': [{ 'name': 'cos', 'arguments': [{'input': 'cosInput'}], 'outputs': 'cosOutput' }], 'expectedOutputs': { 'cosOutput': { 'data': [ -0.73876953125, 0.443359375, 0.90478515625, -0.2452392578125, 0.9990234375, 0.47802734375, -0.744140625, 0.24951171875, 0.266845703125, 0.43994140625, -0.314208984375, 0.08807373046875, -0.273681640625, -0.96630859375, -0.09912109375, 0.74755859375, -0.8095703125, -0.39501953125, -0.269287109375, 0.70166015625, -0.1551513671875, -0.421630859375, 0.3955078125, -0.751953125 ], 'descriptor': {shape: [2, 3, 4], dataType: 'float16'} } } } }, { 'name': 'cos float16 4D tensor', 'graph': { 'inputs': { 'cosInput': { 'data': [ -2.402, -1.111, -0.4397, 1.818, 0.04092, 1.072, -2.41, -1.318, -1.301, -1.115, 1.891, -1.482, 1.848, -2.88, -1.67, -0.7266, -2.514, 1.977, 1.844, -0.793, 1.727, 2.006, 1.164, 2.422 ], 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float16'} } }, 'operators': [{ 'name': 'cos', 'arguments': [{'input': 'cosInput'}], 'outputs': 'cosOutput' }], 'expectedOutputs': { 'cosOutput': { 'data': [ -0.73876953125, 0.443359375, 0.90478515625, -0.2452392578125, 0.9990234375, 0.47802734375, -0.744140625, 0.24951171875, 0.266845703125, 0.43994140625, -0.314208984375, 0.08807373046875, -0.273681640625, -0.96630859375, -0.09912109375, 0.74755859375, -0.8095703125, -0.39501953125, -0.269287109375, 0.70166015625, -0.1551513671875, -0.421630859375, 0.3955078125, -0.751953125 ], 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float16'} } } } }, { 'name': 'cos float16 5D tensor', 'graph': { 'inputs': { 'cosInput': { 'data': [ -2.402, -1.111, -0.4397, 1.818, 0.04092, 1.072, -2.41, -1.318, -1.301, -1.115, 1.891, -1.482, 1.848, -2.88, -1.67, -0.7266, -2.514, 1.977, 1.844, -0.793, 1.727, 2.006, 1.164, 2.422 ], 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float16'} } }, 'operators': [{ 'name': 'cos', 'arguments': [{'input': 'cosInput'}], 'outputs': 'cosOutput' }], 'expectedOutputs': { 'cosOutput': { 'data': [ -0.73876953125, 0.443359375, 0.90478515625, -0.2452392578125, 0.9990234375, 0.47802734375, -0.744140625, 0.24951171875, 0.266845703125, 0.43994140625, -0.314208984375, 0.08807373046875, -0.273681640625, -0.96630859375, -0.09912109375, 0.74755859375, -0.8095703125, -0.39501953125, -0.269287109375, 0.70166015625, -0.1551513671875, -0.421630859375, 0.3955078125, -0.751953125 ], 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float16'} } } } } ]; webnn_conformance_test( cosTests, buildAndExecuteGraph, getCosPrecisionTolerance);