research34 min studyLesson 794 in Engineering

Connector compatibility requires sequence, width, ownership, and mask contracts

Distinguish feature projection from learned resampling and cross-attention, then verify tensor shapes and visibility rules before model execution.

On this page

What you’ll understand

  • Derive projected-feature and cross-attention tensor shapes.
  • Calculate a materialized attention-score footprint under explicit assumptions.
  • Identify media-ownership, padding, and all-masked-row failures independently of shape matching.

A bridge between a vision encoder and a language model is more than a matrix that makes the last dimension fit. It must define how many features remain, which request owns them, which text positions may attend to them, and how missing or padded entries behave. LLaVA-style projection, BLIP-2 learned queries, and Flamingo-style resampling with cross-attention solve different interfaces. A shape-compatible tensor can still expose another example's media or future information through an incorrect mask. Treat each boundary as a testable contract.

Projection changes width while resampling can change length

A simple linear projector maps every visual feature from width d_v to language width d_t. Applied independently to each position, it keeps the visual sequence length unchanged. A learned-query module instead can produce a specified number of outputs by attending over the visual sequence, so it changes the length interface as well as the representation. Nonlinear projectors add further choices. Record the actual input and output shapes and feature-selection rule, including whether a class token is retained, before budgeting language positions or loading a connector checkpoint.

The original BLIP-2 experiments use 32 learned queries of width 768, but those are paper-specific architecture choices rather than universal constants. Likewise, a Flamingo cross-attention bridge is not interchangeable with a prefix projector merely because both connect frozen components. Matching widths does not establish a shared learned feature space or compatible training. Freeze policies, position encodings, normalization, and connector weights belong to the same validated artifact set. Test known media with its intended language interface rather than relying on successful tensor allocation as evidence of semantic compatibility.

Xv∈RB×Lv×dv,W∈Rdv×dt⟹XvW∈RB×Lv×dtX_v\in\mathbb R^{B\times L_v\times d_v},\quad W\in\mathbb R^{d_v\times d_t}\quad\Longrightarrow\quad X_vW\in\mathbb R^{B\times L_v\times d_t}
This is a positionwise linear width projection with compatible dimensions. It does not reduce L_v or specify training, normalization, positional information, padding, or ownership. B counts examples; sequence lengths and widths are element counts rather than bytes.↔ Scroll wide equations horizontally.

Cross-attention has a rectangular visibility matrix

With text queries and visual keys and values, attention scores have one axis for text positions and another for visual positions, separately for each example and head. Query and key head widths must match, while the value width determines the per-head output. The additive mask must broadcast to the intended axes and block padding or disallowed media. A mask with the right total number of elements but the wrong axis interpretation can silently expose another image or prevent the desired one from being read.

An all-masked query row has no valid probability distribution under the usual negative-infinity additive mask. Choose an explicit valid null-input path, skip rule, or rejection contract rather than accepting a NaN or treating arbitrary zeros as meaningful visual evidence. Interleaved architectures may restrict a text token to particular preceding media; a text causal mask alone does not encode that ownership. Test padding, multiple examples, and media-order changes with distinct content so a cross-example or cross-media leak becomes observable. Verify the actual library's Boolean and additive-mask conventions separately.

A=softmax⁡(QK⊤dk+M),Z=AVA=\operatorname{softmax}\left(\frac{QK^\top}{\sqrt{d_k}}+M\right),\qquad Z=AV
For one head, Q has shape L_t×d_k, K has L_v×d_k, V has L_v×d_value, and A has L_t×L_v. Softmax runs across valid visual keys for each query. M uses compatible additive logits and must leave at least one valid key for this formula. Batched/head axes and implementation mask conventions require explicit handling.↔ Scroll wide equations horizontally.

Probe shapes and visibility before a performance claim

Build tiny examples with different media per batch row, padded visual positions, and a known allowed media boundary. Assert shape invariants and perturb only a blocked input to test whether output changes unexpectedly. Such tests diagnose interface leakage but do not prove all learned reasoning is grounded. For memory analysis, distinguish a hypothetical materialized score matrix from fused or tiled implementations that avoid storing all scores. Measure actual peak allocation, kernel behavior, and output equivalence before using a shape formula to predict a deployment's capacity or speed.

