Add deterministic resume shard cursor and auto-calibrated rotate cadence
Browse files74 unmodified lines75767778798081828384851 unmodified line87888990919293150 unmodified lines2442452462472482492502512523 unmodified lines25625725825926026120 unmodified lines28228328428528628728828974 unmodified linesdelay *= 2class ShardPool:def __init__(self, shard_names, repo_id, token, local_dir, pool_size=3, seed=0):self.all_shards = list(shard_names)self.rng = random.Random(seed)self.rng.shuffle(self.all_shards)self.cursor = 0self.repo_id, self.token, self.local_dir, self.pool_size = repo_id, token, local_dir, pool_sizeos.makedirs(local_dir, exist_ok=True)self.pool = []1 unmodified linedef _next_name(self):if self.cursor >= len(self.all_shards):self.rng.shuffle(self.all_shards)self.cursor = 0name = self.all_shards[self.cursor]self.cursor += 1150 unmodified linesWEIGHT_DECAY = 0.1GRAD_CLIP = 1.0MAX_STEPS = int(os.environ.get("MAX_STEPS", TOTAL_STEPS))EVAL_EVERY = 500SAVE_EVERY = 2000ROTATE_EVERY = 500torch.backends.cudnn.benchmark = Truetorch.backends.cudnn.allow_tf32 = True3 unmodified linesif IS_MAIN:log(f"Model params: {model.num_params()/1e6:.1f}M | world_size={world_size}")log("Compiling model for Extreme Speed (this takes 1-2 mins on step 0)...")compute_cap = torch.cuda.get_device_capability(local_rank)USE_COMPILE = compute_cap[0] >= 8 # Only Ampere+ (A100, H100, H200, RTX 4090)20 unmodified linestrain_shards = sorted(f for f in files if f.startswith("shards/") and f.endswith(".bin") and "_val" not in f)val_shards = sorted(f for f in files if f.startswith("shards/") and f.endswith("_val.bin"))log(f"📦 Active Training Shards: {len(train_shards)} | Validation Shards: {len(val_shards)}")shard_pool = ShardPool(train_shards, DATA_REPO, HF_TOKEN, f"{WORK}/train_shards_r{rank}", seed=rank + start_step)val_tokens = load_val_tokens(val_shards, DATA_REPO, HF_TOKEN, f"{WORK}/val_shards") if IS_MAIN else None@torch.no_grad()74 unmodified lines757677787980818283841 unmodified line868788899091150 unmodified lines2422432442452462472482492502512522532543 unmodified lines25825926026126226326420 unmodified lines28528628728828929029129229329429529674 unmodified linesdelay *= 2class ShardPool:def __init__(self, shard_names, repo_id, token, local_dir, pool_size=3, seed=0, start_cursor=0):self.all_shards = list(shard_names)self.rng = random.Random(seed)self.cursor = start_cursor % max(1, len(self.all_shards))self.repo_id, self.token, self.local_dir, self.pool_size = repo_id, token, local_dir, pool_sizeos.makedirs(local_dir, exist_ok=True)self.pool = []1 unmodified linedef _next_name(self):if self.cursor >= len(self.all_shards):self.cursor = 0name = self.all_shards[self.cursor]self.cursor += 1150 unmodified linesWEIGHT_DECAY = 0.1GRAD_CLIP = 1.0MAX_STEPS = int(os.environ.get("MAX_STEPS", TOTAL_STEPS))EVAL_EVERY = int(os.environ.get("EVAL_EVERY", 250))SAVE_EVERY = int(os.environ.get("SAVE_EVERY", 250))# Dynamically calculate ROTATE_EVERY so 1 shard (50M tokens) is rotated as soon as its tokens are consumedtokens_per_step = MICRO_BATCH * GRAD_ACCUM * world_size * CONTEXT_LENtokens_per_shard = 50_000_000ROTATE_EVERY = max(10, tokens_per_shard // max(1, tokens_per_step))torch.backends.cudnn.benchmark = Truetorch.backends.cudnn.allow_tf32 = True3 unmodified linesif IS_MAIN:log(f"Model params: {model.num_params()/1e6:.1f}M | world_size={world_size}")log(f"Dynamic Shard Cadence: {tokens_per_step:,} tok/step -> Rotate shard every {ROTATE_EVERY} steps")log("Compiling model for Extreme Speed (this takes 1-2 mins on step 0)...")compute_cap = torch.cuda.get_device_capability(local_rank)USE_COMPILE = compute_cap[0] >= 8 # Only Ampere+ (A100, H100, H200, RTX 4090)20 unmodified linestrain_shards = sorted(f for f in files if f.startswith("shards/") and f.endswith(".bin") and "_val" not in f)val_shards = sorted(f for f in files if f.startswith("shards/") and f.endswith("_val.bin"))# Compute deterministic starting shard so resuming NEVER repeats old shards!steps_completed = max(0, start_step - 40000)start_shard_idx = (steps_completed // ROTATE_EVERY) % max(1, len(train_shards))log(f"🎯 Step {start_step}: Resuming at Shard #{start_shard_idx:03d} ({start_shard_idx} shards already consumed, 0 duplicates)")shard_pool = ShardPool(train_shards, DATA_REPO, HF_TOKEN, f"{WORK}/train_shards_r{rank}", seed=rank + start_step, start_cursor=start_shard_idx)val_tokens = load_val_tokens(val_shards, DATA_REPO, HF_TOKEN, f"{WORK}/val_shards") if IS_MAIN else None@torch.no_grad()
code/train.py+16-9