Bidirectional Scan for Vision
Vision Mamba processes image patches bidirectionally to capture both forward and backward context, unlike the causal (left-to-right only) scanning in language models.
For a 1D sequence :
- Forward scan: Cumulative sum from left to right
- Backward scan: Cumulative sum from right to left
- Output: Element-wise sum of forward and backward scans
This bidirectional approach ensures each patch attends to information from both directions, crucial for non-causal tasks like image classification.
Task: Implement the bidirectional scan fusion.
Example
x = [1.0, 2.0, 3.0, 4.0]
[11.0, 12.0, 13.0, 14.0]
- Input:
xis a 1D sequence of patch values, . The output has the same length: one fused value per patch. - Forward scan: position holds the running total from the left end up to and including : , , , , giving .
- Backward scan: position holds the total from to the right end: , , , , so read left to right it is .
- Fusion: add the two scans position by position: , , , , which is the output
[11.0, 12.0, 13.0, 14.0]. - Notice that is the only element counted by both scans, so each output equals . Every patch sees the whole sequence, plus an extra copy of itself. That is why an all-ones input of length 4 gives everywhere.
Constraints
- Sequence length :
- Values are floats