Skip to content

[wasm] Slice: read the input through a heap view instead of readSync (fixes O(n²) unstack) - #8764

Open
RichardBJ wants to merge 1 commit into
tensorflow:masterfrom
RichardBJ:wasm-slice-heap-view
Open

RichardBJ wants to merge 1 commit into
tensorflow:masterfrom
RichardBJ:wasm-slice-heap-view

Conversation

@RichardBJ

Copy link
Copy Markdown

Fixes #8763.

Problem. cpu and webgl worked fine, but I found the wasm Slice kernel began with backend.readSync(x.dataId), which copies the whole input
out of the wasm heap on every call, however small the slice chosen. Since Unpack calls slice once per output, this means that
tf.unstack increased quadratically in the unstacked length. Every LSTM/GRU layer unstacks its input along time, so
recurrent 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 x through the backend.typedArrayFromHeap(x), a view with no copy. The
views are taken after makeOutput, because makeOutput may grow the heap itself and detach earlier views. String
tensors still keep the readSync path. The slice2d/3d/4d helpers and the rank > 4 path are also unchanged; they now
read from the view. The fix is in Slice, so Unpack, split and other callers of slice all 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:

  • 3,000 random slices, rank 1–6, float32/int32/bool, contiguous and strided: all exactly equal to the cpu backend.
  • String slices (unchanged path): correct.
  • A slice whose output forces heap growth (16 MB → 265 MB during makeOutput): correct.
  • Finally, tf.stack(tf.unstack(x, 1), 1) exactly equals x; tf.concat(tf.split(x, 40, 1), 1) also equals x; an LSTM layer
    gives 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):

cpu backend wasm before wasm after
tf.unstack of [8, 500, 32] along axis 1 53 ms 70 ms 1 ms
tf.unstack of [8, 1000, 32] along axis 1 91 ms 256 ms 2 ms
tf.unstack of [8, 4000, 32] along axis 1 319 ms 2159 ms 7 ms
tf.unstack of [8, 8000, 32] along axis 1 662 ms 9024 ms 15 ms
LSTM layer (16 units, returnSequences) on [8, 2000, 32] 646 ms 847 ms 261 ms
LSTM layer (16 units, returnSequences) on [8, 8192, 32] 2604 ms 10848 ms 2534 ms

The 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.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[wasm] unstack (Unpack kernel) copies the whole input once per output slice: O(n²) time, slows LSTM layers

1 participant