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

[TOPI] C++ topi operators #312

Merged
merged 3 commits into from
Aug 13, 2017
Merged

[TOPI] C++ topi operators #312

merged 3 commits into from
Aug 13, 2017

Conversation

nicolasvasilache
Copy link
Contributor

Summary:
This diff implements C++ topi contributions for:

  • relu with parametrix threshold
  • pad with generic padBefore / padAfter specification
  • matmult with transposes
  • conv2d_nchw, conv2d_hwcn with runtime constant padding and strides
  • depthwise_conv2d_nchw with runtime constant padding and strides
  • group_conv2d_ngchw with runtime constant padding and strides
  • broadcast_to a broadcastable shape
  • broadcast_bop where bop is an usual binary op (+ - * / %)

Convolution padding is implemented using the pad operation.
To avoid extra memory consumption, it is generally recommended to inline the padding with the autoinliner.
Unfortunately in its current form the elemwise checks are too restrictive to allow inlining.
So this diff also proposes an extension to LHS injective (i.e. no reduction axis in the current IR design)

Test Plan:
Tested in C++ testsuite in a separate repository, I am looking for suggestions to quickly spin up some tests for tvm.

Reviewers: tqchen

Subscribers:

Tasks:

Tags:

Blame Revision:

Summary:
This diff implements C++ topi contributions for:
  - relu with parametrix threshold
  - pad with generic padBefore / padAfter specification
  - matmult with transposes
  - conv2d_nchw, conv2d_hwcn with runtime constant padding and strides
  - depthwise_conv2d_nchw with runtime constant padding and strides
  - group_conv2d_ngchw with runtime constant padding and strides
  - broadcast_to a broadcastable shape
  - broadcast_bop where bop is an usual binary op (+ - * / %)

Convolution padding is implemented using the pad operation.
To avoid extra memory consumption, it is generally recommended to inline the padding with the autoinliner.
Unfortunately in its current form the elemwise checks are too restrictive to allow inlining.
So this diff also proposes an extension to LHS injective (i.e. no reduction axis in the current IR design)

Test Plan:
Tested in C++ testsuite in a separate repository, I am looking for suggestions to quickly spin up some tests for tvm.

Reviewers: tqchen

Subscribers:

Tasks:

Tags:

Blame Revision:
@tqchen
Copy link
Member

tqchen commented Aug 11, 2017

It is indeed helpful to introduce an AutoInlineInjective pass. Now, there are three types of of "injective like relatiosn"

  • Strictly ewise
  • Broadcast relation
  • General injective

It is helpful to still keep these three categories, since they might leads to different ways of scheduling. For example, sometimes it is helpful to introduce a caching stage for general injective operator to make the memory layout access more friendly

@tqchen
Copy link
Member

tqchen commented Aug 11, 2017

In light of that, maybe it is helpful to introduce an AutoInlineInjective pass, this makes the intention explicit through function name, as opposed to the additional boolean argument, which user need to lookup through the document

return ivars;
}

typedef std::function<tvm::Expr(tvm::Expr, tvm::Expr)> BinaryExpr;
Copy link
Member

Choose a reason for hiding this comment

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

