[Objective-C API] Fixes from package testing and clean up (#7866)

This commit is contained in:
Edward Chen 2021-05-27 19:36:50 -07:00 committed by GitHub
parent 35b49b64c7
commit ab4b5055c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 116 additions and 86 deletions

View file

@ -1,26 +1,32 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
if(${CMAKE_VERSION} VERSION_LESS "3.18")
message(FATAL_ERROR "CMake 3.18+ is required when building the Objective-C API.")
if(NOT APPLE)
message(FATAL_ERROR "The Objective-C API must be built on an Apple platform.")
endif()
if(NOT APPLE)
message(FATAL_ERROR "Objective-C API must be built on an Apple platform.")
set(ONNXRUNTIME_OBJC_MIN_CMAKE_VERSION "3.18")
if(CMAKE_VERSION VERSION_LESS ONNXRUNTIME_OBJC_MIN_CMAKE_VERSION)
message(FATAL_ERROR "The Objective-C API requires CMake ${ONNXRUNTIME_OBJC_MIN_CMAKE_VERSION}+.")
endif()
if(NOT onnxruntime_BUILD_SHARED_LIB)
message(FATAL_ERROR "The Objective-C API requires onnxruntime_BUILD_SHARED_LIB to be enabled.")
endif()
check_language(OBJC)
if(CMAKE_OBJC_COMPILER)
enable_language(OBJC)
enable_language(OBJC)
else()
message(FATAL_ERROR "Objective-C is not supported.")
message(FATAL_ERROR "Objective-C is not supported.")
endif()
check_language(OBJCXX)
if(CMAKE_OBJCXX_COMPILER)
enable_language(OBJCXX)
enable_language(OBJCXX)
else()
message(FATAL_ERROR "Objective-C++ is not supported.")
message(FATAL_ERROR "Objective-C++ is not supported.")
endif()
add_compile_options(
@ -45,30 +51,24 @@ set(onnxruntime_objc_headers
"${OBJC_ROOT}/include/ort_value.h"
)
file(GLOB onnxruntime_objc_srcs
file(GLOB onnxruntime_objc_srcs CONFIGURE_DEPENDS
"${OBJC_ROOT}/src/*.h"
"${OBJC_ROOT}/src/*.m"
"${OBJC_ROOT}/src/*.mm")
# files common to implementation and test targets
set(onnxruntime_objc_common_srcs
"${OBJC_ROOT}/common/assert_arc_enabled.mm")
source_group(TREE "${OBJC_ROOT}" FILES
${onnxruntime_objc_headers}
${onnxruntime_objc_srcs}
${onnxruntime_objc_common_srcs})
${onnxruntime_objc_srcs})
onnxruntime_add_shared_library(onnxruntime_objc
${onnxruntime_objc_headers}
${onnxruntime_objc_srcs}
${onnxruntime_objc_common_srcs})
${onnxruntime_objc_srcs})
target_include_directories(onnxruntime_objc
PUBLIC
"${OBJC_ROOT}/include"
PRIVATE
"${ONNXRUNTIME_ROOT}"
"${ONNXRUNTIME_ROOT}/include/onnxruntime/core/session"
"${OBJC_ROOT}")
find_library(FOUNDATION_LIB Foundation REQUIRED)
@ -112,7 +112,7 @@ if(onnxruntime_BUILD_UNIT_TESTS)
# onnxruntime_objc_test target
file(GLOB onnxruntime_objc_test_srcs
file(GLOB onnxruntime_objc_test_srcs CONFIGURE_DEPENDS
"${OBJC_ROOT}/test/*.h"
"${OBJC_ROOT}/test/*.m"
"${OBJC_ROOT}/test/*.mm")
@ -121,8 +121,7 @@ if(onnxruntime_BUILD_UNIT_TESTS)
xctest_add_bundle(onnxruntime_objc_test onnxruntime_objc
${onnxruntime_objc_headers}
${onnxruntime_objc_test_srcs}
${onnxruntime_objc_common_srcs})
${onnxruntime_objc_test_srcs})
onnxruntime_configure_target(onnxruntime_objc_test)
@ -131,12 +130,13 @@ if(onnxruntime_BUILD_UNIT_TESTS)
"${OBJC_ROOT}")
set_target_properties(onnxruntime_objc_test PROPERTIES
FOLDER "ONNXRuntimeTest")
FOLDER "ONNXRuntimeTest"
XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO")
add_custom_command(TARGET onnxruntime_objc_test POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${OBJC_ROOT}/test/testdata"
"$<TARGET_BUNDLE_CONTENT_DIR:onnxruntime_objc_test>/Resources/testdata")
"$<TARGET_BUNDLE_CONTENT_DIR:onnxruntime_objc_test>/Resources")
xctest_add_test(XCTest.onnxruntime_objc_test onnxruntime_objc_test)

