diff --git a/js/web/lib/backend-wasm-with-training.ts b/js/web/lib/backend-wasm-with-training.ts index 6d31861fb1bcd..5fac10da7791d 100644 --- a/js/web/lib/backend-wasm-with-training.ts +++ b/js/web/lib/backend-wasm-with-training.ts @@ -3,15 +3,17 @@ import {InferenceSession, TrainingSessionHandler} from 'onnxruntime-common'; -import {OnnxruntimeWebAssemblyBackend} from './backend-wasm' +import {OnnxruntimeWebAssemblyBackend} from './backend-wasm'; class OnnxruntimeTrainingWebAssemblyBackend extends OnnxruntimeWebAssemblyBackend { + /* eslint-disable @typescript-eslint/no-unused-vars */ async createTrainingSessionHandler( checkpointStateUriOrBuffer: string|Uint8Array, trainModelUriOrBuffer: string|Uint8Array, evalModelUriOrBuffer: string|Uint8Array, optimizerModelUriOrBuffer: string|Uint8Array, options: InferenceSession.SessionOptions): Promise { throw new Error('Method not implemented yet.'); } + /* eslint-enable @typescript-eslint/no-unused-vars */ } export const wasmBackend = new OnnxruntimeTrainingWebAssemblyBackend(); diff --git a/js/web/lib/index.ts b/js/web/lib/index.ts index 03e1108b31d12..30d0af5dc9e47 100644 --- a/js/web/lib/index.ts +++ b/js/web/lib/index.ts @@ -7,7 +7,7 @@ // So we import code inside the if-clause to allow terser remove the code safely. export * from 'onnxruntime-common'; -import {registerBackend, env, Backend} from 'onnxruntime-common'; +import {registerBackend, env} from 'onnxruntime-common'; import {version} from './version'; if (!BUILD_DEFS.DISABLE_WEBGL) { diff --git a/js/web/package.json b/js/web/package.json index 8ae5b733e5f21..798434bf5d574 100644 --- a/js/web/package.json +++ b/js/web/package.json @@ -78,6 +78,10 @@ "./webgpu": { "types": "./types.d.ts", "default": "./dist/ort.webgpu.min.js" + }, + "./training": { + "types": "./types.d.ts", + "default": "./dist/ort-web.training.wasm.min.js" } }, "types": "./types.d.ts", diff --git a/js/web/script/build.ts b/js/web/script/build.ts index bda7da0ab2a8b..ff94d1a806a09 100644 --- a/js/web/script/build.ts +++ b/js/web/script/build.ts @@ -34,6 +34,7 @@ const ROOT_FOLDER = path.join(__dirname, '..'); const WASM_BINDING_FOLDER = path.join(ROOT_FOLDER, 'lib', 'wasm', 'binding'); const WASM_BINDING_JS_PATH = path.join(WASM_BINDING_FOLDER, 'ort-wasm.js'); const TRAINING_WASM_BINDING_SIMD_JS_PATH = path.join(WASM_BINDING_FOLDER, 'ort-training-wasm-simd.js'); +const TRAINING_WASM_BINDING_SIMD_MIN_JS_PATH = path.join(WASM_BINDING_FOLDER, 'ort-training-wasm-simd.min.js'); const WASM_BINDING_THREADED_JS_PATH = path.join(WASM_BINDING_FOLDER, 'ort-wasm-threaded.js'); const WASM_BINDING_SIMD_THREADED_JSEP_JS_PATH = path.join(WASM_BINDING_FOLDER, 'ort-wasm-simd-threaded.jsep.js'); const WASM_BINDING_THREADED_WORKER_JS_PATH = path.join(WASM_BINDING_FOLDER, 'ort-wasm-threaded.worker.js'); @@ -93,6 +94,31 @@ if (WASM) { */ `; + npmlog.info('Build', 'Minimizing file "ort-training-wasm-simd.js"...'); + try { + const terser = spawnSync( + 'npx', + [ + 'terser', TRAINING_WASM_BINDING_SIMD_JS_PATH, '--compress', 'passes=2', '--format', 'comments=false', + '--mangle', 'reserved=[_scriptDir]', '--module' + ], + {shell: true, encoding: 'utf-8', cwd: ROOT_FOLDER}); + if (terser.status !== 0) { + console.error(terser.error); + process.exit(terser.status === null ? undefined : terser.status); + } + + fs.writeFileSync(TRAINING_WASM_BINDING_SIMD_MIN_JS_PATH, terser.stdout); + fs.writeFileSync(TRAINING_WASM_BINDING_SIMD_JS_PATH, `${COPYRIGHT_BANNER}${terser.stdout}`); + + validateFile(TRAINING_WASM_BINDING_SIMD_MIN_JS_PATH); + validateFile(TRAINING_WASM_BINDING_SIMD_JS_PATH); + } catch (e) { + npmlog.error('Build', `Failed to run terser on ort-training-wasm-simd.js. ERR: ${e}`); + throw e; + } + npmlog.info('Build', 'Minimizing file "ort-training-wasm-simd.js"... DONE'); + npmlog.info('Build', 'Minimizing file "ort-wasm-threaded.js"...'); try { const terser = spawnSync( diff --git a/js/web/webpack.config.js b/js/web/webpack.config.js index 7115a3e3b4258..1ac421b5246d9 100644 --- a/js/web/webpack.config.js +++ b/js/web/webpack.config.js @@ -104,6 +104,7 @@ function buildConfig({filename, format, target, mode, devtool, build_defs}) { config.resolve.alias['./binding/ort-wasm-threaded.js'] = './binding/ort-wasm-threaded.min.js'; config.resolve.alias['./binding/ort-wasm-threaded-simd.jsep.js'] = './binding/ort-wasm-threaded-simd.jsep.min.js'; config.resolve.alias['./binding/ort-wasm-threaded.worker.js'] = './binding/ort-wasm-threaded.min.worker.js'; + config.resolve.alias['./binding/ort-training-wasm-simd.js'] = './binding/ort-training-wasm-simd.min.js'; const options = defaultTerserPluginOptions(target); options.terserOptions.format.preamble = COPYRIGHT_BANNER; @@ -291,8 +292,8 @@ module.exports = () => { // ort-web.es5.min.js buildOrtWebConfig({suffix: '.es5.min', target: 'es5'}), - // ort.wasm.min.js - buildOrtConfig({ + // ort-web.training.wasm.min.js + buildOrtWebConfig({ suffix: '.training.wasm.min', build_defs: { ...DEFAULT_BUILD_DEFS,