-
Notifications
You must be signed in to change notification settings - Fork 935
make internal broadcast and unbroadcast both primitives #292
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
Open
mattjj
wants to merge
4
commits into
dev-1.2
Choose a base branch
from
internal-broadcast-primitives
base: dev-1.2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6714dc6
make broadcast and unbroadcast both primitives
mattjj 70fa6ba
replace broadcast implementation with a call to onp.broadcast_to
mattjj 91f3960
broadcast/unbroadcast primitives should call onp (not anp)
mattjj 00b8f29
prevent broadcast/unbroadcast nodes from always being added (allow fa…
mattjj 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| import numpy as onp | ||
| from numpy.core.einsumfunc import _parse_einsum_input | ||
| from ..util import func | ||
| from autograd.tracer import primitive, getval | ||
| from autograd.tracer import primitive, notrace_primitive, getval | ||
| from autograd.vspace import vspace | ||
| from autograd.core import defvjp, defvjps, defvjp_is_zero, defvjp_argnum, SparseObject | ||
| from . import numpy_wrapper as anp | ||
|
|
@@ -519,11 +519,11 @@ def vjp(g): | |
| defvjp_argnum(anp.einsum, grad_einsum) | ||
|
|
||
| defvjp(anp.diagonal, | ||
| lambda ans, A, offset=0, axis1=0, axis2=1 : | ||
| lambda g: anp.make_diagonal(g, offset, axis1, axis2)) | ||
| lambda ans, A, offset=0, axis1=0, axis2=1 : | ||
| lambda g: anp.make_diagonal(g, offset, axis1, axis2)) | ||
| defvjp(anp.make_diagonal, | ||
| lambda ans, D, offset=0, axis1=0, axis2=1 : | ||
| lambda g: anp.diagonal(g, offset, axis1, axis2)) | ||
| lambda ans, D, offset=0, axis1=0, axis2=1 : | ||
| lambda g: anp.diagonal(g, offset, axis1, axis2)) | ||
|
|
||
| def match_complex(target, x): | ||
| target_iscomplex = anp.iscomplexobj(target) | ||
|
|
@@ -535,17 +535,53 @@ def match_complex(target, x): | |
| else: | ||
| return x | ||
|
|
||
| @notrace_primitive | ||
| def _needs_broadcast(x, target_meta): | ||
| target_shape, _, _, target_iscomplex = target_meta | ||
| return (onp.shape(x) != target_shape | ||
| or (target_iscomplex != onp.iscomplexobj(x))) | ||
|
|
||
| def broadcast(x, target_meta, broadcast_idx=0): | ||
| if _needs_broadcast(x, target_meta): | ||
| return _broadcast(x, target_meta, broadcast_idx) | ||
| return x | ||
|
|
||
| @primitive | ||
| def _broadcast(x, target_meta, broadcast_idx=0): | ||
| target_shape, _, _, target_iscomplex = target_meta | ||
| x = onp.broadcast_to(x, target_shape) | ||
| if target_iscomplex and not onp.iscomplexobj(x): | ||
| x = x + 0j # TODO(mattjj): this might promote the dtype | ||
| return x | ||
|
|
||
| def grad_broadcast(ans, x, target_meta, broadcast_idx=0): | ||
| meta = anp.metadata(x) | ||
| return lambda g: _unbroadcast(g, meta, broadcast_idx) | ||
| defvjp(_broadcast, grad_broadcast) | ||
|
|
||
| def unbroadcast(x, target_meta, broadcast_idx=0): | ||
| target_shape, target_ndim, dtype, target_iscomplex = target_meta | ||
| while anp.ndim(x) > target_ndim: | ||
| x = anp.sum(x, axis=broadcast_idx) | ||
| if _needs_broadcast(x, target_meta): | ||
| return _unbroadcast(x, target_meta, broadcast_idx) | ||
| return x | ||
|
|
||
| @primitive | ||
| def _unbroadcast(x, target_meta, broadcast_idx=0): | ||
| target_shape, target_ndim, _, target_iscomplex = target_meta | ||
| x_shape = onp.shape(x) | ||
| while onp.ndim(x) > target_ndim: | ||
| x = onp.sum(x, axis=broadcast_idx) | ||
| for axis, size in enumerate(target_shape): | ||
| if size == 1: | ||
| x = anp.sum(x, axis=axis, keepdims=True) | ||
| if anp.iscomplexobj(x) and not target_iscomplex: | ||
| x = anp.real(x) | ||
| if size == 1: # TODO(mattjj): bug here w/ passing through scalars? | ||
| x = onp.sum(x, axis=axis, keepdims=True) | ||
|
Collaborator
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. You could do a similar thing for this one. |
||
| if onp.iscomplexobj(x) and not target_iscomplex: | ||
| x = onp.real(x) | ||
| return x | ||
|
|
||
| def grad_unbroadcast(ans, x, target_meta, broadcast_idx=0): | ||
| meta = anp.metadata(x) | ||
| return lambda g: _broadcast(g, meta, broadcast_idx) | ||
| defvjp(_unbroadcast, grad_unbroadcast) | ||
|
|
||
| def unbroadcast_f(target, f): | ||
| target_meta = anp.metadata(target) | ||
| return lambda g: unbroadcast(f(g), target_meta) | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I was wondering if we should replace the above two lines with:
or similar. Am I right that only calling
sumonce might lead to better performance, basically because only one output array has to be allocated?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.
I briefly tried something like that (though if
broadcast_idxis -1, which is the only nonzero use case I noticed in the code, then I think we want something different) and it didn't seem to make a speed difference, so I dropped it. Now is a good time to make sure it's performant, though!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.
Doing a few timings it looks like there is a benefit for small arrays but it's not massive:
and for slightly bigger arrays it's the other way round (maybe I've made some mistake?):
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.
Wow, I got similar timings. That seems weird for the bigger arrays...