IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Python class

QKVLinear

QKVLinear​

class max.nn.common_layers.linear.QKVLinear(in_dim, q_dim, kv_dim, *, bias=False, stacked=False)

source

Bases: TransparentModule[(<class ‘max.tensor.tensor.Tensor’>,), Tensor]

A fused q/k/v projection with two checkpoint layouts.

With stacked=False (the default) it holds separate q_proj/k_proj/v_proj Linear children and is name-transparent, so weights load under native <parent>.q_proj.weight names. With stacked=True it holds one pre-fused [q_dim + 2*kv_dim, in_dim] weight and stays opaque, keeping it under <parent>.qkv_proj.weight. Either way forward is a single matmul over the fused q||k||v weight.

Build a projection whose fused weight stacks a 16-row Q block and two 4-row K and V blocks:

from max.nn.common_layers.linear import QKVLinear

layer = QKVLinear(in_dim=8, q_dim=16, kv_dim=4)

Constructs a fused q/k/v linear projection.

Parameters:

  • in_dim (DimLike) – The input (hidden) dimension.
  • q_dim (int) – The Q projection output dimension.
  • kv_dim (int) – The K and V projection output dimension (each).
  • bias (bool) – Whether to use a bias in the transformation.
  • stacked (bool) – Store one pre-fused weight (opaque) instead of separate, name-transparent q_proj/k_proj/v_proj children.

forward()​

forward(x)

source

Applies the fused q/k/v projection: x @ fused_weight.T + bias.

Parameters:

x (Tensor)

Return type:

Tensor

fused_bias​

property fused_bias: Tensor | Literal[0]

source

The fused q||k||v bias, or 0 when bias is disabled.

fused_weight​

property fused_weight: Tensor

source

The fused q||k||v weight [q_dim + 2*kv_dim, in_dim].