Skip to content

Commit 976f952

Browse files
lucylqfacebook-github-bot
authored andcommitted
Add test when there are no segments (#12646)
Summary: The deprecation warning should only appear when there are values inside the constant_buffer and nothing inside the constant_segment. Even when there are no constants in the program, we expect to have one 'segment' storing the placeholder for non-constants. Fix failing jobs caused by error logging from #12295 Test failures: https://hud.pytorch.org/pr/pytorch/executorch/12631#46239138027 Arm test is using a recently-generated PTE, however hitting the deprecation warning because the pte has no constants. Differential Revision: D78568839
1 parent 9f2b8cd commit 976f952

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

runtime/executor/program.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,21 @@ Result<executorch_flatbuffer::ExecutionPlan*> get_execution_plan(
217217
std::move(constant_segment_data.get()),
218218
std::move(pte_data_map));
219219
} else {
220-
// The constant data is stored inside the flatbuffer, so this program does
221-
// not contain a separate segment for it.
222-
223-
// NOTE: This branch is deprecated from ExecuTorch 0.7 onwards.
224-
// Please regenerate your PTE file to ensure newer ExecuTorch runtimes can
225-
// support it. ExecuTorch deprecation policy:
226-
// https://docs.pytorch.org/executorch/stable/api-life-cycle.html#deprecation-policy.
227-
// For support, contact the PyTorch Edge team or make an issue in:
228-
// https://github.com/pytorch/executorch/issues.
229-
ET_LOG(
230-
Error,
231-
"!!DEPRECATED!! This branch is deprecated from ExecuTorch 0.7; re-export this PTE file to ensure support on newer runtimes.");
220+
if (constant_segment->offsets()->size() == 0) {
221+
// NOTE: Storing constants in the constant_buffer is deprecated from
222+
// ExecuTorch 0.7 onwards. Please regenerate your PTE file to ensure newer
223+
// ExecuTorch runtimes can support it. ExecuTorch deprecation policy:
224+
// https://docs.pytorch.org/executorch/stable/api-life-cycle.html#deprecation-policy.
225+
// For support, contact the PyTorch Edge team or make an issue in:
226+
// https://github.com/pytorch/executorch/issues.
227+
ET_LOG(
228+
Error,
229+
"!!DEPRECATED!! This branch is deprecated from ExecuTorch 0.7; re-export this PTE file to ensure support on newer runtimes.");
230+
}
231+
232+
// The constant data is either stored inside the flatbuffer, or there are
233+
// no constants; the constant segment is empty and does not
234+
// need to be loaded.
232235
return Program(
233236
loader,
234237
segment_base_offset,

runtime/executor/test/program_test.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,49 @@ TEST_F(ProgramTest, LoadConstantSegment) {
457457
EXPECT_GE(flatbuffer_program->constant_segment()->offsets()->size(), 1);
458458
}
459459

460+
TEST_F(ProgramTest, LoadConstantSegment_WhenEmpty) {
461+
// Load the serialized ModuleAddMul data.
462+
const char* add_path = std::getenv("ET_MODULE_ADD_PATH");
463+
Result<FileDataLoader> add_loader = FileDataLoader::from(add_path);
464+
ASSERT_EQ(add_loader.error(), Error::Ok);
465+
466+
// This file should always be compatible.
467+
Result<FreeableBuffer> add_header = add_loader->load(
468+
/*offset=*/0,
469+
Program::kMinHeadBytes,
470+
/*segment_info=*/
471+
DataLoader::SegmentInfo(DataLoader::SegmentInfo::Type::Program));
472+
ASSERT_EQ(add_header.error(), Error::Ok);
473+
EXPECT_EQ(
474+
Program::check_header(add_header->data(), add_header->size()),
475+
Program::HeaderStatus::CompatibleVersion);
476+
477+
Result<Program> program = Program::load(&add_loader.get());
478+
ASSERT_EQ(program.error(), Error::Ok);
479+
480+
// Expect no real segments when we try to load it.
481+
const auto segment_info = DataLoader::SegmentInfo(
482+
DataLoader::SegmentInfo::Type::Constant,
483+
/*segment_index_=*/0);
484+
Result<FreeableBuffer> segment =
485+
ProgramTestFriend::LoadSegment(&program.get(), segment_info);
486+
EXPECT_EQ(segment.error(), Error::NotFound);
487+
488+
const executorch_flatbuffer::Program* flatbuffer_program =
489+
ProgramTestFriend::GetInternalProgram(&program.get());
490+
491+
// Expect empty data segment for the placeholder constant.
492+
EXPECT_EQ(flatbuffer_program->segments()->size(), 1);
493+
494+
// The constant buffer should be empty.
495+
EXPECT_EQ(flatbuffer_program->constant_buffer()->size(), 0);
496+
497+
// Check constant segment offsets.
498+
EXPECT_EQ(flatbuffer_program->constant_segment()->segment_index(), 0);
499+
// Expect one offset for the placeholder constant.
500+
EXPECT_GE(flatbuffer_program->constant_segment()->offsets()->size(), 1);
501+
}
502+
460503
TEST_F(ProgramTest, LoadConstantSegmentWhenConstantBufferExists) {
461504
// Load the serialized ModuleAddMul data, with constants in the flatbuffer and
462505
// no constants in the segment.

0 commit comments

Comments
 (0)