Skip to content

Remove tuple from module implementation #9519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
* LICENSE file in the root directory of this source tree.
*/

#import <Foundation/Foundation.h>

#ifdef __cplusplus
#import <executorch/extension/module/module.h>
#import <executorch/runtime/core/evalue.h>
#endif
#import <RuntimeBridgingCore/RuntimeBridgingCore-Swift.h>

NS_ASSUME_NONNULL_BEGIN

@interface ExecutorchRuntimeTensorValue : NSObject

@property (nonatomic, readonly) NSArray<NSNumber *> *shape;

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;

Expand All @@ -29,7 +32,7 @@ NS_ASSUME_NONNULL_BEGIN
#endif

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

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSNumber *> *)shape
{
if (_tensor->scalar_type() == torch::executor::ScalarType::Float) {
const auto *tensorPtr = _tensor->data<float>();
const auto sizes = _tensor->sizes();
std::vector<float> tensorVec(tensorPtr, tensorPtr + _tensor->numel());
std::vector<int32_t> tensorSizes(sizes.begin(), sizes.end());

NSMutableArray<NSNumber *> *floatArray = [[NSMutableArray alloc] initWithCapacity:tensorVec.size()];
for (float &i : tensorVec) {
[floatArray addObject:@(i)];
}
const auto sizes = _tensor->sizes();
std::vector<int32_t> tensorSizes(sizes.begin(), sizes.end());

NSMutableArray<NSNumber *> *sizesArray = [[NSMutableArray alloc] initWithCapacity:tensorSizes.size()];
for (int &tensorSize : tensorSizes) {
[sizesArray addObject:@(tensorSize)];
}
NSMutableArray<NSNumber *> *sizesArray = [[NSMutableArray alloc] initWithCapacity:tensorSizes.size()];
for (int &tensorSize : tensorSizes) {
[sizesArray addObject:@(tensorSize)];
}

return [[ModelRuntimeTensorValueBridgingTuple alloc] initWithFloatArray:floatArray shape:sizesArray];
return sizesArray;
}

- (NSArray<NSNumber *> * _Nullable)floatArrayAndReturnError:(NSError * _Nullable * _Nullable)error {
if (_tensor->scalar_type() == torch::executor::ScalarType::Float) {
const auto *tensorPtr = _tensor->data<float>();
const auto sizes = _tensor->sizes();
std::vector<float> tensorVec(tensorPtr, tensorPtr + _tensor->numel());
std::vector<int32_t> tensorSizes(sizes.begin(), sizes.end());

NSMutableArray<NSNumber *> *floatArray = [[NSMutableArray alloc] initWithCapacity:tensorVec.size()];
for (float &i : tensorVec) {
[floatArray addObject:@(i)];
}
return floatArray;
}

if (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#import <executorch/runtime/core/evalue.h>
#endif

#import <RuntimeBridgingCore/RuntimeBridgingCore-Swift.h>

#import "ExecutorchRuntimeTensorValue.h"

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

Loading