From ab3cda4f5136c9c1af2005df2fd6d793d4ee6f05 Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Sun, 3 Oct 2021 08:12:26 -0400 Subject: [PATCH] fix backend order initialization --- demo/nodejs/node.js | 1 + src/tfjs/backend.ts | 16 ++++++++++++---- src/tfjs/humangl.ts | 14 +++++++------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/demo/nodejs/node.js b/demo/nodejs/node.js index 5979e524..78969f7c 100644 --- a/demo/nodejs/node.js +++ b/demo/nodejs/node.js @@ -135,6 +135,7 @@ async function detect(input) { } else { log.data(' Gesture: N/A'); } + if (result && result.object && result.object.length > 0) { for (let i = 0; i < result.object.length; i++) { const object = result.object[i]; diff --git a/src/tfjs/backend.ts b/src/tfjs/backend.ts index 4dbb395a..3b5de6d5 100644 --- a/src/tfjs/backend.ts +++ b/src/tfjs/backend.ts @@ -45,7 +45,7 @@ export async function check(instance, force = false) { if (!available.includes(instance.config.backend)) { log(`error: backend ${instance.config.backend} not found in registry`); - instance.config.backend = env.env.node ? 'tensorflow' : 'humangl'; + instance.config.backend = env.env.node ? 'tensorflow' : 'webgl'; if (instance.config.debug) log(`override: setting backend ${instance.config.backend}`); } @@ -83,9 +83,17 @@ export async function check(instance, force = false) { log('changing webgl: WEBGL_DELETE_TEXTURE_THRESHOLD:', true); tf.ENV.set('WEBGL_DELETE_TEXTURE_THRESHOLD', 0); } - // @ts-ignore getGPGPUContext only exists on WebGL backend - const gl = await tf.backend().getGPGPUContext().gl; - if (instance.config.debug) log(`gl version:${gl.getParameter(gl.VERSION)} renderer:${gl.getParameter(gl.RENDERER)}`); + if (tf.backend().getGPGPUContext) { + const gl = await tf.backend().getGPGPUContext().gl; + if (instance.config.debug) log(`gl version:${gl.getParameter(gl.VERSION)} renderer:${gl.getParameter(gl.RENDERER)}`); + } + } + + // handle webgpu + if (tf.getBackend() === 'humangl') { + tf.ENV.set('WEBGPU_USE_GLSL', true); + tf.ENV.set('WEBGL_PACK_DEPTHWISECONV', false); + tf.ENV.set('WEBGL_USE_SHAPES_UNIFORMS', true); } // wait for ready diff --git a/src/tfjs/humangl.ts b/src/tfjs/humangl.ts index f3500f49..f6fdec71 100644 --- a/src/tfjs/humangl.ts +++ b/src/tfjs/humangl.ts @@ -93,13 +93,6 @@ export async function register(instance): Promise { log('error: cannot set WebGL context:', err); return; } - const current = tf.backend().getGPGPUContext ? tf.backend().getGPGPUContext().gl : null; - if (current) { - log(`humangl webgl version:${current.getParameter(current.VERSION)} renderer:${current.getParameter(current.RENDERER)}`); - } else { - log('error: no current gl context:', current, config.gl); - return; - } try { const ctx = new tf.GPGPUContext(config.gl); tf.registerBackend(config.name, () => new tf.MathBackendWebGL(ctx), config.priority); @@ -117,6 +110,13 @@ export async function register(instance): Promise { log('error: cannot update WebGL backend registration:', err); return; } + const current = tf.backend().getGPGPUContext ? tf.backend().getGPGPUContext().gl : null; + if (current) { + log(`humangl webgl version:${current.getParameter(current.VERSION)} renderer:${current.getParameter(current.RENDERER)}`); + } else { + log('error: no current gl context:', current, config.gl); + return; + } try { tf.ENV.set('WEBGL_VERSION', 2); } catch (err) {