Recurrent (Feedback) Inputs
Overview
Recurrent inputs enable sequential frame-by-frame inference where the model’s output at frame t feeds back into a memory input slot at frame t+1. This is essential for architectures like NeuroSAM that track objects across video frames by conditioning each prediction on the previous one.
Recurrent positions are configured as MemoryFrameBinding entries whose frame variant is RecurrentFrameSource. They live in the same memory_frames vector as static positions — see Memory Frame Bindings and Hybrid Sequence Inputs.
Core types: src/DeepLearning/bindings/DeepLearningBindingData.hpp
Engine: src/WhiskerToolbox/DeepLearning_Widget/Core/SlotAssembler.cpp
Orchestration: src/WhiskerToolbox/DeepLearning_Widget/Inference/InferenceController.cpp
Data model
RecurrentFrameSource
| Field | Type | Description |
|---|---|---|
slot_name |
string |
Memory input slot name (on MemoryFrameBinding) |
memory_index |
int |
Position along sequence_dim (0 for single-frame slots) |
frame.output_slot_name |
string |
Model output slot to read the tensor from |
frame.init |
RecurrentInitVariant |
How to seed the cache at t=0 |
A recurrent binding is active when hasActiveRecurrentBinding() is true: output_slot_name is non-empty and not "(None)".
Initialization modes
At t=0, the recurrent cache needs an initial tensor because no previous output exists. Three strategies are supported via RecurrentInitVariant:
| Mode | Struct | Behavior |
|---|---|---|
| Zeros | ZerosInit |
All-zeros tensor matching the per-element input shape |
| StaticCapture | StaticCaptureInit |
Use a DataBank tensor or capture from DataManager at data_key / frame; falls back to zeros |
| FirstOutput | FirstOutputInit |
Run forward once with zeros at frame 0, skip decoding, use raw output as initial state; real sequence starts at frame 1 |
StaticCaptureInit resolution (see runRecurrentSequence() t=0 block):
- Static
MemoryFrameBindingat the samememory_indexwith DataBank source. defaultBankEntryId(slot_name, memory_index)in the session DataBank.- On-the-fly
captureStaticInput()fromStaticCaptureInit.data_keyatStaticCaptureInit.frame. - Zeros fallback with warning.
Sequential inference loop
SlotAssembler::runRecurrentSequence() implements the frame loop:
for each frame in [start_frame, start_frame + frame_count):
1. assembleInputs() — static positions + dynamic inputs (batch_size = 1)
2. Inject recurrent cache tensors at each active recurrent memory_index
3. runModelAndPostEncoder()
4. On frame 0 with FirstOutputInit: update cache, skip decode, continue
5. decodeOutputs() into DataManager
6. Cache output tensors for next frame's recurrent inputs
Batch size is always 1 during recurrent inference — each frame depends on the previous frame’s output.
Recurrent cache
The cache lives in SlotAssembler::Impl::recurrent_cache:
std::unordered_map<std::string, at::Tensor> recurrent_cache;Keys use recurrentCacheKey(input_slot_name) → "recurrent:{input_slot_name}".
The cache is cleared:
- When the model is reset (
resetModel()) - At the start of each
InferenceController::runRecurrentSequence()call (clearRecurrentCache()) - Explicitly via
clearRecurrentCache()
resetModel() does not clear the session DataBank.
FirstOutput frame-0 behavior
When any active recurrent binding uses FirstOutputInit, frame 0:
- Runs forward and decodes are skipped (
continuebeforedecodeOutputs). - All active recurrent bindings update their cache from the corresponding output slots (including non-
FirstOutputbindings on the same frame). - The real decoded sequence begins at frame 1.
UI integration
MemorySlotWidget
Each memory input slot gets a MemorySlotWidget. Recurrent positions expose output-slot and init-mode controls via RecurrentFrameSourceForm (schema-driven AutoParamWidget).
When any recurrent binding is active:
dl::constraints::computeBatchSizeConstraint()forces min=max=1DeepLearningPropertiesWidget::_updateBatchSizeConstraint()disables the batch spinbox with an explanatory tooltip
View widget
recurrentProgressChanged(current, total) drives the progress display during recurrent inference (“Frame N / M”).
Run action
The ↻ Recurrent button calls DeepLearningPropertiesWidget::_onRunRecurrentSequence():
- Validates model readiness and active recurrent bindings
- Prompts for frame count (default from batch spinbox or 100)
- Syncs bindings from UI to
DeepLearningState - Delegates to
InferenceController::runRecurrentSequence()
Unlike batch inference, recurrent sequences run synchronously on the main thread inside InferenceController (no QThread worker). Progress is reported via recurrentProgressChanged, not batchProgressChanged.
State persistence
MemoryFrameBinding entries (static and recurrent) are stored in DeepLearningStateData::memory_frames and serialized via reflect-cpp. Bindings are cleared when the model changes (model-specific configuration).
Encoded tensors in the session DataBank are not serialized — re-capture after workspace restore. See Static Input Capture.
Batch size constraints
Recurrent bindings force batch_size = 1. Enforced in:
- UI — spinbox disabled when
constraint.forced_by_recurrentis true - Engine —
runRecurrentSequence()always passesbatch_size=1toassembleInputs()
See Session Constraints.