Skip to content

Commit 24dec27

Browse files
bsoyluoglufacebook-github-bot
authored andcommitted
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
1 parent cedf52a commit 24dec27

File tree

6 files changed

+40
-48
lines changed

6 files changed

+40
-48
lines changed

extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9+
#import <Foundation/Foundation.h>
10+
911
#ifdef __cplusplus
1012
#import <executorch/extension/module/module.h>
1113
#import <executorch/runtime/core/evalue.h>
1214
#endif
13-
#import <RuntimeBridgingCore/RuntimeBridgingCore-Swift.h>
1415

1516
NS_ASSUME_NONNULL_BEGIN
1617

1718
@interface ExecutorchRuntimeTensorValue : NSObject
1819

20+
@property (nonatomic, readonly) NSArray<NSNumber *> *shape;
21+
1922
- (instancetype)init NS_UNAVAILABLE;
2023
+ (instancetype)new NS_UNAVAILABLE;
2124

@@ -29,7 +32,7 @@ NS_ASSUME_NONNULL_BEGIN
2932
#endif
3033

3134
#pragma mark -
32-
- (ModelRuntimeTensorValueBridgingTuple * _Nullable)floatRepresentationAndReturnError:(NSError * _Nullable * _Nullable)error SWIFT_WARN_UNUSED_RESULT;
35+
- (NSArray<NSNumber *> * _Nullable)floatArrayAndReturnError:(NSError * _Nullable * _Nullable)error;
3336

3437
@end
3538

extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeTensorValue.mm

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,31 @@ - (nullable instancetype)initWithTensor:(torch::executor::Tensor)tensor error:(N
7171
return [self initWithData:floatVector shape:shapeVector];
7272
}
7373

74-
- (nullable ModelRuntimeTensorValueBridgingTuple *)floatRepresentationAndReturnError:(NSError * _Nullable * _Nullable)error
74+
- (NSArray<NSNumber *> *)shape
7575
{
76-
if (_tensor->scalar_type() == torch::executor::ScalarType::Float) {
77-
const auto *tensorPtr = _tensor->data<float>();
78-
const auto sizes = _tensor->sizes();
79-
std::vector<float> tensorVec(tensorPtr, tensorPtr + _tensor->numel());
80-
std::vector<int32_t> tensorSizes(sizes.begin(), sizes.end());
81-
82-
NSMutableArray<NSNumber *> *floatArray = [[NSMutableArray alloc] initWithCapacity:tensorVec.size()];
83-
for (float &i : tensorVec) {
84-
[floatArray addObject:@(i)];
85-
}
76+
const auto sizes = _tensor->sizes();
77+
std::vector<int32_t> tensorSizes(sizes.begin(), sizes.end());
8678

87-
NSMutableArray<NSNumber *> *sizesArray = [[NSMutableArray alloc] initWithCapacity:tensorSizes.size()];
88-
for (int &tensorSize : tensorSizes) {
89-
[sizesArray addObject:@(tensorSize)];
90-
}
79+
NSMutableArray<NSNumber *> *sizesArray = [[NSMutableArray alloc] initWithCapacity:tensorSizes.size()];
80+
for (int &tensorSize : tensorSizes) {
81+
[sizesArray addObject:@(tensorSize)];
82+
}
9183

92-
return [[ModelRuntimeTensorValueBridgingTuple alloc] initWithFloatArray:floatArray shape:sizesArray];
84+
return sizesArray;
85+
}
86+
87+
- (NSArray<NSNumber *> * _Nullable)floatArrayAndReturnError:(NSError * _Nullable * _Nullable)error {
88+
if (_tensor->scalar_type() == torch::executor::ScalarType::Float) {
89+
const auto *tensorPtr = _tensor->data<float>();
90+
const auto sizes = _tensor->sizes();
91+
std::vector<float> tensorVec(tensorPtr, tensorPtr + _tensor->numel());
92+
std::vector<int32_t> tensorSizes(sizes.begin(), sizes.end());
93+
94+
NSMutableArray<NSNumber *> *floatArray = [[NSMutableArray alloc] initWithCapacity:tensorVec.size()];
95+
for (float &i : tensorVec) {
96+
[floatArray addObject:@(i)];
97+
}
98+
return floatArray;
9399
}
94100

95101
if (error) {

extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/Data/ExecutorchRuntimeValue.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#import <executorch/runtime/core/evalue.h>
1212
#endif
1313

14-
#import <RuntimeBridgingCore/RuntimeBridgingCore-Swift.h>
15-
1614
#import "ExecutorchRuntimeTensorValue.h"
1715

1816
NS_ASSUME_NONNULL_BEGIN
@@ -30,7 +28,7 @@ NS_ASSUME_NONNULL_BEGIN
3028
#endif
3129

3230
#pragma mark -
33-
- (ExecutorchRuntimeTensorValue *_Nullable)asTensorValueAndReturnError:(NSError * _Nullable * _Nullable)error SWIFT_WARN_UNUSED_RESULT;
31+
- (ExecutorchRuntimeTensorValue *_Nullable)asTensorValueAndReturnError:(NSError * _Nullable * _Nullable)error;
3432

3533
@end
3634

extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeEngineTests.mm

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@ - (void)testValidModel
5050
XCTAssertEqual(output.count, 1);
5151
NSError *tensorValueError = nil;
5252
NSError *floatRepresentationError = nil;
53-
const auto resultTensorValue = [[output.firstObject asTensorValueAndReturnError:&tensorValueError]
54-
floatRepresentationAndReturnError:&floatRepresentationError];
53+
const auto tensorValue = [output.firstObject asTensorValueAndReturnError:&tensorValueError];
54+
const auto resultFloatArray = [tensorValue floatArrayAndReturnError:&floatRepresentationError];
55+
const auto resultShape = tensorValue.shape;
5556

5657
XCTAssertNil(tensorValueError);
5758
XCTAssertNil(floatRepresentationError);
58-
XCTAssertEqual(resultTensorValue.floatArray.count, 1);
59-
XCTAssertEqual(resultTensorValue.shape.count, 1);
60-
XCTAssertEqual(resultTensorValue.floatArray.firstObject.floatValue, 4.0);
61-
XCTAssertEqual(resultTensorValue.shape.firstObject.integerValue, 1);
59+
XCTAssertEqual(resultFloatArray.count, 1);
60+
XCTAssertEqual(resultShape.count, 1);
61+
XCTAssertEqual(resultFloatArray.firstObject.floatValue, 4.0);
62+
XCTAssertEqual(resultShape.firstObject.integerValue, 1);
6263
}
6364

6465
@end

extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeValueTests.mm

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ - (void)testTensorValue
3131

3232
ExecutorchRuntimeTensorValue *tensorValue = [[ExecutorchRuntimeTensorValue alloc] initWithFloatArray:data shape:shape];
3333

34-
const auto tuple = [tensorValue floatRepresentationAndReturnError:nil];
35-
XCTAssertEqualObjects(tuple.floatArray, data);
36-
XCTAssertEqualObjects(tuple.shape, shape);
34+
const auto floatArray = [tensorValue floatArrayAndReturnError:nil];
35+
const auto shapeArray = [tensorValue shape];
36+
37+
XCTAssertEqualObjects(floatArray, data);
38+
XCTAssertEqualObjects(shapeArray, shape);
3739
}
3840

3941
- (void)testTensorValueWithFloatArrayWithError

extension/apple/RuntimeBridgingCore/RuntimeBridgingCore/ModelRuntimeTensorValueBridging.swift

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)