Skip to content

WebGPU: Support int64 for Reshape - #29830

Merged
hariharans29 merged 3 commits into
microsoft:mainfrom
huningxin:copilot/steering-update-reshape-int64-support
Jul 23, 2026
Merged

WebGPU: Support int64 for Reshape#29830
hariharans29 merged 3 commits into
microsoft:mainfrom
huningxin:copilot/steering-update-reshape-int64-support

Conversation

@huningxin

Copy link
Copy Markdown
Contributor

Description

This updates WebGPU Reshape to follow the existing enable_int64 factory-registration pattern (used by Unsqueeze/Expand/etc.) instead of static macro registration, and adds explicit int64 WebGPU test coverage for Reshape.

Motivation and Context

PR microsoft/onnxruntime#27478 added int64 support for the Expand and Unsqueeze (Squeeze) operators in the WebGPU EP. However, the WebNN EP maps a number of those shape-manipulation operators — including Squeeze/Unsqueeze — onto Reshape (WebNN spec doesn't support Squeeze/Unsqueeze). As a result, when a model uses int64 data through these WebNN-mapped ops, execution falls back because Reshape itself does not yet accept int64 tensors, undermining the int64 coverage that #27478 was intended to enable.

Fix #29756

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@huningxin

Copy link
Copy Markdown
Contributor Author

/cc @adrastogi

@huningxin

Copy link
Copy Markdown
Contributor Author

@hariharans29, PTAL, thanks!

@hariharans29

Copy link
Copy Markdown
Member

Review: PR #29830 — WebGPU: Support int64 for Reshape (head dbe35c5)

