Streaming state and sliding inputs
Preserve sample timing and per-stream state while separating overlapped windows, cached computation, and independent evaluation examples.
On this page
The question for today
What you’ll understand
- Compute sliding-window count, overlap and buffer size in sample units.
- Define state ownership, reset rules and startup behavior for a streaming model.
- Avoid treating overlapping windows as independent evaluation cases.
A streaming classifier repeatedly observes an evolving signal rather than a collection of independent files. Its input window, step size, feature state, and model state determine both latency and semantics. Recomputing a full model on overlapping windows is not automatically equivalent to a stateful incremental implementation. Define the stream’s time base and state lifecycle first, then verify the incremental path against a reference using the same padding, receptive field, and reset policy.
Window length and hop are different latency quantities
At sample rate f_s, a W-sample window spans W/f_s seconds, while an H-sample hop produces a new window every H/f_s seconds after initialization. Overlap is W−H when H is no larger than W. A detector can emit frequent decisions while each decision still depends on a long history. State whether outputs are causal, whether lookahead is required, and whether timestamps refer to the beginning, end, or center of the observed window.
Use integer sample indices rather than repeatedly rounding wall-clock durations. Clock drift, missing samples, and discontinuities need an explicit policy: drop a decision, insert marked padding, resample, or reset. A circular buffer reduces data movement only if producer and consumer ownership is correct. With DMA capture, do not overwrite samples still being read. Also distinguish raw-sample buffers, feature windows, cached activations, and recurrent state because each has different dimensions and retention requirements.
Make every state belong to one stream and version
A streaming network can cache convolution history, maintain a recurrent hidden state, or update a feature extractor’s running statistics. Assign each state to a stream identifier and a compatible model/preprocessor version. Reset at session boundaries and after discontinuities according to the application contract, rather than carrying one person’s audio history into another session. On restart, decide whether state is restored from trusted storage or rebuilt from new samples. Either choice affects startup accuracy, latency, and privacy.
Changing a model can change state shape or meaning even when its output labels remain the same. Do not feed an old hidden state into new weights without a validated migration rule. ExecuTorch 0.7 documents restrictions around exported buffer mutation and backend support, so statefulness is not inferred from a Python module simply having a buffer. A portable deployment may expose state tensors as explicit inputs and outputs, with application-owned lifetimes and a tested invocation order.
Verify streaming equivalence and evaluate real events
Test a long reference recording both as one defined sequence and through the deployed chunk boundaries. Compare outputs after accounting for declared startup context, padding and lookahead; repeat with resets, missing chunks, and multiple interleaved streams. Overlapping windows share signal content and are not independent statistical evidence. Split evaluation by recording, subject, device session, or another defensible unit, and report event detection delay and false alarms over time rather than only an inflated count of nearly duplicate windows.
The modular lab illustrates circular index progression only. Reset, set modulus m to 10 and additive step a to 4, predict residue eight after two Steps and period five, then take those Steps. Compare a fresh Reset with both m at 10 and a at 3 before two Steps: residue six and period ten. Each Step advances one addition. This toy has no audio, window extraction, DMA ownership, cached network state, or evidence of streaming-model equivalence.
Work it through
A one-second signal yields fewer than one hundred full windows
A mono signal has 16,000 samples at 16 kHz. Use window W = 400 samples, hop H = 160, no padding, and two bytes per raw sample. Compute full windows, raw-buffer capacity, and naive sample visits.
The window spans 400/16,000 = 25 ms, while the hop is 160/16,000 = 10 ms. Each neighboring pair shares 240 samples.
Full-window count is floor((16,000 − 400)/160) + 1 = 98. A raw circular buffer for one window requires 400 × 2 = 800 bytes, excluding state, feature and DMA buffers.
Naively rereading every window visits 98 × 400 = 39,200 sample positions. The last window ends at exclusive index 400 + 97 × 160 = 15,920, leaving 80 final samples unused under the stated no-padding policy.
Decision frequency, retained history, sample traffic and model-state cost are separate quantities.
The living notebook
Explore the model
Interactive lab · modular
Circular indices without a streaming network
Reset, set m = 10 and a = 4, predict residue 8 after two Steps and period 5, then take those Steps. Compare a fresh Reset with both m = 10 and a = 3 before two Steps: residue 6 and period 10. One Step is one modular addition. This illustrates indexing, not real sample buffers, producer ownership, or recurrent-state correctness.
- Residue r0
- 0
- Orbit period · m/gcd(m,a)
- 12
- Distinct residues visited
- 1 of 12
Addition modulo m starts at zero: rₙ = na mod m. Playback advances two additions per second. The orbit has period m/gcd(m,a), including period one for a = 0. This is not a cryptographic security test or a primality algorithm. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA correct index cycle is necessary bookkeeping, not a complete streaming implementation.
Pause & reason
Check your understanding
Your turn
Try it yourself
A mono stream provides 4,000 samples at 8 kHz. Use W = 320, H = 80, no padding and two-byte samples. Compute window duration, hop duration, overlap, full-window count, and one-window raw-buffer capacity.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Apply the integer full-window formula; buffer capacity counts bytes, while overlap counts sample positions.
Compare with the solution
The window spans 40 ms and the hop spans 10 ms. Overlap is 320 − 80 = 240 samples. Full-window count is floor((4,000 − 320)/80) + 1 = 47. One raw window occupies 320 × 2 = 640 bytes. This does not include feature extraction history, recurrent state or a second DMA buffer, and the 47 highly overlapped windows are not automatically 47 independent evaluation units.
Keep the idea
Take it with you
- Specify sample indices, window, hop, padding and timestamps explicitly.
- State belongs to a stream and compatible model/preprocessor version.
- Validate chunk equivalence and evaluate independent events or sessions.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.