IA3 › Worked Example: Computing IA3-Adapted Attention

Michael BrenndoerferDecember 6, 20256 min read
Language AI HandbookAll chapters
Part 35 of 67Parameter-Efficient Fine-tuning
Chapter 261IA3 › Worked Example: Computing IA3-Adapted Attention
Reading section 5 of 11 · Worked Example: Computing IA3-Adapted Attention · 6 minSection 5 of 11 · 6 min

Book contents

Section 5 of 11 · 6 min read

IA3 › Worked Example: Computing IA3-Adapted AttentionOne section · Sections in this chapter5 / 11 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's work through a concrete numerical example to solidify the mechanics. We will trace exactly how IA3 modifies a small attention computation, showing every step of the calculation.

Consider a tiny transformer with hidden dimension d=4d = 4, processing a sequence of 3 tokens. The input matrix XX and frozen weight matrices WKW_K and WVW_V are:

X=[101001011100]WK=[0.50.10.20.00.00.50.10.30.20.00.50.10.10.30.00.5]WV=I4X = \begin{bmatrix} 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ 1 & 1 & 0 & 0 \end{bmatrix} \quad W_K = \begin{bmatrix} 0.5 & 0.1 & 0.2 & 0.0 \\ 0.0 & 0.5 & 0.1 & 0.3 \\ 0.2 & 0.0 & 0.5 & 0.1 \\ 0.1 & 0.3 & 0.0 & 0.5 \end{bmatrix} \quad W_V = I_4

For simplicity, we set WV=I4W_V = I_4 (the identity matrix), so value vectors equal the input embeddings. The IA3 rescaling vector for keys is:

ℓK=[2.00.51.50.3]\ell_K = \begin{bmatrix} 2.0 & 0.5 & 1.5 & 0.3 \end{bmatrix}

Step 1: Compute the standard key matrix. Multiply XX by WKW_K:

K=XWK=[0.70.10.70.10.10.80.10.80.50.60.30.4]K = X W_K = \begin{bmatrix} 0.7 & 0.1 & 0.7 & 0.1 \\ 0.1 & 0.8 & 0.1 & 0.8 \\ 0.5 & 0.6 & 0.3 & 0.4 \end{bmatrix}

Each row is the key vector for one of the 3 tokens.

Step 2: Apply IA3 rescaling. Multiply each element of KK by the corresponding entry of ℓK\ell_K. Since ℓK=[2.0,0.5,1.5,0.3]\ell_K = [2.0, 0.5, 1.5, 0.3]:

K′=ℓK⊙K=[2.0×0.70.5×0.11.5×0.70.3×0.12.0×0.10.5×0.81.5×0.10.3×0.82.0×0.50.5×0.61.5×0.30.3×0.4]=[1.40.051.050.030.20.40.150.241.00.30.450.12]K' = \ell_K \odot K = \begin{bmatrix} 2.0 \times 0.7 & 0.5 \times 0.1 & 1.5 \times 0.7 & 0.3 \times 0.1 \\ 2.0 \times 0.1 & 0.5 \times 0.8 & 1.5 \times 0.1 & 0.3 \times 0.8 \\ 2.0 \times 0.5 & 0.5 \times 0.6 & 1.5 \times 0.3 & 0.3 \times 0.4 \end{bmatrix} = \begin{bmatrix} 1.4 & 0.05 & 1.05 & 0.03 \\ 0.2 & 0.4 & 0.15 & 0.24 \\ 1.0 & 0.3 & 0.45 & 0.12 \end{bmatrix}

Notice the effect: dimensions 1 and 3 (indices 0 and 2), which had large initial values, are amplified further. Dimensions 2 and 4 (indices 1 and 3) are compressed. The first token's key has been pulled strongly toward dimensions 1 and 3, while its contributions from dimensions 2 and 4 are nearly suppressed.

Step 3: Observe the effect on attention scores. Suppose we compute a simple self-attention score for the first token querying itself (ignoring the multi-head structure for clarity). If the query for token 1 is q=[0.7,0.1,0.7,0.1]q = [0.7, 0.1, 0.7, 0.1] (the same as its unscaled key vector for this example), then:

Without IA3: q⋅k1=0.72+0.12+0.72+0.12=0.49+0.01+0.49+0.01=1.00q \cdot k_1 = 0.7^2 + 0.1^2 + 0.7^2 + 0.1^2 = 0.49 + 0.01 + 0.49 + 0.01 = 1.00

With IA3: q⋅k1′=0.7×1.4+0.1×0.05+0.7×1.05+0.1×0.03=0.98+0.005+0.735+0.003=1.723q \cdot k_1' = 0.7 \times 1.4 + 0.1 \times 0.05 + 0.7 \times 1.05 + 0.1 \times 0.03 = 0.98 + 0.005 + 0.735 + 0.003 = 1.723

The amplified dimensions 1 and 3 contribute much more to the attention score after IA3. This larger score makes token 1 appear more needed to itself. Meanwhile, the other tokens with less activation in dimensions 1 and 3 will see their keys relatively less scaled, changing the relative attention distribution.

Step 4: Count the parameters. For this toy example with d=4d = 4 and dff=16d_{ff} = 16 (assuming the 4×4\times expansion), IA3 introduces:

  • ∣ℓK∣=4|\ell_K| = 4 parameters
  • ∣ℓV∣=4|\ell_V| = 4 parameters
  • ∣ℓff∣=16|\ell_{ff}| = 16 parameters
  • Total: 24 parameters

The frozen weight matrices WKW_K and WVW_V together contain 4×4+4×4=324 \times 4 + 4 \times 4 = 32 parameters, plus WQW_Q and the output projection WOW_O would add another 32. In this toy scale, IA3 adds 24 parameters to a block with 64 frozen parameters: 37% overhead. At real scales (LLaMA-7B with d=4096d = 4096), this ratio shrinks sharply. The frozen weights per layer include four 4096×40964096 \times 4096 matrices totaling 67 million parameters, while IA3 adds only about 19,000 parameters, which is 0.028% overhead.

Out[13]:
Visualization
Bar chart of attention weights for six tokens (The, movie, was, absolutely, terrible, period) in blue, showing a nearly uniform distribution with all tokens receiving roughly equal attention around 0.15 to 0.2.
Attention weights before adaptation when the token 'terrible' queries the context. The nearly uniform distribution across all six positions shows the model's general pretraining objective, which did not specifically train the model to focus on sentiment-bearing words in this setup.
Bar chart of attention weights for the same six tokens in coral after IA3 adaptation, with absolutely receiving approximately 0.40 and movie approximately 0.27, while most other tokens remain near 0.1 or below.
Attention weights after IA3 adaptation for a sentiment analysis task. The word 'terrible' now allocates about 40 percent of attention to the intensifier 'absolutely' and 27 percent to 'movie', while assigning much lower weights to the structural tokens. IA3 key rescaling reshaped the attention pattern without modifying any weight matrices, with selective amplification of sentiment-needed key dimensions.