Add int64_t support to BackendOptions for pointer-sized handles (#19232)#19232
Add int64_t support to BackendOptions for pointer-sized handles (#19232)#19232shoumikhin wants to merge 1 commit intopytorch:mainfrom
Conversation
|
@shoumikhin has exported this pull request. If you are a Meta employee, you can view the originating Diff in D103241865. |
This PR needs a
|
There was a problem hiding this comment.
Pull request overview
Adds int64_t as a supported runtime backend option type so backends can receive pointer-sized opaque handles via runtime_specs at Method::load_method() time.
Changes:
- Extend
OptionValueto include anint64_talternative and add a matchingBackendOptions::set_option(..., int64_t)overload. - Extend
BackendInitContext::get_runtime_spec<T>to allowT = int64_t. - Add a unit test covering
BackendOptionsint64_tset/get behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| runtime/backend/options.h | Adds int64_t to OptionValue and a new set_option overload; updates docs. |
| runtime/backend/backend_init_context.h | Allows get_runtime_spec<int64_t> and adds <cstdint>. |
| runtime/backend/test/backend_options_test.cpp | Adds int64_t BackendOptions test coverage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…dles (pytorch#19232) Summary: Add an `int64_t` alternative to the `OptionValue` variant in `executorch/runtime/backend/options.h`, plus a matching `set_option(int64_t)` overload on `BackendOptions` and an extended `get_runtime_spec<int64_t>` template in `BackendInitContext`. ## Motivation Backends increasingly need to receive pointer-sized opaque handles at `Method::load_method()` time via the existing `runtime_specs` channel: - NVIDIA CUDA driver handles (e.g., `CUgreenCtx` for green-context-aware TensorRT inference, where the runtime user pre-creates a green context and binds the engine's stream to it via `cuGreenCtxStreamCreate`). - Externally-created CUDA streams shared across delegates. - Vulkan/Metal command queue handles, etc. The current `OptionValue` variant supports `bool`, `int`, and a 256-byte char array. None of these safely round-trips a 64-bit pointer on LP64 platforms — `int` is 32-bit and would silently truncate the upper half of a pointer, producing use-after-free or segfaults at the consumer side. A workaround using the `const char*` arm (stringify the pointer to hex, parse with `strtoull` in the backend) is possible but ugly, error-prone, and unnecessary given how trivial the additive fix is. ## Scope of change - Add `int64_t` to the `OptionValue` variant alternatives. - Add a `set_option(const char (&key)[N], int64_t value)` overload on `BackendOptions`. - Extend `BackendInitContext::get_runtime_spec<T>` template's `static_assert` to allow `T = int64_t` in addition to `bool`, `int`, `const char*`. The internal `std::get_if<T>` already works with the new variant alternative — no other body changes needed. - Add `<cstdint>` include in both headers. ## Properties This change is purely additive: - **Existing consumers see no behavior change.** The `bool`, `int`, and `const char*` paths are untouched. Variant alternatives are appended, not reordered, so `std::variant::index()` semantics for existing values are preserved. - **No serialized format changes.** `runtime_specs` are passed in-process at `Method::load_method` and are never serialized into the .pte file. AOT `CompileSpec` lives in a separate Python schema (flatbuffer-backed) and is unchanged here. - **No public API removed or renamed.** The new `int64_t` overload is selected only when callers explicitly pass `int64_t`-typed values; integer literals continue to bind to the `int` overload. ## Caller usage The intended usage pattern, illustrated for the green-context case: // 1) User creates the opaque resource and owns its lifetime CUgreenCtx green = nullptr; cuGreenCtxCreate(&green, desc, device, CU_GREEN_CTX_DEFAULT_STREAM); // 2) Pass as a runtime spec at Method::load_method time BackendOption opt{}; std::strcpy(opt.key, "green_context"); opt.value = static_cast<int64_t>(reinterpret_cast<intptr_t>(green)); Span<const BackendOption> specs(&opt, 1); // ... thread `specs` through Module::load_program / load_method // 3) In the backend's init(): auto h = context.get_runtime_spec<int64_t>("green_context"); if (h.ok()) { auto green = reinterpret_cast<CUgreenCtx>(h.get()); CUstream raw_stream; cuGreenCtxStreamCreate( &raw_stream, green, CU_STREAM_NON_BLOCKING, /*priority=*/0); // bind raw_stream to the engine for enqueueV3... } Differential Revision: D103241865
fc70db4 to
6ab86f1
Compare
6ab86f1 to
11a3f4c
Compare
There was a problem hiding this comment.
Pull request overview
Adds int64_t support to backend option plumbing so runtimes/backends can pass and retrieve pointer-sized opaque handles (and other 64-bit values) with strict type checking.
Changes:
- Extend
BackendOptions/BackendInitContext::get_runtime_spec<T>to supportint64_tvalues in the underlyingstd::variant. - Update API documentation around string storage semantics and supported option types.
- Add unit tests covering
int64_tset/get behavior and type-mismatch errors.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| runtime/backend/options.h | Adds int64_t to OptionValue, adds set_option(..., int64_t), updates docs for supported types and string handling |
| runtime/backend/backend_init_context.h | Extends get_runtime_spec<T> static_assert and retrieval logic to allow int64_t |
| runtime/backend/test/backend_options_test.cpp | Adds int64_t round-trip and type-mismatch tests for BackendOptions |
| runtime/backend/test/backend_init_context_test.cpp | Adds get_runtime_spec<int64_t> tests and expands type-mismatch coverage |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
11a3f4c to
1ab303f
Compare
1ab303f to
b478c32
Compare
…rch#19232) Summary: Add `int64_t` as a new variant arm to `OptionValue` in `BackendOptions` and extend `BackendInitContext::get_runtime_spec<T>` to support it. Lets backends receive pointer-sized opaque handles (e.g., `cudaStream_t`, `CUgreenCtx`) as runtime specs at delegate `init()` time. Motivation: GPU delegates today have no public surface to receive caller-owned device resources, so they default to allocating their own streams from the device's primary-context stream pool. This silently bypasses caller-set CUDA Green Contexts, which is exactly the gap MRS's MLRT runtime hits when partitioning a Jetson Thor across concurrent models. With this change, callers that own a GPU context can pass a green-ctx-bound stream into a Module's load path via `BackendOptions`, and GPU delegates that opt in (TensorRT, CUDA, future Vulkan / Metal) honor it instead of allocating their own. Fully additive: - New `int64_t` variant arm + matching `set_option(int64_t)` overload. - `get_option<int64_t>` falls through the existing `bool/int` path. - `BackendInitContext::get_runtime_spec<int64_t>` extends the existing `static_assert`. - No behavior change for existing `bool` / `int` / `const char*` callers. - No public API removal or signature change. Documentation: - The int64_t arm docstring spells out the canonical `uintptr_t` round-trip pattern so callers don't bikeshed the cast — raw `reinterpret_cast<int64_t>(ptr)` is implementation-defined per the standard; via `uintptr_t` it is well-defined on every platform. - Calls out the literal-overload-resolution gotcha (`42` → `int`, `42L` → platform-dependent) and recommends `static_cast<int64_t>` at the call site. Defensive scaffolding: - `static_assert(std::variant_size_v<OptionValue> == 4, ...)` in `set_option_impl` guards against future variant arm additions silently breaking call sites. - Companion `static_assert` constrains the impl template `T` to the four valid variant arms so accidental misuse fails at compile time. Differential Revision: D103241865
b478c32 to
305af49
Compare
There was a problem hiding this comment.
Pull request overview
Adds int64_t as a supported BackendOptions value type so backends can receive pointer-sized opaque handles (e.g., CUDA streams/contexts) via runtime specs at delegate init time.
Changes:
- Extend
OptionValueto include anint64_tvariant arm and add aset_option(..., int64_t)overload. - Extend
BackendInitContext::get_runtime_spec<T>to allowT = int64_t. - Add unit tests covering
int64_toption set/get and type-mismatch behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| runtime/backend/options.h | Adds int64_t option support and updates option documentation/validation scaffolding. |
| runtime/backend/backend_init_context.h | Allows retrieving int64_t runtime specs via get_runtime_spec<int64_t>(). |
| runtime/backend/test/backend_options_test.cpp | Adds tests verifying int64_t options round-trip and type mismatch with int. |
| runtime/backend/test/backend_init_context_test.cpp | Adds tests for get_runtime_spec<int64_t>() and mismatch behavior between int and int64_t. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * arm allows callers to pass pointer-sized opaque handles (e.g., driver | ||
| * handles like CUgreenCtx) by reinterpret_cast to/from int64_t. |
|
it looks good to me, thanks @shoumikhin ! |
|
Adding an int64_t to backend options seems fine to me, but not sure how I feel about the intended use-case of passing in caller-owned device resources. Seems a bit hacky. Maybe @JacobSzwejbka has thoughts there, I know he had some concerns about people overusing backend options. |
Summary:
Add
int64_tas a new variant arm toOptionValueinBackendOptionsand extend
BackendInitContext::get_runtime_spec<T>to support it. Letsbackends receive pointer-sized opaque handles (e.g.,
cudaStream_t,CUgreenCtx) as runtime specs at delegateinit()time.Motivation: GPU delegates today have no public surface to receive
caller-owned device resources, so they default to allocating their own
streams from the device's primary-context stream pool. This silently
bypasses caller-set CUDA Green Contexts, which is exactly the gap MRS's
MLRT runtime hits when partitioning a Jetson Thor across concurrent
models. With this change, callers that own a GPU context can pass a
green-ctx-bound stream into a Module's load path via
BackendOptions,and GPU delegates that opt in (TensorRT, CUDA, future Vulkan / Metal)
honor it instead of allocating their own.
Fully additive:
int64_tvariant arm + matchingset_option(int64_t)overload.get_option<int64_t>falls through the existingbool/intpath.BackendInitContext::get_runtime_spec<int64_t>extends the existingstatic_assert.bool/int/const char*callers.Documentation:
uintptr_tround-trip pattern so callers don't bikeshed the cast — raw
reinterpret_cast<int64_t>(ptr)is implementation-defined per thestandard; via
uintptr_tit is well-defined on every platform.42→int,42L→ platform-dependent) and recommendsstatic_cast<int64_t>atthe call site.
Defensive scaffolding:
static_assert(std::variant_size_v<OptionValue> == 4, ...)inset_option_implguards against future variant arm additions silentlybreaking call sites.
static_assertconstrains the impl templateTto the fourvalid variant arms so accidental misuse fails at compile time.
Differential Revision: D103241865