Repository navigation
Conversation
Slice began with backend.readSync(x.dataId), which copies the whole input out of the wasm heap on every call, however small the slice. Unpack calls slice once per output, so tf.unstack was quadratic in the unstacked length, and every LSTM/GRU layer (which unstacks along time) paid for it. Read x through backend.typedArrayFromHeap instead, taken after makeOutput because makeOutput may grow the heap and detach earlier views. Strings keep the readSync path. Output is unchanged. Fixes tensorflow#8763
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #8763.
Problem. cpu and webgl worked fine, but I found the wasm
Slicekernel began withbackend.readSync(x.dataId), which copies the whole inputout of the wasm heap on every call, however small the slice chosen. Since
Unpackcallssliceonce per output, this means thattf.unstackincreased quadratically in the unstacked length. Every LSTM/GRU layer unstacks its input along time, sorecurrent models paid very heavily for it on every call. Note that from T ≈ 500 the wasm backend was slower than the plain-JS cpu
backend for this op, and the gap grows with T, so at T = 8000 it was more than 10x slower than the plain-JS cpu backend code (table below).
Change. For numeric dtypes, read
xthrough thebackend.typedArrayFromHeap(x), a view with no copy. Theviews are taken after
makeOutput, becausemakeOutputmay grow the heap itself and detach earlier views. Stringtensors still keep the
readSyncpath. The slice2d/3d/4d helpers and the rank > 4 path are also unchanged; they nowread from the view. The fix is in
Slice, soUnpack,splitand other callers ofsliceall benefit.Testing. I could not run the Bazel suite here locally, but I tested a line-for-line JS transcription of the new
kernel, registered over @tensorflow/tfjs 4.22.0 (wasm) in Node 26, against the cpu backend:
makeOutput): correct.tf.stack(tf.unstack(x, 1), 1)exactly equalsx;tf.concat(tf.split(x, 40, 1), 1)also equalsx; an LSTM layergives bit-identical output before and after my change.
Example timings (Node 26 on Windows, wasm single thread; each row from one run, all three columns
measured in that run, mean of 3 calls):
tf.unstackof [8, 500, 32] along axis 1tf.unstackof [8, 1000, 32] along axis 1tf.unstackof [8, 4000, 32] along axis 1tf.unstackof [8, 8000, 32] along axis 1The unstack times repeat closely. Whole-layer times vary between runs on a busy desktop: over four
runs of the [8, 8192, 32] LSTM, wasm before was 10.8–16.7 s and wasm after 1.1–2.9 s (4–10x faster),
against 2.6–5.5 s on the cpu backend. The LSTM output is bit-identical before and after in every run.
The CI's slice/unstack tests (already included for wasm in setup_test.ts) cover correctness.