-
Notifications
You must be signed in to change notification settings - Fork 129
Speedup Scan
in different backends
#1281
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f03ded4
Stack eager optimization for single tensor
ricardoV94 4d9c1c5
Simplify `expand_empty`
ricardoV94 8046e97
Fix constant number of steps reduction in ScanSaveMem rewrite
ricardoV94 b0b1ee1
Benchmark Scan buffer optimization in Numba
ricardoV94 93fb5a4
Do not try to save initial values buffer size in Scan
ricardoV94 1364e31
Do more agressive scan memory saves in JIT backends
ricardoV94 ca09602
Minor numba Scan tweaks
ricardoV94 2ff1b22
Allow inplacing of SITSOT and last MITSOT in numba Scan, when they ar…
ricardoV94 cae417f
Cleanup Scan symbolic buffer size graph
ricardoV94 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 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,7 @@ def range_arr(x): | |
|
||
|
||
@numba_funcify.register(Scan) | ||
def numba_funcify_Scan(op, node, **kwargs): | ||
def numba_funcify_Scan(op: Scan, node, **kwargs): | ||
# Apply inner rewrites | ||
# TODO: Not sure this is the right place to do this, should we have a rewrite that | ||
# explicitly triggers the optimization of the inner graphs of Scan? | ||
|
@@ -67,9 +67,32 @@ def numba_funcify_Scan(op, node, **kwargs): | |
.optimizer | ||
) | ||
fgraph = op.fgraph | ||
# When the buffer can only hold one SITSOT or as as many MITSOT as there are taps, | ||
# We must always discard the oldest tap, so it's safe to destroy it in the inner function. | ||
# TODO: Allow inplace for MITMOT | ||
destroyable_sitsot = [ | ||
inner_sitsot | ||
for outer_sitsot, inner_sitsot in zip( | ||
op.outer_sitsot(node.inputs), op.inner_sitsot(fgraph.inputs), strict=True | ||
) | ||
if outer_sitsot.type.shape[0] == 1 | ||
] | ||
destroyable_mitsot = [ | ||
oldest_inner_mitmot | ||
for outer_mitsot, oldest_inner_mitmot, taps in zip( | ||
op.outer_mitsot(node.inputs), | ||
op.oldest_inner_mitsot(fgraph.inputs), | ||
op.info.mit_sot_in_slices, | ||
strict=True, | ||
) | ||
if outer_mitsot.type.shape[0] == abs(min(taps)) | ||
] | ||
destroyable = {*destroyable_sitsot, *destroyable_mitsot} | ||
add_supervisor_to_fgraph( | ||
fgraph=fgraph, | ||
input_specs=[In(x, borrow=True, mutable=False) for x in fgraph.inputs], | ||
input_specs=[ | ||
In(x, borrow=True, mutable=x in destroyable) for x in fgraph.inputs | ||
], | ||
accept_inplace=True, | ||
) | ||
rewriter(fgraph) | ||
|
@@ -222,14 +245,16 @@ def add_output_storage_post_proc_stmt( | |
# the storage array. | ||
# This is needed when the output storage array does not have a length | ||
# equal to the number of taps plus `n_steps`. | ||
# If the storage size only allows one entry, there's nothing to rotate | ||
output_storage_post_proc_stmts.append( | ||
dedent( | ||
f""" | ||
if (i + {tap_size}) > {storage_size}: | ||
if 1 < {storage_size} < (i + {tap_size}): | ||
{outer_in_name}_shift = (i + {tap_size}) % ({storage_size}) | ||
{outer_in_name}_left = {outer_in_name}[:{outer_in_name}_shift] | ||
{outer_in_name}_right = {outer_in_name}[{outer_in_name}_shift:] | ||
{outer_in_name} = np.concatenate(({outer_in_name}_right, {outer_in_name}_left)) | ||
if {outer_in_name}_shift > 0: | ||
{outer_in_name}_left = {outer_in_name}[:{outer_in_name}_shift] | ||
{outer_in_name}_right = {outer_in_name}[{outer_in_name}_shift:] | ||
{outer_in_name} = np.concatenate(({outer_in_name}_right, {outer_in_name}_left)) | ||
""" | ||
).strip() | ||
) | ||
|
@@ -417,4 +442,4 @@ def scan({", ".join(outer_in_names)}): | |
|
||
scan_op_fn = compile_function_src(scan_op_src, "scan", {**globals(), **global_env}) | ||
|
||
return numba_basic.numba_njit(scan_op_fn) | ||
return numba_basic.numba_njit(scan_op_fn, boundscheck=False) | ||
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.
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. Probably not found this easier to read |
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
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
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.
PyTensor Scan allows sequences to be longer than steps, in which case they just get unused. The Scan save memory rewrite doesn't bother with trimming them.
JAX however doesn't allow it. Fixing the constant nsteps optimization revealed this issue.