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]Add where operator #1416

Merged
merged 4 commits into from
Jul 13, 2018
Merged

[TOPI]Add where operator #1416

merged 4 commits into from
Jul 13, 2018

Conversation

kevinthesun
Copy link
Contributor

where operator to support gluoncv SSD model. #1269

@kevinthesun kevinthesun changed the title Add where operator [TOPI]Add where operator Jul 10, 2018
@tqchen
Copy link
Member

tqchen commented Jul 11, 2018

@kevinthesun please request reviews from reviewers

const TShape& y_shape = in_attrs->at(2);
CHECK_EQ(x_shape, y_shape) << "x and y must have the same shape: "
<< x_shape << " vs " << y_shape;
if (cond_shape != x_shape) {
Copy link
Contributor

Choose a reason for hiding this comment

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

How about proper broadcasting to allow arbitrary shape for condition?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Current implementation aligns with numpy to allow condition shape to be either same as x or 1-D. We can add more broadcasting later if necessary.

inline bool WhereInferType(const NodeAttrs &attrs,
std::vector<int> *in_attrs,
std::vector<int> *out_attrs) {
DTYPE_ASSIGN(out_attrs->at(0), in_attrs->at(1));
Copy link
Contributor

Choose a reason for hiding this comment

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

Also check for dtype of X, Y to be same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

size is the same as x’s first dimension size. Each row of the output array
is from x’s row if the corresponding element from condition is true, and
from y’s row if false.

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest to add some basic example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

return tvm::select(condition(indices) != 0, x(indices), y(indices));
}, name, tag);
} else {
CHECK_EQ(condition->shape.size(), 1) << "condition array must be either "
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this check ever fail as you are checking the same condition in if statement?

"have the same shape as x or to be a 1-D array.";
out = compute(
oshape, [&](const Array<Var>& indices) {
Array<Expr> condition_idx{indices[0]};
Copy link
Contributor

@PariksheetPinjari909 PariksheetPinjari909 Jul 11, 2018

Choose a reason for hiding this comment

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

Is condition index always from the first dimension? Is it 'where' operator's property or user can specify the axis?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are two cases for condition shape. 1. the same as x. 2. to be 1-D. This is for 1-D case.

Copy link
Contributor

@PariksheetPinjari909 PariksheetPinjari909 Jul 12, 2018

Choose a reason for hiding this comment

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

In second case of 1-D condition array, 'condition_idx{indices[0]}' always take first entry of indices array. It can be done 'condition_idx{indices[axis]}' where user can specify the axis along which output is taken from x or y (example: in 2D x and y, choose complete column of x or row of x if condition array has true entry. Current implementation always takes row of x if condition is true.).

But never mind, i checked TF and MxNet implementation, both are using first dimension. This looks okay.

Copy link
Contributor

@srkreddy1238 srkreddy1238 left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM.

@PariksheetPinjari909
Copy link
Contributor

@kevinthesun In 1-D case of condition array, what will happen if the size of condition array does not match with size of first dimension of x array? If it is expected to throw error, you can add a check for it.

@kevinthesun
Copy link
Contributor Author

@PariksheetPinjari909 Added.

Copy link
Contributor

@PariksheetPinjari909 PariksheetPinjari909 left a comment

Choose a reason for hiding this comment

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

Thanks. LGTM.

@tqchen tqchen merged commit 3e77cc3 into apache:master Jul 13, 2018
@tqchen
Copy link
Member

tqchen commented Jul 13, 2018

Thanks @PariksheetPinjari909 @srkreddy1238 for the reviews, thanks @kevinthesun for the contribution. this is now merged!

@kevinthesun kevinthesun deleted the AddWhere branch July 18, 2018 22:29
tqchen pushed a commit to tqchen/tvm that referenced this pull request Aug 4, 2018
sergei-mironov pushed a commit to sergei-mironov/tvm that referenced this pull request Aug 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants