test: direct unit suite for the opt mode-framework core (56 tests)#1915
test: direct unit suite for the opt mode-framework core (56 tests)#1915arham766 wants to merge 1 commit into
Conversation
Hermetic tests for ModeloptStateManager, apply_mode, and the modelopt_state/save/restore surface via synthetic mode descriptors in a throwaway registry (fully torn down, including removal from the global registry list, since __del__ is unreachable while _all_registries holds a strong reference). Deliberately complements rather than duplicates test_chaining (real-mode chains, in-memory round-trips) and test_load_modelopt_state (corrupt-file schema validation); the file-based mto.save/mto.restore path was covered nowhere. Adversarially reviewed: 14 expectations re-derived from source, 5/5 seeded mutations killed with surgical precision, combined-process runs with the sibling suites pass in both orders. Documents three genuine defects with NOTE tests (modelopt_state aliases internal state; apply_mode of a ModelLike tuple with an empty mode list crashes; apply_mode rejects ModeDescriptor instances despite its docstring and the ModeType annotation). Part of the coverage initiative in NVIDIA#1902. Signed-off-by: arham766 <arhamislam766@yahoo.com>
📝 WalkthroughWalkthroughThis PR adds a new unit test file for the modelopt PyTorch conversion module, covering ModeloptStateManager state bookkeeping, mode sequencing, apply_mode behavior, serialization via modelopt_state, and end-to-end save/restore round-trip persistence, using a synthetic test mode registry and fixtures. ChangesConversion State Manager Test Coverage
Estimated code review effort: 3 (Moderate) | ~25 minutes Related Issues: None mentioned in the summary. Related PRs: None mentioned in the summary. Suggested labels: tests, torch, opt Suggested reviewers: None specified. 🐰 A rabbit hops through tests anew, 🚥 Pre-merge checks | ✅ 6✅ Passed checks (6 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/unit/torch/opt/test_conversion.py (1)
177-190: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueFixture registration/teardown is sound.
Module-scoped
yieldfixture correctly registers synthetic modes once and always tears them down after all tests in the module finish (pytest runsyield-fixture teardown regardless of test outcome). The explicit removal from_ModeRegistryCls._all_registrieswith an inline comment justifying why__del__won't fire is a good practice for avoiding global test-state residue.One minor nit:
registry.remove_mode(cls().name)instantiates a throwaway instance of each descriptor class purely to read.name. Sincenameis a fixed literal per subclass, this could be simplified, but it's inconsequential.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/unit/torch/opt/test_conversion.py` around lines 177 - 190, The fixture cleanup in tc_modes is fine, but the teardown currently creates a throwaway instance of each _TC_DESCRIPTOR_CLASSES entry just to read its name. Update the remove-mode path to use the descriptor’s class-level name source instead of instantiating the class, keeping the same register_mode/remove_mode flow in tc_modes and preserving the explicit _ModeRegistryCls._all_registries cleanup.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@tests/unit/torch/opt/test_conversion.py`:
- Around line 177-190: The fixture cleanup in tc_modes is fine, but the teardown
currently creates a throwaway instance of each _TC_DESCRIPTOR_CLASSES entry just
to read its name. Update the remove-mode path to use the descriptor’s
class-level name source instead of instantiating the class, keeping the same
register_mode/remove_mode flow in tc_modes and preserving the explicit
_ModeRegistryCls._all_registries cleanup.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 6b6863e9-c03e-4b81-89ff-141f992047e0
📒 Files selected for processing (1)
tests/unit/torch/opt/test_conversion.py
What does this PR do?
Type of change: new tests
Part of the unit-coverage initiative in #1902. Direct unit suite (56 tests) for modelopt/torch/opt/conversion.py — the mode-framework core every feature (quantize/prune/distill/nas) rides on. Synthetic mode descriptors live in a throwaway registry with complete teardown (including explicit removal from _ModeRegistryCls._all_registries, whose strong reference makes del unreachable — verified zero global residue and clean combined-process runs with test_chaining/test_mode_registry in both orders).
Coverage: manager construction/is_converted/remove_state; add_mode state-dict structure, ordering, by-reference metadata (load-bearing for the update hooks), next_modes/next_prohibited_modes enforcement with failed-adds-not-recorded, export-stack bookkeeping; load_state_dict replay incl. minor-vs-patch version warning rules, deepcopy isolation, unknown-mode/corrupt-config errors; transfer_state_dict; apply_mode (configs, kwargs broadcast+validation, explicit registry, update_for_new_mode write-back); modelopt_state (save_mode_in_state filtering, update_for_save per call); file and BytesIO save/restore round-trips incl. a real quantize mode.
Deliberate non-overlap map with test_chaining.py / test_load_modelopt_state.py / test_mode_registry.py is documented in the suite; the file-based mto.save()/mto.restore() path had no coverage anywhere.
Three genuine defects documented with NOTE tests (happy to send fixes as follow-ups):
(Reviewing also surfaced that tests/unit/torch/opt/test_mode_registry.py's final assertion compares a string against registry objects and is vacuously true — flagged for a follow-up test fix.)
Usage
N/A — tests only.
Testing
56 tests, ~0.4s, hermetic. Adversarial review: 14 expectations independently re-derived, 5/5 seeded mutations killed (each by exactly the tests naming that behavior), no vacuous tests, Windows/Linux-safe.
Before your PR is "Ready for review"
Additional Information
Issue: #1902
Summary by CodeRabbit