Retrieved article excerpt
Open article · Retrieved 2026-11-25T10:22:48.655230+00:00
# esmc.cpp
Metal-accelerated C/C++ inference for [ESM Cambrian](https://www.evolutionaryscale.ai/blog/esm-cambrian) (ESM-C), built on [llama.cpp](https://github.com/ggml-org/llama.cpp) / ggml.
- **Source:** [github.com/AnanyaP-WDW/esmc.cpp](https://github.com/AnanyaP-WDW/esmc.cpp)
- **Pre-converted GGUF models:** [AnanyaPathak/esmc-300m-gguf](https://huggingface.co/AnanyaPathak/esmc-300m-gguf) on Hugging Face (model card with benchmarks, quick start, and usage)
## Build (milestone 0)
```
git submodule update --init --recursive
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j8
./build/esmc-embed --help
```
Or: `make -C build esmc-embed`
## Weight inspection (milestone 1)
```
python3 -m venv .venv && .venv/bin/pip install -r tools/requirements.txt
hf download biohub/ESMC-300M --local-dir ./esmc-300m
.venv/bin/python tools/inspect_esmc_weights.py ./esmc-300m
```
The biohub checkpoint uses the native `esmc.*` layout (fused QKV, fused SwiGLU `fc1_weight`).
The HuggingFace `model.layers.*` layout in plan §2.1 is also supported when present.
## Convert to GGUF (milestone 2)
```
.venv/bin/pip install -e ./ggml/gguf-py -r tools/requirements.txt
.venv/bin/python tools/convert_esmc_to_gguf.py ./esmc-300m ./models/esmc-300m-f16.gguf
# Verify metadata + tensors
.venv/bin/python tools/verify_gguf.py ./models/esmc-300m-f16.gguf
.venv/bin/python ./ggml/gguf-py/gguf/scripts/gguf_dump.py ./models/esmc-300m-f16.gguf --no-tensors
```
## Tokenizer test (milestone 4)
```
.venv/bin/python tests/test_tokenizer.py
# ACDEF -> [0, 5, 23, 13, 9, 18, 2] (matches HF tokenizer.json)
```
## Layer-0 Q/K check (milestone 5)
```
.venv/bin/python tests/check_layer0_qk.py
./build/esmc-embed -m ./models/esmc-300m-f16.gguf -s ACDEF --layers 8 --no-metal
```
## Load GGUF in C++ (milestone 3)
```
cmake --build build -j8
./build/esmc-embed -m ./models/esmc-300m-f16.gguf --verify-load --no-metal
```
## Embed sequences
```
# Per-residue embeddings (.npy: [n_tokens, n_embd])
./build/esmc-embed -m ./models/esmc-300m-Q4_K_M.gguf \
-s "MKTVRQERLKSIVRILERSKEPVSGAQLAEELSVSRQVIVQDIAYLRSLGY" \
--pool none --output embedding.npy
# Mean-pooled sequence embedding (strips CLS/EOS)
./build/esmc-embed -m ./models/esmc-300m-Q4_K_M.gguf \
-s "MKTVRQ..." --pool mean --output embedding.npy
```
## Reproduce the paper results (300M, end-to-end)
The commands below reproduce every number in the paper, in order, from a clean
clone. Run them on an Apple Silicon Mac (36 GB recommended) for the full
CPU + Metal matrix; non-Apple/CPU-only hosts can run everything except the Metal
rows. Budget ~3 GB of downloads (checkpoint + ProteinGym archive) and a few hours
for the complete benchmark matrix.
**Prerequisites:** macOS with the Xcode command-line tools, CMake ≥ 3.14,
Python ≥ 3.10, and a HuggingFace account/token for the checkpoint download. The
PyTorch reference and the PyTorch baselines require `torch`; the Metal backend
requires an Apple GPU.
### 1. Clone and build
```
git clone --recursive https://github.com/AnanyaP-WDW/esmc.cpp.git
cd esmc.cpp
git submodule update --init --recursive # only if you cloned without --recursive
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j8 # builds esmc-embed, esmc-bench, esmc-quantize
```
### 2. Python environment
```
python3 -m venv .venv
.venv/bin/pip install -r tools/requirements.txt
.venv/bin/pip install -e ./ggml/gguf-py
.venv/bin/pip install torch # PyTorch reference + baselines
```
### 3. Download the ESM-C 300M checkpoint
```
hf download EvolutionaryScale/esmc-300m-2024-12 --local-dir ./esmc-300m
```
### 4. Convert to GGUF (F16 + F32) and verify
```
.venv/bin/python tools/convert_esmc_to_gguf.py ./esmc-300m ./models/esmc-300m-f16.gguf --dtype f16
.venv/bin/python tools/convert_esmc_to_gguf.py ./esmc-300m ./models/esmc-300m-f32.gguf --dtype f32
.venv/bin/python tools/verify_gguf.py ./models/esmc-300m-f16.gguf
```
### 5. Quantize from the F32 GGUF
```
cmake --build build --target esmc-quantize
./build/esmc-quantize models/esmc-300m-f32.gguf models/esmc-300m-Q8_0.gguf Q8_0
./build/esmc-quantize models/esmc-300m-f32.gguf models/esmc-300m-Q4_K_M.gguf Q4_K_M
./build/esmc-quantize models/esmc-300m-f32.gguf models/esmc-300m-Q4_K_S.gguf Q4_K_S
```
### 6. Staged validation (tokenizer → layer-0 Q/K → full forward)
```
.venv/bin/python tests/test_tokenizer.py # M4: token IDs
.venv/bin/python tests/check_layer0_qk.py # M5: layer-0 Q/K probe
.venv/bin/python tests/validate.py --no-metal # M6: full forward (CPU)
.venv/bin/python tests/validate.py --metal --compare-cpu-metal # CPU/Metal parity
```
### 7. Generate the 100-sequence PyTorch reference
The correctness harness compares against a PyTorch reference that is **not**
checked into git (~107 MB). Regenerate it once from the checkpoint:
```
.venv/bin/python tests/generate_reference.py \
--fasta benchmarks/sequences_correctness.fasta \
--output tests/reference_embeddings.npz
```
### 8. Run the benchmarks
```
# Numerical correctness (paper Table 2): all precisions vs PyTorch, 100 Swiss-Prot seqs
.venv/bin/python benchmarks/correctness.py
# Throughput (paper Table 3, Fig 1): esmc.cpp CPU/Metal + PyTorch CPU/MPS
cmake --build build --target esmc-bench
.venv/bin/python benchmarks/throughput.py --config benchmarks/config_throughput_300m.json
# Memory footprint (paper Fig 2): peak RSS across all 36 configurations
.venv/bin/python benchmarks/memory.py --config benchmarks/config_memory_300m.json
# Downstream ProteinGym variant-effect (paper Table 4, Fig 3): 10 assays x 1000 variants
.venv/bin/python benchmarks/fetch_proteingym_subset.py --download \
--selection-mode multi-assay --max-assays 10 --max-rows 1000 --min-rows 1000 \
--max-length 512 --output-dir benchmarks/proteingym_subset_10k \
--manifest benchmarks/datasets/proteingym_subset_10k_manifest.json
.venv/bin/python benchmarks/downstream.py --config benchmarks/config_downstream_300m_10k.json
```
### 9. Figures and reproduction bundle
```
.venv/bin/python benchmarks/paper_artifacts.py # figure SVGs + summary CSVs
.venv/bin/python benchmarks/make_reproduction_bundle.py # self-contained results bundle
```
## Benchmark results (300M)
**Host:** Apple M4 Max (arm64), 36 GB unified memory, macOS 26.5.
**Reference:** official PyTorch ESM-C 300M (`tests/ref_forward.py` on native safetensors).
**Datasets:** 100 reviewed human [UniProt](https://www.uniprot.org/) sequences (correctness); length-bucketed FASTA (throughput/memory); [ProteinGym](https://www.proteingym.org/) 10-assay × 1000-variant subset (downstream).
Raw CSV/JSON artifacts live under `results/`; paper-ready Markdown/LaTeX tables are regenerated by `benchmarks/make_reproduction_bundle.py` → `results/reproduction_bundle/tables/`. Full experiment log: [lab\_manual.md](https://github.com/AnanyaP-WDW/esmc.cpp/blob/main/lab_manual.md).
### Plots
Regenerate with `benchmarks/paper_artifacts.py` (writes SVG; PNG if matplotlib is installed).
#### Numerical correctness (aggregate mean cosine vs PyTorch)
[Aggregate mean cosine by precision](https://github.com/AnanyaP-WDW/esmc.cpp/blob/main/figures/correctness_mean_cosine.svg)
[SVG](https://github.com/AnanyaP-WDW/esmc.cpp/blob/main/figures/correctness_mean_cosine.svg)
#### Throughput (seq/s by length bucket)
[Throughput: best esmc.cpp vs PyTorch CPU and MPS](https://github.com/AnanyaP-WDW/esmc.cpp/blob/main/figures/throughput_seqps.svg)
[PDF](https://github.com/AnanyaP-WDW/esmc.cpp/blob/main/figures/throughput_seqps.pdf) · [SVG](https://github.com/AnanyaP-WDW/esmc.cpp/blob/main/figures/throughput_seqps.svg)
#### Peak memory (long bucket, 36 GB M4 Max)
[Peak resident set size by configuration](https://github.com/AnanyaP-WDW/esmc.cpp/blob/main/figures/memory_long_rss.svg)
[PDF](https://github.com/AnanyaP-WDW/esmc.cpp/blob/main/figures/memory_long_rss.pdf) · [SVG](https://github.com/AnanyaP-WDW/esmc.cpp/blob/main/figures/memory_long_rss.svg)
#### Downstream preservation (ProteinGym 10-assay pass rates)
[Downstream metric pass rates by precision](https://github.com/AnanyaP-WDW/esmc.cpp/blob/main/figures/downstream_10k_pass_rate.svg)
[PDF](https://github.com/AnanyaP-WDW/esmc.cpp/blob/main/figures/downstream_10k_pass_rate.pdf) · [SVG](https://github.com/AnanyaP-WDW/esmc.cpp/blob/main/figures/downstream_10k_pass_rate.svg)
### Numerical correctness vs PyTorch
Per-residue cosine similarity on 100 Swiss-Prot sequences (short / medium / long buckets). Pass = per-sequence mean cosine > 0.999 (F16, Q8\_0) or > 0.99 (Q4\_K\_\*). The Q4 gate was recalibrated from 0.995 after the FFN LayerNorm bias fix: the old gate was measured against a reference that omitted the bias too, and honest Q4 fidelity against the corrected reference is ~0.994.
| Precision | Seqs | Aggregate mean cos | Worst mean cos | Worst min cos | Mean-pool L2 (max) | Pass rate |
| --- | --- | --- | --- | --- | --- | --- |
| F16 | 100 | 1.00000 | 1.00000 | 0.99999 | 0.0012 | 100/100 |
| Q8\_0 | 100 | 0.99987 | 0.99959 | 0.99639 | 0.0194 | 100/100 |
| Q4\_K\_M | 100 | 0.99534 | 0.99228 | 0.93738 | 0.0792 | 100/100 |
| Q4\_K\_S | 100 | 0.99422 | 0.99019 | 0.93516 | 0.0900 | 100/100 |
GGUF on-disk size (300M): F16 634 MiB, Q8\_0 337 MiB, Q4\_K\_M 238 MiB, Q4\_K\_S 228 MiB.
### Throughput (seq/s)
Single-sequence throughput by length bucket; each backend runs in a fresh process. **Best esmc.cpp** = highest seq/s for that bucket among CPU/Metal × F16/Q8\_0/Q4\_K\_\*. Measured on the M4 Max (36 GB) host; PyTorch baselines re-run with the same harness (torch 2.12).
| Bucket | Tokens | Best esmc.cpp | seq/s |
| --- | --- | --- | --- |
| short | 47 | metal/f16 | 95.7 |
| medium | 235 | metal/f16 | 38.3 |
| long | 850 | metal/f16 | 9.5 |
F16 Metal is fastest across all buckets on the M4 Max, exploiting the native FP16 tensor-core path. Quantization's primary value is model-size reduction rather than throughput.
### Batched corpus embedding (M4 Max, Metal F16)
`esmc-embed --fasta` over synthetic corpora of identical-length sequences: model loaded once, one run per configuration, median of 3 interleaved single/batched runs. "Single" = one sequence per call in-process; "Batched" = token-budget batches (`--max-tokens 8192 --max-batch 32`), length-sorted. Per-residue rates include one model load and tokenization.
| Corpus | Tokens/seq | Single (ms/seq) | Batched (ms/seq) | Speedup | Batched (residues/s) |
| --- | --- | --- | --- | --- | --- |
| short, 2000 × 45 aa | 47 | 13.4 | 7.9 | **1.70×** | 5,723 |
| medium, 1000 × 233 aa | 235 | 32.8 | 31.8 | 1.03× | 7,317 |
| long, 100 × 848 aa | 850 | 127.1 | 127.0 | 1.00× | 6,677 |
| mixed, 1000 seq | ~293 | 44.5 | 41.2 | 1.08× | 7,069 |
Batching is a real win for **short** sequences (~1.7×), where per-call launch overhead dominates. At medium/long lengths the per-sequence GEMMs already saturate the GPU and batching adds padding/attention work, so it is roughly neutral. For reference, the PyTorch baselines reach 3,603 / 7,220 / 4,921 residues/s (MPS, short/medium/long) and 868 / 1,854 / 2,584 (CPU) with torch 2.12, so `esmc.cpp` is ~1.0–1.6× faster per residue than PyTorch MPS — the dominant win is the C++/ggml runtime and load-once batching, not batching per se.
#### Per-residue throughput vs batch size
Uniform-length sequences, M4 Max Metal F16, median of 15 iterations; batch 1 is the single-sequence path. Values are **residues/s** (higher is better).
| Batch size | short (45 aa) | medium (233 aa) | long (848 aa) |
| --- | --- | --- | --- |
| 1 | 4,258 | 9,046 | 9,465 |
| 2 | 6,249 | 9,770 | **9,565** |
| 4 | 7,551 | **10,313** | 8,302 |
| 8 | **8,238** | 10,012 | 7,997 |
| 16 | 7,391 | 8,470 | 7,318 |
| 32 | 6,938 | 7,081 | 3,952 |
The optimal batch size is length-dependent: short sequences favor batch 8 (~1.9× over batch 1), medium favor batch 4 (~1.14×), and long sequences are fastest at