KatInterview

Fixing and Scaling a Misbehaving CIFAR-10 Training Job

Machine LearningModel answer
MediumModel answerMachine Learningpytorchdebuggingfsdp

Last updated

Diagnose a CIFAR-10 PyTorch job that stalls, hits NaNs, or runs slowly, then scale it with FSDP, reason about sparse gradient communication, and decide when custom kernels pay off.

A teammate hands you a PyTorch script that trains an image classifier on CIFAR-10. It has one or more of these problems:

  • Accuracy hovers around chance level (about 10%).
  • The loss turns into NaN or Inf partway through training.
  • Each epoch takes far longer than the hardware should require.

Answer the following as you would in a technical discussion.

  1. Debugging. Lay out a systematic checklist for finding the root cause of each symptom. Cover data, model, loss, optimizer, device placement, and numeric precision, and name the quick experiments you would run to narrow things down.
  2. Scaling out with FSDP. Describe how you would move this job to several GPUs with Fully Sharded Data Parallel. Address:
    • how distributed training is initialized,
    • how the model is wrapped,
    • what happens to parameters, gradients, and optimizer state,
    • how gradient accumulation and checkpointing work.
  3. Sparse gradient all-reduce. Explain how you would implement or approximate a sparse gradient (or sparse matrix) all-reduce. Which communication patterns would you use, how do they compare to a dense all-reduce, and when is the effort justified?
  4. Custom kernels. When would you propose writing a custom CUDA or Triton kernel for training, and how would you verify both the speedup and the correctness?

Be concrete: mention specific PyTorch APIs, rough numbers, and trade-offs. Point out where the standard advice does not fit this particular workload.

Hints

Hint 1

Start with experiments that remove whole categories of bugs: check the initial loss against ln(10), overfit a single small batch, and look at the post-transform images with their labels.

Hint 2

For FSDP, think about what is sharded (parameters, gradients, optimizer state), when all-gather and reduce-scatter occur, and why the optimizer must be created after wrapping. Also ask whether a model as small as a CIFAR ResNet even needs FSDP.

Hint 3

For sparse all-reduce and custom kernels, do the arithmetic: compare bytes on the wire for top-k plus indices versus dense, and compare a kernel's achieved bandwidth or FLOPs against the hardware roofline before deciding it is worth writing.