Skip to content

The best ChatGPT that $100 can buy.

Python

bring back an assert guarding against bad param sizing

Andrej Karpathy98eed6df189e
diff --git a/nanochat/optim.py b/nanochat/optim.py
index 4cc2a1f..42d862b 100644
--- a/nanochat/optim.py
+++ b/nanochat/optim.py
@@ -377,6 +377,7 @@ class DistMuonAdamW(torch.optim.Optimizer):
param_infos[p] = dict(future=future, grad_slice=grad, is_small=True)
else:
# Large params: reduce_scatter
+ assert grad.shape[0] % world_size == 0, f"AdamW reduce_scatter requires shape[0] ({grad.shape[0]}) divisible by world_size ({world_size})"
rank_size = grad.shape[0] // world_size
grad_slice = torch.empty_like(grad[:rank_size])
future = dist.reduce_scatter_tensor(grad_slice, grad, op=dist.ReduceOp.AVG, async_op=True).get_future()