Skip to content

Commit 5e87ec4

Browse files
Gasoonjiafacebook-github-bot
authored andcommitted
remove "Bundled" prefix for bundled program schema (#397)
Summary: Pull Request resolved: #397 Now we already have a specific namespace for bundledprogram, this diff removes the "Bundled" prefix for part of bundled program schema's variables. Rest variables will be updated in the following diffs. Differential Revision: D49337170 fbshipit-source-id: a858db2c2740ce7a6bef54dd7ba3db0953fb9195
1 parent e611fcc commit 5e87ec4

File tree

5 files changed

+97
-102
lines changed

5 files changed

+97
-102
lines changed

bundled_program/core.py

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,34 @@
88
import typing
99
from typing import Dict, List, Type
1010

11+
import executorch.bundled_program.schema as bp_schema
12+
import executorch.exir.schema as core_schema
13+
1114
import torch
1215
import torch.fx
1316
from executorch.bundled_program.config import (
1417
BundledConfig,
1518
ConfigExecutionPlanTest,
1619
ConfigValue,
1720
)
18-
from executorch.bundled_program.schema import (
19-
BundledBool,
20-
BundledDouble,
21-
BundledExecutionPlanTest,
22-
BundledInt,
23-
BundledIOSet,
24-
BundledProgram,
25-
BundledTensor,
26-
BundledValue,
27-
)
21+
2822
from executorch.bundled_program.version import BUNDLED_PROGRAM_SCHEMA_VERSION
2923
from executorch.exir._serialize import _serialize_pte_binary
30-
from executorch.exir.schema import (
31-
Bool,
32-
Double,
33-
ExecutionPlan,
34-
Int,
35-
KernelTypes,
36-
Program,
37-
Tensor,
38-
)
24+
3925
from executorch.exir.tensor import get_scalar_type, scalar_type_enum, TensorSpec
4026

4127
# pyre-ignore
42-
supported_program_type_table: Dict[Type[KernelTypes], ConfigValue] = {
43-
Tensor: torch.Tensor,
44-
Int: int,
45-
Double: float,
46-
Bool: bool,
28+
supported_program_type_table: Dict[Type[core_schema.KernelTypes], ConfigValue] = {
29+
core_schema.Tensor: torch.Tensor,
30+
core_schema.Int: int,
31+
core_schema.Double: float,
32+
core_schema.Bool: bool,
4733
}
4834

4935

50-
def emit_bundled_tensor(spec: TensorSpec, bundled_values: List[BundledValue]) -> None:
36+
def emit_bundled_tensor(
37+
spec: TensorSpec, bundled_values: List[bp_schema.Value]
38+
) -> None:
5139
# QuantizedSchema in tensor has deprecated and may not be used anymore.
5240
# So here we don't emit it.
5341

@@ -64,8 +52,8 @@ def emit_bundled_tensor(spec: TensorSpec, bundled_values: List[BundledValue]) ->
6452
tensor_data: bytes = bytes(spec_array)
6553

6654
bundled_values.append(
67-
BundledValue(
68-
val=BundledTensor(
55+
bp_schema.Value(
56+
val=bp_schema.Tensor(
6957
scalar_type=scalar_type_enum(spec.dtype),
7058
sizes=spec.shape,
7159
data=tensor_data,
@@ -75,55 +63,67 @@ def emit_bundled_tensor(spec: TensorSpec, bundled_values: List[BundledValue]) ->
7563
)
7664

7765

78-
def emit_prim(val: ConfigValue, bundled_values: List[BundledValue]):
66+
def emit_prim(val: ConfigValue, bundled_values: List[bp_schema.Value]):
7967
if type(val) == int:
80-
bundled_values.append(BundledValue(val=BundledInt(int_val=val)))
68+
bundled_values.append(bp_schema.Value(val=bp_schema.Int(int_val=val)))
8169
elif type(val) == bool:
82-
bundled_values.append(BundledValue(val=BundledBool(bool_val=val)))
70+
bundled_values.append(bp_schema.Value(val=bp_schema.Bool(bool_val=val)))
8371
elif type(val) == float:
84-
bundled_values.append(BundledValue(val=BundledDouble(double_val=val)))
72+
bundled_values.append(bp_schema.Value(val=bp_schema.Double(double_val=val)))
8573
else:
8674
assert 0, "Unsupported primitive type received."
8775

8876

89-
def get_program_input(program: Program, plan_idx: int, input_idx: int) -> KernelTypes:
77+
def get_program_input(
78+
program: core_schema.Program, plan_idx: int, input_idx: int
79+
) -> core_schema.KernelTypes:
9080
return (
9181
program.execution_plan[plan_idx]
9282
.values[program.execution_plan[plan_idx].inputs[input_idx]]
9383
.val
9484
)
9585

9686

97-
def get_program_output(program: Program, plan_idx: int, output_idx: int) -> KernelTypes:
87+
def get_program_output(
88+
program: core_schema.Program, plan_idx: int, output_idx: int
89+
) -> core_schema.KernelTypes:
9890
return (
9991
program.execution_plan[plan_idx]
10092
.values[program.execution_plan[plan_idx].outputs[output_idx]]
10193
.val
10294
)
10395

10496

105-
def get_input_dtype(program: Program, plan_idx: int, input_idx: int) -> torch.dtype:
97+
def get_input_dtype(
98+
program: core_schema.Program, plan_idx: int, input_idx: int
99+
) -> torch.dtype:
106100
# pyre-fixme[16]: now assert all input and outputs is in tenor type. Support multuple datatypes in the future.
107101
return get_scalar_type(get_program_input(program, plan_idx, input_idx).scalar_type)
108102

109103

110-
def get_input_type(program: Program, plan_idx: int, input_idx: int) -> type:
111-
type_lookup = {Int: int, Bool: bool, Double: float}
104+
def get_input_type(program: core_schema.Program, plan_idx: int, input_idx: int) -> type:
105+
type_lookup = {
106+
core_schema.Int: int,
107+
core_schema.Bool: bool,
108+
core_schema.Double: float,
109+
}
112110
# pyre-fixme[6]: Incompatible parameter type [6]: In call `dict.__getitem__`, for 1st positional only parameter
113-
# expected `Type[Union[Bool, Double, Int]]` but got `Type[Union[Bool, Double, Int, Tensor, BoolList, DoubleList,
111+
# expected `Type[Union[core_schema.Bool, core_schema.Double, core_schema.Int]]` but got `Type[Union[core_schema.Bool, core_schema.Double, core_schema.Int, core_schema.Tensor, BoolList, DoubleList,
114112
# IntList, Null, OptionalTensorList, String, TensorList]]`.
115113
return type_lookup[type(get_program_input(program, plan_idx, input_idx))]
116114

117115

118-
def get_output_dtype(program: Program, plan_idx: int, output_idx: int) -> torch.dtype:
116+
def get_output_dtype(
117+
program: core_schema.Program, plan_idx: int, output_idx: int
118+
) -> torch.dtype:
119119
return get_scalar_type(
120120
# pyre-ignore[16]: now assert all outputs is in tensor type.
121121
get_program_output(program, plan_idx, output_idx).scalar_type
122122
)
123123

124124

125125
def assert_valid_bundle(
126-
program: Program,
126+
program: core_schema.Program,
127127
bundled_config: BundledConfig,
128128
) -> None:
129129
"""Check if the program and BundledConfig matches each other.
@@ -163,7 +163,7 @@ def assert_valid_bundle(
163163
plan_test: ConfigExecutionPlanTest = bundled_config.execution_plan_tests[
164164
bp_plan_id
165165
]
166-
plan: ExecutionPlan = program.execution_plan[program_plan_id]
166+
plan: core_schema.ExecutionPlan = program.execution_plan[program_plan_id]
167167

168168
# User does not provide testcases for current plan, skip it
169169
if plan_test.method_name > plan.name:
@@ -185,7 +185,8 @@ def assert_valid_bundle(
185185
# Check if the type of Program's output is supported
186186
for index in range(len(plan.outputs)):
187187
assert (
188-
type(get_program_output(program, program_plan_id, index)) == Tensor
188+
type(get_program_output(program, program_plan_id, index))
189+
== core_schema.Tensor
189190
), "Only supports program with output in Tensor type."
190191

191192
# Check if the I/O sets of each execution plan test match program's requirement.
@@ -257,10 +258,10 @@ def assert_valid_bundle(
257258

258259

259260
def create_bundled_program(
260-
program: Program,
261+
program: core_schema.Program,
261262
bundled_config: BundledConfig,
262-
) -> BundledProgram:
263-
"""Create BundledProgram by bundling the given program and bundled_config together.
263+
) -> bp_schema.BundledProgram:
264+
"""Create bp_schema.BundledProgram by bundling the given program and bundled_config together.
264265
265266
Args:
266267
program: The program to be bundled.
@@ -269,16 +270,16 @@ def create_bundled_program(
269270

270271
assert_valid_bundle(program, bundled_config)
271272

272-
execution_plan_tests: List[BundledExecutionPlanTest] = []
273+
execution_plan_tests: List[bp_schema.BundledExecutionPlanTest] = []
273274

274275
# Emit data and metadata of bundled tensor
275276
for plan_test in bundled_config.execution_plan_tests:
276-
test_sets: List[BundledIOSet] = []
277+
test_sets: List[bp_schema.BundledIOSet] = []
277278

278279
# emit I/O sets for each execution plan test
279280
for i in range(len(plan_test.test_sets)):
280-
inputs: List[BundledValue] = []
281-
expected_outputs: List[BundledValue] = []
281+
inputs: List[bp_schema.Value] = []
282+
expected_outputs: List[bp_schema.Value] = []
282283

283284
cur_plan_test_inputs = plan_test.test_sets[i].inputs
284285
cur_plan_test_expected_outputs = plan_test.test_sets[i].expected_outputs
@@ -303,19 +304,19 @@ def create_bundled_program(
303304
expected_outputs,
304305
)
305306
test_sets.append(
306-
BundledIOSet(inputs=inputs, expected_outputs=expected_outputs)
307+
bp_schema.BundledIOSet(inputs=inputs, expected_outputs=expected_outputs)
307308
)
308309

309310
# emit the whole execution plan test
310311
execution_plan_tests.append(
311-
BundledExecutionPlanTest(
312+
bp_schema.BundledExecutionPlanTest(
312313
method_name=plan_test.method_name, test_sets=test_sets
313314
)
314315
)
315316

316317
program_bytes: bytes = _serialize_pte_binary(program)
317318

318-
return BundledProgram(
319+
return bp_schema.BundledProgram(
319320
version=BUNDLED_PROGRAM_SCHEMA_VERSION,
320321
execution_plan_tests=execution_plan_tests,
321322
program=program_bytes,

bundled_program/schema.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
@dataclass
16-
class BundledTensor:
16+
class Tensor:
1717
"""All information we need to bundle for a tensor EValue input."""
1818

1919
# The scalar type of Tensor
@@ -26,33 +26,33 @@ class BundledTensor:
2626

2727

2828
@dataclass
29-
class BundledInt:
29+
class Int:
3030
int_val: int
3131

3232

3333
@dataclass
34-
class BundledBool:
34+
class Bool:
3535
bool_val: bool
3636

3737

3838
@dataclass
39-
class BundledDouble:
39+
class Double:
4040
double_val: float
4141

4242

43-
BundledValueUnion = Union[
44-
BundledTensor,
45-
BundledInt,
46-
BundledDouble,
47-
BundledBool,
43+
ValueUnion = Union[
44+
Tensor,
45+
Int,
46+
Double,
47+
Bool,
4848
]
4949

5050

5151
@dataclass
52-
class BundledValue:
52+
class Value:
5353
"""Abstraction for BundledIOSet values"""
5454

55-
val: "BundledValueUnion"
55+
val: "ValueUnion"
5656

5757

5858
@dataclass
@@ -61,12 +61,12 @@ class BundledIOSet:
6161

6262
# All inputs required by Program for execution. Its length should be
6363
# equal to the length of program inputs.
64-
inputs: List[BundledValue]
64+
inputs: List[Value]
6565

6666
# The expected outputs generated while running the model in eager mode
6767
# using the inputs provided. Its length should be equal to the length
6868
# of program outputs.
69-
expected_outputs: List[BundledValue]
69+
expected_outputs: List[Value]
7070

7171

7272
@dataclass

bundled_program/tests/test_bundle_data.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,35 @@
99
import unittest
1010
from typing import List
1111

12+
import executorch.bundled_program.schema as bp_schema
13+
1214
import torch
1315
from executorch.bundled_program.config import ConfigValue
1416
from executorch.bundled_program.core import create_bundled_program
15-
from executorch.bundled_program.schema import (
16-
BundledBool,
17-
BundledDouble,
18-
BundledInt,
19-
BundledTensor,
20-
BundledValue,
21-
)
2217
from executorch.bundled_program.tests.common import get_common_program
2318
from executorch.exir._serialize import _serialize_pte_binary
2419

2520

2621
class TestBundle(unittest.TestCase):
2722
def assertIOsetDataEqual(
2823
self,
29-
program_ioset_data: List[BundledValue],
24+
program_ioset_data: List[bp_schema.Value],
3025
config_ioset_data: List[ConfigValue],
3126
) -> None:
3227
self.assertEqual(len(program_ioset_data), len(config_ioset_data))
3328
for program_element, config_element in zip(
3429
program_ioset_data, config_ioset_data
3530
):
36-
if isinstance(program_element.val, BundledTensor):
31+
if isinstance(program_element.val, bp_schema.Tensor):
3732
# TODO: Update to check the bundled input share the same type with the config input after supporting multiple types.
3833
self.assertTrue(isinstance(config_element, torch.Tensor))
3934
self.assertEqual(program_element.val.sizes, list(config_element.size()))
4035
# TODO(gasoonjia): Check the inner data.
41-
elif type(program_element.val) == BundledInt:
36+
elif type(program_element.val) == bp_schema.Int:
4237
self.assertEqual(program_element.val.int_val, config_element)
43-
elif type(program_element.val) == BundledDouble:
38+
elif type(program_element.val) == bp_schema.Double:
4439
self.assertEqual(program_element.val.double_val, config_element)
45-
elif type(program_element.val) == BundledBool:
40+
elif type(program_element.val) == bp_schema.Bool:
4641
self.assertEqual(program_element.val.bool_val, config_element)
4742

4843
def test_bundled_program(self) -> None:

0 commit comments

Comments
 (0)