View file

@ -7,4 +7,3 @@ set -e
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
clang-format -i $(find ${SCRIPT_DIR} -name "*.h" -o -name "*.m" -o -name "*.mm")

View file

@ -5,7 +5,7 @@
#include <exception>
#include "core/session/onnxruntime_cxx_api.h"
#include "onnxruntime_cxx_api.h"
NS_ASSUME_NONNULL_BEGIN

View file

@ -5,7 +5,7 @@
#include <algorithm>
#include "core/session/onnxruntime_cxx_api.h"
#include "onnxruntime_cxx_api.h"
namespace {

View file

@ -3,7 +3,7 @@
#import "ort_enums.h"
#include "core/session/onnxruntime_c_api.h"
#include "onnxruntime_c_api.h"
OrtLoggingLevel PublicToCAPILoggingLevel(ORTLoggingLevel logging_level);

View file

@ -5,7 +5,7 @@
#include <optional>
#include "core/session/onnxruntime_cxx_api.h"
#include "onnxruntime_cxx_api.h"
#import "src/error_utils.h"
#import "src/ort_enums_internal.h"

View file

@ -3,7 +3,7 @@
#import "ort_env.h"
#include "core/session/onnxruntime_cxx_api.h"
#include "onnxruntime_cxx_api.h"
NS_ASSUME_NONNULL_BEGIN

View file

@ -6,7 +6,7 @@
#include <optional>
#include <vector>
#include "core/session/onnxruntime_cxx_api.h"
#include "onnxruntime_cxx_api.h"
#import "src/error_utils.h"
#import "src/ort_enums_internal.h"

View file

@ -3,7 +3,7 @@
#import "ort_session.h"
#include "core/session/onnxruntime_cxx_api.h"
#include "onnxruntime_cxx_api.h"
NS_ASSUME_NONNULL_BEGIN

View file

@ -7,7 +7,7 @@
#include "safeint/SafeInt.hpp"
#include "core/session/onnxruntime_cxx_api.h"
#include "onnxruntime_cxx_api.h"
#import "src/error_utils.h"
#import "src/ort_enums_internal.h"

View file

@ -3,7 +3,7 @@
#import "ort_value.h"
#include "core/session/onnxruntime_cxx_api.h"
#include "onnxruntime_cxx_api.h"
NS_ASSUME_NONNULL_BEGIN

View file

@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
static_assert(__has_feature(objc_arc), "Objective-C ARC must be enabled.");

View file

@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#import <XCTest/XCTest.h>
NS_ASSUME_NONNULL_BEGIN
#define ORTAssertNullableResultSuccessful(result, error) \
do { \
XCTAssertNotNil(result, @"Expected non-nil result but got nil. Error: %@", error); \
XCTAssertNil(error); \
} while (0)
#define ORTAssertBoolResultSuccessful(result, error) \
do { \
XCTAssertTrue(result, @"Expected true result but got false. Error: %@", error); \
XCTAssertNil(error); \
} while (0)
#define ORTAssertNullableResultUnsuccessful(result, error) \
do { \
XCTAssertNil(result); \
XCTAssertNotNil(error); \
} while (0)
#define ORTAssertBoolResultUnsuccessful(result, error) \
do { \
XCTAssertFalse(result); \
XCTAssertNotNil(error); \
} while (0)
NS_ASSUME_NONNULL_END

View file

@ -5,6 +5,8 @@
#import "ort_env.h"
#import "test/assertion_utils.h"
NS_ASSUME_NONNULL_BEGIN
@interface ORTEnvTest : XCTestCase
@ -16,8 +18,7 @@ NS_ASSUME_NONNULL_BEGIN
NSError* err = nil;
ORTEnv* env = [[ORTEnv alloc] initWithLoggingLevel:ORTLoggingLevelWarning
error:&err];
XCTAssertNotNil(env);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(env, err);
}
@end

