apeforest commented on a change in pull request #16654: Multithreaded Inference 
Support
URL: https://github.com/apache/incubator-mxnet/pull/16654#discussion_r366631849
 
 

 ##########
 File path: src/imperative/cached_op_threadsafe.cc
 ##########
 @@ -0,0 +1,329 @@
+/*
+ * 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 <unordered_set>
+#include <iostream>
+#include "./imperative_utils.h"
+#include "../executor/exec_pass.h"
+#include "./cached_op_threadsafe.h"
+#include "../profiler/profiler.h"
+#include "../operator/operator_common.h"
+#include "../operator/subgraph/common.h"
+
+namespace mxnet {
+
+DMLC_REGISTER_PARAMETER(CachedOpThreadSafeConfig);
+
+constexpr uint32_t kEidNotExist = std::numeric_limits<uint32_t>::max();
+
+
+struct CachedOpThreadSafe::GraphInfo {
+  nnvm::Graph fwd_graph;
+};
+
+struct CachedOpThreadSafe::DynamicRuntime {
+  GraphInfo info;
+  std::vector<OpStatePtr> op_states;
+};
+
+OpStatePtr CachedOpThreadSafe::GetCachedOpState(
+    const Context& ctx) {
+
+  for (const auto& i : cached_op_states_[ctx]) {
+    // only create one state per device when not using static memory
+    if (!config_.static_alloc || i.unique()) {
+      return i;
+    }
+  }
+  nnvm::Graph full_graph;
+  auto state_ptr = OpStatePtr::Create<CachedOpState>(ctx, fwd_graph_, 
full_graph, false);
+
+  cached_op_states_[ctx].push_back(state_ptr);
+  return state_ptr;
+}
+
+
+CachedOpThreadSafe::CachedOpThreadSafe(const nnvm::Symbol& sym,
+                                       const std::vector<std::pair<std::string,
+                                       std::string> >& flags) : CachedOp(sym, 
flags) {
+  using namespace nnvm;
+  using namespace imperative;
+  static const std::vector<const Op *> zero_ops{Op::Get("zeros_like"),
+                                                Op::Get("_zeros")};
+  config_.Init(flags);
+
+  if (config_.static_shape) {
+      CHECK(config_.static_alloc) << "static_alloc must be True when 
static_shape is True";
+  }
+
+  // construct forward graph
+  CreateForwardGraph(sym.Copy(), &fwd_graph_);
+  SetForwardRefCounts(&fwd_graph_);
+
+  SetInputIndices(fwd_graph_, config_.param_indices,
+                  &config_.data_indices);
+}
+
+/*
+ * \brief Thread safe version of DynamicForward, with thread local buffer
+ * used to store intermediate nodes in the graph
+ */
+OpStatePtr CachedOpThreadSafe::DynamicForward(const Context& default_ctx,
+                                              const std::vector<NDArray*>& 
inputs,
+                                              const std::vector<NDArray*>& 
outputs) {
+  using namespace nnvm;
+  using namespace imperative;
+
+  {
+  auto state_ptr = GetCachedOpState(default_ctx);
 
 Review comment:
   indent? Also, better to refactor into a function.

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