pratikgx's picture
Add PLE-safe MLX conversion of google/cellgemma-e4b, model card, and Apache 2.0 license
b22c61e verified
|
Raw History Blame Contribute Delete
5.62 kB
---
license: apache-2.0
base_model: google/cellgemma-e4b
library_name: mlx-lm
pipeline_tag: text-generation
tags:
- mlx
- cellgemma
- gemma4
- biology
- perturbation
---
# CellGemma E4B (PLE-safe MLX 8-bit)
[google/cellgemma-e4b](https://huggingface.co/google/cellgemma-e4b) converted to [MLX](https://github.com/ml-explore/mlx-lm) 8-bit for Apple silicon. This is a quantized derivative of Google DeepMind's CellGemma E4B. The base model, and this conversion, are released under [Apache 2.0](https://huggingface.co/google/cellgemma-e4b/blob/main/LICENSE). Keep the license terms of the base model when you use or redistribute this repo.
CellGemma is not a chat model. It takes a baseline cell (top 1,000 genes) plus a perturbation and writes a ranked residual gene list (`+GENE` / `-GENE`). Dataset-specific ExpressionDecoders in the [base repo](https://huggingface.co/google/cellgemma-e4b) map that list to expression and log-fold change. Do not use it for clinical diagnosis, patient care, or medical decisions.
## Quantization
Converted from the official `google/cellgemma-e4b` weights with `mlx-lm` **0.32.2** (`mlx` / `mlx-metal` 0.32.4), on CPU, using a **PLE-safe** predicate:
- Token embeddings (`embed*`) stay in bf16. `nn.Embedding` is not quantized.
- Per-layer embedding (PLE) linears (`per_layer_*`) stay in bf16. Quantizing Gemma 4 PLE / ScaledLinear layers has produced garbage in some community conversions.
- Layer norms stay in bf16.
- Attention and MLP projections are 8-bit affine, group size 64.
- `router.proj` is kept at 8-bit (same as this recipe's default for that layer).
Average bits per weight of the saved model: **12.226** (PLE and embeddings dominate the file size). On-disk size: **11.04 GB**.
This is not a community conversion of a third-party quant. The official Hugging Face weights were converted directly.
## Usage
```bash
pip install mlx-lm==0.32.2
```
```python
from mlx_lm import load
from mlx_lm.generate import stream_generate
from mlx_lm.sample_utils import make_sampler
from mlx_lm.tokenizer_utils import TokenizerWrapper
model, tokenizer = load("pratikgx/cellgemma-e4b-MLX-8bit")
if not isinstance(tokenizer, TokenizerWrapper):
tokenizer = TokenizerWrapper(tokenizer)
tokenizer.add_eos_token("<turn|>")
# Official CellGemma wrap. Build `prompt` as in the base model card:
# preamble + perturbation + top-1,000 baseline genes + mean-shift list.
# https://huggingface.co/google/cellgemma-e4b
prompt = "<your cell + perturbation prompt>"
wrapped = f"<bos><|turn>user\n{prompt}<turn|>\n<|turn>model\n"
# The wrap already includes <bos>. Do not add special tokens again.
prompt_ids = tokenizer.encode(wrapped, add_special_tokens=False)
# Greedy. The official card also shows temperature 0.7 / do_sample=True.
pieces = []
for resp in stream_generate(
model,
tokenizer,
prompt_ids,
max_tokens=8192,
sampler=make_sampler(temp=0.0),
):
pieces.append(resp.text)
print("".join(pieces))
```
For a full virtual screen, pass the generated `+GENE` / `-GENE` list through the matching ExpressionDecoder from [google/cellgemma-e4b](https://huggingface.co/google/cellgemma-e4b) (`decoders/` in that repo).
## Measured on a 64GB M5 Pro Mac mini
Greedy decoding, `max_new_tokens=8192`, official K562 MYC knockdown and Tahoe Panobinostat 0.5 uM prompts, median of 3 runs. Compared against a new greedy `transformers` bf16 baseline (not the official card's temperature 0.7 setup). A background app was running.
| | transformers bf16 | this 8-bit MLX |
|---|---:|---:|
| Load | 8.97 s | 2.33 s |
| Memory | 14.4 GB weights | 11.00 GB at load, 12.7 GB peak MLX |
| Tahoe prefill / decode | 2,138 tok/s, 19.0 tok/s | 3,999 tok/s, 37.7 tok/s |
| Tahoe time to first token / screen | 1.93 s, 433 s (8,192 cap) | 1.06 s, 218 s (8,192 cap) |
| K562 prefill / decode | 2,147 tok/s, 20.6 tok/s | 4,037 tok/s, 39.4 tok/s |
| K562 time to first token / screen | 2.09 s, 83 s (1,668 tokens) | 1.11 s, 51 s (1,971 tokens) |
Outputs were deterministic across the 3 greedy runs.
### Gene-list agreement versus greedy bf16
The 8-bit lists hold up well at the ranks you would actually read, with some mid-list drift.
**Tahoe Panobinostat** (both hit the 8,192 cap):
- Top 10 overlap: 9/10 up (90%), 10/10 down (100%). Exact rank: 8/10 up, 10/10 down.
- Top 50 overlap: 50/50 up (100%), 49/50 down (98%). Exact rank: 27/50 up, 18/50 down.
- Sign agreement on 3,297 shared genes: 100%. Spearman 0.948 (signed ranks).
- ExpressionDecoder LFC Pearson 0.88; sign agreement on both-nonzero genes 95.1%.
**K562 MYC knockdown** (bf16 stopped at 1,668 tokens, 8-bit at 1,971; both emitted a stop token):
- Top 10 up and top 10 down are an **exact rank match** with bf16 (10/10 and 10/10).
- Top 50 overlap: 40/50 up (80%), 29/50 down (58%). Exact rank: 20/50 up, 17/50 down.
- Sign agreement on 657 shared genes: 99.8%. Spearman 0.914.
- LFC Pearson 0.76; sign agreement on both-nonzero genes 98.6%.
The 8-bit K562 list is a little longer than bf16, so membership below the head is not identical, even though the top 10 is.
## Related
- Base model: [google/cellgemma-e4b](https://huggingface.co/google/cellgemma-e4b)
- 4-bit companion: [pratikgx/cellgemma-e4b-MLX-4bit](https://huggingface.co/pratikgx/cellgemma-e4b-MLX-4bit) (faster, but gene-list ranks drift and the K562 run did not stop)
- Official transformers bf16 timing on this machine (temperature 0.7, card setup): [pratikgx/cellgemma-e4b-m5-pro-mac-mini-bench](https://huggingface.co/datasets/pratikgx/cellgemma-e4b-m5-pro-mac-mini-bench)
Converted and timed by Pratik Gandhi on 1 December 2026.