Skip to content

Commit 1f6ff45

Browse files
Add more types to create a Value from. (#9698)
Summary: #8364 Reviewed By: bsoyluoglu Differential Revision: D71936422 Co-authored-by: Anthony Shoumikhin <[email protected]>
1 parent 30d7882 commit 1f6ff45

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed

extension/apple/ExecuTorch/Exported/ExecuTorchValue.h

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ typedef NS_ENUM(uint32_t, ExecuTorchValueTag) {
3030
ExecuTorchValueTagOptionalTensorList,
3131
} NS_SWIFT_NAME(ValueTag);
3232

33+
typedef NSNumber *ExecuTorchScalarValue
34+
NS_SWIFT_BRIDGED_TYPEDEF NS_SWIFT_NAME(ScalarValue);
35+
typedef NSString *ExecuTorchStringValue
36+
NS_SWIFT_BRIDGED_TYPEDEF NS_SWIFT_NAME(StringValue);
37+
typedef BOOL ExecuTorchBooleanValue
38+
NS_SWIFT_BRIDGED_TYPEDEF NS_SWIFT_NAME(BoolValue);
39+
typedef NSInteger ExecuTorchIntegerValue
40+
NS_SWIFT_BRIDGED_TYPEDEF NS_SWIFT_NAME(IntegerValue);
41+
typedef double ExecuTorchDoubleValue
42+
NS_SWIFT_BRIDGED_TYPEDEF NS_SWIFT_NAME(DoubleValue);
43+
3344
/**
3445
* A dynamic value type used by ExecuTorch.
3546
*
@@ -54,6 +65,41 @@ __attribute__((deprecated("This API is experimental.")))
5465
*/
5566
@property(nullable, nonatomic, readonly) ExecuTorchTensor *tensorValue NS_SWIFT_NAME(tensor);
5667

68+
/**
69+
* The string value if the tag is ExecuTorchValueTagString.
70+
*
71+
* @return An NSString instance or nil.
72+
*/
73+
@property(nullable, nonatomic, readonly) ExecuTorchStringValue stringValue NS_SWIFT_NAME(string);
74+
75+
/**
76+
* The scalar value if the tag is boolean, integer or double.
77+
*
78+
* @return A scalar value or nil.
79+
*/
80+
@property(nullable, nonatomic, readonly) ExecuTorchScalarValue scalarValue NS_SWIFT_NAME(scalar);
81+
82+
/**
83+
* The boolean value if the tag is ExecuTorchValueTagBoolean.
84+
*
85+
* @return A BOOL representing the boolean value.
86+
*/
87+
@property(nonatomic, readonly) ExecuTorchBooleanValue boolValue NS_SWIFT_NAME(boolean);
88+
89+
/**
90+
* The integer value if the tag is ExecuTorchValueTagInteger.
91+
*
92+
* @return An NSInteger representing the integer value.
93+
*/
94+
@property(nonatomic, readonly) ExecuTorchIntegerValue intValue NS_SWIFT_NAME(integer);
95+
96+
/**
97+
* The double value if the tag is ExecuTorchValueTagDouble.
98+
*
99+
* @return A double representing the double value.
100+
*/
101+
@property(nonatomic, readonly) ExecuTorchDoubleValue doubleValue NS_SWIFT_NAME(double);
102+
57103
/**
58104
* Returns YES if the value is of type None.
59105
*
@@ -68,6 +114,41 @@ __attribute__((deprecated("This API is experimental.")))
68114
*/
69115
@property(nonatomic, readonly) BOOL isTensor;
70116

117+
/**
118+
* Returns YES if the value is a string.
119+
*
120+
* @return A BOOL indicating whether the value is a string.
121+
*/
122+
@property(nonatomic, readonly) BOOL isString;
123+
124+
/**
125+
* Returns YES if the value is a scalar (boolean, integer or double).
126+
*
127+
* @return A BOOL indicating whether the value is a scalar.
128+
*/
129+
@property(nonatomic, readonly) BOOL isScalar;
130+
131+
/**
132+
* Returns YES if the value is a boolean.
133+
*
134+
* @return A BOOL indicating whether the value is a boolean.
135+
*/
136+
@property(nonatomic, readonly) BOOL isBoolean;
137+
138+
/**
139+
* Returns YES if the value is an integer.
140+
*
141+
* @return A BOOL indicating whether the value is an integer.
142+
*/
143+
@property(nonatomic, readonly) BOOL isInteger;
144+
145+
/**
146+
* Returns YES if the value is a double.
147+
*
148+
* @return A BOOL indicating whether the value is a double.
149+
*/
150+
@property(nonatomic, readonly) BOOL isDouble;
151+
71152
/**
72153
* Creates an instance encapsulating a Tensor.
73154
*
@@ -76,6 +157,42 @@ __attribute__((deprecated("This API is experimental.")))
76157
*/
77158
+ (instancetype)valueWithTensor:(ExecuTorchTensor *)value NS_SWIFT_NAME(init(_:));
78159

160+
/**
161+
* Creates an instance encapsulating a string.
162+
*
163+
* @param value A string.
164+
* @return A new ExecuTorchValue instance with a tag of ExecuTorchValueTagString.
165+
*/
166+
+ (instancetype)valueWithString:(ExecuTorchStringValue)value
167+
NS_SWIFT_NAME(init(_:));
168+
169+
/**
170+
* Creates an instance encapsulating a boolean.
171+
*
172+
* @param value A boolean.
173+
* @return A new ExecuTorchValue instance with a tag of ExecuTorchValueTagBoolean.
174+
*/
175+
+ (instancetype)valueWithBoolean:(ExecuTorchBooleanValue)value
176+
NS_SWIFT_NAME(init(_:));
177+
178+
/**
179+
* Creates an instance encapsulating an integer.
180+
*
181+
* @param value An integer.
182+
* @return A new ExecuTorchValue instance with a tag of ExecuTorchValueTagInteger.
183+
*/
184+
+ (instancetype)valueWithInteger:(ExecuTorchIntegerValue)value
185+
NS_SWIFT_NAME(init(_:));
186+
187+
/**
188+
* Creates an instance encapsulating a double value.
189+
*
190+
* @param value A double value.
191+
* @return A new ExecuTorchValue instance with a tag of ExecuTorchValueTagDouble.
192+
*/
193+
+ (instancetype)valueWithDouble:(ExecuTorchDoubleValue)value
194+
NS_SWIFT_NAME(init(_:));
195+
79196
@end
80197

81198
NS_ASSUME_NONNULL_END

extension/apple/ExecuTorch/Exported/ExecuTorchValue.mm

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ + (instancetype)valueWithTensor:(ExecuTorchTensor *)value {
2727
return [[ExecuTorchValue alloc] initWithTag:ExecuTorchValueTagTensor value:value];
2828
}
2929

30+
+ (instancetype)valueWithString:(ExecuTorchStringValue)value {
31+
ET_CHECK(value);
32+
return [[ExecuTorchValue alloc] initWithTag:ExecuTorchValueTagString value:value];
33+
}
34+
35+
+ (instancetype)valueWithBoolean:(ExecuTorchBooleanValue)value {
36+
return [[ExecuTorchValue alloc] initWithTag:ExecuTorchValueTagBoolean value:@(value)];
37+
}
38+
39+
+ (instancetype)valueWithInteger:(ExecuTorchIntegerValue)value {
40+
return [[ExecuTorchValue alloc] initWithTag:ExecuTorchValueTagInteger value:@(value)];
41+
}
42+
43+
+ (instancetype)valueWithDouble:(ExecuTorchDoubleValue)value {
44+
return [[ExecuTorchValue alloc] initWithTag:ExecuTorchValueTagDouble value:@(value)];
45+
}
46+
3047
- (instancetype)init {
3148
return [self initWithTag:ExecuTorchValueTagNone value:nil];
3249
}
@@ -48,6 +65,29 @@ - (nullable ExecuTorchTensor *)tensorValue {
4865
return self.isTensor ? _value : nil;
4966
}
5067

68+
- (nullable ExecuTorchScalarValue)scalarValue {
69+
return self.isScalar ? _value : nil;
70+
}
71+
72+
- (nullable ExecuTorchStringValue)stringValue {
73+
return self.isString ? _value : nil;
74+
}
75+
76+
- (ExecuTorchBooleanValue)boolValue {
77+
ET_CHECK(self.isBoolean);
78+
return [(ExecuTorchScalarValue)_value boolValue];
79+
}
80+
81+
- (ExecuTorchIntegerValue)intValue {
82+
ET_CHECK(self.isInteger);
83+
return [(ExecuTorchScalarValue)_value integerValue];
84+
}
85+
86+
- (ExecuTorchDoubleValue)doubleValue {
87+
ET_CHECK(self.isDouble);
88+
return [(ExecuTorchScalarValue)_value doubleValue];
89+
}
90+
5191
- (BOOL)isNone {
5292
return _tag == ExecuTorchValueTagNone;
5393
}
@@ -56,4 +96,26 @@ - (BOOL)isTensor {
5696
return _tag == ExecuTorchValueTagTensor;
5797
}
5898

99+
- (BOOL)isScalar {
100+
return _tag == ExecuTorchValueTagBoolean ||
101+
_tag == ExecuTorchValueTagInteger ||
102+
_tag == ExecuTorchValueTagDouble;
103+
}
104+
105+
- (BOOL)isString {
106+
return _tag == ExecuTorchValueTagString;
107+
}
108+
109+
- (BOOL)isBoolean {
110+
return _tag == ExecuTorchValueTagBoolean;
111+
}
112+
113+
- (BOOL)isInteger {
114+
return _tag == ExecuTorchValueTagInteger;
115+
}
116+
117+
- (BOOL)isDouble {
118+
return _tag == ExecuTorchValueTagDouble;
119+
}
120+
59121
@end

extension/apple/ExecuTorch/__tests__/ValueTest.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,28 @@ class ValueTest: XCTestCase {
2222
XCTAssertTrue(value.isTensor)
2323
XCTAssertEqual(value.tensor, tensor)
2424
}
25+
26+
func testString() {
27+
let value = Value("hello")
28+
XCTAssertTrue(value.isString)
29+
XCTAssertEqual(value.string, "hello")
30+
}
31+
32+
func testBoolean() {
33+
let value = Value(true)
34+
XCTAssertTrue(value.isBoolean)
35+
XCTAssertEqual(value.boolean, true)
36+
}
37+
38+
func testInteger() {
39+
let value = Value(42)
40+
XCTAssertTrue(value.isInteger)
41+
XCTAssertEqual(value.integer, 42)
42+
}
43+
44+
func testDouble() {
45+
let value = Value(3.14)
46+
XCTAssertTrue(value.isDouble)
47+
XCTAssertEqual(value.double, 3.14)
48+
}
2549
}

0 commit comments

Comments
 (0)