v9.1.1
Menu

Bidirectional Scan for Vision

mediumPROVision MambaArray, AlgorithmsAdvanced Architectures

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 x=[x1,x2,...,xN]x = [x_1, x_2, ..., x_N]:

  • 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

Input
x = [1.0, 2.0, 3.0, 4.0]
Output
[11.0, 12.0, 13.0, 14.0]
Reasoning
  • Input: x is a 1D sequence of N=4N = 4 patch values, x=[1,2,3,4]x = [1, 2, 3, 4]. The output has the same length: one fused value per patch.
  • Forward scan: position ii holds the running total from the left end up to and including xix_i: 11, 1+2=31+2=3, 3+3=63+3=6, 6+4=106+4=10, giving [1,3,6,10][1, 3, 6, 10].
  • Backward scan: position ii holds the total from xix_i to the right end: 44, 3+4=73+4=7, 2+7=92+7=9, 1+9=101+9=10, so read left to right it is [10,9,7,4][10, 9, 7, 4].
  • Fusion: add the two scans position by position: 1+10=111+10=11, 3+9=123+9=12, 6+7=136+7=13, 10+4=1410+4=14, which is the output [11.0, 12.0, 13.0, 14.0].
  • Notice that xix_i is the only element counted by both scans, so each output equals ∑jxj+xi=10+xi\sum_j x_j + x_i = 10 + x_i. Every patch sees the whole sequence, plus an extra copy of itself. That is why an all-ones input of length 4 gives 4+1=54 + 1 = 5 everywhere.

Constraints

  • Sequence length NN: 1≤N≤10001 \leq N \leq 1000
  • Values are floats

More from Advanced Architectures

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.