-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[Relay, TOPI] Refactor strided_slice and add axes argument #8165
Conversation
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.
Mostly LGTM, a couple of nitpicks. My only design complaint is that we seem to have some API consistency problems. If we provide a dynamic begin and an axes argument, it looks like we'll hit a codepath that doesn't use the axes properly?
@@ -917,7 +922,7 @@ def strided_slice(data, begin, end, strides=None, slice_mode="end"): | |||
begin = _make.where(begin < cast_like(const(0), begin), begin + ishape_slice, begin) | |||
begin = _make.where(begin >= ishape_slice, ishape_slice, begin) | |||
return _dyn_make.strided_slice(data, begin, end, strides, slice_mode) |
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.
Do we not support axes with dynamic begin?
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.
Perhaps we should throw an error if we hit this codepath and axes is defined? With a TODO for extending axes to the dynamic case?
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.
Yeah I haven't put much thought into axes
argument for dynamic strided slice. My goal was to preserve more static dimensions along axes
, which doesn't apply to the dynamic parameter variant. We should support it for convenience and API consistency sake, but for now I'd like to leave it as TODO and assert axes is None
here until someone complains or comes up with a good use case.
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.
Added TODO and assert.
commit 667011f Author: Masahiro Masuda <masahi129@gmail.com> Date: Thu May 27 16:28:57 2021 +0900 Squashed commit of the following: commit 95242d8 Author: Masahiro Masuda <masahi129@gmail.com> Date: Thu May 27 15:45:19 2021 +0900 Add function attribute for shape func for profiling commit b8ede24 Author: Masahiro Masuda <masahi129@gmail.com> Date: Thu May 27 10:21:06 2021 +0900 layout transform support complete commit 5782b70 Author: Masahiro Masuda <masahi129@gmail.com> Date: Thu May 27 08:31:11 2021 +0900 support layout transform part1 commit e94aa6b Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 19:47:46 2021 +0900 moved utilities to its own file commit 8bf8891 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 17:39:50 2021 +0900 fix format commit e89d599 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 17:33:02 2021 +0900 ToVec -> ConvertToVec commit 001982c Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 17:26:56 2021 +0900 format commit fae57f9 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 17:24:35 2021 +0900 use Any for relay type rel path commit 053eee2 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 17:14:44 2021 +0900 fix commit fbb099c Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 16:39:37 2021 +0900 refactor type rel commit ecfe3cd Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 16:23:47 2021 +0900 working commit b357c2f Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 16:07:07 2021 +0900 refactoring output shape calc commit f69ef40 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 14:23:36 2021 +0900 bug fix end param init commit a5611c9 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 13:42:31 2021 +0900 fix test shape commit e79931a Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 13:42:03 2021 +0900 dyn slice tests left as todo now work commit 7db4cea Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 13:36:30 2021 +0900 remove dynamic input specific op commit 510bce6 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 12:52:30 2021 +0900 refactoring dynamic slice commit 1b3969a Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 09:06:46 2021 +0900 fix slice axes dispatch commit 9a79560 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 08:32:54 2021 +0900 refactor compute commit 80442f8 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 08:11:18 2021 +0900 fixed output shape, refactored version working commit d2538ae Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 07:56:05 2021 +0900 restore another slice variant commit 36aa777 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 06:41:50 2021 +0900 refactoring slice with axes commit 32698b7 Author: Masahiro Masuda <masahi129@gmail.com> Date: Sat May 22 13:11:01 2021 +0900 fix axes null check commit 54fb723 Author: Masahiro Masuda <masahi129@gmail.com> Date: Sat May 22 12:52:18 2021 +0900 Revert "[Bugfix][Vulkan] Call VulkanDeviceAPI destructor on program exit (apache#7997)" This reverts commit 58c3413. commit 37eaf57 Author: Masahiro Masuda <masahi129@gmail.com> Date: Sat May 22 04:30:37 2021 +0900 remove wip layout transform support for slice with axes commit 9bcb2ad Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 18:01:59 2021 +0900 fix pylint commit 7063a09 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 17:57:03 2021 +0900 minor fix commit 96c9231 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 17:54:16 2021 +0900 support dynamic scatter nd commit d4a4db8 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 17:33:19 2021 +0900 gather_dim -> num_indices_per_tuple commit a489375 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 17:23:46 2021 +0900 add dynamic gather_nd test commit 533854a Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 17:18:26 2021 +0900 refactor gather_nd ref funcs commit 36a4501 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 14:36:34 2021 +0900 add gather_nd shape func commit 1853c35 Author: Masahiro Masuda <masahi129@gmail.com> Date: Sat May 22 04:20:39 2021 +0900 add eyelike support commit 150e945 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 04:08:37 2021 +0900 migrating inlined topi compute to topi/transform.h commit 763ac37 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 03:45:37 2021 +0900 strided slice with axes support
thanks @mbrookhart |
* Initial import commit 667011f Author: Masahiro Masuda <masahi129@gmail.com> Date: Thu May 27 16:28:57 2021 +0900 Squashed commit of the following: commit 95242d8 Author: Masahiro Masuda <masahi129@gmail.com> Date: Thu May 27 15:45:19 2021 +0900 Add function attribute for shape func for profiling commit b8ede24 Author: Masahiro Masuda <masahi129@gmail.com> Date: Thu May 27 10:21:06 2021 +0900 layout transform support complete commit 5782b70 Author: Masahiro Masuda <masahi129@gmail.com> Date: Thu May 27 08:31:11 2021 +0900 support layout transform part1 commit e94aa6b Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 19:47:46 2021 +0900 moved utilities to its own file commit 8bf8891 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 17:39:50 2021 +0900 fix format commit e89d599 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 17:33:02 2021 +0900 ToVec -> ConvertToVec commit 001982c Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 17:26:56 2021 +0900 format commit fae57f9 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 17:24:35 2021 +0900 use Any for relay type rel path commit 053eee2 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 17:14:44 2021 +0900 fix commit fbb099c Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 16:39:37 2021 +0900 refactor type rel commit ecfe3cd Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 16:23:47 2021 +0900 working commit b357c2f Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 16:07:07 2021 +0900 refactoring output shape calc commit f69ef40 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 14:23:36 2021 +0900 bug fix end param init commit a5611c9 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 13:42:31 2021 +0900 fix test shape commit e79931a Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 13:42:03 2021 +0900 dyn slice tests left as todo now work commit 7db4cea Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 13:36:30 2021 +0900 remove dynamic input specific op commit 510bce6 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 12:52:30 2021 +0900 refactoring dynamic slice commit 1b3969a Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 09:06:46 2021 +0900 fix slice axes dispatch commit 9a79560 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 08:32:54 2021 +0900 refactor compute commit 80442f8 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 08:11:18 2021 +0900 fixed output shape, refactored version working commit d2538ae Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 07:56:05 2021 +0900 restore another slice variant commit 36aa777 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 06:41:50 2021 +0900 refactoring slice with axes commit 32698b7 Author: Masahiro Masuda <masahi129@gmail.com> Date: Sat May 22 13:11:01 2021 +0900 fix axes null check commit 54fb723 Author: Masahiro Masuda <masahi129@gmail.com> Date: Sat May 22 12:52:18 2021 +0900 Revert "[Bugfix][Vulkan] Call VulkanDeviceAPI destructor on program exit (apache#7997)" This reverts commit 58c3413. commit 37eaf57 Author: Masahiro Masuda <masahi129@gmail.com> Date: Sat May 22 04:30:37 2021 +0900 remove wip layout transform support for slice with axes commit 9bcb2ad Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 18:01:59 2021 +0900 fix pylint commit 7063a09 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 17:57:03 2021 +0900 minor fix commit 96c9231 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 17:54:16 2021 +0900 support dynamic scatter nd commit d4a4db8 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 17:33:19 2021 +0900 gather_dim -> num_indices_per_tuple commit a489375 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 17:23:46 2021 +0900 add dynamic gather_nd test commit 533854a Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 17:18:26 2021 +0900 refactor gather_nd ref funcs commit 36a4501 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 14:36:34 2021 +0900 add gather_nd shape func commit 1853c35 Author: Masahiro Masuda <masahi129@gmail.com> Date: Sat May 22 04:20:39 2021 +0900 add eyelike support commit 150e945 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 04:08:37 2021 +0900 migrating inlined topi compute to topi/transform.h commit 763ac37 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 03:45:37 2021 +0900 strided slice with axes support * fix bad merge * fix cpplint * fix pylint * more cpplint fix * fix compiler warning * add doc * add tests * typo fixed * support axes argument in topi cpp strided slice * Properly test axes argument in relay tests * fix bad merge (revert vm change) * fix tests
* Initial import commit 667011f Author: Masahiro Masuda <masahi129@gmail.com> Date: Thu May 27 16:28:57 2021 +0900 Squashed commit of the following: commit 95242d8 Author: Masahiro Masuda <masahi129@gmail.com> Date: Thu May 27 15:45:19 2021 +0900 Add function attribute for shape func for profiling commit b8ede24 Author: Masahiro Masuda <masahi129@gmail.com> Date: Thu May 27 10:21:06 2021 +0900 layout transform support complete commit 5782b70 Author: Masahiro Masuda <masahi129@gmail.com> Date: Thu May 27 08:31:11 2021 +0900 support layout transform part1 commit e94aa6b Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 19:47:46 2021 +0900 moved utilities to its own file commit 8bf8891 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 17:39:50 2021 +0900 fix format commit e89d599 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 17:33:02 2021 +0900 ToVec -> ConvertToVec commit 001982c Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 17:26:56 2021 +0900 format commit fae57f9 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 17:24:35 2021 +0900 use Any for relay type rel path commit 053eee2 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 17:14:44 2021 +0900 fix commit fbb099c Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 16:39:37 2021 +0900 refactor type rel commit ecfe3cd Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 16:23:47 2021 +0900 working commit b357c2f Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 16:07:07 2021 +0900 refactoring output shape calc commit f69ef40 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 14:23:36 2021 +0900 bug fix end param init commit a5611c9 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 13:42:31 2021 +0900 fix test shape commit e79931a Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 13:42:03 2021 +0900 dyn slice tests left as todo now work commit 7db4cea Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 13:36:30 2021 +0900 remove dynamic input specific op commit 510bce6 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 12:52:30 2021 +0900 refactoring dynamic slice commit 1b3969a Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 09:06:46 2021 +0900 fix slice axes dispatch commit 9a79560 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 08:32:54 2021 +0900 refactor compute commit 80442f8 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 08:11:18 2021 +0900 fixed output shape, refactored version working commit d2538ae Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 07:56:05 2021 +0900 restore another slice variant commit 36aa777 Author: Masahiro Masuda <masahi129@gmail.com> Date: Mon May 24 06:41:50 2021 +0900 refactoring slice with axes commit 32698b7 Author: Masahiro Masuda <masahi129@gmail.com> Date: Sat May 22 13:11:01 2021 +0900 fix axes null check commit 54fb723 Author: Masahiro Masuda <masahi129@gmail.com> Date: Sat May 22 12:52:18 2021 +0900 Revert "[Bugfix][Vulkan] Call VulkanDeviceAPI destructor on program exit (apache#7997)" This reverts commit 58c3413. commit 37eaf57 Author: Masahiro Masuda <masahi129@gmail.com> Date: Sat May 22 04:30:37 2021 +0900 remove wip layout transform support for slice with axes commit 9bcb2ad Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 18:01:59 2021 +0900 fix pylint commit 7063a09 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 17:57:03 2021 +0900 minor fix commit 96c9231 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 17:54:16 2021 +0900 support dynamic scatter nd commit d4a4db8 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 17:33:19 2021 +0900 gather_dim -> num_indices_per_tuple commit a489375 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 17:23:46 2021 +0900 add dynamic gather_nd test commit 533854a Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 17:18:26 2021 +0900 refactor gather_nd ref funcs commit 36a4501 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 14:36:34 2021 +0900 add gather_nd shape func commit 1853c35 Author: Masahiro Masuda <masahi129@gmail.com> Date: Sat May 22 04:20:39 2021 +0900 add eyelike support commit 150e945 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 04:08:37 2021 +0900 migrating inlined topi compute to topi/transform.h commit 763ac37 Author: Masahiro Masuda <masahi129@gmail.com> Date: Fri May 21 03:45:37 2021 +0900 strided slice with axes support * fix bad merge * fix cpplint * fix pylint * more cpplint fix * fix compiler warning * add doc * add tests * typo fixed * support axes argument in topi cpp strided slice * Properly test axes argument in relay tests * fix bad merge (revert vm change) * fix tests
This PR has two purposes:
axes
argument to static paramstrided_slice
strided_slice
related code so that addingaxes
argument would not increase clutter. Also I did further refactoring to remove duplicated output shape calculation between topi/relay type rel (left as TODO) and minimize the number ofte::compute
for strided slice (currently there are 4 of them).For 1, currently slicing along the second axis with input shape
(?, 3)
, begin[0]
, end[1]
calls the dynamic parameter variant of relay/topistrided_slice
withbegin = (0, 0)
andend = (?, ?)
, which results in the output shape(?, ?)
rather than desired(?, 1)
. This causes a problem when the output is fed togather_nd
which requires a certain dimension to be static.The root problem is that our
strided_slice
op does not have a notion ofaxes
and always tries to slice along all axes. So when some of the dimensions are dynamic, we call dynamic parameter variant ofstrided_slice
which makes all input params and the output shape dynamic, even if we only need to slice along static dimensions with static begin/end. My solution addsaxes
argument to static parameter variant ofstrided_slice
so that slicing would not touch dynamic dimensions and preserve static dimensions along providedaxes
.For 2, we accumulated many patches for
strided_slice
over the last few years and I found the current implementation extremely messy and hard to maintain. In order not to make the situation worse by introducingaxes
argument, I decided to roll major refactoring of ourstrided_slice
related code.First, the output shape calculation logic is duplicated between topi and relay type rel:
This has been fixed by introducing StridedSliceOutputShape function in topi and use it from both topi/relay.
Second, currently there are 4
te::compute
forstrided_slice
, each with slightly different expectation on its input and input param canonicalization.I examined each of them in detail and decided that we only need two
te::compute
to support all cases:Array<Integer>
. Input shape can be partially dynamic/static.tvm/include/tvm/topi/transform.h
Lines 700 to 712 in 64c1bbf
Array<PrimExpr>
.tvm/include/tvm/topi/transform.h
Lines 594 to 608 in 64c1bbf
In both cases, static input dimensions are preserved as much as possible. The
axes
is added only to the first variant, according to my use case.please review @tqchen @mbrookhart @kevinthesun @yongwww @comaniac