Skip to content

feat: Add config serialization and loading support#36

Merged
markurtz merged 10 commits intomainfrom
feat/config-serialization
Jun 30, 2025
Merged

feat: Add config serialization and loading support#36
markurtz merged 10 commits intomainfrom
feat/config-serialization

Conversation

@rahul-tuli
Copy link
Copy Markdown
Collaborator

PR 1: Add Config Serialization and Loading Support

Summary

Implements from_pretrained, to_dict, and from_dict methods for SpeculatorModelConfig to enable loading and saving configurations from the HuggingFace Hub and local paths. This is a prerequisite for model implementations that need these serialization capabilities.

Changes

Core Implementation

  • SpeculatorModelConfig.from_pretrained(): Load configs from HF Hub or local paths with automatic subclass detection
  • SpeculatorModelConfig.from_dict(): Polymorphic instantiation from dictionaries using registry pattern
  • EagleSpeculatorConfig.to_dict(): Proper serialization of all config attributes
  • EagleSpeculatorConfig.from_dict(): Handle nested config reconstruction (transformer_layer_config, etc.)

Technical Details

Registry Pattern

# Base class uses registry for polymorphic instantiation
model_type = config_dict.get("speculators_model_type")
config_class = cls.registry.get(model_type)
return config_class(**config_dict)

Nested Config Handling

  • Automatically converts dict representations to proper config objects:
    • transformer_layer_config → LlamaConfig
    • speculators_config → SpeculatorsConfig
    • token_proposals → TokenProposalConfig

Comment Improvements

  • Updated comments to explain "why" rather than "what"
  • Removed redundant documentation

Testing

  • test_config_loading.py: Comprehensive unit tests for serialization roundtrips
  • Test Coverage:
    • to_dict/from_dict roundtrip preservation
    • from_pretrained with architecture inference
    • Nested config handling
    • Extra kwargs override behavior

API Changes

  • No breaking changes - only adds new functionality
  • Maintains compatibility with HuggingFace PretrainedConfig

Example Usage

# Load from HuggingFace Hub
config = SpeculatorModelConfig.from_pretrained("org/model-name")

# Load from local path
config = SpeculatorModelConfig.from_pretrained("/path/to/model")

# Serialize and deserialize
config_dict = config.to_dict()
new_config = SpeculatorModelConfig.from_dict(config_dict)

Review Notes

  • The registry pattern allows future speculator types to be added without modifying base class
  • All changes maintain backward compatibility
  • Test coverage includes edge cases like missing model_type fields
@nm-red-hat-upstream-automation-bot
Copy link
Copy Markdown

📦 Build Artifacts Available
The build artifacts (.whl and .tar.gz) have been successfully generated and are available for download: https://github.com/neuralmagic/speculators/actions/runs/15896923777/artifacts/3408415086.
They will be retained for up to 30 days.

@rahul-tuli rahul-tuli marked this pull request as ready for review June 26, 2025 09:03
@markurtz markurtz requested a review from Copilot June 27, 2025 16:15

This comment was marked as outdated.

@markurtz markurtz force-pushed the eagle-speculator-config branch from a56e3c4 to 6771fd1 Compare June 27, 2025 19:29
Base automatically changed from eagle-speculator-config to main June 27, 2025 19:36
rahul-tuli and others added 8 commits June 27, 2025 19:38
serialization support

  - Add LlamaEagleSpeculator model class
  - Implement model serialization/deserialization
  - Add torch as a project dependency
  - Include unit and integration tests
  - Fixes for serialization handling

  This introduces the core LlamaEagleSpeculator
  functionality with full serialization support
- Implement from_pretrained() for loading configs from HF Hub or local paths
- Add from_dict() with polymorphic instantiation using registry pattern
- Implement to_dict() for EagleSpeculatorConfig with proper serialization
- Handle nested config reconstruction (transformer_layer_config, etc.)
- Update comments to explain "why" rather than "what"
- Add comprehensive unit tests for serialization roundtrips
- Remove incompatible legacy tests expecting NotImplementedError

Co-Authored-By: rtuli@redhat.com
@markurtz markurtz force-pushed the feat/config-serialization branch from e1aa106 to e3f9e7f Compare June 27, 2025 19:50
@nm-red-hat-upstream-automation-bot
Copy link
Copy Markdown

📦 Build Artifacts Available
The build artifacts (.whl and .tar.gz) have been successfully generated and are available for download: https://github.com/neuralmagic/speculators/actions/runs/15934725470/artifacts/3422000105.
They will be retained for up to 30 days.

Copy link
Copy Markdown
Collaborator

@markurtz markurtz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting here, working through simplifying this diff

@nm-red-hat-upstream-automation-bot
Copy link
Copy Markdown

📦 Build Artifacts Available
The build artifacts (.whl and .tar.gz) have been successfully generated and are available for download: https://github.com/neuralmagic/speculators/actions/runs/15974113330/artifacts/3432197377.
They will be retained for up to 30 days.

@nm-red-hat-upstream-automation-bot
Copy link
Copy Markdown

📦 Build Artifacts Available
The build artifacts (.whl and .tar.gz) have been successfully generated and are available for download: https://github.com/neuralmagic/speculators/actions/runs/15974267522/artifacts/3432251988.
They will be retained for up to 30 days.

Copy link
Copy Markdown
Collaborator

@markurtz markurtz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing my request changes block now that it's been reworked to utilize Pydantic builtins and reduce the complexity of the logic

@markurtz markurtz requested a review from Copilot June 30, 2025 13:32
Copy link
Copy Markdown
Contributor

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 PR adds configuration serialization and deserialization support for speculator models, including methods for loading from local paths or the HuggingFace Hub as well as proper handling of nested configurations. Key changes include:

  • Implementing from_pretrained, from_dict, and to_dict methods with registry support.
  • Adding new field serialization and validation logic for EagleSpeculatorConfig.
  • Updating and expanding unit and integration tests for configuration marshalling.

Reviewed Changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tox.ini Updated Python environment list to drop py38.
tests/unit/test_config.py Refactored tests with updated markers and added additional marshalling tests.
tests/unit/models/test_eagle.py Expanded coverage for EagleSpeculatorConfig including backwards compatibility.
tests/integration/test_config.py Replaced a temporary failure test for from_pretrained with a stub.
src/speculators/models/eagle.py Added field serialization and validation for transformer_layer_config.
src/speculators/config.py Revised configuration loading (from_pretrained, from_dict) and init logic.
pyproject.toml Updated lint rule codes to match new guidelines.
Comments suppressed due to low confidence (1)

tests/integration/test_config.py:44

  • This placeholder test for from_pretrained does not currently validate actual loading behavior. Once the functionality is fully implemented, please add comprehensive tests to ensure proper integration.
    assert True
Copy link
Copy Markdown
Collaborator

@brian-dellabetta brian-dellabetta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit to remove extraneous test, otherwise LGTM

@markurtz markurtz merged commit 3c05f12 into main Jun 30, 2025
10 of 11 checks passed
@markurtz markurtz deleted the feat/config-serialization branch June 30, 2025 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants