Skip to content

Refactor MNIST forward style and improve device selection#1419

Open
amit-chaubey wants to merge 1 commit intopytorch:mainfrom
amit-chaubey:cleanup-mnist-forward-device-selection
Open

Refactor MNIST forward style and improve device selection#1419
amit-chaubey wants to merge 1 commit intopytorch:mainfrom
amit-chaubey:cleanup-mnist-forward-device-selection

Conversation

@amit-chaubey
Copy link
Copy Markdown

Summary

This PR improves readability in mnist/main.py and makes device selection explicit for CUDA/MPS fallback paths.

Changes

  • Refactor Net.forward() to use direct functional composition:
    • x = F.relu(self.conv1(x))
    • x = F.relu(self.conv2(x))
    • x = F.relu(self.fc1(x))
    • return F.log_softmax(self.fc2(x), dim=1) directly
  • Add concise type hints and short docstrings for forward, train, test, and main.
  • Add _mps_available() helper and update device selection:
    • Prefer torch.accelerator.current_accelerator() when available
    • Fallback to torch.device("cuda"), then torch.device("mps"), else CPU
  • Update use_accel gating so CUDA/MPS availability also enables accelerator code path.

Validation

  • python -m py_compile mnist/main.py

@meta-cla meta-cla bot added the cla signed label Apr 13, 2026
@netlify
Copy link
Copy Markdown

netlify bot commented Apr 13, 2026

Deploy Preview for pytorch-examples-preview canceled.

Name Link
🔨 Latest commit 5b84c52
🔍 Latest deploy log https://app.netlify.com/projects/pytorch-examples-preview/deploys/69dcc4df7d2c1d0008b62b5b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant