Skip to content

[executorch][passes] Add config and pass to tag constants for external file #7193

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 5 commits into from
Dec 10, 2024
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
13 changes: 12 additions & 1 deletion examples/portable/scripts/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

# Example script for exporting simple models to flatbuffer

# pyre-unsafe

import argparse
import logging

Expand Down Expand Up @@ -48,6 +50,15 @@ def main() -> None:
required=False,
help="specify segment alignment in hex. Default is 0x1000. Use 0x4000 for iOS",
)

parser.add_argument(
"-e",
"--external_constants",
action=argparse.BooleanOptionalAction,
default=False,
help="Save constants in external .ptd file. Default is False",
)

parser.add_argument("-o", "--output_dir", default=".", help="output directory")

args = parser.parse_args()
Expand All @@ -62,7 +73,7 @@ def main() -> None:
*MODEL_NAME_TO_MODEL[args.model_name]
)

backend_config = ExecutorchBackendConfig()
backend_config = ExecutorchBackendConfig(external_constants=args.external_constants)
if args.segment_alignment is not None:
backend_config.segment_alignment = int(args.segment_alignment, 16)
if dynamic_shapes is not None:
Expand Down
4 changes: 4 additions & 0 deletions exir/capture/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ class ExecutorchBackendConfig:
# If set to true, view_copy operations will be converted to lightweight
# view operations in the ET runtime
remove_view_copy: bool = True

# If set to true, all constant tensors will be stored in a separate file,
# external to the PTE file.
external_constants: bool = False
27 changes: 27 additions & 0 deletions exir/emit/test/test_emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1633,3 +1633,30 @@ def forward(self, x):
input = torch.zeros(1)
executorch_model(input)
self.assertEqual(input, torch.ones(1))

def test_constant_tagged_tensors(self) -> None:
class LinearModule(torch.nn.Module):
def __init__(self):
super().__init__()
self.linear = torch.nn.Linear(5, 5)

def forward(self, x):
return self.linear(x)

model = to_edge(export(LinearModule(), (torch.ones(5, 5),))).to_executorch(
config=ExecutorchBackendConfig(
external_constants=True,
)
)
emitter_output = model._emitter_output
# Check that constant_buffer is empty besides the non-constant placeholder 0.
self.assertEqual(len(emitter_output.program.constant_buffer), 1)
# Check that constant weights are in the external constant buffer.
self.assertEqual(len(emitter_output.external_constant_buffer), 2)
# Setting external_constants=True, saves all constants to the key
# '_default_external_constant'.
external_map = emitter_output.external_constant_map[
"_default_external_constant"
]
self.assertEqual(external_map["linear.weight"], 0)
self.assertEqual(external_map["linear.bias"], 1)
11 changes: 11 additions & 0 deletions exir/passes/TARGETS
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ python_library(
deps = [
":const_prop_pass",
":debug_handle_generator_pass",
":external_constants_pass",
":insert_write_back_for_buffers_pass",
":memory_format_ops_pass",
":memory_planning_pass",
Expand Down Expand Up @@ -54,6 +55,16 @@ python_library(
],
)

python_library(
name = "external_constants_pass",
srcs = [
"external_constants_pass.py",
],
deps = [
"//caffe2:torch",
],
)

python_library(
name = "insert_write_back_for_buffers_pass",
srcs = [
Expand Down
29 changes: 29 additions & 0 deletions exir/passes/external_constants_pass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# pyre-strict

import torch
from executorch.exir.tensor import TensorSpec
from torch.export.exported_program import ExportedProgram


def external_constants_pass(
ep: ExportedProgram,
) -> ExportedProgram:
"""
Move all constants to external file.
"""
for module in ep.graph_module.modules():
if not isinstance(module, torch.fx.GraphModule):
continue

for node in module.graph.nodes:
if node.op == "placeholder":
spec = node.meta.get("spec")
if isinstance(spec, TensorSpec) and spec.const:
node.meta["constant_tag"] = "_default_external_constant"
return ep
4 changes: 4 additions & 0 deletions exir/program/_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
MemoryFormatOpsPass,
OpReplacePass,
)
from executorch.exir.passes.external_constants_pass import external_constants_pass
from executorch.exir.passes.insert_write_back_for_buffers_pass import (
insert_write_back_for_buffers_pass,
)
Expand Down Expand Up @@ -1380,6 +1381,9 @@ def to_executorch(
)
else:
new_gm_res = memory_planning_pass(new_gm) # pyre-ignore[29]

if config.external_constants:
new_gm_res = external_constants_pass(new_gm_res)
assert new_gm_res is not None
new_gm = new_gm_res.graph_module

Expand Down
Loading