Merge pull request #14 from LokiMetaSmith/fix-cpu-ddp-init

Fix hardware detection for AMD ROCm and single-process CPU crashes
This commit is contained in:
Lawrence R Kincheloe III 2025-11-22 17:52:07 -06:00 committed by GitHub
commit ddc51d34df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 37 deletions

View File

@ -1,34 +0,0 @@
█████ █████
░░███ ░░███
████████ ██████ ████████ ██████ ██████ ░███████ ██████ ███████
░░███░░███ ░░░░░███ ░░███░░███ ███░░███ ███░░███ ░███░░███ ░░░░░███░░░███░
░███ ░███ ███████ ░███ ░███ ░███ ░███░███ ░░░ ░███ ░███ ███████ ░███
░███ ░███ ███░░███ ░███ ░███ ░███ ░███░███ ███ ░███ ░███ ███░░███ ░███ ███
████ █████░░████████ ████ █████░░██████ ░░██████ ████ █████░░███████ ░░█████
░░░░ ░░░░░ ░░░░░░░░ ░░░░ ░░░░░ ░░░░░░ ░░░░░░ ░░░░ ░░░░░ ░░░░░░░░ ░░░░░
Overriding: depth = 2
Overriding: device_batch_size = 2
Overriding: max_seq_len = 128
Overriding: num_iterations = 10
Overriding: run = dummy
Autodetected device type: cpu
2025-11-22 17:01:10,440 - nanochat.common - INFO - Distributed world size: 1
/app/nanochat/tokenizer.py:397: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
token_bytes = torch.load(f, map_location=device)
Vocab size: 265
num_layers: 2
model_dim: 128
num_heads: 1
num_kv_heads: 1
Tokens / micro-batch / rank: 2 x 128 = 256
Tokens / micro-batch: 256
Total batch size 524,288 => gradient accumulation steps: 2048
Number of parameters: 461,056
Estimated FLOPs per token: 2.956032e+06
Using user-provided number of iterations: 10
Total number of training tokens: 5,242,880
Tokens : Params ratio: 11.37
Total training FLOPs estimate: 1.549812e+13
Scaling the LR for the AdamW parameters ∝1/√(128/768) = 2.449490

View File

@ -47,7 +47,8 @@ python_functions = ["test_*"]
[tool.uv.sources]
torch = [
{ index = "pytorch-cpu", extra = "cpu" },
{ index = "pytorch-cu128", extra = "gpu" },
{ index = "pytorch-cu128", extra = "gpu" },
{ index = "pytorch-rocm", extra = "amd" },
]
[[tool.uv.index]]
@ -60,6 +61,11 @@ name = "pytorch-cu128"
url = "https://download.pytorch.org/whl/cu128"
explicit = true
[[tool.uv.index]]
name = "pytorch-rocm"
url = "https://repo.amd.com/rocm/whl/"
explicit = true
[project.optional-dependencies]
cpu = [
"torch>=2.8.0",
@ -67,11 +73,15 @@ cpu = [
gpu = [
"torch>=2.8.0",
]
amd = [
"torch>=2.8.0",
]
[tool.uv]
conflicts = [
[
{ extra = "cpu" },
{ extra = "gpu" },
{ extra = "gpu" },
{ extra = "amd" },
],
]

View File

@ -23,8 +23,21 @@ mkdir -p $NANOCHAT_BASE_DIR
command -v uv &> /dev/null || curl -LsSf https://astral.sh/uv/install.sh | sh
# create a .venv local virtual environment (if it doesn't exist)
[ -d ".venv" ] || uv venv
# install the repo dependencies
uv sync --extra gpu
# Detect hardware to install the correct torch version
if command -v nvidia-smi &> /dev/null; then
echo "NVIDIA GPU detected. Installing CUDA dependencies..."
EXTRAS="gpu"
elif [ -e /dev/kfd ]; then
echo "AMD GPU detected. Installing ROCm dependencies..."
EXTRAS="amd"
else
echo "No dedicated GPU detected. Installing CPU dependencies..."
EXTRAS="cpu"
fi
uv sync --extra $EXTRAS
# activate venv so that `python` uses the project's venv instead of system python
source .venv/bin/activate