KellenSunderland commented on a change in pull request #14040: Reformat of 
TensorRT to use subgraph API
URL: https://github.com/apache/incubator-mxnet/pull/14040#discussion_r253288427
 
 

 ##########
 File path: src/operator/subgraph/tensorrt/tensorrt-inl.h
 ##########
 @@ -0,0 +1,217 @@
+#ifndef MXNET_OPERATOR_SUBGRAPH_TENSORRT_TENSORRT_INL_H_
+#define MXNET_OPERATOR_SUBGRAPH_TENSORRT_TENSORRT_INL_H_
+/*
+ * 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.
+ */
+
+/*!
+ * Copyright (c) 2018 by Contributors
+ * \file tensorrt-inl.h
+ * \brief TensorRT operation registration
+ * \author Marek Kolodziej, Clement Fuji Tsang
+*/
+
+#if MXNET_USE_TENSORRT
+
+
+#include "../common.h"
+#include "../subgraph_property.h"
+#include "nnvm_to_onnx-inl.h"
+#include "./onnx_to_tensorrt.h"
+
+namespace mxnet {
+namespace op {
+
+using int64 = ::google::protobuf::int64;
+
+struct TRTParam {
+  std::unordered_map<std::string, uint32_t> inputs_to_idx;
+  std::unordered_map<std::string, uint32_t> outputs_to_idx;
+  std::unordered_map<std::string, NDArray> params_map;
+};
+
+struct TRTEngineParam {
+  nvinfer1::IExecutionContext* trt_executor = nullptr;
+  std::vector<std::pair<uint32_t, bool> > binding_vec;
+};
+
+
+class TensorrtSelector : public SubgraphSelector {
+ public:
+  const std::unordered_set<std::string> unconditionalTRTops = {
+    "Convolution",
+    "BatchNorm",
+    "elemwise_add",
+    "elemwise_sub",
+    "elemwise_mul",
+    "rsqrt",
+    "pad",
+    "Pad",
+    "mean",
+    "FullyConnected",
+    "Flatten",
+    "SoftmaxOutput",
+  };
+
+  const std::unordered_set<std::string> withWeightsOps = {
+    "Convolution",
+    "BatchNorm",
+    "FullyConnected"
+  };
+
+  bool isTRTCompatible(const nnvm::Node &n) {
+    const std::string op_name = n.op()->name;
+    if (op_name == "Pooling") {
+      return (n.attrs.dict.at("pool_type") == "avg" ||
+          n.attrs.dict.at("pool_type") == "max");
+    }
+
+    if (unconditionalTRTops.count(op_name)) {
+      return true;
+    }
+
+    if (op_name == "Activation") {
+      return n.attrs.dict.at("act_type") == "relu" ||
+        n.attrs.dict.at("act_type") == "tanh" ||
+        n.attrs.dict.at("act_type") == "sigmoid";
+    }
+
+    return false;
+  }
+
+  bool Select(const nnvm::Node &n) override {
+    return !n.is_variable() && isTRTCompatible(n);
+  }
+
+  bool SelectInput(const nnvm::Node &n, const nnvm::Node &new_node) override {
+    if (new_node.is_variable()) {
+      if (withWeightsOps.count(n.op()->name)) {
+        return n.inputs[0].node->attrs.name != new_node.attrs.name;
+      } else {
+        return false;
+      }
+    }
+    if (isTRTCompatible(new_node))
+      return true;
+    return false;
+  }
+
+  bool SelectOutput(const nnvm::Node &n, const nnvm::Node &new_node) override {
+   return isTRTCompatible(new_node);
+  }
+
+  std::vector<nnvm::Node*> Filter(const std::vector<nnvm::Node*>& candidates) 
override {
+    bool found_one = false;
+    // TensorRT is interesting with at least 2 operations
+    for (auto& n : candidates) {
+      if (!n->is_variable()) {
+        if (found_one) {
+          return candidates;
+        } else {
+          found_one = true;
+        }
+      }
+    }
+    return std::vector<nnvm::Node*>();
+  }
+};
+
+class TensorrtProperty : public SubgraphProperty {
+ public:
+  static SubgraphPropertyPtr Create() {
+    return std::make_shared<TensorrtProperty>();
+  }
+
+  nnvm::NodePtr CreateSubgraphNode(const nnvm::Symbol &sym,
+                                   const int subgraph_id = 0) const override {
+    nnvm::NodePtr n = nnvm::Node::Create();
+    nnvm::Symbol new_sym;
+    std::unique_copy(sym.outputs.begin(), sym.outputs.end(),
+        std::back_inserter(new_sym.outputs), [](
+        nnvm::NodeEntry lhs, nnvm::NodeEntry rhs) {
+          return lhs.index == rhs.index && lhs.node.get() == rhs.node.get();
+        });
+    n->attrs.name = "TensorRT" + std::to_string(subgraph_id);
+    n->attrs.op = Op::Get("_TensorRT");
+    CHECK(n->attrs.op);
+    n->attrs.subgraphs.emplace_back(std::make_shared<nnvm::Symbol>(new_sym));
+    std::ostringstream params_oss;
+    for (auto &e : new_sym.ListInputNames(nnvm::Symbol::kAll)) {
+      params_oss << e << ";";
+    }
+    auto tensorrt_params_names = params_oss.str();
+    tensorrt_params_names.pop_back();
+    n->attrs.dict["subgraph_params_names"] = tensorrt_params_names;
+    TRTParam param;
+    n->attrs.parsed = param;
+    n->op()->attr_parser(&(n->attrs));
+    return n;
+  }
+
+  SubgraphSelectorPtr CreateSubgraphSelector() const override {
+    return std::make_shared<TensorrtSelector>();
+  }
+
+  void ConnectSubgraphOutputs(const nnvm::NodePtr subgraph_node, \
+                              std::vector<nnvm::NodeEntry*>* output_entries) 
const override {
+    std::vector<nnvm::NodeEntry>& outputs = 
subgraph_node->attrs.subgraphs[0]->outputs;
+    TRTParam& _params = nnvm::get<TRTParam>(subgraph_node->attrs.parsed);
+    for (int i = 0; i < outputs.size(); i++) {
+      auto& o = outputs[i];
+      for (auto& e : *output_entries) {
+        if (o.index == e->index && o.node.get() == e->node.get()) {
+          e->index = i;
+          e->node = subgraph_node;
+          // TODO(cfujitsang): For futur support this would fail
 
 Review comment:
   futur -> future.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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