Skip to content

[1/N] Add backend option #11288

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

Closed
wants to merge 1 commit into from
Closed

Conversation

cccclai
Copy link
Contributor

@cccclai cccclai commented Jun 2, 2025

Summary: Introduce backend option as discussed in #10216

Differential Revision: D75770142

Step 1: Introducd Backend Option class

In later stage, it will be plugged in with the rest of the stack.

Copy link

pytorch-bot bot commented Jun 2, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/11288

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit e91888c with merge base 8e2737c (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 2, 2025
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D75770142

@cccclai cccclai changed the title Add backend option [1/N] Add backend option Jun 2, 2025
@cccclai cccclai force-pushed the export-D75770142 branch from ca30f97 to 359364a Compare June 2, 2025 18:31
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D75770142

@cccclai cccclai added the release notes: api Changes to public facing apis (any interfaces, pybinded runtime methods, etc.) label Jun 2, 2025
cccclai added a commit to cccclai/executorch-1 that referenced this pull request Jun 2, 2025
Summary:

Introduce backend option as discussed in pytorch#10216

Step 1: Introducd Backend Option class

In later stage, it will be plugged in with the rest of the stack.

Differential Revision: D75770142
@cccclai cccclai force-pushed the export-D75770142 branch from 359364a to 01bae44 Compare June 2, 2025 18:44
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D75770142

cccclai added a commit to cccclai/executorch-1 that referenced this pull request Jun 2, 2025
Summary:

Introduce backend option as discussed in pytorch#10216

Step 1: Introducd Backend Option class

In later stage, it will be plugged in with the rest of the stack.

Differential Revision: D75770142
@cccclai cccclai force-pushed the export-D75770142 branch from 01bae44 to 1a68abc Compare June 2, 2025 18:51
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D75770142

cccclai added a commit to cccclai/executorch-1 that referenced this pull request Jun 2, 2025
Summary:

Introduce backend option as discussed in pytorch#10216

Step 1: Introducd Backend Option class

In later stage, it will be plugged in with the rest of the stack.

Differential Revision: D75770142
@cccclai cccclai force-pushed the export-D75770142 branch from 1a68abc to fbdb2b5 Compare June 2, 2025 18:53
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D75770142

cccclai added a commit to cccclai/executorch-1 that referenced this pull request Jun 2, 2025
Summary:

Introduce backend option as discussed in pytorch#10216

Step 1: Introducd Backend Option class

In later stage, it will be plugged in with the rest of the stack.

Differential Revision: D75770142
@cccclai cccclai force-pushed the export-D75770142 branch from fbdb2b5 to 7be9c3a Compare June 2, 2025 19:07
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D75770142

cccclai added a commit to cccclai/executorch-1 that referenced this pull request Jun 2, 2025
Summary:

Introduce backend option as discussed in pytorch#10216

Step 1: Introducd Backend Option class

In later stage, it will be plugged in with the rest of the stack.

Differential Revision: D75770142
@cccclai cccclai force-pushed the export-D75770142 branch from 7be9c3a to 2d0c08c Compare June 2, 2025 19:36
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D75770142

cccclai added a commit to cccclai/executorch-1 that referenced this pull request Jun 2, 2025
Summary:

Introduce backend option as discussed in pytorch#10216

Step 1: Introducd Backend Option class

In later stage, it will be plugged in with the rest of the stack.

Differential Revision: D75770142
@cccclai cccclai force-pushed the export-D75770142 branch from 2d0c08c to d3a4574 Compare June 2, 2025 19:57
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D75770142

cccclai added a commit to cccclai/executorch-1 that referenced this pull request Jun 2, 2025
Summary:

Introduce backend option as discussed in pytorch#10216

Step 1: Introducd Backend Option class

In later stage, it will be plugged in with the rest of the stack.

Differential Revision: D75770142
@cccclai cccclai force-pushed the export-D75770142 branch from d3a4574 to 7d42f0e Compare June 2, 2025 21:33
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D75770142

Summary:

Introduce backend option as discussed in pytorch#10216

Step 1: Introducd Backend Option class

In later stage, it will be plugged in with the rest of the stack.

Differential Revision: D75770142
@cccclai cccclai force-pushed the export-D75770142 branch from 7d42f0e to e91888c Compare June 2, 2025 22:09
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D75770142

@JacobSzwejbka
Copy link
Contributor

Will review tomorrow

// Union-like container for option values (only one member is valid per option)
struct OptionValue {
bool bool_value; // Storage for boolean values
int int_value; // Storage for integer values
Copy link
Contributor

Choose a reason for hiding this comment

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

int64_t

@mergennachin
Copy link
Contributor

mergennachin commented Jun 4, 2025

I don't think we need to define a generic BackendOption that takes arbitrary key value. Each backend owners could define their options instead. Vulkan will define its own config structure, Core ML will define its own config structure. Wdyt?

@JacobSzwejbka, @mcr229, @kimishpatel

Something like this:

BackendInterface { // parent interface
  virtual Error setOption(const BackendOptions& options) = 0;
}

struct BackendOptions {  // Empty parent struct
  virtual ~BackendOptions() = default;
};

Each individual backends define their own configs

struct VulkanBackendOptions : public BackendOptions {
  const char* metadata[20] = "";
  int thread_count = 1;
  // ... other Vulkan-specific options
};

VulkanBackend extends BackendInterface {
  Error setOption(const BackendOptions& options) override {
    auto* vulkan_opts = dynamic_cast<const VulkanBackendOptions*>(&options);
  }
}

// User code

VulkanBackendOptions opts;
opts.thread_count = 5;
auto backend = std::make_unique<VulkanBackend>();
backend->setOption(opts);  // 

@cccclai
Copy link
Contributor Author

cccclai commented Jun 4, 2025

I don't think we need to define a generic BackendOption that takes arbitrary key value. Each backend owners could define their options instead. Vulkan will define its own config structure, Core ML will define its own config structure. Wdyt?

@JacobSzwejbka, @mcr229, @kimishpatel

Something like this:

BackendInterface { // parent interface
  virtual Error setOption(const BackendOptions& options) = 0;
}

struct BackendOptions {  // Empty parent struct
  virtual ~BackendOptions() = default;
};

Each individual backends define their own configs

struct VulkanBackendOptions : public BackendOptions {
  const char* metadata[20] = "";
  int thread_count = 1;
  // ... other Vulkan-specific options
};
VulkanBackend extends BackendInterface {
  Error setOption(const BackendOptions& options) override {
    auto* vulkan_opts = dynamic_cast<const VulkanBackendOptions*>(&options);
  }
}

// User code

VulkanBackendOptions opts;
opts.thread_count = 5;
auto backend = std::make_unique<VulkanBackend>();
backend->setOption(opts);  // 

I thought about this idea at the beginning, but it means that users code needs to link to a specific backend, otherwise the runner code will fail to build. Currently the same user code can be backend agnostic, meaning the same code can still run with different backends with/without linking the backend. As the example, the portable runner can work with/without xnnpack https://github.com/pytorch/executorch/blob/main/examples/xnnpack/executor_runner/targets.bzl.

It also exposes the backend specific symbol to users, and the contract interface is a bit looser. Previously, The contract is like backend <-> ET <-> backend, and if we change it to this, it can be user <-> backend. And users can pass arbitrary things to backend. Users can run backend->init, backend->execute in their own code too, and it will be out of our control.

With the proposed solution (#10216), the user code can still be backend agnostic, and the backend option is still optional. We are still on the hook of the contract.

@cccclai
Copy link
Contributor Author

cccclai commented Jun 4, 2025

Btw I was thinking using ghstack such that I can export a stack of PRs...but it means this PR will be closed and a new PR will be created. I will copy the comments over just so we keep the history.

Edit: the new pr is here #11389, I'm closing this PR and please leave comments in the new PR

enum class OptionType { BOOL, INT, STRING };

// Union-like container for option values (only one member is valid per option)
struct OptionValue {
Copy link
Contributor

@digantdesai digantdesai Jun 5, 2025

Choose a reason for hiding this comment

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

Why not use a union? Or structure with a template?

Copy link
Contributor

Choose a reason for hiding this comment

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

or std::variant that can hold only bool, int and char *?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

union can be a good option, let me try

Copy link
Contributor

@kimishpatel kimishpatel left a comment

Choose a reason for hiding this comment

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

Left a few comments

#include <cstring>

namespace executorch {
namespace ET_RUNTIME_NAMESPACE {
Copy link
Contributor

Choose a reason for hiding this comment

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

why this capitalized?

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 used across the whole ExecuTorch stack, but I find now it's not needed so switch to executorch::runtime namespace

};

// Supported option data types
enum class OptionType { BOOL, INT, STRING };
Copy link
Contributor

Choose a reason for hiding this comment

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

Just use std::varaint


// Represents a single backend configuration option
struct BackendOption {
const char* key; // Name of the option
Copy link
Contributor

Choose a reason for hiding this comment

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

you can also do

struct BackendOptions {
  const char* key;
  std::variant<int, bool, char *> value;
}

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 will introduce std library I assume?

/// Sets or updates a boolean option
/// @param key: Typed option key
/// @param value: Boolean value to set
void set_option(OptionKey<bool> key, bool value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just make this set_option<T>(BackendOption<T> key_value)...

If tomorrow you expand BackendOption to have other types like float or int8 you just have to update allowed types in BackendOpton.

Copy link
Contributor

Choose a reason for hiding this comment

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

and assert to on allowable types

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BackendOptions is a list of BackendOption, and each BackendOption can be different type (Like cpu: {thread: 2, profile: true}). If we make BackendOption a template, then BackendOptions can only hold the same type of BackendOption, which is not we would like.

enum class OptionType { BOOL, INT, STRING };

// Union-like container for option values (only one member is valid per option)
struct OptionValue {
Copy link
Contributor

Choose a reason for hiding this comment

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

or std::variant that can hold only bool, int and char *?


private:
BackendOption options[MaxCapacity]{}; // Storage for options
size_t size; // Current number of options
Copy link
Contributor

Choose a reason for hiding this comment

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

size_ for private variables.

Comment on lines +134 to +137
executorch::runtime::Error get_option_internal(
const char* key,
OptionType expected_type,
OptionValue& out) const {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it necessary that type is specified? If we use std::variant we can just make this get_optional_internal(const char* key, BackendOption& option); User can be responsible for type check. Not sure why we need to provide it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

std::variant is part of the std library, and this code needs to stay in core

@cccclai
Copy link
Contributor Author

cccclai commented Jun 6, 2025

Address comments in #11389, please take a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported release notes: api Changes to public facing apis (any interfaces, pybinded runtime methods, etc.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants