mineself2016 commited on
Commit
b8d89c6
·
verified ·
1 Parent(s): 51aef22

Update continue pretraining docs and tokenizer details

Browse files
Files changed (1) hide show
  1. examples/3_continue_pretraining.py +43 -33
examples/3_continue_pretraining.py CHANGED
@@ -8,13 +8,13 @@ Usage:
8
 
9
  import torch
10
  import numpy as np
 
11
  from torch.utils.data import Dataset
12
  from transformers import (
13
  AutoModelForMaskedLM,
14
  AutoTokenizer,
15
  Trainer,
16
  TrainingArguments,
17
- DataCollatorForLanguageModeling,
18
  )
19
 
20
 
@@ -67,6 +67,28 @@ def create_mock_pretraining_data(n_sequences=5000, seq_len=2048):
67
  return sequences
68
 
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  def main():
71
  print("=" * 80)
72
  print("GeneMamba Phase 3: Continue Pretraining")
@@ -78,15 +100,13 @@ def main():
78
  print("\n[Step 1] Loading model for masked LM...")
79
 
80
  try:
81
- model = AutoModelForMaskedLM.from_pretrained(
82
- "GeneMamba-24l-512d",
83
  trust_remote_code=True,
84
- local_files_only=True,
85
  )
86
- tokenizer = AutoTokenizer.from_pretrained(
87
- "GeneMamba-24l-512d",
88
  trust_remote_code=True,
89
- local_files_only=True,
90
  )
91
  except Exception as e:
92
  print(f"Note: Could not load from hub ({e})")
@@ -102,11 +122,21 @@ def main():
102
  num_hidden_layers=24,
103
  )
104
  model = GeneMambaForMaskedLM(config)
105
- tokenizer = None
 
 
 
 
 
106
 
107
  print(f"✓ Model loaded")
108
  print(f" - Architecture: {model.config.num_hidden_layers} layers, "
109
  f"hidden_size={model.config.hidden_size}")
 
 
 
 
 
110
 
111
  # ============================================================
112
  # Step 2: Prepare pretraining data
@@ -132,31 +162,11 @@ def main():
132
  # ============================================================
133
  print("\n[Step 3] Setting up data collator...")
134
 
135
- if tokenizer is not None:
136
- data_collator = DataCollatorForLanguageModeling(
137
- tokenizer=tokenizer,
138
- mlm=True,
139
- mlm_probability=0.15, # Mask 15% of tokens
140
- )
141
- else:
142
- # Custom collator if no tokenizer available
143
- class CustomDataCollator:
144
- def __call__(self, batch):
145
- input_ids = torch.stack([item["input_ids"] for item in batch])
146
-
147
- # Create masked labels (for MLM loss)
148
- labels = input_ids.clone()
149
- mask = torch.rand(input_ids.shape) < 0.15
150
-
151
- # Set input to [MASK] token (id=0)
152
- input_ids[mask] = 0
153
-
154
- # Set labels to -100 where not masked (loss ignores these)
155
- labels[~mask] = -100
156
-
157
- return {"input_ids": input_ids, "labels": labels}
158
-
159
- data_collator = CustomDataCollator()
160
 
161
  print(f"✓ Data collator ready (MLM probability: 0.15)")
162
 
 
8
 
9
  import torch
10
  import numpy as np
11
+ from pathlib import Path
12
  from torch.utils.data import Dataset
13
  from transformers import (
14
  AutoModelForMaskedLM,
15
  AutoTokenizer,
16
  Trainer,
17
  TrainingArguments,
 
18
  )
19
 
20
 
 
67
  return sequences
68
 
69
 
70
+ class GeneMambaMLMCollator:
71
+ """GeneMamba MLM collator using [UNK] replacement for masked positions."""
72
+
73
+ def __init__(self, pad_token_id: int, unk_token_id: int, mlm_probability: float = 0.15):
74
+ self.pad_token_id = pad_token_id
75
+ self.unk_token_id = unk_token_id
76
+ self.mlm_probability = mlm_probability
77
+
78
+ def __call__(self, features):
79
+ input_ids = torch.stack([f["input_ids"] for f in features])
80
+ labels = input_ids.clone()
81
+
82
+ prob = torch.full(labels.shape, self.mlm_probability)
83
+ mask_positions = torch.bernoulli(prob).bool()
84
+ mask_positions &= input_ids.ne(self.pad_token_id)
85
+
86
+ labels[~mask_positions] = -100
87
+ input_ids[mask_positions] = self.unk_token_id
88
+
89
+ return {"input_ids": input_ids, "labels": labels}
90
+
91
+
92
  def main():
93
  print("=" * 80)
94
  print("GeneMamba Phase 3: Continue Pretraining")
 
100
  print("\n[Step 1] Loading model for masked LM...")
101
 
102
  try:
103
+ tokenizer = AutoTokenizer.from_pretrained(
104
+ "mineself2016/GeneMamba",
105
  trust_remote_code=True,
 
106
  )
107
+ model = AutoModelForMaskedLM.from_pretrained(
108
+ "mineself2016/GeneMamba",
109
  trust_remote_code=True,
 
110
  )
111
  except Exception as e:
112
  print(f"Note: Could not load from hub ({e})")
 
122
  num_hidden_layers=24,
123
  )
124
  model = GeneMambaForMaskedLM(config)
125
+ project_root = Path(__file__).resolve().parent.parent
126
+ tokenizer = AutoTokenizer.from_pretrained(
127
+ str(project_root),
128
+ trust_remote_code=True,
129
+ local_files_only=True,
130
+ )
131
 
132
  print(f"✓ Model loaded")
133
  print(f" - Architecture: {model.config.num_hidden_layers} layers, "
134
  f"hidden_size={model.config.hidden_size}")
135
+ print(f" - Vocab size: {tokenizer.vocab_size}")
136
+ print(f" - [UNK] token/id: {tokenizer.unk_token}/{tokenizer.unk_token_id}")
137
+ print(f" - [PAD] token/id: {tokenizer.pad_token}/{tokenizer.pad_token_id}")
138
+ print(f" - [CLS] token/id: {tokenizer.cls_token}/{tokenizer.cls_token_id}")
139
+ print(f" - [MASK] token/id: {tokenizer.mask_token}/{tokenizer.mask_token_id}")
140
 
141
  # ============================================================
142
  # Step 2: Prepare pretraining data
 
162
  # ============================================================
163
  print("\n[Step 3] Setting up data collator...")
164
 
165
+ data_collator = GeneMambaMLMCollator(
166
+ pad_token_id=tokenizer.pad_token_id,
167
+ unk_token_id=tokenizer.unk_token_id,
168
+ mlm_probability=0.15,
169
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
 
171
  print(f"✓ Data collator ready (MLM probability: 0.15)")
172