MLP Projector Parameter Count
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
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
print(projector_params(1024, 4096, 4096))
20979712
- Identify the layer dimensions and bias setting: , , , and
biasis True (default), so bias terms are included. - Calculate the parameter count for the first linear layer (): weights are and bias is , totaling .
- Calculate the parameter count for the second linear layer (): weights are and bias is , totaling .
- Sum the parameters from both layers to get the total projector size: .
- The final output is 20979712
Constraints
1 <= d_in, d_hidden, d_out <= 100000.- Each Linear contributes
in*outweights plusoutbiases whenbias. - Return an int.