A specialized framework for pre-training BERT models on genomic sequences with support for long sequence extrapolation using ALiBi attention. This project provides a complete solution for training language models on DNA data that can effectively handle sequences much longer than those seen during training.
This project implements a transformer-based architecture for genomic sequence modeling, with special focus on:
The resulting models can be used for various genomic tasks such as:
Unlike standard transformer attention which relies on positional embeddings limited to the training sequence length, ALiBi applies linear biases to attention scores based on the relative positions of tokens. This approach allows models to effectively process sequences much longer than those seen during training.
Benefits:
Custom SentencePiece BPE tokenizer designed specifically for genomic sequences (A, T, G, C).
Benefits:
Built with PyTorch Accelerate for efficient multi-GPU and multi-node training.
Benefits:
# Clone the repository
git clone https://github.com/yourusername/genomic-bert.git
cd genomic-bert
# Install dependencies
pip install -r requirements.txt
python train.py
--input_files path/to/your/genomic.fasta
--output_dir ./output
--attention_type alibi
--batch_size 16
| Option | Description |
|---|---|
--input_files |
Input genomic sequence files in FASTA format (required) |
--output_dir |
Output directory for model, tokenizer, and logs (required) |
--tokenizer_path |
Path to existing tokenizer (optional) |
--model_path |
Path to existing model checkpoint (optional) |
--resume_from_checkpoint |
Path to checkpoint to resume from, or ‘latest’ to find latest checkpoint |
--test_sequence_length |
Override sequence length for testing (useful for debugging) |
| Option | Description |
|---|---|
--force_gpu |
Force GPU usage even if distributed mode is active |
--gpu_ids |
Comma-separated list of GPU IDs to use (defaults to all available) |
| Option | Description |
|---|---|
--vocab_size |
Vocabulary size for BPE tokenizer (default: 4096) |
--tokenizer_sample_size |
Number of sequences to sample for tokenizer training (default: 100000) |
| Option | Description |
|---|---|
--hidden_size |
Hidden size of the model (default: 768) |
--num_hidden_layers |
Number of hidden layers (default: 12) |
--num_attention_heads |
Number of attention heads (default: 12) |
--dropout |
Dropout probability (default: 0.1) |
--attention_type |
Type of attention mechanism to use: "standard" or "alibi" (default: alibi) |
| Option | Description |
|---|---|
--pre_training_length |
Sequence length during pre-training (default: 512) |
--max_inference_length |
Maximum sequence length for inference (default: None = unlimited) |
--mlm_probability |
Probability of masking a token for MLM (default: 0.15) |
--chunk_size |
Base size of sequence chunks (default: 2000) |
--stride |
Stride for overlapping chunks (default: 1000) |
--sample_long_sequences |
Include longer sequences during training to help with extrapolation |
--max_safe_sequence_length |
Maximum safe sequence length for processing (default: 50000) |
--max_supported_model_length |
Maximum sequence length the model should support (default: 16384) |
| Option | Description |
|---|---|
--batch_size |
Batch size per GPU/TPU core/CPU (default: 16) |
--gradient_accumulation_steps |
Number of steps for gradient accumulation (default: 1) |
--epochs |
Number of training epochs (default: 3) |
--learning_rate |
Learning rate (default: 5e-5) |
--weight_decay |
Weight decay (default: 0.01) |
--warmup_steps |
Number of warmup steps (default: 10000) |
--max_grad_norm |
Maximum gradient norm (default: 1.0) |
--num_workers |
Number of data loader workers (default: 4) |
--lr_scheduler_type |
LR scheduler type: "linear", "cosine", "cosine_with_restarts", "polynomial", "constant", "constant_with_warmup" (default: linear) |
| Option | Description |
|---|---|
--logging_steps |
Log every X steps (default: 100) |
--checkpointing_steps |
Save a checkpoint every X steps (default: None) |
--save_steps |
Save a checkpoint every X steps (default: 1000) |
--save_total_limit |
Maximum number of checkpoints to keep (default: 3) |
--log_with_tensorboard |
Log training with TensorBoard |
| Option | Description |
|---|---|
--seed |
Random seed (default: 42) |
--debug |
Enable debug output |
python train.py
--input_files data/*.fasta
--output_dir ./multi_gpu_output
--attention_type alibi
--batch_size 8
--gradient_accumulation_steps 2
python train.py
--input_files path/to/your/genomic.fasta
--output_dir ./finetune_output
--model_path ./pretrained_model
--tokenizer_path ./pretrained_model/tokenizer
--learning_rate 2e-5
--epochs 1
The model automatically tests its ability to handle sequences of different lengths than those seen during training. You can customize this with:
python train.py
--input_files path/to/your/genomic.fasta
--output_dir ./output
--pre_training_length 512
--max_supported_model_length 16384
--sample_long_sequences
.
├── data/
│ ├── __init__.py
│ └── data_collator.py # Dataset and data collator implementations
├── modeling/
│ ├── __init__.py
│ └── alibi_attention.py # ALiBi attention implementation
├── tokenization/
│ ├── __init__.py
│ └── genomic_tokenizer.py # Genomic tokenizer implementation
├── training/
│ ├── __init__.py
│ ├── accelerate_utils.py # Accelerate utilities
│ └── train_utils.py # Training utilities
└── train.py # Main training script
The tokenizer is automatically trained on your input data unless you provide an existing tokenizer. You can customize vocabulary size and other parameters:
python train.py
--input_files path/to/your/genomic.fasta
--output_dir ./output
--vocab_size 8192
--tokenizer_sample_size 200000
--gradient_accumulation_steps to trade speed for memory.If you use this code in your research, please cite:
@article{tbd,
title={TBD},
author={TBD},
journal={arXiv preprint},
year={2025}
}
Imported from gh:codybum/DNABERT-PLUS. Source last updated 2025-03-15. Synced 2026-07-27.