Skip to content

Commit 09e8e19

Browse files
JacobSzwejbkafacebook-github-bot
authored andcommitted
clean up Program api
Summary: Delete all the old deprecated functions. Mark the remaining ones deprecated Differential Revision: D48164916 Privacy Context Container: L1124100 fbshipit-source-id: f77696ce736c12e8e8b7dc6271fa7cb2fcd6f2f8
1 parent 49ccb70 commit 09e8e19

File tree

11 files changed

+161
-352
lines changed

11 files changed

+161
-352
lines changed

exir/backend/test/demos/rpc/ExecutorBackend.cpp

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,38 @@ class ExecutorBackend final : public PyTorchBackendInterface {
6464
ET_ALLOCATE_INSTANCE_OR_RETURN_ERROR(runtime_allocator, Program);
6565
new (client_program) Program(std::move(program_result.get()));
6666

67+
Result<MethodMeta> method_meta = client_program->method_meta("forward");
68+
if (!method_meta.ok()) {
69+
ET_LOG(Error, "error constructing method meta");
70+
return method_meta.error();
71+
}
72+
6773
// Building all different allocators for the client executor
6874
auto client_const_allocator = ET_ALLOCATE_INSTANCE_OR_RETURN_ERROR(
6975
runtime_allocator, MemoryAllocator);
7076
new (client_const_allocator) MemoryAllocator(0, nullptr);
7177

72-
size_t num_non_const_buffers = client_program->num_non_const_buffers() - 1;
78+
auto num_buffers = method_meta->num_non_const_buffers();
79+
size_t num_non_const_buffers = num_buffers - 1;
7380

7481
uint8_t** non_const_buffers = ET_ALLOCATE_LIST_OR_RETURN_ERROR(
7582
runtime_allocator, uint8_t*, num_non_const_buffers);
7683
MemoryAllocator* non_const_allocators = ET_ALLOCATE_LIST_OR_RETURN_ERROR(
7784
runtime_allocator, MemoryAllocator, num_non_const_buffers);
7885

79-
for (size_t id = 1; id < client_program->num_non_const_buffers(); ++id) {
80-
const size_t buffer_size = client_program->get_non_const_buffer_size(id);
86+
for (size_t id = 1; id < num_buffers; ++id) {
87+
auto buffer_size = method_meta->non_const_buffer_size(id);
8188
uint8_t* buffer_i = ET_ALLOCATE_LIST_OR_RETURN_ERROR(
82-
runtime_allocator, uint8_t, buffer_size);
89+
runtime_allocator, uint8_t, buffer_size.get());
8390
non_const_buffers[id - 1] = buffer_i;
8491
new (&non_const_allocators[id - 1])
85-
MemoryAllocator(static_cast<uint32_t>(buffer_size), buffer_i);
92+
MemoryAllocator(static_cast<uint32_t>(buffer_size.get()), buffer_i);
8693
}
8794

8895
auto client_non_const_allocator = ET_ALLOCATE_INSTANCE_OR_RETURN_ERROR(
8996
runtime_allocator, HierarchicalAllocator);
90-
new (client_non_const_allocator) HierarchicalAllocator(
91-
client_program->num_non_const_buffers() - 1, non_const_allocators);
97+
new (client_non_const_allocator)
98+
HierarchicalAllocator(num_non_const_buffers, non_const_allocators);
9299

93100
// Allocate some memory from runtime allocator for the client executor, in
94101
// real case, like if it's an executor in dsp, it should allocate memory
@@ -113,46 +120,49 @@ class ExecutorBackend final : public PyTorchBackendInterface {
113120
client_runtime_allocator,
114121
client_temp_allocator);
115122

116-
// Construct the client executor
117-
auto client_executor =
118-
ET_ALLOCATE_INSTANCE_OR_RETURN_ERROR(runtime_allocator, Executor);
119-
new (client_executor) Executor(client_program, client_memory_manager);
120-
121-
// Initialize the client executor
122-
Error err = client_executor->init_execution_plan("forward");
123-
if (err != Error::Ok) {
124-
ET_LOG(Error, "Failed to init client executor: 0x%x", (unsigned int)err);
125-
return err;
123+
// Construct the client Method
124+
Result<Method> method_res =
125+
client_program->load_method("forward", client_memory_manager);
126+
if (!method_res.ok()) {
127+
ET_LOG(
128+
Error,
129+
"Failed to load client method: 0x%x",
130+
(unsigned int)method_res.error());
131+
return method_res.error();
126132
}
127133

128-
// Return the client executor so it will be passed to `execute()` as
134+
auto client_method =
135+
ET_ALLOCATE_INSTANCE_OR_RETURN_ERROR(runtime_allocator, Method);
136+
new (client_method) Method(std::move(method_res.get()));
137+
138+
// Return the client method so it will be passed to `execute()` as
129139
// `handle`.
130-
return client_executor;
140+
return client_method;
131141
}
132142

133143
Error execute(DelegateHandle* handle, EValue** args) const override {
134-
Executor* client_executor = static_cast<Executor*>(handle);
135-
auto& plan = client_executor->execution_plan();
136-
auto plan_inputs_size = plan.inputs_size();
144+
Method* client_method = static_cast<Method*>(handle);
145+
auto num_inputs = client_method->inputs_size();
137146
Error status = Error::Ok;
138147

139148
// Receive client executor input
140-
for (size_t input_idx = 0; input_idx < plan_inputs_size; input_idx++) {
141-
status = plan.set_input(*args[input_idx], input_idx);
149+
for (size_t input_idx = 0; input_idx < num_inputs; input_idx++) {
150+
status = client_method->set_input(*args[input_idx], input_idx);
142151
}
143152
// Execute client executor
144-
status = plan.execute();
153+
status = client_method->execute();
145154

146155
// Send the client executor output
147-
status = plan.get_outputs(args[plan_inputs_size], plan.outputs_size());
156+
status = client_method->get_outputs(
157+
args[num_inputs], client_method->outputs_size());
148158

149159
return status;
150160
}
151161

152162
void destroy(DelegateHandle* handle) const override {
153163
if (handle != nullptr) {
154-
Executor* client_executor = static_cast<Executor*>(handle);
155-
client_executor->~Executor();
164+
Method* client_executor = static_cast<Method*>(handle);
165+
client_executor->~Method();
156166
}
157167
}
158168
};

extension/pybindings/module.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ void init_module_functions(py::module_& m) {
555555
.def("run_method", &PyModule::run_method)
556556
.def("forward", &PyModule::forward)
557557
.def_property_readonly_static("FORWARD_METHOD_INDEX", [](py::object) {
558-
return torch::executor::Program::kForwardMethodIndex;
558+
return 0; // TODO(T152881296) remove this
559559
});
560560
;
561561

runtime/executor/executor.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ class __ET_DEPRECATED Executor {
3737
* May only be called once for the lifetime of the Executor.
3838
*
3939
* @param[in] index The index of the entry point to use for this plan.
40-
* Defaults to using the `forward()` method.
40+
* Defaults to using the first method sorted alphabetically.
4141
* @retval Error::Ok on successful initialization.
4242
*/
43-
__ET_DEPRECATED __ET_NODISCARD Error
44-
init_execution_plan(size_t index = Program::kForwardMethodIndex);
43+
__ET_DEPRECATED __ET_NODISCARD Error init_execution_plan(size_t index = 0);
4544

4645
/**
4746
* DEPRECATED: Use `Program::load_method()` instead.

runtime/executor/method.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ Method::set_input(const EValue& input_evalue, size_t input_idx) {
675675
"The %zu-th input of method should have the same type as the input_evalue, but get tag %u and tag %u",
676676
input_idx,
677677
e.tag,
678-
input_evalue.tag);
678+
(unsigned int)input_evalue.tag);
679679

680680
if (e.isTensor()) {
681681
const auto& t_dst = e.toTensor();

runtime/executor/program.cpp

Lines changed: 7 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -43,44 +43,6 @@ bool IsAligned(const void* data) {
4343
return addr % kMinimumAlignment == 0;
4444
}
4545

46-
/**
47-
* Tries deserializing the data as a Program flabuffer file. Returns nullptr if
48-
* the file appears to be corrupt or incompatible.
49-
*/
50-
const executorch_flatbuffer::Program* DeserializeFlatbufferData(
51-
const void* data) {
52-
if (Program::check_header(data, Program::kMinHeadBytes) !=
53-
Program::HeaderStatus::CompatibleVersion) {
54-
ET_LOG(
55-
Error,
56-
"Program identifier '%.4s' != expected '%.4s'",
57-
flatbuffers::GetBufferIdentifier(data),
58-
executorch_flatbuffer::ProgramIdentifier());
59-
return nullptr;
60-
}
61-
62-
// The provided pointer must start at an aligned address to ensure internal
63-
// alignment of flatbuffer fields.
64-
if (!IsAligned(data)) {
65-
ET_LOG(
66-
Error,
67-
"Program data 0x%p must be aligned to %zu",
68-
data,
69-
kMinimumAlignment);
70-
return nullptr;
71-
}
72-
73-
const executorch_flatbuffer::Program* program =
74-
executorch_flatbuffer::GetProgram(data);
75-
if (program->segments()->size() > 0) {
76-
ET_LOG(
77-
Error,
78-
"Program constructor does not support segments; use Program::Load()");
79-
return nullptr;
80-
}
81-
return program;
82-
}
83-
8446
Result<executorch_flatbuffer::ExecutionPlan*> get_execution_plan(
8547
const executorch_flatbuffer::Program* program,
8648
const char* method_name) {
@@ -97,16 +59,6 @@ Result<executorch_flatbuffer::ExecutionPlan*> get_execution_plan(
9759

9860
} // namespace
9961

100-
Program::Program(const void* serialized_content)
101-
: Program(
102-
/*loader=*/nullptr,
103-
/*segment_base_offset=*/0,
104-
FreeableBuffer(
105-
/*data=*/nullptr,
106-
/*size=*/0,
107-
/*free_fn=*/nullptr),
108-
DeserializeFlatbufferData(serialized_content)) {}
109-
11062
/* static */ Result<Program> Program::Load(
11163
DataLoader* loader,
11264
Program::Verification verification) {
@@ -233,43 +185,24 @@ Result<MethodMeta> Program::method_meta(const char* method_name) const {
233185
return MethodMeta(plan.get());
234186
}
235187

236-
const void* Program::get_constant_buffer_data(size_t buffer_index) const {
237-
ET_CHECK(is_valid());
188+
Result<const void*> Program::get_constant_buffer_data(
189+
size_t buffer_index) const {
238190
auto internal_program =
239191
static_cast<const executorch_flatbuffer::Program*>(internal_program_);
240-
ET_CHECK_MSG(
241-
buffer_index < constant_buffer_size(),
192+
size_t size = internal_program->constant_buffer()->size();
193+
ET_CHECK_OR_RETURN_ERROR(
194+
buffer_index < size,
195+
InvalidArgument,
242196
"Constant buffer %zu out of program buffer range %zu",
243197
buffer_index,
244-
constant_buffer_size());
198+
size);
245199

246200
const auto& constant_buffer = *internal_program->constant_buffer();
247201

248202
return static_cast<const void*>(
249203
constant_buffer[buffer_index]->storage()->data());
250204
}
251205

252-
size_t Program::constant_buffer_size() const {
253-
ET_CHECK(is_valid());
254-
auto internal_program =
255-
static_cast<const executorch_flatbuffer::Program*>(internal_program_);
256-
return internal_program->constant_buffer()->size();
257-
}
258-
259-
int64_t Program::get_non_const_buffer_size(
260-
size_t buffer_index,
261-
size_t execution_plan_index) const {
262-
ET_CHECK(is_valid());
263-
ET_CHECK_MSG(
264-
execution_plan_index == Program::kForwardMethodIndex,
265-
"Unsupported plan index %zu != %zu",
266-
execution_plan_index,
267-
Program::kForwardMethodIndex);
268-
auto res = this->get_non_const_buffer_size(buffer_index, "forward");
269-
ET_CHECK(res.ok());
270-
return res.get();
271-
}
272-
273206
Result<int64_t> Program::get_non_const_buffer_size(
274207
size_t buffer_index,
275208
const char* method_name) const {
@@ -289,18 +222,6 @@ Result<int64_t> Program::get_non_const_buffer_size(
289222
return (*(plan.get()->non_const_buffer_sizes()))[buffer_index];
290223
}
291224

292-
size_t Program::num_non_const_buffers(size_t execution_plan_index) const {
293-
ET_CHECK(is_valid());
294-
ET_CHECK_MSG(
295-
execution_plan_index == Program::kForwardMethodIndex,
296-
"Unsupported plan index %zu != %zu",
297-
execution_plan_index,
298-
Program::kForwardMethodIndex);
299-
auto res = this->num_non_const_buffers("forward");
300-
ET_CHECK(res.ok());
301-
return res.get();
302-
}
303-
304225
Result<size_t> Program::num_non_const_buffers(const char* method_name) const {
305226
auto plan = get_execution_plan(internal_program_, method_name);
306227
if (!plan.ok()) {
@@ -309,18 +230,6 @@ Result<size_t> Program::num_non_const_buffers(const char* method_name) const {
309230
return plan.get()->non_const_buffer_sizes()->size();
310231
}
311232

312-
const char* Program::get_output_flattening_encoding(
313-
size_t execution_plan_index) const {
314-
ET_CHECK(is_valid());
315-
ET_CHECK_MSG(
316-
execution_plan_index == Program::kForwardMethodIndex,
317-
"Executor only supports a single execution plan at this time, but received a query about plan #%zu",
318-
execution_plan_index);
319-
auto res = this->get_output_flattening_encoding("forward");
320-
ET_CHECK(res.ok());
321-
return res.get();
322-
}
323-
324233
Result<const char*> Program::get_output_flattening_encoding(
325234
const char* method_name) const {
326235
auto plan = get_execution_plan(internal_program_, method_name);
@@ -334,7 +243,6 @@ Error Program::get_backend_delegate_data(
334243
size_t index,
335244
const void** out_data,
336245
size_t* out_size) const {
337-
ET_CHECK(is_valid());
338246
const auto* data_list =
339247
static_cast<const executorch_flatbuffer::Program*>(internal_program_)
340248
->backend_delegate_data();

0 commit comments

Comments
 (0)