Skip to content

Collapse duplicate device-init block in Calibration.__init__#95

Merged
MaxGhenis merged 1 commit into
mainfrom
fix/device-init
Apr 17, 2026
Merged

Collapse duplicate device-init block in Calibration.__init__#95
MaxGhenis merged 1 commit into
mainfrom
fix/device-init

Conversation

@MaxGhenis
Copy link
Copy Markdown
Contributor

Summary

Finding #6 (MED). Lines 100–110 repeated the device resolution already done at 68–75 and guarded CUDA seeding with if self.device == "cuda" — a torch.device vs string comparison inside a branch that only fired when device was not None. The net effect was that the CUDA fallback branch was unreachable and torch.cuda.manual_seed was never called on user-provided CUDA devices, breaking reproducibility of any stochastic CUDA kernel.

The device is now resolved exactly once up front. Torch is seeded uniformly on every path when a seed is set, and torch.cuda.manual_seed_all is invoked whenever self.device.type == "cuda".

Test plan

  • Add tests/test_device_init.py: self.device is a torch.device, torch is seeded on the CPU path, and the default fallback chain resolves without crashing.
  • All existing tests pass (uv run pytest tests -x -q -> 18 passed).

🤖 Generated with Claude Code

Lines 100-110 repeated the device resolution already done at 68-75
and guarded CUDA seeding with ``if self.device == "cuda"`` -- a
torch.device vs string comparison inside a branch that only fired
when ``device`` was not None. The net effect was that the CUDA
fallback branch was unreachable and ``torch.cuda.manual_seed`` was
never called on user-provided CUDA devices, breaking reproducibility
of any stochastic CUDA kernel (e.g. dropout).

Resolve the device exactly once up front. Seed torch uniformly on
every path when a seed is set, and invoke
``torch.cuda.manual_seed_all`` whenever ``self.device.type == "cuda"``.

Adds tests/test_device_init.py covering the torch.device type, torch
seeding on the CPU path, and the default fallback chain.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Apr 17, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
microcalibrate Ready Ready Preview, Comment Apr 17, 2026 0:40am

Request Review

Copy link
Copy Markdown
Contributor Author

@MaxGhenis MaxGhenis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (cannot self-approve; posting as comment).

  • Duplicate device-init block removed; self.device is now resolved exactly once through the single cuda → mps → cpu fallback chain.
  • self.device.type == "cuda" is the correct comparison (was self.device == "cuda" — technically works in modern torch, but was inside the unreachable else branch).
  • torch.manual_seed fires uniformly on every path (not just device is not None).
  • torch.cuda.manual_seed_all(seed) now fires whenever we actually resolved to a CUDA device.
  • Small incidental fix: torch.manual_seed(self.seed) is now guarded by if self.seed is not None, so passing seed=None explicitly no longer crashes (the default remains 42).

No behaviour change on CPU (verified by the existing CPU determinism test); CUDA seeding now actually fires.

@MaxGhenis MaxGhenis merged commit 11b5d2d into main Apr 17, 2026
6 checks passed
@MaxGhenis MaxGhenis deleted the fix/device-init branch April 17, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant