-
Notifications
You must be signed in to change notification settings - Fork 7
Entry Point Plugin Discovery #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…ore hook
- Add entrypoint discovery to register strategy adapters at runtime:
- Implement `_discover_strategy_adapters()` in `src/finetuning_scheduler/fts_supporters.py`
that reads `finetuning_scheduler.strategy_adapters` entry points and extends runtime mapping `STRATEGY_ADAPTERS`.
- Support both `module.Class` and `module:Class` forms, and prefer discovered short-name aliases.
- Add `before_restore_model()` hook to `StrategyAdapter` base (now in `src/finetuning_scheduler/strategy_adapters/base.py`):
- Adapter authors can override this hook to transform/translate `checkpoint` before model restore (e.g. key expansion).
- Integrate adapter hook invocation into FTS restore flow:
- In `src/finetuning_scheduler/fts.py`, call `self.strategy_adapter.before_restore_model(loaded_ckpt)` after
datamodule restore and reassign the transformed checkpoint before calling `restore_model()`.
- Make invocation best-effort (catch exceptions and continue).
- Update configuration mapping and import behavior:
- Accept `custom_strategy_adapters` (plural) mapping on FTS constructor and in `fts_supporters._import_strategy_adapter`.
- If `qualname` is a short alias registered in `STRATEGY_ADAPTERS`, use that adapter class directly.
- Accept both `module:Class` and `module.Class` import specifiers.
- Tests and helpers:
- Add plugin adapter discovery tests in `tests/test_strategy_adapter_discovery.py`.
- Add a test helper `tests/helpers/fake_adapter.py` to simulate entrypoint discovery.
- Update `tests/test_finetuning_scheduler_callback.py` to use the `custom_strategy_adapters` field
in the `MockStrategy` scenario.
Files changed (summary):
- src/finetuning_scheduler/fts_supporters.py (entrypoint discovery registration, `_discover_strategy_adapters`, `STRATEGY_ADAPTERS` usage)
- src/finetuning_scheduler/strategy_adapters/base.py (new `before_restore_model()` hook)
- src/finetuning_scheduler/fts.py (call adapter hook before restore, rename/accept `custom_strategy_adapters`)
- tests/helpers/fake_adapter.py (fake adapter for test discovery)
- tests/test_strategy_adapter_discovery.py (discovery & import tests)
- tests/test_finetuning_scheduler_callback.py (update to plural adapter property)
Why:
- Allow users to install adapters via standard Python entry points and to associate plugin adapters to strategies without patching source.
- Enable strategy-specific checkpoint transformations (e.g., TransformerLens Bridge key expansion) during restore flows.
- Improve import semantics and documentation to support both short alias and fully-qualified adapter references.
Next:
- Add more adapter discovery tests for different packaging forms and edge cases.
- Extend adapter tests to cover `before_restore_model()` transformations (e.g. a mock adapter that expands keys).
…trategy adapter dev
…covery errors with tests - Add new plugin docs: `docs/source/plugins/strategy_adapter_entry_points.rst` describing `custom_strategy_adapters` mapping formats and examples. - Update `custom_strategy_adapters` docstring in `src/finetuning_scheduler/fts.py` with usage details and cross-reference to plugin docs. - Add and improve tests in `tests/test_strategy_adapter_discovery.py` to cover error branches (`ep.load()` failure, fallback import failure, invalid formats). - Use `MagicMock` for `EntryPoint`-style objects in tests to handle Python 3.12 immutability and simulate `ep.load()` side effects. - Minor housekeeping: update docs indices and touched modules (`src/finetuning_scheduler/fts_supporters.py`, `src/finetuning_scheduler/strategy_adapters/*`, `tests/helpers/expected_warns.py`) updated during testing and documentation work. - Validation: affected tests and pre-commit hooks run locally for the modified files; test suite passes for the targeted files.
- Document strategy adapter entry points and add docs/usage guidance. - Adjust CI/dependency matrix: - Remove `requirements/ci/torch-nightly.txt`. - Add `requirements/ci/torch-pre.txt`. - Update `requirements/ci/requirements.txt` and `requirements/ci/requirements-oldest.txt`. - Update `requirements/ci/torch_override.txt`. - Modify lock script and add `requirements/utils/sync_metadata.py`. - Update build/test infrastructure and scripts: - Update `scripts/build_fts_env.sh`, `scripts/gen_fts_coverage.sh`, and `scripts/infra_utils.sh`. - Update Docker base image `dockers/base-cuda/Dockerfile`. - Update GitHub CI action `.github/actions/install-ci-dependencies/action.yml`. - Core changes: - Update `src/finetuning_scheduler/dynamic_versioning/utils.py` (version/override handling). - Update `src/finetuning_scheduler/fts.py` (plugin discovery integration).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This pull request implements entry point plugin discovery for strategy adapters, enabling third-party packages to extend FTS with custom adapters. Key changes include:
Summary: Adds runtime discovery and import semantics for external StrategyAdapter plugins via Python entry points, a before_restore_model() adapter hook for checkpoint transformation, configuration updates (plural custom_strategy_adapters), and comprehensive documentation and tests.
Key Changes:
- Plugin discovery system via Python entry points (
finetuning_scheduler.strategy_adaptersgroup) - New
before_restore_model()lifecycle hook for checkpoint transformation - Refactored parameter naming (singular
custom_strategy_adapter→ pluralcustom_strategy_adapters) - Comprehensive test coverage for discovery, import formats, and error handling
- New documentation explaining plugin registration and usage
- Infrastructure updates (torch-nightly.txt → torch-pre.txt for flexible prerelease channel support)
Reviewed changes
Copilot reviewed 29 out of 30 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_strategy_adapter_discovery.py | New comprehensive test suite for plugin discovery and resolution |
| tests/test_finetuning_scheduler_callback.py | Tests for deprecation warning and adapter exception handling |
| tests/helpers/fake_adapter.py | Simple test fixture adapter class |
| src/finetuning_scheduler/fts_supporters.py | Core discovery function and adapter resolution logic |
| src/finetuning_scheduler/fts.py | Integration of discovery call and before_restore_model hook |
| src/finetuning_scheduler/strategy_adapters/base.py | New adapter lifecycle methods and delegation points |
| src/finetuning_scheduler/strategy_adapters/init.py | Plugin usage documentation in module docstring |
| docs/source/plugins/strategy_adapter_entry_points.rst | Complete plugin development guide |
| scripts/build_fts_env.sh, scripts/gen_fts_coverage.sh | Torch prerelease channel refactoring |
| requirements/ci/torch-pre.txt | New unified prerelease configuration (test/nightly) |
| pyproject.toml | Informational minimum version metadata section |
Comments suppressed due to low confidence (1)
src/finetuning_scheduler/fts_supporters.py:1237
- The docstring for this method mentions a parameter named
qualnamebut the actual parameter is namedstrategy_key. The Args section should documentstrategy_keyandadapter_mapinstead.
"""Resolve the custom strategy adapter specified in the ``custom_strategy_adapters`` configuration.
Args:
qualname (Dict): The user-provided custom strategy adapter fully qualified class name.
Raises:
MisconfigurationException: If the specified custom strategy adapter cannot be imported successfully.
MisconfigurationException: If the specified `strategy_key` does not match the current strategy.
Returns:
StrategyAdapter: The custom strategy adapter class to be instantiated.
…finetuning-scheduler into entry-point-plugin-discovery
Add runtime discovery and import semantics for external StrategyAdapter plugins via Python entry points, a
before_restore_model()adapter hook to transform checkpoints prior to restore, and comprehensive documentation & tests for discovery/error handling.Highlights / What changed
_discover_strategy_adapters()insrc/finetuning_scheduler/fts_supporters.pythat scansfinetuning_scheduler.strategy_adaptersentry points and extends the runtimeSTRATEGY_ADAPTERSmapping.module.Classandmodule:Classforms and prefer short-name aliases when registered.before_restore_model()hook onStrategyAdapter(insrc/finetuning_scheduler/strategy_adapters/base.py) allowing adapters to transform/translate a checkpoint before restore.FinetuningScheduler.restoreflow:self.strategy_adapter.before_restore_model(loaded_ckpt)is called (best-effort, exceptions caught and logged) and the transformed checkpoint is passed torestore_model().custom_strategy_adapters(plural) mapping on the FTS constructor and updated import helper_import_strategy_adapterto resolve aliases and fully-qualified names.docs/source/plugins/strategy_adapter_entry_points.rstexplainingcustom_strategy_adapters, formats, and examples.tests/test_strategy_adapter_discovery.pyto cover successful discovery and error branches (ep.load failure, fallback import failure, invalid formats).tests/helpers/fake_adapter.pyandtests/helpers/expected_warns.pyupdates. Tests useMagicMockfor EntryPoint-style objects to accommodate Python 3.12 immutability and simulateep.load()side effects.fts_supporters.py,fts.py, strategy adapters, and docs indices.Files touched (non-exhaustive)
Motivation
Migration / Backwards compatibility
custom_strategy_adaptersis now accepted in plural form and the import helper accepts bothmodule.Classandmodule:Class. Existing single-entry configurations should continue to work but authors are encouraged to adopt the plural mapping form for clarity.before_restore_model()is an optional hook; existing adapters that don't implement it will be unaffected.Testing
EntryPoint.load()and imports.MagicMockto simulateEntryPointobjects andep.load()side effects for Python 3.12 compatibility.Documentation
docs/source/plugins/strategy_adapter_entry_points.rstdescribing the entry-point format, examples, and how to reference adapters via alias or fully-qualified name.ftsdocstrings to reference the new docs and usage patterns.