Update posttrain.py
Browse files- posttrain.py +534 -553
posttrain.py
CHANGED
|
@@ -1,554 +1,535 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
from
|
| 10 |
-
import
|
| 11 |
-
from
|
| 12 |
-
|
| 13 |
-
from
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
from
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
logging.
|
| 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 |
-
self.
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
self.
|
| 66 |
-
self.
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
self.
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
self.
|
| 73 |
-
self.
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
self.
|
| 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 |
-
self.scaler.
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
def
|
| 164 |
-
"""
|
| 165 |
-
self.
|
| 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 |
-
self
|
| 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 |
-
logger.info(f"Checkpoint
|
| 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 |
-
|
| 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 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
)
|
| 536 |
-
|
| 537 |
-
grpo_trainer.train(
|
| 538 |
-
prompt_loader,
|
| 539 |
-
num_iterations=config['grpo_iterations'],
|
| 540 |
-
max_gen_len=50,
|
| 541 |
-
save_path=config['checkpoint_dir'] + "/grpo"
|
| 542 |
-
)
|
| 543 |
-
|
| 544 |
-
except Exception as e:
|
| 545 |
-
logger.error(f"Error in RLHF: {e}")
|
| 546 |
-
import traceback
|
| 547 |
-
traceback.print_exc()
|
| 548 |
-
|
| 549 |
-
logger.info("\n" + "="*80)
|
| 550 |
-
logger.info("All Training Complete!")
|
| 551 |
-
logger.info("="*80)
|
| 552 |
-
|
| 553 |
-
if __name__ == "__main__":
|
| 554 |
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 |
+
import copy
|
| 11 |
+
from model import MultiModalDenseTransformer
|
| 12 |
+
|
| 13 |
+
from data_loader import (
|
| 14 |
+
create_posttrain_dataloader,
|
| 15 |
+
create_preference_dataloader
|
| 16 |
+
)
|
| 17 |
+
from data_config import POSTTRAIN_MIX
|
| 18 |
+
from reward_model import RewardModel, RewardModelTrainer
|
| 19 |
+
from grpo import GRPOTrainer
|
| 20 |
+
from typing import Optional
|
| 21 |
+
|
| 22 |
+
logging.basicConfig(
|
| 23 |
+
level=logging.INFO,
|
| 24 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 25 |
+
)
|
| 26 |
+
logger = logging.getLogger(__name__)
|
| 27 |
+
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
|
| 28 |
+
|
| 29 |
+
class PostTrainer:
|
| 30 |
+
def __init__(
|
| 31 |
+
self,
|
| 32 |
+
model: MultiModalDenseTransformer,
|
| 33 |
+
tokenizer,
|
| 34 |
+
learning_rate: float = 1e-5,
|
| 35 |
+
weight_decay: float = 0.01,
|
| 36 |
+
num_epochs: int = 3,
|
| 37 |
+
gradient_accumulation_steps: int = 1,
|
| 38 |
+
max_grad_norm: float = 1.0,
|
| 39 |
+
log_interval: int = 10,
|
| 40 |
+
eval_interval: int = 500,
|
| 41 |
+
save_interval: int = 1000,
|
| 42 |
+
checkpoint_dir: str = "checkpoints/posttrain"
|
| 43 |
+
):
|
| 44 |
+
self.model = model
|
| 45 |
+
self.tokenizer = tokenizer
|
| 46 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 47 |
+
|
| 48 |
+
self.model.to(self.device)
|
| 49 |
+
|
| 50 |
+
# 优化器
|
| 51 |
+
self.optimizer = torch.optim.AdamW(
|
| 52 |
+
model.parameters(),
|
| 53 |
+
lr=learning_rate,
|
| 54 |
+
weight_decay=weight_decay,
|
| 55 |
+
betas=(0.9, 0.95),
|
| 56 |
+
eps=1e-8
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# 混合精度
|
| 60 |
+
self.use_amp = torch.cuda.is_available()
|
| 61 |
+
self.scaler = torch.amp.GradScaler('cuda', enabled=self.use_amp)
|
| 62 |
+
|
| 63 |
+
# 训练参数
|
| 64 |
+
self.num_epochs = num_epochs
|
| 65 |
+
self.gradient_accumulation_steps = gradient_accumulation_steps
|
| 66 |
+
self.max_grad_norm = max_grad_norm
|
| 67 |
+
self.log_interval = log_interval
|
| 68 |
+
self.eval_interval = eval_interval
|
| 69 |
+
self.save_interval = save_interval
|
| 70 |
+
|
| 71 |
+
# Checkpoint管理
|
| 72 |
+
self.checkpoint_dir = Path(checkpoint_dir)
|
| 73 |
+
self.checkpoint_dir.mkdir(parents=True, exist_ok=True)
|
| 74 |
+
|
| 75 |
+
# 训练状态
|
| 76 |
+
self.global_step = 0
|
| 77 |
+
self.best_eval_loss = float('inf')
|
| 78 |
+
|
| 79 |
+
logger.info(f"PostTrainer initialized:")
|
| 80 |
+
logger.info(f" Device: {self.device}")
|
| 81 |
+
logger.info(f" Learning Rate: {learning_rate}")
|
| 82 |
+
logger.info(f" Num Epochs: {num_epochs}")
|
| 83 |
+
logger.info(f" Gradient Accumulation: {gradient_accumulation_steps}")
|
| 84 |
+
|
| 85 |
+
def train_step(self, batch: dict) -> dict:
|
| 86 |
+
"""单步训练"""
|
| 87 |
+
instruction_ids = batch['instruction'].to(self.device)
|
| 88 |
+
response_ids = batch['response'].to(self.device)
|
| 89 |
+
|
| 90 |
+
instruction_mask = batch['instruction_mask'].to(self.device)
|
| 91 |
+
response_mask = batch['response_mask'].to(self.device)
|
| 92 |
+
|
| 93 |
+
input_ids = torch.cat([instruction_ids, response_ids], dim=1)
|
| 94 |
+
attention_mask = torch.cat([instruction_mask, response_mask], dim=1)
|
| 95 |
+
|
| 96 |
+
batch_size , seq_len = input_ids.shape
|
| 97 |
+
position_ids=torch.zeros_like(input_ids)
|
| 98 |
+
|
| 99 |
+
for i in range(batch_size):
|
| 100 |
+
non_pad_mask = attention_mask[i].bool()
|
| 101 |
+
if non_pad_mask.any():
|
| 102 |
+
positions=torch.cumsum(non_pad_mask.long(), dim=0) -1
|
| 103 |
+
position_ids[i] = positions * non_pad_mask.long()
|
| 104 |
+
labels = input_ids.clone()
|
| 105 |
+
|
| 106 |
+
# 屏蔽 Instruction 部分
|
| 107 |
+
instr_len = instruction_ids.shape[1]
|
| 108 |
+
labels[:, :instr_len] = -100
|
| 109 |
+
|
| 110 |
+
labels[attention_mask == 0] = -100
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# 准备输入数据
|
| 114 |
+
input_data = {
|
| 115 |
+
'segments': [{
|
| 116 |
+
'type': 'text',
|
| 117 |
+
'data': input_ids,
|
| 118 |
+
'modality_id': 0
|
| 119 |
+
}]
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
# 前向传播
|
| 123 |
+
with torch.amp.autocast('cuda', enabled=self.use_amp):
|
| 124 |
+
outputs = self.model(input_data, attention_mask=attention_mask,
|
| 125 |
+
position_ids = position_ids)
|
| 126 |
+
|
| 127 |
+
logits = outputs['logits']
|
| 128 |
+
|
| 129 |
+
# 计算损失
|
| 130 |
+
shift_logits = logits[:, :-1, :].contiguous()
|
| 131 |
+
shift_labels = labels[:, 1:].contiguous()
|
| 132 |
+
|
| 133 |
+
loss = F.cross_entropy(
|
| 134 |
+
shift_logits.view(-1, shift_logits.size(-1)),
|
| 135 |
+
shift_labels.view(-1),
|
| 136 |
+
ignore_index=-100
|
| 137 |
+
)
|
| 138 |
+
raw_loss = loss.item()
|
| 139 |
+
loss = loss / self.gradient_accumulation_steps
|
| 140 |
+
|
| 141 |
+
# 反向传播
|
| 142 |
+
self.scaler.scale(loss).backward()
|
| 143 |
+
|
| 144 |
+
return {
|
| 145 |
+
'loss': raw_loss
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
def optimizer_step(self):
|
| 149 |
+
"""优化器步骤"""
|
| 150 |
+
self.scaler.unscale_(self.optimizer)
|
| 151 |
+
grad_norm = torch.nn.utils.clip_grad_norm_(
|
| 152 |
+
self.model.parameters(),
|
| 153 |
+
self.max_grad_norm
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
self.scaler.step(self.optimizer)
|
| 157 |
+
self.scaler.update()
|
| 158 |
+
self.optimizer.zero_grad(set_to_none=True)
|
| 159 |
+
self.global_step += 1
|
| 160 |
+
return grad_norm.item()
|
| 161 |
+
|
| 162 |
+
@torch.no_grad()
|
| 163 |
+
def evaluate(self, dataloader, max_batches: int = 50) -> float:
|
| 164 |
+
"""评估"""
|
| 165 |
+
self.model.eval()
|
| 166 |
+
total_loss = 0.0
|
| 167 |
+
num_batches = 0
|
| 168 |
+
|
| 169 |
+
for i, batch in enumerate(dataloader):
|
| 170 |
+
if i >= max_batches:
|
| 171 |
+
break
|
| 172 |
+
|
| 173 |
+
if batch is None:
|
| 174 |
+
continue
|
| 175 |
+
|
| 176 |
+
instruction_ids = batch['instruction'].to(self.device)
|
| 177 |
+
response_ids = batch['response'].to(self.device)
|
| 178 |
+
input_ids = torch.cat([instruction_ids, response_ids], dim=1)
|
| 179 |
+
|
| 180 |
+
labels = input_ids.clone()
|
| 181 |
+
labels[:, :instruction_ids.shape[1]] = -100
|
| 182 |
+
labels[input_ids == self.tokenizer.pad_token_id] = -100
|
| 183 |
+
|
| 184 |
+
input_data = {
|
| 185 |
+
'segments': [{
|
| 186 |
+
'type': 'text',
|
| 187 |
+
'data': input_ids,
|
| 188 |
+
'modality_id': 0
|
| 189 |
+
}]
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
with torch.amp.autocast('cuda', enabled=self.use_amp):
|
| 193 |
+
outputs = self.model(input_data)
|
| 194 |
+
logits = outputs['logits']
|
| 195 |
+
|
| 196 |
+
shift_logits = logits[:, :-1, :].contiguous()
|
| 197 |
+
shift_labels = labels[:, 1:].contiguous()
|
| 198 |
+
|
| 199 |
+
loss = F.cross_entropy(
|
| 200 |
+
shift_logits.view(-1, shift_logits.size(-1)),
|
| 201 |
+
shift_labels.view(-1),
|
| 202 |
+
ignore_index=-100
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
total_loss += loss.item()
|
| 206 |
+
num_batches += 1
|
| 207 |
+
|
| 208 |
+
self.model.train()
|
| 209 |
+
return total_loss / max(num_batches, 1)
|
| 210 |
+
|
| 211 |
+
def train(
|
| 212 |
+
self,
|
| 213 |
+
train_dataloader,
|
| 214 |
+
eval_dataloader=None,
|
| 215 |
+
resume_from: Optional[str] = None
|
| 216 |
+
):
|
| 217 |
+
"""训练循环"""
|
| 218 |
+
logger.info("\n" + "="*80)
|
| 219 |
+
logger.info("Starting Post-Training (SFT)")
|
| 220 |
+
logger.info("="*80 + "\n")
|
| 221 |
+
|
| 222 |
+
if resume_from:
|
| 223 |
+
self.load_checkpoint(resume_from)
|
| 224 |
+
|
| 225 |
+
self.model.train()
|
| 226 |
+
|
| 227 |
+
for epoch in range(self.num_epochs):
|
| 228 |
+
logger.info(f"\nEpoch {epoch+1}/{self.num_epochs}")
|
| 229 |
+
|
| 230 |
+
progress_bar = tqdm(train_dataloader, desc=f"Epoch {epoch+1}")
|
| 231 |
+
running_loss = 0.0
|
| 232 |
+
step_in_accumulation = 0
|
| 233 |
+
|
| 234 |
+
for batch_idx, batch in enumerate(progress_bar):
|
| 235 |
+
if batch is None:
|
| 236 |
+
continue
|
| 237 |
+
|
| 238 |
+
# 训练步骤
|
| 239 |
+
stats = self.train_step(batch)
|
| 240 |
+
running_loss += stats['loss']
|
| 241 |
+
step_in_accumulation += 1
|
| 242 |
+
|
| 243 |
+
# 优化器更新
|
| 244 |
+
if step_in_accumulation == self.gradient_accumulation_steps:
|
| 245 |
+
grad_norm = self.optimizer_step()
|
| 246 |
+
step_in_accumulation = 0
|
| 247 |
+
|
| 248 |
+
# 更新进度条
|
| 249 |
+
progress_bar.set_postfix({'loss': f"{stats['loss']:.4f}"})
|
| 250 |
+
|
| 251 |
+
# 日志
|
| 252 |
+
if self.global_step % self.log_interval == 0:
|
| 253 |
+
avg_loss = running_loss / self.log_interval
|
| 254 |
+
logger.info(
|
| 255 |
+
f"Step {self.global_step} | "
|
| 256 |
+
f"Epoch {epoch+1} | "
|
| 257 |
+
f"Loss: {avg_loss:.4f}"
|
| 258 |
+
)
|
| 259 |
+
running_loss = 0.0
|
| 260 |
+
|
| 261 |
+
# 评估
|
| 262 |
+
if eval_dataloader and self.global_step % self.eval_interval == 0:
|
| 263 |
+
eval_loss = self.evaluate(eval_dataloader)
|
| 264 |
+
logger.info(f"Eval Loss: {eval_loss:.4f}")
|
| 265 |
+
|
| 266 |
+
if eval_loss < self.best_eval_loss:
|
| 267 |
+
self.best_eval_loss = eval_loss
|
| 268 |
+
self.save_checkpoint(
|
| 269 |
+
self.checkpoint_dir / "best_model.pt",
|
| 270 |
+
is_best=True
|
| 271 |
+
)
|
| 272 |
+
|
| 273 |
+
# 保存
|
| 274 |
+
if self.global_step % self.save_interval == 0:
|
| 275 |
+
self.save_checkpoint(
|
| 276 |
+
self.checkpoint_dir / f"step_{self.global_step}.pt"
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
# Epoch结束评估
|
| 280 |
+
if eval_dataloader:
|
| 281 |
+
eval_loss = self.evaluate(eval_dataloader)
|
| 282 |
+
logger.info(f"\nEpoch {epoch+1} Eval Loss: {eval_loss:.4f}")
|
| 283 |
+
|
| 284 |
+
logger.info("\n" + "="*80)
|
| 285 |
+
logger.info("Post-Training Complete!")
|
| 286 |
+
logger.info(f" Best Eval Loss: {self.best_eval_loss:.4f}")
|
| 287 |
+
logger.info("="*80 + "\n")
|
| 288 |
+
|
| 289 |
+
self.save_checkpoint(self.checkpoint_dir / "final_model.pt")
|
| 290 |
+
|
| 291 |
+
def save_checkpoint(self, path: Path, is_best: bool = False):
|
| 292 |
+
"""保存checkpoint"""
|
| 293 |
+
checkpoint = {
|
| 294 |
+
'model_state_dict': self.model.state_dict(),
|
| 295 |
+
'optimizer_state_dict': self.optimizer.state_dict(),
|
| 296 |
+
'scaler_state_dict': self.scaler.state_dict() if self.use_amp else None,
|
| 297 |
+
'global_step': self.global_step,
|
| 298 |
+
'best_eval_loss': self.best_eval_loss,
|
| 299 |
+
'timestamp': datetime.now().isoformat()
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
torch.save(checkpoint, path)
|
| 303 |
+
logger.info(f"Checkpoint saved to {path}" + (" (BEST)" if is_best else ""))
|
| 304 |
+
|
| 305 |
+
def load_checkpoint(self, path: str):
|
| 306 |
+
"""加载checkpoint"""
|
| 307 |
+
checkpoint = torch.load(path, map_location=self.device)
|
| 308 |
+
|
| 309 |
+
self.model.load_state_dict(checkpoint['model_state_dict'])
|
| 310 |
+
self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
|
| 311 |
+
|
| 312 |
+
if self.use_amp and checkpoint.get('scaler_state_dict'):
|
| 313 |
+
self.scaler.load_state_dict(checkpoint['scaler_state_dict'])
|
| 314 |
+
|
| 315 |
+
self.global_step = checkpoint['global_step']
|
| 316 |
+
self.best_eval_loss = checkpoint['best_eval_loss']
|
| 317 |
+
|
| 318 |
+
logger.info(f"Checkpoint loaded from {path}")
|
| 319 |
+
|
| 320 |
+
def main():
|
| 321 |
+
"""主函数"""
|
| 322 |
+
# 配置
|
| 323 |
+
config = {
|
| 324 |
+
# 模型配置
|
| 325 |
+
'model_dim': 1536,
|
| 326 |
+
'vocab_size': 151665,
|
| 327 |
+
'n_layers': 12,
|
| 328 |
+
'n_heads': 12,
|
| 329 |
+
'n_kv_heads': 4,
|
| 330 |
+
'max_seq_len': 512,
|
| 331 |
+
'dropout': 0.0,
|
| 332 |
+
'use_moe': False,
|
| 333 |
+
# 训练配置
|
| 334 |
+
'batch_size': 2,
|
| 335 |
+
'gradient_accumulation_steps': 8,
|
| 336 |
+
'learning_rate': 1e-5,
|
| 337 |
+
'weight_decay': 0.01,
|
| 338 |
+
'num_epochs': 3,
|
| 339 |
+
'max_grad_norm': 1.0,
|
| 340 |
+
|
| 341 |
+
# 数据配置
|
| 342 |
+
'data_mix': 'simple_instruct',
|
| 343 |
+
'max_samples_train': 20000,
|
| 344 |
+
'max_samples_eval': 1000,
|
| 345 |
+
'max_length': 512,
|
| 346 |
+
'num_workers': 4,
|
| 347 |
+
|
| 348 |
+
# RLHF配置
|
| 349 |
+
'do_rlhf': False,
|
| 350 |
+
'preference_dataset': 'hh_rlhf',
|
| 351 |
+
'grpo_iterations': 3,
|
| 352 |
+
'grpo_kl_coef': 0.04,
|
| 353 |
+
'grpo_group_size': 4,
|
| 354 |
+
|
| 355 |
+
# 路径
|
| 356 |
+
'pretrain_checkpoint': '/root/multimodal/checkpoints/pretrain_fixed/step_10000.pt',
|
| 357 |
+
'checkpoint_dir': 'checkpoints/posttrain',
|
| 358 |
+
'log_interval': 50,
|
| 359 |
+
'eval_interval': 500,
|
| 360 |
+
'save_interval': 1000,
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
logger.info("Configuration:")
|
| 364 |
+
logger.info(json.dumps(config, indent=2))
|
| 365 |
+
|
| 366 |
+
# 初始化tokenizer
|
| 367 |
+
logger.info("\nInitializing tokenizer...")
|
| 368 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 369 |
+
"Qwen/Qwen2.5-7B-Instruct",
|
| 370 |
+
use_fast=True,
|
| 371 |
+
trust_remote_code=True
|
| 372 |
+
)
|
| 373 |
+
|
| 374 |
+
if tokenizer.pad_token is None:
|
| 375 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 376 |
+
tokenizer.pad_token_id = tokenizer.eos_token_id
|
| 377 |
+
|
| 378 |
+
config['vocab_size'] = len(tokenizer)
|
| 379 |
+
|
| 380 |
+
# 初始化或加载模型
|
| 381 |
+
logger.info("\nInitializing model...")
|
| 382 |
+
model = MultiModalDenseTransformer(
|
| 383 |
+
model_dim=config['model_dim'],
|
| 384 |
+
vocab_size=config['vocab_size'],
|
| 385 |
+
n_layers=config['n_layers'],
|
| 386 |
+
n_heads=config['n_heads'],
|
| 387 |
+
n_kv_heads=config['n_kv_heads'],
|
| 388 |
+
max_seq_len=config['max_seq_len'],
|
| 389 |
+
dropout=config['dropout'],
|
| 390 |
+
use_moe=config['use_moe'],
|
| 391 |
+
use_gradient_checkpointing=False,
|
| 392 |
+
rope_scaling_type="yarn",
|
| 393 |
+
use_multimodal_fusion=False,
|
| 394 |
+
use_contrastive=False
|
| 395 |
+
)
|
| 396 |
+
|
| 397 |
+
if config['pretrain_checkpoint']:
|
| 398 |
+
logger.info(f"Loading pretrain checkpoint: {config['pretrain_checkpoint']}")
|
| 399 |
+
checkpoint = torch.load(config['pretrain_checkpoint'])
|
| 400 |
+
model.load_state_dict(checkpoint['model_state_dict'])
|
| 401 |
+
|
| 402 |
+
logger.info("\n" + "="*80)
|
| 403 |
+
logger.info("PHASE 1: Supervised Fine-Tuning")
|
| 404 |
+
logger.info("="*80)
|
| 405 |
+
|
| 406 |
+
# 创建数据加载器
|
| 407 |
+
train_dataloader = create_posttrain_dataloader(
|
| 408 |
+
mix_name=config['data_mix'],
|
| 409 |
+
tokenizer=tokenizer,
|
| 410 |
+
batch_size=config['batch_size'],
|
| 411 |
+
num_workers=config['num_workers'],
|
| 412 |
+
max_length=config['max_length'],
|
| 413 |
+
max_samples=config['max_samples_train'],
|
| 414 |
+
split='train',
|
| 415 |
+
shuffle=True
|
| 416 |
+
)
|
| 417 |
+
|
| 418 |
+
eval_dataloader = create_posttrain_dataloader(
|
| 419 |
+
mix_name=config['data_mix'],
|
| 420 |
+
tokenizer=tokenizer,
|
| 421 |
+
batch_size=config['batch_size'] * 2,
|
| 422 |
+
num_workers=config['num_workers'],
|
| 423 |
+
max_length=config['max_length'],
|
| 424 |
+
max_samples=config['max_samples_eval'],
|
| 425 |
+
split='train', # 使用train的后部分作为验证
|
| 426 |
+
shuffle=False
|
| 427 |
+
)
|
| 428 |
+
|
| 429 |
+
# 创建训练器
|
| 430 |
+
trainer = PostTrainer(
|
| 431 |
+
model=model,
|
| 432 |
+
tokenizer=tokenizer,
|
| 433 |
+
learning_rate=config['learning_rate'],
|
| 434 |
+
weight_decay=config['weight_decay'],
|
| 435 |
+
num_epochs=config['num_epochs'],
|
| 436 |
+
gradient_accumulation_steps=config['gradient_accumulation_steps'],
|
| 437 |
+
max_grad_norm=config['max_grad_norm'],
|
| 438 |
+
log_interval=config['log_interval'],
|
| 439 |
+
eval_interval=config['eval_interval'],
|
| 440 |
+
save_interval=config['save_interval'],
|
| 441 |
+
checkpoint_dir=config['checkpoint_dir']
|
| 442 |
+
)
|
| 443 |
+
|
| 444 |
+
trainer.train(train_dataloader, eval_dataloader)
|
| 445 |
+
|
| 446 |
+
if config['do_rlhf']:
|
| 447 |
+
logger.info("\n" + "="*80)
|
| 448 |
+
logger.info("PHASE 2: RLHF with GRPO")
|
| 449 |
+
logger.info("="*80)
|
| 450 |
+
|
| 451 |
+
try:
|
| 452 |
+
# 训练奖励模型
|
| 453 |
+
logger.info("\nTraining Reward Model...")
|
| 454 |
+
|
| 455 |
+
reward_base_model = copy.deepcopy(model)
|
| 456 |
+
reward_model = RewardModel(reward_base_model, use_value_head=True)
|
| 457 |
+
|
| 458 |
+
preference_dataloader = create_preference_dataloader(
|
| 459 |
+
dataset_name=config['preference_dataset'],
|
| 460 |
+
tokenizer=tokenizer,
|
| 461 |
+
batch_size=config['batch_size'],
|
| 462 |
+
num_workers=config['num_workers'],
|
| 463 |
+
max_samples=5000,
|
| 464 |
+
split='train'
|
| 465 |
+
)
|
| 466 |
+
|
| 467 |
+
reward_trainer = RewardModelTrainer(
|
| 468 |
+
reward_model=reward_model,
|
| 469 |
+
learning_rate=1e-5
|
| 470 |
+
)
|
| 471 |
+
|
| 472 |
+
reward_trainer.train(preference_dataloader, num_epochs=1)
|
| 473 |
+
|
| 474 |
+
# GRPO训练
|
| 475 |
+
logger.info("\nStarting GRPO Training...")
|
| 476 |
+
|
| 477 |
+
ref_model = copy.deepcopy(model)
|
| 478 |
+
ref_model.eval()
|
| 479 |
+
|
| 480 |
+
grpo_trainer = GRPOTrainer(
|
| 481 |
+
actor_model=model,
|
| 482 |
+
reward_model=reward_model,
|
| 483 |
+
ref_model=ref_model,
|
| 484 |
+
tokenizer=tokenizer,
|
| 485 |
+
learning_rate=1e-6,
|
| 486 |
+
kl_coef=config['grpo_kl_coef'],
|
| 487 |
+
group_size=config['grpo_group_size'],
|
| 488 |
+
update_batch_size=2,
|
| 489 |
+
use_amp=True
|
| 490 |
+
)
|
| 491 |
+
|
| 492 |
+
# 准备prompts
|
| 493 |
+
prompt_dataloader = create_posttrain_dataloader(
|
| 494 |
+
mix_name=config['data_mix'],
|
| 495 |
+
tokenizer=tokenizer,
|
| 496 |
+
batch_size=4,
|
| 497 |
+
num_workers=2,
|
| 498 |
+
max_samples=1000,
|
| 499 |
+
split='train'
|
| 500 |
+
)
|
| 501 |
+
|
| 502 |
+
# 提取prompts
|
| 503 |
+
prompts = []
|
| 504 |
+
for batch in prompt_dataloader:
|
| 505 |
+
if batch and batch.get('instruction') is not None:
|
| 506 |
+
prompts.append(batch['instruction'])
|
| 507 |
+
if len(prompts) >= 200:
|
| 508 |
+
break
|
| 509 |
+
|
| 510 |
+
if prompts:
|
| 511 |
+
prompt_tensor = torch.cat(prompts[:200], dim=0)
|
| 512 |
+
from torch.utils.data import TensorDataset, DataLoader
|
| 513 |
+
prompt_loader = DataLoader(
|
| 514 |
+
TensorDataset(prompt_tensor),
|
| 515 |
+
batch_size=4
|
| 516 |
+
)
|
| 517 |
+
|
| 518 |
+
grpo_trainer.train(
|
| 519 |
+
prompt_loader,
|
| 520 |
+
num_iterations=config['grpo_iterations'],
|
| 521 |
+
max_gen_len=50,
|
| 522 |
+
save_path=config['checkpoint_dir'] + "/grpo"
|
| 523 |
+
)
|
| 524 |
+
|
| 525 |
+
except Exception as e:
|
| 526 |
+
logger.error(f"Error in RLHF: {e}")
|
| 527 |
+
import traceback
|
| 528 |
+
traceback.print_exc()
|
| 529 |
+
|
| 530 |
+
logger.info("\n" + "="*80)
|
| 531 |
+
logger.info("All Training Complete!")
|
| 532 |
+
logger.info("="*80)
|
| 533 |
+
|
| 534 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
main()
|