From f492002a3ca78b351e5190ed65f454da3669f92d Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Tue, 14 Sep 2021 22:07:13 -0400 Subject: [PATCH] reorganize tfjs bundle --- CHANGELOG.md | 3 ++- build.json | 8 ++++++++ src/env.ts | 2 +- tfjs/tf-browser.ts | 36 +++++++++++++----------------------- tfjs/tf-version.ts | 20 ++++++++++++++++++++ 5 files changed, 44 insertions(+), 25 deletions(-) create mode 100644 tfjs/tf-version.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 309a3d78..07700343 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,9 @@ ## Changelog -### **HEAD -> main** 2021/09/13 mandic00@live.com +### **HEAD -> main** 2021/09/14 mandic00@live.com +- add platform and backend capabilities detection - enhanced automated tests - enable canvas patching for nodejs - full ts strict typechecks diff --git a/build.json b/build.json index 6b0bb75a..a8baf517 100644 --- a/build.json +++ b/build.json @@ -83,6 +83,14 @@ "output": "dist/human.node-wasm.js", "external": ["@tensorflow"] }, + { + "name": "tfjs/browser/version", + "platform": "browser", + "format": "esm", + "input": "tfjs/tf-version.ts", + "output": "dist/tfjs.version.js", + "external": ["fs", "os", "buffer", "util"] + }, { "name": "tfjs/browser/esm/nobundle", "platform": "browser", diff --git a/src/env.ts b/src/env.ts index 221d8c03..5c48d37f 100644 --- a/src/env.ts +++ b/src/env.ts @@ -102,7 +102,7 @@ export async function backendInfo() { env.webgl.backend = env.backends.includes('webgl'); if (env.webgl.supported && env.webgl.backend) { // @ts-ignore getGPGPUContext only exists on WebGL backend - const gl = tf.backend().gpgpu !== 'undefined' ? await tf.backend().getGPGPUContext().gl : null; + const gl = (tf.backend().gpgpu !== 'undefined') && (tf.backend().getGPGPUContext) ? await tf.backend().getGPGPUContext().gl : null; if (gl) { env.webgl.version = gl.getParameter(gl.VERSION); env.webgl.renderer = gl.getParameter(gl.RENDERER); diff --git a/tfjs/tf-browser.ts b/tfjs/tf-browser.ts index b21c1eb1..bf51af3f 100644 --- a/tfjs/tf-browser.ts +++ b/tfjs/tf-browser.ts @@ -3,16 +3,6 @@ * @external */ -// get versions of all packages -import { version as tfjsVersion } from '@tensorflow/tfjs/package.json'; -import { version as tfjsCoreVersion } from '@tensorflow/tfjs-core/package.json'; -import { version as tfjsDataVersion } from '@tensorflow/tfjs-data/package.json'; -import { version as tfjsLayersVersion } from '@tensorflow/tfjs-layers/package.json'; -import { version as tfjsConverterVersion } from '@tensorflow/tfjs-converter/package.json'; -import { version as tfjsBackendCPUVersion } from '@tensorflow/tfjs-backend-cpu/package.json'; -import { version as tfjsBackendWebGLVersion } from '@tensorflow/tfjs-backend-webgl/package.json'; -import { version as tfjsBackendWASMVersion } from '@tensorflow/tfjs-backend-wasm/package.json'; - // export all from sources // requires treeShaking:ignore-annotations due to tfjs misconfiguration /* @@ -25,7 +15,8 @@ export * from '@tensorflow/tfjs-backend-webgl/src/index'; export * from '@tensorflow/tfjs-backend-wasm/src/index'; */ -// export all from build +// export all from build individual packages +/* export * from '@tensorflow/tfjs-core/dist/index.js'; export * from '@tensorflow/tfjs-layers/dist/index.js'; export * from '@tensorflow/tfjs-converter/dist/index.js'; @@ -33,16 +24,15 @@ export * as data from '@tensorflow/tfjs-data/dist/index.js'; export * from '@tensorflow/tfjs-backend-cpu/dist/index.js'; export * from '@tensorflow/tfjs-backend-webgl/dist/index.js'; export * from '@tensorflow/tfjs-backend-wasm/dist/index.js'; -// export * from '@tensorflow/tfjs-backend-webgpu/dist/index.js'; // experimental +*/ -// export versions -export const version = { - tfjs: tfjsVersion, - 'tfjs-core': tfjsCoreVersion, - 'tfjs-data': tfjsDataVersion, - 'tfjs-layers': tfjsLayersVersion, - 'tfjs-converter': tfjsConverterVersion, - 'tfjs-backend-cpu': tfjsBackendCPUVersion, - 'tfjs-backend-webgl': tfjsBackendWebGLVersion, - 'tfjs-backend-wasm': tfjsBackendWASMVersion, -}; +// export all from build bundle +export * from '@tensorflow/tfjs/dist/index.js'; +export * from '@tensorflow/tfjs-backend-webgl/dist/index.js'; +export * from '@tensorflow/tfjs-backend-wasm/dist/index.js'; + +// add webgpu to bundle, experimental +// export * from '@tensorflow/tfjs-backend-webgpu/dist/index.js'; + +// export versions, overrides version object from @tensorflow/tfjs +export { version } from '../dist/tfjs.version.js'; diff --git a/tfjs/tf-version.ts b/tfjs/tf-version.ts new file mode 100644 index 00000000..aaabb80a --- /dev/null +++ b/tfjs/tf-version.ts @@ -0,0 +1,20 @@ +// get versions of all packages +import { version as tfjsVersion } from '@tensorflow/tfjs/package.json'; +import { version as tfjsCoreVersion } from '@tensorflow/tfjs-core/package.json'; +import { version as tfjsDataVersion } from '@tensorflow/tfjs-data/package.json'; +import { version as tfjsLayersVersion } from '@tensorflow/tfjs-layers/package.json'; +import { version as tfjsConverterVersion } from '@tensorflow/tfjs-converter/package.json'; +import { version as tfjsBackendCPUVersion } from '@tensorflow/tfjs-backend-cpu/package.json'; +import { version as tfjsBackendWebGLVersion } from '@tensorflow/tfjs-backend-webgl/package.json'; +import { version as tfjsBackendWASMVersion } from '@tensorflow/tfjs-backend-wasm/package.json'; + +export const version = { + tfjs: tfjsVersion, + 'tfjs-core': tfjsCoreVersion, + 'tfjs-data': tfjsDataVersion, + 'tfjs-layers': tfjsLayersVersion, + 'tfjs-converter': tfjsConverterVersion, + 'tfjs-backend-cpu': tfjsBackendCPUVersion, + 'tfjs-backend-webgl': tfjsBackendWebGLVersion, + 'tfjs-backend-wasm': tfjsBackendWASMVersion, +};