Token Classification
Transformers
Safetensors
generanno
dna
genomics
biology
genome-annotation
custom_code
Instructions to use HuggingFaceBio/Carbon-A-1.2B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use HuggingFaceBio/Carbon-A-1.2B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("token-classification", model="HuggingFaceBio/Carbon-A-1.2B", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForTokenClassification model = AutoModelForTokenClassification.from_pretrained("HuggingFaceBio/Carbon-A-1.2B", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Align Carbon-A model card with Carbon family structure and add detailed inference guide
Browse files- INFERENCE.md +86 -0
- README.md +70 -56
- provenance.json +2 -1
INFERENCE.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Carbon-A inference guide
|
| 2 |
+
|
| 3 |
+
Examples, output formats, and functional checks for Carbon-A. For model facts and a quick start, see the [model card](README.md).
|
| 4 |
+
|
| 5 |
+
## Run inference on a GenBank file
|
| 6 |
+
|
| 7 |
+
Use `infer_genbank.py` for a local eukaryotic GenBank flat file (`.gb`, `.gbff`, or `.gz`). It reads the DNA sequence; existing feature annotations are not used as model inputs. Install the repository's `requirements.txt` and authenticate with `hf auth login`, then run:
|
| 8 |
+
|
| 9 |
+
```bash
|
| 10 |
+
python infer_genbank.py genome.gbff.gz --out-dir genome-predictions
|
| 11 |
+
```
|
| 12 |
+
|
| 13 |
+
For a FASTA file downloaded from GenBank:
|
| 14 |
+
|
| 15 |
+
```bash
|
| 16 |
+
python infer_genbank.py genome.fna.gz --format fasta --out-dir genome-predictions
|
| 17 |
+
```
|
| 18 |
+
|
| 19 |
+
Both commands process every sequence record in the file. Use `--revision <model-commit>` to pin the model, or `--threshold 0.7` to change the binary-call threshold (default 0.5).
|
| 20 |
+
|
| 21 |
+
The script uses 98,304-base windows with a 49,152-base stride and averages CDS probabilities where windows overlap. The last window may be shorter. No BOS/EOS tokens are added. Short records are supported, but the training data used full-length 98,304-base windows. This example uses overlap averaging; it does not reproduce any separate production genome-annotation pipeline.
|
| 22 |
+
|
| 23 |
+
Outputs are one compressed NumPy file per record, named with its accession and record name, plus `summary.json`. Each file contains forward/reverse CDS probabilities and calls, a combined `cds_label_any` track, a `valid` mask, and the number of contributing windows per base (`window_count`). Array position `i` corresponds to record base `i + 1` on **both** strands; the reverse track is not reversed. These are coding-occupancy predictions, not GFF gene or transcript models.
|
| 24 |
+
|
| 25 |
+
Any 6-mer containing an ambiguous nucleotide is excluded from the output. An incomplete final 6-mer is padded with `N` and also excluded. Unscored positions have probability `NaN`, call `-1`, and `valid=False`. Record sequences must be present in the file; annotation-only records with undefined sequence cannot be used. The example retains per-base arrays for one record in memory, so large chromosomes require more host RAM.
|
| 26 |
+
|
| 27 |
+
```python
|
| 28 |
+
import json
|
| 29 |
+
from pathlib import Path
|
| 30 |
+
import numpy as np
|
| 31 |
+
|
| 32 |
+
out = Path("genome-predictions")
|
| 33 |
+
record = json.loads((out / "summary.json").read_text())["records"][0]
|
| 34 |
+
with np.load(out / record["file"]) as prediction:
|
| 35 |
+
plus = prediction["cds_probability_plus"]
|
| 36 |
+
minus = prediction["cds_probability_minus"]
|
| 37 |
+
coding = prediction["cds_label_any"] # 0, 1, or -1 (unscored)
|
| 38 |
+
valid = prediction["valid"]
|
| 39 |
+
print(record["accession"], plus.shape, int(valid.sum()))
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
## Run inference on packed data
|
| 43 |
+
|
| 44 |
+
Use Python 3.10+ and a CUDA-enabled PyTorch installation. Install the requirements and authenticate with your Hugging Face account:
|
| 45 |
+
|
| 46 |
+
```bash
|
| 47 |
+
pip install -r requirements.txt
|
| 48 |
+
hf auth login
|
| 49 |
+
python infer_packed.py --num-rows 1 --out-dir predictions
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
Run these commands from a downloaded copy of this repository. The script downloads a small evaluation HDF5 file when `--h5` is omitted, loads the model from this repository, and predicts one **full-length** row. Model files are downloaded once and cached. The script uses PyTorch SDPA; a separate FlashAttention package is not required.
|
| 53 |
+
|
| 54 |
+
For a local training or evaluation shard:
|
| 55 |
+
|
| 56 |
+
```bash
|
| 57 |
+
python infer_packed.py --h5 /path/to/shard.h5 --start-row 0 --num-rows 2 --batch-size 1 --out-dir predictions-local
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
For repeatable runs, pass `--revision <model-commit>`. Default precision is BF16 on supported GPUs, otherwise FP16; CPU uses FP32 and requires `--device cpu`. Full-length inference on CPU is slow.
|
| 61 |
+
|
| 62 |
+
### Output
|
| 63 |
+
|
| 64 |
+
Each row produces an `.npz` file with arrays of 98,304 positions:
|
| 65 |
+
|
| 66 |
+
- `cds_probability_plus`, `cds_probability_minus`: CDS probabilities.
|
| 67 |
+
- `cds_label_plus`, `cds_label_minus`: calls at `--threshold` (default 0.5).
|
| 68 |
+
- `cds_label_any`: 1 if either strand passes the threshold.
|
| 69 |
+
- `valid_plus`, `valid_minus`: positions eligible for label comparison.
|
| 70 |
+
- `reference_plus`, `reference_minus`: packed RefSeq targets, converted with `label != 0`.
|
| 71 |
+
|
| 72 |
+
Predicted calls are `-1` at excluded special-token positions; references use `-100`. Probabilities are retained at these positions, so use the validity masks when comparing predictions with annotations. A JSON sidecar records the model revision, input row indices, precision, threshold, and output paths. Coordinates are relative to the packed window; the HDF5 rows do not supply genomic accession/start metadata.
|
| 73 |
+
|
| 74 |
+
### Loading details
|
| 75 |
+
|
| 76 |
+
`load_data.py` reads the already-tokenized inputs without adding BOS/EOS tokens. Raw signed labels are converted to binary with `!= 0`, concatenated forward then reverse, and masked at special token positions. Its token IDs match the model tokenizer. To tokenize raw DNA separately, use uppercase sequence and `add_special_tokens=False`; this script itself accepts packed HDF5 only.
|
| 77 |
+
|
| 78 |
+
The model requires `trust_remote_code=True` to load its custom architecture. The requirements pin Transformers to the tested version, 4.56.0. Apache-2.0 notices in the model code are retained.
|
| 79 |
+
|
| 80 |
+
## Inference check
|
| 81 |
+
|
| 82 |
+
On 2026-10-02, the published model and script at commit `d5a62f1bbf483426314525fcff8695847f9fcdc4` were downloaded from the Hub and run on rows 0–1 of `test/fungi1/GCF_000002945.2.h5`, fetched from the data bucket by the script. The run passed on one NVIDIA H100 80 GB using BF16, PyTorch 2.7.1+cu128, Transformers 4.56.0, and batch size 2. Each row covered all 98,304 bases, with finite two-strand predictions and checked threshold/validity-mask outputs. This is a functional inference check, not an accuracy benchmark. See `inference-check.json` for the check summary.
|
| 83 |
+
|
| 84 |
+
### GenBank file inference check
|
| 85 |
+
|
| 86 |
+
On 2026-10-02, `infer_genbank.py` at commit `d429145e77373827ac93cc03ff04dcbdfab5723f` passed an end-to-end test on an NVIDIA H100 using BF16. The input included the complete 230,218-base yeast chromosome I record `NC_001133.9` (four overlapping windows), plus synthetic short and ambiguous-sequence edge cases. Gzipped GenBank and FASTA inputs produced identical predictions. Saved probabilities matched an independent window-by-window calculation and overlap average; coordinates, thresholded calls, invalid-position masks, multiple records, and overwrite refusal were checked. No script changes were required. See `genbank-inference-check.json`. This check establishes that the example runs correctly; it is not an accuracy benchmark.
|
README.md
CHANGED
|
@@ -1,106 +1,120 @@
|
|
| 1 |
---
|
| 2 |
-
license: mit
|
| 3 |
library_name: transformers
|
|
|
|
|
|
|
|
|
|
| 4 |
pipeline_tag: token-classification
|
| 5 |
tags:
|
| 6 |
-
-
|
| 7 |
-
-
|
| 8 |
-
-
|
| 9 |
-
-
|
| 10 |
-
-
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
# Carbon-A
|
| 14 |
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
Use `infer_genbank.py` for a local eukaryotic GenBank flat file (`.gb`, `.gbff`, or `.gz`). It reads the DNA sequence; existing feature annotations are not used as model inputs. Install the repository's `requirements.txt` and authenticate with `hf auth login`, then run:
|
| 28 |
|
| 29 |
```bash
|
| 30 |
-
|
|
|
|
| 31 |
```
|
| 32 |
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
python infer_genbank.py genome.fna.gz --format fasta --out-dir genome-predictions
|
| 37 |
-
```
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
|
| 45 |
-
|
| 46 |
|
| 47 |
```python
|
| 48 |
import json
|
| 49 |
from pathlib import Path
|
| 50 |
import numpy as np
|
| 51 |
|
| 52 |
-
out = Path("
|
| 53 |
record = json.loads((out / "summary.json").read_text())["records"][0]
|
| 54 |
-
with np.load(out / record["file"]) as
|
| 55 |
-
plus =
|
| 56 |
-
minus =
|
| 57 |
-
coding =
|
| 58 |
-
valid =
|
| 59 |
-
print(record["accession"], plus.shape, int(valid.sum()))
|
| 60 |
```
|
| 61 |
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
|
| 66 |
```bash
|
| 67 |
-
|
| 68 |
-
hf auth login
|
| 69 |
-
python infer_packed.py --num-rows 1 --out-dir predictions
|
| 70 |
```
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
For a local training or evaluation shard:
|
| 75 |
|
| 76 |
```bash
|
| 77 |
-
python infer_packed.py --h5 /path/to/shard.h5 --
|
| 78 |
```
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
|
| 86 |
-
-
|
| 87 |
-
- `cds_label_plus`, `cds_label_minus`: calls at `--threshold` (default 0.5).
|
| 88 |
-
- `cds_label_any`: 1 if either strand passes the threshold.
|
| 89 |
-
- `valid_plus`, `valid_minus`: positions eligible for label comparison.
|
| 90 |
-
- `reference_plus`, `reference_minus`: packed RefSeq targets, converted with `label != 0`.
|
| 91 |
|
| 92 |
-
|
| 93 |
|
| 94 |
-
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
|
| 100 |
-
##
|
| 101 |
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
##
|
| 105 |
|
| 106 |
-
|
|
|
|
| 1 |
---
|
|
|
|
| 2 |
library_name: transformers
|
| 3 |
+
license: mit
|
| 4 |
+
language:
|
| 5 |
+
- dna
|
| 6 |
pipeline_tag: token-classification
|
| 7 |
tags:
|
| 8 |
+
- dna
|
| 9 |
+
- biology
|
| 10 |
+
- genomics
|
| 11 |
+
- eukaryotes
|
| 12 |
+
- cds
|
| 13 |
+
- custom-code
|
| 14 |
---
|
| 15 |
|
| 16 |
+
# Carbon-A-1.2B
|
| 17 |
|
| 18 |
+
A DNA annotation model from the **Carbon** family.
|
| 19 |
|
| 20 |
+
**Carbon-A predicts protein-coding sequence (CDS) at single-base resolution in eukaryotic genomes.** It produces separate forward- and reverse-strand probabilities, preserving coding regions that overlap on opposite strands.
|
| 21 |
|
| 22 |
+
## Facts
|
| 23 |
|
| 24 |
+
- **1.2B-parameter DNA annotation model** with two strand-specific classification heads.
|
| 25 |
+
- **Tokenizer:** non-overlapping 6-mers; each DNA token represents six bases.
|
| 26 |
+
- **Sequence length:** 16,384 tokens (**98,304 bp**).
|
| 27 |
+
- **Inputs:** genomic DNA. The examples support GenBank, FASTA, and packed HDF5 files.
|
| 28 |
+
- **Outputs:** per-base probabilities for non-coding and CDS on each strand; binary calls use a configurable threshold, default **0.5**.
|
| 29 |
+
- **Training data:** RefSeq GCF assemblies from mammals, other vertebrates, invertebrates, plants, fungi, and protozoa.
|
| 30 |
+
- **Transformers interface:** `AutoModelForTokenClassification` with `trust_remote_code=True`.
|
| 31 |
+
- **Inference:** PyTorch SDPA, with BF16 on supported GPUs. A separate FlashAttention installation is not required.
|
| 32 |
|
| 33 |
+
## How to use
|
| 34 |
|
| 35 |
+
Download this repository, install its requirements in a Python 3.10+ environment with CUDA-enabled PyTorch, and authenticate with your Hugging Face account:
|
|
|
|
|
|
|
| 36 |
|
| 37 |
```bash
|
| 38 |
+
pip install -r requirements.txt
|
| 39 |
+
hf auth login
|
| 40 |
```
|
| 41 |
|
| 42 |
+
### Annotate a GenBank or FASTA file
|
| 43 |
|
| 44 |
+
The included [infer_genbank.py](infer_genbank.py) script reads the DNA sequence from each record and writes per-base predictions:
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
```bash
|
| 47 |
+
# GenBank flat file; gzip compression is supported.
|
| 48 |
+
python infer_genbank.py genome.gbff.gz --out-dir predictions
|
| 49 |
|
| 50 |
+
# FASTA input.
|
| 51 |
+
python infer_genbank.py genome.fna.gz --format fasta --out-dir predictions-fasta
|
| 52 |
+
```
|
| 53 |
|
| 54 |
+
Long records are processed in **98,304-base windows with 50% overlap**. Probabilities are averaged where windows overlap. Existing GenBank annotations are not model inputs.
|
| 55 |
|
| 56 |
+
Each record produces an `.npz` file named with its accession and record name, plus an entry in `summary.json`. To read the predictions:
|
| 57 |
|
| 58 |
```python
|
| 59 |
import json
|
| 60 |
from pathlib import Path
|
| 61 |
import numpy as np
|
| 62 |
|
| 63 |
+
out = Path("predictions")
|
| 64 |
record = json.loads((out / "summary.json").read_text())["records"][0]
|
| 65 |
+
with np.load(out / record["file"]) as pred:
|
| 66 |
+
plus = pred["cds_probability_plus"]
|
| 67 |
+
minus = pred["cds_probability_minus"]
|
| 68 |
+
coding = pred["cds_label_any"] # 1 if either strand passes the threshold
|
| 69 |
+
valid = pred["valid"]
|
|
|
|
| 70 |
```
|
| 71 |
|
| 72 |
+
Array index `i` corresponds to record base `i + 1` on both strands. Unscored positions have probability `NaN`, call `-1`, and `valid=False`.
|
| 73 |
|
| 74 |
+
### Run on packed training or evaluation data
|
| 75 |
+
|
| 76 |
+
The included [infer_packed.py](infer_packed.py) script can download a small evaluation file and predict one full-length row:
|
| 77 |
|
| 78 |
```bash
|
| 79 |
+
python infer_packed.py --num-rows 1 --out-dir predictions-packed
|
|
|
|
|
|
|
| 80 |
```
|
| 81 |
|
| 82 |
+
Or use a local shard:
|
|
|
|
|
|
|
| 83 |
|
| 84 |
```bash
|
| 85 |
+
python infer_packed.py --h5 /path/to/shard.h5 --num-rows 2 --out-dir predictions-local
|
| 86 |
```
|
| 87 |
|
| 88 |
+
Both scripts accept `--threshold` and `--revision <model-commit>`. See the **[inference guide](INFERENCE.md)** for output fields, tokenization, coordinate handling, and reproducible-run details.
|
| 89 |
+
|
| 90 |
+
## Training data
|
| 91 |
|
| 92 |
+
Examples were constructed from paired **RefSeq GBFF annotations and FASTA sequences**, using GCF-accessioned assemblies. Records at least 98,304 bp long were eligible. Domain-specific sliding windows increase exposure to less abundant groups, particularly fungi and protozoa.
|
| 93 |
|
| 94 |
+
CDS intervals are unioned across isoforms independently on each strand. Windows without annotated CDS are retained, exposing the model to both coding and non-coding genomic context.
|
| 95 |
|
| 96 |
+
The **[packed-data bucket](https://huggingface.co/buckets/HuggingFaceBio/Carbon-A-training-data)** contains training shards, the original evaluation panel, a PyTorch loader, and reuse instructions. Stored labels are converted to binary with `label != 0`; the loader handles signed values and special-token masking.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
## Evaluation
|
| 99 |
|
| 100 |
+
The inference examples have passed functional checks on an **NVIDIA H100**, using BF16, PyTorch 2.7.1, and Transformers 4.56.0:
|
| 101 |
|
| 102 |
+
- Two full-length packed evaluation windows, including strand outputs and thresholded calls.
|
| 103 |
+
- The complete **230,218-base yeast chromosome I** record, processed in four overlapping windows.
|
| 104 |
+
- Identical predictions when the same sequence is supplied as GenBank or FASTA.
|
| 105 |
+
- Independent checks of overlap averaging, coordinates, ambiguous bases, and short records.
|
| 106 |
|
| 107 |
+
These checks verify the inference code. **They do not measure agreement with reference CDS annotations.** Details are recorded in [the packed-data check](inference-check.json) and [the GenBank check](genbank-inference-check.json).
|
| 108 |
|
| 109 |
+
## Limitations
|
| 110 |
|
| 111 |
+
- Predictions represent **CDS occupancy**. Transcript paths, alternative isoforms, and complete gene structures are not resolved.
|
| 112 |
+
- The model is intended for **eukaryotic sequence**. Bacteria and viruses are outside its training scope.
|
| 113 |
+
- The GenBank/FASTA example excludes any 6-mer containing an ambiguous nucleotide, including an incomplete final 6-mer padded with `N`.
|
| 114 |
+
- Shorter records can be processed, but training used full-length 98,304-base windows.
|
| 115 |
+
- The supplied evaluation panel was used during training; it is not a newly held-out test set.
|
| 116 |
+
- Full-length inference on CPU is slow. The file example holds per-base arrays for one record in memory, so large chromosomes need more host RAM.
|
| 117 |
|
| 118 |
+
## License
|
| 119 |
|
| 120 |
+
MIT. Apache-2.0 notices in the model code are retained.
|
provenance.json
CHANGED
|
@@ -45,6 +45,7 @@
|
|
| 45 |
"infer_packed.py",
|
| 46 |
"infer_genbank.py",
|
| 47 |
"inference-check.json",
|
| 48 |
-
"genbank-inference-check.json"
|
|
|
|
| 49 |
]
|
| 50 |
}
|
|
|
|
| 45 |
"infer_packed.py",
|
| 46 |
"infer_genbank.py",
|
| 47 |
"inference-check.json",
|
| 48 |
+
"genbank-inference-check.json",
|
| 49 |
+
"INFERENCE.md"
|
| 50 |
]
|
| 51 |
}
|