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
5# Load the tokenizer and model.
6tokenizer = AutoTokenizer.from_pretrained("GenerTeam/GENERator-v2-prokaryote-3b-base", trust_remote_code=True)
7model = AutoModelForCausalLM.from_pretrained("GenerTeam/GENERator-v2-prokaryote-3b-base")
8config = model.config
9
10max_length = config.max_position_embeddings
11
12# Define input sequences.
13sequences = [
14 "ATGAGGTGGCAAGAAATGGGCTAC",
15 "GAATTCCATGAGGCTATAGAATAATCTAAGAGAAAT"
16]
17
18def left_padding(sequence, padding_char='A', multiple=6):
19 remainder = len(sequence) % multiple
20 if remainder != 0:
21 padding_length = multiple - remainder
22 return padding_char * padding_length + sequence
23 return sequence
24
25def left_truncation(sequence, multiple=6):
26 remainder = len(sequence) % multiple
27 if remainder != 0:
28 return sequence[remainder:]
29 return sequence
30
31# Apply left_padding to all sequences
32# padded_sequences = [left_padding(seq) for seq in sequences]
33
34# Apply left_truncation to all sequences
35truncated_sequences = [left_truncation(seq) for seq in sequences]
36
37# Process the sequences
38sequences = [tokenizer.bos_token + sequence for sequence in truncated_sequences]
39
40# Tokenize the sequences
41tokenizer.padding_side = "left"
42inputs = tokenizer(
43 sequences,
44 add_special_tokens=False,
45 return_tensors="pt",
46 padding=True,
47 truncation=True,
48 max_length=max_length
49)
50
51# Generate the sequences
52with torch.inference_mode():
53 outputs = model.generate(**inputs, max_new_tokens=32, temperature=0.00001, top_k=1)
54
55# Decode the generated sequences
56decoded_sequences = tokenizer.batch_decode(outputs, skip_special_tokens=True)
57
58# Print the decoded sequences
59print(decoded_sequences)
60
61# It is expected to observe non-sense decoded sequences (e.g., 'AAAAAA')
62# The input sequences are too short to provide sufficient context.1
2import torch
3from transformers import AutoTokenizer, AutoModelForCausalLM
4
5# Load the tokenizer and model
6tokenizer = AutoTokenizer.from_pretrained("GENERator-v2-prokaryote-3b-base", trust_remote_code=True)
7model = AutoModelForCausalLM.from_pretrained("GENERator-v2-prokaryote-3b-base")
8
9# Get model configuration
10config = model.config
11max_length = config.max_position_embeddings
12
13# Define input sequences
14sequences = [
15 "ATGAGGTGGCAAGAAATGGGCTAC",
16 "GAATTCCATGAGGCTATAGAATAATCTAAGAGAAAT"
17]
18
19# Truncate each sequence to the nearest multiple of 6
20processed_sequences = [tokenizer.bos_token + seq[:len(seq)//6*6] for seq in sequences]
21
22# Tokenization
23tokenizer.padding_side = "right"
24inputs = tokenizer(
25 processed_sequences,
26 add_special_tokens=True,
27 return_tensors="pt",
28 padding=True,
29 truncation=True,
30 max_length=max_length
31)
32
33# Model Inference
34with torch.inference_mode():
35 outputs = model(**inputs, output_hidden_states=True)
36
37hidden_states = outputs.hidden_states[-1]
38attention_mask = inputs["attention_mask"]
39
40# Option 1: Last token (EOS) embedding
41last_token_indices = attention_mask.sum(dim=1) - 1
42eos_embeddings = hidden_states[torch.arange(hidden_states.size(0)), last_token_indices, :]
43
44# Option 2: Mean pooling over all tokens
45expanded_mask = attention_mask.unsqueeze(-1).expand(hidden_states.size()).to(torch.float32)
46sum_embeddings = torch.sum(hidden_states * expanded_mask, dim=1)
47mean_embeddings = sum_embeddings / expanded_mask.sum(dim=1)
48
49# Output
50print("EOS (Last Token) Embeddings:", eos_embeddings)
51print("Mean Pooling Embeddings:", mean_embeddings)
52
53# ============================================================================
54# Additional notes:
55# - The preprocessing step ensures sequences are multiples of 6 for 6-mer tokenizer
56# - For causal LM, the last token embedding (EOS) is commonly used
57# - Mean pooling considers all tokens including BOS and content tokens
58# - The choice depends on your downstream task requirements
59# - Both methods handle variable sequence lengths via attention mask
60# ============================================================================
61@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 = {Functional In-Context Learning in Genomic Language Models with Nucleotide-Level Supervision and Genome Compression},
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/01/29/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}
}