Views
No views yet
'A' (left padding);'<oov>' (out-of-vocabulary) token to the end of the token sequence. This can result in uninformative subsequent generations, such as repeated 'AAAAAA'.1
2import torch
3from transformers import AutoTokenizer, AutoModelForCausalLM
4
5model = AutoModelForCausalLM.from_pretrained(
6 "GenerTeam/GENERator-v2-eukaryote-1.2b-base",
7 attn_implementation="flash_attention_2",
8 trust_remote_code=True,
9 dtype=torch.bfloat16,
10).cuda().eval()
11
12tokenizer = AutoTokenizer.from_pretrained(
13 "GenerTeam/GENERator-v2-eukaryote-1.2b-base",
14 trust_remote_code=True,
15)
16
17# Define input sequences.
18sequences = [
19 "ATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCG",
20 "ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGT"
21]
22
23# Truncate each sequence to the nearest multiple of 6
24processed_sequences = ["<s>" + seq[len(seq)%6:] for seq in sequences]
25
26# Tokenize the sequences
27inputs = tokenizer(
28 processed_sequences,
29 add_special_tokens=False,
30 return_tensors="pt",
31 padding=True,
32 padding_side="left",
33).to("cuda")
34
35# Generate the sequences
36with torch.inference_mode():
37 outputs = model.generate(**inputs, max_new_tokens=32, do_sample=False)
38
39# Decode the generated sequences
40decoded_sequences = tokenizer.batch_decode(outputs, skip_special_tokens=True)
41
42# Print the decoded sequences
43print(decoded_sequences)1
2import torch
3from transformers import AutoTokenizer, AutoModelForCausalLM
4
5model = AutoModelForCausalLM.from_pretrained(
6 "GenerTeam/GENERator-v2-eukaryote-1.2b-base",
7 attn_implementation="flash_attention_2",
8 trust_remote_code=True,
9 dtype=torch.bfloat16,
10).cuda().eval()
11
12tokenizer = AutoTokenizer.from_pretrained(
13 "GenerTeam/GENERator-v2-eukaryote-1.2b-base",
14 trust_remote_code=True,
15)
16
17# Define input sequences.
18sequences = [
19 "ATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCG",
20 "ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGT"
21]
22
23# Truncate each sequence to the nearest multiple of 6
24processed_sequences = ["<s>" + seq[len(seq)%6:] + "<s>" for seq in sequences]
25
26# Tokenize the sequences
27inputs = tokenizer(
28 processed_sequences,
29 add_special_tokens=False,
30 return_tensors="pt",
31 padding=True,
32 padding_side="right",
33).to("cuda")
34
35with torch.inference_mode():
36 outputs = model(**inputs, output_hidden_states=True)
37
38hidden_states = outputs.hidden_states[-1]
39attention_mask = inputs["attention_mask"]
40
41# Option 1: Separator embedding (last <s> token)
42separator_indices = attention_mask.sum(dim=1) - 1
43separator_embeddings = hidden_states[torch.arange(hidden_states.size(0)), separator_indices, :]
44
45# Option 2: Content token embedding (last DNA token)
46last_dna_indices = attention_mask.sum(dim=1) - 2
47content_embeddings = hidden_states[torch.arange(hidden_states.size(0)), last_dna_indices, :]
48
49# Option 3: Mean pooling over all tokens
50expanded_mask = attention_mask.unsqueeze(-1).expand(hidden_states.size()).to(torch.float32)
51sum_embeddings = torch.sum(hidden_states * expanded_mask, dim=1)
52mean_embeddings = sum_embeddings / expanded_mask.sum(dim=1)
53
54# Output
55print("Separator (Last <s> Token) Embeddings:", separator_embeddings)
56print("Content (Last DNA Token) Embeddings:", content_embeddings)
57print("Mean Pooling Embeddings:", mean_embeddings)
58
59# ============================================================================
60# The choice depends on your downstream task requirements
61# - Separator embeddings and mean pooling embeddings capture species-level information.
62# - Content embeddings capture more localized gene-level information (e.g., strand, codon phase).
63# - More details are provided in GENERator-v2 tech report.
64# ============================================================================
651
2import torch
3from transformers import AutoModelForCausalLM
4
5model = AutoModelForCausalLM.from_pretrained(
6 "GenerTeam/GENERator-v2-eukaryote-1.2b-base",
7 attn_implementation="flash_attention_2",
8 trust_remote_code=True,
9 dtype=torch.bfloat16,
10).cuda().eval()
11
12# Sequence length does not need to be a multiple of 6
13sequences = "ATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGAT"
14
15with torch.no_grad():
16 bp_probs, actual_probs = model.score_sequence(sequences)
17
18print(bp_probs.shape) # [sequence_length, 4]
19print(actual_probs.shape) # [sequence_length]
20
21# bp_probs[i] = [P(A), P(T), P(C), P(G)] at position i (i ranges 0 ... len(seq)-1)
22# actual_probs[i] = probability assigned to the actual base in the input sequence
23
24# model.score_sequence() can also take a list of multiple inputs
25reference = "ATCGATCGATCGATCGATCGATCGATCGATCGATCGATCG"
26perturbed = "ATCGATCGATCGATCGATCGATCGCAGCAGCAGCAGATCG"
27
28with torch.no_grad():
29 bp_probs, actual_probs = model.score_sequence([reference, perturbed])
30
31scores = [torch.log(p.clamp_min(1e-12)).mean().item() for p in actual_probs]
32
33print(f"Log-Likelihood of Reference Sequence: {scores[0]:.4f}")
34print(f"Log-Likelihood of Perturbed Sequence: {scores[1]:.4f}")
35print(f"Reference is preferred: {scores[0] > scores[1]}")
36@article {li2026generator2,
author = {Li, Qiuyi and Zhan, Zhihao and Feng, Shikun and Zhu, Yiheng and He, Yuan and Wu, Wei and Shi, Zhenghang and Wang, Shengjie and Hu, Zongyong and Yang, Zhao and Li, Jiaoyang and Tang, Jian and Liu, Haiguang and Qin, Tao},
title = {GENERator-v2: Reconciling Coarse Tokenization with Single-Nucleotide Resolution in Genomic Language Modeling},
elocation-id = {2026.01.27.702015},
year = {2026},
doi = {10.64898/2026.01.27.702015},
publisher = {Cold Spring Harbor Laboratory},
URL = {https://www.biorxiv.org/content/early/2026/05/04/2026.01.27.702015},
journal = {bioRxiv}
}
@article{wu2025generator,
title={GENERator: a long-context generative genomic foundation model},
author={Wu, Wei and Li, Qiuyi and Li, Mingyang and Fu, Kun and Feng, Fuli and Ye, Jieping and Xiong, Hui and Wang, Zheng},
journal={arXiv preprint arXiv:2502.07272},
year={2025}
}