[B, 1, 768]to [B, 1, 768]
decode · cached positions · keys read · KV cache: appends position
Specification
- KV cache
- 72 KiB per position, all layers2 (K, V) × 12 layers × 12 heads × 64 × 4 bytes. After this call, at 1,001 positions: 70.4 MiB in use per sequence. At the full 1,024 positions: 72 MiB per sequence. Float32: the released code creates its weights without a dtype.model.py 102–108, 132–133, 152–155config.json
Parameters2,362,368 × 12 layers
2,362,368 parameters per instance, across its learned components
| Component | Calculation | Parameters |
|---|---|---|
| Fused Q, K, V projection | 768 × 2,304 + 2,304 | 1,771,776 |
| Attention output projection | 768 × 768 + 768 | 590,592 |
| Per-instance total | 2,362,368 | |
One in each of the 12 layers, each with its own weights.
Not checked against a checkpoint.
Sources
In codeattn(norm(x, 'ln_1'), 'attn', nx, past=past, hparams=hparams)
Direct evidence
Original fused QKV projection, causal mask, head split, and output projection.
model.py 69–112 ↗ · openai/gpt-2 @ 9b63575ef427
Excerpt
def attn(x, scope, n_state, *, past, hparams):
assert x.shape.ndims == 3 # Should be [batch, sequence, features]
assert n_state % hparams.n_head == 0
if past is not None:
assert past.shape.ndims == 5 # Should be [batch, 2, heads, sequence, features], where 2 is [k, v]
def split_heads(x):
# From [batch, sequence, features] to [batch, heads, sequence, features]
return tf.transpose(split_states(x, hparams.n_head), [0, 2, 1, 3])
def merge_heads(x):
# Reverse of split_heads
return merge_states(tf.transpose(x, [0, 2, 1, 3]))
def mask_attn_weights(w):
# w has shape [batch, heads, dst_sequence, src_sequence], where information flows from src to dst.
_, _, nd, ns = shape_list(w)
b = attention_mask(nd, ns, dtype=w.dtype)
b = tf.reshape(b, [1, 1, nd, ns])
w = w*b - tf.cast(1e10, w.dtype)*(1-b)
return w
def multihead_attn(q, k, v):
# q, k, v have shape [batch, heads, sequence, features]
w = tf.matmul(q, k, transpose_b=True)
w = w * tf.rsqrt(tf.cast(v.shape[-1].value, w.dtype))
w = mask_attn_weights(w)
w = softmax(w)
a = tf.matmul(w, v)
return a
with tf.variable_scope(scope):
c = conv1d(x, 'c_attn', n_state*3)
q, k, v = map(split_heads, tf.split(c, 3, axis=2))
present = tf.stack([k, v], axis=1)
if past is not None:
pk, pv = tf.unstack(past, axis=1)
k = tf.concat([pk, k], axis=-2)
v = tf.concat([pv, v], axis=-2)
a = multihead_attn(q, k, v)
a = merge_heads(a)
a = conv1d(a, 'c_proj', n_state)
return a, presentTransformers can select an optimized attention backend while retaining the same projections and causal contract.
modeling_gpt2.py 185–224 ↗ · huggingface/transformers @ 71c6f699ac9b
Excerpt
query_states, key_states, value_states = self.c_attn(hidden_states).split(self.split_size, dim=2)
shape_kv = (*key_states.shape[:-1], -1, self.head_dim)
key_states = key_states.view(shape_kv).transpose(1, 2)
value_states = value_states.view(shape_kv).transpose(1, 2)
shape_q = (*query_states.shape[:-1], -1, self.head_dim)
query_states = query_states.view(shape_q).transpose(1, 2)… lines 192–203 omitted …
)
if using_eager and self.reorder_and_upcast_attn:
attn_output, attn_weights = self._upcast_and_reordered_attn(
query_states, key_states, value_states, attention_mask
)
else:
attn_output, attn_weights = attention_interface(
self,
query_states,
key_states,
value_states,
attention_mask,
dropout=self.attn_dropout.p if self.training else 0.0,
scaling=self.scaling,
**kwargs,
)
attn_output = attn_output.reshape(*attn_output.shape[:-2], -1).contiguous()
attn_output = self.c_proj(attn_output)
attn_output = self.resid_dropout(attn_output)