Skip to content

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
wants to merge 2 commits into from

Conversation

ricardoV94
Copy link
Member

@ricardoV94 ricardoV94 commented Jan 5, 2023

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

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
Copy link
Member Author

@ricardoV94 ricardoV94 Jan 9, 2023

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

@ricardoV94 ricardoV94 changed the title Implement Scalar Scan Implement Scalar Scan (as dummy Op) Apr 25, 2023
@ricardoV94 ricardoV94 closed this May 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider implementing a scalar Scan Op
1 participant