Author: @huningxin (Copilot SWE-agent initial commit, author's second commit polished to the factory pattern). 2 commits. CI: 1/1 checks OK so far (Azure Pipelines needs /azp run to expand; @hariharans29 pinged). Fixes #29756, same issue umbrella as PR #29828.

Verdict: LGTM — approve. Mechanical, well-scoped refactor that mirrors the existing conditional-int64 pattern (Expand, Equal, Unsqueeze) exactly. No blockers.


What it does

Converts WebGPU Reshape from 7 static ONNX_OPERATOR_..._KERNEL_EX macro registrations to 7 factory calls CreateReshapeVersionedKernelInfo<Start, End>(enable_int64) / CreateReshapeKernelInfo<Since>(enable_int64), so that when the webgpu::options::kEnableInt64 runtime option is set, Reshape's T type constraint additionally accepts int64. Motivation is genuinely load-bearing: the WebNN EP maps Squeeze/Unsqueeze onto Reshape internally (WebNN spec lacks Squeeze/Unsqueeze), so any int64 model routed through WebNN Squeeze/Unsqueeze was silently CPU-falling back at Reshape.

Files touched (4):

  • reshape.cc — removes 7 macro registrations, adds two template factories + 7 explicit template instantiations.
  • reshape.h — declares the two template factories.
  • webgpu_execution_provider.cc — removes 7 BuildKernelCreateInfo<...> entries from the static table, adds 7 ORT_THROW_IF_ERROR(kernel_registry->Register(...)) calls in RegisterKernels, adjacent to the sibling Expand / Equal conditional-int64 registrations.
  • tensor_op_test.cc — new #ifdef USE_WEBGPU-guarded Reshape_int64_webgpu test.

Correctness

The int64 safety argument. The header comment nails it:

"Reshape is a pure copy/view op. Enabling int64 is safe because element values are never interpreted or used in shader arithmetic."

Reshape either aliases the input buffer (view semantics via Alias(0, 0)) or does a raw byte-blob copy — the kernel never inspects or arithmetically manipulates element values. So even though WebGPU has no native i64 type and int64 tensors are stored as u32-pair emulation, the shader never touches them. Enabling int64 on T is a documentation-only widening from the kernel's perspective.

Opset ranges preserved. Reshape's ONNX version history is [5,12] / [13,13] / [14,18] / [19,20] / [21,22] / [23,24] / 25+. Each of these seven ranges has both a BuildKernelCreateInfo<...> removal and a matching factory-call addition. Nothing dropped, nothing double-registered. Explicit template instantiations at the bottom of reshape.cc enumerate all seven — matches the 7 registration call sites.

Kernel behavior preserved. Every factory-built KernelDefBuilder:

  • .TypeConstraint("T", GetOpTypeConstraints(enable_int64, true)) — replaces the old WebGpuSupportedNumberTypes(). Same helper Expand and Equal use; the true second arg is the "conditionally-include-int64" opt-in that flips based on enable_int64.
  • .TypeConstraint("shape", int64) — unchanged.
  • .Alias(0, 0)critical, preserved. Reshape's core optimization is the in-place view aliasing. Without this, the EP would materialize a copy on every call and the "pure view" argument above would be violated. Correctly carried through.
  • .InputMemoryType(OrtMemTypeCPU, 1) — shape input on CPU, unchanged.

No orphaned class declarations. The removed ONNX_OPERATOR_VERSIONED_KERNEL_EX macros in reshape.cc were what generated the class symbols referenced by BuildKernelCreateInfo<class ONNX_OPERATOR_VERSIONED_KERNEL_CLASS_NAME(...)> in the provider. Both are removed in lockstep — no dangling symbol references. CI-green (1/1) is consistent with this.

Default behavior unchanged. When enable_int64 = false (default), GetOpTypeConstraints(false, true) presumably returns the same set as the old WebGpuSupportedNumberTypes() — Expand and Equal use the exact same call and preserve their pre-PR default type sets. Non-int64 Reshape sees no behavior change.


Test

New Reshape_int64_webgpu in tensor_op_test.cc:

OpTester test("Reshape", 14);
test.AddInput<int64_t>("data", {2, 3}, {1, 2, 3, 4, 5, 6});
test.AddInput<int64_t>("shape", {3}, {3, 1, 2});
test.AddOutput<int64_t>("reshaped", {3, 1, 2}, {1, 2, 3, 4, 5, 6});
ConfigOptions config_options{};
ASSERT_STATUS_OK(config_options.AddConfigEntry(webgpu::options::kEnableInt64, "1"));
auto provider = WebGpuExecutionProviderWithOptions(config_options);
test.ConfigEp(std::move(provider)).RunWithConfig();
  • Single shape, single dtype — sufficient because the point under test is int64 T typing, not the shape-inference or aliasing logic (both covered by pre-existing Reshape tests).
  • Explicit kEnableInt64="1" config entry — exercises the opt-in path. Confirms the registration wiring works.
  • #ifdef USE_WEBGPU guard — correct; skipped when WebGPU EP isn't in the build.
  • Opset 14 — reasonable modern choice. If reviewers want belt-and-suspenders coverage across the 5–25 opset span, one test per range would be overkill; a single opset test is standard for a dtype widening.

Minor observations (not blockers)

  1. Bool tensors are still not supported for WebGPU Reshape. ONNX Reshape supports "T = all tensor types" including bool, but the old WebGpuSupportedNumberTypes() excluded bool and the new GetOpTypeConstraints(enable_int64, true) presumably does the same. Pre-PR behavior preserved. Not this PR's problem.
  2. String tensors not supported either. Same rationale — WebGPU doesn't handle strings. N/A.
  3. 7 explicit template instantiations at the bottom of reshape.cc — one per opset range. Tedious but standard C++ for split-declaration-and-instantiation. If more opset ranges are added later, the pattern is easy to extend by adding one line here plus one ORT_THROW_IF_ERROR(kernel_registry->Register(...)) in webgpu_execution_provider.cc. Fine.
  4. The test uses WebGpuExecutionProviderWithOptions helper — consistent with how sibling int64-opt-in tests (Expand, Equal) presumably wire up. Good.
  5. CI: only 1/1 checks visible. The full check suite typically fires after /azp run from an authorized commenter. That gate hasn't fired here yet — expect the full pipeline to run once @hariharans29 (or similar) triggers it.

Bottom line

Approve. The refactor mirrors the well-established Expand/Equal/Unsqueeze conditional-int64 pattern exactly, the safety argument for int64 in a copy/view op is correctly documented, all seven opset ranges are moved atomically, the Alias(0, 0) critical-path is preserved, and the new test exercises the opt-in registration end-to-end. Merge once a maintainer approves and the full CI pipeline is triggered and comes back green.

Copilot AI 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.

Pull request overview

This PR extends the WebGPU Execution Provider’s Reshape kernel registration to support conditional int64 enablement (matching the existing enable_int64 factory-registration pattern used by other WebGPU tensor shape ops), and adds an explicit WebGPU int64 test for Reshape to prevent CPU fallback in WebNN-mapped graphs.

Changes:

  • Replace static macro-based Reshape kernel registration with CreateReshape*KernelInfo(enable_int64) factory registration.
  • Add WebGPU Reshape int64 test coverage gated behind USE_WEBGPU and ep.webgpuexecutionprovider.enableInt64.
  • Add Reshape kernel-info factory declarations to the WebGPU reshape header and implement them in reshape.cc.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
onnxruntime/test/providers/cpu/tensor/tensor_op_test.cc Adds a WebGPU-only int64 Reshape test using the WebGPU provider option to enable int64.
onnxruntime/core/providers/webgpu/webgpu_execution_provider.cc Moves Reshape registration to the conditional enable_int64 registration section and removes static registration entries.
onnxruntime/core/providers/webgpu/tensor/reshape.h Declares CreateReshape*KernelInfo(enable_int64) factory helpers.
onnxruntime/core/providers/webgpu/tensor/reshape.cc Implements Reshape kernel-info factories and explicit template instantiations to support conditional int64 constraints.

Comment thread onnxruntime/test/providers/cpu/tensor/tensor_op_test.cc Outdated
@xhcao

xhcao commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

LGTM, thanks

@hariharans29

Copy link
Copy Markdown
Member

Can you please address the copilot comment ?

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@huningxin

Copy link
Copy Markdown
Contributor Author

Can you please address the copilot comment ?

Yes, addressed. @hariharans29, please take another look, thanks!

@hariharans29
hariharans29 enabled auto-merge (squash) July 23, 2026 20:49
@hariharans29
hariharans29 merged commit ad3a745 into microsoft:main Jul 23, 2026
92 of 99 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Microsoft WebNN demo applications cannot successfully run in ORT-WebGPU backend with CPU fallback disabled

5 participants