The matrix lab shows one fixed two-dimensional linear family, not a connector implementation. Reset, set both a=1 and b=0, predict determinant 1 and condition number 1, then Play. Reset and set both a=1 and b=1 for determinant 1 and condition number(3+√5)/2≈2.618. The selected transform is[[a,b],[0,1]], and readouts describe that final matrix while geometry interpolates. It has no batch, token length, learned weights, attention, media ownership, or masks. Use the analogy only to separate a linear transform from the additional interface semantics required in the real bridge.

A rectangular score tensor has an explicit storage scope

A hypothetical cross-attention layer has batch 2, four heads,16 text query positions,64 visual key positions, and head width 32 for queries, keys, and values. Assume score and value tensors use two bytes per scalar and scores are fully materialized. Exclude gradients, softmax copies, and other intermediates.

  1. The score tensor contains 2×4×16×64=8192 values, taking 16384 bytes=16 KiB. Its text and visual axes differ; this is not a 16×16 text self-attention matrix.

  2. The per-head attention output has 2×4×16×32=4096 values, taking 8192 bytes=8 KiB before concatenating heads. Query count determines its sequence axis, not the number of visual keys.

  3. Each key or value tensor has 2×4×64×32=16384 values, taking 32 KiB, so keys and values together take 64 KiB. These scoped counts do not establish actual layer peak memory or the footprint of a fused attention kernel.

The result

Shape and byte accounting are concrete, while mask correctness, semantic compatibility, and implementation memory behavior need separate tests.

Explore the model

Interactive lab

Interactive lab · matrix

A linear geometry example does not supply attention semantics

Reset, set both a=1 and b=0, predict determinant 1 and condition 1, then Play. Reset and re-enter both a=1 and b=1 for determinant 1 and condition(3+√5)/2≈2.618. The family is only[[a,b],[0,1]]; readouts describe the selected final matrix during interpolation. It has no connector sequence, attention heads, learned representation, padding, or media mask.

A linear geometry example does not supply attention semanticsThe selected matrix is A = [[a,b],[0,1]]. Playback interpolates geometry between the identity and A; readouts always describe A. Its determinant is a and its singular values come from AᵀA. Positive a makes every supported matrix invertible. This two-dimensional family is not a general eigensolver or a learned network. Current readings: Area scale · det A: 1.4; Singular values · σmax, σmin: 1.597, 0.876; Condition number · κ₂(A): 1.823. All parameters are adjustable with the labeled controls below.Matrix A with first row 1.4, 0.6 and second row zero, one; display interpolates between identity and A while final determinant remains 1.4.−4−2024−2−1012x · equal geometric scaleyA = [ 1.4 0.6 ; 0 1 ]Display blend: 0% · final values below
Interpolated geometryOriginal unit squareSelected final transform
Area scale · det A
1.4
Singular values · σmax, σmin
1.597, 0.876
Condition number · κ₂(A)
1.823
t = 0 s

The selected matrix is A = [[a,b],[0,1]]. Playback interpolates geometry between the identity and A; readouts always describe A. Its determinant is a and its singular values come from AᵀA. Positive a makes every supported matrix invertible. This two-dimensional family is not a general eigensolver or a learned network. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forMatching a transform's dimensions is necessary but does not establish the complete modality interface.

Check your understanding

A positionwise projector maps visual width 1024 to language width 4096. What follows about its token count?

Try it yourself

Batch 1, two heads, eight text positions, twenty visual positions, and two-byte materialized scores are used in a toy attention layer. Compute score elements and bytes. If every visual key for one query is blocked with negative infinity, is the usual row softmax defined?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Multiply batch, heads, queries, and keys; then examine the denominator when no valid key remains.

Compare with the solution

There are 1×2×8×20=320 score values, or 640 bytes. An all-blocked row has no valid normalized attention distribution under the stated formulation. Handle it through the declared null/skip/reject path; neither the valid tensor shape nor the small memory count makes that row semantically usable.

Take it with you

  • Width projection and sequence resampling solve different interface problems.
  • Cross-attention needs correct axes, valid keys, padding, and media ownership.
  • Materialized shape counts do not replace actual memory measurements or grounding tests.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.