From 1c22c0a82105072ed7e976709e5d127f0a718381 Mon Sep 17 00:00:00 2001 From: Anthony Shoumikhin Date: Thu, 27 Mar 2025 09:31:38 -0700 Subject: [PATCH] Move ExecutorchRuntimeBridge into fb dir. Differential Revision: D71952366 Pull Request resolved: https://github.com/pytorch/executorch/pull/9704 (cherry picked from commit a4925e4ca0c86e69ab560445720d38400fa40090) --- .../Data/ExecutorchRuntimeTensorValue.h | 39 ------ .../Data/ExecutorchRuntimeTensorValue.mm | 116 ------------------ .../Exported/Data/ExecutorchRuntimeValue.h | 35 ------ .../Exported/Data/ExecutorchRuntimeValue.mm | 64 ---------- .../Exported/ExecutorchRuntimeEngine.h | 29 ----- .../Exported/ExecutorchRuntimeEngine.mm | 68 ---------- .../__tests__/ExecutorchRuntimeEngineTests.mm | 67 ---------- .../__tests__/ExecutorchRuntimeValueTests.mm | 66 ---------- 8 files changed, 484 deletions(-) delete mode 100644 extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.h delete mode 100644 extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.mm delete mode 100644 extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeValue.h delete mode 100644 extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeValue.mm delete mode 100644 extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/ExecutorchRuntimeEngine.h delete mode 100644 extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/ExecutorchRuntimeEngine.mm delete mode 100644 extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeEngineTests.mm delete mode 100644 extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeValueTests.mm diff --git a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.h b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.h deleted file mode 100644 index 103f0781017..00000000000 --- a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import - -#ifdef __cplusplus - #import - #import -#endif - -NS_ASSUME_NONNULL_BEGIN - -@interface ExecutorchRuntimeTensorValue : NSObject - -@property (nonatomic, readonly) NSArray *shape; - -- (instancetype)init NS_UNAVAILABLE; -+ (instancetype)new NS_UNAVAILABLE; - -- (instancetype)initWithFloatArray:(NSArray *)floatArray shape:(NSArray *)sizes NS_SWIFT_NAME(init(floatArray:shape:)); - -#ifdef __cplusplus -- (nullable instancetype)initWithTensor:(torch::executor::Tensor)tensor error:(NSError * _Nullable * _Nullable)error; -- (instancetype)initWithData:(std::vector)floatData - shape:(std::vector)shape NS_DESIGNATED_INITIALIZER; -- (torch::executor::Tensor)backedValue; -#endif - -#pragma mark - -- (NSArray * _Nullable)floatArrayAndReturnError:(NSError * _Nullable * _Nullable)error; - -@end - -NS_ASSUME_NONNULL_END diff --git a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.mm b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.mm deleted file mode 100644 index cb3dcddb45f..00000000000 --- a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.mm +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import "ExecutorchRuntimeTensorValue.h" - -#import - -#import - -using torch::executor::TensorImpl; -using torch::executor::ScalarType; - -@implementation ExecutorchRuntimeTensorValue -{ - std::unique_ptr _tensor; - // TensorImpl DOES NOT take ownership. - // This float vector is what keeps the data in memory. - std::vector _floatData; - std::vector _shape; -} - -- (instancetype)initWithData:(std::vector)floatData - shape:(std::vector)shape -{ - if (self = [super init]) { - _floatData.assign(floatData.begin(), floatData.end()); - _shape.assign(shape.begin(), shape.end()); - _tensor = std::make_unique(ScalarType::Float, std::size(_shape), _shape.data(), _floatData.data()); - } - return self; -} - -- (instancetype)initWithFloatArray:(NSArray *)floatArray shape:(NSArray *)shape -{ - std::vector floatVector; - std::vector shapeVector; - - floatVector.reserve(floatArray.count); - for (int i = 0; i < floatArray.count; i++) { - floatVector.push_back([floatArray[i] floatValue]); - } - shapeVector.reserve(shape.count); - for (int i = 0; i < shape.count; i++) { - shapeVector.push_back([shape[i] intValue]); - } - - return [self initWithData:floatVector shape:shapeVector]; -} - -- (nullable instancetype)initWithTensor:(torch::executor::Tensor)tensor error:(NSError * _Nullable * _Nullable)error -{ - if (tensor.scalar_type() != ScalarType::Float) { - if (error) { - *error = [NSError - errorWithDomain:@"ExecutorchRuntimeEngine" - code:(NSInteger)executorch::runtime::Error::InvalidArgument - userInfo: @{NSDebugDescriptionErrorKey: [NSString stringWithFormat:@"Invalid type: torch::executor::ScalarType::%hhd, expected torch::executor::ScalarType::Float", tensor.scalar_type()]}]; - } - return nil; - } - - std::vector floatVector; - std::vector shapeVector; - shapeVector.assign(tensor.sizes().begin(), tensor.sizes().end()); - floatVector.assign(tensor.const_data_ptr(), tensor.const_data_ptr() + tensor.numel()); - return [self initWithData:floatVector shape:shapeVector]; -} - -- (NSArray *)shape -{ - const auto sizes = _tensor->sizes(); - std::vector tensorSizes(sizes.begin(), sizes.end()); - - NSMutableArray *sizesArray = [[NSMutableArray alloc] initWithCapacity:tensorSizes.size()]; - for (int &tensorSize : tensorSizes) { - [sizesArray addObject:@(tensorSize)]; - } - - return sizesArray; -} - -- (NSArray * _Nullable)floatArrayAndReturnError:(NSError * _Nullable * _Nullable)error { - if (_tensor->scalar_type() == torch::executor::ScalarType::Float) { - const auto *tensorPtr = _tensor->data(); - const auto sizes = _tensor->sizes(); - std::vector tensorVec(tensorPtr, tensorPtr + _tensor->numel()); - std::vector tensorSizes(sizes.begin(), sizes.end()); - - NSMutableArray *floatArray = [[NSMutableArray alloc] initWithCapacity:tensorVec.size()]; - for (float &i : tensorVec) { - [floatArray addObject:@(i)]; - } - return floatArray; - } - - if (error) { - *error = [NSError - errorWithDomain:@"ExecutorchRuntimeEngine" - code:(NSInteger)executorch::runtime::Error::InvalidArgument - userInfo: @{NSDebugDescriptionErrorKey: [NSString stringWithFormat:@"Invalid type: torch::executor::ScalarType::%hhd, expected torch::executor::ScalarType::Float", _tensor->scalar_type()]}]; - } - - return nil; -} - -- (torch::executor::Tensor)backedValue -{ - return torch::executor::Tensor(_tensor.get()); -} - -@end diff --git a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeValue.h b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeValue.h deleted file mode 100644 index fc1c2c4a35f..00000000000 --- a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeValue.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. - */ - -#ifdef __cplusplus - #import - #import -#endif - -#import "ExecutorchRuntimeTensorValue.h" - -NS_ASSUME_NONNULL_BEGIN - -@interface ExecutorchRuntimeValue : NSObject - -- (instancetype)init NS_UNAVAILABLE; -+ (instancetype)new NS_UNAVAILABLE; - -- (instancetype)initWithTensor:(ExecutorchRuntimeTensorValue *)tensorValue; - -#ifdef __cplusplus -- (instancetype)initWithEValue:(torch::executor::EValue)value NS_DESIGNATED_INITIALIZER; -- (torch::executor::EValue)getBackedValue; -#endif - -#pragma mark - -- (ExecutorchRuntimeTensorValue *_Nullable)asTensorValueAndReturnError:(NSError * _Nullable * _Nullable)error; - -@end - -NS_ASSUME_NONNULL_END diff --git a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeValue.mm b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeValue.mm deleted file mode 100644 index 9cedc6d2afc..00000000000 --- a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeValue.mm +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import "ExecutorchRuntimeValue.h" - -#import -#import - -#import "ExecutorchRuntimeTensorValue.h" - -using torch::executor::EValue; - -@implementation ExecutorchRuntimeValue -{ - EValue _value; - // IMPORTANT - // Tensor value keeps a reference to the original tensor value. However, the value that is wrapped by LiteInterpreterRuntimeTensorValue DOES NOT TAKE OWNERSHIP OF THE RAW DATA! - // This means once the wrapper is deallocated, the tensor value will be deallocated as well. - // This reference here is to keep the tensor value alive until the runtime is deallocated. - ExecutorchRuntimeTensorValue *_tensorValue; -} - -- (instancetype)initWithEValue:(EValue)value -{ - if (self = [super init]) { - _value = value; - } - return self; -} - -- (instancetype)initWithTensor:(ExecutorchRuntimeTensorValue *)tensorValue -{ - if (self = [self initWithEValue:EValue([tensorValue backedValue])]) { - _tensorValue = tensorValue; - } - return self; -} - -- (nullable ExecutorchRuntimeTensorValue *)asTensorValueAndReturnError:(NSError * _Nullable * _Nullable)error -{ - if (_value.isTensor()) { - return [[ExecutorchRuntimeTensorValue alloc] initWithTensor:_value.toTensor() error:error]; - } - - if (error) { - *error = [NSError - errorWithDomain:@"ExecutorchRuntimeEngine" - code:static_cast(executorch::runtime::Error::InvalidArgument) - userInfo: @{NSDebugDescriptionErrorKey: [NSString stringWithFormat:@"Invalid type: Tag::%d, expected Tag::Tensor", _value.tag]}]; - } - return nil; -} - -- (EValue)getBackedValue -{ - return _value; -} - -@end diff --git a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/ExecutorchRuntimeEngine.h b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/ExecutorchRuntimeEngine.h deleted file mode 100644 index a03f6b3c62f..00000000000 --- a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/ExecutorchRuntimeEngine.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import - -#import "ExecutorchRuntimeValue.h" - -NS_ASSUME_NONNULL_BEGIN - -@interface ExecutorchRuntimeEngine : NSObject - -- (nonnull instancetype)init NS_UNAVAILABLE; -+ (nonnull instancetype)new NS_UNAVAILABLE; - -- (nullable instancetype)initWithModelPath:(NSString *)modelPath - modelMethodName:(NSString *)modelMethodName - error:(NSError * _Nullable * _Nullable)error NS_DESIGNATED_INITIALIZER; - -- (nullable NSArray *)infer:(NSArray *)values - error:(NSError * _Nullable * _Nullable)error NS_SWIFT_NAME(infer(input:)); - -@end - -NS_ASSUME_NONNULL_END diff --git a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/ExecutorchRuntimeEngine.mm b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/ExecutorchRuntimeEngine.mm deleted file mode 100644 index 756ca94f114..00000000000 --- a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/ExecutorchRuntimeEngine.mm +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import "ExecutorchRuntimeEngine.h" - -#import -#import - -#import - -@implementation ExecutorchRuntimeEngine -{ - NSString *_modelPath; - NSString *_modelMethodName; - std::unique_ptr _module; -} - -- (instancetype)initWithModelPath:(NSString *)modelPath - modelMethodName:(NSString *)modelMethodName - error:(NSError **)error -{ - if (self = [super init]) { - _modelPath = modelPath; - _modelMethodName = modelMethodName; - _module = std::make_unique(modelPath.UTF8String); - const auto e = _module->load_method(modelMethodName.UTF8String); - if (e != executorch::runtime::Error::Ok) { - if (error) { - *error = [NSError errorWithDomain:@"ExecutorchRuntimeEngine" - code:(NSInteger)e - userInfo:nil]; - } - return nil; - } - } - return self; -} - -- (nullable NSArray *)infer:(NSArray *)values - error:(NSError **)error -{ - std::vector inputEValues; - inputEValues.reserve(values.count); - for (ExecutorchRuntimeValue *inputValue in values) { - inputEValues.push_back([inputValue getBackedValue]); - } - const auto result = _module->execute(_modelMethodName.UTF8String, inputEValues); - if (!result.ok()) { - if (error) { - *error = [NSError errorWithDomain:@"ExecutorchRuntimeEngine" - code:(NSInteger)result.error() - userInfo:nil]; - } - return nil; - } - NSMutableArray *const resultValues = [NSMutableArray new]; - for (const auto &evalue : result.get()) { - [resultValues addObject:[[ExecutorchRuntimeValue alloc] initWithEValue:evalue]]; - } - return resultValues; -} - -@end diff --git a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeEngineTests.mm b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeEngineTests.mm deleted file mode 100644 index 23bc59396b2..00000000000 --- a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeEngineTests.mm +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import - -#import - -NS_ASSUME_NONNULL_BEGIN - -@interface ExecutorchRuntimeEngineTests : XCTestCase -@end - -@implementation ExecutorchRuntimeEngineTests - -- (void)testInvalidModel -{ - NSString *const modelPath = @"invalid_model_path"; - - NSError *runtimeInitError = nil; - ExecutorchRuntimeEngine *const engine = [[ExecutorchRuntimeEngine alloc] initWithModelPath:modelPath modelMethodName:@"forward" error:&runtimeInitError]; - XCTAssertNil(engine); - XCTAssertNotNil(runtimeInitError); - - XCTAssertEqual(runtimeInitError.code, 34); - // 34 is the code for AccessFailed. -} - -- (void)testValidModel -{ - NSBundle *const bundle = [NSBundle bundleForClass:[self class]]; - // This is a simple model that adds two tensors. - NSString *const modelPath = [bundle pathForResource:@"add" ofType:@"pte"]; - NSError *runtimeInitError = nil; - ExecutorchRuntimeEngine *const engine = [[ExecutorchRuntimeEngine alloc] initWithModelPath:modelPath modelMethodName:@"forward" error:&runtimeInitError]; - XCTAssertNotNil(engine); - XCTAssertNil(runtimeInitError); - - ExecutorchRuntimeTensorValue *inputTensor = [[ExecutorchRuntimeTensorValue alloc] initWithFloatArray:@[@2.0] shape:@[@1]]; - ExecutorchRuntimeValue *inputValue = [[ExecutorchRuntimeValue alloc] initWithTensor:inputTensor]; - - NSError *inferenceError = nil; - const auto output = [engine infer:@[inputValue, inputValue] error:&inferenceError]; - XCTAssertNil(inferenceError); - - XCTAssertEqual(output.count, 1); - NSError *tensorValueError = nil; - NSError *floatRepresentationError = nil; - const auto tensorValue = [output.firstObject asTensorValueAndReturnError:&tensorValueError]; - const auto resultFloatArray = [tensorValue floatArrayAndReturnError:&floatRepresentationError]; - const auto resultShape = tensorValue.shape; - - XCTAssertNil(tensorValueError); - XCTAssertNil(floatRepresentationError); - XCTAssertEqual(resultFloatArray.count, 1); - XCTAssertEqual(resultShape.count, 1); - XCTAssertEqual(resultFloatArray.firstObject.floatValue, 4.0); - XCTAssertEqual(resultShape.firstObject.integerValue, 1); -} - -@end - -NS_ASSUME_NONNULL_END diff --git a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeValueTests.mm b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeValueTests.mm deleted file mode 100644 index c3d3599fef2..00000000000 --- a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeValueTests.mm +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import - -#import -#import - -using torch::executor::EValue; -using torch::executor::TensorImpl; -using torch::executor::ScalarType; - -@interface ExecutorchRuntimeValueTests : XCTestCase -@end - -@implementation ExecutorchRuntimeValueTests - -- (void)testTensorValue -{ - NSMutableArray *data = [NSMutableArray new]; - for (int i = 0; i < 10; i++) { - [data addObject:@(i + 0.5f)]; - } - - NSArray *shape = @[@(10)]; - - ExecutorchRuntimeTensorValue *tensorValue = [[ExecutorchRuntimeTensorValue alloc] initWithFloatArray:data shape:shape]; - - const auto floatArray = [tensorValue floatArrayAndReturnError:nil]; - const auto shapeArray = [tensorValue shape]; - - XCTAssertEqualObjects(floatArray, data); - XCTAssertEqualObjects(shapeArray, shape); -} - -- (void)testTensorValueWithFloatArrayWithError -{ - std::vector data = {1, 2, 3}; - std::vector shape = {3}; - TensorImpl tensorImpl(ScalarType::Int, std::size(shape), shape.data(), data.data()); - - XCTAssertNil([[ExecutorchRuntimeTensorValue alloc] initWithTensor:*new torch::executor::Tensor(&tensorImpl) error:nil]); - NSError *error = nil; - XCTAssertNil([[ExecutorchRuntimeTensorValue alloc] initWithTensor:*new torch::executor::Tensor(&tensorImpl) error:&error]); - XCTAssertNotNil(error); - XCTAssertEqual(error.code, static_cast(executorch::runtime::Error::InvalidArgument)); - XCTAssertEqualObjects(error.userInfo[NSDebugDescriptionErrorKey], @"Invalid type: torch::executor::ScalarType::3, expected torch::executor::ScalarType::Float"); -} - -- (void)testTensorValueWithError -{ - ExecutorchRuntimeValue *value = [[ExecutorchRuntimeValue alloc] initWithEValue:EValue((int64_t)1)]; - XCTAssertNil([value asTensorValueAndReturnError:nil]); - NSError *error = nil; - XCTAssertNil([value asTensorValueAndReturnError:&error]); - XCTAssertNotNil(error); - XCTAssertEqual(error.code, static_cast(executorch::runtime::Error::InvalidArgument)); - XCTAssertEqualObjects(error.userInfo[NSDebugDescriptionErrorKey], @"Invalid type: Tag::4, expected Tag::Tensor"); -} - -@end