mirror of
https://github.com/karpathy/nanochat.git
synced 2025-12-06 04:12:13 +00:00
Compare commits
3 Commits
551fe1dc9e
...
5557b204c5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5557b204c5 | ||
|
|
bc1fca39f3 | ||
|
|
2dc85662c3 |
|
|
@ -113,12 +113,24 @@ def print_banner():
|
|||
"""
|
||||
print0(banner)
|
||||
|
||||
def is_ddp():
|
||||
# TODO is there a proper way
|
||||
return int(os.environ.get('RANK', -1)) != -1
|
||||
def is_ddp_requested() -> bool:
|
||||
"""
|
||||
True if launched by torchrun (env present), even before init.
|
||||
Used to decide whether we *should* initialize a PG.
|
||||
"""
|
||||
return all(k in os.environ for k in ("RANK", "LOCAL_RANK", "WORLD_SIZE"))
|
||||
|
||||
def is_ddp_initialized() -> bool:
|
||||
"""
|
||||
True if torch.distributed is available and the process group is initialized.
|
||||
Used at cleanup to avoid destroying a non-existent PG.
|
||||
"""
|
||||
return dist.is_available() and dist.is_initialized()
|
||||
|
||||
def get_dist_info():
|
||||
if is_ddp():
|
||||
if is_ddp_requested():
|
||||
# We rely on torchrun's env to decide if we SHOULD init.
|
||||
# (Initialization itself happens in compute init.)
|
||||
assert all(var in os.environ for var in ['RANK', 'LOCAL_RANK', 'WORLD_SIZE'])
|
||||
ddp_rank = int(os.environ['RANK'])
|
||||
ddp_local_rank = int(os.environ['LOCAL_RANK'])
|
||||
|
|
@ -161,8 +173,8 @@ def compute_init(device_type="cuda"): # cuda|cpu|mps
|
|||
torch.set_float32_matmul_precision("high") # uses tf32 instead of fp32 for matmuls
|
||||
|
||||
# Distributed setup: Distributed Data Parallel (DDP), optional, and requires CUDA
|
||||
ddp, ddp_rank, ddp_local_rank, ddp_world_size = get_dist_info()
|
||||
if ddp and device_type == "cuda":
|
||||
is_ddp_requested, ddp_rank, ddp_local_rank, ddp_world_size = get_dist_info()
|
||||
if is_ddp_requested and device_type == "cuda":
|
||||
device = torch.device("cuda", ddp_local_rank)
|
||||
torch.cuda.set_device(device) # make "cuda" default to this device
|
||||
dist.init_process_group(backend="nccl", device_id=device)
|
||||
|
|
@ -173,11 +185,11 @@ def compute_init(device_type="cuda"): # cuda|cpu|mps
|
|||
if ddp_rank == 0:
|
||||
logger.info(f"Distributed world size: {ddp_world_size}")
|
||||
|
||||
return ddp, ddp_rank, ddp_local_rank, ddp_world_size, device
|
||||
return is_ddp_requested, ddp_rank, ddp_local_rank, ddp_world_size, device
|
||||
|
||||
def compute_cleanup():
|
||||
"""Companion function to compute_init, to clean things up before script exit"""
|
||||
if is_ddp():
|
||||
if is_ddp_initialized():
|
||||
dist.destroy_process_group()
|
||||
|
||||
class DummyWandb:
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ Notable features:
|
|||
- norm after token embedding
|
||||
- no learnable params in rmsnorm
|
||||
- no bias in linear layers
|
||||
- Multi-Query Attention (MQA) support for more efficient inference
|
||||
- Group-Query Attention (GQA) support for more efficient inference
|
||||
"""
|
||||
|
||||
import math
|
||||
|
|
@ -29,7 +29,7 @@ class GPTConfig:
|
|||
vocab_size: int = 50304
|
||||
n_layer: int = 12
|
||||
n_head: int = 6 # number of query heads
|
||||
n_kv_head: int = 6 # number of key/value heads (MQA)
|
||||
n_kv_head: int = 6 # number of key/value heads (GQA)
|
||||
n_embd: int = 768
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user