tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

Inconsistent ”Could not get context for WebGL version 2” error message

Open anssiko opened this issue 4 years ago • 10 comments

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow.js): no
  • OS Platform and Distribution: macOS 10.15.7 and iOS 14.1
  • Mobile device: iPhone 11
  • TensorFlow.js installed from (npm or script link): https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]
  • TensorFlow.js version: 2.7.0
  • Browser version:
    • Safari 14.0 on iOS: Mozilla/5.0 (iPhone; CPU iPhone OS 14_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1
    • Safari 14.0.1 on macOS: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.1 Safari/605.1.15
  • Tensorflow.js Converter Version: ?

Describe the current behavior

Steps to reproduce:

  1. Load https://js.tensorflow.org/api/latest/#add

  2. Click the first Run button

  3. Console log prints:

Could not get context for WebGL version 2
Tensor
    [11, 22, 33, 44]
  1. Click Run again
  2. Console log prints:
Tensor
    [11, 22, 33, 44]

Describe the expected behavior

Expected the log messages in steps 3 and 5 to be identical, including the WebGL error message.

Standalone code to reproduce the issue

Reproducible test case: https://js.tensorflow.org/api/latest/#add

Other info / logs

The latest stable Safari 14 ships with WebGL 2.0 disabled by default. Make sure WebGL 2.0 is disabled via Safari > Experimental WebKit Features to be able to reproduce this issue.

anssiko avatar Nov 20 '20 08:11 anssiko

@annxingyuan Can you help to confirm? The WebGL 2 check happens on the first API call, once it is identified missing, we will be using WebGL 1 instead. The following API calls would not trigger the warning message.

pyu10055 avatar Nov 20 '20 19:11 pyu10055

i am having same issue on chrome @pyu10055 image

gauravdhra avatar Jan 27 '21 06:01 gauravdhra

Better documentation on the warning message, people understand that this is the case for safari.

pyu10055 avatar Feb 16 '21 21:02 pyu10055

Thank you for dealing with this issue, but is there a temporary solution for this? For instance, would assigning the backend to WebGL version 1 helps? If yes, could you tell us how to do it, please?

bebensiganteng avatar Mar 03 '21 08:03 bebensiganteng

any updates on this issue ?

pritasam avatar Jun 29 '21 22:06 pritasam

Hi Guys,

Not sure of the root cause for this bug, but one of our client reported such issue on some Windows Machine with Chrome. The workaround was to change a flag in Chrome to not use D3D11 as a backend for ANGLE but to force OpenGL.

As this issue only occurred on few computers it might be related to a combination of Drivers + CPU/GPU platforms. This has only be reported on our side for integrated GPU.

EdgarModiface avatar Jul 26 '21 13:07 EdgarModiface

I am getting this error when trying to use tensorflow js in safari, with an ionic app.

rodrigoGA avatar Nov 02 '21 04:11 rodrigoGA

I am getting this error when trying to use tensorflow js in safari, with an ionic app.

Change to wasm backend

maxcodefaster avatar Nov 02 '21 09:11 maxcodefaster

after digging into https://github.com/tensorflow/tfjs/blob/master/tfjs-backend-webgl/src/flags_webgl.ts following code works setting backend to webgl1:

<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js">
    </script>
    <script>
    tf.ENV.registerFlag('HAS_WEBGL', '1');
    tf.ENV.registerFlag('WEBGL_VERSION', '1');
    tf.ENV.backend = 'webgl';
    tf.setBackend('webgl');    
    </script>
(...)

there is a bug in the webgl implemetation in the panfrost mali mesa driver, have to use https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js , other platforms dont seem to be affected

BenjaminWegener avatar Aug 23 '22 08:08 BenjaminWegener

Safari does not support "document" and "OffscreenCanvas" at the service worker in Manifest V3 (MV3).

The error message was from function createCanvas(webGLVersion) { if (typeof OffscreenCanvas !== "undefined" && webGLVersion === 2) return new OffscreenCanvas(300, 150); else if (typeof document !== "undefined") return document.createElement("canvas"); else throw new Error("Cannot create a canvas in this context"); } I found the "createCanvas" at "tf-backend-webgl.es2017.js" in the package (@tensorflow/tfjs-backend-webgl)

In the manifest v2 is working well, because "document" is available at the service worker ( background script ), but only for Safari with MacOS, not for IOS & IpadOS. (It is using CPU for TF, because it could not create a canvas at IOS and IpadOS. So, It takes many many minutes to get a result from TF. It is not acceptable for public services.)

I am still looking for the answer for the MV3. I hope, the "DOM/document" should be supported in the service worker or silmilar method provided. In the MV3, the persistent must be "False" ( I do understand why they decide ... but, It will be big barrior to TFJS. I hope to see other solutions)

I think, there are no right solution now, until Safari support Offscreencanvas in MV3. However, Safari will not seem to support it in the future too, sadly...

noma6386-duplicate avatar Sep 14 '22 21:09 noma6386-duplicate

Same error On OSX 10.11, hardware from 2010, using the Universal Sentence Encoder, loaded into a web page, no Chrome TF extension, on the most recent version of Chrome supported by that platform (ok, thats old and unsupported), this results in a total OS freeze with loss of all UI interactivity, materialising as an inability to switch applications and the mouse pointer disappearing, keyboard dead, display miniport events ignored. No indication of being stuck in a compute loop as the fans don't spin up. I suspect it is hitting a bug in the related to the GPU. On one occasion Chrome exited and came back with with the Dev Tools debugger at an exception breakpoint. I have so far, failed to repeat the recovery.

After 15m the screen flickered and messed up display of UI areas. Mouse pointer returns with a spinning pizza wheel for about 30s which then freezes again. Mode flickering, and eventually at about 17m in the screen goes blank as if powered off (not the case, keyboard backlight remains on)

Would it be safe to assume that TF isn't going to work on older browsers like this one, or infact any older hardware... or is it something that can be configured as above ?

ieb avatar Apr 18 '23 07:04 ieb

The OS crash was caused by running the GPU out of memory. Reducing the batch size of embeddings in the Universal Sentence Encoder has fixed the crash and produced a reliable error Failed to compile fragment shader in Chrome. tf.enableDebugMode() reported a potential memory leak in GPU memory which was enough of a hint for me to move on. Still have to find a fix. Chrome version was 103.0.5060.134, but the crash was probably the OS and not Chrome. (HTH someone).

ieb avatar Apr 18 '23 10:04 ieb

Hi, @anssiko

Apologize for the delayed response and I tried to replicate the same issue with Safari Browser and I haven't got any error message and it's working as expected, For your reference I have added screenshot below so I would request you to please give it try from your end with latest version of TFJs and I tried to execute the same code in Tensorflow.js documentation which you mentioned in the issue template.

If issue still persists please let us know with error log and code snippet ?

Could you please confirm if this issue is resolved for you ? Please feel free to close the issue if it is resolved ?

image

gaikwadrahul8 avatar May 14 '23 21:05 gaikwadrahul8

I can still reproduce this following the steps in the OP. Please note that to reproduce you must have WebGL 2.0 disabled via Safari > Experimental WebKit Features / Safari > Advanced > Experimental Features.

If WebGL 2.0 is enabled, the error does not occur. I don't know what's the default for WebGL 2.0 in Safari nowadays. If it is on by default then this bug will only affect advanced users that know how to disable WebGL 2.0.

Here's what I tested this with now:

  • Safari 16.4.1 on iOS:
    • Mozilla/5.0 (iPhone; CPU iPhone OS 16_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Mobile/15E148 Safari/604.1
  • Safari 16.0 on macOS:
    • Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15

anssiko avatar May 15 '23 13:05 anssiko

Hi, @anssiko

Apologize for the delayed response and I tried to replicate the same issue from my end by disabling the WebGL 2.0 and It's working as expected in safari browser and I'm using safari browser Version 16.5 so could you please try with safari browser version 16.5 and let us know still are you facing the same issue or not please ? Thank you!

gaikwadrahul8 avatar Jun 09 '23 12:06 gaikwadrahul8

It looks like this has been fixed in Safari 16.5 since I no longer can reproduce this per my own instructions. Feel free to close. Thanks!

anssiko avatar Jun 09 '23 12:06 anssiko

Hi, @anssiko

Thank you for the confirmation and Could you please confirm if this issue is resolved for you ? Please feel free to close the issue from your end, if it is resolved ? Thank you!

gaikwadrahul8 avatar Jun 12 '23 14:06 gaikwadrahul8

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

google-ml-butler[bot] avatar Jun 12 '23 15:06 google-ml-butler[bot]