Skip to content

Commit ad64232

Browse files
committed
Stop overwriting external constants that a method returns
A model that returns one of its constants (a buffer or a parameter) gives zeros at run time when it is exported with external_constants=True. There is no error. The same model with its constants kept inside the .pte file returns the right values. MethodMeta decides whether an output is memory planned by looking for an allocation or an inline data buffer. A constant stored in a .ptd file has neither, so it looks like an output with no storage. The Python bindings then give that output a new zero-filled buffer, and set_output_data_ptr replaces the constant's data pointer with it. From then on every read of the constant sees zeros, including operators that use it to compute other outputs. Treat a tensor whose data lives in an external file as memory planned, the same way inline constants already are. One helper now computes this for inputs, outputs and attributes, and the C++ and Objective-C header documentation now says that constants count as memory planned. Callers can see this change for external constants only. Inline constants already behaved this way: - TensorInfo::is_memory_planned() now returns true for an output or an attribute that is an external constant. The Python, Swift and JavaScript bindings show this value. - Method::set_output_data_ptr and Module::set_output on such an output now return InvalidState, instead of silently replacing the constant's data. Add a Python test that returns a buffer, and a sum that uses it, from a program exported with external constants. It checks that the buffer output reports is_memory_planned() and that both outputs stay right over 2 runs. A C++ test checks that the external constant attributes of an existing test program report is_memory_planned().
1 parent 61129f4 commit ad64232

6 files changed

Lines changed: 61 additions & 14 deletions

File tree

‎extension/apple/ExecuTorch/Exported/ExecuTorchModule.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ __attribute__((objc_subclassing_restricted))
3232
/** The scalar type of each element in the tensor. */
3333
@property(nonatomic, readonly) ExecuTorchDataType dataType;
3434

35-
/** YES if the runtime pre-allocated memory for this tensor. */
35+
/** YES if the tensor's memory was planned during export, or it is a constant. */
3636
@property(nonatomic, readonly) BOOL isMemoryPlanned;
3737

3838
/** The (optional) user-visible name of this tensor (may be empty) */

‎extension/pybindings/test/test_pybindings.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3072,6 +3072,31 @@ def test_method_keeps_its_event_tracer_alive(self):
30723072
)
30733073
self.assertIn("ran after its program was dropped", completed.stdout)
30743074

3075+
def test_program_data_separation_returns_constant(self) -> None:
3076+
class ReturnsBuffer(torch.nn.Module):
3077+
def __init__(self):
3078+
super().__init__()
3079+
self.register_buffer("c", torch.tensor([1.0, 2.0, 3.0]))
3080+
3081+
def forward(self, x):
3082+
return self.c, x + self.c
3083+
3084+
eager_module = ReturnsBuffer()
3085+
inputs = (torch.ones(3),)
3086+
program = to_edge(export(eager_module, inputs, strict=True)).to_executorch(
3087+
ExecutorchBackendConfig(external_constants=True)
3088+
)
3089+
data_buffer = bytes(program._tensor_data["_default_external_constant"])
3090+
executorch_module = self.load_fn(program.buffer, data_buffer)
3091+
meta = executorch_module.method_meta("forward")
3092+
self.assertTrue(meta.output_tensor_meta(0).is_memory_planned())
3093+
# Overwriting the constant would also change the sum and the next run.
3094+
for _ in range(2):
3095+
for actual, expected in zip(
3096+
executorch_module.forward(inputs), eager_module(*inputs)
3097+
):
3098+
torch.testing.assert_close(actual, expected)
3099+
30753100
def test_program_data_separation(self) -> None:
30763101
eager_module = ModuleLinear()
30773102
inputs = eager_module.get_inputs()

