-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-57521][ML][CONNECT] Exclude parent from Model.estimatedSize to fix overcounting in ML cache #56584
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
Open
mkincaid
wants to merge
2
commits into
apache:master
Choose a base branch
from
mkincaid:fix/ml-cache-size-estimator-parent
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+87
−1
Open
[SPARK-57521][ML][CONNECT] Exclude parent from Model.estimatedSize to fix overcounting in ML cache #56584
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.ml | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.ml.feature.{MinMaxScaler, StringIndexer} | ||
| import org.apache.spark.ml.linalg.Vectors | ||
| import org.apache.spark.mllib.util.MLlibTestSparkContext | ||
|
|
||
| class ModelSuite extends SparkFunSuite with MLlibTestSparkContext { | ||
|
|
||
| test("SPARK-57521: estimatedSize should not include parent's reachable object graph") { | ||
| val df = spark.createDataFrame(Seq( | ||
| Tuple1("a"), Tuple1("b"), Tuple1("c") | ||
| )).toDF("label") | ||
|
|
||
| val model = new StringIndexer() | ||
| .setInputCol("label").setOutputCol("idx") | ||
| .fit(df) | ||
|
|
||
| assert(model.hasParent, "model should have parent after fit()") | ||
| val size = model.estimatedSize | ||
|
|
||
| // Model data is 3 string labels + overhead, well under 50KB. | ||
| // Without the fix, SizeEstimator traverses model.parent -> estimator -> | ||
| // SparkSession, counting hundreds of KB (local) to hundreds of MB | ||
| // (production cluster) of shared session state per model. | ||
| assert(size < 50 * 1024, | ||
| s"estimatedSize ($size bytes) should reflect model data only, " + | ||
| s"not the parent estimator's reachable object graph (SparkSession)") | ||
|
|
||
| // Parent must be preserved - it is only excluded during estimation | ||
| assert(model.hasParent, "parent should be preserved after estimatedSize call") | ||
| } | ||
|
|
||
| test("SPARK-57521: estimatedSize excludes parent for multiple estimator types") { | ||
| // The issue affects all estimators that execute DataFrame operations during fit(). | ||
| // Test with two different estimator types to verify the fix is in Model, not | ||
| // specific to any one estimator. | ||
| val df = spark.createDataFrame(Seq( | ||
| Tuple1(Vectors.dense(1.0, 2.0)), | ||
| Tuple1(Vectors.dense(3.0, 4.0)) | ||
| )).toDF("features") | ||
|
|
||
| val model = new MinMaxScaler() | ||
| .setInputCol("features").setOutputCol("scaled") | ||
| .fit(df) | ||
|
|
||
| assert(model.hasParent) | ||
| val size = model.estimatedSize | ||
|
|
||
| assert(size < 50 * 1024, | ||
| s"MinMaxScalerModel estimatedSize ($size bytes) should not include " + | ||
| s"parent's reachable object graph") | ||
| assert(model.hasParent, "parent should be preserved after estimatedSize call") | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please investigate and address a possible thread-safety regression here. There is a side-effecting mutation of shared state in a base-class method. Two concurrent estimatedSize calls on the same model can interleave so both save then both restore, with the second finally clobbering parent to null permanently; a concurrent reader (hasParent, transform, save) can also observe parent == null during the window.
In the current path this is masked because estimatedSize is invoked inside MLCache.register (which is synchronized) on a freshly-fit, not-yet-shared model, so it is not an active production bug today, but estimatedSize is private[spark] and the previous implementation was side-effect free, so the new contract is strictly weaker.
Please consider a non-mutating approach rather than mutating shared instance state.
Uh oh!
There was an error while loading. Please reload this page.
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.
Hi @uros-b, thanks for the quick review and input. I pushed a change that adds
synchronizedso that we wouldn't have two concurrentestimatedSizecalls from here. However, I'm realizing this doesn't address the second part of your comment (a concurrent reader from elsewhere would still observe parent == null).As I looked into this further, the truly non-mutating approaches I came up with were:
copyof theModelwith emptyparent, then size that. But this depends on the implementation of thecopymethod which is model-specific (so not sure if it can be relied on to faithfully copy everything we care about sizing).ModelobjectCloneable, thenclone(), clearparent, and size. But changing an interface ofModelitself seems less conservative and beyond the scope I was intending for this original fix.parentis@transientit would be gone in the serialized copy. This seems conceptually appealing (it seems like, in principle, the data the model keeps and serializes is the state we care about sizing) but not sure if it might be expensive for large models and, like thecopyoption, should I worry about the possibility something relevant doesn’t survive the round trip.SizeEstimatoritself should skip walking throughSparkSessionobjects (the same way as there are existing exclusions there forClassLoaderandscala.reflect). This also seems less conservative since other users ofSizeEstimatormight not want the behavior to change.Or I may be missing something easier/cleaner. It is probably pretty obvious that I'm new to this code base, so I want to be thoughtful about design and get more input before proceeding. Appreciate your patience with me and looking forward to your thoughts :)