kshitij12345 commented on a change in pull request #15480: [MXNET-978] Higher Order Gradient Support `elemwise_mul`, `elemwise_add`. URL: https://github.com/apache/incubator-mxnet/pull/15480#discussion_r300922295
########## File path: src/operator/tensor/elemwise_binary_op_basic.cc ########## @@ -251,7 +251,31 @@ NNVM_REGISTER_OP(_backward_mul) .set_attr<FCompute>("FCompute<cpu>", ElemwiseBinaryOp::BackwardUseIn< cpu, mshadow_op::right, mshadow_op::left>) .set_attr<FComputeEx>("FComputeEx<cpu>", ElemwiseBinaryOp::BackwardUseInEx< - cpu, mshadow_op::right, mshadow_op::left>); + cpu, mshadow_op::right, mshadow_op::left>) +.set_attr<nnvm::FGradient>("FGradient", + [](const nnvm::NodePtr& n, const std::vector<nnvm::NodeEntry>& ograds) { + // z = x * y + // NodeEntry{n, 0, 0} : z_grad * y + // NodeEntry{n, 1, 0} : z_grad * x + // n->inputs[0] : z_grad + // n->inputs[1] : x + // n->inputs[1] : y + // ograds[0] : head_grads + // f(x, y) = x * y + // dx = z_grad * y, dy = z_grad * x + // d2x = 0, d2y = 0, dz_grad = dx + dy = y + x + auto dz_grad = MakeNode("elemwise_add", n->attrs.name + "_y_add_x", + {n->inputs[2], n->inputs[1]}, nullptr, &n); + + std::vector<nnvm::NodeEntry> ret; + ret.emplace_back(MakeNode("elemwise_mul", n->attrs.name + "_backward_grad_grad", + {ograds[0], nnvm::NodeEntry{dz_grad}}, nullptr, &n)); + ret.emplace_back(MakeNode("zeros_like", n->attrs.name + "_backward_grad_grad_x", Review comment: Ah. Right. Thank You. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services