apeforest commented on a change in pull request #14836: Refactor AGInfo and 
Imperative
URL: https://github.com/apache/incubator-mxnet/pull/14836#discussion_r305026489
 
 

 ##########
 File path: src/imperative/imperative.cc
 ##########
 @@ -316,181 +312,223 @@ std::vector<NDArray*> Imperative::Backward(
       info.outputs.back() = static_cast<real_t>(1.0);
     }
   }
+  return ograd_entries;
+}
 
-  // Get gradient graph
-  Symbol sym;
-  sym.outputs = graph.outputs;
-  std::vector<NodeEntry> xs;
-  std::vector<NDArray*> x_grads;
-  std::vector<OpReqType> x_reqs;
-  if (variables.size()) {
-    xs.reserve(variables.size());
-    x_grads.reserve(variables.size());
-    x_reqs.reserve(variables.size());
+struct Imperative::GradientVariableNodes {
+  std::vector<nnvm::NodeEntry> variable_nodes;
+  std::vector<NDArray*> gradients;
+  std::vector<OpReqType> op_req_types;
+};
+
+Imperative::GradientVariableNodes Imperative::CreateGradientVariableNodes(
+    const std::vector<NDArray *> &variables,
+    const std::vector<nnvm::NodeEntry> &outputs) {
+  GradientVariableNodes var_nodes;
+  if (!variables.empty()) {
+    var_nodes.variable_nodes.reserve(variables.size());
+    var_nodes.gradients.reserve(variables.size());
+    var_nodes.op_req_types.reserve(variables.size());
     for (size_t i = 0; i < variables.size(); ++i) {
       CHECK(!AGInfo::IsNone(*variables[i]) &&
             AGInfo::IsVariable(variables[i]->entry_.node))
           << "Cannot differentiate with respect to the " << i+1 << "-th 
variable"
           << " because it does not require gradient.";
-      xs.emplace_back(variables[i]->entry_);
-      x_grads.push_back(new NDArray());
-      x_reqs.push_back(kWriteTo);
+      var_nodes.variable_nodes.emplace_back(variables[i]->entry_);
+      var_nodes.gradients.push_back(new NDArray());
+      var_nodes.op_req_types.push_back(kWriteTo);
     }
   } else {
-    std::vector<NodePtr> args = sym.ListInputs(Symbol::kReadOnlyArgs);
-    xs.reserve(args.size());
-    x_grads.reserve(args.size());
-    x_reqs.reserve(args.size());
-    for (const auto& i : args) {
-      AGInfo& info = AGInfo::Get(i);
-      if (info.grad_req == kNullOp) continue;
-      xs.emplace_back(NodeEntry{i, 0, 0});
-      x_grads.push_back(&info.out_grads[0]);
-      x_reqs.push_back(info.grad_req);
-      info.fresh_out_grad = true;
+    nnvm::Symbol s;
+    s.outputs = outputs;
+    std::vector<nnvm::NodePtr> input_ro_nodes = 
s.ListInputs(Symbol::kReadOnlyArgs);
+    var_nodes.variable_nodes.reserve(input_ro_nodes.size());
+    var_nodes.gradients.reserve(input_ro_nodes.size());
+    var_nodes.op_req_types.reserve(input_ro_nodes.size());
+    for (const auto& node : input_ro_nodes) {
+      AGInfo& info = AGInfo::Get(node);
+      if (info.grad_req != kNullOp) {
+        var_nodes.variable_nodes.emplace_back(node);
+        var_nodes.gradients.push_back(&info.out_grads[0]);
+        var_nodes.op_req_types.push_back(info.grad_req);
+        info.fresh_out_grad = true;
+      }
     }
-    CHECK_GT(xs.size(), 0)
+    CHECK_GT(var_nodes.variable_nodes.size(), 0)
         << "There are no inputs in computation graph that require gradients.";
   }
+  return var_nodes;
+}
+
+
 
-  Graph g_graph = pass::MXGradient(
-      graph, graph.outputs, xs, ograd_entries,
+std::vector<NDArray*> Imperative::Backward(
+    const std::vector<NDArray*>& outputs,
+    const std::vector<NDArray*>& ograds,
+    const std::vector<NDArray*>& variables,
+    bool is_train, bool retain_graph,
+    bool create_graph) {
+  using namespace nnvm;
 
 Review comment:
   This namespace is redundant since in this function nnvm:: is still used
   

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to