File size: 26,157 Bytes
02626a9 | 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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 | import torch
from torch.utils.data import Sampler
from accelerate import Accelerator
import math
import os
import random
from dataclasses import dataclass
from psi.utils import initialize_overwatch
overwatch = initialize_overwatch(__name__)
def _normalize_positive_weights(mixture_ratios):
weights = torch.tensor(mixture_ratios, dtype=torch.double)
if weights.numel() == 0:
raise ValueError("Sampler received empty mixture ratios")
if torch.any(weights < 0):
raise ValueError(f"Sampler received negative mixture ratios: {mixture_ratios}")
weight_sum = weights.sum()
if weight_sum.item() <= 0:
raise ValueError("Sampler received no positive mixture ratios")
weights /= weight_sum
return weights
class BatchMixtureSampler(Sampler):
"""
Multi-node safe dataset mixture sampler.
Example: datasets = [ds1, ds2], ratio = [4, 1]
"""
def __init__(self, dataset_lens, mixture_ratios, num_samples_per_epoch, batch_size, seed=42):
self.dataset_lens = dataset_lens
self.weights = _normalize_positive_weights(mixture_ratios)
self.num_samples = num_samples_per_epoch
self.batch_size = batch_size
self.seed = seed
# Distributed settings
self.rank = int(os.environ.get("RANK", 0))
self.world_size = int(os.environ.get("WORLD_SIZE", 1))
# each rank gets this many samples
self.num_samples_rank = math.ceil(self.num_samples / self.world_size)
self.epoch = 0
self.batch_counts = self._build_batch_counts()
def _build_batch_counts(self):
positive_ids = [i for i, weight in enumerate(self.weights.tolist()) if weight > 0]
if not positive_ids:
raise ValueError("BatchMixtureSampler received no positive mixture ratios")
counts = torch.zeros(len(self.dataset_lens), dtype=torch.long)
if self.batch_size >= len(positive_ids):
counts[positive_ids] = 1
remaining = self.batch_size - len(positive_ids)
if remaining > 0:
positive_weights = self.weights[positive_ids]
positive_weights = positive_weights / positive_weights.sum()
raw_extra = positive_weights * remaining
extra_counts = torch.floor(raw_extra).long()
counts[positive_ids] += extra_counts
extra_left = remaining - int(extra_counts.sum().item())
if extra_left > 0:
frac = raw_extra - extra_counts.double()
for frac_index in torch.argsort(frac, descending=True)[:extra_left].tolist():
counts[positive_ids[frac_index]] += 1
else:
for dataset_id in torch.argsort(self.weights, descending=True)[:self.batch_size].tolist():
counts[dataset_id] += 1
return counts.tolist()
def set_epoch(self, epoch):
self.epoch = epoch
def __iter__(self):
num_batches_rank = len(self)
total_batches = num_batches_rank * self.world_size
for batch_idx in range(total_batches):
if batch_idx % self.world_size != self.rank:
continue
g = torch.Generator()
g.manual_seed(self.seed + self.epoch * total_batches + batch_idx)
batch = []
for dataset_id, count in enumerate(self.batch_counts):
if count <= 0:
continue
dataset_len = int(self.dataset_lens[dataset_id])
rand_indices = torch.randint(
low=0,
high=dataset_len,
size=(count,),
generator=g,
).tolist()
batch.extend((dataset_id, sample_id) for sample_id in rand_indices)
shuffle_order = torch.randperm(len(batch), generator=g).tolist()
yield [batch[i] for i in shuffle_order]
def __len__(self):
return math.ceil(self.num_samples_rank / self.batch_size)
class TaskMixtureSampler(Sampler):
"""
Multi-node safe dataset mixture sampler that assigns exactly one dataset to
each global batch, then samples a full homogeneous batch from that dataset.
"""
def __init__(self, dataset_lens, mixture_ratios, num_samples_per_epoch, batch_size, seed=42, datasets=None):
self.dataset_lens = dataset_lens
self.datasets = datasets
self.weights = _normalize_positive_weights(mixture_ratios)
self.num_samples = num_samples_per_epoch
self.batch_size = batch_size
self.seed = seed
self.sync_across_ranks = os.environ.get("PSI_TASK_MIXTURE_SYNC_RANKS", "0").strip().lower() in {
"1",
"true",
"yes",
"on",
}
self.shard_aware = os.environ.get("PSI_TASK_MIXTURE_SHARD_AWARE", "0").strip().lower() in {
"1",
"true",
"yes",
"on",
}
self.task_block_batches = max(int(os.environ.get("PSI_TASK_MIXTURE_BLOCK_BATCHES", "1")), 1)
self.worker_lanes = max(int(os.environ.get("PSI_OMNI_OSS_WORKER_LANES", "1")), 1)
self.rank_local_blocks = os.environ.get("PSI_TASK_MIXTURE_RANK_LOCAL_BLOCKS", "0").strip().lower() in {
"1",
"true",
"yes",
"on",
}
# In OSS training, long task blocks are useful for shard reuse but can
# make the global loss jump when the set of tasks present on each rank
# changes. When enabled, assign a fixed integer number of ranks to each
# task for the whole epoch. Each global step then has the same task
# composition while every rank keeps reusing one task's resident OSS
# shard. Ratios that cannot be represented exactly by ``world_size``
# fall back to the legacy block scheduler below.
self.balanced_rank_mode = os.environ.get(
"PSI_TASK_MIXTURE_BALANCED_RANKS", "0"
).strip().lower() in {"1", "true", "yes", "on"}
self.rank = int(os.environ.get("RANK", 0))
self.world_size = int(os.environ.get("WORLD_SIZE", 1))
self.num_samples_rank = math.ceil(self.num_samples / self.world_size)
self.epoch = 0
def _balanced_rank_assignment(self) -> list[int] | None:
"""Return an exact deterministic task assignment for all ranks.
A requested ratio is used only when multiplying its normalized weights
by ``world_size`` produces positive integers. This supports both the
old 2/2/2/2 mix and the diagnostic 3/1/3/1 mix without silently
approximating an unsupported ratio. Every rank computes the same
epoch-specific permutation; only its own entry is consumed.
"""
if not self.balanced_rank_mode:
return None
positive_ids = [
i for i, weight in enumerate(self.weights.tolist()) if weight > 0
]
task_count = len(positive_ids)
if task_count <= 0 or self.world_size < task_count:
return None
raw_counts = self.weights[positive_ids] * self.world_size
rank_counts = torch.round(raw_counts).long()
if not torch.allclose(
raw_counts,
rank_counts.to(dtype=raw_counts.dtype),
atol=1e-12,
rtol=0.0,
) or torch.any(rank_counts <= 0) or int(rank_counts.sum().item()) != self.world_size:
return None
assignment = [
dataset_id
for dataset_id, rank_count in zip(positive_ids, rank_counts.tolist())
for _ in range(int(rank_count))
]
generator = torch.Generator()
generator.manual_seed(self.seed + self.epoch * 1_000_003 + 17)
permutation = torch.randperm(self.world_size, generator=generator).tolist()
shuffled = [0] * self.world_size
for position, rank in enumerate(permutation):
shuffled[rank] = assignment[position]
return shuffled
def _balanced_dataset_for_rank(self) -> int | None:
assignment = self._balanced_rank_assignment()
if assignment is None:
return None
dataset_id = int(assignment[self.rank])
if self.rank == 0 and self.epoch == 0:
counts = {task_id: assignment.count(task_id) for task_id in sorted(set(assignment))}
print(
f"[TASK MIXTURE] balanced rank mode enabled: assignment={assignment} "
f"counts={counts}",
flush=True,
)
return dataset_id
def set_epoch(self, epoch):
self.epoch = epoch
def _sample_dataset_indices(
self,
dataset_id: int,
generator: torch.Generator,
lane_id: int = 0,
):
dataset_len = int(self.dataset_lens[dataset_id])
if self.shard_aware and self.datasets is not None:
dataset = self.datasets[dataset_id]
sampler = getattr(dataset, "sample_shard_aware_indices", None)
if callable(sampler):
try:
sampled = sampler(self.batch_size, generator, lane_id=lane_id)
except TypeError as exc:
if "lane_id" not in str(exc):
raise
sampled = sampler(self.batch_size, generator)
if sampled is not None:
indices = getattr(sampled, "indices", sampled)
if len(indices) != self.batch_size:
raise RuntimeError(
f"Shard-aware sampler for dataset {dataset_id} returned {len(indices)} "
f"indices, expected {self.batch_size}"
)
if hasattr(sampled, "indices"):
sampled.indices[:] = [int(i) for i in indices]
return sampled
return [int(i) for i in indices]
return torch.randint(
low=0,
high=dataset_len,
size=(self.batch_size,),
generator=generator,
).tolist()
def _build_batch_schedule(self, total_batches, *, seed_offset: int = 0):
positive_ids = [i for i, weight in enumerate(self.weights.tolist()) if weight > 0]
if not positive_ids:
raise ValueError("TaskMixtureSampler received no positive mixture ratios")
positive_weights = self.weights[positive_ids]
positive_weights = positive_weights / positive_weights.sum()
raw_counts = positive_weights * total_batches
batch_counts = torch.floor(raw_counts).long()
batches_left = total_batches - int(batch_counts.sum().item())
if batches_left > 0:
frac = raw_counts - batch_counts.double()
for frac_index in torch.argsort(frac, descending=True)[:batches_left].tolist():
batch_counts[frac_index] += 1
if self.task_block_batches <= 1:
schedule = []
for local_idx, dataset_id in enumerate(positive_ids):
schedule.extend([dataset_id] * int(batch_counts[local_idx].item()))
g = torch.Generator()
g.manual_seed(self.seed + self.epoch + int(seed_offset))
shuffle_order = torch.randperm(len(schedule), generator=g).tolist()
schedule = [schedule[i] for i in shuffle_order]
else:
blocks = []
for local_idx, dataset_id in enumerate(positive_ids):
remaining = int(batch_counts[local_idx].item())
while remaining > 0:
block_size = min(self.task_block_batches, remaining)
blocks.append([dataset_id] * block_size)
remaining -= block_size
g = torch.Generator()
g.manual_seed(self.seed + self.epoch + int(seed_offset))
block_order = torch.randperm(len(blocks), generator=g).tolist()
schedule = [dataset_id for block_idx in block_order for dataset_id in blocks[block_idx]]
if len(schedule) != total_batches:
raise RuntimeError(
f"TaskMixtureSampler built invalid schedule: expected {total_batches} batches, got {len(schedule)}"
)
return schedule
@staticmethod
def _format_batch(dataset_id: int, sampled):
indices = getattr(sampled, "indices", sampled)
current_shard = getattr(sampled, "current_shard", None)
if current_shard:
next_shard = getattr(sampled, "next_shard", None)
sample_hints = getattr(sampled, "sample_hints", None)
if sample_hints is not None:
if len(sample_hints) != len(indices):
raise RuntimeError(
f"OSS sample hint count mismatch: hints={len(sample_hints)} indices={len(indices)}"
)
return [
(
dataset_id,
int(sample_id),
str(current_shard),
str(next_shard) if next_shard else "",
str(hint[0]),
str(hint[1]),
int(hint[2]),
int(hint[3]),
int(hint[4]),
)
for sample_id, hint in zip(indices, sample_hints)
]
return [
(dataset_id, int(sample_id), str(current_shard), str(next_shard) if next_shard else "")
for sample_id in indices
]
return [(dataset_id, int(sample_id)) for sample_id in indices]
def __iter__(self):
num_batches_rank = len(self)
balanced_dataset_id = self._balanced_dataset_for_rank()
if balanced_dataset_id is not None:
# Keep one task on each rank for the epoch. This gives an exact
# equal task mixture on every global optimizer step and preserves
# OSS shard locality/prefetching instead of switching tasks every
# few batches.
for local_batch_idx in range(num_batches_rank):
g = torch.Generator()
g.manual_seed(
self.seed
+ self.epoch * num_batches_rank * self.world_size
+ local_batch_idx * self.world_size
+ self.rank
)
sampled = self._sample_dataset_indices(
balanced_dataset_id,
g,
lane_id=local_batch_idx % self.worker_lanes,
)
yield self._format_batch(balanced_dataset_id, sampled)
return
if self.sync_across_ranks:
batch_schedule = self._build_batch_schedule(num_batches_rank)
for local_batch_idx, dataset_id in enumerate(batch_schedule):
g = torch.Generator()
g.manual_seed(
self.seed
+ self.epoch * num_batches_rank * self.world_size
+ local_batch_idx * self.world_size
+ self.rank
)
sampled = self._sample_dataset_indices(
dataset_id,
g,
lane_id=local_batch_idx % self.worker_lanes,
)
yield self._format_batch(dataset_id, sampled)
return
if self.rank_local_blocks and self.task_block_batches > 1:
batch_schedule = self._build_batch_schedule(
num_batches_rank,
seed_offset=(self.rank + 1) * 1_000_003,
)
for local_batch_idx, dataset_id in enumerate(batch_schedule):
g = torch.Generator()
g.manual_seed(
self.seed
+ self.epoch * num_batches_rank * self.world_size
+ local_batch_idx * self.world_size
+ self.rank
)
sampled = self._sample_dataset_indices(
dataset_id,
g,
lane_id=local_batch_idx % self.worker_lanes,
)
yield self._format_batch(dataset_id, sampled)
return
total_batches = num_batches_rank * self.world_size
batch_schedule = self._build_batch_schedule(total_batches)
local_batch_idx = 0
for batch_idx, dataset_id in enumerate(batch_schedule):
if batch_idx % self.world_size != self.rank:
continue
g = torch.Generator()
g.manual_seed(self.seed + self.epoch * total_batches + batch_idx)
sampled = self._sample_dataset_indices(
dataset_id,
g,
lane_id=local_batch_idx % self.worker_lanes,
)
yield self._format_batch(dataset_id, sampled)
local_batch_idx += 1
def __len__(self):
return math.ceil(self.num_samples_rank / self.batch_size)
class OssTaskBatchSampler(Sampler):
"""Batch sampler that keeps evaluation batches inside one OSS shard.
The normal validation ``RandomSampler`` scatters 64 indices over many
large OSS files, causing unnecessary random slice reads. This sampler
asks the dataset's existing ``sample_shard_aware_indices`` implementation
for a batch and forwards the shard offsets as index metadata. ``Dataset``
consumes that metadata in ``__getitems__`` to avoid repeated manifest or
SQLite lookups while loading the batch.
``num_batches`` is the number of batches before Accelerate shards them
across ranks. Callers should round it up to a multiple of world size when
they want a fixed global validation budget.
"""
def __init__(
self,
dataset,
batch_size: int,
num_batches: int,
*,
seed: int = 42,
worker_lanes: int = 1,
):
self.dataset = dataset
self.batch_size = max(int(batch_size), 1)
self.num_batches = max(int(num_batches), 0)
self.seed = int(seed)
self.worker_lanes = max(int(worker_lanes), 1)
self._prepared_batches = None
self._fixed_batches = None
self.fixed_batches = os.environ.get(
"PSI_OMNI_OSS_EVAL_FIXED_BATCHES", "0"
).strip().lower() in {"1", "true", "yes", "on"}
@staticmethod
def _copy_batches(batches):
# Sample entries are immutable tuples. Copy the two list levels so
# local-path remapping never mutates the canonical evaluation set.
return [list(batch) for batch in batches]
@staticmethod
def _format_sampled(sampled):
indices = getattr(sampled, "indices", sampled)
current_shard = getattr(sampled, "current_shard", None)
if not current_shard:
return [int(index) for index in indices]
next_shard = getattr(sampled, "next_shard", None)
sample_hints = getattr(sampled, "sample_hints", None)
if sample_hints is None:
return [
(
int(index),
str(current_shard),
str(next_shard) if next_shard else "",
)
for index in indices
]
if len(sample_hints) != len(indices):
raise RuntimeError(
f"OSS eval sample hint count mismatch: hints={len(sample_hints)} "
f"indices={len(indices)}"
)
return [
(
int(index),
str(current_shard),
str(next_shard) if next_shard else "",
str(hint[0]),
str(hint[1]),
int(hint[2]),
int(hint[3]),
int(hint[4]),
)
for index, hint in zip(indices, sample_hints)
]
def _iter_batches(self):
sample_fn = getattr(self.dataset, "sample_shard_aware_indices", None)
for batch_idx in range(self.num_batches):
generator = torch.Generator()
generator.manual_seed(self.seed + batch_idx)
if callable(sample_fn):
try:
sampled = sample_fn(
self.batch_size,
generator,
lane_id=batch_idx % self.worker_lanes,
)
except TypeError as exc:
if "lane_id" not in str(exc):
raise
sampled = sample_fn(self.batch_size, generator)
else:
sampled = torch.randint(
low=0,
high=max(len(self.dataset), 1),
size=(self.batch_size,),
generator=generator,
).tolist()
if sampled is None:
sampled = torch.randint(
low=0,
high=max(len(self.dataset), 1),
size=(self.batch_size,),
generator=generator,
).tolist()
yield self._format_sampled(sampled)
def prepare_batches(self):
"""Materialize the deterministic eval schedule before data loading.
The schedule is consumed exactly once by ``__iter__``. This permits
the trainer to stage its shard files locally while the last training
steps continue, without sampling a second or different validation
sequence.
"""
if self._prepared_batches is None:
if self.fixed_batches:
if self._fixed_batches is None:
self._fixed_batches = list(self._iter_batches())
self._prepared_batches = self._copy_batches(self._fixed_batches)
else:
self._prepared_batches = list(self._iter_batches())
return self._prepared_batches
def prepared_shard_paths(self) -> set[str]:
batches = self.prepare_batches()
return {
str(batch[0][1])
for batch in batches
if batch and isinstance(batch[0], (tuple, list)) and len(batch[0]) >= 3
}
def remap_prepared_shards(self, path_mapping: dict[str, str]) -> None:
batches = self.prepare_batches()
remapped_batches = []
for batch in batches:
remapped_batch = []
for sample in batch:
if not isinstance(sample, (tuple, list)) or len(sample) < 3:
remapped_batch.append(sample)
continue
values = list(sample)
values[1] = path_mapping.get(str(values[1]), str(values[1]))
if values[2]:
values[2] = path_mapping.get(str(values[2]), str(values[2]))
remapped_batch.append(tuple(values))
remapped_batches.append(remapped_batch)
self._prepared_batches = remapped_batches
def __iter__(self):
if self._prepared_batches is not None:
batches = self._prepared_batches
self._prepared_batches = None
yield from batches
return
if self.fixed_batches:
if self._fixed_batches is None:
self._fixed_batches = list(self._iter_batches())
yield from self._copy_batches(self._fixed_batches)
return
yield from self._iter_batches()
def __len__(self):
return self.num_batches
@dataclass
class DatasetSpec:
# dataset: torch.utils.data.Dataset
dataset_length: int
prob: float # mixture ratio
image_size: tuple[int, int] # e.g. 224, 448
tokens_per_image: int # (image_size / patch)^2
class TokenMixtureSampler(Sampler):
"""
Multi-node safe dataset mixture sampler that samples from datasets with different image resolutoion while
keeping the total number of tokens per batch (approximately) fixed.
"""
def __init__(
self,
specs: list[DatasetSpec],
tokens_per_batch: int, # per rank
num_batches_per_rank: int,
seed: int = 0,
):
self.specs = specs
self.tokens_per_batch = tokens_per_batch
self.num_batches_per_rank = num_batches_per_rank
self.seed = seed
self.probs = [s.prob for s in specs]
self.epoch = 0
# Distributed settings
self.rank = int(os.environ.get("RANK", 0))
self.world_size = int(os.environ.get("WORLD_SIZE", 1))
def set_epoch(self, epoch):
self.epoch = epoch
# overwatch.info(f"TokenMixtureSampler set to epoch {epoch}")
def __iter__(self):
# assert self.epoch is not None, "TokenMixtureSampler epoch not set. Please call set_epoch(epoch) before iterating."
# For each global batch index, synchronize dataset_id selection across all ranks
for batch_idx in range(self.num_batches_per_rank * self.world_size):
if batch_idx % self.world_size != self.rank:
continue
# Use a deterministic RNG for this global! batch to pick dataset_id and indices
batch_seed = self.seed + self.epoch + batch_idx # // self.world_size
local_rng = random.Random(batch_seed)
dataset_id = local_rng.choices(range(len(self.specs)), weights=self.probs, k=1)[0]
# print(f"[TokenMixtureSampler] rank={self.rank} seed={batch_seed} dataset_id={dataset_id}")
spec = self.specs[dataset_id]
batch_size = max(1, self.tokens_per_batch // spec.tokens_per_image)
local_batch_rng = random.Random(self.seed + self.epoch + batch_idx)
indices = [
(dataset_id, local_batch_rng.randrange(spec.dataset_length))
for _ in range(batch_size)
]
# overwatch.info(f"rank {overwatch.rank()}: {self.epoch} batch {batch_idx} dataset_id {dataset_id} batch_size {batch_size}")
yield indices
def __len__(self):
return self.num_batches_per_rank
|