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

 ##########
 File path: src/imperative/cached_op_threadsafe.h
 ##########
 @@ -0,0 +1,129 @@
+/*
+ * 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.
+ */
+
+// Threadsafe and minimal functionality cached op version for Inference
+// lot of code reused from cached_op.h
+#ifndef MXNET_IMPERATIVE_CACHED_OP_THREADSAFE_H_
+#define MXNET_IMPERATIVE_CACHED_OP_THREADSAFE_H_
+
+#include <mxnet/imperative.h>
+#include <vector>
+#include <atomic>
+#include <utility>
+#include <string>
+#include <unordered_map>
+#include "./cached_op.h"
+
+
+
+namespace mxnet {
+/*! \brief CachedOp Parameters*/
+struct CachedOpThreadSafeConfig
+    : public dmlc::Parameter<CachedOpThreadSafeConfig> {
+  // keeping the config minimal
+  // inlining, bulking, dynamic shapes, static allocing and shaping not
+  // supported
+  // data_indices indicates which of the indices from the arguments are data
+  mxnet::Tuple<uint32_t> data_indices;
+  // param_indices indicates which of the indices from the arguments are params
+  mxnet::Tuple<uint32_t> param_indices;
+  bool static_alloc;
+  bool static_shape;
+  DMLC_DECLARE_PARAMETER(CachedOpThreadSafeConfig) {
+    DMLC_DECLARE_FIELD(static_alloc)
+    .set_default(false)
+    .describe("Statically allocate memory to improve speed. "
+              "Memory usage may increase.");
+    DMLC_DECLARE_FIELD(static_shape)
+    .set_default(false)
+    .describe("Optimize for invariant input shapes between iterations. "
+              "Must also set static_alloc to True. "
+              "Change of input shapes is still allowed but slower.");
+    DMLC_DECLARE_FIELD(data_indices)
+        .set_default(mxnet::Tuple<uint32_t>())
+        .describe("Position of argument variables.");
+            DMLC_DECLARE_FIELD(param_indices)
+        .set_default(mxnet::Tuple<uint32_t>())
+        .describe("Position of parameters.");
+  }
+};
+
+// Thread local buff to store internal states of the graph
+// Used in dynamic_forward
+#if DMLC_CXX11_THREAD_LOCAL
+    static thread_local std::vector<NDArray> buff;
+#else
+    static MX_THREAD_LOCAL std::vector<NDArray> buff;
+#endif
+
+
+
+class CachedOpThreadSafe : public CachedOp {
 
 Review comment:
   Maybe I missed something, but why can't we modify the CachedOp to make it 
thread safe rather than creating a new inherited class? In which scenarios 
users want to choose a thread unsafe CachedOp and is there any guideline 
somewhere? 

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