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_r253288413
########## 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)) Review comment: Can be simplified to ```C++ return isTRTCompatible(new_node); ``` ---------------------------------------------------------------- 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