Skip to content
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 10 commits into from
Aug 23, 2023

Conversation

raulchen
Copy link
Contributor

Why are these changes needed?

Optimize Read -> Map/Write fusion. In this case, we can drop the unnecessary BuildOutputBlocks transform_fn.

Also change MapTransformFn to an abstract class and enforce implementations to use subclasses. This is for optimization rules to better detecting the pattern.

Related issue number

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

Signed-off-by: Hao Chen <chenh1024@gmail.com>

comment and repr

Signed-off-by: Hao Chen <chenh1024@gmail.com>

lint

Signed-off-by: Hao Chen <chenh1024@gmail.com>
Signed-off-by: Hao Chen <chenh1024@gmail.com>
Signed-off-by: Hao Chen <chenh1024@gmail.com>
Signed-off-by: Hao Chen <chenh1024@gmail.com>
Signed-off-by: Hao Chen <chenh1024@gmail.com>
Copy link
Contributor

@stephanie-wang stephanie-wang left a comment

Choose a reason for hiding this comment

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

Looks good! Can we add a unit test for the optimization rule?

# In this case, we can drop the BuildOutputBlocksMapTransformFn.
new_transform_fns = []

for i in range(len(transform_fns)):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for i in range(len(transform_fns)):
for i in range(1, len(transform_fns) - 1):

Nit

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

@c21 c21 left a comment

Choose a reason for hiding this comment

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

Thanks @raulchen! Can we also add unit test for the rule?

def __call__(
self, input: Iterable[MapTransformFnData], ctx: TaskContext
) -> Iterable[MapTransformFnData]:
return self._callable(input, ctx)
pass
Copy link
Contributor

Choose a reason for hiding this comment

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

can we do raise NotImplementedError instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When using @abstractmethod, no need to add raise NotImplementedError.
Abstract classes are better than NotImplementedError, because an exception will raised when creating the object, instead of when calling the method.

Copy link
Contributor

Choose a reason for hiding this comment

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

got it, thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: ... seems to be recommended practice https://docs.python.org/3/library/abc.html

Comment on lines 31 to 33
# Physical operators won't be shared,
# so it's safe to modify the transform_fns in place.
map_transformer._transform_fns = new_transform_fns
Copy link
Contributor

Choose a reason for hiding this comment

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

it would be good to define a method in MapTransformer

Returns:
The optimized transform_fns chain.
"""
pass
Copy link
Contributor

Choose a reason for hiding this comment

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

can we do raise NotImplementedError instead?

pass


class ReadOpZeroCopyMapFusion(ZeroCopyMapFusionRule):
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we be more general for the naming? Such as EliminateBuildOutputblocks? There's no read/write operator in physical world, and this optimization rule is really doing the work to eliminate unnecessary BuildOutputBlocksMapTransformFn.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

def __call__(self, input: Iterable[Row], ctx: TaskContext) -> Iterable[Row]:
yield from self._row_fn(input, ctx)

def __repr__(self) -> str:
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Signed-off-by: Hao Chen <chenh1024@gmail.com>
Signed-off-by: Hao Chen <chenh1024@gmail.com>
def __call__(
self, input: Iterable[MapTransformFnData], ctx: TaskContext
) -> Iterable[MapTransformFnData]:
return self._callable(input, ctx)
pass
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: ... seems to be recommended practice https://docs.python.org/3/library/abc.html

"""Base class for zero-copy map fusion rules.

Subclasses implement the optimization strategies for different combinations of
fused map operators, by dropping unnecessary data conversion `MapTransformFn`s.
Copy link
Contributor

Choose a reason for hiding this comment

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

Add more to the docstring? What is this rule doing? When should subclasses override this rule?

pass


class ReadOpZeroCopyMapFusion(ZeroCopyMapFusionRule):
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

Signed-off-by: Hao Chen <chenh1024@gmail.com>
Copy link
Contributor

@c21 c21 left a comment

Choose a reason for hiding this comment

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

LGTM

Signed-off-by: Hao Chen <chenh1024@gmail.com>
@raulchen
Copy link
Contributor Author

raulchen commented Aug 23, 2023

Added a unit test and all comments are addressed.
I used another test PR to run the release tests. But not sure if there is an infra issue, the job always stucks at waiting for the image building https://buildkite.com/ray-project/release-tests-pr/builds/50420#018a235a-0b83-47db-a45c-9d971c5313b7
I'll use this PR to run release tests again. If it still won't run. I think we can also merge this PR first.

Signed-off-by: Hao Chen <chenh1024@gmail.com>
@raulchen raulchen merged commit 1419cfb into ray-project:master Aug 23, 2023
49 of 55 checks passed
@raulchen raulchen deleted the read-zero-copy-fusion branch August 23, 2023 23:08
stephanie-wang added a commit that referenced this pull request Aug 29, 2023
This updates includes a few fixes for image classification benchmarks:

    use Dataset.map instead of Dataset.map_batches. 

[data] Implement zero-copy fusion for Read op #38789 ensures these will get fused with the Read, but map_batches also has some batch formatting overhead.
fix a bug in the benchmark related to image array dimensions
avoid a copy in the map transform
---------

Signed-off-by: Stephanie Wang <swang@cs.berkeley.edu>
arvind-chandra pushed a commit to lmco/ray that referenced this pull request Aug 31, 2023
Optimize `Read -> Map/Write` fusion. In this case, we can drop the unnecessary `BuildOutputBlocks` transform_fn.

Also change `MapTransformFn` to an abstract class and enforce implementations to use subclasses. This is for optimization rules to better detecting the pattern.
---------

Signed-off-by: Hao Chen <chenh1024@gmail.com>
Signed-off-by: e428265 <arvind.chandramouli@lmco.com>
arvind-chandra pushed a commit to lmco/ray that referenced this pull request Aug 31, 2023
This updates includes a few fixes for image classification benchmarks:

    use Dataset.map instead of Dataset.map_batches.

[data] Implement zero-copy fusion for Read op ray-project#38789 ensures these will get fused with the Read, but map_batches also has some batch formatting overhead.
fix a bug in the benchmark related to image array dimensions
avoid a copy in the map transform
---------

Signed-off-by: Stephanie Wang <swang@cs.berkeley.edu>
Signed-off-by: e428265 <arvind.chandramouli@lmco.com>
LeonLuttenberger pushed a commit to jaidisido/ray that referenced this pull request Sep 5, 2023
This updates includes a few fixes for image classification benchmarks:

    use Dataset.map instead of Dataset.map_batches. 

[data] Implement zero-copy fusion for Read op ray-project#38789 ensures these will get fused with the Read, but map_batches also has some batch formatting overhead.
fix a bug in the benchmark related to image array dimensions
avoid a copy in the map transform
---------

Signed-off-by: Stephanie Wang <swang@cs.berkeley.edu>
jimthompson5802 pushed a commit to jimthompson5802/ray that referenced this pull request Sep 12, 2023
This updates includes a few fixes for image classification benchmarks:

    use Dataset.map instead of Dataset.map_batches.

[data] Implement zero-copy fusion for Read op ray-project#38789 ensures these will get fused with the Read, but map_batches also has some batch formatting overhead.
fix a bug in the benchmark related to image array dimensions
avoid a copy in the map transform
---------

Signed-off-by: Stephanie Wang <swang@cs.berkeley.edu>
Signed-off-by: Jim Thompson <jimthompson5802@gmail.com>
vymao pushed a commit to vymao/ray that referenced this pull request Oct 11, 2023
Optimize `Read -> Map/Write` fusion. In this case, we can drop the unnecessary `BuildOutputBlocks` transform_fn.

Also change `MapTransformFn` to an abstract class and enforce implementations to use subclasses. This is for optimization rules to better detecting the pattern.
---------

Signed-off-by: Hao Chen <chenh1024@gmail.com>
Signed-off-by: Victor <vctr.y.m@example.com>
vymao pushed a commit to vymao/ray that referenced this pull request Oct 11, 2023
This updates includes a few fixes for image classification benchmarks:

    use Dataset.map instead of Dataset.map_batches.

[data] Implement zero-copy fusion for Read op ray-project#38789 ensures these will get fused with the Read, but map_batches also has some batch formatting overhead.
fix a bug in the benchmark related to image array dimensions
avoid a copy in the map transform
---------

Signed-off-by: Stephanie Wang <swang@cs.berkeley.edu>
Signed-off-by: Victor <vctr.y.m@example.com>
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.

None yet

4 participants