Skip to content

[3/N] Add update function in backend interface #11391

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

Open
wants to merge 4 commits into
base: gh/cccclai/23/base
Choose a base branch
from
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
176 changes: 91 additions & 85 deletions runtime/backend/backend_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,88 +6,94 @@
* LICENSE file in the root directory of this source tree.
*/

#pragma once
#include <executorch/runtime/core/error.h>
#include <cstddef>
#include <cstring>
#include <variant>

namespace executorch {
namespace runtime {

// Strongly-typed option key template
template <typename T>
struct OptionKey {
const char* key;
constexpr explicit OptionKey(const char* k) : key(k) {}
};

// Union replaced with std::variant
using OptionValue = std::variant<bool, int, const char*>;

struct BackendOption {
const char* key; // key is the name of the backend option, like num_threads,
// enable_profiling, etc
OptionValue
value; // value is the value of the backend option, like 4, true, etc
};

template <size_t MaxCapacity>
class BackendOptions {
public:
// Initialize with zero options
BackendOptions() : size_(0) {}

// Type-safe setters
template <typename T>
void set_option(OptionKey<T> key, T value) {
const char* k = key.key;
// Update existing if found
for (size_t i = 0; i < size_; ++i) {
if (strcmp(options_[i].key, k) == 0) {
options_[i].value = value;
return;
}
}
// Add new option if space available
if (size_ < MaxCapacity) {
options_[size_++] = BackendOption{k, value};
}
}

// Type-safe getters
template <typename T>
Error get_option(OptionKey<T> key, T& out) const {
const char* k = key.key;
for (size_t i = 0; i < size_; ++i) {
if (strcmp(options_[i].key, k) == 0) {
if (auto* val = std::get_if<T>(&options_[i].value)) {
out = *val;
return Error::Ok;
}
return Error::InvalidArgument;
}
}
return Error::NotFound;
}

private:
BackendOption options_[MaxCapacity]{}; // Storage for backend options
size_t size_; // Current number of options
};

// Helper functions for creating typed option keys (unchanged)
constexpr OptionKey<bool> BoolKey(const char* k) {
return OptionKey<bool>(k);
}

constexpr OptionKey<int> IntKey(const char* k) {
return OptionKey<int>(k);
}

constexpr OptionKey<const char*> StrKey(const char* k) {
return OptionKey<const char*>(k);
}

} // namespace runtime
} // namespace executorch
#pragma once
#include <executorch/runtime/core/error.h>
#include <cstddef>
#include <cstring>
#include <executorch/runtime/core/array_ref.h>
#include <executorch/runtime/core/error.h>
#include <variant>

namespace executorch {
namespace runtime {

// Strongly-typed option key template
template <typename T>
struct OptionKey {
const char* key;
constexpr explicit OptionKey(const char* k) : key(k) {}
};

// Union replaced with std::variant
using OptionValue = std::variant<bool, int, const char*>;

struct BackendOption {
const char* key; // key is the name of the backend option, like num_threads,
// enable_profiling, etc
OptionValue
value; // value is the value of the backend option, like 4, true, etc
};

template <size_t MaxCapacity>
class BackendOptions {
public:
// Initialize with zero options
BackendOptions() : size_(0) {}

// Type-safe setters
template <typename T>
void set_option(OptionKey<T> key, T value) {
const char* k = key.key;
// Update existing if found
for (size_t i = 0; i < size_; ++i) {
if (strcmp(options_[i].key, k) == 0) {
options_[i].value = value;
return;
}
}
// Add new option if space available
if (size_ < MaxCapacity) {
options_[size_++] = BackendOption{k, value};
}
}

// Type-safe getters
template <typename T>
Error get_option(OptionKey<T> key, T& out) const {
const char* k = key.key;
for (size_t i = 0; i < size_; ++i) {
if (strcmp(options_[i].key, k) == 0) {
if (auto* val = std::get_if<T>(&options_[i].value)) {
out = *val;
return Error::Ok;
}
return Error::InvalidArgument;
}
}
return Error::NotFound;
}
executorch::runtime::ArrayRef<BackendOption> view() const {
return executorch::runtime::ArrayRef<BackendOption>(options_, size_);
}

private:
BackendOption options_[MaxCapacity]{}; // Storage for backend options
size_t size_; // Current number of options
};

// Helper functions for creating typed option keys (unchanged)
constexpr OptionKey<bool> BoolKey(const char* k) {
return OptionKey<bool>(k);
}

constexpr OptionKey<int> IntKey(const char* k) {
return OptionKey<int>(k);
}

constexpr OptionKey<const char*> StrKey(const char* k) {
return OptionKey<const char*>(k);
}

} // namespace runtime
} // namespace executorch

16 changes: 16 additions & 0 deletions runtime/backend/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include <executorch/runtime/backend/backend_execution_context.h>
#include <executorch/runtime/backend/backend_init_context.h>
#include <executorch/runtime/backend/backend_update_context.h>
#include <executorch/runtime/backend/backend_options.h>
#include <executorch/runtime/core/array_ref.h>
#include <executorch/runtime/core/error.h>
#include <executorch/runtime/core/evalue.h>
Expand Down Expand Up @@ -99,6 +101,20 @@ class BackendInterface {
DelegateHandle* handle,
EValue** args) const = 0;

/**
* Responsible update the backend status, if any. The backend options are passed in
* by users, and the backend can update its internal status based on the options.
*
* @param[in] context Runtime context if any. Currently it's not used.
* @param[in] args A list of BackendOptions passed in by users.
* @retval Error::Ok if successful.
*/
ET_NODISCARD virtual Error update(
Copy link
Contributor

Choose a reason for hiding this comment

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

I would jsut call it set_backend_options which seems more appropriate

BackendUpdateContext& context,
Copy link
Contributor

Choose a reason for hiding this comment

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

what is supposed to be the role of BackendUpdateContext?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a place holder for future API changes. Currently have the context for init and execute

const executorch::runtime::ArrayRef<BackendOption>& backend_options) const {
return Error::Ok;
};
Comment on lines +113 to +116
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the implementation of this? If it is in the next diff, can you pull it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's the base function. If backends didn't implement this function, this function will be called. If we set it to virtual, then we need all backends to implement this fucntion.


/**
* Responsible for destroying a handle, if it's required for some backend.
* It may be needed for some backends. For example, resources associated with
Expand Down
Loading
Loading