C++11 style ```using BinaryExpr =````

Copy link
Member

Choose a reason for hiding this comment

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

see also next comment, this can be safely deleted

namespace topi {

template <typename T>
tvm::Expr map(const tvm::Array<tvm::Expr>& exprs, T op) {
Copy link
Member

Choose a reason for hiding this comment

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

for user facing function, use doxygen style comment


namespace topi {

inline tvm::Tensor broadcast_to(const tvm::Array<tvm::Expr>& outputShape,
Copy link
Member

Choose a reason for hiding this comment

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

to be consistent with numpy, use broadcast_to(array, shape)

for (int i = 0; i < ovars.size(); ++i) {
bool found = false;
for (int j = 0; j < myVars.size(); ++j) {
if (tvm::ir::Equal(allVars[i], myVars[j])) {
Copy link
Member

Choose a reason for hiding this comment

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

use allVars[i].same_as, will be faster than deep compare

return bh;
}

inline tvm::Array<tvm::Expr> inputShapeFromBroadcast(
Copy link
Member

Choose a reason for hiding this comment

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

should be more approperiate as InputIndexFromBroadcast?


typedef std::function<tvm::Expr(tvm::Expr, tvm::Expr)> BinaryExpr;

inline tvm::Tensor withBroadcast(BinaryExpr op,
Copy link
Member

Choose a reason for hiding this comment

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

templatize op will make compiler much easier to inline it, if we want good performance without the explicit overhead of creating std::function.

template<FBinaryExpr>
function(FBinaryExpr op, ...)

// Only inject 0 here if we have not yet reached the dimension of I
// (i.e. this must be a 1)
if (!found && (ovars.size() - i) <= expectedDims) {
ivars.push_back(tvm::Expr(0));
Copy link
Member

Choose a reason for hiding this comment

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

safter as make_zero(myVars[0].type()), in case type of index differ from int32

}

template <typename T>
inline tvm::Tensor relu(const tvm::Tensor& x, T threshold) {
Copy link
Member

Choose a reason for hiding this comment

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

default threshold to static_cast(0)

@nicolasvasilache
Copy link
Contributor Author

nicolasvasilache commented Aug 13, 2017

Thanks for the review! This should address the requested changes as well as style and lint issues.
Note that I didn't implement the broadcast check for inlining as I don't have a test case for it yet and would rather see this land first.

@tqchen tqchen merged commit f08de2b into apache:master Aug 13, 2017
@tqchen tqchen changed the title [WIP] C++ topi contributions [TOPI] C++ topi operators Aug 13, 2017
@tqchen
Copy link
Member

tqchen commented Aug 13, 2017

Thanks! this is merged. Please have follow-up PRs to add doxygen comment to the user facing functions

Hzfengsy added a commit to Hzfengsy/tvm that referenced this pull request Mar 1, 2021
Hzfengsy added a commit to Hzfengsy/tvm that referenced this pull request Mar 1, 2021
Co-authored-by: Bohan Hou <32121147+spectrometerHBH@users.noreply.github.com>
Co-authored-by: Junru Shao <junrushao1994@gmail.com>
Co-authored-by: Tianqi Chen <tqchen@users.noreply.github.com>
Co-authored-by: Ruihang Lai <lairuihangdongdong@qq.com>
Co-authored-by: Hongyi Jin <3231950289@qq.com>
Co-authored-by: Wuwei Lin <wuwei@apache.org>
comaniac pushed a commit that referenced this pull request Mar 1, 2021
Co-authored-by: Bohan Hou <32121147+spectrometerHBH@users.noreply.github.com>
Co-authored-by: Junru Shao <junrushao1994@gmail.com>
Co-authored-by: Tianqi Chen <tqchen@users.noreply.github.com>
Co-authored-by: Ruihang Lai <lairuihangdongdong@qq.com>
Co-authored-by: Hongyi Jin <3231950289@qq.com>
Co-authored-by: Wuwei Lin <wuwei@apache.org>

Co-authored-by: Bohan Hou <32121147+spectrometerHBH@users.noreply.github.com>
Co-authored-by: Junru Shao <junrushao1994@gmail.com>
Co-authored-by: Tianqi Chen <tqchen@users.noreply.github.com>
Co-authored-by: Ruihang Lai <lairuihangdongdong@qq.com>
Co-authored-by: Hongyi Jin <3231950289@qq.com>
Co-authored-by: Wuwei Lin <wuwei@apache.org>
Lokiiiiii pushed a commit to Lokiiiiii/tvm that referenced this pull request Mar 2, 2021
Co-authored-by: Bohan Hou <32121147+spectrometerHBH@users.noreply.github.com>
Co-authored-by: Junru Shao <junrushao1994@gmail.com>
Co-authored-by: Tianqi Chen <tqchen@users.noreply.github.com>
Co-authored-by: Ruihang Lai <lairuihangdongdong@qq.com>
Co-authored-by: Hongyi Jin <3231950289@qq.com>
Co-authored-by: Wuwei Lin <wuwei@apache.org>

Co-authored-by: Bohan Hou <32121147+spectrometerHBH@users.noreply.github.com>
Co-authored-by: Junru Shao <junrushao1994@gmail.com>
Co-authored-by: Tianqi Chen <tqchen@users.noreply.github.com>
Co-authored-by: Ruihang Lai <lairuihangdongdong@qq.com>
Co-authored-by: Hongyi Jin <3231950289@qq.com>
Co-authored-by: Wuwei Lin <wuwei@apache.org>
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request Mar 2, 2021
Co-authored-by: Bohan Hou <32121147+spectrometerHBH@users.noreply.github.com>
Co-authored-by: Junru Shao <junrushao1994@gmail.com>
Co-authored-by: Tianqi Chen <tqchen@users.noreply.github.com>
Co-authored-by: Ruihang Lai <lairuihangdongdong@qq.com>
Co-authored-by: Hongyi Jin <3231950289@qq.com>
Co-authored-by: Wuwei Lin <wuwei@apache.org>

Co-authored-by: Bohan Hou <32121147+spectrometerHBH@users.noreply.github.com>
Co-authored-by: Junru Shao <junrushao1994@gmail.com>
Co-authored-by: Tianqi Chen <tqchen@users.noreply.github.com>
Co-authored-by: Ruihang Lai <lairuihangdongdong@qq.com>
Co-authored-by: Hongyi Jin <3231950289@qq.com>
Co-authored-by: Wuwei Lin <wuwei@apache.org>
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.

2 participants