Skip to content

Conversation

@SeKim12
Copy link
Contributor

@SeKim12 SeKim12 commented Jun 30, 2024

Addresses #19904, adds associative_scan support for all backends.

This is my first time contributing, so would greatly appreciate any feedback or something I may be missing (and would be more than happy to apply them)! Thanks!

@google-cla
Copy link

google-cla bot commented Jun 30, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Collaborator

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! The code looks good to me.

Args:
f: A Python callable implementing an associative binary operation with
signature ``r = f(a, b)``. Function `f` must be associative, i.e.,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use single backticks for code keywords, throughout.

def associative_scan(f, elems, reverse=False, axis=0):
"""Performs a scan with an associative binary operation, in parallel.
For an introduction to associative scans, see [BLE1990]_.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Prefer in-lining the link with a proper title here.


@keras_export("keras.ops.associative_scan")
def associative_scan(f, elems, reverse=False, axis=0):
"""Performs a scan with an associative binary operation, in parallel.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Below, it would be worth introducing more info about use cases for this op. The docstring should answer, what does this do, and why would I want to use it? Perhaps contrast it with scan.

@codecov-commenter
Copy link

codecov-commenter commented Jun 30, 2024

Codecov Report

Attention: Patch coverage is 69.43005% with 59 lines in your changes missing coverage. Please review.

Project coverage is 72.52%. Comparing base (0c69b3b) to head (efbce61).

Files Patch % Lines
keras/src/backend/tensorflow/core.py 7.14% 52 Missing ⚠️
keras/src/backend/common/backend_utils.py 60.00% 1 Missing and 1 partial ⚠️
keras/src/backend/numpy/core.py 96.00% 1 Missing and 1 partial ⚠️
keras/src/backend/torch/core.py 96.29% 1 Missing and 1 partial ⚠️
keras/api/_tf_keras/keras/ops/__init__.py 0.00% 1 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (0c69b3b) and HEAD (efbce61). Click for more details.

HEAD has 2 uploads less than BASE
Flag BASE (0c69b3b) HEAD (efbce61)
keras 4 3
keras-tensorflow 1 0
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #19938      +/-   ##
==========================================
- Coverage   79.00%   72.52%   -6.48%     
==========================================
  Files         499      499              
  Lines       46531    46724     +193     
  Branches     8561     8617      +56     
==========================================
- Hits        36761    33888    -2873     
- Misses       8039    11106    +3067     
+ Partials     1731     1730       -1     
Flag Coverage Δ
keras 72.43% <69.43%> (-6.43%) ⬇️
keras-jax 62.26% <19.68%> (-0.18%) ⬇️
keras-numpy 57.29% <44.04%> (-0.06%) ⬇️
keras-tensorflow ?
keras-torch 62.32% <44.04%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

def associative_scan(f, elems, reverse=False, axis=0):
"""Performs a scan with an associative binary operation, in parallel.
This operation has a similar use-case to scan. The key difference is that
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the detailed feedback! I'd also like to add a little bit of context to this statement using some ad hoc benchmarking results from this script. I have results for different backends as well showing a similar trend.

numpy_result

Copy link
Collaborator

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

LGTM -- thank you for the contribution. Will apply some minor docstring fixes afterwards.

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Jun 30, 2024
@fchollet fchollet merged commit 42b6cfd into keras-team:master Jun 30, 2024
@google-ml-butler google-ml-butler bot removed ready to pull Ready to be merged into the codebase kokoro:force-run labels Jun 30, 2024
@fchollet
Copy link
Collaborator

It seems there is a device placement issue with torch on GPU, could you take a look? https://btx.cloud.google.com/invocations/0b42aedc-c253-401a-b110-1f441959ccc8/targets/keras%2Fgithub%2Fubuntu%2Fgpu%2Ftorch%2Fcontinuous/log

@SeKim12
Copy link
Contributor Author

SeKim12 commented Jun 30, 2024

It seems there is a device placement issue with torch on GPU, could you take a look? https://btx.cloud.google.com/invocations/0b42aedc-c253-401a-b110-1f441959ccc8/targets/keras%2Fgithub%2Fubuntu%2Fgpu%2Ftorch%2Fcontinuous/log

Thanks for the info! I made a PR with a fix: #19940

@SeKim12 SeKim12 deleted the add-associative-scan branch July 1, 2024 20:16
@carlosluis
Copy link

@SeKim12 thanks for this nice contribution!

Did you test using the pytorch backend? Are you able to compile the associative scan operator? I will be testing this myself on an isolated project, but wanted to get your feedback in case you already ran some benchmarks. Thanks!

@SeKim12
Copy link
Contributor Author

SeKim12 commented Jul 9, 2024

@SeKim12 thanks for this nice contribution!

Did you test using the pytorch backend? Are you able to compile the associative scan operator? I will be testing this myself on an isolated project, but wanted to get your feedback in case you already ran some benchmarks. Thanks!

Hi @carlosluis! I was able to compile it and use it for the PyTorch backend. Here is a script that I used to quickly benchmark performance: https://gist.github.com/SeKim12/0b5a77fbb05c707e60dcee03cfd7c24b

@SamanehSaadat
Copy link
Member

Hi @SeKim12!

This PR imports optree directly (example), which breaks Google colab usage. We recommend using from keras.src import tree instead! Could you refactor parts of your code that uses optree to use Keras' tree instead?

cc @hertschuh @fchollet

@SeKim12
Copy link
Contributor Author

SeKim12 commented Aug 13, 2024

Hi @SamanehSaadat,

My apologies -- I just saw this notification! It seems like the optree imports were handled by @fchollet. Thank you! Please let me know if there is anything else I can do!

ryantqiu pushed a commit to snorkel-marlin-repos/keras-team_keras_pr_19938_89538a5e-cc8c-4459-aaaf-5bbca73d7fb1 that referenced this pull request Oct 1, 2025
Original PR #19938 by SeKim12
Original: keras-team/keras#19938
ryantqiu added a commit to snorkel-marlin-repos/keras-team_keras_pr_19938_89538a5e-cc8c-4459-aaaf-5bbca73d7fb1 that referenced this pull request Oct 1, 2025
Merged from original PR #19938
Original: keras-team/keras#19938
ryantqiu pushed a commit to snorkel-marlin-repos/keras-team_keras_pr_19938_0d2829c0-35d6-40e4-ad24-a3dc0ebe70be that referenced this pull request Oct 2, 2025
Original PR #19938 by SeKim12
Original: keras-team/keras#19938
ryantqiu added a commit to snorkel-marlin-repos/keras-team_keras_pr_19938_0d2829c0-35d6-40e4-ad24-a3dc0ebe70be that referenced this pull request Oct 2, 2025
Merged from original PR #19938
Original: keras-team/keras#19938
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants