Skip to content

Bamba architecture #10810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 43 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
1f0fea7
llama : initial Mamba-2 support
compilade Aug 1, 2024
dceff23
ggml : SIMD ggml_ssm_scan for Mamba-2
compilade Aug 19, 2024
2bfe9de
llama : support running Mamba-Codestral-7B-v0.1
compilade Aug 19, 2024
aff9692
llama : fix Mamba-2 conv state saving
compilade Aug 21, 2024
e04910d
llama : remove unused variable
compilade Aug 22, 2024
fa358e7
llama : add missing break
compilade Aug 22, 2024
38913dc
convert_hf : prefer SentencePiece tokenizer for Mamba-2 when present
compilade Aug 22, 2024
0e601ca
Merge branch 'master' into compilade/mamba2
compilade Sep 18, 2024
273e7a4
llama : avoid redundant state copy for Mamba 1 and 2
compilade Sep 30, 2024
7d6cb36
Merge branch 'master' into compilade/mamba2
compilade Oct 1, 2024
2c77d79
metal : attempt to adapt SSM_SCAN for Mamba-2
compilade Oct 2, 2024
87b97d0
metal : fix SSM_SCAN pipeline scope
compilade Oct 2, 2024
03d0e6e
metal : use log and exp instead of log1pf and expf in SSM_SCAN
compilade Oct 2, 2024
7a351ab
metal : remove unused arguments for SSM_SCAN
compilade Oct 2, 2024
8b15bc6
metal : add back n_seqs to SSM_SCAN args
compilade Oct 2, 2024
5b8ec2b
metal : fix SSM_SCAN state head offset
compilade Oct 2, 2024
62b09b3
metal : fix wrong number of tokens per sequence in SSM_SCAN
compilade Oct 3, 2024
038d958
Merge branch 'master' into compilade/mamba2
compilade Oct 12, 2024
805512a
ggml : remove unused fast broadcast path in GGML_MUL
compilade Oct 12, 2024
7d16e1b
Merge branch 'master' into compilade/mamba2
compilade Nov 1, 2024
3bc7103
ggml : avoid multiply by D in GGML_OP_SSM_SCAN
compilade Nov 4, 2024
8d8f065
Merge branch 'master' into compilade/mamba2
compilade Nov 4, 2024
b4e9c59
convert : fix flake8 lint
compilade Nov 4, 2024
1ee6c48
Merge branch 'master' into compilade/mamba2
compilade Nov 25, 2024
9a68f75
feat(jamba): First pass at GGUF conversion for Jamba models
gabe-l-hart Nov 26, 2024
246dfdb
feat(jamba): Add jamba architecture to llama.cpp enums
gabe-l-hart Nov 26, 2024
e3525e9
feat(convert): Full pass at hparam conversion
gabe-l-hart Dec 2, 2024
fd98682
fix(bamba conv): Jamba -> Bamba
gabe-l-hart Dec 3, 2024
1c1e008
fix(bamba): Jamba->Bamba in llama.cpp
gabe-l-hart Dec 3, 2024
e0af809
feat(bamba): hparam parsing in llama.cpp
gabe-l-hart Dec 3, 2024
fd3bb30
fix(bamba conv): Fizes in tensor name and hparam conversion for llama…
gabe-l-hart Dec 4, 2024
3ee0ae3
feat(bamba): Full tensor parsing for bamba
gabe-l-hart Dec 4, 2024
dfe8d3d
fix(bamba conv): Remove chunk size and consolidate head count w/ time…
gabe-l-hart Dec 5, 2024
41fc019
fix(bamba): Remove ssm_head_count and ssm_chunk_size in llama.cpp
gabe-l-hart Dec 5, 2024
e7b1abb
feat(bamba): Partially complete work on constructing the forward graph
gabe-l-hart Dec 5, 2024
f2478bc
fix: Get n_head_kv per-layer in build_bamba
gabe-l-hart Dec 9, 2024
d3a34e0
fix: per-layer recurrent embd_[kv]_s
gabe-l-hart Dec 9, 2024
92653d0
WIP: Partial work towards separate hybrid cache
gabe-l-hart Dec 9, 2024
44bf431
fix: Only allocate kv cache tensors for the appropriate layers in hyb…
gabe-l-hart Dec 10, 2024
4543ed5
feat: Update the logic in llama_decode_internal for kv_hybrid cache
gabe-l-hart Dec 10, 2024
204e78f
fix: A number of places where hybrid needs to be handled
gabe-l-hart Dec 10, 2024
97e6ba8
fix: Remove outdated TODO in convrsion script
gabe-l-hart Dec 12, 2024
b83e9a6
fix: Remove unused LLM_KV_ATTENTION_LAYER_COUNT
gabe-l-hart Dec 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(bamba): hparam parsing in llama.cpp
Branch: BambaArchitecture

