leezu commented on a change in pull request #17530: Add deferred compute support URL: https://github.com/apache/incubator-mxnet/pull/17530#discussion_r384156913
########## 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: `&input` here points to an array in `info.inputs_`, which is `std::vector<NDArray> inputs_`. Ie. it contains proper NDArrays and not only pointers to NDArrays that could become invalid. We control the lifetime of the NDArrays in `std::vector<NDArray> inputs_`. They are copies, created based on the ndarray handle passed when invoking an operator and created in in `DCInfo::Create`. The copies are destroyed here in `DCInfo::Compute` after computation finished. Note that the copies share the same underlying storage chunk. The storage chunk is deallocated once the frontend doesn't hold any handle to an NDArray owning that chunk anymore and once all copies located in DCInfo.inputs_ are destroyed. This is done by `std::shared_ptr`. ---------------------------------------------------------------- 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