Skip to content
DeepForgeby svx
All problems
MediumDeep Learningdl-252

Relative Position Bias Index

Problem

Build the 1D relative position index matrix used by Swin's bias table: index[i][j] = (i - j) + seq_len - 1, which maps relative offsets to a bias-table row.

The signature is relative_position_bias_index(seq_len).

Hint

The index matrix lets one learned table cover every relative offset inside a window.

Test cases

case 1
in: [3]
expected: [[2,1,0],[3,2,1],[4,3,2]]
case 2
in: [1]
expected: [[0]]
case 3
in: [2]
expected: [[1,0],[2,1]]
case 4
in: [4]
expected: [[3,2,1,0],[4,3,2,1],[5,4,3,2],[6,5,4,3]]
Starter codePython
def relative_position_bias_index(seq_len):
    # Your code here
    pass

4 test cases · run your code against them in the editor