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-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-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-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-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="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: Last token embedding
42last_token_indices = attention_mask.sum(dim=1) - 1
43last_token_embeddings = hidden_states[torch.arange(hidden_states.size(0)), last_token_indices, :]
44
45# Option 2: Mean pooling over all tokens
46expanded_mask = attention_mask.unsqueeze(-1).expand(hidden_states.size()).to(torch.float32)
47sum_embeddings = torch.sum(hidden_states * expanded_mask, dim=1)
48mean_embeddings = sum_embeddings / expanded_mask.sum(dim=1)
49
50# Output
51print("Last Token Embeddings:", last_token_embeddings)
52print("Mean Pooling Embeddings:", mean_embeddings)
53
54# ============================================================================
55# The choice depends on your downstream task requirements
56# - Last token embeddings capture more localized gene-level information (e.g., strand, codon phase).
57# - Mean pooling embeddings capture species-level information.
58# ============================================================================
59@misc{wu2025generator,
title={GENERator: A Long-Context Generative Genomic Foundation Model},
author={Wei Wu and Qiuyi Li and Mingyang Li and Kun Fu and Fuli Feng and Jieping Ye and Hui Xiong and Zheng Wang},
year={2025},
eprint={2502.07272},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2502.07272},
}