Write a calculator that compares a Mamba language model with a Transformer of the same width and depth: parameters, the per-sequence inference cache, and the context length at which the Transformer's cache overtakes Mamba's.
mamba_vs_transformer(D, n_layers, vocab, L, batch, E=2, N=16, K=4, R=None, bytes_per_value=2)
All counts are integers. ED = E * D. If R is None, use .
Mamba layer (mixer plus its pre-norm): in_proj without bias; depthwise conv with weights and biases; x_proj without bias; dt_proj with bias; A_log of size ; the skip D of size ; out_proj without bias; one RMSNorm weight vector of size .
Transformer layer: , that is for the Q, K, V and output projections with biases, for a -wide MLP with biases, and for two LayerNorms (weight and bias each).
Whole models: both use a tied embedding of (no separate output head, no positional table). Mamba adds a final RMSNorm (), the Transformer a final LayerNorm ().
Inference cache per layer for batch sequences, in bytes: Mamba stores the conv state and the SSM state values per sequence; the Transformer stores keys and values, values per sequence at context length L. Multiply by bytes_per_value.
Crossover: the smallest integer context length at which one sequence's per-layer KV cache is strictly larger than its per-layer Mamba state.
Return a dict with these keys:
"R", "mamba_layer_params", "transformer_layer_params", "mamba_params", "transformer_params""param_ratio": Transformer layer over Mamba layer parameters, a float rounded to 4 decimals"mamba_cache_bytes_per_layer", "kv_cache_bytes_per_layer", "mamba_cache_bytes", "kv_cache_bytes" (the last two summed over all n_layers)"crossover_L"