zhreshold commented on a change in pull request #17530: Add deferred compute 
support
URL: https://github.com/apache/incubator-mxnet/pull/17530#discussion_r384191115
 
 

 ##########
 File path: src/imperative/imperative.cc
 ##########
 @@ -544,4 +705,60 @@ std::vector<NDArray*> Imperative::Backward(
   return {};
 }
 
+Imperative::DCInfo::DCInfo(const std::vector<NDArray *> &inputs,
+                           const std::vector<NDArray *> &outputs) {
+  this->inputs_.reserve(inputs.size());
+  this->input_handles_.reserve(inputs.size());
+  for (const NDArray *arr : inputs) {
+    CHECK(!arr->is_none());
+    this->inputs_.push_back(*arr);
+    this->input_handles_.push_back(arr);
+  }
+
+  this->outputs_.reserve(outputs.size());
+  for (const NDArray *arr : outputs) {
+    CHECK(!arr->is_none());
+    this->outputs_.push_back(*arr);
+  }
+}
+
+Imperative::DCInfo &
+Imperative::DCInfo::Create(const nnvm::ObjectPtr &node,
+                           const std::vector<NDArray *> &inputs,
+                           const std::vector<NDArray *> &outputs) {
+  node->info.construct<DCInfo>(inputs, outputs);
+  return Imperative::DCInfo::Get(node);
+}
+
+void Imperative::DCInfo::Compute(const NDArray &arr) {
+  if (Imperative::DCInfo::IsComputed(arr))
+    return;
+
+  DCInfo &info = Imperative::DCInfo::Get(arr.deferredcompute_entry_.node);
+  info.is_computed_ = true;  // We will Invoke at the end of this function.
+
+  // Recursively compute input arrays
+  for (const NDArray &input : info.inputs_) {
+    Compute(input);
+  }
+
+  // Prepare pointers
+  std::vector<NDArray *> ndinputs, ndoutputs;
+  ndinputs.reserve(info.inputs_.size());
+  ndoutputs.reserve(info.outputs_.size());
+  for (NDArray &input : info.inputs_)
+    ndinputs.push_back(&input);
 
 Review comment:
   thx

----------------------------------------------------------------
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

Reply via email to