mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-05-25 22:26:24 +00:00
* Cache initializers and avoid device check ot end of forward (#7905) * ATenOp Enhancement (#7725) * config parser, default argument values * ut * win build * maxpool2d * fix win build * fix build * unfold atenop * Update CMakeLists.txt for openvino EP (#7980) * Add SoftmaxCrossEntropyLossInternal to Support Dynamic ignore_index Input (#7899) * add SoftmaxCrossEntropyLossInternal * bugfix and ut * fix ut * fix ut * support torch1.8.1 * function body for nll_loss_internal * Override ORTModule named_modules to support extra arg (#7954) * add missing provider_options.h in packages (#7995) * consolidate copy binary script for gpu/trt tarball package * add provider_options.h * add provider_options.h * Add cuda provides files (#8002) * Save module output for backward if needed (#8010) * Save module output for backward if needed * Make logic in InsertCastTransformer around forcing a node to fp32 more precise. (#8018) * Address #7981 Reworked the logic around forcing a node to run on fp32 even if it was supported on fp16. The github issue had multiple factors. In ORT 1.8 we remove Identity nodes that produce graph outputs as they're not needed. That resulted in a Loop node no longer having output nodes (it produces graph outputs instead), which meant the check in IsSingleInputNodeFloat16Node returned true as there was no longer a downstream Identity node processing fp16 data. We shouldn't only force a node to fp32 in very specific circumstances, and the changes hopefully check for those more precisely. * Fix Memory Leak from DlpackToOrtValue (#8029) * Update DirectML EP changes from DmlDev as of 2021-06-07 (#7987) * Merged PR 6093117: Fix test_DynamicQuantizedLinear_max_adjusted_expanded by allowing Identity operator to run on non-float inputs Motivation: As part of the OnnxConformance Backend tests, DynamicQuantizedLinear_max_adjusted_expanded is failing. Root Cause: - The test model has `Identity` operator as one of the node. The input of this node is of non-float data type. - In DML, `Identity` operator is registered as operator which requires floating input. - As per `DirectMLSchema.h`, support for non-float input has been added for `Identity` operator in DML but the same has not been reflected in the `OperatorRegistration.cpp`. Changes: - Removed all traces of the requiresFloatFormatsForGraph flag from it's definition and usage. This flag was only used for Identity and it's related operator. - Added null check for the graphOutput nodeArg in GraphDescBuilder.cpp to stop the crash of the test. Related work items: #33076298 * Merged PR 6103324: Remove usage of non-generic error code (FWP_E_NULL_POINTER) Motivation: Addressing Dwayne comment on the previous PR. [Ref: [6093117](https://dev.azure.com/microsoft/WindowsAI/_git/onnxruntime/pullrequest/6093117?discussionId=44292162&path=%2Fonnxruntime%2Fcore%2Fproviders%2Fdml%2FDmlExecutionProvider%2Fsrc%2FGraphPartitioner.cpp)] Changes: Inside the DML EP, we should not use some other platform specific error codes. Instead we should a appropriate generic error code. Related work items: #33076298 Co-authored-by: Sumit Agarwal <sumitagarwal@microsoft.com> * [js/react_native] Use a mobile ORT instead of a full ORT (#8042) * Change full ort to mobile ort * Update Android example to load mobile ort * Change the format of test models to ort * update ios to use mobile ort * revise README * use onnxruntime-mobile-c CocoaPods in a npm package * fix PATH addition in windows should set PATH, not add to the tail the copy of PATH * Reduce Kernel Optimization (#8067) * reduce optimization * bug fix * add a check * add ut * refactor * add ut cases for keepdims=true Co-authored-by: baijumeswani <bmeswani@microsoft.com> Co-authored-by: Vincent Wang <wangwchpku@outlook.com> Co-authored-by: Changming Sun <chasun@microsoft.com> Co-authored-by: George Wu <jywu@microsoft.com> Co-authored-by: Ryan Hill <38674843+RyanUnderhill@users.noreply.github.com> Co-authored-by: Sherlock <baihan.huang@gmail.com> Co-authored-by: Scott McKay <skottmckay@gmail.com> Co-authored-by: sumitsays <sumitagarwal330@gmail.com> Co-authored-by: Sumit Agarwal <sumitagarwal@microsoft.com> Co-authored-by: Sunghoon <35605090+hanbitmyths@users.noreply.github.com> Co-authored-by: iperov <lepersorium@gmail.com>
243 lines
11 KiB
Text
243 lines
11 KiB
Text
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT License.
|
|
|
|
#import "TensorHelper.h"
|
|
|
|
#import <XCTest/XCTest.h>
|
|
#import <onnxruntime/onnxruntime_cxx_api.h>
|
|
|
|
@interface TensorHelperTest : XCTestCase
|
|
|
|
@end
|
|
|
|
@implementation TensorHelperTest
|
|
|
|
template <typename T>
|
|
static void testCreateInputTensorT(const std::array<T, 3> &outValues, std::function<NSNumber *(T value)> &convert,
|
|
ONNXTensorElementDataType onnxType, NSString *jsTensorType) {
|
|
NSMutableDictionary *inputTensorMap = [NSMutableDictionary dictionary];
|
|
|
|
// dims
|
|
NSArray *dims = @[ [NSNumber numberWithLong:outValues.size()] ];
|
|
inputTensorMap[@"dims"] = dims;
|
|
|
|
// type
|
|
inputTensorMap[@"type"] = jsTensorType;
|
|
|
|
// encoded data
|
|
size_t byteBufferSize = sizeof(T) * outValues.size();
|
|
unsigned char *byteBuffer = static_cast<unsigned char *>(malloc(byteBufferSize));
|
|
NSData *byteBufferRef = [NSData dataWithBytesNoCopy:byteBuffer length:byteBufferSize];
|
|
T *typePtr = (T *)[byteBufferRef bytes];
|
|
for (size_t i = 0; i < outValues.size(); ++i) {
|
|
typePtr[i] = outValues[i];
|
|
}
|
|
|
|
NSString *dataEncoded = [byteBufferRef base64EncodedStringWithOptions:0];
|
|
inputTensorMap[@"data"] = dataEncoded;
|
|
|
|
Ort::AllocatorWithDefaultOptions ortAllocator;
|
|
std::vector<Ort::MemoryAllocation> allocations;
|
|
Ort::Value inputTensor = [TensorHelper createInputTensor:inputTensorMap
|
|
ortAllocator:ortAllocator
|
|
allocations:allocations];
|
|
|
|
XCTAssertEqual(inputTensor.GetTensorTypeAndShapeInfo().GetElementType(), onnxType);
|
|
XCTAssertTrue(inputTensor.IsTensor());
|
|
XCTAssertEqual(inputTensor.GetTensorTypeAndShapeInfo().GetDimensionsCount(), 1);
|
|
XCTAssertEqual(inputTensor.GetTensorTypeAndShapeInfo().GetShape(),
|
|
std::vector<int64_t>{static_cast<int64_t>(outValues.size())});
|
|
XCTAssertEqual(inputTensor.GetTensorTypeAndShapeInfo().GetElementCount(), outValues.size());
|
|
const auto tensorData = inputTensor.GetTensorData<T>();
|
|
for (size_t i = 0; i < outValues.size(); ++i) {
|
|
XCTAssertEqual(tensorData[i], outValues[i]);
|
|
}
|
|
}
|
|
|
|
- (void)testCreateInputTensorFloat {
|
|
std::array<float_t, 3> outValues{std::numeric_limits<float_t>::min(), 2.0f, std::numeric_limits<float_t>::max()};
|
|
std::function<NSNumber *(float_t value)> convert = [](float_t value) { return [NSNumber numberWithFloat:value]; };
|
|
testCreateInputTensorT<float_t>(outValues, convert, ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT, JsTensorTypeFloat);
|
|
}
|
|
|
|
- (void)testCreateInputTensorDouble {
|
|
std::array<double_t, 3> outValues{std::numeric_limits<double_t>::min(), 2.0f, std::numeric_limits<double_t>::max()};
|
|
std::function<NSNumber *(double_t value)> convert = [](double_t value) { return [NSNumber numberWithDouble:value]; };
|
|
testCreateInputTensorT<double_t>(outValues, convert, ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE, JsTensorTypeDouble);
|
|
}
|
|
|
|
- (void)testCreateInputTensorBool {
|
|
std::array<bool, 3> outValues{false, true, true};
|
|
std::function<NSNumber *(bool value)> convert = [](bool value) { return [NSNumber numberWithBool:value]; };
|
|
testCreateInputTensorT<bool>(outValues, convert, ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL, JsTensorTypeBool);
|
|
}
|
|
|
|
- (void)testCreateInputTensorInt8 {
|
|
std::array<int8_t, 3> outValues{std::numeric_limits<int8_t>::min(), 2, std::numeric_limits<int8_t>::max()};
|
|
std::function<NSNumber *(int8_t value)> convert = [](int8_t value) { return [NSNumber numberWithChar:value]; };
|
|
testCreateInputTensorT<int8_t>(outValues, convert, ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8, JsTensorTypeByte);
|
|
}
|
|
|
|
- (void)testCreateInputTensorInt16 {
|
|
std::array<int16_t, 3> outValues{std::numeric_limits<int16_t>::min(), 2, std::numeric_limits<int16_t>::max()};
|
|
std::function<NSNumber *(int16_t value)> convert = [](int16_t value) { return [NSNumber numberWithShort:value]; };
|
|
testCreateInputTensorT<int16_t>(outValues, convert, ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16, JsTensorTypeShort);
|
|
}
|
|
|
|
- (void)testCreateInputTensorInt32 {
|
|
std::array<int32_t, 3> outValues{std::numeric_limits<int32_t>::min(), 2, std::numeric_limits<int32_t>::max()};
|
|
std::function<NSNumber *(int32_t value)> convert = [](int32_t value) { return [NSNumber numberWithInt:value]; };
|
|
testCreateInputTensorT<int32_t>(outValues, convert, ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32, JsTensorTypeInt);
|
|
}
|
|
|
|
- (void)testCreateInputTensorInt64 {
|
|
std::array<int64_t, 3> outValues{std::numeric_limits<int64_t>::min(), 2, std::numeric_limits<int64_t>::max()};
|
|
std::function<NSNumber *(int64_t value)> convert = [](int64_t value) { return [NSNumber numberWithLongLong:value]; };
|
|
testCreateInputTensorT<int64_t>(outValues, convert, ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64, JsTensorTypeLong);
|
|
}
|
|
|
|
- (void)testCreateInputTensorString {
|
|
std::array<std::string, 3> outValues{"a", "b", "c"};
|
|
|
|
NSMutableDictionary *inputTensorMap = [NSMutableDictionary dictionary];
|
|
|
|
// dims
|
|
NSArray *dims = @[ [NSNumber numberWithLong:outValues.size()] ];
|
|
inputTensorMap[@"dims"] = dims;
|
|
|
|
// type
|
|
inputTensorMap[@"type"] = JsTensorTypeString;
|
|
|
|
// data
|
|
NSMutableArray *data = [NSMutableArray array];
|
|
for (auto value : outValues) {
|
|
[data addObject:[NSString stringWithUTF8String:value.c_str()]];
|
|
}
|
|
inputTensorMap[@"data"] = data;
|
|
|
|
Ort::AllocatorWithDefaultOptions ortAllocator;
|
|
std::vector<Ort::MemoryAllocation> allocations;
|
|
Ort::Value inputTensor = [TensorHelper createInputTensor:inputTensorMap
|
|
ortAllocator:ortAllocator
|
|
allocations:allocations];
|
|
|
|
XCTAssertEqual(inputTensor.GetTensorTypeAndShapeInfo().GetElementType(), ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING);
|
|
XCTAssertTrue(inputTensor.IsTensor());
|
|
XCTAssertEqual(inputTensor.GetTensorTypeAndShapeInfo().GetDimensionsCount(), 1);
|
|
XCTAssertEqual(inputTensor.GetTensorTypeAndShapeInfo().GetShape(),
|
|
std::vector<int64_t>{static_cast<int64_t>(outValues.size())});
|
|
XCTAssertEqual(inputTensor.GetTensorTypeAndShapeInfo().GetElementCount(), outValues.size());
|
|
for (int i = 0; i < inputTensor.GetTensorTypeAndShapeInfo().GetElementCount(); ++i) {
|
|
size_t elementLength = inputTensor.GetStringTensorElementLength(i);
|
|
std::string element(elementLength, '\0');
|
|
inputTensor.GetStringTensorElement(elementLength, i, (void *)element.data());
|
|
XCTAssertEqual(element, outValues[i]);
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
static void testCreateOutputTensorT(const std::array<T, 5> &outValues, std::function<NSNumber *(T value)> &convert,
|
|
NSString *jsTensorType, NSString *testDataFileName,
|
|
NSString *testDataFileExtension) {
|
|
NSBundle *bundle = [NSBundle bundleForClass:[TensorHelperTest class]];
|
|
NSString *dataPath = [bundle pathForResource:testDataFileName ofType:testDataFileExtension];
|
|
|
|
std::unique_ptr<Ort::Env> ortEnv{new Ort::Env(ORT_LOGGING_LEVEL_INFO, "Default")};
|
|
Ort::SessionOptions sessionOptions;
|
|
std::unique_ptr<Ort::Session> session{new Ort::Session(*ortEnv, [dataPath UTF8String], sessionOptions)};
|
|
|
|
Ort::AllocatorWithDefaultOptions ortAllocator;
|
|
std::vector<Ort::MemoryAllocation> allocations;
|
|
|
|
std::vector<const char *> inputNames;
|
|
inputNames.reserve(session->GetInputCount());
|
|
for (size_t i = 0; i < session->GetInputCount(); ++i) {
|
|
auto inputName = session->GetInputName(i, ortAllocator);
|
|
allocations.emplace_back(ortAllocator, inputName, strlen(inputName) + 1);
|
|
inputNames.emplace_back(inputName);
|
|
}
|
|
|
|
std::vector<const char *> outputNames;
|
|
outputNames.reserve(session->GetOutputCount());
|
|
for (size_t i = 0; i < session->GetOutputCount(); ++i) {
|
|
auto outputName = session->GetOutputName(i, ortAllocator);
|
|
allocations.emplace_back(ortAllocator, outputName, strlen(outputName) + 1);
|
|
outputNames.emplace_back(outputName);
|
|
}
|
|
|
|
NSMutableDictionary *inputTensorMap = [NSMutableDictionary dictionary];
|
|
|
|
// dims
|
|
NSArray *dims = @[ [NSNumber numberWithLong:1], [NSNumber numberWithLong:outValues.size()] ];
|
|
inputTensorMap[@"dims"] = dims;
|
|
|
|
// type
|
|
inputTensorMap[@"type"] = jsTensorType;
|
|
|
|
// encoded data
|
|
size_t byteBufferSize = sizeof(T) * outValues.size();
|
|
unsigned char *byteBuffer = static_cast<unsigned char *>(malloc(byteBufferSize));
|
|
NSData *byteBufferRef = [NSData dataWithBytesNoCopy:byteBuffer length:byteBufferSize];
|
|
T *typePtr = (T *)[byteBufferRef bytes];
|
|
for (size_t i = 0; i < outValues.size(); ++i) {
|
|
typePtr[i] = outValues[i];
|
|
}
|
|
|
|
NSString *dataEncoded = [byteBufferRef base64EncodedStringWithOptions:0];
|
|
inputTensorMap[@"data"] = dataEncoded;
|
|
|
|
Ort::Value inputTensor = [TensorHelper createInputTensor:inputTensorMap
|
|
ortAllocator:ortAllocator
|
|
allocations:allocations];
|
|
|
|
std::vector<Ort::Value> feeds;
|
|
feeds.emplace_back(std::move(inputTensor));
|
|
|
|
Ort::RunOptions runOptions;
|
|
auto output = session->Run(runOptions, inputNames.data(), feeds.data(), inputNames.size(), outputNames.data(),
|
|
outputNames.size());
|
|
|
|
NSDictionary *resultMap = [TensorHelper createOutputTensor:outputNames values:output];
|
|
|
|
XCTAssertTrue([[resultMap objectForKey:@"output"] isEqualToDictionary:inputTensorMap]);
|
|
}
|
|
|
|
- (void)testCreateOutputTensorFloat {
|
|
std::array<float_t, 5> outValues{std::numeric_limits<float_t>::min(), 1.0f, 2.0f, 3.0f,
|
|
std::numeric_limits<float_t>::max()};
|
|
std::function<NSNumber *(float_t value)> convert = [](float_t value) { return [NSNumber numberWithFloat:value]; };
|
|
testCreateOutputTensorT<float_t>(outValues, convert, JsTensorTypeFloat, @"test_types_float", @"ort");
|
|
}
|
|
|
|
- (void)testCreateOutputTensorDouble {
|
|
std::array<double_t, 5> outValues{std::numeric_limits<double_t>::min(), 1.0f, 2.0f, 3.0f,
|
|
std::numeric_limits<double_t>::max()};
|
|
std::function<NSNumber *(double_t value)> convert = [](double_t value) { return [NSNumber numberWithDouble:value]; };
|
|
testCreateOutputTensorT<double_t>(outValues, convert, JsTensorTypeDouble, @"test_types_double", @"ort");
|
|
}
|
|
|
|
- (void)testCreateOutputTensorBool {
|
|
std::array<bool, 5> outValues{false, true, true, false, true};
|
|
std::function<NSNumber *(bool value)> convert = [](bool value) { return [NSNumber numberWithBool:value]; };
|
|
testCreateOutputTensorT<bool>(outValues, convert, JsTensorTypeBool, @"test_types_bool", @"ort");
|
|
}
|
|
|
|
- (void)testCreateOutputTensorInt8 {
|
|
std::array<int8_t, 5> outValues{std::numeric_limits<int8_t>::min(), 1, -2, 3, std::numeric_limits<int8_t>::max()};
|
|
std::function<NSNumber *(int8_t value)> convert = [](int8_t value) { return [NSNumber numberWithChar:value]; };
|
|
testCreateOutputTensorT<int8_t>(outValues, convert, JsTensorTypeByte, @"test_types_int8", @"ort");
|
|
}
|
|
|
|
- (void)testCreateOutputTensorInt32 {
|
|
std::array<int32_t, 5> outValues{std::numeric_limits<int32_t>::min(), 1, -2, 3, std::numeric_limits<int32_t>::max()};
|
|
std::function<NSNumber *(int32_t value)> convert = [](int32_t value) { return [NSNumber numberWithInt:value]; };
|
|
testCreateOutputTensorT<int32_t>(outValues, convert, JsTensorTypeInt, @"test_types_int32", @"ort");
|
|
}
|
|
|
|
- (void)testCreateOutputTensorInt64 {
|
|
std::array<int64_t, 5> outValues{std::numeric_limits<int64_t>::min(), 1, -2, 3, std::numeric_limits<int64_t>::max()};
|
|
std::function<NSNumber *(int64_t value)> convert = [](int64_t value) { return [NSNumber numberWithLongLong:value]; };
|
|
testCreateOutputTensorT<int64_t>(outValues, convert, JsTensorTypeLong, @"test_types_int64", @"ort");
|
|
}
|
|
|
|
@end
|