PIXELBANKv9.1.0
Menu

CLS and Register Token Sequence Length

Problem Statement

A ViT prepends a learnable [CLS] token and, in newer designs (DINOv2, "registers"), a few extra register tokens to the patch sequence before the transformer. Compute the final sequence length fed to the encoder.

Background

The transformer input length is

L=Rpatchesâ‹…Cpatches+[CLS]+nregisterL = R_{\text{patches}} \cdot C_{\text{patches}} + [\text{CLS}] + n_{\text{register}}

The CLS token contributes exactly one slot when present; register tokens add n_register more. These extra tokens carry no image content but occupy real positions in the attention matrix, so they count toward compute.

Your Task

Implement:

def sequence_length(rows, cols, use_cls=True, n_register=0):

Return the total sequence length as an int.

Input Format

  • rows, cols (int): patch grid dimensions.
  • use_cls (bool): whether a CLS token is prepended.
  • n_register (int): number of register tokens.

Output Format

  • A single int.

Sample

print(sequence_length(14, 14, True, 0))

Output:

197

Example:

Input:
print(sequence_length(14, 14, True, 0))
Output:
197
Reasoning:
  • Calculate the number of image patch tokens by multiplying the grid dimensions: 14×14=19614 \times 14 = 196.
  • Determine the contribution of the CLS token; since use_cls is True, it adds exactly 1 slot to the sequence.
  • Add the number of register tokens; since n_register is 0, this adds 0 to the total.
  • Sum these components to find the total sequence length: 196+1+0=197196 + 1 + 0 = 197.
  • The final output is 197

Constraints:

  • 1 <= rows, cols <= 256, 0 <= n_register <= 64
  • Add 1 only when use_cls is true.
  • Return an int.
🔒

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

0/0
Run code to see test results.
CLS and Register Token Sequence Length - Easy | PixelBank