Collapse duplicate device-init block in Calibration.__init__#95
Merged
Conversation
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>
Contributor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
MaxGhenis
commented
Apr 17, 2026
Contributor
Author
MaxGhenis
left a comment
There was a problem hiding this comment.
LGTM (cannot self-approve; posting as comment).
- Duplicate device-init block removed;
self.deviceis now resolved exactly once through the singlecuda → mps → cpufallback chain. self.device.type == "cuda"is the correct comparison (wasself.device == "cuda"— technically works in modern torch, but was inside the unreachable else branch).torch.manual_seedfires uniformly on every path (not justdevice 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 byif self.seed is not None, so passingseed=Noneexplicitly no longer crashes (the default remains 42).
No behaviour change on CPU (verified by the existing CPU determinism test); CUDA seeding now actually fires.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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"— atorch.devicevs string comparison inside a branch that only fired whendevicewas notNone. The net effect was that the CUDA fallback branch was unreachable andtorch.cuda.manual_seedwas 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_allis invoked wheneverself.device.type == "cuda".Test plan
tests/test_device_init.py:self.deviceis atorch.device, torch is seeded on the CPU path, and the default fallback chain resolves without crashing.uv run pytest tests -x -q-> 18 passed).🤖 Generated with Claude Code