diff --git a/configs.h b/configs.h index 7b420b5..e704664 100644 --- a/configs.h +++ b/configs.h @@ -37,7 +37,7 @@ static constexpr size_t kTopK = GEMMA_TOPK; struct ConfigGemma7B { static constexpr int kSeqLen = gcpp::kSeqLen; - static constexpr int kVocabSize = 256128; + static constexpr int kVocabSize = 256000; static constexpr int kLayers = 28; static constexpr int kModelDim = 3072; static constexpr int kFFHiddenDim = 16 * 3072 / 2; // = 24576 @@ -49,12 +49,12 @@ struct ConfigGemma7B { struct ConfigGemma2B { static constexpr int kSeqLen = gcpp::kSeqLen; - static constexpr int kVocabSize = 256128; + static constexpr int kVocabSize = 256000; static constexpr int kLayers = 18; static constexpr int kModelDim = 2048; static constexpr int kFFHiddenDim = 16 * 2048 / 2; // = 16384 static constexpr int kHeads = 8; - static constexpr int kKVHeads = 8; // TODO(austinvhuang): add MQA support + static constexpr int kKVHeads = 1; static constexpr int kQKVDim = 256; // query size == key size == value size static constexpr int kTopK = gcpp::kTopK; }; diff --git a/gemma.cc b/gemma.cc index a230bea..27e8921 100644 --- a/gemma.cc +++ b/gemma.cc @@ -72,12 +72,13 @@ template struct Layer { Layer() = default; static constexpr size_t kHeads = TConfig::kHeads; + static constexpr size_t kKVHeads = TConfig::kKVHeads; static constexpr size_t kModelDim = TConfig::kModelDim; static constexpr size_t kQKVDim = TConfig::kQKVDim; static constexpr size_t kFFHiddenDim = TConfig::kFFHiddenDim; static constexpr size_t kAttVecEinsumWSize = kHeads * kQKVDim * kModelDim; - // 3x for (query, key, value) - static constexpr size_t kQKVEinsumWSize = 3 * kHeads * kQKVDim * kModelDim; + static constexpr size_t kQKVEinsumWSize = + (kHeads + 2 * kKVHeads) * kQKVDim * kModelDim; // 2x for (gelu gating vector, gated vector) static constexpr size_t kGatingEinsumWSize = 2 * kFFHiddenDim * kModelDim; @@ -315,47 +316,47 @@ HWY_NOINLINE void Attention(size_t batch_start, size_t batch_idx, size_t layer, static constexpr size_t kModelDim = gcpp::Activations::kModelDim; static constexpr size_t kHeads = TConfig::kHeads; + static constexpr size_t kKVHeads = TConfig::kKVHeads; static const float kQueryScale = static_cast(1.0 / sqrt(static_cast(kQKVDim))); - pool.Run(0, kHeads, [&](const uint64_t head, size_t /*thread*/) HWY_ATTR { - // linear projections to QKV - const size_t head_offset = - 3 * kQKVDim * kModelDim; // 3x for QKV dimensions - const size_t q_offset = head * head_offset + 0 * kQKVDim * kModelDim; - const size_t k_offset = head * head_offset + 1 * kQKVDim * kModelDim; - const size_t v_offset = head * head_offset + 2 * kQKVDim * kModelDim; + const size_t batch_offset = batch_idx * kModelDim; + auto ProjQ = [&](uint64_t head, size_t head_offset) HWY_ATTR { float* HWY_RESTRICT q = activations.q.data() + head * kQKVDim + batch_idx * kHeads * kQKVDim; - const size_t batch_offset = batch_idx * kModelDim; - MatVecLoop( - c_layer->c_qkv_einsum_w, q_offset, + c_layer->c_qkv_einsum_w, head_offset + 0 * kQKVDim * kModelDim, activations.pre_att_rms_out.data() + batch_offset, q); + }; - const size_t kv_offset = - pos * kCachePosSize + layer * kCacheLayerSize + head * kQKVDim; + auto ProjKV = + [&](size_t k_offset, size_t v_offset, size_t kv_offset) HWY_ATTR { + TwoOfsMatVecLoop( + c_layer->c_qkv_einsum_w, k_offset, v_offset, + activations.pre_att_rms_out.data() + batch_offset, + kv_cache.key_cache.get() + kv_offset, + kv_cache.value_cache.get() + kv_offset); - TwoOfsMatVecLoop( - c_layer->c_qkv_einsum_w, k_offset, v_offset, - activations.pre_att_rms_out.data() + batch_offset, - kv_cache.key_cache.get() + kv_offset, - kv_cache.value_cache.get() + kv_offset); + Rope(kv_cache.key_cache.get() + kv_offset, kQKVDim, pos); + }; + auto Attn = [&](uint64_t head, size_t head_offset) HWY_ATTR { // Calculate scores + float* HWY_RESTRICT q = + activations.q.data() + head * kQKVDim + batch_idx * kHeads * kQKVDim; float* HWY_RESTRICT head_att = activations.att.data() + head * TConfig::kSeqLen + batch_idx * kHeads * kQKVDim; Rope(q, kQKVDim, pos); - Rope(kv_cache.key_cache.get() + kv_offset, kQKVDim, pos); MulByConst(kQueryScale, q, kQKVDim); + // Compute Q dot K scores for (size_t pos2 = 0; pos2 <= pos; ++pos2) { const size_t cache_offset = - pos2 * kCachePosSize + layer * kCacheLayerSize + head * kQKVDim; + pos2 * kCachePosSize + layer * kCacheLayerSize + head_offset; const float* HWY_RESTRICT k2 = kv_cache.key_cache.get() + cache_offset; const float score = Dot(q, k2, kQKVDim); head_att[pos2] = score; @@ -368,7 +369,7 @@ HWY_NOINLINE void Attention(size_t batch_start, size_t batch_idx, size_t layer, hwy::ZeroBytes(att_out, kQKVDim * sizeof(*att_out)); for (size_t pos2 = 0; pos2 <= pos; ++pos2) { const size_t cache_offset = - pos2 * kCachePosSize + layer * kCacheLayerSize + head * kQKVDim; + pos2 * kCachePosSize + layer * kCacheLayerSize + head_offset; float* HWY_RESTRICT v2 = kv_cache.value_cache.get() + cache_offset; MulByConstAndAdd(head_att[pos2], v2, att_out, kQKVDim); } @@ -381,7 +382,41 @@ HWY_NOINLINE void Attention(size_t batch_start, size_t batch_idx, size_t layer, MatVecLoop(c_layer->c_attn_vec_einsum_w, head * kModelDim * kQKVDim, att_out, head_out); - }); + }; + + if constexpr (kHeads == kKVHeads) { + // Multi-Head Attention + pool.Run(0, kHeads, [&](const uint64_t head, size_t /*thread*/) HWY_ATTR { + const size_t head_offset = head * 3 * kQKVDim * kModelDim; + + ProjQ(head, head_offset); + + const size_t k_offset = head_offset + 1 * kQKVDim * kModelDim; + const size_t v_offset = head_offset + 2 * kQKVDim * kModelDim; + const size_t kv_offset = + pos * kCachePosSize + layer * kCacheLayerSize + head * kQKVDim; + + ProjKV(k_offset, v_offset, kv_offset); + + Attn(head, head * kQKVDim); + }); + } else { + // Multi-Query Attention + pool.Run(0, kHeads, [&](const uint64_t head, size_t /*thread*/) HWY_ATTR { + ProjQ(head, head * kQKVDim * kModelDim); + }); + + constexpr const size_t q_offset = kHeads * kQKVDim * kModelDim; + constexpr const size_t k_offset = q_offset + 0 * kQKVDim * kModelDim; + constexpr const size_t v_offset = q_offset + 1 * kQKVDim * kModelDim; + const size_t kv_offset = pos * kCachePosSize + layer * kCacheLayerSize; + + ProjKV(k_offset, v_offset, kv_offset); + + pool.Run(0, kHeads, [&](const uint64_t head, size_t /*thread*/) HWY_ATTR { + Attn(head, 0); + }); + } // accumulate output across all heads into att_post2. head 0 already wrote // directly to att_post2. diff --git a/util/convert_weights.py b/util/convert_weights.py index bd6750a..0211c01 100644 --- a/util/convert_weights.py +++ b/util/convert_weights.py @@ -72,26 +72,12 @@ parser.add_argument( args = parser.parse_args() -def expand_qkv(qkv_proj: np.array) -> np.array: - """This won't be needed anymore when MQA is implemented""" - assert qkv_proj.shape == (2560, 2048) - qkv = qkv_proj.reshape((10, 256, 2048)) - - q_proj = qkv[:8].reshape((1,8,256,2048)) - kv_proj = qkv[8:] - kv_proj = kv_proj[:, np.newaxis, :, :] - kv_proj = np.repeat(kv_proj, 8, axis=1) - - qkv = np.concatenate([q_proj, kv_proj]) - qkv = np.transpose(qkv, axes=[1,0,2,3]) - return qkv - TRANSFORMATIONS = { "2b":defaultdict( lambda: lambda x: x, { - "embedder.weight": lambda x: np.concatenate([x, np.zeros([128, 2048])], 0), - "self_attn.qkv_proj.weight": expand_qkv, + "embedder.weight": lambda x: x, + "self_attn.qkv_proj.weight": lambda x: x.reshape((10, 256, 2048)), "self_attn.o_proj.weight": lambda x: x.reshape((2048, 8, 256)).transpose([1,0,2]), "mlp.gate_proj.weight": lambda x: x[np.newaxis, :, :], "mlp.up_proj.weight": lambda x: x[np.newaxis, :, :], @@ -101,7 +87,7 @@ TRANSFORMATIONS = { "7b":defaultdict( lambda: lambda x: x, { - "embedder.weight": lambda x: np.concatenate([x, np.zeros([128, 3072])], 0), + "embedder.weight": lambda x: x, "self_attn.qkv_proj.weight": lambda x: x.reshape((3, 16, 256, 3072)).transpose([1,0,2,3]), "self_attn.o_proj.weight": lambda x: x.reshape((3072, 16, 256)).transpose([1,0,2]), "mlp.gate_proj.weight": lambda x: x[np.newaxis, :, :], @@ -113,9 +99,9 @@ TRANSFORMATIONS = { VALIDATIONS = { "2b": { - "embedder.weight": lambda x: x.shape == (256128, 2048), + "embedder.weight": lambda x: x.shape == (256000, 2048), "model.norm.weight": lambda x: x.shape == (2048,), - "self_attn.qkv_proj.weight": lambda x: x.shape == (8, 3, 256, 2048), + "self_attn.qkv_proj.weight": lambda x: x.shape == (10, 256, 2048), "self_attn.o_proj.weight": lambda x: x.shape == (8, 2048, 256), "mlp.gate_proj.weight": lambda x: x.shape == (1, 16384, 2048), "mlp.up_proj.weight": lambda x: x.shape == (1, 16384, 2048), @@ -124,7 +110,7 @@ VALIDATIONS = { "post_attention_layernorm.weight": lambda x: x.shape == (2048,), }, "7b": { - "embedder.weight": lambda x: x.shape == (256128, 3072), + "embedder.weight": lambda x: x.shape == (256000, 3072), "model.norm.weight": lambda x: x.shape == (3072,), "self_attn.qkv_proj.weight": lambda x: x.shape == (16, 3, 256, 3072), "self_attn.o_proj.weight": lambda x: x.shape == (16, 3072, 256),