Skip to content

Update bundled_program to use new namespace #5200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2024
Merged
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
41 changes: 26 additions & 15 deletions devtools/bundled_program/bundled_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@
#include <executorch/runtime/executor/method.h>
#include <executorch/runtime/platform/log.h>

namespace torch {
namespace executor {
using exec_aten::ArrayRef;
using exec_aten::Half;
using exec_aten::ScalarType;
using exec_aten::Tensor;
using ::executorch::runtime::Error;
using ::executorch::runtime::EValue;
using ::executorch::runtime::Method;
using ::executorch::runtime::Result;

namespace executorch {
namespace bundled_program {

namespace {

#define kMaxDim 16
constexpr size_t kMaxDim = 16;

#ifdef USE_ATEN_LIB

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

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

// Load testset_idx-th bundled data into the Method
ET_NODISCARD Error LoadBundledInput(
ET_NODISCARD Error load_bundled_input(
Method& method,
serialized_bundled_program* bundled_program_ptr,
SerializedBundledProgram* bundled_program_ptr,
size_t testset_idx) {
ET_CHECK_OR_RETURN_ERROR(
bundled_program_flatbuffer::BundledProgramBufferHasIdentifier(
Expand Down Expand Up @@ -319,19 +328,19 @@ ET_NODISCARD Error LoadBundledInput(
ET_CHECK_OR_RETURN_ERROR(
status == Error::Ok,
NotSupported,
"set_input failed during load bundled inputs with status %" PRIu32,
static_cast<error_code_t>(status));
"set_input failed during load bundled inputs with status 0%" PRIx32,
static_cast<uint32_t>(status));
}

internal::event_tracer_set_bundled_input_index(
::executorch::runtime::internal::event_tracer_set_bundled_input_index(
method.get_event_tracer(), testset_idx);

return Error::Ok;
}

ET_NODISCARD Error VerifyResultWithBundledExpectedOutput(
ET_NODISCARD Error verify_method_outputs(
Method& method,
serialized_bundled_program* bundled_program_ptr,
SerializedBundledProgram* bundled_program_ptr,
size_t testset_idx,
double rtol,
double atol) {
Expand Down Expand Up @@ -390,12 +399,12 @@ ET_NODISCARD Error VerifyResultWithBundledExpectedOutput(
return Error::Ok;
}

ET_NODISCARD Error GetProgramData(
ET_NODISCARD Error get_program_data(
void* file_data,
size_t file_data_len,
const void** out_program_data,
size_t* out_program_data_len) {
if (IsBundledProgram(file_data)) {
if (is_bundled_program(file_data, file_data_len)) {
auto program_bundled =
bundled_program_flatbuffer::GetBundledProgram(file_data);
*out_program_data = program_bundled->program()->data();
Expand All @@ -410,11 +419,13 @@ ET_NODISCARD Error GetProgramData(
return Error::Ok;
}

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

} // namespace bundled_program
} // namespace executor
} // namespace torch
} // namespace executorch
71 changes: 60 additions & 11 deletions devtools/bundled_program/bundled_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
#include <executorch/runtime/core/memory_allocator.h>
#include <executorch/runtime/executor/method.h>

namespace torch {
namespace executor {
namespace executorch {
namespace bundled_program {

/**
* An opaque pointer to a serialized bundled program.
*/
using serialized_bundled_program = const void;
using SerializedBundledProgram = const void;

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

/**
Expand All @@ -49,9 +48,9 @@ ET_NODISCARD Error LoadBundledInput(
* @returns Return Error::Ok if two outputs match, or the error happens during
* execution.
*/
ET_NODISCARD Error VerifyResultWithBundledExpectedOutput(
Method& method,
serialized_bundled_program* bundled_program_ptr,
ET_NODISCARD ::executorch::runtime::Error verify_method_outputs(
::executorch::runtime::Method& method,
SerializedBundledProgram* bundled_program_ptr,
size_t testset_idx,
double rtol = 1e-5,
double atol = 1e-8);
Expand All @@ -73,7 +72,7 @@ ET_NODISCARD Error VerifyResultWithBundledExpectedOutput(
* in it, and out_program_data/out_program_data_len point to the data. Other
* values on failure.
*/
ET_NODISCARD Error GetProgramData(
ET_NODISCARD ::executorch::runtime::Error get_program_data(
void* file_data,
size_t file_data_len,
const void** out_program_data,
Expand All @@ -83,11 +82,61 @@ ET_NODISCARD Error GetProgramData(
* Checks whether the given file is a bundled program.
*
* @param[in] file_data The contents of the given file.
* @param[in] file_data_len The length of file_data, in bytes.
*
* @returns true if the given file is a bundled program, false otherwise
*/
bool IsBundledProgram(void* file_data);
bool is_bundled_program(void* file_data, size_t file_data_len);

/// DEPRECATED: Use the version with the file_data_len parameter.
ET_DEPRECATED inline bool is_bundled_program(void* file_data) {
// 128 is enough data to contain the identifier in the flatbuffer header.
return is_bundled_program(file_data, 128);
}

} // namespace bundled_program
} // namespace executorch

namespace torch {
namespace executor {
namespace bundled_program {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using serialized_bundled_program =
::executorch::bundled_program::SerializedBundledProgram;

ET_NODISCARD inline ::executorch::runtime::Error LoadBundledInput(
::executorch::runtime::Method& method,
serialized_bundled_program* bundled_program_ptr,
size_t testset_idx) {
return ::executorch::bundled_program::load_bundled_input(
method, bundled_program_ptr, testset_idx);
}

ET_NODISCARD inline ::executorch::runtime::Error
VerifyResultWithBundledExpectedOutput(
::executorch::runtime::Method& method,
serialized_bundled_program* bundled_program_ptr,
size_t testset_idx,
double rtol = 1e-5,
double atol = 1e-8) {
return ::executorch::bundled_program::verify_method_outputs(
method, bundled_program_ptr, testset_idx, rtol, atol);
}

ET_NODISCARD inline ::executorch::runtime::Error GetProgramData(
void* file_data,
size_t file_data_len,
const void** out_program_data,
size_t* out_program_data_len) {
return ::executorch::bundled_program::get_program_data(
file_data, file_data_len, out_program_data, out_program_data_len);
}

inline bool IsBundledProgram(void* file_data) {
// 128 is enough data to contain the identifier in the flatbuffer header.
return ::executorch::bundled_program::is_bundled_program(file_data, 128);
}
} // namespace bundled_program
} // namespace executor
} // namespace torch
14 changes: 7 additions & 7 deletions extension/pybindings/pybindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void et_pal_emit_log_message(
}

namespace py = pybind11;
using executorch::bundled_program::verify_method_outputs;
using ::executorch::extension::BufferDataLoader;
using ::executorch::extension::MallocMemoryAllocator;
using ::executorch::extension::MmapDataLoader;
Expand All @@ -92,8 +93,6 @@ using ::executorch::runtime::Span;
using ::executorch::runtime::Tag;
using torch::executor::etdump_result;
using torch::executor::ETDumpGen;
using torch::executor::bundled_program::LoadBundledInput;
using torch::executor::bundled_program::VerifyResultWithBundledExpectedOutput;

#ifndef USE_ATEN_LIB
using ::executorch::extension::alias_attensor_to_etensor;
Expand Down Expand Up @@ -655,11 +654,11 @@ struct PyModule final {
const std::string method_name,
size_t testset_idx) {
const void* bundled_program_ptr = m.get_bundled_program_ptr();
Error status = LoadBundledInput(
Error status = executorch::bundled_program::load_bundled_input(
module_->get_method(method_name), bundled_program_ptr, testset_idx);
THROW_IF_ERROR(
status,
"LoadBundledInput failed with status %" PRIu32,
"load_bundled_input failed with status 0x%" PRIx32,
static_cast<uint32_t>(status));
}

Expand All @@ -671,13 +670,14 @@ struct PyModule final {
double atol = 1e-8) {
const void* bundled_program_ptr = m.get_bundled_program_ptr();
auto& method = module_->get_method(method_name);
Error status = LoadBundledInput(method, bundled_program_ptr, testset_idx);
Error status = executorch::bundled_program::load_bundled_input(
method, bundled_program_ptr, testset_idx);
THROW_IF_ERROR(
status,
"LoadBundledInput failed with status %" PRIu32,
"load_bundled_input failed with status 0x%" PRIx32,
static_cast<uint32_t>(status));
py::list outputs = plan_execute(method_name);
status = VerifyResultWithBundledExpectedOutput(
status = executorch::bundled_program::verify_method_outputs(
method, bundled_program_ptr, testset_idx, rtol, atol);
THROW_IF_ERROR(
status,
Expand Down
Loading