Skip to content

[executorch][native] Add Scalar value type to the in-memory IR - #22276

Open
SS-JIA wants to merge 1 commit into
gh/SS-JIA/613/basefrom
gh/SS-JIA/613/head
Open

[executorch][native] Add Scalar value type to the in-memory IR#22276
SS-JIA wants to merge 1 commit into
gh/SS-JIA/613/basefrom
gh/SS-JIA/613/head

Conversation

@SS-JIA

@SS-JIA SS-JIA commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Add Scalar (backends/native/runtime/graph/Scalar.{h,cpp}), a concrete scalar
value — a std::variant<int64_t, double, bool>, analogous to c10::Scalar:
one alternative per domain (integral, floating point, boolean), each stored at
its widest type, so a narrower value is stored exactly and to<T>() narrows it
back on read.

Construct implicitly from int / int64_t / double / bool; read via strict
accessors (to_int / to_double / to_bool, which throw on a tag mismatch) or
the promoting to<T>() template (a std::visit that static_casts whichever
alternative is live, like c10::Scalar::to<T>()). Plus is_*() predicates. No
"none" state — the graph's Value owns that.

Every pointer converts to bool, so the implicit constructors would otherwise
let Scalar s = some_ptr; compile and quietly produce a Bool. A deleted
Scalar(T*) template rejects that at the call site while leaving the numeric
constructors implicit.

to_string(const Scalar&) joins the IR printer in utils/Print.{h,cpp}. It
renders a double through the shared ptn::format_double
(graph/StringFormat.h) rather than std::to_string, whose fixed six-decimal
format prints 1e-300 as "0.000000" and 1e300 as 312 digits.

The constructors and the is_*() / to<T>() readers are constexpr,
so a Scalar can be built and inspected in a constant-evaluated context.

Pure std — no ExecuTorch and no flatbuffers dependency. Mirrored into both the
fbcode/ and xplat/ trees to match the native backend layout.

Authored with Claude Code.

Differential Revision: D114396762

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 28, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (1 Unrelated Failure)

As of commit cd0319a with merge base a5f15b5 (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@digantdesai digantdesai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

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. meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants