-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
[data] Implement zero-copy fusion for Read op #38789
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1ee542e
Implement zero-copy fusion for read op
raulchen a8d1c1a
default rule
raulchen 4ed03fb
comment
raulchen 998865d
lint
raulchen 61640a4
unskip test
raulchen b24e206
setter getter
raulchen 679ff65
rename
raulchen d8a3a68
unit test
raulchen d56561e
refine
raulchen 5bbef7d
comment
raulchen 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 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
88 changes: 88 additions & 0 deletions
88
python/ray/data/_internal/logical/rules/zero_copy_map_fusion.py
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,88 @@ | ||||||
from abc import abstractmethod | ||||||
from typing import List | ||||||
|
||||||
from ray.data._internal.execution.operators.map_operator import MapOperator | ||||||
from ray.data._internal.execution.operators.map_transformer import ( | ||||||
BuildOutputBlocksMapTransformFn, | ||||||
MapTransformFn, | ||||||
MapTransformFnDataType, | ||||||
) | ||||||
from ray.data._internal.logical.interfaces.optimizer import Rule | ||||||
from ray.data._internal.logical.interfaces.physical_plan import PhysicalPlan | ||||||
|
||||||
|
||||||
class ZeroCopyMapFusionRule(Rule): | ||||||
"""Base abstract class for all zero-copy map fusion rules. | ||||||
|
||||||
A zero-copy map fusion rule is a rule that optimizes the transform_fn chain of | ||||||
a fused MapOperator. The optimization is usually done by removing unnecessary | ||||||
data conversions. | ||||||
|
||||||
This base abstract class defines the common util functions. And subclasses | ||||||
should implement the `_optimize` method for the concrete optimization | ||||||
strategy. | ||||||
""" | ||||||
|
||||||
def apply(self, plan: PhysicalPlan) -> PhysicalPlan: | ||||||
self._traverse(plan.dag) | ||||||
return plan | ||||||
|
||||||
def _traverse(self, op): | ||||||
raulchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
"""Traverse the DAG and apply the optimization to each MapOperator.""" | ||||||
if isinstance(op, MapOperator): | ||||||
map_transformer = op.get_map_transformer() | ||||||
transform_fns = map_transformer.get_transform_fns() | ||||||
new_transform_fns = self._optimize(transform_fns) | ||||||
# Physical operators won't be shared, | ||||||
# so it's safe to modify the transform_fns in place. | ||||||
map_transformer.set_transform_fns(new_transform_fns) | ||||||
|
||||||
for input_op in op.input_dependencies: | ||||||
self._traverse(input_op) | ||||||
|
||||||
@abstractmethod | ||||||
def _optimize(self, transform_fns: List[MapTransformFn]) -> List[MapTransformFn]: | ||||||
"""Optimize the transform_fns chain of a MapOperator. | ||||||
|
||||||
Args: | ||||||
transform_fns: The old transform_fns chain. | ||||||
Returns: | ||||||
The optimized transform_fns chain. | ||||||
""" | ||||||
... | ||||||
|
||||||
|
||||||
class EliminateBuildOutputBlocks(ZeroCopyMapFusionRule): | ||||||
"""This rule eliminates unnecessary BuildOutputBlocksMapTransformFn, | ||||||
if the previous fn already outputs blocks. | ||||||
|
||||||
This happens for the "Read -> Map/Write" fusion. | ||||||
""" | ||||||
|
||||||
def _optimize(self, transform_fns: List[MapTransformFn]) -> List[MapTransformFn]: | ||||||
# For the following subsquence, | ||||||
# 1. Any MapTransformFn with block output. | ||||||
# 2. BuildOutputBlocksMapTransformFn | ||||||
# 3. Any MapTransformFn with block input. | ||||||
# We drop the BuildOutputBlocksMapTransformFn in the middle. | ||||||
new_transform_fns = [] | ||||||
|
||||||
for i in range(len(transform_fns)): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Nit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is intentional. Because the fist and last transform_fns also need to be added to the result. |
||||||
cur_fn = transform_fns[i] | ||||||
drop = False | ||||||
if ( | ||||||
i > 0 | ||||||
and i < len(transform_fns) - 1 | ||||||
and isinstance(cur_fn, BuildOutputBlocksMapTransformFn) | ||||||
): | ||||||
prev_fn = transform_fns[i - 1] | ||||||
next_fn = transform_fns[i + 1] | ||||||
if ( | ||||||
prev_fn.output_type == MapTransformFnDataType.Block | ||||||
and next_fn.input_type == MapTransformFnDataType.Block | ||||||
): | ||||||
drop = True | ||||||
if not drop: | ||||||
new_transform_fns.append(cur_fn) | ||||||
|
||||||
return new_transform_fns |
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.
Oops, something went wrong.
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.
where is this being used?
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.
Currently not being used. I only used this when debugging, and decided to keep it as having a better repr won't hurt.
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.
now it's used in the unit test.