Skip to content

Commit 2adb1bc

Browse files
authored
[Android] Remove runtime internal evalue list types (#7012)
List types are used in runtime only. Exported model inputs and outputs won't use them. Therefore, we remove them from Java public API.
1 parent 4e90293 commit 2adb1bc

File tree

2 files changed

+3
-224
lines changed

2 files changed

+3
-224
lines changed

extension/android/src/main/java/org/pytorch/executorch/EValue.java

Lines changed: 3 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.nio.ByteBuffer;
1313
import java.util.Arrays;
1414
import java.util.Locale;
15-
import java.util.Optional;
1615
import org.pytorch.executorch.annotations.Experimental;
1716

1817
/**
@@ -44,26 +43,8 @@ public class EValue {
4443
private static final int TYPE_CODE_INT = 4;
4544
private static final int TYPE_CODE_BOOL = 5;
4645

47-
private static final int TYPE_CODE_LIST_BOOL = 6;
48-
private static final int TYPE_CODE_LIST_DOUBLE = 7;
49-
private static final int TYPE_CODE_LIST_INT = 8;
50-
private static final int TYPE_CODE_LIST_TENSOR = 9;
51-
private static final int TYPE_CODE_LIST_SCALAR = 10;
52-
private static final int TYPE_CODE_LIST_OPTIONAL_TENSOR = 11;
53-
5446
private String[] TYPE_NAMES = {
55-
"None",
56-
"Tensor",
57-
"String",
58-
"Double",
59-
"Int",
60-
"Bool",
61-
"ListBool",
62-
"ListDouble",
63-
"ListInt",
64-
"ListTensor",
65-
"ListScalar",
66-
"ListOptionalTensor",
47+
"None", "Tensor", "String", "Double", "Int", "Bool",
6748
};
6849

6950
@DoNotStrip private final int mTypeCode;
@@ -104,31 +85,6 @@ public boolean isString() {
10485
return TYPE_CODE_STRING == this.mTypeCode;
10586
}
10687

107-
@DoNotStrip
108-
public boolean isBoolList() {
109-
return TYPE_CODE_LIST_BOOL == this.mTypeCode;
110-
}
111-
112-
@DoNotStrip
113-
public boolean isIntList() {
114-
return TYPE_CODE_LIST_INT == this.mTypeCode;
115-
}
116-
117-
@DoNotStrip
118-
public boolean isDoubleList() {
119-
return TYPE_CODE_LIST_DOUBLE == this.mTypeCode;
120-
}
121-
122-
@DoNotStrip
123-
public boolean isTensorList() {
124-
return TYPE_CODE_LIST_TENSOR == this.mTypeCode;
125-
}
126-
127-
@DoNotStrip
128-
public boolean isOptionalTensorList() {
129-
return TYPE_CODE_LIST_OPTIONAL_TENSOR == this.mTypeCode;
130-
}
131-
13288
/** Creates a new {@code EValue} of type {@code Optional} that contains no value. */
13389
@DoNotStrip
13490
public static EValue optionalNone() {
@@ -175,46 +131,6 @@ public static EValue from(String value) {
175131
return iv;
176132
}
177133

178-
/** Creates a new {@code EValue} of type {@code List[bool]}. */
179-
@DoNotStrip
180-
public static EValue listFrom(boolean... list) {
181-
final EValue iv = new EValue(TYPE_CODE_LIST_BOOL);
182-
iv.mData = list;
183-
return iv;
184-
}
185-
186-
/** Creates a new {@code EValue} of type {@code List[int]}. */
187-
@DoNotStrip
188-
public static EValue listFrom(long... list) {
189-
final EValue iv = new EValue(TYPE_CODE_LIST_INT);
190-
iv.mData = list;
191-
return iv;
192-
}
193-
194-
/** Creates a new {@code EValue} of type {@code List[double]}. */
195-
@DoNotStrip
196-
public static EValue listFrom(double... list) {
197-
final EValue iv = new EValue(TYPE_CODE_LIST_DOUBLE);
198-
iv.mData = list;
199-
return iv;
200-
}
201-
202-
/** Creates a new {@code EValue} of type {@code List[Tensor]}. */
203-
@DoNotStrip
204-
public static EValue listFrom(Tensor... list) {
205-
final EValue iv = new EValue(TYPE_CODE_LIST_TENSOR);
206-
iv.mData = list;
207-
return iv;
208-
}
209-
210-
/** Creates a new {@code EValue} of type {@code List[Optional[Tensor]]}. */
211-
@DoNotStrip
212-
public static EValue listFrom(Optional<Tensor>... list) {
213-
final EValue iv = new EValue(TYPE_CODE_LIST_OPTIONAL_TENSOR);
214-
iv.mData = list;
215-
return iv;
216-
}
217-
218134
@DoNotStrip
219135
public Tensor toTensor() {
220136
preconditionType(TYPE_CODE_TENSOR, mTypeCode);
@@ -245,36 +161,6 @@ public String toStr() {
245161
return (String) mData;
246162
}
247163

248-
@DoNotStrip
249-
public boolean[] toBoolList() {
250-
preconditionType(TYPE_CODE_LIST_BOOL, mTypeCode);
251-
return (boolean[]) mData;
252-
}
253-
254-
@DoNotStrip
255-
public long[] toIntList() {
256-
preconditionType(TYPE_CODE_LIST_INT, mTypeCode);
257-
return (long[]) mData;
258-
}
259-
260-
@DoNotStrip
261-
public double[] toDoubleList() {
262-
preconditionType(TYPE_CODE_LIST_DOUBLE, mTypeCode);
263-
return (double[]) mData;
264-
}
265-
266-
@DoNotStrip
267-
public Tensor[] toTensorList() {
268-
preconditionType(TYPE_CODE_LIST_TENSOR, mTypeCode);
269-
return (Tensor[]) mData;
270-
}
271-
272-
@DoNotStrip
273-
public Optional<Tensor>[] toOptionalTensorList() {
274-
preconditionType(TYPE_CODE_LIST_OPTIONAL_TENSOR, mTypeCode);
275-
return (Optional<Tensor>[]) mData;
276-
}
277-
278164
private void preconditionType(int typeCodeExpected, int typeCode) {
279165
if (typeCode != typeCodeExpected) {
280166
throw new IllegalStateException(
@@ -294,8 +180,7 @@ private String getTypeName(int typeCode) {
294180
* Serializes an {@code EValue} into a byte array.
295181
*
296182
* @return The serialized byte array.
297-
* @apiNote This method is experimental and subject to change without notice. This does NOT
298-
* supoprt list type.
183+
* @apiNote This method is experimental and subject to change without notice.
299184
*/
300185
public byte[] toByteArray() {
301186
if (isNone()) {
@@ -331,8 +216,7 @@ public byte[] toByteArray() {
331216
*
332217
* @param bytes The byte array to deserialize from.
333218
* @return The deserialized {@code EValue}.
334-
* @apiNote This method is experimental and subject to change without notice. This does NOT list
335-
* type.
219+
* @apiNote This method is experimental and subject to change without notice.
336220
*/
337221
public static EValue fromByteArray(byte[] bytes) {
338222
ByteBuffer buffer = ByteBuffer.wrap(bytes);

extension/android_test/src/test/java/org/pytorch/executorch/EValueTest.java

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import static org.junit.Assert.fail;
1515

1616
import java.util.Arrays;
17-
import java.util.Optional;
1817
import org.junit.Test;
1918
import org.junit.runner.RunWith;
2019
import org.junit.runners.JUnit4;
@@ -66,70 +65,6 @@ public void testStringValue() {
6665
assertEquals(evalue.toStr(), "a");
6766
}
6867

69-
@Test
70-
public void testBoolListValue() {
71-
boolean[] value = {true, false, true};
72-
EValue evalue = EValue.listFrom(value);
73-
assertTrue(evalue.isBoolList());
74-
assertTrue(Arrays.equals(value, evalue.toBoolList()));
75-
}
76-
77-
@Test
78-
public void testIntListValue() {
79-
long[] value = {Long.MIN_VALUE, 0, Long.MAX_VALUE};
80-
EValue evalue = EValue.listFrom(value);
81-
assertTrue(evalue.isIntList());
82-
assertTrue(Arrays.equals(value, evalue.toIntList()));
83-
}
84-
85-
@Test
86-
public void testDoubleListValue() {
87-
double[] value = {Double.MIN_VALUE, 0.1d, 0.01d, 0.001d, Double.MAX_VALUE};
88-
EValue evalue = EValue.listFrom(value);
89-
assertTrue(evalue.isDoubleList());
90-
assertTrue(Arrays.equals(value, evalue.toDoubleList()));
91-
}
92-
93-
@Test
94-
public void testTensorListValue() {
95-
long[][] data = {{1, 2, 3}, {1, 2, 3, 4, 5, 6}};
96-
long[][] shape = {{1, 3}, {2, 3}};
97-
Tensor[] tensors = {Tensor.fromBlob(data[0], shape[0]), Tensor.fromBlob(data[1], shape[1])};
98-
99-
EValue evalue = EValue.listFrom(tensors);
100-
assertTrue(evalue.isTensorList());
101-
102-
assertTrue(Arrays.equals(evalue.toTensorList()[0].shape, shape[0]));
103-
assertTrue(Arrays.equals(evalue.toTensorList()[0].getDataAsLongArray(), data[0]));
104-
105-
assertTrue(Arrays.equals(evalue.toTensorList()[1].shape, shape[1]));
106-
assertTrue(Arrays.equals(evalue.toTensorList()[1].getDataAsLongArray(), data[1]));
107-
}
108-
109-
@Test
110-
@SuppressWarnings("unchecked")
111-
public void testOptionalTensorListValue() {
112-
long[][] data = {{1, 2, 3}, {1, 2, 3, 4, 5, 6}};
113-
long[][] shape = {{1, 3}, {2, 3}};
114-
115-
EValue evalue =
116-
EValue.listFrom(
117-
Optional.<Tensor>empty(),
118-
Optional.of(Tensor.fromBlob(data[0], shape[0])),
119-
Optional.of(Tensor.fromBlob(data[1], shape[1])));
120-
assertTrue(evalue.isOptionalTensorList());
121-
122-
assertTrue(!evalue.toOptionalTensorList()[0].isPresent());
123-
124-
assertTrue(evalue.toOptionalTensorList()[1].isPresent());
125-
assertTrue(Arrays.equals(evalue.toOptionalTensorList()[1].get().shape, shape[0]));
126-
assertTrue(Arrays.equals(evalue.toOptionalTensorList()[1].get().getDataAsLongArray(), data[0]));
127-
128-
assertTrue(evalue.toOptionalTensorList()[2].isPresent());
129-
assertTrue(Arrays.equals(evalue.toOptionalTensorList()[2].get().shape, shape[1]));
130-
assertTrue(Arrays.equals(evalue.toOptionalTensorList()[2].get().getDataAsLongArray(), data[1]));
131-
}
132-
13368
@Test
13469
public void testAllIllegalCast() {
13570
EValue evalue = EValue.optionalNone();
@@ -174,46 +109,6 @@ public void testAllIllegalCast() {
174109
fail("Should have thrown an exception");
175110
} catch (IllegalStateException e) {
176111
}
177-
178-
// try bool list
179-
assertFalse(evalue.isBoolList());
180-
try {
181-
evalue.toBoolList();
182-
fail("Should have thrown an exception");
183-
} catch (IllegalStateException e) {
184-
}
185-
186-
// try int list
187-
assertFalse(evalue.isIntList());
188-
try {
189-
evalue.toIntList();
190-
fail("Should have thrown an exception");
191-
} catch (IllegalStateException e) {
192-
}
193-
194-
// try double list
195-
assertFalse(evalue.isDoubleList());
196-
try {
197-
evalue.toBool();
198-
fail("Should have thrown an exception");
199-
} catch (IllegalStateException e) {
200-
}
201-
202-
// try Tensor list
203-
assertFalse(evalue.isTensorList());
204-
try {
205-
evalue.toTensorList();
206-
fail("Should have thrown an exception");
207-
} catch (IllegalStateException e) {
208-
}
209-
210-
// try optional Tensor list
211-
assertFalse(evalue.isOptionalTensorList());
212-
try {
213-
evalue.toOptionalTensorList();
214-
fail("Should have thrown an exception");
215-
} catch (IllegalStateException e) {
216-
}
217112
}
218113

219114
@Test

0 commit comments

Comments
 (0)