--- license: apache-2.0 library_name: mlx base_model: google/cellgemma-e4b base_model_relation: quantized pipeline_tag: text-generation language: - en tags: - mlx - mlx-lm - apple-silicon - macos - cellgemma - gemma4 - biology - single-cell - transcriptomics - gene-expression - perturbation-prediction - crispr - 4-bit --- # CellGemma E4B MLX 4-bit for Apple Silicon Run Google's CellGemma E4B on Apple Silicon with MLX. This 4-bit export predicts how gene expression changes after a perturbation, such as a drug treatment or a gene knockdown. It includes the language model, all 13 expression decoders, sample controls and gene descriptions. Converted by [bsisduck](https://huggingface.co/bsisduck) from [google/cellgemma-e4b](https://huggingface.co/google/cellgemma-e4b). Google DeepMind developed the original model and decoders. No additional training was performed for this export. ## Choose a format | | [MLX 4-bit](https://huggingface.co/bsisduck/cellgemma-e4b-mlx-4bit) | [MLX BF16](https://huggingface.co/bsisduck/cellgemma-e4b-mlx-bf16) | | --- | --- | --- | | Language-model weights on disk | 4.35 GB | 15.46 GB | | Model with supporting assets | About 4.49 GB | About 15.60 GB | | Weight format | 4-bit affine, group size 64 | Bfloat16, unquantized | | Use | Local experiments with lower memory use | Reference weights and conversion checks | | Original weight values | Quantized | All 665 model tensors checked for exact equality | Sizes use decimal GB and exclude the Python environment. The 4-bit language-model files are about 72% smaller. The expression decoders retain their original weights in both exports. **This repository is the 4-bit version.** It reduces weight storage while keeping the upstream input format and decoder assets. Quantization can change predictions. ## Run the supplied example **Runtime status, 4 December 2026:** this example has completed 4-bit inference, but a later scheduled run hit a Metal GPU timeout with both formats during warmup. The cause is unresolved. See the measurement records below; this stack is not yet a consistently reproducible performance baseline. The commands below use Python 3.12 and the versions tested on an Apple M2 Max with macOS 27.0. The installed MLX 0.32.5 binary in this test requires macOS 27. An older macOS installation needs a compatible MLX build; that combination has not been tested here. ```sh python3.12 -m venv .venv source .venv/bin/activate python -m pip install \ mlx==0.32.5 mlx-lm==0.32.2 transformers==5.25.0 \ huggingface-hub==2.5.0 numpy==2.5.5 safetensors==0.8.0 hf download bsisduck/cellgemma-e4b-mlx-4bit \ --local-dir cellgemma-e4b-mlx-4bit python cellgemma-e4b-mlx-4bit/examples/predict.py \ --model cellgemma-e4b-mlx-4bit \ --dataset tahoe --max-tokens 512 --output tahoe-preview ``` This runs the supplied Tahoe control cell 0 with Panobinostat at 0.5 uM. [The example script](examples/predict.py) constructs the prompt, generates signed gene ranks and reconstructs continuous expression with the matching decoder. It runs locally and uses files from the downloaded directory. An HF token is not required for this public repository. The output directory contains: | File | Contents | | --- | --- | | `expression.csv` | One row per gene: control, mean baseline, predicted expression and change | | `residual_genes.txt` | Raw signed gene sequence from the language model | | `benchmark.json` | Timing, memory, sampling settings, environment and truncation status | The Tahoe example has 19,720 rows. `change` means predicted expression minus control in normalized log1p units. It is not a raw fold-change ratio. Genes absent from the generated sequence retain the mean-shift prediction; absence does not establish that a gene is unaffected. For the supplied K562 control and a MYC knockdown: ```sh python cellgemma-e4b-mlx-4bit/examples/predict.py \ --model cellgemma-e4b-mlx-4bit \ --dataset k562 --knockdown-gene MYC --max-tokens 512 \ --output k562-myc-preview ``` K562 has 18,238 genes. The script uses the supplied knowledge-graph description of MYC. Choose a new output directory for each invocation to preserve earlier results. A 512-token run is a preview. If `truncated` is `true`, generation reached the limit. Use `--max-tokens 8192` for the upstream output budget. The example enforces the upstream documented 16K context, even though the configuration's `max_position_embeddings` value is larger. Longer generation changes both the ranks available to the decoder and its reconstructed values. ## Local performance measurements The **4-bit** series below completed on 3 December 2026 using an Apple M2 Max with 32 GiB unified memory and macOS 27.0. It used Tahoe control cell 0, the supplied Panobinostat prompt, 4,128 input tokens and 128 generated tokens. One warmup was followed by three measured runs. All outputs reached the 128-token limit. Values are **median (minimum to maximum)** across the three measured runs. Other desktop applications remained open, so these are observations under shared system load, not an isolated hardware benchmark or a guaranteed speed. | Measurement | 4-bit: median (minimum to maximum) | | --- | --- | | Time to first token | 10.73 s (10.51 to 15.24) | | Prediction and reconstruction | 23.22 s (18.01 to 43.61) | | Generation throughput | 10.51 tokens/s (4.55 to 17.79) | | Peak MLX allocation | 5.05 GB (5.03 to 5.05) | [Raw 4-bit report](benchmarks/2026-12-03-m2-max-4bit.json), including warmup, every measured run and the environment. The two formats do not have a completed, matched repeat benchmark. An earlier, single BF16 validation run reported **9.83 generation tokens/s** and **16.36 GB peak MLX allocation** for 128 output tokens. Those figures are retained in the [original BF16 validation report](https://huggingface.co/bsisduck/cellgemma-e4b-mlx-bf16/blob/441409ed14ef3e6234b66c2abc8e6f65e8737979/validation.json). It used a different validation procedure and had no repeated measurements, so it is not a direct speed comparison with the table above. Later attempts did not reproduce a complete series: | Attempt | Outcome | | --- | --- | | Preliminary 4-bit attempt on 3 December | Metal GPU timeout before a measured result | | [BF16 on 3 December](benchmarks/2026-12-03-m2-max-bf16-incomplete.json) | Warmup stopped after 269 seconds following model loading; no completed prediction | | [4-bit on 4 December](benchmarks/2026-12-04-m2-max-4bit-failed.json) | Metal GPU timeout during warmup; zero measured runs | | [BF16 on 4 December](benchmarks/2026-12-04-m2-max-bf16-failed.json) | Metal GPU timeout during warmup; zero measured runs | The 4 December attempt ran sequentially through macOS launchd at background CPU priority. Nine unit tests passed before generation. The records do not isolate whether the GPU failures came from MLX, Metal, system state or scheduling. The successful 4-bit series is retained as a dated observation, not evidence that the later failures are fixed. Leave room for macOS and other applications when planning memory; peak allocation is not a minimum RAM requirement. These measurements describe inference on a supplied example. They do not measure biological accuracy, performance on unseen perturbations or parity with Google's published evaluation. The original model's scientific benchmark results are in [Google's model card](https://huggingface.co/google/cellgemma-e4b#benchmark-results). Those scores have not been re-established for this MLX conversion. To collect timing measurements on your Mac: ```sh python cellgemma-e4b-mlx-4bit/examples/predict.py \ --model cellgemma-e4b-mlx-4bit \ --dataset tahoe --cell-index 0 --max-tokens 128 \ --warmup 1 --runs 3 --seed 42 --output local-benchmark ``` The script records every run, including warmup, and computes median, minimum and maximum across the measured runs. No prompt cache is reused. It uses a 256-token prefill batch, temperature 0.7, top-p 0.95 and top-k 64. Timing excludes downloading, model loading and input preparation. First-token latency includes prompt processing. Prediction time includes generation and expression reconstruction. MLX peak allocation is not total process or system memory. ## Conversion checks The [validation report](validation.json) contains the original technical checks. Its total duration includes loading and parity checks, so use the separate benchmark reports for inference timing: | Check | Result | | --- | --- | | Dataset decoders compared with a PyTorch reference | 13 of 13 passed | | Largest absolute decoder difference on the test inputs | 0.00000566245 | | BF16 model tensors compared with the source | 665 exactly equal | | Original Tahoe generation smoke test | 4,128 input tokens; 128 output tokens | | Recognized signed entries in that smoke test | 32 positive and 32 negative for each format | | Reconstructed values | Finite for all tested genes | Decoder comparison used 20 synthetic feature/control rows per dataset decoder, with `rtol=3e-4` and `atol=3e-5`. It checks the implementation on those inputs. The BF16 equality result applies to the BF16 export, not the quantized weights. The smoke-test outputs for 4-bit and BF16 differ, including their gene order. A recognized gene sequence is not an accuracy score. ## Input and decoder contract CellGemma expects a specific perturbation prompt rather than a chat question. The example keeps the upstream preamble and these sections in order: perturbation description, `Source:`, `Mean Global prediction:` and `Target:`. It uses the Gemma turn delimiters, a single beginning-of-sequence token, and `` as an additional stop token. Do not apply a generic chat template on top of it. For your own data, follow the [upstream preprocessing instructions](UPSTREAM_MODEL_CARD.md#3-input-expression-preprocessing). Inputs must be normalized log1p expression with matching gene symbols and an assay-appropriate decoder. A decoder from a different assay changes the baseline and reconstruction. The supplied controls are useful for checking the inference path; they are not a held-out evaluation set. | Decoder folders | Assay group | | --- | --- | | `tahoe`, `l1000`, `emeraldbay`, `sciplex` | Small-molecule perturbations | | `hepg2`, `jurkat`, `k562`, `rpe1`, `vcc`, `norman` | Genetic perturbations | | `parse`, `dong`, `tisch` | Cytokine or clinical datasets | All 13 decoders are included. Only Tahoe and K562 have bundled control examples, with 100 cells each. The command-line example supports those two datasets. ## Files and provenance | Path | Purpose | | --- | --- | | `*.safetensors`, `config.json` | MLX language-model weights and configuration | | `tokenizer.json`, `tokenizer_config.json` | CellGemma tokenizer with the upstream gene vocabulary | | `decoders//` | ExpressionDecoder weights, configuration and mean deltas | | `controls_sample.json` | Supplied Tahoe and K562 controls | | `kg_sentences.json` | Gene descriptions for genetic perturbation prompts | | `conversion.json` | Source revision, conversion format and package versions | | `validation.json` | Conversion checks from the initial publication | | `benchmarks/` | Completed 4-bit measurements and records of incomplete or failed attempts | | `examples/` | Runnable MLX example and its prompt/decoder helpers | | `UPSTREAM_MODEL_CARD.md`, `LICENSE` | Original model documentation and Apache 2.0 license | Source: [`google/cellgemma-e4b` at `6212145e0131d3744171739ea5232afc6aaa7c76`](https://huggingface.co/google/cellgemma-e4b/tree/6212145e0131d3744171739ea5232afc6aaa7c76). The conversion used `mlx-lm==0.32.2`, `mlx==0.32.5` and `transformers==5.25.0`. E4B describes effective computation, not a 4 GB file or a count of all stored parameters. The expression decoders and supporting data were copied from the source export. Their original safetensors names are retained; [the MLX decoder helper](examples/decoder.py) reads them directly. The original [4-bit weight revision](https://huggingface.co/bsisduck/cellgemma-e4b-mlx-4bit/tree/41e0f131d83b6341b7911ffd1d5e9cb12960d66b) and [BF16 weight revision](https://huggingface.co/bsisduck/cellgemma-e4b-mlx-bf16/tree/441409ed14ef3e6234b66c2abc8e6f65e8737979) remain available for reproducibility. Later documentation commits do not change those weight files. ## Scope and limitations This is a research model for cell perturbation experiments. It is not a diagnostic system or a treatment recommender. Outputs need experimental validation, especially for a new assay, cell type or perturbation outside the training distribution. Quantization can alter generated ranks and reconstructed expression. Neither the technical checks nor the local performance measurements establish clinical suitability or biological equivalence. The model, source assets and example code are distributed under [Apache 2.0](LICENSE). See the [original model card](UPSTREAM_MODEL_CARD.md) for training data, intended use and limitations. Report conversion or example-code problems in this repository's Community tab, with the model revision, package versions, hardware and a non-sensitive reproducer.