View file

@ -7,6 +7,8 @@
#import "ort_session.h"
#import "ort_value.h"
#import "test/assertion_utils.h"
#include <vector>
NS_ASSUME_NONNULL_BEGIN
@ -24,9 +26,10 @@ NS_ASSUME_NONNULL_BEGIN
self.continueAfterFailure = NO;
NSError* err = nil;
_ortEnv = [[ORTEnv alloc] initWithLoggingLevel:ORTLoggingLevelWarning
error:nil];
XCTAssertNotNil(_ortEnv);
error:&err];
ORTAssertNullableResultSuccessful(_ortEnv, err);
}
- (void)tearDown {
@ -35,17 +38,14 @@ NS_ASSUME_NONNULL_BEGIN
[super tearDown];
}
+ (NSString*)getTestDataWithRelativePath:(NSString*)relativePath {
NSString* testDataDir = [NSString stringWithFormat:@"%@/Contents/Resources/testdata",
[[NSBundle bundleForClass:[ORTSessionTest class]] bundlePath]];
return [testDataDir stringByAppendingString:relativePath];
}
// model with an Add op
// inputs: A, B
// output: C = A + B
+ (NSString*)getAddModelPath {
return [ORTSessionTest getTestDataWithRelativePath:@"/single_add.onnx"];
NSBundle* bundle = [NSBundle bundleForClass:[ORTSessionTest class]];
NSString* path = [bundle pathForResource:@"single_add.basic"
ofType:@"ort"];
return path;
}
+ (NSMutableData*)dataWithScalarFloat:(float)value {
@ -60,24 +60,21 @@ NS_ASSUME_NONNULL_BEGIN
elementType:ORTTensorElementDataTypeFloat
shape:shape
error:&err];
XCTAssertNotNil(ortValue);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(ortValue, err);
return ortValue;
}
+ (ORTSessionOptions*)makeSessionOptions {
NSError* err = nil;
ORTSessionOptions* sessionOptions = [[ORTSessionOptions alloc] initWithError:&err];
XCTAssertNotNil(sessionOptions);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(sessionOptions, err);
return sessionOptions;
}
+ (ORTRunOptions*)makeRunOptions {
NSError* err = nil;
ORTRunOptions* runOptions = [[ORTRunOptions alloc] initWithError:&err];
XCTAssertNotNil(runOptions);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(runOptions, err);
return runOptions;
}
@ -95,15 +92,13 @@ NS_ASSUME_NONNULL_BEGIN
modelPath:[ORTSessionTest getAddModelPath]
sessionOptions:[ORTSessionTest makeSessionOptions]
error:&err];
XCTAssertNotNil(session);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(session, err);
BOOL runResult = [session runWithInputs:@{@"A" : a, @"B" : b}
outputs:@{@"C" : c}
runOptions:[ORTSessionTest makeRunOptions]
error:&err];
XCTAssertTrue(runResult);
XCTAssertNil(err);
ORTAssertBoolResultSuccessful(runResult, err);
const float cExpected = 3.0f;
float cActual;
@ -123,23 +118,20 @@ NS_ASSUME_NONNULL_BEGIN
modelPath:[ORTSessionTest getAddModelPath]
sessionOptions:[ORTSessionTest makeSessionOptions]
error:&err];
XCTAssertNotNil(session);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(session, err);
NSDictionary<NSString*, ORTValue*>* outputs =
[session runWithInputs:@{@"A" : a, @"B" : b}
outputNames:[NSSet setWithArray:@[ @"C" ]]
runOptions:[ORTSessionTest makeRunOptions]
error:&err];
XCTAssertNotNil(outputs);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(outputs, err);
ORTValue* cOutput = outputs[@"C"];
XCTAssertNotNil(cOutput);
NSData* cData = [cOutput tensorDataWithError:&err];
XCTAssertNotNil(cData);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(cData, err);
const float cExpected = 3.0f;
float cActual;
@ -153,34 +145,29 @@ NS_ASSUME_NONNULL_BEGIN
modelPath:[ORTSessionTest getAddModelPath]
sessionOptions:[ORTSessionTest makeSessionOptions]
error:&err];
XCTAssertNotNil(session);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(session, err);
NSArray<NSString*>* inputNames = [session inputNamesWithError:&err];
XCTAssertNotNil(inputNames);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(inputNames, err);
XCTAssertEqualObjects(inputNames, (@[ @"A", @"B" ]));
NSArray<NSString*>* overridableInitializerNames = [session overridableInitializerNamesWithError:&err];
XCTAssertNotNil(overridableInitializerNames);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(overridableInitializerNames, err);
XCTAssertEqualObjects(overridableInitializerNames, (@[]));
NSArray<NSString*>* outputNames = [session outputNamesWithError:&err];
XCTAssertNotNil(outputNames);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(outputNames, err);
XCTAssertEqualObjects(outputNames, (@[ @"C" ]));
}
- (void)testInitFailsWithInvalidPath {
NSString* invalidModelPath = [ORTSessionTest getTestDataWithRelativePath:@"/invalid/path/to/model.onnx"];
NSString* invalidModelPath = @"invalid/path/to/model.ort";
NSError* err = nil;
ORTSession* session = [[ORTSession alloc] initWithEnv:self.ortEnv
modelPath:invalidModelPath
sessionOptions:[ORTSessionTest makeSessionOptions]
error:&err];
XCTAssertNil(session);
XCTAssertNotNil(err);
ORTAssertNullableResultUnsuccessful(session, err);
}
- (void)testRunFailsWithInvalidInput {
@ -195,15 +182,13 @@ NS_ASSUME_NONNULL_BEGIN
modelPath:[ORTSessionTest getAddModelPath]
sessionOptions:[ORTSessionTest makeSessionOptions]
error:&err];
XCTAssertNotNil(session);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(session, err);
BOOL runResult = [session runWithInputs:@{@"D" : d}
outputs:@{@"C" : c}
runOptions:[ORTSessionTest makeRunOptions]
error:&err];
XCTAssertFalse(runResult);
XCTAssertNotNil(err);
ORTAssertBoolResultUnsuccessful(runResult, err);
}
@end

