Skip to content

Conversation

@speediedan
Copy link
Owner

@speediedan speediedan commented Jan 9, 2026

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

  • Implemented entry-point discovery for strategy adapters:
    • New _discover_strategy_adapters() in src/finetuning_scheduler/fts_supporters.py that scans finetuning_scheduler.strategy_adapters entry points and extends the runtime STRATEGY_ADAPTERS mapping.
    • Import semantics accept both module.Class and module:Class forms and prefer short-name aliases when registered.
  • Added adapter lifecycle hook:
    • before_restore_model() hook on StrategyAdapter (in src/finetuning_scheduler/strategy_adapters/base.py) allowing adapters to transform/translate a checkpoint before restore.
    • Integrated invocation into FinetuningScheduler.restore flow: self.strategy_adapter.before_restore_model(loaded_ckpt) is called (best-effort, exceptions caught and logged) and the transformed checkpoint is passed to restore_model().
  • Configuration/API updates:
    • Accept custom_strategy_adapters (plural) mapping on the FTS constructor and updated import helper _import_strategy_adapter to resolve aliases and fully-qualified names.
  • Docs & tests:
    • New docs: docs/source/plugins/strategy_adapter_entry_points.rst explaining custom_strategy_adapters, formats, and examples.
    • Tests added/updated in tests/test_strategy_adapter_discovery.py to cover successful discovery and error branches (ep.load failure, fallback import failure, invalid formats).
    • Test helpers: tests/helpers/fake_adapter.py and tests/helpers/expected_warns.py updates. Tests use MagicMock for EntryPoint-style objects to accommodate Python 3.12 immutability and simulate ep.load() side effects.
  • Misc: minor housekeeping across fts_supporters.py, fts.py, strategy adapters, and docs indices.
  • Infrastructure updates (torch-nightly.txt → torch-pre.txt for flexible prerelease channel support)

Files touched (non-exhaustive)

  • src/finetuning_scheduler/fts_supporters.py
  • src/finetuning_scheduler/strategy_adapters/base.py
  • src/finetuning_scheduler/fts.py
  • tests/test_strategy_adapter_discovery.py
  • tests/test_finetuning_scheduler_callback.py
  • tests/helpers/fake_adapter.py
  • docs/source/plugins/strategy_adapter_entry_points.rst

Motivation

  • Allow third-party or user-installed strategy adapters to be discovered and registered via standard Python packaging entry points so users can provide adapters without modifying library source.
  • Enable adapters to perform strategy-specific checkpoint transformations (e.g., key expansion, mapping for TransformerBridge) before the library restores a model.
  • Improve robustness and documentation for adapter import semantics and error handling.

Migration / Backwards compatibility

  • custom_strategy_adapters is now accepted in plural form and the import helper accepts both module.Class and module: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

  • New tests verify discovery behavior, supported import formats, and error/fallback branches for EntryPoint.load() and imports.
  • Tests include a fake adapter helper and use MagicMock to simulate EntryPoint objects and ep.load() side effects for Python 3.12 compatibility.

Documentation

  • Added docs/source/plugins/strategy_adapter_entry_points.rst describing the entry-point format, examples, and how to reference adapters via alias or fully-qualified name.
  • Updated fts docstrings to reference the new docs and usage patterns.

…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).
…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).
@speediedan speediedan marked this pull request as ready for review January 10, 2026 01:22
Copilot AI review requested due to automatic review settings January 10, 2026 01:22
Copy link

Copilot AI left a 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_adapters group)
  • New before_restore_model() lifecycle hook for checkpoint transformation
  • Refactored parameter naming (singular custom_strategy_adapter → plural custom_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 qualname but the actual parameter is named strategy_key. The Args section should document strategy_key and adapter_map instead.
        """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.

@speediedan speediedan merged commit 8831869 into main Jan 10, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants