ONNX modifications with no op exit early#246
Conversation
📝 WalkthroughWalkthroughThis PR optimizes ONNX input normalization processing by introducing a centralized decision method that determines whether inputs require normalization changes, enabling the ONNX attachment function to short-circuit expensive model loading and rewriting operations. It also improves DLC metadata extraction performance through conditional caching and includes validation tests. ChangesONNX Input Normalization Optimization
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
tests/test_utils/test_config.py (1)
843-864: ⚡ Quick winTest validates the short-circuit behavior correctly.
The test confirms that when no ONNX input modifications are required (encoding set to NONE with no mean/scale overrides), the function returns the original model path and does not create a modified file.
Optional: Broaden test coverage for other neutral-value scenarios
Consider adding a test case for other "no modification needed" scenarios, such as:
encoding.from == encoding.to(e.g., both BGR)mean_values = None(or explicitly[0, 0, 0])scale_values = None(or explicitly[1, 1, 1])def test_modified_onnx_noop_with_matching_encoding(): model_path = DATA_DIR / "dummy_model.onnx" modified_path = DATA_DIR / "dummy_model_noop_modified2.onnx" config = Config.get_config( None, { "input_model": str(model_path), "encoding.from": "BGR", "encoding.to": "BGR", "mean_values": None, "scale_values": None, }, ) input_configs = { inp.name: inp for inp in next(iter(config.stages.values())).inputs } result = onnx_attach_normalization_to_inputs( model_path, modified_path, input_configs, ) assert result == model_path assert not modified_path.exists()This would validate the short-circuit works for all neutral-value combinations, not just the NONE encoding case.
🤖 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/test_utils/test_config.py` around lines 843 - 864, Add one or more tests that assert the same short-circuit behavior for other neutral-value scenarios besides encoding "NONE": create tests (e.g., test_modified_onnx_noop_with_matching_encoding) that call Config.get_config with encoding.from and encoding.to set to the same value (like "BGR"), and with mean_values set to None or [0,0,0] and scale_values set to None or [1,1,1]; then call onnx_attach_normalization_to_inputs with the generated input_configs and assert it returns the original model path and that the modified_path file does not exist, reusing DATA_DIR, onnx_attach_normalization_to_inputs, Config.get_config and the existing test pattern to keep coverage consistent.
🤖 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.
Inline comments:
In `@modelconverter/utils/metadata.py`:
- Around line 53-55: The call to subprocess_run invoking "snpe-dlc-info"
(subprocess_run(["snpe-dlc-info", "-i", path, "-s", csv_path], silent=True)) can
hang indefinitely; add a timeout argument (e.g., timeout=some_seconds) to the
subprocess_run call and update the surrounding logic to catch the timeout
exception (e.g., subprocess.TimeoutExpired) so you can log/handle the failure
and proceed or fail gracefully instead of blocking archive generation; modify
the invocation in metadata extraction where subprocess_run is called and ensure
any cleanup or error logging uses the same path/csv_path context.
---
Nitpick comments:
In `@tests/test_utils/test_config.py`:
- Around line 843-864: Add one or more tests that assert the same short-circuit
behavior for other neutral-value scenarios besides encoding "NONE": create tests
(e.g., test_modified_onnx_noop_with_matching_encoding) that call
Config.get_config with encoding.from and encoding.to set to the same value (like
"BGR"), and with mean_values set to None or [0,0,0] and scale_values set to None
or [1,1,1]; then call onnx_attach_normalization_to_inputs with the generated
input_configs and assert it returns the original model path and that the
modified_path file does not exist, reusing DATA_DIR,
onnx_attach_normalization_to_inputs, Config.get_config and the existing test
pattern to keep coverage consistent.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 27517f98-2c72-4baa-90ab-4e77d74ea154
📒 Files selected for processing (4)
modelconverter/utils/config.pymodelconverter/utils/metadata.pymodelconverter/utils/onnx_tools.pytests/test_utils/test_config.py
Purpose
<model name>.info.csvfile fromsnpe-dlc-infoand is not older than the dlc file itself then we use it. This way we save some time instead of always re-generating itSpecification
None / not applicable
Dependencies & Potential Impact
None / not applicable
Deployment Plan
None / not applicable
Testing & Validation
None / not applicable
AI Usage
Assisted-by: AGENT_NAME:MODEL_VERSION [TOOL1] [TOOL2]
Submitted code was reviewed by a human: YES/NO
The author is taking the responsibility for the contribution: YES/NO
Summary by CodeRabbit
Refactor
Tests