View file

@ -7,6 +7,8 @@
#include <vector>
#import "test/assertion_utils.h"
NS_ASSUME_NONNULL_BEGIN
@interface ORTValueTest : XCTestCase
@ -33,8 +35,7 @@ NS_ASSUME_NONNULL_BEGIN
elementType:elementType
shape:shape
error:&err];
XCTAssertNotNil(ortValue);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(ortValue, err);
auto checkTensorInfo = [&](ORTTensorTypeAndShapeInfo* tensorInfo) {
XCTAssertEqual(tensorInfo.elementType, elementType);
@ -42,20 +43,17 @@ NS_ASSUME_NONNULL_BEGIN
};
ORTValueTypeInfo* typeInfo = [ortValue typeInfoWithError:&err];
XCTAssertNotNil(typeInfo);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(typeInfo, err);
XCTAssertEqual(typeInfo.type, ORTValueTypeTensor);
XCTAssertNotNil(typeInfo.tensorTypeAndShapeInfo);
checkTensorInfo(typeInfo.tensorTypeAndShapeInfo);
ORTTensorTypeAndShapeInfo* tensorInfo = [ortValue tensorTypeAndShapeInfoWithError:&err];
XCTAssertNotNil(tensorInfo);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(tensorInfo, err);
checkTensorInfo(tensorInfo);
NSData* actualData = [ortValue tensorDataWithError:&err];
XCTAssertNotNil(actualData);
XCTAssertNil(err);
ORTAssertNullableResultSuccessful(actualData, err);
XCTAssertEqual(actualData.length, sizeof(int32_t));
int32_t actualValue;
memcpy(&actualValue, actualData.bytes, sizeof(int32_t));
@ -73,8 +71,7 @@ NS_ASSUME_NONNULL_BEGIN
elementType:ORTTensorElementDataTypeInt32
shape:shape
error:&err];
XCTAssertNil(ortValue);
XCTAssertNotNil(err);
ORTAssertNullableResultUnsuccessful(ortValue, err);
}
@end

12
objectivec/test/testdata/gen_models.sh vendored Executable file
View file

@ -0,0 +1,12 @@
#!/bin/bash
set -e
# Get directory this script is in
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd ${DIR}
python3 ./single_add_gen.py
python3 -m onnxruntime.tools.convert_onnx_models_to_ort --optimization_level basic .

Binary file not shown.

View file

@ -13,4 +13,4 @@
C

B
B