My DGX Spark keeps freezing and crashing when I try to run this code no matter the LLM

First, I would run just after a reboot (as soon as the system is back up), sudo nvidia-bug-report.sh. This will generate an nvidia-bug-report.log.gz file in your working directory.

Another thing would be trying to run the code with sudo swapoff -a a training run that would otherwise freeze the system will instead cause the process to be killed by the OOM killer, allowing the OS to survive

I would also check the following threads first: