Repository navigation
fix(measure): every measurement leaves the model in the mode it came in - #10
Merged
Merged
Conversation
Measuring put the model in eval mode and never put it back, so a model being trained came back in eval mode (dropout/BN silently off for the rest of training). Same family as the device bug: `_on_device` restored the device, nothing restored `model.training`. `_measuring(model, dev)` composes `_on_device` with the mode round-trip (record `model.training`, `eval()`, restore in `finally`), and replaces the six `_on_device(model.eval(), dev)` sites: - speed.py `_forward_latencies` (compute_speed, sweep_*) - memory.py `_gpu_metrics` - memory.py `_cpu_metrics` - energy.py `compute_energy` - profiling.py `_profile_layers` (LayerProfiler.profile) - roofline.py `RooflineAnalyzer.profile` `benchmark()` keeps its own eval/restore: it is not redundant, because compute_compute() never sets eval and relies on it. It now says so and uses the same `model.train(was_training)` idiom. Tests: per touched notebook, a `.train()` model must come back with `training is True` (red on master for all five entry points) and an `.eval()` one must stay False (catches an over-eager restore). Those cells are CPU-only, except energy's, which sits with the existing codecarbon cell under `#|notest` to keep host power metering out of CI. Core also covers restore-on-exception and the CUDA device+mode round-trip. Claude-Session: https://claude.ai/code/session_0177tJZDQTTjQdycXV1EZtdJ
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
Same family as #9: every measurement put the model in eval mode and never restored
training, so a model in training mode came back in eval mode aftercompute_speed(and the sweeps),compute_memory,compute_energy,LayerProfiler.profileorRooflineAnalyzer.profile. Onlybenchmark()restored the flag.One context manager,
_measuring(model, dev)infasterbench.core, recordsmodel.training, wraps_on_device, yields the model in eval mode, and restores the mode in afinally. The six sites go fromwith _on_device(model.eval(), dev) as model:towith _measuring(model, dev) as model:, one line each;_on_deviceitself is unchanged.benchmark()keeps its own restore becausecompute_computenever sets eval mode andbenchmark()'smodel.eval()is what covers it.Red → green
On
origin/master(agit archivesnapshot), a.train()model measured on CPU:On this branch all five read
training=True, and each site also checks that a model already in eval mode stays in eval mode. Unlike the device tests of #9, these run on CPU, so CI covers them (the energy one stays#| notest, next to the existing codecarbon cell, so CI does not gain a second tracker run); a mutation of the assertion fails the default suite. The two-line CUDA check in core's#| notestcell also asserts device and mode together.Tests
Default suite:
Success.The seven touched notebooks withflags='notest'on a GPU:Success.each. CI gate (nbdev-cleanthennbdev-exportwith CI's nbdev 3.3.15):git status --porcelain -unoempty on two passes.__version__0.1.0,_modidx.pygains_measuring,__init__.pyuntouched.https://claude.ai/code/session_0177tJZDQTTjQdycXV1EZtdJ