Skip to content

Commit 02304d7

Browse files
authored
Update bundled_program to use new namespace
Differential Revision: D62402292 Pull Request resolved: #5200
1 parent db34239 commit 02304d7

File tree

3 files changed

+93
-33
lines changed

3 files changed

+93
-33
lines changed

devtools/bundled_program/bundled_program.cpp

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,21 @@
2323
#include <executorch/runtime/executor/method.h>
2424
#include <executorch/runtime/platform/log.h>
2525

26-
namespace torch {
27-
namespace executor {
26+
using exec_aten::ArrayRef;
27+
using exec_aten::Half;
28+
using exec_aten::ScalarType;
29+
using exec_aten::Tensor;
30+
using ::executorch::runtime::Error;
31+
using ::executorch::runtime::EValue;
32+
using ::executorch::runtime::Method;
33+
using ::executorch::runtime::Result;
34+
35+
namespace executorch {
2836
namespace bundled_program {
2937

3038
namespace {
3139

32-
#define kMaxDim 16
40+
constexpr size_t kMaxDim = 16;
3341

3442
#ifdef USE_ATEN_LIB
3543

@@ -53,6 +61,7 @@ at::Tensor tensor_like(bundled_program_flatbuffer::Tensor* bundled_tensor) {
5361
}
5462

5563
#else // !USE_ATEN_LIB
64+
using torch::executor::TensorImpl;
5665
// Create a tensorimpl with same content using bundled tensor
5766
TensorImpl impl_like(bundled_program_flatbuffer::Tensor* bundled_tensor) {
5867
ScalarType scalar_type =
@@ -234,9 +243,9 @@ get_method_test_suite(
234243
} // namespace
235244

236245
// Load testset_idx-th bundled data into the Method
237-
ET_NODISCARD Error LoadBundledInput(
246+
ET_NODISCARD Error load_bundled_input(
238247
Method& method,
239-
serialized_bundled_program* bundled_program_ptr,
248+
SerializedBundledProgram* bundled_program_ptr,
240249
size_t testset_idx) {
241250
ET_CHECK_OR_RETURN_ERROR(
242251
bundled_program_flatbuffer::BundledProgramBufferHasIdentifier(
@@ -319,19 +328,19 @@ ET_NODISCARD Error LoadBundledInput(
319328
ET_CHECK_OR_RETURN_ERROR(
320329
status == Error::Ok,
321330
NotSupported,
322-
"set_input failed during load bundled inputs with status %" PRIu32,
323-
static_cast<error_code_t>(status));
331+
"set_input failed during load bundled inputs with status 0%" PRIx32,
332+
static_cast<uint32_t>(status));
324333
}
325334

326-
internal::event_tracer_set_bundled_input_index(
335+
::executorch::runtime::internal::event_tracer_set_bundled_input_index(
327336
method.get_event_tracer(), testset_idx);
328337

329338
return Error::Ok;
330339
}
331340

332-
ET_NODISCARD Error VerifyResultWithBundledExpectedOutput(
341+
ET_NODISCARD Error verify_method_outputs(
333342
Method& method,
334-
serialized_bundled_program* bundled_program_ptr,
343+
SerializedBundledProgram* bundled_program_ptr,
335344
size_t testset_idx,
336345
double rtol,
337346
double atol) {
@@ -390,12 +399,12 @@ ET_NODISCARD Error VerifyResultWithBundledExpectedOutput(
390399
return Error::Ok;
391400
}
392401

393-
ET_NODISCARD Error GetProgramData(
402+
ET_NODISCARD Error get_program_data(
394403
void* file_data,
395404
size_t file_data_len,
396405
const void** out_program_data,
397406
size_t* out_program_data_len) {
398-
if (IsBundledProgram(file_data)) {
407+
if (is_bundled_program(file_data, file_data_len)) {
399408
auto program_bundled =
400409
bundled_program_flatbuffer::GetBundledProgram(file_data);
401410
*out_program_data = program_bundled->program()->data();
@@ -410,11 +419,13 @@ ET_NODISCARD Error GetProgramData(
410419
return Error::Ok;
411420
}
412421

413-
bool IsBundledProgram(void* file_data) {
422+
bool is_bundled_program(void* file_data, ET_UNUSED size_t file_data_len) {
423+
// Even though the flatbuffer API doesn't accept a length, it's important to
424+
// require one so that we could change the internal representation, or use a
425+
// future API that does require a length.
414426
return bundled_program_flatbuffer::BundledProgramBufferHasIdentifier(
415427
file_data);
416428
}
417429

418430
} // namespace bundled_program
419-
} // namespace executor
420-
} // namespace torch
431+
} // namespace executorch

devtools/bundled_program/bundled_program.h

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
#include <executorch/runtime/core/memory_allocator.h>
1212
#include <executorch/runtime/executor/method.h>
1313

14-
namespace torch {
15-
namespace executor {
14+
namespace executorch {
1615
namespace bundled_program {
1716

1817
/**
1918
* An opaque pointer to a serialized bundled program.
2019
*/
21-
using serialized_bundled_program = const void;
20+
using SerializedBundledProgram = const void;
2221

2322
/**
2423
* Load testset_idx-th bundled input of method_idx-th Method test in
@@ -31,9 +30,9 @@ using serialized_bundled_program = const void;
3130
* @returns Return Error::Ok if load successfully, or the error happens during
3231
* execution.
3332
*/
34-
ET_NODISCARD Error LoadBundledInput(
35-
Method& method,
36-
serialized_bundled_program* bundled_program_ptr,
33+
ET_NODISCARD ::executorch::runtime::Error load_bundled_input(
34+
::executorch::runtime::Method& method,
35+
SerializedBundledProgram* bundled_program_ptr,
3736
size_t testset_idx);
3837

3938
/**
@@ -49,9 +48,9 @@ ET_NODISCARD Error LoadBundledInput(
4948
* @returns Return Error::Ok if two outputs match, or the error happens during
5049
* execution.
5150
*/
52-
ET_NODISCARD Error VerifyResultWithBundledExpectedOutput(
53-
Method& method,
54-
serialized_bundled_program* bundled_program_ptr,
51+
ET_NODISCARD ::executorch::runtime::Error verify_method_outputs(
52+
::executorch::runtime::Method& method,
53+
SerializedBundledProgram* bundled_program_ptr,
5554
size_t testset_idx,
5655
double rtol = 1e-5,
5756
double atol = 1e-8);
@@ -73,7 +72,7 @@ ET_NODISCARD Error VerifyResultWithBundledExpectedOutput(
7372
* in it, and out_program_data/out_program_data_len point to the data. Other
7473
* values on failure.
7574
*/
76-
ET_NODISCARD Error GetProgramData(
75+
ET_NODISCARD ::executorch::runtime::Error get_program_data(
7776
void* file_data,
7877
size_t file_data_len,
7978
const void** out_program_data,
@@ -83,11 +82,61 @@ ET_NODISCARD Error GetProgramData(
8382
* Checks whether the given file is a bundled program.
8483
*
8584
* @param[in] file_data The contents of the given file.
85+
* @param[in] file_data_len The length of file_data, in bytes.
8686
*
8787
* @returns true if the given file is a bundled program, false otherwise
8888
*/
89-
bool IsBundledProgram(void* file_data);
89+
bool is_bundled_program(void* file_data, size_t file_data_len);
90+
91+
/// DEPRECATED: Use the version with the file_data_len parameter.
92+
ET_DEPRECATED inline bool is_bundled_program(void* file_data) {
93+
// 128 is enough data to contain the identifier in the flatbuffer header.
94+
return is_bundled_program(file_data, 128);
95+
}
96+
97+
} // namespace bundled_program
98+
} // namespace executorch
99+
100+
namespace torch {
101+
namespace executor {
102+
namespace bundled_program {
103+
// TODO(T197294990): Remove these deprecated aliases once all users have moved
104+
// to the new `::executorch` namespaces.
105+
using serialized_bundled_program =
106+
::executorch::bundled_program::SerializedBundledProgram;
107+
108+
ET_NODISCARD inline ::executorch::runtime::Error LoadBundledInput(
109+
::executorch::runtime::Method& method,
110+
serialized_bundled_program* bundled_program_ptr,
111+
size_t testset_idx) {
112+
return ::executorch::bundled_program::load_bundled_input(
113+
method, bundled_program_ptr, testset_idx);
114+
}
115+
116+
ET_NODISCARD inline ::executorch::runtime::Error
117+
VerifyResultWithBundledExpectedOutput(
118+
::executorch::runtime::Method& method,
119+
serialized_bundled_program* bundled_program_ptr,
120+
size_t testset_idx,
121+
double rtol = 1e-5,
122+
double atol = 1e-8) {
123+
return ::executorch::bundled_program::verify_method_outputs(
124+
method, bundled_program_ptr, testset_idx, rtol, atol);
125+
}
126+
127+
ET_NODISCARD inline ::executorch::runtime::Error GetProgramData(
128+
void* file_data,
129+
size_t file_data_len,
130+
const void** out_program_data,
131+
size_t* out_program_data_len) {
132+
return ::executorch::bundled_program::get_program_data(
133+
file_data, file_data_len, out_program_data, out_program_data_len);
134+
}
90135

136+
inline bool IsBundledProgram(void* file_data) {
137+
// 128 is enough data to contain the identifier in the flatbuffer header.
138+
return ::executorch::bundled_program::is_bundled_program(file_data, 128);
139+
}
91140
} // namespace bundled_program
92141
} // namespace executor
93142
} // namespace torch

extension/pybindings/pybindings.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ void et_pal_emit_log_message(
7171
}
7272

7373
namespace py = pybind11;
74+
using executorch::bundled_program::verify_method_outputs;
7475
using ::executorch::extension::BufferDataLoader;
7576
using ::executorch::extension::MallocMemoryAllocator;
7677
using ::executorch::extension::MmapDataLoader;
@@ -92,8 +93,6 @@ using ::executorch::runtime::Span;
9293
using ::executorch::runtime::Tag;
9394
using torch::executor::etdump_result;
9495
using torch::executor::ETDumpGen;
95-
using torch::executor::bundled_program::LoadBundledInput;
96-
using torch::executor::bundled_program::VerifyResultWithBundledExpectedOutput;
9796

9897
#ifndef USE_ATEN_LIB
9998
using ::executorch::extension::alias_attensor_to_etensor;
@@ -655,11 +654,11 @@ struct PyModule final {
655654
const std::string method_name,
656655
size_t testset_idx) {
657656
const void* bundled_program_ptr = m.get_bundled_program_ptr();
658-
Error status = LoadBundledInput(
657+
Error status = executorch::bundled_program::load_bundled_input(
659658
module_->get_method(method_name), bundled_program_ptr, testset_idx);
660659
THROW_IF_ERROR(
661660
status,
662-
"LoadBundledInput failed with status %" PRIu32,
661+
"load_bundled_input failed with status 0x%" PRIx32,
663662
static_cast<uint32_t>(status));
664663
}
665664

@@ -671,13 +670,14 @@ struct PyModule final {
671670
double atol = 1e-8) {
672671
const void* bundled_program_ptr = m.get_bundled_program_ptr();
673672
auto& method = module_->get_method(method_name);
674-
Error status = LoadBundledInput(method, bundled_program_ptr, testset_idx);
673+
Error status = executorch::bundled_program::load_bundled_input(
674+
method, bundled_program_ptr, testset_idx);
675675
THROW_IF_ERROR(
676676
status,
677-
"LoadBundledInput failed with status %" PRIu32,
677+
"load_bundled_input failed with status 0x%" PRIx32,
678678
static_cast<uint32_t>(status));
679679
py::list outputs = plan_execute(method_name);
680-
status = VerifyResultWithBundledExpectedOutput(
680+
status = executorch::bundled_program::verify_method_outputs(
681681
method, bundled_program_ptr, testset_idx, rtol, atol);
682682
THROW_IF_ERROR(
683683
status,

0 commit comments

Comments
 (0)