Skip to content

refactor: remove redundant .to(device) in StageNet and convert assert to ValueError#966

Open
haoyu-haoyu wants to merge 2 commits intosunlabuiuc:masterfrom
haoyu-haoyu:refactor/code-quality
Open

refactor: remove redundant .to(device) in StageNet and convert assert to ValueError#966
haoyu-haoyu wants to merge 2 commits intosunlabuiuc:masterfrom
haoyu-haoyu:refactor/code-quality

Conversation

@haoyu-haoyu
Copy link
Copy Markdown
Contributor

Summary

  • Remove 9 redundant .to(device) calls from StageNet.step() loop (called T times per forward pass)
  • Convert input-validation assert statements to ValueError in user-facing modules

Changes

Performance: stagenet_mha.py

The step() method is called once per timestep in the forward loop. It contained 9 .to(device=device) calls on intermediate tensors that already inherit device from their inputs. Removing them eliminates ~9T unnecessary operations per forward pass.

Code quality: assertValueError

Converted input-validation assertions to proper ValueError exceptions in:

File Assertions converted
datasets/splitter.py 19 (ratio validation, data type checks)
calib/calibration/hb.py 6 (parameter bounds)
tasks/readmission_prediction.py 5 (window/age validation)
tasks/sleep_staging.py 4 (epoch/channel validation)
tasks/benchmark_ehrshot.py 1
tasks/covid19_cxr_classification.py 1
tasks/in_hospital_mortality_mimic4.py 1
tasks/medical_transcriptions_classification.py 1

Internal invariants (red-black tree, model framework checks) are intentionally left as assert.

Test plan

  • CI passes
  • python -m pytest tests/ passes
  • No semantic changes — only error type changes from AssertionError to ValueError

… to ValueError

Remove 9 redundant .to(device) calls from StageNet step() loop and
convert input-validation assertions to ValueError in splitter, tasks,
and calibration modules.

Assisted-by: Claude Code
The assert-to-ValueError conversion in the previous commit left two
docstrings still referencing AssertionError.

Assisted-by: Claude Code
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