PyCharm NanoGPT Setup and Debugging | Generated by AI
Here’s how to set it up in PyCharm:
-
Open the project: File → Open → ~/projects/nanoGPT
-
Set Python interpreter: PyCharm → Settings → Project → Python Interpreter → Add → Existing →
/opt/homebrew/bin/python3.14 - Create a Run Configuration for sample.py:
- Run → Edit Configurations → + → Python
- Name:
sample-gpt2 - Script path:
sample.py -
Parameters:
--init_from=gpt2 --device=mps --dtype=float32 --num_samples=1 --max_new_tokens=150 --start="Hello world" - Working directory:
/Users/lzwjava/projects/nanoGPT
- Create another for the Shakespeare model:
- Name:
sample-shakespeare - Script path:
sample.py -
Parameters:
--out_dir=out-shakespeare-char --device=mps --dtype=float32 --num_samples=1 --max_new_tokens=200 --start="ROMEO:"
- Name:
- Set breakpoints — good places to start:
model.pyline ~175: insideforward()ofCausalSelfAttention— watch q,k,v shapesmodel.pyline ~210: insideforward()ofMLP— watch the 4x expansionsample.pyline 87: themodel.generate()call — step into to see autoregressive loopmodel.pyline ~245:GPT.generate()— watch how tokens are appended one by one
- Debug: click the bug icon (Shift+F9) instead of Run. Use:
- F7: Step Into (dive into functions)
- F8: Step Over
- F9: Resume (run to next breakpoint)
- Alt+Click on variable: evaluate expression
For train.py, create a config like:
- Script path:
train.py - Parameters:
config/train_shakespeare_char.py --device=mps - This trains the char-level model — set a breakpoint in the training loop at
train.pyline ~175 (for X, Y in loader:) to watch loss decrease step by step.
That’s it. PyCharm’s debugger with MPS tensors works fine — you can inspect shapes, values, gradients in the Variables panel.