[GitHub] eric-haibin-lin commented on a change in pull request #7698: Second order gradient and Subgraph execution

2017-09-23 Thread git
eric-haibin-lin commented on a change in pull request #7698: Second order 
gradient and Subgraph execution
URL: https://github.com/apache/incubator-mxnet/pull/7698#discussion_r140639796
 
 

 ##
 File path: src/imperative/cached_op.cc
 ##
 @@ -0,0 +1,463 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include 
+#include 
+#include "./imperative_utils.h"
+
+namespace mxnet {
+
+Imperative::CachedOp::CachedOp(const nnvm::Symbol& sym) {
+  using namespace nnvm;
+  using namespace imperative;
+  static const std::vector zero_ops{Op::Get("zeros_like"), 
Op::Get("_zeros")};
+  static const auto _copy = Op::Get("_copy");
+
+  // construct forward graph
+  {
+NodeEntryMap dedup_out;
+for (const auto& i : sym.outputs) {
+  if (dedup_out.count(i)) {
+NodePtr copy_node = Node::Create();
+copy_node->attrs.op = _copy;
+copy_node->attrs.name =
+i.node->attrs.name + "_copy" + std::to_string(dedup_out[i]++);
+copy_node->inputs.emplace_back(i);
+if (_copy->attr_parser != nullptr) {
+  _copy->attr_parser(&(copy_node->attrs));
+}
+fwd_graph_.outputs.push_back(NodeEntry{copy_node, 0, 0});
+  } else {
+dedup_out.insert({i, 0});
+fwd_graph_.outputs.push_back(i);
+  }
+}
+const auto& idx = fwd_graph_.indexed_graph();
+CHECK_GE(idx.input_nodes().size(), 1) << "CachedOp requires at least 1 
input";
+
+std::vector ref_count(idx.num_node_entries(), 0);
+for (const auto& i : idx.input_nodes()) ++ref_count[idx.entry_id(i, 0)];
+for (const auto& i : idx.outputs()) ++ref_count[idx.entry_id(i)];
+for (size_t i = 0; i < idx.num_nodes(); ++i) {
+  for (const auto& j : idx[i].inputs) ++ref_count[idx.entry_id(j)];
+}
+
+fwd_graph_.attrs["forward_ref_count"] =
+std::make_shared(std::move(ref_count));
+  }
+
+  // construct backward graph
+  std::vector ograd_entries;
+  {
+ograd_entries.reserve(fwd_graph_.outputs.size());
+for (size_t i = 0; i < fwd_graph_.outputs.size(); ++i) {
+  ograd_entries.emplace_back(NodeEntry{Node::Create(), 0, 0});
+}
+
+std::vector xs;
+std::vector args = sym.ListInputs(Symbol::kReadOnlyArgs);
+xs.reserve(args.size());
+for (const auto& i : args) xs.emplace_back(NodeEntry{i, 0, 0});
+CHECK_GT(xs.size(), 0)
+<< "There are no inputs in computation graph that require gradients.";
+
+grad_graph_ = pass::Gradient(
+fwd_graph_, fwd_graph_.outputs, xs, ograd_entries,
+exec::AggregateGradient, nullptr, nullptr,
+zero_ops, "_copy");
+  }
+
+  // construct full graph
+  {
+size_t num_forward_nodes = fwd_graph_.indexed_graph().num_nodes();
+size_t num_forward_entries = fwd_graph_.indexed_graph().num_node_entries();
+
+full_graph_.outputs = fwd_graph_.outputs;
+curr_grad_req_ = std::vector(grad_graph_.outputs.size(), true);
+for (const auto& i : grad_graph_.outputs) 
full_graph_.outputs.emplace_back(i);
+const auto& idx = full_graph_.indexed_graph();
+
+std::vector ref_count(idx.num_node_entries(), 0);
+for (size_t i = num_forward_nodes; i < idx.num_nodes(); ++i) {
+  for (const auto& j : idx[i].inputs) {
+ ++ref_count[idx.entry_id(j)];
+  }
+}
+
+auto full_ref_count = fwd_graph_.GetAttr("forward_ref_count");
+for (size_t i = 0; i < num_forward_entries; ++i) full_ref_count[i] += 
ref_count[i];
+fwd_graph_.attrs["full_ref_count"] =
+std::make_shared(std::move(full_ref_count));
+
+size_t num_forward_inputs = num_inputs();
+for (uint32_t i = 0; i < ograd_entries.size(); ++i) {
+  if (!idx.exist(ograd_entries[i].node.get())) continue;
+  auto eid = idx.entry_id(ograd_entries[i]);
+  if (ref_count[eid] > 0) {
+bwd_ograd_dep_.push_back(i);
+bwd_input_eid_.push_back(eid);
+  }
+}
+save_inputs_.resize(num_forward_inputs, false);
+for (uint32_t i = 0; i < num_forward_inputs; ++i) {
+  auto eid = idx.entry_id(idx.input_nodes()[i], 0);
+  if (ref_count[eid] > 0) {
+save_inputs_[i] = true;
+bwd_in_dep_.push_back(i);
+bwd_input_eid_.push_back(eid);
+  }
+  

[GitHub] eric-haibin-lin commented on a change in pull request #7698: Second order gradient and Subgraph execution

2017-09-17 Thread git
eric-haibin-lin commented on a change in pull request #7698: Second order 
gradient and Subgraph execution
URL: https://github.com/apache/incubator-mxnet/pull/7698#discussion_r139303370
 
 

 ##
 File path: src/c_api/c_api_ndarray.cc
 ##
 @@ -798,32 +341,58 @@ int MXAutogradBackward(mx_uint num_output,
NDArrayHandle *output_handles,
NDArrayHandle *ograd_handles,
int retain_graph) {
-  return MXAutogradBackwardEx(num_output, output_handles, ograd_handles, 
retain_graph, true);
+  return MXAutogradBackwardEx(num_output, output_handles, ograd_handles,
+  0, nullptr, retain_graph, false, true,
+  nullptr, nullptr);
 }
 
 int MXAutogradBackwardEx(mx_uint num_output,
  NDArrayHandle *output_handles,
  NDArrayHandle *ograd_handles,
+ mx_uint num_variables,
+ NDArrayHandle *var_handles,
  int retain_graph,
- int is_train) {
+ int create_graph,
+ int is_train,
+ NDArrayHandle **grad_handles,
+ int **grad_stypes) {
+  MXAPIThreadLocalEntry *ret = MXAPIThreadLocalStore::Get();
   API_BEGIN();
 
-  std::vector outputs, ograds;
+  std::vector outputs, ograds, variables;
   outputs.reserve(num_output);
   for (mx_uint i = 0; i < num_output; ++i) {
-outputs.emplace_back(*static_cast(output_handles[i]));
+outputs.emplace_back(reinterpret_cast(output_handles[i]));
   }
 
   ograds.reserve(num_output);
   for (mx_uint i = 0; i < num_output; ++i) {
-if (ograd_handles != nullptr && ograd_handles[i] != nullptr) {
-  ograds.emplace_back(*static_cast(ograd_handles[i]));
+if (ograd_handles != nullptr) {
+  ograds.emplace_back(reinterpret_cast(ograd_handles[i]));
 } else {
-  ograds.emplace_back();
+  ograds.emplace_back(nullptr);
 
 Review comment:
   `ograds.emplace_back();` always generates dense ograd. Does `nullptr` 
respect the storage type inferred from the graph? 
   
 

This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


[GitHub] eric-haibin-lin commented on a change in pull request #7698: Second order gradient and Subgraph execution

2017-09-15 Thread git
eric-haibin-lin commented on a change in pull request #7698: Second order 
gradient and Subgraph execution
URL: https://github.com/apache/incubator-mxnet/pull/7698#discussion_r139200011
 
 

 ##
 File path: src/operator/tensor/elemwise_binary_op_basic.cu
 ##
 @@ -36,21 +36,21 @@ NNVM_REGISTER_OP(_backward_add)
   ElemwiseBinaryOp::BackwardUseNoneWithHalf2);
 
-NNVM_REGISTER_OP(_sub)
+NNVM_REGISTER_OP(elemwise_sub)
 .set_attr("FCompute", ElemwiseBinaryOp::ComputeWithHalf2);
 
 NNVM_REGISTER_OP(_backward_sub)
 .set_attr("FCompute", 
ElemwiseBinaryOp::BackwardUseNoneWithHalf2<
   gpu, mshadow_op::identity, mshadow_op::negation>);
 
-NNVM_REGISTER_OP(_mul)
+NNVM_REGISTER_OP(elemwise_mul)
 .set_attr("FCompute", ElemwiseBinaryOp::ComputeWithHalf2);
 
 NNVM_REGISTER_OP(_backward_mul)
 .set_attr("FCompute",
   ElemwiseBinaryOp::BackwardUseInWithHalf2);
 
-NNVM_REGISTER_OP(_div)
+NNVM_REGISTER_OP(elemwise_div)
 
 Review comment:
   Doesn't this break backward compatibility? @cjolivier01 @piiswrong 
 

This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


[GitHub] eric-haibin-lin commented on a change in pull request #7698: Second order gradient and Subgraph execution

2017-09-15 Thread git
eric-haibin-lin commented on a change in pull request #7698: Second order 
gradient and Subgraph execution
URL: https://github.com/apache/incubator-mxnet/pull/7698#discussion_r139199432
 
 

 ##
 File path: tests/python/unittest/test_autograd.py
 ##
 @@ -117,7 +117,7 @@ def check_unary_func(x):
 f_square_grad = lambda x: [2*x]
 autograd_assert(x, func=f_square, grad_func=f_square_grad)
 uniform = nd.uniform(shape=(4, 5))
-stypes = ['row_sparse', 'csr', 'default']
+stypes = ['default']  #,'row_sparse', 'csr']
 
 Review comment:
   ok
 

This is an automated message from the Apache Git Service.
To respond to the message, please log on 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