feat: add components_to_export filter to MobiusBuilder pass#2456
feat: add components_to_export filter to MobiusBuilder pass#2456titaiwangms wants to merge 10 commits into
Conversation
Add an optional `components_to_export` parameter to MobiusBuilder that allows callers to select a subset of components from a multi-component model (e.g. only vision + embedding, skipping the decoder). When set, pkg.save() is called with a components predicate that filters out unneeded components before writing to disk, avoiding unnecessary I/O for large model files. When not set, all components are exported (backward-compatible default). Multi-component models filtered to one component still return a CompositeModelHandler (sub-directory layout) rather than a plain ONNXModelHandler (root layout), since the save layout is determined by the model's architecture, not the filter result. Add 5 new unit tests covering: subset filter, default exports-all, single-component-via-filter, unknown-component error, and config defaults. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace lambda assigned to variable (E731 linting violation) with a proper named inner function, matching Google Python style guide. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Use type_=list[str] instead of bare list for components_to_export - Use 'is not None' check to handle empty-list edge case correctly - Update docstring: remove inaccurate 'Has no effect on single-component models' and clarify that ValueError is raised for unknown names - Update _fake_pkg._save to respect the components filter kwarg so skipped component directories are not written to disk - Add disk assertions: verify skipped component dir is absent on disk - Wrap long AcceleratorSpec construction lines (Black 120-char limit) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
An empty list [] is always a programming mistake — it would silently produce a model with zero components. Explicitly check before the unknown-names validation so the error is clear and actionable. Also adds test_components_to_export_empty_list_raises to cover this case. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds an optional components_to_export configuration parameter to the MobiusBuilder ONNX pass so callers can export and return only a selected subset of components from multi-component Mobius model packages (keeping default behavior unchanged when unset).
Changes:
- Added
components_to_exportpass config and implemented filtering/validation logic inMobiusBuilder. - Updated the Mobius package save call to pass a component filter callback when configured.
- Expanded unit tests to cover subset export, unknown/empty component lists, and default-config presence.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
olive/passes/onnx/mobius_model_builder.py |
Introduces components_to_export config and applies it during package saving and handler construction. |
test/passes/onnx/test_mobius_model_builder.py |
Updates the fake package save behavior and adds tests validating the new filtering feature. |
…e_pkg filter - Wrap pkg.save(components=...) in try/except TypeError to gracefully fall back when an older mobius version does not support the 'components' kwarg - Log a warning when fallback is triggered so users know to upgrade mobius - Fix _fake_pkg._save in tests: single-component branch now respects the 'components' filter kwarg consistently with the multi-component branch - Update components_to_export docstring to explicitly mention empty list raises ValueError - Add test_pkg_save_typeerror_falls_back_gracefully to cover the fallback path Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
| except TypeError: | ||
| if components_filter is not None: | ||
| logger.warning( | ||
| "MobiusBuilder: installed mobius version does not support the 'components' filter kwarg; " | ||
| "all components will be saved. Upgrade mobius to enable selective export." | ||
| ) |
There was a problem hiding this comment.
Fixed: replaced the try/except TypeError with inspect.signature(pkg.save).parameters check. This detects kwarg support upfront from the function signature, avoiding any execution of save() before the error — so no orphaned dirs and no masked real TypeErrors. The check happens cleanly before the call.
| # Old mobius saved all; pass returns CompositeModelHandler with all keys (filter not enforced). | ||
| assert isinstance(result, CompositeModelHandler) | ||
| # A warning must have been logged about the missing kwarg support. | ||
| warning_messages = [str(call) for call in mock_logger.warning.call_args_list] | ||
| assert any("components" in msg and "mobius" in msg for msg in warning_messages) |
There was a problem hiding this comment.
Fixed: test renamed to test_pkg_save_old_api_no_components_kwarg_falls_back_gracefully. Now asserts: (1) result.model_component_names == ["vision_encoder", "embedding"] — the 2 requested components, not all 3; (2) all 3 component dirs are on disk (old API writes all, decoder is orphaned); (3) pkg.save was NOT called with components= kwarg. Added a separate regression test test_pkg_save_components_kwarg_detected_and_filter_applied for the modern-API path.
Review (synthesized from 4 reviewers: readability / code / critical / deep)Convergent findings flagged with [N reviewers]. Severity follows worst-case among reviewers. Major1.
Minor
PraiseThe architecturally-single-component (
|
…t check before build()
Addresses PR review comments:
1. Replace try/except TypeError with inspect.signature check:
- inspect.signature(pkg.save).parameters is checked before calling save()
- Avoids masking real TypeErrors and prevents orphan dirs that the
try/except approach could produce if save() partially executed
- Log a warning when falling back to old API (no filter applied)
2. Move empty-list validation before mobius.build():
- Fail fast with a clear ValueError before the expensive build step
- Unknown component check still runs after build (needs pkg.keys())
3. Fix test for old-API fallback path:
- Simulate old mobius API by setting pkg.save.__signature__ without
'components' parameter (inspect.signature detects this at call time)
- Assert all 3 component dirs are on disk (old API writes all)
- Assert result only has requested 2 components (package_keys controls handler)
- Assert pkg.save was NOT called with 'components=' kwarg
4. Add regression test test_pkg_save_components_kwarg_detected_and_filter_applied:
- Explicitly verifies that when pkg.save signature includes 'components',
the filter IS passed and only requested components land on disk
Also update _fake_pkg to set pkg.save.__signature__ = inspect.signature(_save)
so production code's inspect.signature check resolves 'components' in parameters.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ring - Add MobiusBuilder entry to docs/source/reference/pass.rst (autoconfigclass) - Add MobiusBuilder row to pass table in docs/source/reference/options.md - Update MobiusBuilder class docstring to document components_to_export parameter with example JSON config and ValueError behavior Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Summary
Add optional
components_to_exportparameter (list of component names) to theMobiusBuilderpass. When set (e.g.["vision", "embedding"]), only those components are exported and returned; the rest (e.g. decoder) are discarded. When not set (defaultNone), all components are exported as before (fully backward compatible).Motivation
When exporting large multi-component VLMs (e.g. Mistral-3 with decoder + vision_encoder + embedding), users often need only a subset of components — for example when the decoder is separately exported or already quantized. This filter avoids re-exporting and storing unnecessary components.
Changes
olive/passes/onnx/mobius_model_builder.py:components_to_export: list[str]PassConfigParam(defaultNone)is not Noneto handle empty-list edge case correctlyValueErrorif the list is empty (always a user mistake) or if any name is not found in the package components (fast-fail with clear message)pkg.save()with a named innercomponents_filterfunctioncomponents=kwarg, log a warning and callpkg.save()without it (graceful degradation for older mobius)ValueErrorbehavior for both empty list and unknown namestest/passes/onnx/test_mobius_model_builder.py:_fake_pkg._saveto respect thecomponentsfilter in the single-component branch (consistent with multi-component branch)Nonetest_pkg_save_typeerror_falls_back_gracefully: verifies that an old mobius API that raisesTypeErrorforcomponents=triggers the fallback path and logs a warningTesting
All 24 tests pass (1 pre-existing skip for
test_write_genai_config_requires_real_mobiuswhich requires real mobius package):