Skip to main content

Hybrid Sequence Inputs

Overview

Hybrid sequence inputs let a single memory slot (e.g. memory_images with shape [5, 3, 256, 256] and sequence_dim = 0) mix static and recurrent sourcing across positions. Early positions hold reference frames from DataManager or pre-encoded DataBank tensors; later positions feed back the model’s previous output at that index.

This pattern is used by NeuroSAM-style models: user-selected reference frames occupy early sequence positions and the model’s own prediction at the final position is fed back recurrently.

Core types: src/DeepLearning/bindings/DeepLearningBindingData.hpp
Assembly: src/WhiskerToolbox/DeepLearning_Widget/Core/SlotAssembler.cpp
UI: src/WhiskerToolbox/DeepLearning_Widget/UI/Helpers/MemorySlotWidget.hpp

Data model

Each sequence position is one MemoryFrameBinding with the same slot_name and a distinct memory_index. The frame tagged union selects static or recurrent for that position only — there is no separate recurrent panel or parallel recurrent_bindings vector.

Position kind frame_kind Source
Static StaticFrameSource DataManagerStaticSource (re-encode each run) or DataBankStaticSource (reuse cached tensor)
Recurrent RecurrentFrameSource Raw output tensor from output_slot_name, seeded at t=0 via init

See Memory Frame Bindings for the full type reference and DataBank for pre-encoded static tensors.

Assembly logic

Per-position configuration

assembleInputs() groups memory_frames by slot_name and processes each static input slot:

Static positions (hasActiveStaticSource):

  • DataManager — channel-encode data_key at current_frame + time_offset into the slice at memory_index.
  • DataBank — copy the pre-encoded tensor from resolvedBankEntryId() into the slice. No re-encoding from source geometry.

Recurrent positions (hasActiveRecurrentBinding):

  • Skipped during static assembly (left zero-padded in the assembled tensor).
  • Filled later in runRecurrentSequence() by injecting the recurrent cache tensor at memory_index along sequence_dim.

Each memory_index is configured exactly once as either static or recurrent. There is no position-claiming map.

Hybrid injection in runRecurrentSequence()

During the frame loop:

  1. assembleInputs() fills static positions (and leaves recurrent slices at zero).
  2. For each active recurrent frame, the cached tensor is injected:
    • Sequence slots: tensor.select(sequence_dim + 1, memory_index).copy_(cache)
    • Non-sequence slots: the entire input tensor is replaced.
  3. runModelAndPostEncoder() runs forward.
  4. Output tensors are cached for the next frame (keyed by input slot name).

The recurrent cache key is recurrent:{input_slot_name} via recurrentCacheKey() — one cache entry per input slot, not per memory_index. Hybrid configurations typically use a single recurrent position per slot (e.g. the last index in a sequence).

t=0 initialization

Recurrent init modes (ZerosInit, StaticCaptureInit, FirstOutputInit) apply only to recurrent positions. Static positions are populated from their configured sources on every frame, including frame 0.

StaticCaptureInit resolution order at t=0:

  1. If a static MemoryFrameBinding exists at the same slot_name + memory_index with DataBank source, use that bank entry’s encoded tensor.
  2. Else try defaultBankEntryId(slot_name, memory_index) in the session DataBank.
  3. Else if StaticCaptureInit specifies data_key and frame >= 0, call captureStaticInput() to encode into the default bank entry and use the result.
  4. Else fall back to zeros (with a spdlog::warn).

Batch constraint

Any active recurrent memory frame forces batch_size = 1 via dl::constraints::computeBatchSizeConstraint(). When all recurrent positions are removed or deactivated, the constraint relaxes to the model’s batch_mode.

See Session Constraints.

Validation

At the start of runRecurrentSequence():

  • Throws if no active recurrent frames are configured.
  • Logs (does not throw) when memory_index exceeds sequence length.
  • Logs shape mismatches between the recurrent output slot and the per-element input shape at that position.

During assembleInputs(), unfilled sequence positions produce a stderr warning when filled_count < sequence_length.

UI: MemorySlotWidget

MemorySlotWidget provides one row per sequence position with a Frame kind combo (Static / Recurrent) and the appropriate AutoParamWidget form:

  • Static — StaticFrameSourceForm with static_source combo (DataManager / DataBank).
  • Recurrent — RecurrentFrameSourceForm with output slot and init mode.

Sequence slots expose Add Entry / Remove Last controls up to the model sequence length. Non-sequence memory slots (sequence_dim = -1) use a single position row.

See MemorySlotWidget.

Example configuration

For a NeuroSAM-style model with memory_images shape [5, 3, 256, 256]:

memory_index frame_kind Configuration
0 Static DataManager: video_key, frame offset 0
1 Static DataManager: video_key, frame offset 40
2 Static DataBank: memory_images_2 (captured reference)
3 Static DataManager: video_key, frame offset 120
4 Recurrent Output: decoder_output, Init: ZerosInit