Signed-off-by: Gabe Goodhart <ghart@us.ibm.com>
  • Loading branch information
gabe-l-hart committed Dec 12, 2024
commit e0af809b05bb365f8c080c9dc99645f44751706d
80 changes: 74 additions & 6 deletions src/llama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ enum llm_kv {
LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT,
LLM_KV_ATTENTION_SLIDING_WINDOW,
LLM_KV_ATTENTION_SCALE,
LLM_KV_ATTENTION_LAYER_COUNT,
LLM_KV_ATTENTION_LAYER_INDICES,

LLM_KV_ROPE_DIMENSION_COUNT,
LLM_KV_ROPE_FREQ_BASE,
Expand All @@ -331,6 +333,11 @@ enum llm_kv {
LLM_KV_SSM_TIME_STEP_RANK,
LLM_KV_SSM_GROUP_COUNT,
LLM_KV_SSM_DT_B_C_RMS,
LLM_KV_SSM_HEAD_COUNT,
LLM_KV_SSM_HEAD_DIM,
LLM_KV_SSM_CHUNK_SIZE,
LLM_KV_SSM_CONV_BIAS,
LLM_KV_SSM_PROJ_BIAS,

LLM_KV_WKV_HEAD_SIZE,

Expand Down Expand Up @@ -427,6 +434,7 @@ static const std::map<llm_kv, const char *> LLM_KV_NAMES = {
{ LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, "%s.attention.relative_buckets_count" },
{ LLM_KV_ATTENTION_SLIDING_WINDOW, "%s.attention.sliding_window" },
{ LLM_KV_ATTENTION_SCALE, "%s.attention.scale" },
{ LLM_KV_ATTENTION_LAYER_INDICES, "%s.attention.layer_indices" },

{ LLM_KV_ROPE_DIMENSION_COUNT, "%s.rope.dimension_count" },
{ LLM_KV_ROPE_FREQ_BASE, "%s.rope.freq_base" },
Expand All @@ -448,6 +456,11 @@ static const std::map<llm_kv, const char *> LLM_KV_NAMES = {
{ LLM_KV_SSM_TIME_STEP_RANK, "%s.ssm.time_step_rank" },
{ LLM_KV_SSM_GROUP_COUNT, "%s.ssm.group_count" },
{ LLM_KV_SSM_DT_B_C_RMS, "%s.ssm.dt_b_c_rms" },
{ LLM_KV_SSM_HEAD_COUNT, "%s.ssm.head_count" },
{ LLM_KV_SSM_HEAD_DIM, "%s.ssm.head_dim" },
{ LLM_KV_SSM_CHUNK_SIZE, "%s.ssm.chunk_size" },
{ LLM_KV_SSM_CONV_BIAS, "%s.ssm.conv_bias" },
{ LLM_KV_SSM_PROJ_BIAS, "%s.ssm.proj_bias" },

{ LLM_KV_WKV_HEAD_SIZE, "%s.wkv.head_size" },

Expand Down Expand Up @@ -2471,12 +2484,20 @@ struct llama_hparams {
float rope_yarn_log_mul;

// for State Space Models
uint32_t ssm_d_conv = 0;
uint32_t ssm_d_inner = 0;
uint32_t ssm_d_state = 0;
uint32_t ssm_dt_rank = 0;
uint32_t ssm_n_group = 0;
bool ssm_dt_b_c_rms = false;
uint32_t ssm_d_conv = 0;
uint32_t ssm_d_inner = 0;
uint32_t ssm_d_state = 0;
uint32_t ssm_dt_rank = 0;
uint32_t ssm_n_group = 0;
bool ssm_dt_b_c_rms = false;
uint32_t ssm_head_count = 0;
uint32_t ssm_head_dim = 0;
uint32_t ssm_chunk_size = 0;
bool ssm_conv_bias = false;
bool ssm_proj_bias = false;

// for hybrid state space models
std::array<bool, LLAMA_MAX_LAYERS> ssm_layer_arr;

float f_clamp_kqv = 0.0f;
float f_max_alibi_bias = 0.0f;
Expand Down Expand Up @@ -2533,6 +2554,13 @@ struct llama_hparams {
if (this->ssm_dt_rank != other.ssm_dt_rank) return true;
if (this->ssm_n_group != other.ssm_n_group) return true;
if (this->ssm_dt_b_c_rms != other.ssm_dt_b_c_rms) return true;
if (this->ssm_head_count != other.ssm_head_count) return true;
if (this->ssm_head_dim != other.ssm_head_dim) return true;
if (this->ssm_chunk_size != other.ssm_chunk_size) return true;
if (this->ssm_conv_bias != other.ssm_conv_bias) return true;
if (this->ssm_proj_bias != other.ssm_proj_bias) return true;

if (this->ssm_layer_arr != other.ssm_layer_arr) return true;

if (this->rescale_every_n_layers != other.rescale_every_n_layers) return true;
if (this->time_mix_extra_dim != other.time_mix_extra_dim) return true;
Expand Down Expand Up @@ -2625,6 +2653,10 @@ struct llama_hparams {
return ssm_d_state * ssm_d_inner;
}
}

bool ssm_layer(uint32_t il) const {
return ssm_layer_arr[il];
}
};

static_assert(std::is_trivially_copyable<llama_hparams>::value, "llama_hparams must be trivially copyable");
Expand Down Expand Up @@ -5492,6 +5524,7 @@ static void llm_load_hparams(
std::fill(hparams.n_head_arr.begin(), hparams.n_head_arr.end(), 0);
std::fill(hparams.n_head_kv_arr.begin(), hparams.n_head_kv_arr.end(), 0);
std::fill(hparams.n_ff_arr.begin(), hparams.n_ff_arr.end(), 0);
std::fill(hparams.ssm_layer_arr.begin(), hparams.ssm_layer_arr.end(), false);

ml.get_key_or_arr(LLM_KV_FEED_FORWARD_LENGTH, hparams.n_ff_arr, hparams.n_layer);
ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT, hparams.n_head_arr, hparams.n_layer);
Expand Down Expand Up @@ -5960,6 +5993,32 @@ static void llm_load_hparams(
default: model.type = e_model::MODEL_UNKNOWN;
}
} break;
case LLM_ARCH_BAMBA:
{
// Mamba2 parameters
ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
ml.get_key(LLM_KV_SSM_HEAD_COUNT, hparams.ssm_head_count);
ml.get_key(LLM_KV_SSM_HEAD_DIM, hparams.ssm_head_dim);
ml.get_key(LLM_KV_SSM_CHUNK_SIZE, hparams.ssm_chunk_size);
ml.get_key(LLM_KV_SSM_CONV_BIAS, hparams.ssm_conv_bias);
ml.get_key(LLM_KV_SSM_PROJ_BIAS, hparams.ssm_proj_bias);

// Attention params
std::fill(hparams.ssm_layer_arr.begin(), hparams.ssm_layer_arr.end(), true);
std::vector<uint32_t> attn_layer_indices;
ml.get_arr(LLM_KV_ATTENTION_LAYER_INDICES, attn_layer_indices);
for (const auto attn_idx : attn_layer_indices) {
GGML_ASSERT(attn_idx < hparams.n_layer);
hparams.ssm_layer_arr[attn_idx] = false;
}

// Feed forward params
ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
} break;
case LLM_ARCH_XVERSE:
{
ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
Expand Down Expand Up @@ -7038,6 +7097,11 @@ static void llm_load_print_meta(llama_model_loader & ml, llama_model & model) {
LLAMA_LOG_INFO("%s: ssm_dt_rank = %u\n", __func__, hparams.ssm_dt_rank);
LLAMA_LOG_INFO("%s: ssm_n_group = %u\n", __func__, hparams.ssm_n_group);
LLAMA_LOG_INFO("%s: ssm_dt_b_c_rms = %d\n", __func__, hparams.ssm_dt_b_c_rms);
LLAMA_LOG_INFO("%s: ssm_head_count = %d\n", __func__, hparams.ssm_head_count);
LLAMA_LOG_INFO("%s: ssm_head_dim = %d\n", __func__, hparams.ssm_head_dim);
LLAMA_LOG_INFO("%s: ssm_chunk_size = %d\n", __func__, hparams.ssm_chunk_size);
LLAMA_LOG_INFO("%s: ssm_conv_bias = %d\n", __func__, hparams.ssm_conv_bias);
LLAMA_LOG_INFO("%s: ssm_proj_bias = %d\n", __func__, hparams.ssm_proj_bias);
}

LLAMA_LOG_INFO("%s: model type = %s\n", __func__, llama_model_type_name(model.type));
Expand Down Expand Up @@ -7106,6 +7170,10 @@ static void llm_load_print_meta(llama_model_loader & ml, llama_model & model) {
LLAMA_LOG_INFO("%s: f_residual_scale = %f\n", __func__, hparams.f_residual_scale);
LLAMA_LOG_INFO("%s: f_attention_scale = %f\n", __func__, hparams.f_attention_scale);
}

if (model.arch == LLM_ARCH_BAMBA) {
LLAMA_LOG_INFO("%s: ssm_layer_arr = %s\n", __func__, print_f([&](uint32_t il) { return uint32_t(hparams.ssm_layer(il)); }, hparams.n_layer).c_str());
}
}

enum llm_tensor_layer {
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy