Views
No views yet

LlamaForCausalLM).<dna>...</dna> exactly as for the larger models. See the Carbon-3B card for tokenizer details.1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4repo = "HuggingFaceBio/Carbon-500M"
5tok = AutoTokenizer.from_pretrained(repo, trust_remote_code=True)
6model = AutoModelForCausalLM.from_pretrained(
7 repo, dtype=torch.bfloat16,
8).cuda().eval()
9
10prompt = "<dna>ATGCGCTAGCTACGATCGATCGTAGCTAGCTAGCTAGCTACG"
11inputs = tok(prompt, return_tensors="pt", add_special_tokens=False).to("cuda")
12out = model.generate(**inputs, max_new_tokens=64, do_sample=False)
13print(tok.decode(out[0][inputs.input_ids.shape[1]:], skip_special_tokens=True))assistant_model argument:1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4tok = AutoTokenizer.from_pretrained("HuggingFaceBio/Carbon-3B", trust_remote_code=True)
5draft = AutoModelForCausalLM.from_pretrained(
6 "HuggingFaceBio/Carbon-500M", dtype=torch.bfloat16
7).cuda().eval()
8target = AutoModelForCausalLM.from_pretrained(
9 "HuggingFaceBio/Carbon-3B", dtype=torch.bfloat16
10).cuda().eval()
11
12prompt = "<dna>ATGCGCTAGCTACGATCGATCGTAGCTAGCTAGCTAGCTACG"
13inputs = tok(prompt, return_tensors="pt", add_special_tokens=False).to("cuda")
14out = target.generate(
15 **inputs, max_new_tokens=256, do_sample=False,
16 assistant_model=draft,
17)
18print(tok.decode(out[0][inputs.input_ids.shape[1]:], skip_special_tokens=True))fns branch loads custom modeling code for Factorized Nucleotide Supervision (FNS). Carbon still uses its efficient 6-mer tokenizer, but during generation each selected 6-mer is assembled from six per-position nucleotide distributions, giving base-pair-level control over decoded DNA. Use this branch when you need exact base-pair counts, per-position masks, or temperature/top-p behavior applied at the nucleotide level rather than over the 4,096-way 6-mer distribution:1import math
2import torch
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5model_id = "HuggingFaceBio/Carbon-500M"
6revision = "fns"
7device = "cuda"
8
9tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision, trust_remote_code=True)
10model = AutoModelForCausalLM.from_pretrained(
11 model_id,
12 revision=revision,
13 trust_remote_code=True,
14 dtype=torch.bfloat16,
15).to(device).eval()
16
17context = "ATGCGCTAGCTACGATCGATCGTAGCTAGCTAGCTAGCTACG"
18n_bp = 60
19
20inputs = tokenizer(f"<dna>{context}", return_tensors="pt", add_special_tokens=False).to(device)
21
22with torch.no_grad():
23 output_ids = model.generate(
24 **inputs,
25 max_new_tokens=math.ceil(n_bp / tokenizer.k),
26 do_sample=False,
27 pad_token_id=tokenizer.eos_token_id,
28 )
29
30generated_ids = output_ids[0, inputs.input_ids.shape[1]:]
31generated_dna = tokenizer.decode(generated_ids, skip_special_tokens=True)[:n_bp]
32
33print(generated_dna)score_sequence(), which returns the probability assigned to the observed base at each position. Taking the mean log probability gives a base-pair-level sequence score, where higher values indicate higher model likelihood:1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_id = "HuggingFaceBio/Carbon-500M"
5revision = "fns"
6device = "cuda"
7
8tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision, trust_remote_code=True)
9model = AutoModelForCausalLM.from_pretrained(
10 model_id,
11 revision=revision,
12 trust_remote_code=True,
13 dtype=torch.bfloat16,
14).to(device).eval()
15
16reference = "GGGCTATAAAGGCCATCGATCGATCGATCGATCGATCGATCG"
17perturbed = "GGGCGCGCGCGGCCATCGATCGATCGATCGATCGATCGATCG"
18
19with torch.no_grad():
20 bp_probs, actual_probs = model.score_sequence([reference, perturbed])
21
22scores = [torch.log(p.clamp_min(1e-12)).mean().item() for p in actual_probs]
23
24print(f"reference mean bp logp: {scores[0]:.4f}")
25print(f"perturbed mean bp logp: {scores[1]:.4f}")
26print(f"reference preferred: {scores[0] > scores[1]}")@article{allal2026carbon,
title={Carbon: Decoding the Language of Life},
author={Allal, Loubna Ben and Li, Qiuyi and Fiusco, Maurizio and Tunstall, Lewis and Rasul, Kashif and Beeching, Ed and Aubakirova, Dana and Pati{\~n}o, Carlos and Frere, Thibaud and Lozhkov, Anton and others},
journal={bioRxiv},
pages={2026--05},
year={2026},
publisher={Cold Spring Harbor Laboratory}
}