The best ChatGPT that $100 can buy.
Python
add autodetect of device and related stuff. getting weird warnings/errors still, so wip
karpathy786119d593e8
diff --git a/nanochat/common.py b/nanochat/common.pyindex 05e371c..86c18de 100644--- a/nanochat/common.py+++ b/nanochat/common.py@@ -89,6 +89,16 @@ def get_dist_info():else:return False, 0, 0, 1+def autodetect_device_type():+ # prefer to use CUDA if available, otherwise use MPS, otherwise fallback on CPU+ if torch.cuda.is_available():+ device_type = "cuda"+ if torch.backends.mps.is_available():+ device_type = "mps"+ device_type = "cpu"+ print0(f"Autodetected device type: {device_type}")+ return device_type+def compute_init(device_type="cuda"): # cuda|cpu|mps"""Basic initialization that we keep doing over and over, so make common."""diff --git a/scripts/base_eval.py b/scripts/base_eval.pyindex a566d49..2d58d87 100644--- a/scripts/base_eval.py+++ b/scripts/base_eval.py@@ -19,7 +19,7 @@ import yamlimport pandas as pdimport torch-from nanochat.common import compute_init, compute_cleanup, print0, get_base_dir+from nanochat.common import compute_init, compute_cleanup, print0, get_base_dir, autodetect_device_typefrom nanochat.tokenizer import HuggingFaceTokenizerfrom nanochat.checkpoint_manager import load_modelfrom nanochat.core_eval import evaluate_task@@ -121,8 +121,10 @@ def main():assert len(sys.argv) in [1, 2], "Usage: python base_eval.py [hf_path]"# distributed / precision setup- ddp, ddp_rank, ddp_local_rank, ddp_world_size, device = compute_init()- autocast_ctx = torch.amp.autocast(device_type="cuda", dtype=torch.bfloat16)+ device_type = autodetect_device_type()+ ddp, ddp_rank, ddp_local_rank, ddp_world_size, device = compute_init(device_type)+ dtype = torch.bfloat16 if device_type == "cuda" else torch.float32 # use fp32 on CPU|MPS+ autocast_ctx = torch.amp.autocast(device_type=device_type, dtype=dtype)# Load model and tokenizer from command line or from file systemif len(sys.argv) >= 2:diff --git a/scripts/base_loss.py b/scripts/base_loss.pyindex ba3876d..1609d83 100644--- a/scripts/base_loss.py+++ b/scripts/base_loss.py@@ -9,7 +9,7 @@ torchrun --standalone --nproc_per_node=8 -m scripts.base_lossimport osimport torchfrom nanochat.checkpoint_manager import load_model-from nanochat.common import compute_init, print0, compute_cleanup+from nanochat.common import compute_init, print0, compute_cleanup, autodetect_device_typefrom nanochat.dataloader import tokenizing_distributed_data_loaderfrom nanochat.tokenizer import get_token_bytesfrom nanochat.loss_eval import evaluate_bpb@@ -20,15 +20,16 @@ device_batch_size = 32split_tokens = 20*524288 # number of tokens to evaluate per splitmodel_tag = None # optional model tag for the output directory namemodel_step = None # optional model step for the output directory name+device_type = "" # cuda|cpu|mps (empty => autodetect)exec(open(os.path.join('nanochat', 'configurator.py')).read()) # overrides from command line or config file# Load the base model and the tokenizer-ddp, ddp_rank, ddp_local_rank, ddp_world_size, device = compute_init()+device_type = autodetect_device_type() if device_type == "" else device_type+ddp, ddp_rank, ddp_local_rank, ddp_world_size, device = compute_init(device_type)+dtype = torch.bfloat16 if device_type == "cuda" else torch.float32 # use fp32 on CPU|MPSmodel, tokenizer, meta = load_model("base", device, phase="eval", model_tag=model_tag, step=model_step)sequence_len = meta["model_config"]["sequence_len"] # could be arbitrary really--# Set up the precision we'll run with-autocast_ctx = torch.amp.autocast(device_type="cuda", dtype=torch.bfloat16)+autocast_ctx = torch.amp.autocast(device_type=device_type, dtype=dtype)# Evaluate the loss on each splittokens_per_step = device_batch_size * sequence_len * ddp_world_sizediff --git a/scripts/base_train.py b/scripts/base_train.pyindex ebc5ff4..147fce2 100644--- a/scripts/base_train.py+++ b/scripts/base_train.py@@ -7,7 +7,7 @@ or distributed as:torchrun --nproc_per_node=8 base_train.py-python -m scripts.base_train --device_type=cpu --depth=4 --max_seq_len=512 --device_batch_size=1 --eval_tokens=512 --total_batch_size=512 --num_iterations=1000+python -m scripts.base_train --device_type=cpu --depth=4 --max_seq_len=512 --device_batch_size=1 --eval_tokens=512 --core_metric_max_per_task=8 --total_batch_size=512 --num_iterations=500If you have a Macbook, you're better off using device_type=mps instead of cpu"""@@ -19,7 +19,7 @@ import torchfrom nanochat.gpt import GPT, GPTConfigfrom nanochat.dataloader import tokenizing_distributed_data_loader-from nanochat.common import compute_init, compute_cleanup, print0, DummyWandb, print_banner, get_base_dir+from nanochat.common import compute_init, compute_cleanup, print0, DummyWandb, print_banner, get_base_dir, autodetect_device_typefrom nanochat.tokenizer import get_tokenizer, get_token_bytesfrom nanochat.checkpoint_manager import save_checkpointfrom nanochat.loss_eval import evaluate_bpb@@ -31,7 +31,7 @@ print_banner()# User settingsrun = "dummy" # wandb run name default ("dummy" is special - we won't log to wandb)# Runtime-device_type = "cuda" # cuda|cpu|mps+device_type = "" # cuda|cpu|mps (empty => autodetect good device type default, in order: CUDA > MPS > CPU)# Model architecturedepth = 20 # the depth of the Transformer model to train, rest of the kwargs are derivedmax_seq_len = 2048 # max context length@@ -62,6 +62,7 @@ user_config = {k: globals()[k] for k in config_keys} # will be useful for loggin# -----------------------------------------------------------------------------# Compute init+device_type = autodetect_device_type() if device_type == "" else device_typeddp, ddp_rank, ddp_local_rank, ddp_world_size, device = compute_init(device_type)master_process = ddp_rank == 0 # this process will do logging, checkpointing etc.dtype = torch.bfloat16 if device_type == "cuda" else torch.float32 # use fp32 on CPU|MPS