In large language models (LLMs), weight matrices are often but not always square. The shape of the weight matrices depends on the specific layer and its purpose within the model. Let's break down a few common types of layers you might encounter in an LLM and their typical weight matrices:
Transformer's Self-Attention Layer
- Query, Key, and Value Matrices (Wq, Wk, Wv): These matrices map the input tokens (with dimension usually equal to the model's hidden size) to the query, key, and value vectors.
[
\mathbf{Q} = \mathbf{X} \mathbf{W_q}, \quad \mathbf{K} = \mathbf{X} \mathbf{W_k}, \quad \mathbf{V} = \mathbf{X} \mathbf{W_v}
]
If the model's hidden size is (d_{model}) and the attention head size is (d_{head}), then (\mathbf{W_q}), (\mathbf{W_k}), and (\mathbf{W_v}) are typically of shape ((d_{model}, d_{head})). These matrices are often not square.
Output Projection in Self-Attention
- Output Weight Matrix (Wo): After computing the attention, the concatenated outputs of the multiple attention heads are projected back using a weight matrix.
[
\mathbf{O} = \mathbf{ZH} \mathbf{W_o}
]
If there are (n) heads and each head's dimension is (d_{head}), then typically (\mathbf{W_o}) is of shape ((n \times d_{head}, d_{model})). This matrix is often not square.
Feed-Forward Network
- Intermediate and Output Matrices: In the feed-forward network (FFN) within a transformer block, there are typically two linear transformations.
[
\mathbf{H}{ffn} = \max(0, \mathbf{X} \mathbf{W_1} + \mathbf{b_1}) \mathbf{W_2} + \mathbf{b_2}
]
Here, (\mathbf{W_1}) might have shape ((d{model}, d_{ff})) and (\mathbf{W_2}) could have shape ((d_{ff}, d_{model})). If (d_{ff}) (the intermediate size) is different from (d_{model}), these matrices are not square.
Embedding Layers
- Token and Positional Embedding Matrices: These matrices map input tokens and positions to dense vectors.
- Token embeddings are typically of shape ((vocab_size, d_{model})).
- Positional embeddings are of shape ((max_position, d_{model})).
These are generally not square.
Layer Normalization and Bias Terms
- Layer Norm Weights and Biases: These parameters are vectors of size (d_{model}) and are thus not matrices, square or otherwise.
Context from Provided Equation
From your provided context, the matrices being rotated and transformed seem to be specific to the architecture you are working on. If they are involved in 2D transformations as shown:
[
\begin{pmatrix}
\cos m\theta & -\sin m\theta \
\sin m\theta & \cos m\theta
\end{pmatrix}
]
This is a 2x2 rotation matrix, which is indeed square, but this is more about the geometric transformation rather than typical LLM weight matrices.
Conclusion
In summary, while some weight matrices can be square (such as in batch normalization and occasionally in other linear transformations), many of the weight matrices in large language models are not necessarily square and are designed according to the specific input/output dimensions they need to handle.