Text Generation
Safetensors
MLX
mlx-lm
gemma4_text
cellgemma
gemma4
biology
perturbation
4-bit precision
Instructions to use pratikgx/cellgemma-e4b-MLX-4bit with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use pratikgx/cellgemma-e4b-MLX-4bit with MLX:
# Make sure mlx-lm is installed # pip install --upgrade mlx-lm # if on a CUDA device, also pip install mlx[cuda] # Generate text with mlx-lm from mlx_lm import load, generate model, tokenizer = load("pratikgx/cellgemma-e4b-MLX-4bit") prompt = "Once upon a time in" text = generate(model, tokenizer, prompt=prompt, verbose=True) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- MLX LM
How to use pratikgx/cellgemma-e4b-MLX-4bit with MLX LM:
Generate or start a chat session
# Install MLX LM uv tool install mlx-lm # Generate some text mlx_lm.generate --model "pratikgx/cellgemma-e4b-MLX-4bit" --prompt "Once upon a time"
- Atomic Chat
|
Download README.md from pratikgx/cellgemma-e4b-MLX-4bit: direct link, hf CLI and curl.
- Browser
- Download file 6.62 kB
-
https://huggingface.co/pratikgx/cellgemma-e4b-MLX-4bit/resolve/main/README.md
- Command line
-
hf download hf://pratikgx/cellgemma-e4b-MLX-4bit/README.md
-
curl -L -o README.md https://huggingface.co/pratikgx/cellgemma-e4b-MLX-4bit/resolve/main/README.md
6.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 4-bit) | |
| [google/cellgemma-e4b](https://huggingface.co/google/cellgemma-e4b) converted to [MLX](https://github.com/ml-explore/mlx-lm) 4-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. | |
| **If you care about gene-list fidelity, prefer the [8-bit companion](https://huggingface.co/pratikgx/cellgemma-e4b-MLX-8bit).** In a greedy head-to-head against transformers bf16 on the official example prompts, 8-bit kept the top-10 K562 ranks exactly and stayed close on Tahoe. This 4-bit conversion is faster and smaller, but the ranked lists drift, and the K562 example never emitted a stop token (it ran to the 8,192 token cap and the tail started repeating genes). | |
| ## 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 4-bit affine, group size 64. | |
| - `router.proj` is forced to 8-bit. | |
| Average bits per weight of the saved model: **10.213** (PLE and embeddings dominate the file size). On-disk size: **9.23 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-4bit") | |
| 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 4-bit MLX | 8-bit MLX | | |
| |---|---:|---:|---:| | |
| | Load | 8.97 s | 1.88 s | 2.33 s | | |
| | Memory | 14.4 GB weights | 9.19 GB at load, 10.8 GB peak MLX | 11.00 GB at load, 12.7 GB peak MLX | | |
| | Tahoe prefill / decode | 2,138 tok/s, 19.0 tok/s | 3,946 tok/s, 51.1 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.05 s, 162 s (8,192 cap) | 1.06 s, 218 s (8,192 cap) | | |
| | K562 prefill / decode | 2,147 tok/s, 20.6 tok/s | 3,614 tok/s, 44.4 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.24 s, 186 s (**8,192 cap, did not stop**) | 1.11 s, 51 s (1,971 tokens, stopped) | | |
| Outputs were deterministic across the 3 greedy runs. | |
| ### Gene-list agreement versus greedy bf16 | |
| Direction often holds. Exact rank usually does not. This is not a drop-in replacement for bf16 lists. | |
| **Tahoe Panobinostat** (both hit the 8,192 cap): | |
| - Top 10 overlap: 5/10 up (50%), 9/10 down (90%). Exact rank: 0/10 up, 2/10 down. | |
| - Top 50 overlap: 39/50 up (78%), 45/50 down (90%). Exact rank: 2/50 up, 2/50 down. | |
| - Sign agreement on 2,949 shared genes: 100%. Spearman 0.921 (signed ranks). | |
| - ExpressionDecoder LFC Pearson 0.78; sign agreement on both-nonzero genes 94.6%. | |
| - The head of the up list shuffles: bf16 leads with HSP90AA1, H1-0, H3-3B; 4-bit leads with ER-stress genes (HSP90B1, HSPA5, PDIA4, CALR) before those names appear. | |
| **K562 MYC knockdown** (bf16 stopped at 1,668 tokens; 4-bit hit the 8,192 cap and did not emit a stop token): | |
| - Top 10 overlap: 6/10 up (60%), 3/10 down (30%). Exact rank: 0/10 and 0/10. | |
| - Top 50 overlap: 36/50 up (72%), 19/50 down (38%). Exact rank: 3/50 up, 1/50 down. | |
| - Sign agreement on 761 shared genes: 99.7%. Spearman 0.900. | |
| - LFC Pearson 0.29 (the much longer list changes what the decoder sees). Sign agreement on both-nonzero genes 95.4%. | |
| - The tail of the 4-bit K562 list starts repeating genes (for example DEFB131B 23 times). Top-k overlap above is computed on the ranked heads, so that tail does not inflate those percentages. Hematopoietic names such as GYPB, BLVRB, HBZ, ALAS2, SLC25A37 still appear near the top of the up list, but not in the same order. | |
| ## Related | |
| - Base model: [google/cellgemma-e4b](https://huggingface.co/google/cellgemma-e4b) | |
| - 8-bit companion (closer gene lists): [pratikgx/cellgemma-e4b-MLX-8bit](https://huggingface.co/pratikgx/cellgemma-e4b-MLX-8bit) | |
| - 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. | |