From 24dec27c44b122f49712fc6a4061620040510eaa Mon Sep 17 00:00:00 2001 From: Berker Soyluoglu Date: Fri, 21 Mar 2025 16:47:59 -0700 Subject: [PATCH] Remove tuple from module implementation Summary: We do not want to have a wrapper to hold these values, the tensor object can provide them all on its own. Reviewed By: shoumikhin Differential Revision: D71658527 --- .../Data/ExecutorchRuntimeTensorValue.h | 7 +++- .../Data/ExecutorchRuntimeTensorValue.mm | 38 +++++++++++-------- .../Exported/Data/ExecutorchRuntimeValue.h | 4 +- .../__tests__/ExecutorchRuntimeEngineTests.mm | 13 ++++--- .../__tests__/ExecutorchRuntimeValueTests.mm | 8 ++-- .../ModelRuntimeTensorValueBridging.swift | 18 --------- 6 files changed, 40 insertions(+), 48 deletions(-) delete mode 100644 extension/apple/RuntimeBridgingCore/RuntimeBridgingCore/ModelRuntimeTensorValueBridging.swift diff --git a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.h b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.h index 84b92ee0b90..103f0781017 100644 --- a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.h +++ b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.h @@ -6,16 +6,19 @@ * LICENSE file in the root directory of this source tree. */ +#import + #ifdef __cplusplus #import #import #endif -#import NS_ASSUME_NONNULL_BEGIN @interface ExecutorchRuntimeTensorValue : NSObject +@property (nonatomic, readonly) NSArray *shape; + - (instancetype)init NS_UNAVAILABLE; + (instancetype)new NS_UNAVAILABLE; @@ -29,7 +32,7 @@ NS_ASSUME_NONNULL_BEGIN #endif #pragma mark - -- (ModelRuntimeTensorValueBridgingTuple * _Nullable)floatRepresentationAndReturnError:(NSError * _Nullable * _Nullable)error SWIFT_WARN_UNUSED_RESULT; +- (NSArray * _Nullable)floatArrayAndReturnError:(NSError * _Nullable * _Nullable)error; @end diff --git a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.mm b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.mm index 4f56276a2a1..cb3dcddb45f 100644 --- a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.mm +++ b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.mm @@ -71,25 +71,31 @@ - (nullable instancetype)initWithTensor:(torch::executor::Tensor)tensor error:(N return [self initWithData:floatVector shape:shapeVector]; } -- (nullable ModelRuntimeTensorValueBridgingTuple *)floatRepresentationAndReturnError:(NSError * _Nullable * _Nullable)error +- (NSArray *)shape { - 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)]; - } + 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)]; - } + NSMutableArray *sizesArray = [[NSMutableArray alloc] initWithCapacity:tensorSizes.size()]; + for (int &tensorSize : tensorSizes) { + [sizesArray addObject:@(tensorSize)]; + } - return [[ModelRuntimeTensorValueBridgingTuple alloc] initWithFloatArray:floatArray shape:sizesArray]; + 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) { diff --git a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeValue.h b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeValue.h index 9e4b3fe4282..fc1c2c4a35f 100644 --- a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeValue.h +++ b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeValue.h @@ -11,8 +11,6 @@ #import #endif -#import - #import "ExecutorchRuntimeTensorValue.h" NS_ASSUME_NONNULL_BEGIN @@ -30,7 +28,7 @@ NS_ASSUME_NONNULL_BEGIN #endif #pragma mark - -- (ExecutorchRuntimeTensorValue *_Nullable)asTensorValueAndReturnError:(NSError * _Nullable * _Nullable)error SWIFT_WARN_UNUSED_RESULT; +- (ExecutorchRuntimeTensorValue *_Nullable)asTensorValueAndReturnError:(NSError * _Nullable * _Nullable)error; @end diff --git a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeEngineTests.mm b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeEngineTests.mm index e243535cdf5..23bc59396b2 100644 --- a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeEngineTests.mm +++ b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeEngineTests.mm @@ -50,15 +50,16 @@ - (void)testValidModel XCTAssertEqual(output.count, 1); NSError *tensorValueError = nil; NSError *floatRepresentationError = nil; - const auto resultTensorValue = [[output.firstObject asTensorValueAndReturnError:&tensorValueError] - floatRepresentationAndReturnError:&floatRepresentationError]; + const auto tensorValue = [output.firstObject asTensorValueAndReturnError:&tensorValueError]; + const auto resultFloatArray = [tensorValue floatArrayAndReturnError:&floatRepresentationError]; + const auto resultShape = tensorValue.shape; XCTAssertNil(tensorValueError); XCTAssertNil(floatRepresentationError); - XCTAssertEqual(resultTensorValue.floatArray.count, 1); - XCTAssertEqual(resultTensorValue.shape.count, 1); - XCTAssertEqual(resultTensorValue.floatArray.firstObject.floatValue, 4.0); - XCTAssertEqual(resultTensorValue.shape.firstObject.integerValue, 1); + XCTAssertEqual(resultFloatArray.count, 1); + XCTAssertEqual(resultShape.count, 1); + XCTAssertEqual(resultFloatArray.firstObject.floatValue, 4.0); + XCTAssertEqual(resultShape.firstObject.integerValue, 1); } @end diff --git a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeValueTests.mm b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeValueTests.mm index 4754232e5d1..c3d3599fef2 100644 --- a/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeValueTests.mm +++ b/extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeValueTests.mm @@ -31,9 +31,11 @@ - (void)testTensorValue ExecutorchRuntimeTensorValue *tensorValue = [[ExecutorchRuntimeTensorValue alloc] initWithFloatArray:data shape:shape]; - const auto tuple = [tensorValue floatRepresentationAndReturnError:nil]; - XCTAssertEqualObjects(tuple.floatArray, data); - XCTAssertEqualObjects(tuple.shape, shape); + const auto floatArray = [tensorValue floatArrayAndReturnError:nil]; + const auto shapeArray = [tensorValue shape]; + + XCTAssertEqualObjects(floatArray, data); + XCTAssertEqualObjects(shapeArray, shape); } - (void)testTensorValueWithFloatArrayWithError diff --git a/extension/apple/RuntimeBridgingCore/RuntimeBridgingCore/ModelRuntimeTensorValueBridging.swift b/extension/apple/RuntimeBridgingCore/RuntimeBridgingCore/ModelRuntimeTensorValueBridging.swift deleted file mode 100644 index d81f55df479..00000000000 --- a/extension/apple/RuntimeBridgingCore/RuntimeBridgingCore/ModelRuntimeTensorValueBridging.swift +++ /dev/null @@ -1,18 +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 Foundation - -public class ModelRuntimeTensorValueBridgingTuple: NSObject { - @objc public let floatArray: [NSNumber] - @objc public let shape: [NSNumber] - @objc public init(floatArray: [NSNumber], shape: [NSNumber]) { - self.floatArray = floatArray - self.shape = shape - } -}