forked from eric-mitchell/direct-preference-optimization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainers.py
545 lines (435 loc) · 28.5 KB
/
trainers.py
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
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
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
538
539
540
541
542
543
544
545
import torch
torch.backends.cuda.matmul.allow_tf32 = True
import torch.nn.functional as F
import torch.nn as nn
import transformers
from omegaconf import DictConfig
import torch.distributed as dist
from torch.distributed.fsdp import (
FullyShardedDataParallel as FSDP,
MixedPrecision,
StateDictType,
BackwardPrefetch,
ShardingStrategy,
CPUOffload,
)
from torch.distributed.fsdp.api import FullStateDictConfig, FullOptimStateDictConfig
from torch.distributed.fsdp.wrap import transformer_auto_wrap_policy
import tensor_parallel as tp
import contextlib
from preference_datasets import get_batch_iterator
from utils import (
slice_and_move_batch_for_device,
formatted_dict,
all_gather_if_needed,
pad_to_length,
get_block_class_from_model,
rank0_print,
get_local_dir,
)
import numpy as np
import wandb
import tqdm
import random
import os
from collections import defaultdict
import time
import json
import functools
from typing import Optional, Dict, List, Union, Tuple
def preference_loss(policy_chosen_logps: torch.FloatTensor,
policy_rejected_logps: torch.FloatTensor,
reference_chosen_logps: torch.FloatTensor,
reference_rejected_logps: torch.FloatTensor,
beta: float,
label_smoothing: float = 0.0,
ipo: bool = False,
reference_free: bool = False) -> Tuple[torch.FloatTensor, torch.FloatTensor, torch.FloatTensor]:
"""Compute the DPO loss for a batch of policy and reference model log probabilities.
Args:
policy_chosen_logps: Log probabilities of the policy model for the chosen responses. Shape: (batch_size,)
policy_rejected_logps: Log probabilities of the policy model for the rejected responses. Shape: (batch_size,)
reference_chosen_logps: Log probabilities of the reference model for the chosen responses. Shape: (batch_size,)
reference_rejected_logps: Log probabilities of the reference model for the rejected responses. Shape: (batch_size,)
beta: Temperature parameter for the DPO loss, typically something in the range of 0.1 to 0.5. We ignore the reference model as beta -> 0.
label_smoothing: conservativeness for DPO loss, which assumes that preferences are noisy (flipped with probability label_smoothing)
ipo: If True, use the IPO loss instead of the DPO loss.
reference_free: If True, we ignore the _provided_ reference model and implicitly use a reference model that assigns equal probability to all responses.
Returns:
A tuple of three tensors: (losses, chosen_rewards, rejected_rewards).
The losses tensor contains the DPO loss for each example in the batch.
The chosen_rewards and rejected_rewards tensors contain the rewards for the chosen and rejected responses, respectively.
"""
pi_logratios = policy_chosen_logps - policy_rejected_logps
ref_logratios = reference_chosen_logps - reference_rejected_logps
if reference_free:
ref_logratios = 0
logits = pi_logratios - ref_logratios # also known as h_{\pi_\theta}^{y_w,y_l}
if ipo:
losses = (logits - 1/(2 * beta)) ** 2 # Eq. 17 of https://arxiv.org/pdf/2310.12036v2.pdf
else:
# Eq. 3 https://ericmitchell.ai/cdpo.pdf; label_smoothing=0 gives original DPO (Eq. 7 of https://arxiv.org/pdf/2305.18290.pdf)
losses = -F.logsigmoid(beta * logits) * (1 - label_smoothing) - F.logsigmoid(-beta * logits) * label_smoothing
chosen_rewards = beta * (policy_chosen_logps - reference_chosen_logps).detach()
rejected_rewards = beta * (policy_rejected_logps - reference_rejected_logps).detach()
return losses, chosen_rewards, rejected_rewards
def _get_batch_logps(logits: torch.FloatTensor, labels: torch.LongTensor, average_log_prob: bool = False) -> torch.FloatTensor:
"""Compute the log probabilities of the given labels under the given logits.
Args:
logits: Logits of the model (unnormalized). Shape: (batch_size, sequence_length, vocab_size)
labels: Labels for which to compute the log probabilities. Label tokens with a value of -100 are ignored. Shape: (batch_size, sequence_length)
average_log_prob: If True, return the average log probability per (non-masked) token. Otherwise, return the sum of the log probabilities of the (non-masked) tokens.
Returns:
A tensor of shape (batch_size,) containing the average/sum log probabilities of the given labels under the given logits.
"""
assert logits.shape[:-1] == labels.shape
labels = labels[:, 1:].clone()
logits = logits[:, :-1, :]
loss_mask = (labels != -100)
# dummy token; we'll ignore the losses on these tokens later
labels[labels == -100] = 0
per_token_logps = torch.gather(logits.log_softmax(-1), dim=2, index=labels.unsqueeze(2)).squeeze(2)
if average_log_prob:
return (per_token_logps * loss_mask).sum(-1) / loss_mask.sum(-1)
else:
return (per_token_logps * loss_mask).sum(-1)
def concatenated_inputs(batch: Dict[str, Union[List, torch.LongTensor]]) -> Dict[str, torch.LongTensor]:
"""Concatenate the chosen and rejected inputs into a single tensor.
Args:
batch: A batch of data. Must contain the keys 'chosen_input_ids' and 'rejected_input_ids', which are tensors of shape (batch_size, sequence_length).
Returns:
A dictionary containing the concatenated inputs under the key 'concatenated_input_ids'.
"""
max_length = max(batch['chosen_input_ids'].shape[1], batch['rejected_input_ids'].shape[1])
concatenated_batch = {}
for k in batch:
if k.startswith('chosen') and isinstance(batch[k], torch.Tensor):
pad_value = -100 if 'labels' in k else 0
concatenated_key = k.replace('chosen', 'concatenated')
concatenated_batch[concatenated_key] = pad_to_length(batch[k], max_length, pad_value=pad_value)
for k in batch:
if k.startswith('rejected') and isinstance(batch[k], torch.Tensor):
pad_value = -100 if 'labels' in k else 0
concatenated_key = k.replace('rejected', 'concatenated')
concatenated_batch[concatenated_key] = torch.cat((
concatenated_batch[concatenated_key],
pad_to_length(batch[k], max_length, pad_value=pad_value),
), dim=0)
return concatenated_batch
class BasicTrainer(object):
def __init__(self, policy: nn.Module, config: DictConfig, seed: int, run_dir: str, reference_model: Optional[nn.Module] = None, rank: int = 0, world_size: int = 1):
"""A trainer for a language model, supporting either SFT or DPO training.
If multiple GPUs are present, naively splits the model across them, effectively
offering N times available memory, but without any parallel computation.
"""
self.seed = seed
self.rank = rank
self.world_size = world_size
self.config = config
self.run_dir = run_dir
tokenizer_name_or_path = config.model.tokenizer_name_or_path or config.model.name_or_path
rank0_print(f'Loading tokenizer {tokenizer_name_or_path}')
self.tokenizer = transformers.AutoTokenizer.from_pretrained(tokenizer_name_or_path, cache_dir=get_local_dir(config.local_dirs))
if self.tokenizer.pad_token_id is None:
self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
data_iterator_kwargs = dict(
names=config.datasets,
tokenizer=self.tokenizer,
shuffle=True,
max_length=config.max_length,
max_prompt_length=config.max_prompt_length,
sft_mode=config.loss.name == 'sft',
)
self.policy = policy
self.reference_model = reference_model
self.train_iterator = get_batch_iterator(**data_iterator_kwargs, split='train', n_epochs=config.n_epochs, n_examples=config.n_examples, batch_size=config.batch_size, silent=rank != 0, cache_dir=get_local_dir(config.local_dirs))
rank0_print(f'Loaded train data iterator')
self.eval_iterator = get_batch_iterator(**data_iterator_kwargs, split='test', n_examples=config.n_eval_examples, batch_size=config.eval_batch_size, silent=rank != 0, cache_dir=get_local_dir(config.local_dirs))
self.eval_batches = list(self.eval_iterator)
rank0_print(f'Loaded {len(self.eval_batches)} eval batches of size {config.eval_batch_size}')
def get_batch_samples(self, batch: Dict[str, torch.LongTensor]) -> Tuple[str, str]:
"""Generate samples from the policy (and reference model, if doing DPO training) for the given batch of inputs."""
# FSDP generation according to https://github.com/pytorch/pytorch/issues/100069
ctx = lambda: (FSDP.summon_full_params(self.policy, writeback=False, recurse=False) if 'FSDP' in self.config.trainer else contextlib.nullcontext())
with ctx():
policy_output = self.policy.generate(
batch['prompt_input_ids'], attention_mask=batch['prompt_attention_mask'], max_length=self.config.max_length, do_sample=True, pad_token_id=self.tokenizer.pad_token_id)
if self.config.loss.name in {'dpo', 'ipo'}:
ctx = lambda: (FSDP.summon_full_params(self.reference_model, writeback=False, recurse=False) if 'FSDP' in self.config.trainer else contextlib.nullcontext())
with ctx():
reference_output = self.reference_model.generate(
batch['prompt_input_ids'], attention_mask=batch['prompt_attention_mask'], max_length=self.config.max_length, do_sample=True, pad_token_id=self.tokenizer.pad_token_id)
policy_output = pad_to_length(policy_output, self.config.max_length, self.tokenizer.pad_token_id)
policy_output = all_gather_if_needed(policy_output, self.rank, self.world_size)
policy_output_decoded = self.tokenizer.batch_decode(policy_output, skip_special_tokens=True)
if self.config.loss.name in {'dpo', 'ipo'}:
reference_output = pad_to_length(reference_output, self.config.max_length, self.tokenizer.pad_token_id)
reference_output = all_gather_if_needed(reference_output, self.rank, self.world_size)
reference_output_decoded = self.tokenizer.batch_decode(reference_output, skip_special_tokens=True)
else:
reference_output_decoded = []
return policy_output_decoded, reference_output_decoded
def concatenated_forward(self, model: nn.Module, batch: Dict[str, Union[List, torch.LongTensor]]) -> Tuple[torch.FloatTensor, torch.FloatTensor]:
"""Run the given model on the given batch of inputs, concatenating the chosen and rejected inputs together.
We do this to avoid doing two forward passes, because it's faster for FSDP.
"""
concatenated_batch = concatenated_inputs(batch)
all_logits = model(concatenated_batch['concatenated_input_ids'], attention_mask=concatenated_batch['concatenated_attention_mask']).logits.to(torch.float32)
all_logps = _get_batch_logps(all_logits, concatenated_batch['concatenated_labels'], average_log_prob=False)
chosen_logps = all_logps[:batch['chosen_input_ids'].shape[0]]
rejected_logps = all_logps[batch['chosen_input_ids'].shape[0]:]
return chosen_logps, rejected_logps
def get_batch_metrics(self, batch: Dict[str, Union[List, torch.LongTensor]], loss_config: DictConfig, train=True):
"""Compute the SFT or DPO loss and other metrics for the given batch of inputs."""
metrics = {}
train_test = 'train' if train else 'eval'
if loss_config.name in {'dpo', 'ipo'}:
policy_chosen_logps, policy_rejected_logps = self.concatenated_forward(self.policy, batch)
with torch.no_grad():
reference_chosen_logps, reference_rejected_logps = self.concatenated_forward(self.reference_model, batch)
if loss_config.name == 'dpo':
loss_kwargs = {'beta': loss_config.beta, 'reference_free': loss_config.reference_free, 'label_smoothing': loss_config.label_smoothing, 'ipo': False}
elif loss_config.name == 'ipo':
loss_kwargs = {'beta': loss_config.beta, 'ipo': True}
else:
raise ValueError(f'unknown loss {loss_config.name}')
losses, chosen_rewards, rejected_rewards = preference_loss(
policy_chosen_logps, policy_rejected_logps, reference_chosen_logps, reference_rejected_logps, **loss_kwargs)
reward_accuracies = (chosen_rewards > rejected_rewards).float()
chosen_rewards = all_gather_if_needed(chosen_rewards, self.rank, self.world_size)
rejected_rewards = all_gather_if_needed(rejected_rewards, self.rank, self.world_size)
reward_accuracies = all_gather_if_needed(reward_accuracies, self.rank, self.world_size)
metrics[f'rewards_{train_test}/chosen'] = chosen_rewards.cpu().numpy().tolist()
metrics[f'rewards_{train_test}/rejected'] = rejected_rewards.cpu().numpy().tolist()
metrics[f'rewards_{train_test}/accuracies'] = reward_accuracies.cpu().numpy().tolist()
metrics[f'rewards_{train_test}/margins'] = (chosen_rewards - rejected_rewards).cpu().numpy().tolist()
policy_rejected_logps = all_gather_if_needed(policy_rejected_logps.detach(), self.rank, self.world_size)
metrics[f'logps_{train_test}/rejected'] = policy_rejected_logps.cpu().numpy().tolist()
elif loss_config.name == 'sft':
policy_chosen_logits = self.policy(batch['chosen_input_ids'], attention_mask=batch['chosen_attention_mask']).logits.to(torch.float32)
policy_chosen_logps = _get_batch_logps(policy_chosen_logits, batch['chosen_labels'], average_log_prob=False)
losses = -policy_chosen_logps
policy_chosen_logps = all_gather_if_needed(policy_chosen_logps.detach(), self.rank, self.world_size)
metrics[f'logps_{train_test}/chosen'] = policy_chosen_logps.cpu().numpy().tolist()
all_devices_losses = all_gather_if_needed(losses.detach(), self.rank, self.world_size)
metrics[f'loss/{train_test}'] = all_devices_losses.cpu().numpy().tolist()
return losses.mean(), metrics
def train(self):
"""Begin either SFT or DPO training, with periodic evaluation."""
rank0_print(f'Using {self.config.optimizer} optimizer')
self.optimizer = getattr(torch.optim, self.config.optimizer)(self.policy.parameters(), lr=self.config.lr)
self.scheduler = torch.optim.lr_scheduler.LambdaLR(self.optimizer, lr_lambda=lambda step: min(1.0, (step + 1) / (self.config.warmup_steps + 1)))
torch.manual_seed(self.seed)
np.random.seed(self.seed)
random.seed(self.seed)
if self.config.loss.name in {'dpo', 'ipo'}:
self.reference_model.eval()
self.example_counter = 0
self.batch_counter = 0
last_log = None
for batch in self.train_iterator:
#### BEGIN EVALUATION ####
if self.example_counter % self.config.eval_every == 0 and (self.example_counter > 0 or self.config.do_first_eval):
rank0_print(f'Running evaluation after {self.example_counter} train examples')
self.policy.eval()
all_eval_metrics = defaultdict(list)
if self.config.sample_during_eval:
all_policy_samples, all_reference_samples = [], []
policy_text_table = wandb.Table(columns=["step", "prompt", "sample"])
if self.config.loss.name in {'dpo', 'ipo'}:
reference_text_table = wandb.Table(columns=["step", "prompt", "sample"])
for eval_batch in (tqdm.tqdm(self.eval_batches, desc='Computing eval metrics') if self.rank == 0 else self.eval_batches):
local_eval_batch = slice_and_move_batch_for_device(eval_batch, self.rank, self.world_size, self.rank)
with torch.no_grad():
_, eval_metrics = self.get_batch_metrics(local_eval_batch, self.config.loss, train=False)
for k, v in eval_metrics.items():
all_eval_metrics[k].extend(v)
if self.config.sample_during_eval:
if self.config.n_eval_model_samples < self.config.eval_batch_size:
rank0_print(f'Warning: n_eval_model_samples ({self.config.n_eval_model_samples}) < eval_batch_size ({self.config.eval_batch_size}). Sampling from the first complete eval batch of prompts.')
sample_batches = self.eval_batches[:1]
else:
n_sample_batches = self.config.n_eval_model_samples // self.config.eval_batch_size
sample_batches = self.eval_batches[:n_sample_batches]
for eval_batch in (tqdm.tqdm(sample_batches, desc='Generating samples...') if self.rank == 0 else sample_batches):
local_eval_batch = slice_and_move_batch_for_device(eval_batch, self.rank, self.world_size, self.rank)
policy_samples, reference_samples = self.get_batch_samples(local_eval_batch)
all_policy_samples.extend(policy_samples)
all_reference_samples.extend(reference_samples)
for prompt, sample in zip(eval_batch['prompt'], policy_samples):
policy_text_table.add_data(self.example_counter, prompt, sample)
if self.config.loss.name in {'dpo', 'ipo'}:
for prompt, sample in zip(eval_batch['prompt'], reference_samples):
reference_text_table.add_data(self.example_counter, prompt, sample)
mean_eval_metrics = {k: sum(v) / len(v) for k, v in all_eval_metrics.items()}
rank0_print(f'eval after {self.example_counter}: {formatted_dict(mean_eval_metrics)}')
if self.config.sample_during_eval:
rank0_print(json.dumps(all_policy_samples[:10], indent=2))
if self.config.loss.name in {'dpo', 'ipo'}:
rank0_print(json.dumps(all_reference_samples[:10], indent=2))
if self.config.wandb.enabled and self.rank == 0:
wandb.log(mean_eval_metrics, step=self.example_counter)
if self.config.sample_during_eval:
wandb.log({"policy_samples": policy_text_table}, step=self.example_counter)
if self.config.loss.name in {'dpo', 'ipo'}:
wandb.log({"reference_samples": reference_text_table}, step=self.example_counter)
if self.example_counter > 0:
if self.config.debug:
rank0_print('skipping save in debug mode')
else:
output_dir = os.path.join(self.run_dir, f'step-{self.example_counter}')
rank0_print(f'creating checkpoint to write to {output_dir}...')
self.save(output_dir, mean_eval_metrics)
#### END EVALUATION ####
#### BEGIN TRAINING ####
self.policy.train()
start_time = time.time()
batch_metrics = defaultdict(list)
for microbatch_idx in range(self.config.gradient_accumulation_steps):
global_microbatch = slice_and_move_batch_for_device(batch, microbatch_idx, self.config.gradient_accumulation_steps, self.rank)
local_microbatch = slice_and_move_batch_for_device(global_microbatch, self.rank, self.world_size, self.rank)
loss, metrics = self.get_batch_metrics(local_microbatch, self.config.loss, train=True)
(loss / self.config.gradient_accumulation_steps).backward()
for k, v in metrics.items():
batch_metrics[k].extend(v)
grad_norm = self.clip_gradient()
self.optimizer.step()
self.scheduler.step()
self.optimizer.zero_grad()
step_time = time.time() - start_time
examples_per_second = self.config.batch_size / step_time
batch_metrics['examples_per_second'].append(examples_per_second)
batch_metrics['grad_norm'].append(grad_norm)
self.batch_counter += 1
self.example_counter += self.config.batch_size
if last_log is None or time.time() - last_log > self.config.minimum_log_interval_secs:
mean_train_metrics = {k: sum(v) / len(v) for k, v in batch_metrics.items()}
mean_train_metrics['counters/examples'] = self.example_counter
mean_train_metrics['counters/updates'] = self.batch_counter
rank0_print(f'train stats after {self.example_counter} examples: {formatted_dict(mean_train_metrics)}')
if self.config.wandb.enabled and self.rank == 0:
wandb.log(mean_train_metrics, step=self.example_counter)
last_log = time.time()
else:
rank0_print(f'skipping logging after {self.example_counter} examples to avoid logging too frequently')
#### END TRAINING ####
def clip_gradient(self):
"""Clip the gradient norm of the parameters of a non-FSDP policy."""
return torch.nn.utils.clip_grad_norm_(self.policy.parameters(), self.config.max_grad_norm).item()
def write_state_dict(self, step: int, state: Dict[str, torch.Tensor], metrics: Dict, filename: str, dir_name: Optional[str] = None):
"""Write a checkpoint to disk."""
if dir_name is None:
dir_name = os.path.join(self.run_dir, f'LATEST')
os.makedirs(dir_name, exist_ok=True)
output_path = os.path.join(dir_name, filename)
rank0_print(f'writing checkpoint to {output_path}...')
torch.save({
'step_idx': step,
'state': state,
'metrics': metrics if metrics is not None else {},
}, output_path)
def save(self, output_dir: Optional[str] = None, metrics: Optional[Dict] = None):
"""Save policy, optimizer, and scheduler state to disk."""
policy_state_dict = self.policy.state_dict()
self.write_state_dict(self.example_counter, policy_state_dict, metrics, 'policy.pt', output_dir)
del policy_state_dict
optimizer_state_dict = self.optimizer.state_dict()
self.write_state_dict(self.example_counter, optimizer_state_dict, metrics, 'optimizer.pt', output_dir)
del optimizer_state_dict
scheduler_state_dict = self.scheduler.state_dict()
self.write_state_dict(self.example_counter, scheduler_state_dict, metrics, 'scheduler.pt', output_dir)
class FSDPTrainer(BasicTrainer):
def __init__(self, policy: nn.Module, config: DictConfig, seed: int, run_dir: str, reference_model: Optional[nn.Module] = None, rank: int = 0, world_size: int = 1):
"""A trainer subclass that uses PyTorch FSDP to shard the model across multiple GPUs.
This trainer will shard both the policy and reference model across all available GPUs.
Models are sharded at the block level, where the block class name is provided in the config.
"""
super().__init__(policy, config, seed, run_dir, reference_model, rank, world_size)
assert config.model.block_name is not None, 'must specify model.block_name (e.g., GPT2Block or GPTNeoXLayer) for FSDP'
wrap_class = get_block_class_from_model(policy, config.model.block_name)
model_auto_wrap_policy = functools.partial(transformer_auto_wrap_policy, transformer_layer_cls={wrap_class},)
shared_fsdp_kwargs = dict(
auto_wrap_policy=model_auto_wrap_policy,
sharding_strategy=ShardingStrategy.FULL_SHARD,
cpu_offload=CPUOffload(offload_params=False),
backward_prefetch=BackwardPrefetch.BACKWARD_PRE,
device_id=rank,
ignored_modules=None,
limit_all_gathers=False,
use_orig_params=False,
sync_module_states=False
)
rank0_print('Sharding policy...')
mp_dtype = getattr(torch, config.model.fsdp_policy_mp) if config.model.fsdp_policy_mp is not None else None
policy_mp_policy = MixedPrecision(param_dtype=mp_dtype, reduce_dtype=mp_dtype, buffer_dtype=mp_dtype)
self.policy = FSDP(policy, **shared_fsdp_kwargs, mixed_precision=policy_mp_policy)
if config.activation_checkpointing:
rank0_print('Attempting to enable activation checkpointing...')
try:
# use activation checkpointing, according to:
# https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/
#
# first, verify we have FSDP activation support ready by importing:
from torch.distributed.algorithms._checkpoint.checkpoint_wrapper import (
checkpoint_wrapper,
apply_activation_checkpointing,
CheckpointImpl,
)
non_reentrant_wrapper = functools.partial(
checkpoint_wrapper,
offload_to_cpu=False,
checkpoint_impl=CheckpointImpl.NO_REENTRANT,
)
except Exception as e:
rank0_print('FSDP activation checkpointing not available:', e)
else:
check_fn = lambda submodule: isinstance(submodule, wrap_class)
rank0_print('Applying activation checkpointing wrapper to policy...')
apply_activation_checkpointing(self.policy, checkpoint_wrapper_fn=non_reentrant_wrapper, check_fn=check_fn)
rank0_print('FSDP activation checkpointing enabled!')
if config.loss.name in {'dpo', 'ipo'}:
rank0_print('Sharding reference model...')
self.reference_model = FSDP(reference_model, **shared_fsdp_kwargs)
print('Loaded model on rank', rank)
dist.barrier()
def clip_gradient(self):
"""Clip the gradient norm of the parameters of an FSDP policy, gathering the gradients across all GPUs."""
return self.policy.clip_grad_norm_(self.config.max_grad_norm).item()
def save(self, output_dir=None, metrics=None):
"""Save policy, optimizer, and scheduler state to disk, gathering from all processes and saving only on the rank 0 process."""
save_policy = FullStateDictConfig(offload_to_cpu=True, rank0_only=True)
with FSDP.state_dict_type(self.policy, StateDictType.FULL_STATE_DICT, state_dict_config=save_policy):
policy_state_dict = self.policy.state_dict()
if self.rank == 0:
self.write_state_dict(self.example_counter, policy_state_dict, metrics, 'policy.pt', output_dir)
del policy_state_dict
dist.barrier()
save_policy = FullOptimStateDictConfig(offload_to_cpu=True, rank0_only=True)
with FSDP.state_dict_type(self.policy, StateDictType.FULL_STATE_DICT, optim_state_dict_config=save_policy):
optimizer_state_dict = FSDP.optim_state_dict(self.policy, self.optimizer)
if self.rank == 0:
self.write_state_dict(self.example_counter, optimizer_state_dict, metrics, 'optimizer.pt', output_dir)
del optimizer_state_dict
dist.barrier()
if self.rank == 0:
scheduler_state_dict = self.scheduler.state_dict()
self.write_state_dict(self.example_counter, scheduler_state_dict, metrics, 'scheduler.pt', output_dir)
dist.barrier()
class TensorParallelTrainer(BasicTrainer):
def __init__(self, policy, config, seed, run_dir, reference_model=None, rank=0, world_size=1):
"""A trainer subclass that uses TensorParallel to shard the model across multiple GPUs.
Based on https://github.com/BlackSamorez/tensor_parallel. Note sampling is extremely slow,
see https://github.com/BlackSamorez/tensor_parallel/issues/66.
"""
super().__init__(policy, config, seed, run_dir, reference_model, rank, world_size)
rank0_print('Sharding policy...')
self.policy = tp.tensor_parallel(policy, sharded=True)
if config.loss.name in {'dpo', 'ipo'}:
rank0_print('Sharding reference model...')
self.reference_model = tp.tensor_parallel(reference_model, sharded=False)
def save(self, output_dir=None, metrics=None):
"""Save (unsharded) policy state to disk."""
with tp.save_tensor_parallel(self.policy):
policy_state_dict = self.policy.state_dict()
self.write_state_dict(self.example_counter, policy_state_dict, metrics, 'policy.pt', output_dir)
del policy_state_dict