Skip to content
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

GPUAdapter invalidation causes TensorFlow.js WebGPU backend to fail — Follow-up to #22029 #28135

Closed
desaisoftwaree opened this issue Feb 16, 2025 · 8 comments

Comments

@desaisoftwaree
Copy link

Version: Deno 2.1.10
(stable, release, x86_64-pc-windows-msvc)
v8 13.0.245.12-rusty
typescript 5.6.2

Library:
TensorFlow.js 4.22.0 (WebGPU backend)

Description: This is a follow-up to the now-closed issue #22029. The core problem persists: using the TensorFlow.js WebGPU backend under Deno leads to adapter invalidation errors.

When TensorFlow.js attempts to initialize the WebGPU backend, it eventually tries to re-check the same GPUAdapter (after a device is created). Deno immediately invalidates a GPUAdapter after the first device creation, causing:
deno run --allow-net=cdn.jsdelivr.net main.js
Initialization of backend webgpu failed
TypeError: The adapter cannot be reused, as a device creation has invalidated it
at GPUAdapter.get info (ext:deno_webgpu/01_webgpu.js:525:13)
at Object.factory (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-webgpu@4.22.0/+esm:170:749)
at async Vs.ready (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core@4.22.0/+esm:186:1575)
at async file:///D:/Dev/deno-web-gpu/main.js:4:1
error: Uncaught (in promise) Error: Could not initialize any backends, all backend initializations failed.
at Vs.ready (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core@4.22.0/+esm:186:1658)
at async file:///D:/Dev/deno-web-gpu/main.js:4:1

Steps to Reproduce:

  1. Create a simple Deno project with this structure:

deno.json

{
  "importMap": "import_map.json"
}

import_map.json

{
  "imports": {
    "@tensorflow/tfjs-core": "https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core@4.22.0/+esm",
    "@tensorflow/tfjs-backend-webgpu": "https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-webgpu@4.22.0/+esm"
  }
}
  1. main.js
import * as tf from "@tensorflow/tfjs-core";
import "@tensorflow/tfjs-backend-webgpu";

await tf.ready();
tf.randomGamma([2, 2], 1).print();
  1. Run:
deno run --allow-net=cdn.jsdelivr.net main.js
  1. Observe the error about the adapter not being reusable after device creation.
    Expected Behavior: In browsers (e.g., Chrome, Edge), the WebGPU adapter is not invalidated right after device creation. TensorFlow.js’s WebGPU backend can repeatedly query or reuse the adapter without failing. Ideally, Deno would match the browser’s behaviour closely enough that TF.js does not crash on startup.

Actual Behavior: Deno invalidates the GPUAdapter as soon as a device is created. TensorFlow.js tries to re-check or reuse the adapter, triggering a TypeError: The adapter cannot be reused, as a device creation has invalidated it.

Why This Matters:

  • TensorFlow.js (and potentially other libraries) rely on creating or querying the adapter multiple times, mirroring how browsers handle WebGPU.
  • Deno’s strict adapter invalidation policy disrupts these libraries, making the WebGPU backend unusable.

Additional Context:

  • The originally closed Deno issue is #22029. That issue was closed, but the underlying limitation remains a blocker for TF.js WebGPU usage under Deno.

Possible Approaches:

  • Align Deno’s WebGPU adapter lifecycle closer to the browser behaviour so that libraries like TensorFlow.js can function without changes.

  • Provide a configuration or override that allows multiple device creations or repeated adapter queries.

Thank you for revisiting this. Looking forward to any updates or clarifications on whether Deno can (or will) change its WebGPU behaviour, or if library developers need to implement a workaround to accommodate Deno’s stricter handling.

@crowlKats
Copy link
Member

Fixed in #27665

@desaisoftwaree
Copy link
Author

Fixed in #27665

Can you please provide a link or any guide on how to implement it?

@crowlKats
Copy link
Member

@desaisoftwaree not sure I understand what you are asking for

@desaisoftwaree
Copy link
Author

@desaisoftwaree not sure I understand what you are asking for

I am not able to understand why is this issue closed immediately. It would be great if you can provide any hints.

@petamoriken
Copy link
Contributor

I believe this issue has been already resolved and is not yet released so you can try deno upgrade --canary.

@desaisoftwaree
Copy link
Author

I believe this issue has been already resolved and is not yet released so you can try deno upgrade --canary.

I upgraded to canary but the same error persists.

@petamoriken
Copy link
Contributor

Observe the error about the adapter not being reusable after device creation.
Expected Behavior: In browsers (e.g., Chrome, Edge), the WebGPU adapter is not invalidated right after device creation. TensorFlow.js’s WebGPU backend can repeatedly query or reuse the adapter without failing. Ideally, Deno would match the browser’s behaviour closely enough that TF.js does not crash on startup.

My understanding is that there is a divergence between Chrome's implementation and wgpu (used by Firefox and Deno) due to changes in the WebGPU spec. Perhaps a fix on the TensorFlow.js side might be appropriate.
#25874 (comment)

@crowlKats What do you think?

@desaisoftwaree
Copy link
Author

You are correct

There is a divergence between Chrome's implementation and wgpu (used by Firefox and Deno) due to changes in the WebGPU spec
I tried running tensorflow js on Firefox and enabled the webgpu from about:config but errors are thrown. Also, WebGPU for Firefox is not yet complete

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

No branches or pull requests

3 participants