This solution builds TensorFlow/JS for Browsers, including individual modules as well as a unified bundle
Included Backends: CPU
, WebGL
, WebGPU
, WASM
Node packages (tfjs-node
and tfjs-node-gpu
) are not covered
- Does not perform any modifications to
tfjs
sources
Instead it relies on custom import resolver during build process - Does not use build hacks such as symlinks or virtual packages or manually set sideeffects
- Masively smaller:
- original
tf.es2017.js
is 3.9MB withoutwasm
andwebgpu
backends which are another 2MB on top - new
tfjs.esm.js
is 2.5MB withwasm
andwebgpu
included
and that is non-minfied and with tree shaking disabled
- original
- Entire build is about 10x faster
Full build takes <2min on a notebook (of which >90% is compiling WASM code)
Rebuild takes <10sec - Changing WASM build compiler parameters improves performance slightly
- Changing backend imports to link to source files instead of symlinked packages solves a lot of dependency issues
- Changing imports from files generated in dist to actual sources makes everything tree shakable
- Internal dependencies:
npm install
- External dependencies:
Git: assumed to be already installed if you're here :)
Bazel: requiresbazel 4.x
https://docs.bazel.build/versions/4.2.1/install-ubuntu.html
Python: requirespython 3.x
aliased aspython
, not compatible withpython 2.x
EmScripten SDK: auto-downloaded during first build
npm run build
executes steps:
- download:
scripts/download.sh
on first run it clones@tensorflow/tfjs
from git into./src
on subsequent runs it rolls forward cloned copy of@tensorflow/tfjs
- prepare:
scripts/prepare.sh
creates versioned.tfjs-*.ts
from build templates
and.js
imports from.json
files - build wasm:
scripts/wasm.sh
build wasm binaries and wasm.js
exports usingbazel
- build js packages:
node scripts/bundle.js
build targets in/dist
usingesbuild
with custom resolver plugins and TypeScript compiler settings - build typedefs:
scripts/types.sh
build typedefs bundle in/lib
usingtsc
&api-extractor
To remove currently downloaded version of TFJS and reset all cached info
npm run clean