Skip to content
Open
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
19 changes: 19 additions & 0 deletions runtime/executor/method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,25 @@ Error Method::execute() {
"Cannot execute until method has been initialized.");
ET_LOG(Debug, "Executing method: %s.", method_meta().name());

// Validate that outputs are set.
for (size_t i = 0; i < method_meta().num_outputs(); i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just cache that we've checked this so we dont do this every time

auto& output = mutable_value(get_output_index(i));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: dont need a mutable reference here

if (!output.isTensor()) {
continue;
}

auto tensor_meta = method_meta().output_tensor_meta(i);
if (tensor_meta->is_memory_planned()) {
continue;
}

auto& t = output.toTensor();
if (t.const_data_ptr() == nullptr) {
ET_LOG(Error, "Output tensor at index %" ET_PRIsize_t " is not set.", i);
return Error::InvalidState;
}
}

// Chains are executed sequentially today, but future async designs may
// branch and run many in parallel or out of order.
for (step_state_.chain_idx = 0; step_state_.chain_idx < n_chains_;
Expand Down
33 changes: 33 additions & 0 deletions runtime/executor/test/method_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,36 @@ TEST_F(MethodTest, OptionalTensorListDeserialization) {
EXPECT_EQ(outputs.toTensor().size(2), 10);
}
*/

TEST_F(MethodTest, UnsetOutputTest) {
// Validate that methods with non-memory planned outputs return an error when
// the output data pointer is not set. It should not crash.

ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
Result<Method> method = programs_["cat"]->load_method("forward", &mmm.get());
ASSERT_EQ(method.error(), Error::Ok);

constexpr int buffer_size = 8;
float buffer[buffer_size];
for (int i = 0; i < buffer_size; ++i) {
buffer[i] = 0.f;
}
int32_t sizes[2] = {2, 4};
uint8_t dim_order[2] = {0, 1};
int32_t strides[2] = {4, 1};
executorch::aten::TensorImpl impl(
executorch::aten::ScalarType::Float,
2,
sizes,
buffer,
dim_order,
strides);

auto input_err =
method->set_input(EValue(executorch::aten::Tensor(&impl)), 0);
ASSERT_EQ(input_err, Error::Ok);

// Execute the method once. It should return an error (not crash).
auto execute_error = method->execute();
ASSERT_EQ(execute_error, Error::InvalidState);
}
Loading