This is an automated email from the ASF dual-hosted git repository.

akaashrp pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new 06a3626f72 [Web] Expose nonblocking WebGPU command submission (#21005)
06a3626f72 is described below

commit 06a3626f7274f9245dd11e55b9cccafcbd3221f7
Author: Akaash Parthasarathy <[email protected]>
AuthorDate: Wed Nov 18 12:03:11 2026 -0800

    [Web] Expose nonblocking WebGPU command submission (#21005)
    
    Expose `DLDevice.submitPendingCommands()` to submit queued WebGPU work
    without waiting for GPU completion. Return whether a command buffer was
    submitted.
---
 web/src/runtime.ts                    | 15 +++++++++++++++
 web/src/webgpu.ts                     | 13 +++++++++++++
 web/tests/node/test_webgpu.js         | 19 +++++++++++++++++++
 web/tests/node/test_webgpu_runtime.js | 31 +++++++++++++++++++++++++++++++
 4 files changed, 78 insertions(+)

diff --git a/web/src/runtime.ts b/web/src/runtime.ts
index 906306db9c..e57e9ddbcf 100644
--- a/web/src/runtime.ts
+++ b/web/src/runtime.ts
@@ -386,6 +386,21 @@ export class DLDevice {
     }
   }
 
+  /**
+   * Submit pending WebGPU commands without waiting for completion.
+   *
+   * @returns Whether a command buffer was submitted.
+   */
+  submitPendingCommands(): boolean {
+    if (this.deviceType !== DeviceStrToEnum.webgpu) {
+      throw new Error(
+        "Nonblocking command submission is only available on WebGPU devices.",
+      );
+    }
+    assert(this.lib.webGPUContext !== undefined);
+    return this.lib.webGPUContext.submitPendingCommands();
+  }
+
   toString(): string {
     return (
       DeviceEnumToStr[this.deviceType] + ":" + this.deviceId.toString()
diff --git a/web/src/webgpu.ts b/web/src/webgpu.ts
index 038f9accca..2f1e8de755 100644
--- a/web/src/webgpu.ts
+++ b/web/src/webgpu.ts
@@ -489,6 +489,19 @@ export class WebGPUContext {
     }
   }
 
+  /**
+   * Submit pending commands without waiting for GPU completion.
+   *
+   * @returns Whether a command buffer was submitted.
+   */
+  submitPendingCommands(): boolean {
+    if (!this.pendingEncoder) {
+      return false;
+    }
+    this.flushCommands();
+    return true;
+  }
+
   /**
    * Dispose context.
    */
diff --git a/web/tests/node/test_webgpu.js b/web/tests/node/test_webgpu.js
index 30f011d7e1..6f15d1be37 100644
--- a/web/tests/node/test_webgpu.js
+++ b/web/tests/node/test_webgpu.js
@@ -162,6 +162,25 @@ test("compute dispatches and GPU copies share one 
submission", async () => {
   expect(queue.submit).toHaveBeenCalledTimes(1);
 });
 
+test("pending WebGPU commands can be submitted without waiting", () => {
+  const { context, queue } = createContext();
+  const shader = context.createShader(
+    {
+      name: "main",
+      arg_types: [],
+      launch_param_tags: [],
+    },
+    "@compute @workgroup_size(1) fn main() {}"
+  );
+  shader();
+  expect(context.submitPendingCommands()).toBe(true);
+  expect(queue.submit).toHaveBeenCalledTimes(1);
+  expect(queue.onSubmittedWorkDone).not.toHaveBeenCalled();
+
+  expect(context.submitPendingCommands()).toBe(false);
+  expect(queue.submit).toHaveBeenCalledTimes(1);
+});
+
 test("a host write flushes pending GPU copies before writeBuffer", () => {
   const { context, queue, events, source, destination } = createContext();
   const copyWithinGPU = context.getDeviceAPI("deviceCopyWithinGPU");
diff --git a/web/tests/node/test_webgpu_runtime.js 
b/web/tests/node/test_webgpu_runtime.js
new file mode 100644
index 0000000000..eb4e838493
--- /dev/null
+++ b/web/tests/node/test_webgpu_runtime.js
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+jest.mock("../../src/compact", () => ({}));
+const { DLDevice } = require("../../src/runtime");
+
+test("public nonblocking submission delegates only for WebGPU devices", () => {
+  const context = { submitPendingCommands: 
jest.fn().mockReturnValueOnce(true).mockReturnValueOnce(false) };
+  const lib = { webGPUContext: context };
+  const gpu = new DLDevice("webgpu", 0, lib);
+  expect(gpu.submitPendingCommands()).toBe(true);
+  expect(gpu.submitPendingCommands()).toBe(false);
+  expect(context.submitPendingCommands).toHaveBeenCalledTimes(2);
+  expect(() => new DLDevice("cpu", 0, 
lib).submitPendingCommands()).toThrow("only available on WebGPU");
+  expect(context.submitPendingCommands).toHaveBeenCalledTimes(2);
+});

Reply via email to