Skip to content
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

Merged
merged 13 commits into from
Jun 2, 2021

Conversation

masahi
Copy link
Member

@masahi masahi commented May 31, 2021

This PR has two purposes:

  1. Add axes argument to static param strided_slice
  2. Refactor strided_slice related code so that adding axes 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 of te::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/topi strided_slice with begin = (0, 0) and end = (?, ?), which results in the output shape (?, ?) rather than desired (?, 1). This causes a problem when the output is fed to gather_nd which requires a certain dimension to be static.

The root problem is that our strided_slice op does not have a notion of axes and always tries to slice along all axes. So when some of the dimensions are dynamic, we call dynamic parameter variant of strided_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 adds axes argument to static parameter variant of strided_sliceso that slicing would not touch dynamic dimensions and preserve static dimensions along provided axes.

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 introducing axes argument, I decided to roll major refactoring of our strided_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 for strided_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:

  • A static parameter variant, where all input parameters are Array<Integer>. Input shape can be partially dynamic/static.
    return te::compute(
    out_shape,
    [&](const Array<tir::Var>& indices) {
    Array<PrimExpr> real_indices;
    for (size_t i = 0; i < out_shape.size(); ++i) real_indices.push_back(indices[i]);
    for (size_t i = 0; i < axes.size(); ++i) {
    auto stride = make_const(strides[i].dtype(), strides_vec[i]);
    PrimExpr ind = indices[axes[i]] * stride + begin_expr[i];
    real_indices.Set(axes[i], ind);
    }
    return x(real_indices);
    },
    name, tag);
  • A partially dynamic/static parameter variant, where all input parameters are Array<PrimExpr>.
    return te::compute(
    out_shape,
    [&](const Array<tvm::tir::Var>& indices) {
    Array<PrimExpr> real_indices;
    for (size_t i = 0; i < num_slice_axes; ++i) {
    real_indices.push_back(indices[i] * strides[i] + tvm::min(begin[i], x->shape[i] - 1));
    }
    // keep input dim
    for (size_t i = num_slice_axes; i < src_tensor_dim; ++i) {
    real_indices.push_back(indices[i]);
    }
    return x(real_indices);
    },
    name, tag);
    }

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

@masahi masahi marked this pull request as ready for review June 1, 2021 06:57
Copy link
Contributor

@mbrookhart mbrookhart left a 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?

include/tvm/topi/transform.h Outdated Show resolved Hide resolved
src/relay/op/tensor/transform.cc Show resolved Hide resolved
src/topi/transform.cc Show resolved Hide resolved
@@ -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)
Copy link
Contributor

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?

Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added TODO and assert.

masahi added 11 commits June 2, 2021 18:25
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
@masahi
Copy link
Member Author

masahi commented Jun 2, 2021

thanks @mbrookhart

trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Jun 17, 2021
* 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
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request Jun 17, 2021
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants