Update pretrain.py
Browse files- pretrain.py +459 -501
pretrain.py
CHANGED
|
@@ -1,502 +1,460 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
import
|
| 9 |
-
from
|
| 10 |
-
import
|
| 11 |
-
from
|
| 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 |
-
self.
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
#
|
| 59 |
-
self.
|
| 60 |
-
self.
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
self.
|
| 66 |
-
self.
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
self.
|
| 71 |
-
self.
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
self.
|
| 81 |
-
self.
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
self.
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
logger.info(f"
|
| 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 |
-
if
|
| 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 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
logger.info("
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
'
|
| 415 |
-
'
|
| 416 |
-
'
|
| 417 |
-
'
|
| 418 |
-
'
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
'
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
max_seq_len=config['max_seq_len'],
|
| 461 |
-
dropout=config['dropout'],
|
| 462 |
-
use_moe=config['use_moe'],
|
| 463 |
-
use_gradient_checkpointing=True,
|
| 464 |
-
rope_scaling_type="yarn",
|
| 465 |
-
use_multimodal_fusion=False,
|
| 466 |
-
use_contrastive=False
|
| 467 |
-
)
|
| 468 |
-
|
| 469 |
-
# 创建数据加载器
|
| 470 |
-
logger.info(f"\nCreating dataloader (mix: {config['data_mix']})...")
|
| 471 |
-
dataloader = create_pretrain_dataloader(
|
| 472 |
-
mix_name=config['data_mix'],
|
| 473 |
-
tokenizer=tokenizer,
|
| 474 |
-
batch_size=config['batch_size'],
|
| 475 |
-
num_workers=config['num_workers'],
|
| 476 |
-
max_length=config['max_length']
|
| 477 |
-
)
|
| 478 |
-
|
| 479 |
-
# 创建训练器
|
| 480 |
-
trainer = PreTrainer(
|
| 481 |
-
model=model,
|
| 482 |
-
tokenizer=tokenizer,
|
| 483 |
-
learning_rate=config['learning_rate'],
|
| 484 |
-
weight_decay=config['weight_decay'],
|
| 485 |
-
warmup_steps=config['warmup_steps'],
|
| 486 |
-
max_steps=config['max_steps'],
|
| 487 |
-
gradient_accumulation_steps=config['gradient_accumulation_steps'],
|
| 488 |
-
max_grad_norm=config['max_grad_norm'],
|
| 489 |
-
log_interval=config['log_interval'],
|
| 490 |
-
save_interval=config['save_interval'],
|
| 491 |
-
checkpoint_dir=config['checkpoint_dir'],
|
| 492 |
-
loss_log_file=config['loss_log_file']
|
| 493 |
-
)
|
| 494 |
-
|
| 495 |
-
# 🔧 开始训练(从头开始,不要用旧的checkpoint)
|
| 496 |
-
logger.info("\n🚀 Starting fresh training with fixes...\n")
|
| 497 |
-
trainer.train(dataloader, resume_from="/root/step_6500.pt")
|
| 498 |
-
#trainer.train(dataloader)
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
if __name__ == "__main__":
|
| 502 |
main()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from transformers import AutoTokenizer
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
import logging
|
| 7 |
+
from tqdm import tqdm
|
| 8 |
+
import json
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
from model import MultiModalDenseTransformer
|
| 11 |
+
from data_loader import create_pretrain_dataloader
|
| 12 |
+
|
| 13 |
+
logging.basicConfig(
|
| 14 |
+
level=logging.INFO,
|
| 15 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 16 |
+
)
|
| 17 |
+
logger = logging.getLogger(__name__)
|
| 18 |
+
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class PreTrainer:
|
| 22 |
+
def __init__(
|
| 23 |
+
self,
|
| 24 |
+
model: MultiModalDenseTransformer,
|
| 25 |
+
tokenizer,
|
| 26 |
+
learning_rate: float = 3e-4,
|
| 27 |
+
weight_decay: float = 0.1,
|
| 28 |
+
warmup_steps: int = 1000,
|
| 29 |
+
max_steps: int = 100000,
|
| 30 |
+
gradient_accumulation_steps: int = 16,
|
| 31 |
+
max_grad_norm: float = 1.0,
|
| 32 |
+
log_interval: int = 10,
|
| 33 |
+
save_interval: int = 1000,
|
| 34 |
+
checkpoint_dir: str = "checkpoints/pretrain",
|
| 35 |
+
loss_log_file: str = "checkpoints/pretrain/train_loss.log"
|
| 36 |
+
):
|
| 37 |
+
self.model = model
|
| 38 |
+
self.tokenizer = tokenizer
|
| 39 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 40 |
+
|
| 41 |
+
self.model.to(self.device)
|
| 42 |
+
|
| 43 |
+
self.optimizer = torch.optim.AdamW(
|
| 44 |
+
model.parameters(),
|
| 45 |
+
lr=learning_rate,
|
| 46 |
+
weight_decay=weight_decay,
|
| 47 |
+
betas=(0.9, 0.95),
|
| 48 |
+
eps=1e-8
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
from torch.optim.lr_scheduler import CosineAnnealingWarmRestarts
|
| 52 |
+
|
| 53 |
+
self.warmup_steps = warmup_steps
|
| 54 |
+
self.max_lr = learning_rate
|
| 55 |
+
self.min_lr = learning_rate * 0.1
|
| 56 |
+
self.current_step = 0
|
| 57 |
+
|
| 58 |
+
# 混合精度
|
| 59 |
+
self.use_amp = torch.cuda.is_available()
|
| 60 |
+
self.scaler = torch.amp.GradScaler('cuda', enabled=self.use_amp)
|
| 61 |
+
|
| 62 |
+
# 训练参数
|
| 63 |
+
self.gradient_accumulation_steps = gradient_accumulation_steps
|
| 64 |
+
self.max_grad_norm = max_grad_norm
|
| 65 |
+
self.max_steps = max_steps
|
| 66 |
+
self.log_interval = log_interval
|
| 67 |
+
self.save_interval = save_interval
|
| 68 |
+
|
| 69 |
+
# Checkpoint管理
|
| 70 |
+
self.checkpoint_dir = Path(checkpoint_dir)
|
| 71 |
+
self.checkpoint_dir.mkdir(parents=True, exist_ok=True)
|
| 72 |
+
|
| 73 |
+
# 损失日志
|
| 74 |
+
self.loss_log_file = Path(loss_log_file)
|
| 75 |
+
self.loss_log_file.parent.mkdir(parents=True, exist_ok=True)
|
| 76 |
+
|
| 77 |
+
# 训练状态
|
| 78 |
+
self.global_step = 0
|
| 79 |
+
self.tokens_seen = 0
|
| 80 |
+
self.running_loss = 0.0
|
| 81 |
+
self.best_loss = float('inf')
|
| 82 |
+
|
| 83 |
+
logger.info(f"PreTrainer initialized:")
|
| 84 |
+
logger.info(f" Device: {self.device}")
|
| 85 |
+
logger.info(f" Learning Rate: {learning_rate}")
|
| 86 |
+
logger.info(f" Max Steps: {max_steps}")
|
| 87 |
+
logger.info(f" Gradient Accumulation: {gradient_accumulation_steps}")
|
| 88 |
+
logger.info(f" Effective Batch Size: {gradient_accumulation_steps}")
|
| 89 |
+
logger.info(f" Mixed Precision: {self.use_amp}")
|
| 90 |
+
|
| 91 |
+
def _get_lr(self) -> float:
|
| 92 |
+
"""手动计算学习率(Warmup + Cosine)"""
|
| 93 |
+
if self.current_step < self.warmup_steps:
|
| 94 |
+
# Linear warmup
|
| 95 |
+
return self.max_lr * (self.current_step / self.warmup_steps)
|
| 96 |
+
else:
|
| 97 |
+
# Cosine decay
|
| 98 |
+
progress = (self.current_step - self.warmup_steps) / (self.max_steps - self.warmup_steps)
|
| 99 |
+
return self.min_lr + (self.max_lr - self.min_lr) * 0.5 * (1 + torch.cos(torch.tensor(progress * 3.14159)))
|
| 100 |
+
|
| 101 |
+
def _set_lr(self, lr: float):
|
| 102 |
+
"""设置学习率"""
|
| 103 |
+
for param_group in self.optimizer.param_groups:
|
| 104 |
+
param_group['lr'] = lr
|
| 105 |
+
|
| 106 |
+
def train_step(self, batch: dict) -> dict:
|
| 107 |
+
input_ids = batch['input_ids'].to(self.device)
|
| 108 |
+
attention_mask = batch['attention_mask'].to(self.device)
|
| 109 |
+
batch_size, seq_len = input_ids.shape
|
| 110 |
+
position_ids= torch.zeros_like(input_ids)
|
| 111 |
+
|
| 112 |
+
for i in range(batch_size):
|
| 113 |
+
non_pad_mask = attention_mask[i].bool()
|
| 114 |
+
if non_pad_mask.any():
|
| 115 |
+
positions = torch.cumsum(non_pad_mask.long(), dim=0) -1
|
| 116 |
+
position_ids[i]=positions * non_pad_mask.long()
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
# 准备输入
|
| 121 |
+
input_data = {
|
| 122 |
+
'segments': [{
|
| 123 |
+
'type': 'text',
|
| 124 |
+
'data': input_ids,
|
| 125 |
+
'modality_id': 0
|
| 126 |
+
}]
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
# 前向传播
|
| 130 |
+
with torch.amp.autocast('cuda', enabled=self.use_amp):
|
| 131 |
+
outputs = self.model(
|
| 132 |
+
input_data,
|
| 133 |
+
attention_mask=attention_mask,
|
| 134 |
+
position_ids=position_ids)
|
| 135 |
+
logits = outputs['logits']
|
| 136 |
+
|
| 137 |
+
# 计算损失(标准自回归)
|
| 138 |
+
shift_logits = logits[:, :-1, :].contiguous()
|
| 139 |
+
shift_labels = input_ids[:, 1:].contiguous()
|
| 140 |
+
shift_attention_mask = attention_mask[:, 1:].contiguous()
|
| 141 |
+
|
| 142 |
+
loss = F.cross_entropy(
|
| 143 |
+
shift_logits.view(-1, shift_logits.size(-1)),
|
| 144 |
+
shift_labels.view(-1),
|
| 145 |
+
reduction='none'
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
# 应用mask
|
| 149 |
+
loss = (loss * shift_attention_mask.view(-1)).sum() / (shift_attention_mask.sum() + 1e-8)
|
| 150 |
+
loss_for_backward = loss / self.gradient_accumulation_steps
|
| 151 |
+
|
| 152 |
+
self.scaler.scale(loss_for_backward).backward()
|
| 153 |
+
self.tokens_seen += attention_mask.sum().item()
|
| 154 |
+
|
| 155 |
+
return {
|
| 156 |
+
'loss': loss.item(), # 返回真实的、未缩放的loss
|
| 157 |
+
'lr': self.optimizer.param_groups[0]['lr']
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
def optimizer_step(self):
|
| 161 |
+
"""优化器步骤"""
|
| 162 |
+
# Unscale梯度
|
| 163 |
+
self.scaler.unscale_(self.optimizer)
|
| 164 |
+
|
| 165 |
+
# 梯度裁剪
|
| 166 |
+
grad_norm = torch.nn.utils.clip_grad_norm_(
|
| 167 |
+
self.model.parameters(),
|
| 168 |
+
self.max_grad_norm
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
# 更新参数
|
| 172 |
+
self.scaler.step(self.optimizer)
|
| 173 |
+
self.scaler.update()
|
| 174 |
+
self.optimizer.zero_grad(set_to_none=True)
|
| 175 |
+
|
| 176 |
+
# 更新学习率
|
| 177 |
+
self.current_step += 1
|
| 178 |
+
self.global_step += 1
|
| 179 |
+
lr = self._get_lr()
|
| 180 |
+
self._set_lr(lr)
|
| 181 |
+
|
| 182 |
+
return grad_norm.item()
|
| 183 |
+
|
| 184 |
+
def _write_loss_to_txt(self, step, avg_loss, lr, tokens_seen):
|
| 185 |
+
"""写入损失日志"""
|
| 186 |
+
log_content = (
|
| 187 |
+
f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] "
|
| 188 |
+
f"Step: {step}/{self.max_steps}, "
|
| 189 |
+
f"Average Loss: {avg_loss:.4f}, "
|
| 190 |
+
f"Learning Rate: {lr:.2e}, "
|
| 191 |
+
f"Tokens Seen: {tokens_seen/1e9:.2f}B\n"
|
| 192 |
+
)
|
| 193 |
+
with open(self.loss_log_file, 'a', encoding='utf-8') as f:
|
| 194 |
+
f.write(log_content)
|
| 195 |
+
|
| 196 |
+
def train(self, dataloader, resume_from=None):
|
| 197 |
+
"""训练循环"""
|
| 198 |
+
logger.info("\n" + "="*80)
|
| 199 |
+
logger.info("Starting Pre-Training (Fixed Version)")
|
| 200 |
+
logger.info("="*80 + "\n")
|
| 201 |
+
|
| 202 |
+
# 恢复训练
|
| 203 |
+
if resume_from:
|
| 204 |
+
self.load_checkpoint(resume_from)
|
| 205 |
+
|
| 206 |
+
# 初始化日志
|
| 207 |
+
if not self.loss_log_file.exists():
|
| 208 |
+
with open(self.loss_log_file, 'w', encoding='utf-8') as f:
|
| 209 |
+
f.write(" Fixed Training Log (Real Loss Values)\n")
|
| 210 |
+
f.write("="*80 + "\n")
|
| 211 |
+
|
| 212 |
+
self.model.train()
|
| 213 |
+
progress_bar = tqdm(total=self.max_steps, initial=self.global_step)
|
| 214 |
+
|
| 215 |
+
step_in_accumulation = 0
|
| 216 |
+
accumulated_loss = 0.0
|
| 217 |
+
|
| 218 |
+
batches_to_skip = self.global_step * self.gradient_accumulation_steps
|
| 219 |
+
|
| 220 |
+
logger.info(f"Current Global Step: {self.global_step}")
|
| 221 |
+
if batches_to_skip > 0:
|
| 222 |
+
logger.info(f" Resuming: Need to skip {batches_to_skip} batches to restore data state...")
|
| 223 |
+
logger.info("This might take a while depending on network/disk speed...")
|
| 224 |
+
|
| 225 |
+
# 创建迭代器
|
| 226 |
+
data_iterator = iter(dataloader)
|
| 227 |
+
|
| 228 |
+
skipped = 0
|
| 229 |
+
if batches_to_skip > 0:
|
| 230 |
+
with tqdm(total=batches_to_skip, desc="Skipping trained batches", unit="batch") as skip_pbar:
|
| 231 |
+
while skipped < batches_to_skip:
|
| 232 |
+
try:
|
| 233 |
+
# 只取数据,不进模型,不计算梯度
|
| 234 |
+
_ = next(data_iterator)
|
| 235 |
+
skipped += 1
|
| 236 |
+
skip_pbar.update(1)
|
| 237 |
+
except StopIteration:
|
| 238 |
+
logger.error("Dataset exhausted during skipping! Check your dataset size or max_steps.")
|
| 239 |
+
return
|
| 240 |
+
|
| 241 |
+
logger.info(" Data fast-forward complete. Resuming training...")
|
| 242 |
+
|
| 243 |
+
try:
|
| 244 |
+
while True:
|
| 245 |
+
try:
|
| 246 |
+
batch = next(data_iterator)
|
| 247 |
+
except StopIteration:
|
| 248 |
+
break
|
| 249 |
+
|
| 250 |
+
if batch is None or batch['input_ids'].size(0) == 0:
|
| 251 |
+
continue
|
| 252 |
+
stats = self.train_step(batch)
|
| 253 |
+
step_in_accumulation += 1
|
| 254 |
+
accumulated_loss += stats['loss']
|
| 255 |
+
|
| 256 |
+
if step_in_accumulation >= self.gradient_accumulation_steps:
|
| 257 |
+
avg_step_loss = accumulated_loss / self.gradient_accumulation_steps
|
| 258 |
+
grad_norm = self.optimizer_step()
|
| 259 |
+
stats['grad_norm'] = grad_norm
|
| 260 |
+
stats['loss'] = avg_step_loss
|
| 261 |
+
self.running_loss += avg_step_loss
|
| 262 |
+
|
| 263 |
+
step_in_accumulation = 0
|
| 264 |
+
accumulated_loss = 0.0
|
| 265 |
+
progress_bar.update(1)
|
| 266 |
+
progress_bar.set_postfix({
|
| 267 |
+
'loss': f"{stats['loss']:.4f}",
|
| 268 |
+
'lr': f"{stats['lr']:.2e}",
|
| 269 |
+
'tokens': f"{self.tokens_seen/1e9:.2f}B",
|
| 270 |
+
'grad': f"{grad_norm:.2f}"
|
| 271 |
+
})
|
| 272 |
+
|
| 273 |
+
# 日志记录
|
| 274 |
+
if self.global_step % self.log_interval == 0:
|
| 275 |
+
avg_loss = self.running_loss / self.log_interval
|
| 276 |
+
|
| 277 |
+
logger.info(
|
| 278 |
+
f"Step {self.global_step}/{self.max_steps} | "
|
| 279 |
+
f"Loss: {avg_loss:.4f} | "
|
| 280 |
+
f"LR: {stats['lr']:.2e} | "
|
| 281 |
+
f"GradNorm: {grad_norm:.2f} | "
|
| 282 |
+
f"Tokens: {self.tokens_seen/1e9:.2f}B"
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
if avg_loss < self.best_loss:
|
| 286 |
+
self.best_loss = avg_loss
|
| 287 |
+
logger.info(f" New best loss: {self.best_loss:.4f}")
|
| 288 |
+
|
| 289 |
+
self._write_loss_to_txt(
|
| 290 |
+
step=self.global_step,
|
| 291 |
+
avg_loss=avg_loss,
|
| 292 |
+
lr=stats['lr'],
|
| 293 |
+
tokens_seen=self.tokens_seen
|
| 294 |
+
)
|
| 295 |
+
self.running_loss = 0.0
|
| 296 |
+
|
| 297 |
+
# 保存checkpoint
|
| 298 |
+
if self.global_step % self.save_interval == 0:
|
| 299 |
+
self.save_checkpoint(
|
| 300 |
+
self.checkpoint_dir / f"step_{self.global_step}.pt"
|
| 301 |
+
)
|
| 302 |
+
|
| 303 |
+
# 完成训练
|
| 304 |
+
if self.global_step >= self.max_steps:
|
| 305 |
+
break
|
| 306 |
+
|
| 307 |
+
except KeyboardInterrupt:
|
| 308 |
+
self.save_checkpoint(
|
| 309 |
+
self.checkpoint_dir / f"interrupted_step_{self.global_step}.pt"
|
| 310 |
+
)
|
| 311 |
+
|
| 312 |
+
finally:
|
| 313 |
+
progress_bar.close()
|
| 314 |
+
|
| 315 |
+
logger.info("\n" + "="*80)
|
| 316 |
+
logger.info("Pre-Training Complete!")
|
| 317 |
+
logger.info(f" Total Steps: {self.global_step}")
|
| 318 |
+
logger.info(f" Total Tokens: {self.tokens_seen/1e9:.2f}B")
|
| 319 |
+
logger.info(f" Best Loss: {self.best_loss:.4f}")
|
| 320 |
+
logger.info("="*80 + "\n")
|
| 321 |
+
|
| 322 |
+
# 保存最终模型
|
| 323 |
+
self.save_checkpoint(self.checkpoint_dir / "final_model.pt")
|
| 324 |
+
|
| 325 |
+
def save_checkpoint(self, path: Path):
|
| 326 |
+
"""保存checkpoint"""
|
| 327 |
+
checkpoint = {
|
| 328 |
+
'model_state_dict': self.model.state_dict(),
|
| 329 |
+
'optimizer_state_dict': self.optimizer.state_dict(),
|
| 330 |
+
'scaler_state_dict': self.scaler.state_dict() if self.use_amp else None,
|
| 331 |
+
'global_step': self.global_step,
|
| 332 |
+
'current_step': self.current_step,
|
| 333 |
+
'tokens_seen': self.tokens_seen,
|
| 334 |
+
'best_loss': self.best_loss,
|
| 335 |
+
'timestamp': datetime.now().isoformat()
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
torch.save(checkpoint, path)
|
| 339 |
+
logger.info(f" Checkpoint saved to {path}")
|
| 340 |
+
|
| 341 |
+
def load_checkpoint(self, path: str):
|
| 342 |
+
"""加载checkpoint"""
|
| 343 |
+
checkpoint = torch.load(path, map_location=self.device, weights_only=True)
|
| 344 |
+
|
| 345 |
+
self.model.load_state_dict(checkpoint['model_state_dict'])
|
| 346 |
+
self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
|
| 347 |
+
|
| 348 |
+
if self.use_amp and checkpoint.get('scaler_state_dict'):
|
| 349 |
+
self.scaler.load_state_dict(checkpoint['scaler_state_dict'])
|
| 350 |
+
|
| 351 |
+
self.global_step = checkpoint['global_step']
|
| 352 |
+
self.current_step = checkpoint.get('current_step', self.global_step)
|
| 353 |
+
self.tokens_seen = checkpoint['tokens_seen']
|
| 354 |
+
self.best_loss = checkpoint.get('best_loss', float('inf'))
|
| 355 |
+
|
| 356 |
+
logger.info(f" Checkpoint loaded from {path}")
|
| 357 |
+
logger.info(f" Resuming from step {self.global_step}")
|
| 358 |
+
logger.info(f" Tokens seen: {self.tokens_seen/1e9:.2f}B")
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
def main():
|
| 362 |
+
config = {
|
| 363 |
+
# 模型配置
|
| 364 |
+
'model_dim': 1536,
|
| 365 |
+
'vocab_size': 151665,
|
| 366 |
+
'n_layers': 12,
|
| 367 |
+
'n_heads': 12,
|
| 368 |
+
'n_kv_heads': 4,
|
| 369 |
+
'max_seq_len': 512,
|
| 370 |
+
'dropout': 0.1,
|
| 371 |
+
'use_moe': False,
|
| 372 |
+
'batch_size': 4,
|
| 373 |
+
'gradient_accumulation_steps': 8,
|
| 374 |
+
'learning_rate': 3e-4,
|
| 375 |
+
'weight_decay': 0.1,
|
| 376 |
+
'warmup_steps': 500,
|
| 377 |
+
'max_steps': 10000,
|
| 378 |
+
'max_grad_norm': 1.0,
|
| 379 |
+
|
| 380 |
+
# 数据配置
|
| 381 |
+
'data_mix': 'text_only',
|
| 382 |
+
'max_length': 512,
|
| 383 |
+
'num_workers': 2,
|
| 384 |
+
|
| 385 |
+
# 日志和保存
|
| 386 |
+
'log_interval': 10,
|
| 387 |
+
'save_interval': 500,
|
| 388 |
+
'checkpoint_dir': 'checkpoints/pretrain_fixed',
|
| 389 |
+
'loss_log_file': 'checkpoints/pretrain_fixed/train_loss.log'
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
logger.info("="*80)
|
| 393 |
+
logger.info(json.dumps(config, indent=2))
|
| 394 |
+
logger.info("="*80 + "\n")
|
| 395 |
+
|
| 396 |
+
# 初始化tokenizer
|
| 397 |
+
logger.info("Initializing tokenizer...")
|
| 398 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 399 |
+
"Qwen/Qwen2.5-7B-Instruct",
|
| 400 |
+
use_fast=True,
|
| 401 |
+
trust_remote_code=True
|
| 402 |
+
)
|
| 403 |
+
|
| 404 |
+
if tokenizer.pad_token is None:
|
| 405 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 406 |
+
tokenizer.pad_token_id = tokenizer.eos_token_id
|
| 407 |
+
|
| 408 |
+
config['vocab_size'] = len(tokenizer)
|
| 409 |
+
logger.info(f"Vocab size: {config['vocab_size']}\n")
|
| 410 |
+
|
| 411 |
+
# 初始化模型
|
| 412 |
+
logger.info("Initializing model...")
|
| 413 |
+
model = MultiModalDenseTransformer(
|
| 414 |
+
model_dim=config['model_dim'],
|
| 415 |
+
vocab_size=config['vocab_size'],
|
| 416 |
+
n_layers=config['n_layers'],
|
| 417 |
+
n_heads=config['n_heads'],
|
| 418 |
+
n_kv_heads=config['n_kv_heads'],
|
| 419 |
+
max_seq_len=config['max_seq_len'],
|
| 420 |
+
dropout=config['dropout'],
|
| 421 |
+
use_moe=config['use_moe'],
|
| 422 |
+
use_gradient_checkpointing=True,
|
| 423 |
+
rope_scaling_type="yarn",
|
| 424 |
+
use_multimodal_fusion=False,
|
| 425 |
+
use_contrastive=False
|
| 426 |
+
)
|
| 427 |
+
|
| 428 |
+
# 创建数据加载器
|
| 429 |
+
logger.info(f"\nCreating dataloader (mix: {config['data_mix']})...")
|
| 430 |
+
dataloader = create_pretrain_dataloader(
|
| 431 |
+
mix_name=config['data_mix'],
|
| 432 |
+
tokenizer=tokenizer,
|
| 433 |
+
batch_size=config['batch_size'],
|
| 434 |
+
num_workers=config['num_workers'],
|
| 435 |
+
max_length=config['max_length']
|
| 436 |
+
)
|
| 437 |
+
|
| 438 |
+
# 创建训练器
|
| 439 |
+
trainer = PreTrainer(
|
| 440 |
+
model=model,
|
| 441 |
+
tokenizer=tokenizer,
|
| 442 |
+
learning_rate=config['learning_rate'],
|
| 443 |
+
weight_decay=config['weight_decay'],
|
| 444 |
+
warmup_steps=config['warmup_steps'],
|
| 445 |
+
max_steps=config['max_steps'],
|
| 446 |
+
gradient_accumulation_steps=config['gradient_accumulation_steps'],
|
| 447 |
+
max_grad_norm=config['max_grad_norm'],
|
| 448 |
+
log_interval=config['log_interval'],
|
| 449 |
+
save_interval=config['save_interval'],
|
| 450 |
+
checkpoint_dir=config['checkpoint_dir'],
|
| 451 |
+
loss_log_file=config['loss_log_file']
|
| 452 |
+
)
|
| 453 |
+
|
| 454 |
+
logger.info("\n Starting fresh training with fixes...\n")
|
| 455 |
+
trainer.train(dataloader, resume_from="/root/step_6500.pt")
|
| 456 |
+
#trainer.train(dataloader)
|
| 457 |
+
|
| 458 |
+
|
| 459 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
main()
|