onnxruntime/js/node/test/e2e/simple-e2e-tests.ts
Yulong Wang 009f342caf
[JS] refactor Javascript/Typescript libraries in ONNX Runtime (#7308)
* working on re-organizing js code for ortweb

* remove dup files

* move folder

* fix common references

* fix common es5

* add webpack to common

* split interfact/impl

* use cjs for node

* add npmignore for common

* update sourcemap config for common

* update node

* adjust folder/path in CI and build

* update folder

* nit: readme

* add bundle for dev

* correct nodejs paths

* enable ORT_API_MANUAL_INIT

* set name for umd library

* correct name for commonjs export

* add priority into registerBackend()

* fix npm ci pwd

* update eslintrc

* revise code

* revert package-lock lockfileVersion 2->1

* update prebuild

* resolve comments

* update document

* revise eslint config

* update eslint for typescript rules

* revert changes by mistake in backend.ts

* add env

* resolve comments
2021-04-16 01:33:10 -07:00

105 lines
4.3 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import assert from 'assert';
import {InferenceSession} from 'onnxruntime-common';
import * as path from 'path';
import {Tensor} from '../../lib';
import {assertDataEqual, TEST_DATA_ROOT} from '../test-utils';
const MODEL_TEST_TYPES_CASES:
Array<{model: string; type: Tensor.Type; input0: Tensor.DataType; expectedOutput0: Tensor.DataType}> = [
{
model: path.join(TEST_DATA_ROOT, 'test_types_BOOL.pb'),
type: 'bool',
input0: Uint8Array.from([1, 0, 0, 1, 0]),
expectedOutput0: Uint8Array.from([1, 0, 0, 1, 0])
},
{
model: path.join(TEST_DATA_ROOT, 'test_types_DOUBLE.pb'),
type: 'float64',
input0: Float64Array.from([1.0, 2.0, 3.0, 4.0, 5.0]),
expectedOutput0: Float64Array.from([1.0, 2.0, 3.0, 4.0, 5.0])
},
{
model: path.join(TEST_DATA_ROOT, 'test_types_FLOAT.pb'),
type: 'float32',
input0: Float32Array.from([1.0, 2.0, 3.0, 4.0, 5.0]),
expectedOutput0: Float32Array.from([1.0, 2.0, 3.0, 4.0, 5.0])
},
{
model: path.join(TEST_DATA_ROOT, 'test_types_INT8.pb'),
type: 'int8',
input0: Int8Array.from([1, -2, 3, 4, -5]),
expectedOutput0: Int8Array.from([1, -2, 3, 4, -5])
},
{
model: path.join(TEST_DATA_ROOT, 'test_types_INT16.pb'),
type: 'int16',
input0: Int16Array.from([1, -2, 3, 4, -5]),
expectedOutput0: Int16Array.from([1, -2, 3, 4, -5])
},
{
model: path.join(TEST_DATA_ROOT, 'test_types_INT32.pb'),
type: 'int32',
input0: Int32Array.from([1, -2, 3, 4, -5]),
expectedOutput0: Int32Array.from([1, -2, 3, 4, -5])
},
{
model: path.join(TEST_DATA_ROOT, 'test_types_INT64.pb'),
type: 'int64',
input0: BigInt64Array.from([BigInt(1), BigInt(-2), BigInt(3), BigInt(4), BigInt(-5)]),
expectedOutput0: BigInt64Array.from([BigInt(1), BigInt(-2), BigInt(3), BigInt(4), BigInt(-5)])
},
{
model: path.join(TEST_DATA_ROOT, 'test_types_STRING.pb'),
type: 'string',
input0: ['a', 'b', 'c', 'd', 'e'],
expectedOutput0: ['a', 'b', 'c', 'd', 'e']
},
{
model: path.join(TEST_DATA_ROOT, 'test_types_UINT8.pb'),
type: 'uint8',
input0: Uint8Array.from([1, 2, 3, 4, 5]),
expectedOutput0: Uint8Array.from([1, 2, 3, 4, 5])
},
{
model: path.join(TEST_DATA_ROOT, 'test_types_UINT16.pb'),
type: 'uint16',
input0: Uint16Array.from([1, 2, 3, 4, 5]),
expectedOutput0: Uint16Array.from([1, 2, 3, 4, 5])
},
{
model: path.join(TEST_DATA_ROOT, 'test_types_UINT32.pb'),
type: 'uint32',
input0: Uint32Array.from([1, 2, 3, 4, 5]),
expectedOutput0: Uint32Array.from([1, 2, 3, 4, 5])
},
{
model: path.join(TEST_DATA_ROOT, 'test_types_UINT64.pb'),
type: 'uint64',
input0: BigUint64Array.from([BigInt(1), BigInt(2), BigInt(3), BigInt(4), BigInt(5)]),
expectedOutput0: BigUint64Array.from([BigInt(1), BigInt(2), BigInt(3), BigInt(4), BigInt(5)])
},
];
describe('E2E Tests - simple E2E tests', () => {
MODEL_TEST_TYPES_CASES.forEach(testCase => {
it(`${testCase.model}`, async () => {
const session = await InferenceSession.create(testCase.model);
const output = await session.run({'input': new Tensor(testCase.type, testCase.input0, [1, 5])});
assert(Object.prototype.hasOwnProperty.call(output, 'output'), '\'output\' should be in the result object.');
assert(output.output instanceof Tensor, 'result[output] should be a Tensor object.');
assert.strictEqual(output.output.size, 5, `output size expected 5, got ${output.output.size}.`);
assert.strictEqual(
output.output.type, testCase.type, `tensor type expected ${testCase.type}, got ${output.output.type}.`);
assert.strictEqual(
Object.getPrototypeOf(output.output.data), Object.getPrototypeOf(testCase.expectedOutput0),
`tensor data expected ${Object.getPrototypeOf(testCase.expectedOutput0).constructor.name}, got ${
Object.getPrototypeOf(output.output.data).constructor.name}`);
assertDataEqual(testCase.type, output.output.data, testCase.expectedOutput0);
});
});
});