-
Notifications
You must be signed in to change notification settings - Fork 134
Implement Scalar Scan (as dummy Op) #174
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
Closed
Closed
Conversation
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
aea10aa
to
7396b31
Compare
ricardoV94
commented
Jan 6, 2023
7396b31
to
aad7681
Compare
ricardoV94
commented
Jan 9, 2023
Comment on lines
+1058
to
+1072
# Scan output size is given by the size of the input leading dimension, by default its n_steps + 1. | ||
# If we only want to store the last elements we can shorten the leading dimension to 1 | ||
scan_node = ret[0].owner.inputs[0].owner | ||
scan_inputs = scan_node.inputs | ||
n_steps = scan_inputs[0] | ||
n_non_seqs = scan_node.op.info.n_non_seqs | ||
carried_inputs = scan_inputs[1 : len(scan_inputs) - n_non_seqs :] | ||
constant_inputs = scan_inputs[len(scan_inputs) - n_non_seqs :] | ||
new_carried_inputs = [] | ||
for carried_input in carried_inputs: | ||
assert isinstance(carried_input.owner.op, IncSubtensor) | ||
fill_value = carried_input.owner.inputs[1] | ||
# TODO: Check for the global flag where this is controlled | ||
new_carried_inputs.append(expand_empty(fill_value, 1)) | ||
ret = scan_node.op.make_node(n_steps, *new_carried_inputs, *constant_inputs).outputs |
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.
One shouldn't have to hack into scan internals to avoid saving the intermediate results... This is needed because of #178
1 task
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Alternative to #283
Closes #83
The idea here is to implement a specialized kind of looping operation that can be treated as an Elemwise. I create a dummy scalar Op, whose Elemwise version is then converted to a scan by a rewrite.
As a test case I made use of the Scalar Scans in the gradient of gammaincc. Performance is now 20-30x better than before when running the new benchmark test, even though the two branches are now always computed. Further improvements could be achieved with a lazy switch, but that is not really the goal of this PR.
PS: It shouldn't be so hard to write a scan without accumulation