PIXELBANKv9.1.0
Menu

Positional Embedding Interpolation Count

Problem Statement

A ViT pretrained at one resolution is fine-tuned at another. Its learned positional embeddings must be interpolated from the old patch grid to the new one. Report how many position vectors exist before and after, keeping any prefix (CLS/register) tokens untouched.

Background

Positional embeddings come as num_prefix + old_grid2** vectors: a few non-spatial prefix tokens (CLS, registers) plus one per patch on a square old_grid x old_grid layout. When the image resolution changes, only the patch positions are bilinearly interpolated to the new_grid x new_grid layout; the prefix embeddings are copied through unchanged. So the new count is num_prefix + new_grid2**.

Your Task

Implement:

def interp_pos_count(total_old, new_grid, num_prefix=1):
  • total_old: total number of positional embeddings before interpolation.
  • Infer old_grid from total_old - num_prefix (it is a perfect square).
  • Return a dict with "old_grid", "new_grid", "total_new" (ints).

Input Format

  • total_old (int), new_grid (int), num_prefix (int).

Output Format

  • A dict of three ints.

Sample

print(interp_pos_count(197, 16, 1))

Output:

{'old_grid': 14, 'new_grid': 16, 'total_new': 257}

Example:

Input:
print(interp_pos_count(197, 16, 1))
Output:
{'old_grid': 14, 'new_grid': 16, 'total_new': 257}
Reasoning:
  • Subtract the number of prefix tokens from the total old embeddings to isolate the spatial patch count: 197−1=196197 - 1 = 196.
  • Determine the old grid dimension by taking the square root of the patch count, as the patches form a square grid: 196=14\sqrt{196} = 14.
  • Identify the new grid dimension directly from the input parameter: 1616.
  • Calculate the total number of new embeddings by adding the unchanged prefix tokens to the new spatial patch count: 1+162=1+256=2571 + 16^2 = 1 + 256 = 257.
  • The final output is {'old_grid': 14, 'new_grid': 16, 'total_new': 257}

Constraints:

  • total_old - num_prefix is a perfect square (the old grid).
  • Prefix embeddings pass through; only patch positions change.
  • total_new = num_prefix + new_grid**2.
🔒

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.
Positional Embedding Interpolation Count - Medium | PixelBank