‎runtime/executor/method.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class Method final {
160160
*
161161
* @param[in] output_idx The index of the output to set the data_ptr for. Must
162162
* correspond to a tensor, and that tensor must not have had a buffer
163-
* allocated by the memory plan.
163+
* allocated by the memory plan or be a constant.
164164
*
165165
* @returns Error::Ok on success, non-Ok on failure.
166166
*/

‎runtime/executor/method_meta.cpp‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ etensor::Device tensor_device(const executorch_flatbuffer::Tensor* tensor) {
3535
static_cast<etensor::DeviceIndex>(
3636
tensor->extra_tensor_info()->device_index())};
3737
}
38+
39+
// Constants count as memory planned too: their data belongs to the program or
40+
// to an external data file, so a caller must not replace it.
41+
bool is_memory_planned(const executorch_flatbuffer::Tensor* tensor) {
42+
return tensor->allocation_info() != nullptr ||
43+
tensor->data_buffer_idx() != 0 ||
44+
(tensor->extra_tensor_info() != nullptr &&
45+
tensor->extra_tensor_info()->location() ==
46+
executorch_flatbuffer::TensorDataLocation::EXTERNAL);
47+
}
48+
3849
bool has_required_metadata(const executorch_flatbuffer::ExecutionPlan& plan) {
3950
const auto* values = plan.values();
4051
const auto* inputs = plan.inputs();
@@ -308,10 +319,8 @@ Result<TensorInfo> MethodMeta::input_tensor_meta(size_t index) const {
308319
Span<const uint8_t>(
309320
tensor_value->dim_order()->data(), tensor_value->dim_order()->size()),
310321
static_cast<executorch::aten::ScalarType>(tensor_value->scalar_type()),
311-
tensor_value->allocation_info() != nullptr ||
312-
tensor_value->data_buffer_idx() != 0 /* is_memory_planned */,
313-
std::string_view{nullptr, 0}, // Count constant returns as
314-
// memory planned.
322+
is_memory_planned(tensor_value),
323+
std::string_view{nullptr, 0},
315324
tensor_device(tensor_value));
316325
}
317326

@@ -366,10 +375,8 @@ Result<TensorInfo> MethodMeta::output_tensor_meta(size_t index) const {
366375
Span<const uint8_t>(
367376
tensor_value->dim_order()->data(), tensor_value->dim_order()->size()),
368377
static_cast<executorch::aten::ScalarType>(tensor_value->scalar_type()),
369-
tensor_value->allocation_info() != nullptr ||
370-
tensor_value->data_buffer_idx() != 0 /* is_memory_planned */,
371-
std::string_view{nullptr, 0}, // Count constant returns as
372-
// memory planned.
378+
is_memory_planned(tensor_value),
379+
std::string_view{nullptr, 0},
373380
tensor_device(tensor_value));
374381
}
375382

@@ -415,7 +422,6 @@ Result<TensorInfo> MethodMeta::attribute_tensor_meta(size_t index) const {
415422
index);
416423
auto t_name =
417424
tensor_value->extra_tensor_info()->fully_qualified_name();
418-
// Count constant returns as memory planned
419425
return TensorInfo::create(
420426
Span<const int32_t>(
421427
tensor_value->sizes()->data(), tensor_value->sizes()->size()),
@@ -424,8 +430,7 @@ Result<TensorInfo> MethodMeta::attribute_tensor_meta(size_t index) const {
424430
tensor_value->dim_order()->size()),
425431
static_cast<executorch::aten::ScalarType>(
426432
tensor_value->scalar_type()),
427-
tensor_value->allocation_info() != nullptr ||
428-
tensor_value->data_buffer_idx() != 0 /* is_memory_planned */,
433+
is_memory_planned(tensor_value),
429434
std::string_view{t_name->c_str(), t_name->size()},
430435
tensor_device(tensor_value));
431436
}

‎runtime/executor/method_meta.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ class TensorInfo final {
5858
executorch::aten::ScalarType scalar_type() const;
5959

6060
/**
61-
* Returns whether the tensor's memory was planned during export.
61+
* Returns whether the tensor's memory was planned during export. Also true
62+
* for constants, whose data belongs to the program or to an external data
63+
* file.
6264
*/
6365
bool is_memory_planned() const;
6466

‎runtime/executor/test/method_meta_test.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,21 @@ TEST_F(MethodMetaTest, MethodMetaAttribute) {
204204
ASSERT_EQ(bad_access.error(), Error::InvalidArgument);
205205
}
206206

207+
TEST_F(MethodMetaTest, ExternalConstantAttributesAreMemoryPlanned) {
208+
load_program(
209+
std::getenv("ET_MODULE_ADD_MUL_PROGRAM_PATH"), "add_mul_program");
210+
Result<MethodMeta> method_meta =
211+
programs_["add_mul_program"]->method_meta("forward");
212+
ASSERT_EQ(method_meta.error(), Error::Ok);
213+
214+
ASSERT_EQ(method_meta->num_attributes(), 2);
215+
for (size_t i = 0; i < method_meta->num_attributes(); ++i) {
216+
auto attribute = method_meta->attribute_tensor_meta(i);
217+
ASSERT_TRUE(attribute.ok());
218+
EXPECT_TRUE(attribute->is_memory_planned()) << attribute->name();
219+
}
220+
}
221+
207222
TEST_F(MethodMetaTest, MemoryPlannedBufferDeviceDefaultsCpu) {
208223
Result<MethodMeta> method_meta = programs_["add"]->method_meta("forward");
209224
ASSERT_EQ(method_meta.error(), Error::Ok);

0 commit comments

Comments
 (0)