PIXELBANKv9.1.0
Menu

Problem Statement

LLaVA-1.5 replaced its single linear projector with a two-layer MLP that maps vision features into the language model's embedding space. Count the trainable parameters of that projector.

Background

A two-layer MLP projector is Linear(d_in, d_hidden) -> GELU -> Linear(d_hidden, d_out). A Linear(a, b) layer with bias has a*b + b parameters (weights plus one bias per output). GELU has none. So the total is

(din dhidden+dhidden)+(dhidden dout+dout)(d_{in}\, d_{hidden} + d_{hidden}) + (d_{hidden}\, d_{out} + d_{out})

Your Task

Implement:

def projector_params(d_in, d_hidden, d_out, bias=True):

Return the total parameter count as an int. When bias is false, drop the bias terms.

Input Format

  • d_in, d_hidden, d_out (int): layer widths.
  • bias (bool): whether the Linear layers have biases.

Output Format

  • A single int.

Sample

print(projector_params(1024, 4096, 4096))

Output:

20979712

Example:

Input:
print(projector_params(1024, 4096, 4096))
Output:
20979712
Reasoning:
  • Identify the layer dimensions and bias setting: din=1024d_{in} = 1024, dhidden=4096d_{hidden} = 4096, dout=4096d_{out} = 4096, and bias is True (default), so bias terms are included.
  • Calculate the parameter count for the first linear layer (din→dhiddend_{in} \to d_{hidden}): weights are 1024×4096=4,194,3041024 \times 4096 = 4{,}194{,}304 and bias is 40964096, totaling 4,198,4004{,}198{,}400.
  • Calculate the parameter count for the second linear layer (dhidden→doutd_{hidden} \to d_{out}): weights are 4096×4096=16,777,2164096 \times 4096 = 16{,}777{,}216 and bias is 40964096, totaling 16,781,31216{,}781{,}312.
  • Sum the parameters from both layers to get the total projector size: 4,198,400+16,781,312=20,979,7124{,}198{,}400 + 16{,}781{,}312 = 20{,}979{,}712.
  • The final output is 20979712

Constraints:

  • 1 <= d_in, d_hidden, d_out <= 100000.
  • Each Linear contributes in*out weights plus out biases when bias.
  • 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.