Skip to content

Commit 685424d

Browse files
lucylqfacebook-github-bot
authored andcommitted
Remove extract_constant_segment from config (#5096)
Summary: Pull Request resolved: #5096 All usages of extract_constant_segment should be removed. Update program and method tests, and update test artifacts to only generate .pte with constant segment. Add test/models/deprecated/ containing - readme - .pte file with deprecated constant_buffer feature. Differential Revision: D61996249
1 parent cea5abb commit 685424d

File tree

13 files changed

+56
-56
lines changed

13 files changed

+56
-56
lines changed

exir/_serialize/_program.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ def serialize_pte_binary(
347347
*,
348348
mutable_data: Optional[List[Buffer]] = None,
349349
extract_delegate_segments: bool = False,
350-
extract_constant_segment: bool = True,
351350
segment_alignment: int = 128,
352351
constant_tensor_alignment: Optional[int] = None,
353352
delegate_alignment: Optional[int] = None,
@@ -363,8 +362,6 @@ def serialize_pte_binary(
363362
and the starting segment offset.
364363
- Update the Program.segments field with the offsets and lengths
365364
of each segment.
366-
extract_constant_segment: Whether to move the constant data from the Program
367-
into a separate segment.
368365
segment_alignment: Alignment in bytes. The starting offset of each
369366
segment will be aligned to this value in the output data.
370367
constant_tensor_alignment: The minimum alignment of tensor
@@ -387,19 +384,18 @@ def serialize_pte_binary(
387384
# Store extracted segment data; this may be constant data or delegate data.
388385
segments: List[Cord] = []
389386

390-
if extract_constant_segment:
391-
constant_segment_data, constant_segment_offsets = _extract_constant_segment(
392-
program.constant_buffer, tensor_alignment=constant_tensor_alignment
387+
constant_segment_data, constant_segment_offsets = _extract_constant_segment(
388+
program.constant_buffer, tensor_alignment=constant_tensor_alignment
389+
)
390+
if len(constant_segment_data) > 0:
391+
# Update program.constant_segment with constant subsegment offset information.
392+
program.constant_segment = SubsegmentOffsets(
393+
segment_index=len(segments), offsets=constant_segment_offsets
393394
)
394-
if len(constant_segment_data) > 0:
395-
# Update program.constant_segment with constant subsegment offset information.
396-
program.constant_segment = SubsegmentOffsets(
397-
segment_index=len(segments), offsets=constant_segment_offsets
398-
)
399-
# Clear the constant buffer, as constant data will be stored in segments.
400-
program.constant_buffer = []
401-
# Add to the aggregate segments cord.
402-
segments.append(constant_segment_data)
395+
# Clear the constant buffer, as constant data will be stored in segments.
396+
program.constant_buffer = []
397+
# Add to the aggregate segments cord.
398+
segments.append(constant_segment_data)
403399

404400
if mutable_data is not None:
405401
mutable_segment_data, mutable_segment_offsets = _extract_constant_segment(

exir/capture/_config.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ class ExecutorchBackendConfig:
6565
# This makes it possible to free those blobs at runtime.
6666
extract_delegate_segments: bool = True
6767

68-
# Whether to extract constants from the Program into separate segments,
69-
# rather than encoding those constants in the flatbuffer data.
70-
# This reduces the memory overhead of creating the .pte file for models with
71-
# large constant data.
72-
extract_constant_segment: bool = True
73-
7468
# When extracting segments, the starting offset of each segment will be
7569
# aligned to this value (in bytes). Must be a power of two.
7670
segment_alignment: int = 128

exir/program/_program.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,6 @@ def to_executorch(
439439
new_prog,
440440
emit_stacktrace=config.emit_stacktrace,
441441
extract_delegate_segments=config.extract_delegate_segments,
442-
extract_constant_segment=config.extract_constant_segment,
443442
segment_alignment=config.segment_alignment,
444443
constant_tensor_alignment=config.constant_tensor_alignment,
445444
delegate_alignment=config.delegate_alignment,
@@ -468,7 +467,6 @@ def __init__(
468467
exir_exported_program: ExirExportedProgram,
469468
emit_stacktrace: bool,
470469
extract_delegate_segments: bool,
471-
extract_constant_segment: bool,
472470
segment_alignment: int,
473471
constant_tensor_alignment: Optional[int] = None,
474472
delegate_alignment: Optional[int] = None,
@@ -483,7 +481,6 @@ def __init__(
483481
self._emitter_output: Optional[EmitterOutput] = None
484482
self._emit_stacktrace: bool = emit_stacktrace
485483
self._extract_delegate_segments: bool = extract_delegate_segments
486-
self._extract_constant_segment: bool = extract_constant_segment
487484
self._segment_alignment: int = segment_alignment
488485
self._constant_tensor_alignment: Optional[int] = constant_tensor_alignment
489486
self._delegate_alignment: Optional[int] = delegate_alignment
@@ -493,7 +490,6 @@ def _get_pte_data(self) -> Cord:
493490
self._pte_data = _serialize_pte_binary(
494491
program=self.program,
495492
extract_delegate_segments=self._extract_delegate_segments,
496-
extract_constant_segment=self._extract_constant_segment,
497493
segment_alignment=self._segment_alignment,
498494
constant_tensor_alignment=self._constant_tensor_alignment,
499495
delegate_alignment=self._delegate_alignment,
@@ -1351,7 +1347,6 @@ def __init__(
13511347
program=self._emitter_output.program,
13521348
mutable_data=self._emitter_output.mutable_data,
13531349
extract_delegate_segments=backend_config.extract_delegate_segments,
1354-
extract_constant_segment=backend_config.extract_constant_segment,
13551350
segment_alignment=backend_config.segment_alignment,
13561351
constant_tensor_alignment=backend_config.constant_tensor_alignment,
13571352
delegate_alignment=backend_config.delegate_alignment,

runtime/executor/test/method_test.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,9 @@ class MethodTest : public ::testing::Test {
5959
load_program(std::getenv("ET_MODULE_INDEX_PATH"), "index");
6060
load_program(
6161
std::getenv("ET_MODULE_DYNAMIC_CAT_UNALLOCATED_IO_PATH"), "cat");
62+
load_program(std::getenv("ET_MODULE_LINEAR_PATH"), "linear");
6263
load_program(
63-
std::getenv("ET_MODULE_LINEAR_CONSTANT_SEGMENT_PATH"),
64-
"linear_constant_segment");
65-
load_program(
66-
std::getenv("ET_MODULE_LINEAR_CONSTANT_BUFFER_PATH"),
64+
std::getenv("DEPRECATED_ET_MODULE_LINEAR_CONSTANT_BUFFER_PATH"),
6765
"linear_constant_buffer");
6866
}
6967

@@ -274,7 +272,7 @@ TEST_F(MethodTest, ConstantSegmentTest) {
274272
// Execute model with constants stored in segment.
275273
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
276274
Result<Method> method =
277-
programs_["linear_constant_segment"]->load_method("forward", &mmm.get());
275+
programs_["linear"]->load_method("forward", &mmm.get());
278276
ASSERT_EQ(method.error(), Error::Ok);
279277

280278
// Can execute the method.

runtime/executor/test/program_test.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,7 @@ TEST_F(ProgramTest, DEPRECATEDLoad) {
382382
TEST_F(ProgramTest, LoadConstantSegment) {
383383
// Load the serialized ModuleLinear data, with constants in the segment and no
384384
// constants in the flatbuffer.
385-
const char* linear_path =
386-
std::getenv("ET_MODULE_LINEAR_CONSTANT_SEGMENT_PATH");
385+
const char* linear_path = std::getenv("ET_MODULE_LINEAR_PATH");
387386
Result<FileDataLoader> linear_loader = FileDataLoader::from(linear_path);
388387
ASSERT_EQ(linear_loader.error(), Error::Ok);
389388

@@ -424,11 +423,11 @@ TEST_F(ProgramTest, LoadConstantSegment) {
424423
EXPECT_GE(flatbuffer_program->constant_segment()->offsets()->size(), 1);
425424
}
426425

427-
TEST_F(ProgramTest, LoadConstantSegmentWithNoConstantSegment) {
426+
TEST_F(ProgramTest, LoadConstantSegmentWhenConstantBufferExists) {
428427
// Load the serialized ModuleLinear data, with constants in the flatbuffer and
429428
// no constants in the segment.
430429
const char* linear_path =
431-
std::getenv("ET_MODULE_LINEAR_CONSTANT_BUFFER_PATH");
430+
std::getenv("DEPRECATED_ET_MODULE_LINEAR_CONSTANT_BUFFER_PATH");
432431
Result<FileDataLoader> linear_loader = FileDataLoader::from(linear_path);
433432
ASSERT_EQ(linear_loader.error(), Error::Ok);
434433

runtime/executor/test/targets.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,16 @@ def define_common_targets(is_fbcode = False):
9797
# file in fbcode. See https://fburl.com/9esapdmd
9898
if not runtime.is_oss and is_fbcode:
9999
modules_env = {
100+
# Deprecated model that still works with ExecuTorch runtime.
101+
"DEPRECATED_ET_MODULE_LINEAR_CONSTANT_BUFFER_PATH": "$(location fbcode//executorch/test/models/deprecated:ModuleLinear-no-constant-segment.pte)",
100102
# The tests use this var to find the program file to load. This uses
101103
# an fbcode target path because the authoring/export tools
102104
# intentionally don't work in xplat (since they're host-only tools).
103105
"ET_MODULE_ADD_HALF_PATH": "$(location fbcode//executorch/test/models:exported_programs[ModuleAddHalf.pte])",
104106
"ET_MODULE_ADD_PATH": "$(location fbcode//executorch/test/models:exported_programs[ModuleAdd.pte])",
105107
"ET_MODULE_DYNAMIC_CAT_UNALLOCATED_IO_PATH": "$(location fbcode//executorch/test/models:exported_programs[ModuleDynamicCatUnallocatedIO.pte])",
106108
"ET_MODULE_INDEX_PATH": "$(location fbcode//executorch/test/models:exported_programs[ModuleIndex.pte])",
107-
"ET_MODULE_LINEAR_CONSTANT_BUFFER_PATH": "$(location fbcode//executorch/test/models:exported_programs[ModuleLinear-no-constant-segment.pte])",
108-
"ET_MODULE_LINEAR_CONSTANT_SEGMENT_PATH": "$(location fbcode//executorch/test/models:exported_programs[ModuleLinear.pte])",
109+
"ET_MODULE_LINEAR_PATH": "$(location fbcode//executorch/test/models:exported_programs[ModuleLinear.pte])",
109110
"ET_MODULE_MULTI_ENTRY_PATH": "$(location fbcode//executorch/test/models:exported_programs[ModuleMultipleEntry.pte])",
110111
"ET_MODULE_SIMPLE_TRAIN_PATH": "$(location fbcode//executorch/test/models:exported_programs[ModuleSimpleTrain.pte])",
111112
}

schema/program.fbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ table Program {
429429
// Each constant is assigned an index into the table which are each individually aligned.
430430
// 0 index is reserved to be pointed to by non-constant Tensors.
431431
// If this field is non-empty, constant_segment.offsets must be empty.
432+
// DEPRECATED: After D61996249 on 2024-09-05, no new PTE files will use this field.
432433
constant_buffer:[Buffer];
433434

434435
// List of delegate data. Pointed to by BackendDelegateDataReference.

test/end2end/exported_module.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ def export(
6767
ignore_to_out_var_failure: bool = False,
6868
dynamic_memory_planning_mode: DynamicMemoryPlanningMode = DynamicMemoryPlanningMode.UPPER_BOUND,
6969
capture_config=None,
70-
extract_constant_segment: bool = True,
7170
skip_type_promotion: bool = False,
7271
export_joint_graph: bool = False,
7372
) -> "ExportedModule":
@@ -206,7 +205,6 @@ def __init__(self, method):
206205
dynamic_memory_planning_mode=dynamic_memory_planning_mode,
207206
memory_planning_pass=memory_planning_pass,
208207
to_out_var_pass=ToOutVarPass(ignore_to_out_var_failure),
209-
extract_constant_segment=extract_constant_segment,
210208
)
211209
)
212210

Binary file not shown.

test/models/deprecated/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Deprecated Models
2+
3+
This readme documents deprecated models that remain compatible with versions of the ExecuTorch runtime.
4+
5+
ModuleLinear-no-constant-segment.pte
6+
- This file contains constants stored in the constant_buffer, which was deprecated in D61996249 on 09-04-2024. Now, constants are stored in a separate segment.
7+
- Works with ExecuTorch runtime ET12.
8+
- Generated using commit hash e7c60c17d2d3, with command:
9+
```
10+
buck2 build fbcode//executorch/test/models:exported_programs[ModuleLinear-no-constant-segment.pte] --show-output
11+
```

test/models/deprecated/TARGETS

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2+
3+
oncall("executorch")
4+
5+
runtime.export_file(
6+
name = "ModuleLinear-no-constant-segment.pte",
7+
src = "ModuleLinear-no-constant-segment.pte",
8+
visibility = [
9+
"//executorch/runtime/executor/test/...",
10+
"//executorch/test/...",
11+
],
12+
)

test/models/export_program.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ def export_joint():
190190

191191
def export_module_to_program(
192192
module_class: Type[nn.Module],
193-
extract_constant_segment: bool,
194193
skip_type_promotion: bool,
195194
):
196195
"""Exports the module and returns the serialized program data."""
@@ -211,7 +210,6 @@ def export_module_to_program(
211210
module = ExportedModule.export(
212211
module_class,
213212
methods,
214-
extract_constant_segment=extract_constant_segment,
215213
skip_type_promotion=skip_type_promotion,
216214
export_joint_graph=export_joint,
217215
**export_kwargs,
@@ -259,18 +257,15 @@ def main() -> None:
259257
# Skip type promotion to keep the model in fp16.
260258
# Type promotion will convert to fp32.
261259
skip_type_promotion = True
262-
for extract_constant_segment in (True, False):
263-
suffix = "" if extract_constant_segment else "-no-constant-segment"
264-
outfile = os.path.join(args.outdir, f"{module_name}{suffix}.pte")
265-
with open(outfile, "wb") as fp:
266-
fp.write(
267-
export_module_to_program(
268-
module_class,
269-
extract_constant_segment=extract_constant_segment,
270-
skip_type_promotion=skip_type_promotion,
271-
)
260+
outfile = os.path.join(args.outdir, f"{module_name}.pte")
261+
with open(outfile, "wb") as fp:
262+
fp.write(
263+
export_module_to_program(
264+
module_class,
265+
skip_type_promotion=skip_type_promotion,
272266
)
273-
print(f"Exported {module_name} and wrote program data to {outfile}")
267+
)
268+
print(f"Exported {module_name} and wrote program data to {outfile}")
274269

275270

276271
if __name__ == "__main__":

test/run_oss_cpp_tests.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,23 @@ export_test_model() {
5656
python3 -m test.models.export_program --modules "ModuleAdd,ModuleAddHalf,ModuleDynamicCatUnallocatedIO,ModuleIndex,ModuleLinear,ModuleMultipleEntry,ModuleSimpleTrain" --outdir "cmake-out" 2> /dev/null
5757
python3 -m test.models.export_delegated_program --modules "ModuleAddMul" --backend_id "StubBackend" --outdir "cmake-out" || true
5858

59+
DEPRECATED_ET_MODULE_LINEAR_CONSTANT_BUFFER_PATH="$(realpath test/models/deprecated/ModuleLinear-no-constant-segment.pte)"
5960
ET_MODULE_ADD_HALF_PATH="$(realpath cmake-out/ModuleAddHalf.pte)"
6061
ET_MODULE_ADD_PATH="$(realpath cmake-out/ModuleAdd.pte)"
6162
ET_MODULE_DYNAMIC_CAT_UNALLOCATED_IO_PATH="$(realpath cmake-out/ModuleDynamicCatUnallocatedIO.pte)"
6263
ET_MODULE_INDEX_PATH="$(realpath cmake-out/ModuleIndex.pte)"
63-
ET_MODULE_LINEAR_CONSTANT_BUFFER_PATH="$(realpath cmake-out/ModuleLinear-no-constant-segment.pte)"
64-
ET_MODULE_LINEAR_CONSTANT_SEGMENT_PATH="$(realpath cmake-out/ModuleLinear.pte)"
64+
ET_MODULE_LINEAR_PATH="$(realpath cmake-out/ModuleLinear.pte)"
6565
ET_MODULE_MULTI_ENTRY_PATH="$(realpath cmake-out/ModuleMultipleEntry.pte)"
6666
ET_MODULE_ADD_MUL_NOSEGMENTS_DA1024_PATH="$(realpath cmake-out/ModuleAddMul-nosegments-da1024.pte)"
6767
ET_MODULE_ADD_MUL_NOSEGMENTS_PATH="$(realpath cmake-out/ModuleAddMul-nosegments.pte)"
6868
ET_MODULE_ADD_MUL_PATH="$(realpath cmake-out/ModuleAddMul.pte)"
6969
ET_MODULE_SIMPLE_TRAIN_PATH="$(realpath cmake-out/ModuleSimpleTrain.pte)"
70+
export DEPRECATED_ET_MODULE_LINEAR_CONSTANT_BUFFER_PATH
7071
export ET_MODULE_ADD_HALF_PATH
7172
export ET_MODULE_ADD_PATH
7273
export ET_MODULE_DYNAMIC_CAT_UNALLOCATED_IO_PATH
7374
export ET_MODULE_INDEX_PATH
74-
export ET_MODULE_LINEAR_CONSTANT_BUFFER_PATH
75-
export ET_MODULE_LINEAR_CONSTANT_SEGMENT_PATH
75+
export ET_MODULE_LINEAR_PATH
7676
export ET_MODULE_MULTI_ENTRY_PATH
7777
export ET_MODULE_ADD_MUL_NOSEGMENTS_DA1024_PATH
7878
export ET_MODULE_ADD_MUL_NOSEGMENTS_PATH

0 commit comments

Comments
 (0)