Skip to content

Commit 29e90e0

Browse files
committed
Update on "[executorch][serialization] Data serialization interface"
Introduce data serialization interface. Differential Revision: [D65947145](https://our.internmc.facebook.com/intern/diff/D65947145/) [ghstack-poisoned]
2 parents 62ed79b + 6d480e1 commit 29e90e0

File tree

79 files changed

+487
-665
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+487
-665
lines changed

backends/apple/coreml/runtime/test/export_stateful_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def main() -> None:
4747
torch.randn((1, embedding_dim)),
4848
torch.tensor([0]),
4949
)
50-
exported_model = export(model, example_inputs)
50+
exported_model = export(model, example_inputs, strict=True)
5151
edge_program_manager = exir.to_edge(exported_model)
5252
compile_specs = CoreMLBackend.generate_compile_specs(
5353
compute_precision=ct.precision.FLOAT16,

backends/apple/coreml/test/test_coreml_partitioner.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717

1818
class TestCoreMLPartitioner(unittest.TestCase):
19-
2019
# TODO(T182928844): Delegate dim order op to backend.
2120
edge_compile_config = executorch.exir.EdgeCompileConfig(_skip_dim_order=True)
2221

@@ -34,7 +33,7 @@ def forward(self, a, x, b):
3433
model.eval()
3534

3635
example_inputs = (torch.randn(2, 2), torch.randn(2, 2), torch.randn(2, 2))
37-
exir_program_aten = torch.export.export(model, example_inputs)
36+
exir_program_aten = torch.export.export(model, example_inputs, strict=True)
3837

3938
edge_program_manager = executorch.exir.to_edge(
4039
exir_program_aten, compile_config=self.edge_compile_config
@@ -61,7 +60,7 @@ def test_vit_skip_conv(self):
6160
model.eval()
6261

6362
example_inputs = (torch.randn(1, 3, 224, 224),)
64-
exir_program_aten = torch.export.export(model, example_inputs)
63+
exir_program_aten = torch.export.export(model, example_inputs, strict=True)
6564
edge_program_manager = executorch.exir.to_edge(
6665
exir_program_aten, compile_config=self.edge_compile_config
6766
)
@@ -106,7 +105,7 @@ def forward(self, q, k_val, input_pos):
106105
k_val = torch.randn((1, embedding_dim))
107106
input_pos = torch.tensor([0])
108107
example_inputs = (q, k_val, input_pos)
109-
exir_program_aten = torch.export.export(model, example_inputs)
108+
exir_program_aten = torch.export.export(model, example_inputs, strict=True)
110109

111110
compile_specs = CoreMLBackend.generate_compile_specs(
112111
minimum_deployment_target=ct.target.iOS18

backends/apple/mps/test/test_mps_utils.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,7 @@ def lower_module_and_test_output(
247247
)
248248

249249
executorch_program = to_edge(
250-
export(
251-
delegated_program,
252-
sample_inputs,
253-
),
250+
export(delegated_program, sample_inputs, strict=True),
254251
compile_config=exir.EdgeCompileConfig(
255252
_check_ir_validity=False,
256253
_skip_dim_order=True, # TODO(T182928844): Delegate dim order op to backend.

backends/cadence/aot/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def export_program(
176176
torch._C._set_mkldnn_enabled(False)
177177

178178
# else: capture the model and return it.
179-
expo_program = export(model, inputs)
179+
expo_program = export(model, inputs, strict=True)
180180

181181
if dump_graphs:
182182
logging.info("Exported graph:")

backends/example/test_example_delegate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def get_example_inputs():
6060

6161
quantized_gm = m
6262
exported_program = to_edge(
63-
export(quantized_gm, copy.deepcopy(example_inputs)),
63+
export(quantized_gm, copy.deepcopy(example_inputs), strict=True),
6464
compile_config=EDGE_COMPILE_CONFIG,
6565
)
6666

@@ -92,7 +92,7 @@ def test_delegate_mobilenet_v2(self):
9292

9393
quantized_gm = m
9494
exported_program = to_edge(
95-
export(quantized_gm, copy.deepcopy(example_inputs)),
95+
export(quantized_gm, copy.deepcopy(example_inputs), strict=True),
9696
compile_config=EDGE_COMPILE_CONFIG,
9797
)
9898

backends/qualcomm/tests/test_qnn_delegate.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,7 @@ def test_qnn_backend_multi_contexts_composite(self):
16171617
)
16181618
sample_input = module.get_random_input()
16191619
edge_prog = to_edge(
1620-
torch.export.export(module, sample_input),
1620+
torch.export.export(module, sample_input, strict=True),
16211621
)
16221622
update_spill_fill_size(edge_prog.exported_program())
16231623
exec_prog = edge_prog.to_executorch()
@@ -1957,7 +1957,7 @@ def calibrator(gm):
19571957
self.assertEqual(len(exported_progs), 1)
19581958
# lower all graph again, the skipped operators will be left in CPU
19591959
exec_prog = to_edge(
1960-
torch.export.export(graph_module, sample_input),
1960+
torch.export.export(graph_module, sample_input, strict=True),
19611961
).to_executorch()
19621962
self.verify_output(module, sample_input, exec_prog)
19631963

@@ -2004,7 +2004,7 @@ def calibrator(gm):
20042004
self.assertEqual(len(exported_progs), 2)
20052005
# lower all graph again, the skipped operators will be left in CPU
20062006
exec_prog = exec_prog = to_edge(
2007-
torch.export.export(graph_module, sample_input),
2007+
torch.export.export(graph_module, sample_input, strict=True),
20082008
).to_executorch()
20092009
self.verify_output(module, sample_input, exec_prog)
20102010

@@ -2041,7 +2041,7 @@ def calibrator(gm):
20412041
self.assertEqual(len(exported_progs), 5)
20422042
# lower all graph again, the skipped operators will be delegated with fp16
20432043
exec_prog = to_edge(
2044-
torch.export.export(graph_module, sample_input),
2044+
torch.export.export(graph_module, sample_input, strict=True),
20452045
).to_executorch()
20462046
self.verify_output(module, sample_input, exec_prog)
20472047

@@ -2086,7 +2086,7 @@ def test_qnn_backend_multi_contexts_composite(self):
20862086
)
20872087
sample_input = module.get_random_input()
20882088
edge_prog = to_edge(
2089-
torch.export.export(module, sample_input),
2089+
torch.export.export(module, sample_input, strict=True),
20902090
)
20912091
update_spill_fill_size(edge_prog.exported_program())
20922092
exec_prog = edge_prog.to_executorch()
@@ -2721,7 +2721,6 @@ def test_ssd300_vgg16(self):
27212721

27222722

27232723
class TestExampleQaihubScript(TestQNN):
2724-
27252724
def required_envs(self, conditions=None) -> bool:
27262725
conditions = [] if conditions is None else conditions
27272726
return all(

backends/qualcomm/tests/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def get_qdq_module(
385385
custom_quant_annotations: Tuple[Callable] = (),
386386
quant_dtype: QuantDtype = QuantDtype.use_8a8w,
387387
) -> torch.fx.GraphModule:
388-
m = torch.export.export(module, inputs).module()
388+
m = torch.export.export(module, inputs, strict=True).module()
389389

390390
quantizer = QnnQuantizer()
391391
quantizer.add_custom_quant_annotations(custom_quant_annotations)

backends/qualcomm/utils/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def capture_program(
337337
inputs: Tuple[torch.Tensor],
338338
custom_pass_config: FrozenSet[str] = frozenset(),
339339
) -> exir.ExirExportedProgram:
340-
ep = torch.export.export(module, inputs)
340+
ep = torch.export.export(module, inputs, strict=True)
341341
decomposed_ep = ep.run_decompositions(get_decomp_table())
342342
# We choose call_operator by target in ConvertBinaryOpsWithScalar
343343
# because it is the same source_fn_stack for MultiheadAttention
@@ -551,7 +551,7 @@ def prepare_subgm(subgm, subgm_name):
551551

552552
fp_node_id_set = fp_node_id_set if fp_node_id_set is not None else set()
553553
fp_node_op_set = fp_node_op_set if fp_node_op_set is not None else set()
554-
graph_module = torch.export.export(nn_module, sample_input).module()
554+
graph_module = torch.export.export(nn_module, sample_input, strict=True).module()
555555
# define node support type
556556
capability_partitioner = CapabilityBasedPartitioner(
557557
graph_module,
@@ -664,7 +664,7 @@ def forward(self, *inputs):
664664
).default(inputs)
665665

666666
model = Model()
667-
prog = torch.export.export(model, tuple(inputs.values()))
667+
prog = torch.export.export(model, tuple(inputs.values()), strict=True)
668668
# bookkeeping for variables' life cycle
669669
return {
670670
"custom_op": custom_op,

backends/vulkan/runtime/graph/ops/glsl/copy_channel_offset.glsl

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@ ${layout_declare_tensor(B, "w", "t_out", DTYPE, STORAGE)}
1818
${layout_declare_tensor(B, "r", "existing_out", DTYPE, STORAGE)}
1919
${layout_declare_tensor(B, "r", "t_in", DTYPE, STORAGE)}
2020

21-
${layout_declare_ubo(B, "ivec4", "out_sizes")}
22-
${layout_declare_ubo(B, "ivec4", "in_sizes")}
23-
24-
layout(set = 0, binding = 5) uniform PRECISION restrict CopyArgs {
21+
layout(push_constant) uniform restrict Block {
22+
ivec4 out_sizes;
23+
ivec4 in_sizes;
2524
// Operates on (x, y, z) logical extents.
26-
ivec3 range;
25+
// channel_range is stored in range.w
26+
ivec4 range;
2727
// Analogus to range variable in copy. It defines the # of channel being
2828
// copied.
29-
int channel_range;
30-
ivec3 dst_offset;
31-
int dst_channel_offset;
29+
// dst channel offset is stored in dst_offset.w
30+
ivec4 dst_offset;
3231
int src_channel_offset;
3332
};
3433

@@ -47,11 +46,11 @@ void main() {
4746
// Note: Unlike other shaders, the range is often not equal to the destination
4847
// texture extent.
4948
const ivec3 lpos = ivec3(gl_GlobalInvocationID);
50-
if (any(greaterThanEqual(lpos, range))) {
49+
if (any(greaterThanEqual(lpos, range.xyz))) {
5150
return;
5251
}
5352

54-
const ivec3 out_lpos = lpos + dst_offset;
53+
const ivec3 out_lpos = lpos + dst_offset.xyz;
5554

5655
const ivec4 out_tidx = lpos_to_tidx(out_lpos, out_sizes, out_axis_map.w, packed_dim);
5756

@@ -61,12 +60,12 @@ void main() {
6160
ivec4 in_tidx = out_tidx;
6261
for (int i=0; i<4; i++) {
6362

64-
in_tidx[packed_dim] = out_tidx[packed_dim] - dst_channel_offset + i;
63+
in_tidx[packed_dim] = out_tidx[packed_dim] - dst_offset.w + i;
6564

6665
// Handle the partial update for begining of channel in an existing tensor.
6766
// If the source channel index is below zero or exceeds the range, we skip
6867
// updating the element to avoid overwriting existing data.
69-
if ((in_tidx[packed_dim] < 0) || (in_tidx[packed_dim] >= channel_range)) {
68+
if ((in_tidx[packed_dim] < 0) || (in_tidx[packed_dim] >= range.w)) {
7069
continue;
7170
}
7271

backends/vulkan/runtime/graph/ops/glsl/copy_offset.glsl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ layout(std430) buffer;
1717
${layout_declare_tensor(B, "w", "t_out", DTYPE, STORAGE)}
1818
${layout_declare_tensor(B, "r", "t_in", DTYPE, STORAGE)}
1919

20-
${layout_declare_ubo(B, "ivec3", "range", "ivec3", "src_offset", "ivec3", "dst_offset")}
20+
layout(push_constant) uniform restrict Block {
21+
ivec3 range;
22+
ivec3 src_offset;
23+
ivec3 dst_offset;
24+
};
2125

2226
#include "indexing_utils.h"
2327

backends/vulkan/runtime/graph/ops/impl/Copy.cpp

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@ void add_copy_offset_node(
3333
add_dtype_suffix(kernel_name, *t_out);
3434
add_storage_type_suffix(kernel_name, *t_out);
3535

36-
const struct Block final {
37-
alignas(16) ivec3 range;
38-
alignas(16) ivec3 src_offset;
39-
alignas(16) ivec3 dst_offset;
40-
} offset_params{
41-
range,
42-
src_offset,
43-
dst_offset,
44-
};
45-
4636
auto shader = VK_KERNEL_FROM_STR(kernel_name);
4737

4838
graph.execute_nodes().emplace_back(new DispatchNode(
@@ -56,11 +46,18 @@ void add_copy_offset_node(
5646
{in, vkapi::kRead},
5747
},
5848
// Parameter buffers
59-
{
60-
graph.create_params_buffer(offset_params),
61-
},
49+
{},
6250
// Specialization Constants
63-
{graph.hashed_layout_of(out), graph.hashed_layout_of(in)}));
51+
{graph.hashed_layout_of(out), graph.hashed_layout_of(in)},
52+
nullptr,
53+
{},
54+
{
55+
PushConstantDataInfo(&range, sizeof(range), sizeof(utils::ivec4)),
56+
PushConstantDataInfo(
57+
&src_offset, sizeof(src_offset), sizeof(utils::ivec4)),
58+
PushConstantDataInfo(
59+
&dst_offset, sizeof(dst_offset), sizeof(utils::ivec4)),
60+
}));
6461
}
6562

6663
void add_copy_channel_offset_node(
@@ -128,28 +125,23 @@ void add_copy_channel_offset_node(
128125
// The shader combines the global invocation id and the dst_offset to get
129126
// the actual coordinate.
130127

131-
ivec3 dst_offset{
128+
const ivec3 dst_offset{
132129
0, 0, dst_first_z + batch_idx * utils::div_up_4(out_channels)};
133130

134-
uvec3 global_size{
131+
const uvec3 global_size{
135132
utils::safe_downcast<uint32_t>(dim_at<kWidth4D>(in_sizes)),
136133
utils::safe_downcast<uint32_t>(dim_at<kHeight4D>(in_sizes)),
137134
utils::safe_downcast<uint32_t>(dst_last_z - dst_first_z + 1)};
138-
uvec3 local_size = graph.create_local_wg_size(global_size);
139-
140-
const struct Block final {
141-
ivec3 range;
142-
int32_t channel_range;
143-
ivec3 dst_offset;
144-
int32_t dst_channel_offset;
145-
int32_t src_channel_offset;
146-
} channel_offset_params{
147-
utils::make_ivec3(global_size),
148-
channel_range,
149-
dst_offset,
150-
dst_channel_offset,
151-
src_channel_offset,
152-
};
135+
const uvec3 local_size = graph.create_local_wg_size(global_size);
136+
137+
const utils::ivec4 range_params = {
138+
static_cast<int>(global_size[0]),
139+
static_cast<int>(global_size[1]),
140+
static_cast<int>(global_size[2]),
141+
channel_range};
142+
143+
const utils::ivec4 offset_params = {
144+
dst_offset[0], dst_offset[1], dst_offset[2], dst_channel_offset};
153145

154146
auto shader = VK_KERNEL_FROM_STR(kernel_name);
155147

@@ -165,13 +157,17 @@ void add_copy_channel_offset_node(
165157
{in, vkapi::MemoryAccessType::READ},
166158
},
167159
// Parameter buffers
168-
{
169-
t_out->sizes_ubo(),
170-
t_in->sizes_ubo(),
171-
graph.create_params_buffer(channel_offset_params),
172-
},
160+
{},
173161
// Specialization Constants
174-
{graph.hashed_layout_of(out), graph.hashed_layout_of(in)}));
162+
{graph.hashed_layout_of(out), graph.hashed_layout_of(in)},
163+
nullptr,
164+
{},
165+
{graph.sizes_pc_of(out),
166+
graph.sizes_pc_of(in),
167+
PushConstantDataInfo(&range_params, sizeof(range_params)),
168+
PushConstantDataInfo(&offset_params, sizeof(offset_params)),
169+
PushConstantDataInfo(
170+
&src_channel_offset, sizeof(src_channel_offset))}));
175171
}
176172
}
177173

backends/vulkan/targets.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@fbcode//target_determinator/macros:ci.bzl", "ci")
1+
load("@fbsource//tools/target_determinator/macros:ci.bzl", "ci")
22
load("@fbcode_macros//build_defs:native_rules.bzl", "buck_genrule")
33
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
44
load("@fbsource//tools/build_defs:platform_defs.bzl", "ANDROID", "CXX", "FBCODE")

backends/vulkan/test/compute_api_tests.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@fbcode//target_determinator/macros:ci.bzl", "ci")
1+
load("@fbsource//tools/target_determinator/macros:ci.bzl", "ci")
22
load("@fbsource//tools/build_defs:fb_xplat_cxx_binary.bzl", "fb_xplat_cxx_binary")
33
load("@fbsource//tools/build_defs:fb_xplat_cxx_test.bzl", "fb_xplat_cxx_test")
44
load("@fbsource//tools/build_defs:platform_defs.bzl", "ANDROID", "MACOSX", "CXX")

backends/vulkan/test/test_vulkan_delegate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def run_test():
112112
model(*sample_inputs)
113113

114114
program: ExportedProgram = export(
115-
model, sample_inputs, dynamic_shapes=dynamic_shapes
115+
model, sample_inputs, dynamic_shapes=dynamic_shapes, strict=True
116116
)
117117

118118
edge_program = to_edge_transform_and_lower(

backends/xnnpack/partition/graphs/sdpa.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def forward(
7676
v,
7777
mask,
7878
),
79+
strict=True,
7980
),
8081
compile_config=get_xnnpack_edge_compile_config(),
8182
)

backends/xnnpack/test/tester/tester.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def run(
194194
inputs: Tuple[torch.Tensor],
195195
) -> None:
196196
self.exported_program = export(
197-
artifact, inputs, dynamic_shapes=self.dynamic_shapes
197+
artifact, inputs, dynamic_shapes=self.dynamic_shapes, strict=True
198198
)
199199

200200
@property

build/packaging/smoke_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def export_linear_model() -> bytes:
6565

6666
# Export the pytorch model and process for ExecuTorch.
6767
print("Exporting program...")
68-
exported_program = export(LinearModel(), example_inputs)
68+
exported_program = export(LinearModel(), example_inputs, strict=True)
6969
print("Lowering to edge...")
7070
edge_program = to_edge(exported_program)
7171
print("Creating ExecuTorch program...")

devtools/backend_debug/tests/test_delegation_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def forward(self, a, x, b):
3131

3232
m = Model()
3333
inputs = (torch.randn(2, 2), torch.randn(2, 2), torch.randn(2, 2))
34-
edge = to_edge(torch.export.export(m, inputs)).to_backend(
34+
edge = to_edge(torch.export.export(m, inputs, strict=True)).to_backend(
3535
AddMulPartitionerDemo()
3636
)
3737
delegation_info = get_delegation_info(edge.exported_program().graph_module)

0 commit comments

Comments
 (0)