Git, Shell and Linux for AI Work
Commit and branch, pipe and filter logs, keep runs alive in tmux, move files to a GPU box and fix Linux errors.
- Concepts and terms
- Tasks and commands
- Annotated code
- Diagrams
- Troubleshooting
- Questions and tips
Where git moves your work#
Git tasks#
| Task | Code |
|---|---|
| Set your identity | git config --global user.name "Your Name" |
| Save work | git add file.py then git commit -m "<message>" |
| Back it up | git push origin main |
| Branch for an experiment | git checkout -b experiment/ |
| Merge it back | git checkout main then git merge experiment/new-optimizer |
| Work on your fork | git checkout -b my-progress then git push -u origin my-progress |
| History of one phase | git log --oneline -- phases/ |
| Files of one commit | git show --stat <hash> |
Pipes and redirects#
| Command | What it does |
|---|---|
> | Write stdout to a file (overwrite) |
>> | Append stdout to a file |
2> | Write stderr to a file |
2>&1 | Send stderr to the same place as stdout |
| | Send stdout as stdin to the next command |
python train.py > train_full.log 2>&1 | Both streams to one file |
python train.py 2>&1 | tee train.log | Log and show the output |
Filter training logs#
cat train.log | grep "loss" | wc -lcount loss linesgrep "loss:" train.log | awk '{print $NF}' > losses.txtlast field onlytail -f train.log | grep --line-buffered "ERROR"live errorsgrep "final_accuracy" results/*.log | sort -t= -k2 -n -rbest run firstdiff <(grep "accuracy" exp1.log) <(grep "accuracy" exp2.log)find . -name "*.py" | xargs wc -l | tail -1project sizeenv | grep -i cudacheck before a runtmux sessions#
| Task | Code |
|---|---|
| Start a named session | tmux new -s training |
| Split horizontally | Ctrl+B then " |
| Split vertically | Ctrl+B then % |
| Move between panes | Ctrl+B then arrow keys |
| Detach, run keeps going | Ctrl+B then d |
| Reattach | tmux attach -t training |
| List sessions | tmux ls |
| Kill a session | tmux kill-session -t training |
Linux files, permissions, packages#
| Command | What it does |
|---|---|
mkdir -p a/ | Make nested directories |
rm -rf my-dir/ | Delete a directory. There is no undo |
find . -name "*.ckpt" -size +1G | Checkpoints larger than 1GB |
grep -r "learning_rate" . | Search all files here |
chmod +x train.sh | Make a script executable |
chmod 755 deploy.sh | Owner full, others read+execute |
sudo apt update && sudo apt install -y build-essential git curl wget tmux htop unzip python3-venv | Fresh GPU box packages |
sudo systemctl status nginx | Check a service. Also start, stop, restart, enable |
Work on a remote GPU box#
ssh -i ~/.ssh/my_gpu_key user@gpu-box-ipconnect with a keyscp model.pt user@gpu-box-ip:~/models/copy to remotescp user@gpu-box-ip:~/results/metrics.json ./copy from remotersync -avz --progress ./data/ user@remote:/data/changed bytes onlyssh -L 8888:localhost:8888 user@gpu-box-ipremote Jupyter on localhost:8888~/.ssh/config: Host gpu, HostName, User, IdentityFile
ssh -G gpuprint the settings SSH usesssh gpuconnect by the short nameTroubleshooting#
| Symptom | Cause | Fix |
|---|---|---|
| "Permission denied" | Permission bits | chmod +x or sudo |
| Run dies when the laptop closes | The SSH session ends | Run it inside tmux |
sed -i 's/a/b/' file fails on macOS | macOS sed needs '' after -i | sed -i '' 's/ |
| Bash script breaks | Windows \r\n line endings | Run dos2unix |
| File not found on Linux only | Case-sensitive filesystem | Model.py and model.py differ |
pbcopy not found | Not available over SSH | Use cat or less |
| No CUDA in WSL2 | Linux driver in WSL | Install the Windows NVIDIA driver |
| Disk full | Models and datasets | du -sh *, then clear caches |
Course aliases#
- Load them
source phases/00-setup-and-tooling/10-terminal-and-shell/code/shell_aliases.shgpu,gpumem,gpuprocs- GPU status, memory and processes as CSV.
ae,uvvenv- activate
.venv, or make one with uv and activate it. watchloss,taillog <pattern>- follow
logs/*.logfor a pattern. trainenv <name>- tmux session with
nvidia-smiandhtoppanes. syncto <host> <remote_path>- rsync up.
syncfrompulls back. bigmodels,memhogs- largest model files, top memory processes.
Questions#
&,nohupor tmux?nohupand tmux survive a closed terminal. Only tmux lets you reattach.rsyncorscp?rsyncfor anything large. It resumes and sends changed bytes.- Why fork the course repo?
- Only maintainers can push. Your fork is
origin. - Rebase or submodules?
- The course does not need them.
- Run as root?
- No. Use
sudoonly when needed.
Tips#
- Add
*.pt,*.pthand*.safetensorsto.gitignore. - Run
sudo apt updatebefore each install. - Use tmux for anything longer than a few minutes.
- Double-check the path before
rm -rf. - macOS defaults to
~/.zshrc. Most Linux servers use~/.bashrc.
Run, monitor and free disk#
| Command | What it does |
|---|---|
watch -n1 nvidia-smi | GPU use every second |
nvidia-smi --query-compute-apps= | Processes on the GPU |
htop | F6 sorts, F5 tree, F9 kills, / searches |
nohup python train.py > train.log 2>&1 & | Background run that survives terminal close |
df -h | Disk use of all drives |
du -sh ~/ | pip and huggingface models land here |
du -h --max-depth= | Biggest space hogs |
pip cache purge and sudo apt clean | Clear caches |