Skip to content

tf.LayersModel.save not working inside web worker #3402

@paulkre

Description

@paulkre

TensorFlow.js version

2.0.0

Browser version

Chrome 83.0.4103.61

Description

model.worker.ts:4 Uncaught ReferenceError: window is not defined
    at Module../node_modules/workerize-loader/dist/rpc-worker-loader.js!./node_modules/babel-loader/lib/index.js?!./node_modules/eslint-loader/dist/cjs.js?!./src/game/ml/model.worker.ts (model.worker.ts:4)
    at __webpack_require__ (bootstrap:19)
    at bootstrap:83
    at bootstrap:83

Saving a LayersModel to the IndexedDB inside a web worker does not work because the window object is not defined. This is because inside a web worker the global scope is saved in a variable named self or globalThis.

The problem was identified in #2643 and it was attempted to be fixed in PR #2647. Unfortunately, the merged solution did not fix the problem (I am not sure how it passed the review).

The following line will throw a ReferenceError because window is not defined.

const theWindow: any = window || self;

A better implementation would be:

const theWindow: any = typeof window === 'undefined' ? self : window;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions