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

[NNVM][TOPI] Add gradients for broadcast_* ops #1234

Merged
merged 16 commits into from Jun 27, 2018
Merged

[NNVM][TOPI] Add gradients for broadcast_* ops #1234

merged 16 commits into from Jun 27, 2018

Conversation

nhynes
Copy link
Member

@nhynes nhynes commented Jun 5, 2018

This PR

  • adds gradient operators for broadcast_* ops by adding a collapse_sum op
  • fixes a bug in reduce ops which didn't correctly apply excluded reduce axes
  • fixes a bug in MakeReduceTargetShape which prevents reduction to scalar

@tqchen
Copy link
Member

tqchen commented Jun 6, 2018

@kevinthesun @nishi-t can you help review this?

}
} else {
--j;
}
Copy link
Contributor

@nishi-t nishi-t Jun 7, 2018

Choose a reason for hiding this comment

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

Is it possible to make L271-281 more simple ?
For example:

if ( j < 0 ) {
  r_axes.push_back(i);
  squeeze_axes.push_back(i);
} else if ( ishpae[i] != oshape[j]) {
  r_axes.push_back(i);
  if (oshape[j] == 1) {
    keepdims = true;
    --j;
  }
} else {
  --j;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Just a bit. I changed the index variable names and added some comments to help make it clearer.

Copy link
Contributor

Choose a reason for hiding this comment

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

looks good to me

NNVM_REGISTER_COLLAPSE_OP(sum)
.describe(R"code(Reduces lhs to the shape of rhs via sum)code" NNVM_ADD_FILELINE)
.set_attr<FTVMCompute>(
"FTVMCompute", [](const NodeAttrs& attrs,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we move computation part into tvm? It's valuable to pack it first in tvm and then directly call in nnvm.

Copy link
Member Author

@nhynes nhynes Jun 26, 2018

Choose a reason for hiding this comment

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

Apparently not. Moving the entire op into topi yields RuntimeError: Not yet support ewise after reduce. I guess I could turn the entire thing into a single topi op, but that defeats the purpose of writing composable functions.

.add_arguments(ReduceParam::__FIELDS__()) \
.set_attr_parser(AxesParamParser<ReduceParam>) \
.set_attr<FInferShape>("FInferShape", CollapseShape) \
.set_attr<FInferType>("FInferType", ElemwiseType<2, 1>) \
Copy link
Contributor

Choose a reason for hiding this comment

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

We need FCorrectLayout attribute to for correct layout pass. An example for elemwise binary op https://github.com/dmlc/tvm/blob/master/nnvm/src/top/elemwise_op_common.h#L301.

@@ -130,7 +134,61 @@ def test_multi_loss_graph_gradients():
# test reverse infer type for label
assert grad_g.apply('InferType').json_attr('dtype_num_unknown_nodes') == 0

def _run_op_grad(op):
Copy link
Contributor

Choose a reason for hiding this comment

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

For operator unit test, we can add them under compiler/test_top_levelx.py, such as https://github.com/dmlc/tvm/blob/master/nnvm/tests/python/compiler/test_top_level1.py. Helper function supporting backward is implemented so that gradient can be tested here.

@tqchen tqchen added status: review in progress status: need update need update based on feedbacks labels Jun 12, 2018
@tqchen
Copy link
Member

tqchen commented Jun 24, 2018

@nhynes can you act on the review comments?

@nhynes
Copy link
Member Author

nhynes commented Jun 26, 2018

@tqchen @kevinthesun @nishi-t How can I get this PR to work with cuda? The issue is supporting no-op collapse. identity is eltwise which is unsupported after reduce and no-reduce-axis reduce ops also don't work since AssertionError: reduce_axis must be bigger than zero!. What's worse is that trying to simplify the identity out leads to /tvm/src/lang/ir.cc:23: Reduce do not work with old Visitor, use IRFunctor style visitor

How should I proceed? This PR fixes several bugs with reduce, so it'd be a shame to see it go to waste.

@tqchen
Copy link
Member

tqchen commented Jun 26, 2018

The easiest way might be to patch schedule_reduce to support no-reduce-axis case, and call schedule_injective in that case, which is always safe

@tqchen tqchen merged commit 195c734 into apache:master Jun 27, 2018
@nhynes nhynes deleted the bc-grads branch June 27, 2018 02:11
tqchen pushed a commit to tqchen/tvm that referenced this pull request Jul 6, 2018
mnuyens pushed a commit to mnuyens/tvm that referenced this pull request Jul 10, 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.

None yet

4 participants