File size: 8,768 Bytes
d3fa071
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""
Phase 3: Continue Pretraining
Demonstrates how to continue pretraining GeneMamba on your own data using masked LM objective.

Usage:
    python examples/3_continue_pretraining.py
"""

import torch
import numpy as np
from torch.utils.data import Dataset
from transformers import (
    AutoModelForMaskedLM,
    AutoTokenizer,
    Trainer,
    TrainingArguments,
    DataCollatorForLanguageModeling,
)


class PretrainingDataset(Dataset):
    """
    Dataset for pretraining/continued pretraining.
    Loads sequences and their lengths.
    """
    
    def __init__(self, input_ids_list, max_length=2048):
        self.input_ids_list = input_ids_list
        self.max_length = max_length
    
    def __len__(self):
        return len(self.input_ids_list)
    
    def __getitem__(self, idx):
        input_ids = self.input_ids_list[idx]
        
        # Pad or truncate to max_length
        if len(input_ids) >= self.max_length:
            input_ids = input_ids[:self.max_length]
        else:
            input_ids = np.pad(
                input_ids,
                (0, self.max_length - len(input_ids)),
                constant_values=1  # Pad token ID
            )
        
        return {
            "input_ids": torch.tensor(input_ids, dtype=torch.long),
        }


def create_mock_pretraining_data(n_sequences=5000, seq_len=2048):
    """Create mock single-cell sequences for pretraining."""
    
    print("Creating mock pretraining dataset...")
    
    # Create ranked gene sequences
    # In practice, these would come from your scRNA-seq data
    sequences = []
    for _ in range(n_sequences):
        # Random ranked sequence
        seq = np.random.randint(2, 25426, seq_len)
        sequences.append(seq)
    
    print(f"βœ“ Created {n_sequences} sequences of length {seq_len}")
    
    return sequences


def main():
    print("=" * 80)
    print("GeneMamba Phase 3: Continue Pretraining")
    print("=" * 80)
    
    # ============================================================
    # Step 1: Load pretrained model for masked LM
    # ============================================================
    print("\n[Step 1] Loading model for masked LM...")
    
    try:
        model = AutoModelForMaskedLM.from_pretrained(
            "GeneMamba-24l-512d",
            trust_remote_code=True,
            local_files_only=True,
        )
        tokenizer = AutoTokenizer.from_pretrained(
            "GeneMamba-24l-512d",
            trust_remote_code=True,
            local_files_only=True,
        )
    except Exception as e:
        print(f"Note: Could not load from hub ({e})")
        print("Using local initialization...")
        
        # Initialize locally
        from configuration_genemamba import GeneMambaConfig
        from modeling_genemamba import GeneMambaForMaskedLM
        
        config = GeneMambaConfig(
            vocab_size=25426,
            hidden_size=512,
            num_hidden_layers=24,
        )
        model = GeneMambaForMaskedLM(config)
        tokenizer = None
    
    print(f"βœ“ Model loaded")
    print(f"  - Architecture: {model.config.num_hidden_layers} layers, "
          f"hidden_size={model.config.hidden_size}")
    
    # ============================================================
    # Step 2: Prepare pretraining data
    # ============================================================
    print("\n[Step 2] Preparing pretraining dataset...")
    
    sequences = create_mock_pretraining_data(n_sequences=5000, seq_len=2048)
    
    # Split train/eval
    train_size = int(0.9 * len(sequences))
    train_sequences = sequences[:train_size]
    eval_sequences = sequences[train_size:]
    
    train_dataset = PretrainingDataset(train_sequences)
    eval_dataset = PretrainingDataset(eval_sequences)
    
    print(f"βœ“ Datasets created:")
    print(f"  - Training: {len(train_dataset)} samples")
    print(f"  - Evaluation: {len(eval_dataset)} samples")
    
    # ============================================================
    # Step 3: Set up data collator for MLM
    # ============================================================
    print("\n[Step 3] Setting up data collator...")
    
    if tokenizer is not None:
        data_collator = DataCollatorForLanguageModeling(
            tokenizer=tokenizer,
            mlm=True,
            mlm_probability=0.15,  # Mask 15% of tokens
        )
    else:
        # Custom collator if no tokenizer available
        class CustomDataCollator:
            def __call__(self, batch):
                input_ids = torch.stack([item["input_ids"] for item in batch])
                
                # Create masked labels (for MLM loss)
                labels = input_ids.clone()
                mask = torch.rand(input_ids.shape) < 0.15
                
                # Set input to [MASK] token (id=0)
                input_ids[mask] = 0
                
                # Set labels to -100 where not masked (loss ignores these)
                labels[~mask] = -100
                
                return {"input_ids": input_ids, "labels": labels}
        
        data_collator = CustomDataCollator()
    
    print(f"βœ“ Data collator ready (MLM probability: 0.15)")
    
    # ============================================================
    # Step 4: Set up training arguments
    # ============================================================
    print("\n[Step 4] Setting up training...")
    
    output_dir = "./pretrain_results"
    
    training_args = TrainingArguments(
        output_dir=output_dir,
        num_train_epochs=2,
        per_device_train_batch_size=16,
        per_device_eval_batch_size=16,
        learning_rate=2e-5,
        weight_decay=0.01,
        warmup_steps=500,
        logging_steps=100,
        eval_strategy="epoch",
        save_strategy="epoch",
        load_best_model_at_end=True,
        metric_for_best_model="eval_loss",
        report_to="none",  # Disable W&B
        seed=42,
    )
    
    print(f"βœ“ Training config:")
    print(f"  - Output dir: {output_dir}")
    print(f"  - Epochs: {training_args.num_train_epochs}")
    print(f"  - Batch size: {training_args.per_device_train_batch_size}")
    print(f"  - Learning rate: {training_args.learning_rate}")
    print(f"  - MLM masking: 15%")
    
    # ============================================================
    # Step 5: Train
    # ============================================================
    print("\n[Step 5] Starting continued pretraining...")
    
    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=train_dataset,
        eval_dataset=eval_dataset,
        data_collator=data_collator,
    )
    
    train_result = trainer.train()
    
    print(f"βœ“ Training complete!")
    print(f"  - Final training loss: {train_result.training_loss:.4f}")
    
    # ============================================================
    # Step 6: Evaluate
    # ============================================================
    print("\n[Step 6] Evaluating on held-out set...")
    
    eval_results = trainer.evaluate()
    
    print(f"βœ“ Evaluation Results:")
    for metric, value in eval_results.items():
        if isinstance(value, (int, float)):
            print(f"  - {metric}: {value:.4f}")
    
    # ============================================================
    # Step 7: Save model
    # ============================================================
    print("\n[Step 7] Saving continued pretrained model...")
    
    save_dir = "./genemamba_continued_pretrain"
    model.save_pretrained(save_dir)
    if tokenizer is not None:
        tokenizer.save_pretrained(save_dir)
    
    print(f"βœ“ Model saved to '{save_dir}'")
    
    # ============================================================
    # Step 8: Test model inference
    # ============================================================
    print("\n[Step 8] Testing inference on masked input...")
    
    model.eval()
    
    # Create sample input with masked tokens
    sample_input = torch.randint(2, 25426, (1, 2048))
    sample_input[0, :10] = 0  # Mask first 10 tokens
    
    with torch.no_grad():
        outputs = model(sample_input)
        logits = outputs.logits
        predictions = torch.argmax(logits, dim=-1)
    
    print(f"βœ“ Sample predictions generated")
    print(f"  - Input shape: {sample_input.shape}")
    print(f"  - Output logits shape: {logits.shape}")
    print(f"  - Top predicted genes (tokens): {predictions[0, :10].tolist()}")
    
    print("\n" + "=" * 80)
    print("Phase 3 Complete! Model ready for downstream tasks or further training.")
    print("=" * 80)
    
    return model, trainer


if __name__ == "__main__":
    model, trainer = main()