-
Notifications
You must be signed in to change notification settings - Fork 28.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-46812][SQL][PYTHON] Make mapInPandas / mapInArrow support Reso…
…urceProfile ### What changes were proposed in this pull request? Support stage-level scheduling for some PySpark DataFrame APIs (mapInPandas and mapInArrow). ### Why are the changes needed? The introduction of barrier mode in Spark, as seen in #40520, allows for the implementation of Spark ML cases (pure Python algorithms) using DataFrame APIs such as mapInPandas and mapInArrow, so it's necessary to enable stage-level scheduling for DataFrame APIs. ### Does this PR introduce _any_ user-facing change? Yes, This PR adds a new argument "profile" for mapInPandas and mapInArrow. ``` python def mapInPandas( self, func: "PandasMapIterFunction", schema: Union[StructType, str], barrier: bool = False, profile: Optional[ResourceProfile] = None, ) -> "DataFrame": def mapInArrow( self, func: "ArrowMapIterFunction", schema: Union[StructType, str], barrier: bool = False, profile: Optional[ResourceProfile] = None, ) -> "DataFrame": ``` How to use it? take mapInPandas as an example, ``` python from pyspark import TaskContext def func(iterator): tc = TaskContext.get() assert tc.cpus() == 3 for batch in iterator: yield batch df = spark.range(10) from pyspark.resource import TaskResourceRequests, ResourceProfileBuilder treqs = TaskResourceRequests().cpus(3) rp = ResourceProfileBuilder().require(treqs).build df.mapInPandas(func, "id long", False, rp).collect() ``` ### How was this patch tested? The newly added tests can pass, and some manual tests are needed for dynamic allocation on or off. ### Was this patch authored or co-authored using generative AI tooling? No Closes #44852 from wbo4958/df-rp. Lead-authored-by: Bobby Wang <bobwang@nvidia.com> Co-authored-by: Bobby Wang <wbo4958@gmail.com> Signed-off-by: Weichen Xu <weichen.xu@databricks.com>
- Loading branch information
1 parent
0818096
commit c4e4497
Showing
12 changed files
with
206 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,104 @@ | ||
# | ||
# 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. | ||
# | ||
import unittest | ||
|
||
from pyspark import SparkContext, TaskContext | ||
from pyspark.resource import TaskResourceRequests, ResourceProfileBuilder | ||
from pyspark.sql import SparkSession | ||
from pyspark.testing.sqlutils import ( | ||
have_pandas, | ||
have_pyarrow, | ||
pandas_requirement_message, | ||
pyarrow_requirement_message, | ||
) | ||
from pyspark.testing.utils import ReusedPySparkTestCase | ||
|
||
|
||
@unittest.skipIf( | ||
not have_pandas or not have_pyarrow, | ||
pandas_requirement_message or pyarrow_requirement_message, | ||
) | ||
class ResourceProfileTestsMixin(object): | ||
def test_map_in_arrow_without_profile(self): | ||
def func(iterator): | ||
tc = TaskContext.get() | ||
assert tc.cpus() == 1 | ||
for batch in iterator: | ||
yield batch | ||
|
||
df = self.spark.range(10) | ||
df.mapInArrow(func, "id long").collect() | ||
|
||
def test_map_in_arrow_with_profile(self): | ||
def func(iterator): | ||
tc = TaskContext.get() | ||
assert tc.cpus() == 3 | ||
for batch in iterator: | ||
yield batch | ||
|
||
df = self.spark.range(10) | ||
|
||
treqs = TaskResourceRequests().cpus(3) | ||
rp = ResourceProfileBuilder().require(treqs).build | ||
df.mapInArrow(func, "id long", False, rp).collect() | ||
|
||
def test_map_in_pandas_without_profile(self): | ||
def func(iterator): | ||
tc = TaskContext.get() | ||
assert tc.cpus() == 1 | ||
for batch in iterator: | ||
yield batch | ||
|
||
df = self.spark.range(10) | ||
df.mapInPandas(func, "id long").collect() | ||
|
||
def test_map_in_pandas_with_profile(self): | ||
def func(iterator): | ||
tc = TaskContext.get() | ||
assert tc.cpus() == 3 | ||
for batch in iterator: | ||
yield batch | ||
|
||
df = self.spark.range(10) | ||
|
||
treqs = TaskResourceRequests().cpus(3) | ||
rp = ResourceProfileBuilder().require(treqs).build | ||
df.mapInPandas(func, "id long", False, rp).collect() | ||
|
||
|
||
class ResourceProfileTests(ResourceProfileTestsMixin, ReusedPySparkTestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.sc = SparkContext("local-cluster[1, 4, 1024]", cls.__name__, conf=cls.conf()) | ||
cls.spark = SparkSession(cls.sc) | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
super(ResourceProfileTests, cls).tearDownClass() | ||
cls.spark.stop() | ||
|
||
|
||
if __name__ == "__main__": | ||
from pyspark.sql.tests.test_resources import * # noqa: F401 | ||
|
||
try: | ||
import xmlrunner | ||
|
||
testRunner = xmlrunner.XMLTestRunner(output="target/test-reports", verbosity=2) | ||
except ImportError: | ||
testRunner = None | ||
unittest.main(testRunner=testRunner, verbosity=2) |
This file contains 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 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 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 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 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 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
Oops, something went wrong.