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

WebGPU backend fails on Deno with “The adapter cannot be reused, as it has been invalidated by a device creation #8518

Open
desaisoftwaree opened this issue Feb 16, 2025 · 0 comments
Assignees
Labels
type:bug Something isn't working

Comments

@desaisoftwaree
Copy link

Environment:

TensorFlow.js version: 4.22.0 (via CDN)
Backend: @tensorflow/tfjs-backend-webgpu
Deno version: 2.1.10
OS/Platform: Windows 11 x64

Issue Description:
I am attempting to use the TensorFlow.js WebGPU backend under Deno, and it fails during initialization with the following error:

deno run --allow-net=cdn.jsdelivr.net main.js

Initialization of backend webgpu failed
TypeError: The adapter cannot be reused, as it has been invalidated by a device creation
at GPUAdapter.get info (ext:deno_webgpu/01_webgpu.js:525:13)
at Object.factory (https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/+esm:170:749)
at async Vs.ready (https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/+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/[email protected]/+esm:186:1658)
at async file:///D:/Dev/deno-web-gpu/main.js:4:1

Subsequently, TensorFlow.js reports that it could not initialize any backends.

From investigating, it appears Deno’s WebGPU implementation disallows reusing the same GPUAdapter object after a device has been created; the adapter becomes “invalidated.” Meanwhile, TensorFlow.js’s WebGPU backend tries to re-query or re-initialize the adapter in a way that triggers this problem.

Steps to Reproduce:

  1. Deno project setup:

deno.json

{
  "importMap": "import_map.json"
}

import_map.json

{
  "imports": {
    "@tensorflow/tfjs-core": "https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/+esm",
    "@tensorflow/tfjs-backend-webgpu": "https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/+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 in Deno with network access:
deno run --allow-net=cdn.jsdelivr.net main.js

Expected Behavior:

  • TensorFlow.js successfully initializes the WebGPU backend, and a 2x2 tf.randomGamma output is printed to the console.

Actual Behavior:

  • Initialization of the WebGPU backend fails, and the runtime logs an error about the adapter being invalidated after device creation.

Additional Context / Thoughts:

  • The Deno team’s WebGPU implementation invalidates an adapter object immediately after a device is created. This is different from how WebGPU typically behaves in Chromium-based browsers, where multiple device creations are not strictly blocked in this manner.
  • It appears the TF.js WebGPU backend is designed with the browser’s typical behaviour in mind—hence it tries to re-check or re-create the adapter internally, which fails under Deno’s stricter approach.

Potential Approaches:

  • If TF.js could cache the adapter/device once and avoid re-requesting or re-creating, it might work around Deno’s behaviour.
  • Alternatively, if Deno changed its adapter handling to match the browser more closely, this issue might also be resolved.

Would it be possible to adjust the WebGPU backend to handle an adapter that cannot be re-used, or to avoid re-initializing the adapter once a device has already been created?

Thank you for reviewing. I’m happy to provide additional logs or testing if needed.

@desaisoftwaree desaisoftwaree added the type:bug Something isn't working label Feb 16, 2025
@shmishra99 shmishra99 self-assigned this Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants