Enable SPM support for Ext and configure pipelines for extensions target testing (#3)

* initial ext files

* add a workable version of spm for extensions

* update

* update pipeline

* update pipelines

* fix dev/release pipelines for extensions pod

* fix dev pipelines for extensions pod

* fix release pipelines for extensions pod

* fix dev  pipelines for extensions pod

* adding empty include folder for configuring extensions target path

* test

* test

* test

* revert pipeline changes

* revert gitignore changes

* add ext pod binary target for release pipeline

* add pipeline for extensions

* update

* update package.swift

* update latest from branch

* update

* fix

* fix

* update

* try dummy empty file

* test

* update package.swift to use fatalerror

* syntax

* try gitignore revert

* update gitignore

* add -list

* onnxruntime-Package

* update using onnxruntime-Package

* update dev pipeline

* fix dev pipelines

* syntax

* pull extensions/

* update pipelines again

* syntax

* variables

* fix

* fix -r

* update Package.swift

* update Package.swift

* minor update

* address pr comments

* minor updates

* fix

* refine messages

* syntax

* syntax again

* address pr comments partial

* address pr comments

* add .h header file and notes

* minor updates

* syncing objc source files and add code to register custom ops using function pointer

* format

* move to the header for function doc

---------

Co-authored-by: rachguo <rachguo@rachguos-Mini.attlocal.net>
Co-authored-by: rachguo <rachguo@rachguos-Mac-mini.local>
This commit is contained in:
Rachel Guo
2023-08-11 10:07:43 -07:00
committed by GitHub
parent 7acc38c99c
commit c76787fd3e
33 changed files with 1424 additions and 62 deletions
+85 -8
View File
@@ -71,6 +71,12 @@ bool SafeMultiply(size_t a, size_t b, size_t& out) {
shape:(NSArray<NSNumber*>*)shape
error:(NSError**)error {
try {
if (elementType == ORTTensorElementDataTypeString) {
ORT_CXX_API_THROW(
"ORTTensorElementDataTypeString element type provided. "
"Please call initWithTensorStringData:shape:error: instead to create an ORTValue with string data.",
ORT_INVALID_ARGUMENT);
}
const auto memoryInfo = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU);
const auto ONNXElementType = PublicToCAPITensorElementType(elementType);
const auto shapeVector = [shape]() {
@@ -85,9 +91,49 @@ bool SafeMultiply(size_t a, size_t b, size_t& out) {
memoryInfo, tensorData.mutableBytes, tensorData.length,
shapeVector.data(), shapeVector.size(), ONNXElementType);
return [self initWithCAPIOrtValue:ortValue.release()
externalTensorData:tensorData
error:error];
return [self initWithCXXAPIOrtValue:std::move(ortValue)
externalTensorData:tensorData
error:error];
}
ORT_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error)
}
- (nullable instancetype)initWithTensorStringData:(NSArray<NSString*>*)tensorStringData
shape:(NSArray<NSNumber*>*)shape
error:(NSError**)error {
try {
Ort::AllocatorWithDefaultOptions allocator;
size_t tensorSize = 1U;
const auto shapeVector = [&tensorSize, shape]() {
std::vector<int64_t> result{};
result.reserve(shape.count);
for (NSNumber* dim in shape) {
const auto dimValue = dim.longLongValue;
if (dimValue < 0 || !SafeMultiply(static_cast<size_t>(dimValue), tensorSize, tensorSize)) {
ORT_CXX_API_THROW("Failed to compute the tensor size.", ORT_RUNTIME_EXCEPTION);
}
result.push_back(dimValue);
}
return result;
}();
if (tensorSize != [tensorStringData count]) {
ORT_CXX_API_THROW(
"Computed tensor size does not equal the length of the provided tensor string data.",
ORT_INVALID_ARGUMENT);
}
Ort::Value ortValue = Ort::Value::CreateTensor(
allocator, shapeVector.data(), shapeVector.size(), ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING);
size_t index = 0;
for (NSString* stringData in tensorStringData) {
ortValue.FillStringTensorElement([stringData UTF8String], index++);
}
return [self initWithCXXAPIOrtValue:std::move(ortValue)
externalTensorData:nil
error:error];
}
ORT_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error)
}
@@ -110,6 +156,12 @@ bool SafeMultiply(size_t a, size_t b, size_t& out) {
- (nullable NSMutableData*)tensorDataWithError:(NSError**)error {
try {
const auto tensorTypeAndShapeInfo = _typeInfo->GetTensorTypeAndShapeInfo();
if (tensorTypeAndShapeInfo.GetElementType() == ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING) {
ORT_CXX_API_THROW(
"This ORTValue holds string data. Please call tensorStringDataWithError: "
"instead to retrieve the string data from this ORTValue.",
ORT_RUNTIME_EXCEPTION);
}
const size_t elementCount = tensorTypeAndShapeInfo.GetElementCount();
const size_t elementSize = SizeOfCAPITensorElementType(tensorTypeAndShapeInfo.GetElementType());
size_t rawDataLength;
@@ -127,19 +179,44 @@ bool SafeMultiply(size_t a, size_t b, size_t& out) {
ORT_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error)
}
- (nullable NSArray<NSString*>*)tensorStringDataWithError:(NSError**)error {
try {
const auto tensorTypeAndShapeInfo = _typeInfo->GetTensorTypeAndShapeInfo();
const size_t elementCount = tensorTypeAndShapeInfo.GetElementCount();
const size_t tensorStringDataLength = _value->GetStringTensorDataLength();
std::vector<char> tensorStringData(tensorStringDataLength, '\0');
std::vector<size_t> offsets(elementCount);
_value->GetStringTensorContent(tensorStringData.data(), tensorStringDataLength,
offsets.data(), offsets.size());
NSMutableArray<NSString*>* result = [NSMutableArray arrayWithCapacity:elementCount];
for (size_t idx = 0; idx < elementCount; ++idx) {
const size_t strLength = (idx == elementCount - 1) ? tensorStringDataLength - offsets[idx]
: offsets[idx + 1] - offsets[idx];
[result addObject:[[NSString alloc] initWithBytes:tensorStringData.data() + offsets[idx]
length:strLength
encoding:NSUTF8StringEncoding]];
}
return result;
}
ORT_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error)
}
#pragma mark - Internal
- (nullable instancetype)initWithCAPIOrtValue:(OrtValue*)CAPIOrtValue
externalTensorData:(nullable NSMutableData*)externalTensorData
error:(NSError**)error {
- (nullable instancetype)initWithCXXAPIOrtValue:(Ort::Value&&)existingCXXAPIOrtValue
externalTensorData:(nullable NSMutableData*)externalTensorData
error:(NSError**)error {
if ((self = [super init]) == nil) {
return nil;
}
try {
_value = Ort::Value{CAPIOrtValue};
_typeInfo = _value->GetTypeInfo();
_typeInfo = existingCXXAPIOrtValue.GetTypeInfo();
_externalTensorData = externalTensorData;
// transfer C++ Ort::Value ownership to this instance
_value = std::move(existingCXXAPIOrtValue);
return self;
}
ORT_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error);