Maximum training speed optimizations: zero-sync Z-loss, ce_chunk=512, async token accumulation, 64-token alignment, compile before DDP
Browse files190 unmodified lines19119219319419419519619719819919920020120230 unmodified lines23323423523623623723823919 unmodified lines259260261262263264262263264265266190 unmodified lines seq_lens = non_pad_mask.sum(dim=1, keepdim=True).clamp(min=1).float() dims = seq_lens * x.size(2) mag = (self.cfg.neftune_alpha / torch.sqrt(dims)).to(x.dtype) noise = torch.zeros_like(x).uniform_(-1.0, 1.0) * mag * non_pad_mask noise = torch.empty_like(x).uniform_(-1.0, 1.0) * mag * non_pad_mask else: special_mask = (idx < 64000).unsqueeze(-1) dims = x.size(1) * x.size(2) mag = self.cfg.neftune_alpha / math.sqrt(max(1, dims)) noise = torch.zeros_like(x).uniform_(-1.0, 1.0) * mag * special_mask noise = torch.empty_like(x).uniform_(-1.0, 1.0) * mag * special_mask x = x + noise
# Pre-slice and pre-cast RoPE buffers ONCE outside the 24-layer block loop30 unmodified lines loss = None if targets is not None: V = self.cfg.vocab_size ce_chunk = getattr(self.cfg, "ce_chunk", 128) # 128 tokens per chunk keeps intermediate logits <= 512MB even at batch 16 ce_chunk = getattr(self.cfg, "ce_chunk", 512) # 512 tokens per chunk saturates Tensor Cores with fewer GEMM calls
if shift_labels: # Shifted: predict token[i+1] from hidden[i]19 unmodified lines ) if self.cfg.z_loss_weight > 0.0: valid_mask = (chunk_tgt != -100) if valid_mask.any(): lse = torch.logsumexp(chunk_logits[valid_mask], dim=-1) chunk_loss = chunk_loss + self.cfg.z_loss_weight * (lse ** 2).sum() lse = torch.logsumexp(chunk_logits, dim=-1) chunk_loss = chunk_loss + self.cfg.z_loss_weight * torch.where(valid_mask, lse.square(), 0.0).sum()
total_loss = total_loss + chunk_loss
code/model.py+5-6