Skip to content

fix(automl): preserve SVC random state during sklearn cloning - #1679

Open
Aditya Kumar (adityakumar027) wants to merge 2 commits into
microsoft:mainfrom
adityakumar027:fix-svc-clone-random-state
Open

Aditya Kumar (adityakumar027) wants to merge 2 commits into
microsoft:mainfrom
adityakumar027:fix-svc-clone-random-state

Conversation

@adityakumar027

Copy link
Copy Markdown

Why are these changes needed?

Fixes #1678.

SVCEstimator overwrites random_state every time its constructor runs. That loses an explicitly configured state and can break sklearn cloning when cloning a NumPy scalar or RandomState object. It also leaves the random_seed alias in the parameters passed to LinearSVC.

Use the same normalization as the neighboring estimators: consume random_seed, keep an existing random_state, and use the historical default only when neither was supplied.

Tests

  • Added eight regression cases covering None, integer, NumPy integer and RandomState values, the default and random_seed alias, explicit random_state precedence, and a small StackingClassifier fit.
  • Before the fix: six failures, two passes. After: all eight pass with scikit-learn 1.6.1.
  • Black 23.3.0 and Ruff 0.0.261 pass on both changed files.
  • A broader run including test_model.py was killed while running its large existing model test, so I am not claiming that suite passed.

Prepared with AI assistance.

Checks

  • Full pre-commit run (Black and Ruff were run directly)
  • No documentation changes needed
  • Added regression tests
  • All CI checks passed (awaiting CI)

Signed-off-by: Aditya Kumar <adi.workspace76865@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The integration test still clones and fits with the default random state rather than 7, so it does not verify that stacking preserves the supplied NumPy integer state.

1 open issue
What changed in this PR

Fixes SVCEstimator parameter normalization to preserve configured random states during scikit-learn cloning, supporting FLAML’s ensemble and stacking workflows.

Changes:

  • Preserves explicit random_state values, consumes the random_seed alias, and retains the historical default.
  • Adds eight regression cases covering random-state types, alias precedence, cloning, and stacking.
File Description
test/​automl/​test_svc_clone.py Adds cloning, seed normalization, and stacking tests.
flaml/​automl/​model.py Updates SVC initialization to preserve random state and remove the seed alias.

Review effort: Balanced

Comment thread test/automl/test_svc_clone.py Resolved

@thinkall Li Jiang (thinkall) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The constructor correction behaves correctly in focused validation: eight tests passed on Python 3.10/NumPy 1.26 and eight on Python 3.13/NumPy 2.3. The same tests detect six expected failures per stack on unmodified main. The whole-PR Low review found two coverage gaps that should be addressed before approval:

  1. The stacking fixture uses SVCEstimator().set_params(random_state=7), but the inherited setter does not update the parameters returned by FLAML's get_params(). Probes confirm that it fits with 10242048 on both current and main, so the prediction-shape assertion does not exercise this regression. Use SVCEstimator(random_state=7) and assert stacking.named_estimators_["svc"].model.random_state == 7 after fitting. This corroborates the existing Copilot thread; no duplicate inline issue is being opened.

  2. The RandomState case compares only get_state()[1] and reads the mutable expected generator after construction/cloning. Snapshot the full state before construction, preferably after advancing the generator with a normal draw, and compare the complete state with np.testing.assert_equal. Current full-state preservation works on both stacks, but a position-only change passes the existing key-array assertion. This is a test-oracle gap, not an observed production RNG regression.

Approval remains withheld while these findings and the independent peer-review/CI gates remain unresolved.

Posted by thinkall-agent-auto-reviewer

Signed-off-by: Aditya Kumar <adi.workspace76865@gmail.com>
@adityakumar027

Copy link
Copy Markdown
Author

Fixed both coverage gaps: stacking now constructs the SVC with a NumPy integer state and asserts the fitted model keeps 7. The RandomState test takes a full snapshot after a normal draw, then checks both the cloned and original generators against it. All eight tests pass; unmodified main now fails seven, including the stacking case. Black and Ruff pass.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

There are no active review issues for the SVC random-state preservation change.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@thinkall Li Jiang (thinkall) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Whole-PR review at d85e37accd51e0847c5a5e2e1100c2c3fc9c9030: both previous test-coverage findings are fixed. The RandomState oracle captures the full advanced state before construction, and the stacking test constructs the explicit seed and checks the fitted clone.

The Low skill review is clean. Targeted suites passed 16 tests across the two native stacks; the same new tests on unmodified main had 14 expected failures and 2 passes. All-files pre-commit passed. The current-head Balanced peer recommends approval, all review threads are resolved, applicable checks pass, and all current-head workflow runs completed successfully. The skipped docs gh-release job is explicitly disabled for pull requests by its verified workflow condition; no unsuccessful or pending workflow was ignored.

Approved. Review artifacts are committed only in detached local evidence worktrees and were not pushed to this PR.

Posted by thinkall-agent-auto-reviewer

This branch has not been deployed

No deployments
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.

[Bug]: SVCEstimator fails sklearn cloning during FLAML ensemble/stacking

3 participants