tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

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

Open desaisoftwaree opened this issue 1 year ago • 6 comments

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 avatar Feb 16 '25 14:02 desaisoftwaree

Hi @desaisoftwaree, Apologies for the delayed response. Thank you for bringing this to our attention and for providing the reproducible code. We truly appreciate you flagging this issue, and we will file a bug internally.

Varun-S10 avatar Mar 27 '26 04:03 Varun-S10

Hi @desaisoftwaree, Thank you for your patience. As a workaround for this adapter invalidation error, could you please try using the CPU backend, and let us know if that resolves the issue for you?

Varun-S10 avatar Jun 11 '26 12:06 Varun-S10

With CPU everything was working since very beginning but that is not useful due to complexity, size of operations performed and performance needed in real tensorflow application. The original given example here is just to demonstrate the core issue.

desaisoftwaree avatar Jun 12 '26 04:06 desaisoftwaree

Hello @desaisoftwaree ,

Thank you for bringing this to our attention and for providing a comprehensive overview. We appreciate the time you invested in creating this issue.

Currently, our internal development team is concentrated on other scheduled milestones, so we cannot prioritize this task immediately. However, we would love to see this move forward as a community-led effort.

If you’d like to submit a fix, please fork the repo and open a PR following the TensorFlow Contribution Guidelines, Tfjs Contributions and be sure to link this issue in your PR to avoid duplicate work, kindly leave a comment here if you plan to take this on.

Thank you again for your interest and time.

nileshau-afk avatar Jun 24 '26 08:06 nileshau-afk

Hi @desaisoftwaree, Thank you for opening this issue. Since this issue has been open for a long time, we are closing this issue for now. If this is still relevant and you would like to work on it, please feel free to reopen the issue or submit a pull request referencing it. Thank you for your understanding.

Varun-S10 avatar Aug 26 '26 05:08 Varun-S10

Are you satisfied with the resolution of your issue? Yes No

google-ml-butler[bot] avatar Aug 26 '26 05:08 google-ml-butler[bot]