test: direct unit suites for opt Hparam and BaseSearcher (78 tests) - #1913
test: direct unit suites for opt Hparam and BaseSearcher (78 tests)#1913arham766 wants to merge 1 commit into
Conversation
Hermetic CPU-only tests for the two search-framework primitives; no direct suites existed (test_dynamic.py uses Hparam only as an opaque payload; nas/autoquant tests cover subclasses). Adversarially reviewed and verified to kill seeded mutations of both modules (importance normalization, __iand__ intersection, active_slice truncation, training-mode restore, checkpoint strict default, path resolution). Part of the coverage initiative in NVIDIA#1902. Signed-off-by: arham766 <arhamislam766@yahoo.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughAdds two new unit test modules: one exhaustively testing the ChangesHparam Unit Tests
BaseSearcher Unit Tests
Estimated code review effort: 2 (Simple) | ~15 minutes Related Issues: None found. Related PRs: None found. Suggested labels: tests Suggested reviewers: None specified. 🚥 Pre-merge checks | ✅ 6✅ Passed checks (6 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
### What does this PR do?
Type of change: Bug fix
`Hparam._force_configurable()` restored `_is_configurable` only after a
normal context-manager exit. If code inside the context raised, the
restoration statement was skipped and a previously non-configurable
`Hparam` remained permanently configurable.
Wrap the `yield` in `try`/`finally` so the original state is restored on
both normal and exceptional exits. Add a regression test that raises
inside the context and verifies the temporary configurable state does
not leak.
### Usage
No call-site changes are required. Existing users of the context manager
retain the same behavior, including when an operation inside the context
fails:
```python
with hp._force_configurable():
hp.choices = new_choices
```
### Testing
- Verified the new regression test fails against the pre-fix
implementation because `hp.is_configurable` remains `True`.
- `.venv/bin/python -m pytest tests/unit/torch/opt/test_dynamic.py -q`
(`3 passed`)
- `.venv/bin/python -m pytest tests/unit/torch/opt -q` (`66 passed, 2
skipped`)
- Relevant pre-commit hooks passed: file/merge/line-ending checks, Ruff
check and format, mypy, license insertion check, Bandit.
- `git diff --check`
### Before your PR is "*Ready for review*"
Make sure you read and follow [Contributor
guidelines](https://github.com/NVIDIA/Model-Optimizer/blob/main/CONTRIBUTING.md)
and your commits are signed (`git commit -s -S`).
Make sure you read and follow the [Security Best
Practices](https://github.com/NVIDIA/Model-Optimizer/blob/main/SECURITY.md#security-coding-practices-for-contributors)
(e.g. avoiding hardcoded `trust_remote_code=True`, `torch.load(...,
weights_only=False)`, `pickle`, etc.).
- Is this change backward compatible?: ✅
- If you copied code from any other sources or added a new PIP
dependency, did you follow guidance in `CONTRIBUTING.md`: N/A
- Did you write any new necessary tests?: ✅
- Did you update
[Changelog](https://github.com/NVIDIA/Model-Optimizer/blob/main/CHANGELOG.rst)?:
N/A — this is a small, backward-compatible correctness fix rather than a
critical bug fix.
- Did you get Claude approval on this PR?: N/A — not available to an
external contributor before submission.
### Additional Information
Related prior work: #1913 was closed without merging and only tested the
normal-exit path. This PR adds exception-path coverage together with the
production fix.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Improved configuration handling so temporary settings are reliably
restored even when an error occurs.
* Exceptions raised during configuration operations continue to be
reported correctly.
* **Tests**
* Added coverage for configuration restoration after a failed operation.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Signed-off-by: taking-lying-flat <1615405@qq.com>
What does this PR do?
Type of change: new tests
Part of the unit-coverage initiative in #1902. Direct suites for the two search-framework primitives (no direct coverage existed; the duplication audit against test_dynamic.py, tests/unit/torch/nas, and test_autoquant.py is in the review notes):
Usage
N/A — tests only.
Testing
78 tests, <1s, hermetic (CPU-only, tmp_path, deterministic). Adversarially reviewed: 12 expectations independently re-derived from source, 6/6 seeded mutations killed, no vacuous tests, Windows/Linux path-agnostic. Full tests/unit/torch/opt dir passes alongside (excluding the pre-existing test_lr_config.py env failures).
Before your PR is "Ready for review"
Additional Information
Issue: #1902
Summary by CodeRabbit
Hparam, including defaults, configurability, active/choices updates, ordering, importance handling, merging, and pickle roundtrips.BaseSearchertests covering search flow, checkpoint save/load, resume behavior, scoring, and forward-loop/data-loader execution rules.