[MSLITE] fix bugs and code check

This commit is contained in:
Liu_Xuu 2021-07-30 17:07:47 +08:00
parent 4e8b34749a
commit 58e740a48a
26 changed files with 227 additions and 74 deletions

View File

@ -21,6 +21,10 @@ namespace mindspore::lite {
int ActivationTensorRT::IsSupport(const schema::Primitive *primitive,
const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
if (in_tensors.size() != 1) {
MS_LOG(ERROR) << "Unsupported input tensor size, size is " << in_tensors.size();
return RET_ERROR;
@ -62,6 +66,7 @@ int ActivationTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
}
activation_layer->setName(op_name_.c_str());
activation_layer->getOutput(0)->setName(out_tensors_[0].Name().c_str());
this->AddInnerOutTensors(activation_layer->getOutput(0));
return RET_OK;

View File

@ -20,6 +20,10 @@
namespace mindspore::lite {
int ConcateTensorRT::IsSupport(const schema::Primitive *primitive, const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
if (in_tensors.size() < 1) {
MS_LOG(ERROR) << "Unsupported input tensor size, size is " << in_tensors.size();
return RET_ERROR;
@ -41,7 +45,6 @@ int ConcateTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
MS_LOG(ERROR) << "concate_op convert failed";
return RET_ERROR;
}
MS_LOG(INFO) << "in tensort size of concate: " << tensorrt_in_tensors_.size();
if (tensorrt_in_tensors_.size() != in_tensors_.size()) {
MS_LOG(ERROR) << "concate_op in tensor is invalid";
return RET_ERROR;
@ -64,6 +67,7 @@ int ConcateTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
concate_layer->setAxis(axis);
}
concate_layer->setName(op_name_.c_str());
concate_layer->getOutput(0)->setName(out_tensors_[0].Name().c_str());
this->AddInnerOutTensors(concate_layer->getOutput(0));
return RET_OK;

View File

@ -24,6 +24,10 @@ constexpr int BIAS_INDEX = 2;
int ConvolutionTensorRT::IsSupport(const schema::Primitive *primitive,
const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
if (in_tensors.size() != 2 && in_tensors.size() != 3) {
MS_LOG(ERROR) << "Unsupported input tensor size, size is " << in_tensors.size();
return RET_ERROR;
@ -53,8 +57,12 @@ int ConvolutionTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
}
transpose_layer_in->setName((op_name_ + "_transpose2NCHW").c_str());
// transpose weight
const mindspore::MSTensor &weight_tensor = in_tensors_[1];
nvinfer1::Weights kernelWeights = lite::TransposeWeight(weight_tensor, &pack_weight_);
// conv
int nbOutputMaps = conv_op->out_channel();
int nbOutputMaps = weight_tensor.Shape()[0];
if (nbOutputMaps <= 0) {
MS_LOG(ERROR) << "out_channel is invalid";
return RET_ERROR;
@ -67,9 +75,6 @@ int ConvolutionTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
}
nvinfer1::Dims kernelSize = lite::ConvertCudaDims(std::vector<int64_t>(kernel_size->begin(), kernel_size->end()));
// transpose weight
nvinfer1::Weights kernelWeights = lite::TransposeWeight(in_tensors_[1], &pack_weight_);
// bias
nvinfer1::Weights biasWeights{};
if (in_tensors_.size() >= INPUT_SIZE3) {
@ -113,7 +118,7 @@ int ConvolutionTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
return RET_ERROR;
}
transpose_layer_out->setName((op_name_ + "_transpose2NHWC").c_str());
transpose_layer_out->getOutput(0)->setName(out_tensors_[0].Name().c_str());
this->AddInnerOutTensors(transpose_layer_out->getOutput(0));
return RET_OK;
}

View File

@ -23,6 +23,10 @@ namespace mindspore::lite {
int DeconvolutionTensorRT::IsSupport(const schema::Primitive *primitive,
const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
if (in_tensors.size() != 2 && in_tensors.size() != 3) {
MS_LOG(ERROR) << "Unsupported input tensor size, size is " << in_tensors.size();
return RET_ERROR;
@ -51,8 +55,12 @@ int DeconvolutionTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
}
transpose_layer_in->setName((op_name_ + "_transpose2NCHW").c_str());
// transpose weight
const mindspore::MSTensor &weight_tensor = in_tensors_[1];
nvinfer1::Weights kernelWeights = lite::TransposeWeight(weight_tensor, &pack_weight_);
// deconv basic params
int nbOutputMaps = deconv_op->out_channel();
int nbOutputMaps = weight_tensor.Shape()[0];
if (nbOutputMaps <= 0) {
MS_LOG(ERROR) << "out_channel is invalid";
return RET_ERROR;
@ -65,9 +73,6 @@ int DeconvolutionTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
}
nvinfer1::Dims kernelSize = lite::ConvertCudaDims(std::vector<int64_t>(kernel_size->begin(), kernel_size->end()));
// transpose weight
nvinfer1::Weights kernelWeights = lite::TransposeWeight(in_tensors_[1], &pack_weight_);
// bias
nvinfer1::Weights biasWeights{};
if (in_tensors_.size() >= 3) {
@ -111,7 +116,7 @@ int DeconvolutionTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
return RET_ERROR;
}
transpose_layer_out->setName((op_name_ + "_transpose2NHWC").c_str());
transpose_layer_out->getOutput(0)->setName(out_tensors_[0].Name().c_str());
this->AddInnerOutTensors(transpose_layer_out->getOutput(0));
return RET_OK;
}

View File

@ -21,6 +21,10 @@ namespace mindspore::lite {
int ElementWiseTensorRT::IsSupport(const schema::Primitive *primitive,
const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
std::map<schema::PrimitiveType, nvinfer1::ElementWiseOperation> element_wise_ops = {
{schema::PrimitiveType_AddFusion, nvinfer1::ElementWiseOperation::kSUM},
{schema::PrimitiveType_PowFusion, nvinfer1::ElementWiseOperation::kPOW},
@ -61,6 +65,13 @@ int ElementWiseTensorRT::IsSupport(const schema::Primitive *primitive,
MS_LOG(ERROR) << "invalid output tensort size: " << out_tensors.size();
return RET_ERROR;
}
// if constant tensor is scalar, it needs to know another input tensor's shape to broadcast
if (in_tensors[0].Shape()[0] == -1 && in_tensors[1].Shape().size() == 0) {
MS_LOG(ERROR) << "invalid all input tensor shape unknown for: " << op_name_;
return RET_ERROR;
}
return RET_OK;
}
@ -69,23 +80,25 @@ int ElementWiseTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
MS_LOG(ERROR) << "network or input tensor size is invalid";
return RET_ERROR;
}
// create ITensor from MS scalar
if (this->in_tensors_[1].Shape().size() == 0) {
nvinfer1::ITensor *scalar_input =
lite::ConvertScalarToITensor(network, this->in_tensors_[0].Shape().size(), this->in_tensors_[1].MutableData());
if (scalar_input == nullptr) {
MS_LOG(ERROR) << "create Itensor from scalar failed";
return RET_ERROR;
}
this->AddInnerInTensors(scalar_input);
}
first_in_tensor_index_ = strcmp(tensorrt_in_tensors_[0]->getName(), in_tensors_[0].Name().c_str()) == 0 ? 0 : 1;
// add elementwise
if (this->tensorrt_in_tensors_.size() != 2) {
MS_LOG(ERROR) << "invalid inner in tensors cnt: " << this->tensorrt_in_tensors_.size();
return RET_ERROR;
// create ITensor from MS constant tensor of index 1 - first_in_tensor_index_
nvinfer1::ITensor *constant_input = nullptr;
if (this->in_tensors_[1 - first_in_tensor_index_].Shape().size() == 0) {
constant_input = lite::ConvertScalarToITensor(network, this->in_tensors_[first_in_tensor_index_].Shape().size(),
in_tensors_[1 - first_in_tensor_index_].Data().get());
} else {
constant_input = lite::ConvertConstantTensor(network, in_tensors_[1 - first_in_tensor_index_]);
}
if (constant_input == nullptr) {
MS_LOG(ERROR) << "create Itensor from constant tensor failed: " << op_name_;
return RET_ERROR;
}
this->AddInnerInTensors(constant_input);
}
nvinfer1::IElementWiseLayer *cal_layer =
network->addElementWise(*tensorrt_in_tensors_[0], *tensorrt_in_tensors_[1], element_wise_op_);
nvinfer1::IElementWiseLayer *cal_layer = network->addElementWise(
*tensorrt_in_tensors_[first_in_tensor_index_], *tensorrt_in_tensors_[1 - first_in_tensor_index_], element_wise_op_);
if (cal_layer == nullptr) {
MS_LOG(ERROR) << "addElementWise failed for TensorRT.";

View File

@ -35,8 +35,12 @@ class ElementWiseTensorRT : public TensorRTOp {
const std::vector<mindspore::MSTensor> &out_tensors) override;
private:
nvinfer1::ElementWiseOperation element_wise_op_;
nvinfer1::ITensor *AddActivation(nvinfer1::INetworkDefinition *network, nvinfer1::ITensor *in_tensor);
nvinfer1::ElementWiseOperation element_wise_op_;
// index of first input MSTensor in the trt input tensor vector
size_t first_in_tensor_index_ = 0;
};
} // namespace mindspore::lite
#endif // MINDSPORE_LITE_SRC_DELEGATE_TENSORRT_OP_ELEMENTWISE_TENSORRT_H_

View File

@ -22,6 +22,10 @@ constexpr int AXIS_INDEX = 2;
int GatherTensorRT::IsSupport(const schema::Primitive *primitive, const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
if (in_tensors.size() != 3) {
MS_LOG(ERROR) << "invalid input tensor size: " << in_tensors.size();
return RET_ERROR;
@ -61,6 +65,7 @@ int GatherTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
return RET_ERROR;
}
gather_layer->setName(op_name_.c_str());
gather_layer->getOutput(0)->setName(out_tensors_[0].Name().c_str());
this->AddInnerOutTensors(gather_layer->getOutput(0));
return RET_OK;
}

View File

@ -22,6 +22,10 @@ constexpr int BIAS_INDEX = 2;
int MatMulTensorRT::IsSupport(const mindspore::schema::Primitive *primitive,
const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
if (in_tensors.size() != 2 && in_tensors.size() != 3) {
MS_LOG(ERROR) << "Unsupported input tensor size, size is " << in_tensors.size();
return RET_ERROR;
@ -41,16 +45,18 @@ int MatMulTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
auto matmul_layer = network->addMatrixMultiply(*tensorrt_in_tensors_[0], transpose_a_, *weight, transpose_b_);
matmul_layer->setName(op_name_.c_str());
nvinfer1::ITensor *out_tensor = matmul_layer->getOutput(0);
if (in_tensors_.size() == 3) {
if (in_tensors_.size() == BIAS_INDEX + 1) {
auto bias = ConvertTensorWithExpandDims(network, in_tensors_[BIAS_INDEX], in_tensors_[0].Shape().size());
auto bias_layer = network->addElementWise(*matmul_layer->getOutput(0), *bias, nvinfer1::ElementWiseOperation::kSUM);
auto bias_layer_name = op_name_ + "_bias";
bias_layer->setName(bias_layer_name.c_str());
this->AddInnerOutTensors(bias_layer->getOutput(0));
} else {
this->AddInnerOutTensors(matmul_layer->getOutput(0));
out_tensor = bias_layer->getOutput(0);
}
out_tensor->setName(out_tensors_[0].Name().c_str());
this->AddInnerOutTensors(out_tensor);
return RET_OK;
}
} // namespace mindspore::lite

View File

@ -23,6 +23,10 @@ namespace mindspore::lite {
int PadTensorRT::IsSupport(const mindspore::schema::Primitive *primitive,
const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
if (in_tensors.size() != 2 && in_tensors.size() != 3) {
MS_LOG(ERROR) << "Unsupported input tensor size, size is " << in_tensors.size();
return RET_ERROR;
@ -98,6 +102,7 @@ int PadTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
return RET_ERROR;
}
transpose_layer_out->setName((op_name_ + "_transpose2NHWC").c_str());
transpose_layer_out->getOutput(0)->setName(out_tensors_[0].Name().c_str());
this->AddInnerOutTensors(transpose_layer_out->getOutput(0));
return RET_OK;

View File

@ -22,6 +22,10 @@ namespace mindspore::lite {
int PoolTensorRT::IsSupport(const mindspore::schema::Primitive *primitive,
const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
if (in_tensors.size() != 1) {
MS_LOG(ERROR) << "Unsupported input tensor size, size is " << in_tensors.size();
return RET_ERROR;
@ -89,6 +93,7 @@ int PoolTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
return RET_ERROR;
}
transpose_layer_out->setName((op_name_ + "_transpose2NHWC").c_str());
transpose_layer_out->getOutput(0)->setName(out_tensors_[0].Name().c_str());
this->AddInnerOutTensors(transpose_layer_out->getOutput(0));
return RET_OK;
}

View File

@ -19,6 +19,10 @@
namespace mindspore::lite {
int ReduceTensorRT::IsSupport(const schema::Primitive *primitive, const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
auto reduce_op = primitive->value_as_ReduceFusion();
if (reduce_op == nullptr) {
MS_LOG(ERROR) << "convert failed";

View File

@ -26,6 +26,10 @@ constexpr int POWER_INDEX = 3;
int ScaleTensorRT::IsSupport(const schema::Primitive *primitive, const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
if (in_tensors.size() != 2 && in_tensors.size() != 3 && in_tensors.size() != 4) {
MS_LOG(ERROR) << "Unsupported input tensor size, size is: " << in_tensors.size();
return RET_ERROR;

View File

@ -19,6 +19,10 @@
namespace mindspore::lite {
int ShapeTensorRT::IsSupport(const schema::Primitive *primitive, const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
if (in_tensors.size() != 1) {
MS_LOG(ERROR) << "Unsupported input tensor size, size is " << in_tensors.size();
return RET_ERROR;
@ -41,6 +45,7 @@ int ShapeTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
return RET_ERROR;
}
shape_layer->setName(op_name_.c_str());
shape_layer->getOutput(0)->setName(out_tensors_[0].Name().c_str());
this->AddInnerOutTensors(shape_layer->getOutput(0));
return RET_OK;
}

View File

@ -16,19 +16,49 @@
#include "src/delegate/tensorrt/op/shuffle_tensorrt.h"
#include <vector>
#include <numeric>
#include <functional>
namespace mindspore::lite {
int ShuffleTensorRT::IsSupport(const schema::Primitive *primitive, const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if ((type_ == schema::PrimitiveType::PrimitiveType_Squeeze ||
type_ == schema::PrimitiveType::PrimitiveType_Unsqueeze) &&
in_tensors.size() != 1) {
MS_LOG(ERROR) << "invalid input tensort size: " << in_tensors.size();
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
if ((type_ == schema::PrimitiveType::PrimitiveType_Transpose) && in_tensors.size() != 2) {
MS_LOG(ERROR) << "invalid input tensort size: " << in_tensors.size();
return RET_ERROR;
switch (type_) {
case schema::PrimitiveType_Flatten:
case schema::PrimitiveType_Squeeze:
case schema::PrimitiveType_Unsqueeze: {
if (in_tensors.size() != 1) {
MS_LOG(ERROR) << "Unsupported in_tensors size " << in_tensors.size() << " of "
<< schema::EnumNamePrimitiveType(type_);
return RET_ERROR;
}
break;
}
case schema::PrimitiveType_Reshape: {
if (in_tensors.size() != 2) {
MS_LOG(ERROR) << "PrimitiveType_Transpose Unsupported in_tensors size: " << in_tensors.size();
return RET_ERROR;
}
break;
}
case schema::PrimitiveType_Transpose: {
if (in_tensors.size() != 2) {
MS_LOG(ERROR) << "PrimitiveType_Transpose Unsupported in_tensors size: " << in_tensors.size();
return RET_ERROR;
}
if (in_tensors[1].Data() == nullptr) {
MS_LOG(ERROR) << "Unsupported shape tensor of " << schema::EnumNamePrimitiveType(type_);
return RET_ERROR;
}
break;
}
default: {
MS_LOG(ERROR) << "Unsupported op type:" << schema::EnumNamePrimitiveType(type_);
return RET_ERROR;
}
}
if (out_tensors.size() != 1) {
MS_LOG(ERROR) << "invalid output tensort size: " << out_tensors.size();
@ -49,7 +79,7 @@ int ShuffleTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
}
shuffle_layer->setName(op_name_.c_str());
switch (this->type()) {
switch (type_) {
case schema::PrimitiveType_Unsqueeze: {
int ret = AddUnsqueezeOp(shuffle_layer);
if (ret != RET_OK) {
@ -82,6 +112,14 @@ int ShuffleTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
}
break;
}
case schema::PrimitiveType_Flatten: {
int ret = AddFlattenOp(shuffle_layer);
if (ret != RET_OK) {
MS_LOG(ERROR) << "AddFlattenOp failed.";
return ret;
}
break;
}
default:
MS_LOG(ERROR) << "Unsupported op type.";
return RET_ERROR;
@ -148,7 +186,6 @@ int ShuffleTensorRT::AddUnsqueezeOp(nvinfer1::IShuffleLayer *shuffle_layer) {
}
nvinfer1::Dims unsqueeze_dims = lite::ConvertCudaDims(unsqueeze_shape);
MS_LOG(INFO) << "AddUnsqueezeOp: " << op_name_ << " unsqueeze_dims.nbDims: " << unsqueeze_dims.nbDims;
shuffle_layer->setReshapeDimensions(unsqueeze_dims);
return shuffle_layer->getOutput(0) == nullptr ? RET_ERROR : RET_OK;
@ -166,8 +203,8 @@ int ShuffleTensorRT::AddTransposeOp(nvinfer1::IShuffleLayer *shuffle_layer) {
}
// perm
mindspore::MSTensor perm_ternsor = in_tensors_[1];
if (perm_ternsor.Data() == nullptr || perm_ternsor.ElementNum() != tensorrt_in_tensors_[0]->getDimensions().nbDims) {
MS_LOG(ERROR) << "AddTransposeOp perm_ternsor data is invalid.";
if (perm_ternsor.Data() == nullptr) {
MS_LOG(ERROR) << "AddTransposeOp perm_ternsor data is invalid: " << op_name_;
return RET_ERROR;
}
int *perm_data = reinterpret_cast<int *>(perm_ternsor.MutableData());
@ -180,26 +217,38 @@ int ShuffleTensorRT::AddTransposeOp(nvinfer1::IShuffleLayer *shuffle_layer) {
shuffle_layer->setFirstTranspose(perm);
return RET_OK;
}
int ShuffleTensorRT::AddReshapeOp(nvinfer1::IShuffleLayer *shuffle_layer) {
auto reshape_op = this->op_primitive_->value_as_Reshape();
if (reshape_op == nullptr) {
MS_LOG(ERROR) << "AddReshapeOp convert failed";
return RET_ERROR;
}
if (in_tensors_.size() != 2) {
MS_LOG(ERROR) << "AddReshapeOp size of in tensort needs check: " << in_tensors_.size();
return RET_ERROR;
}
mindspore::MSTensor &shape_tensor = in_tensors_[1];
nvinfer1::Dims reshape_dims = ConvertCudaDims(shape_tensor.Data().get(), shape_tensor.ElementNum());
int ret = InferReshapeDims(tensorrt_in_tensors_[0]->getDimensions(), &reshape_dims);
if (ret != RET_OK) {
MS_LOG(ERROR) << "invalid dims for reshape " << op_name_;
return ret;
if (shape_tensor.Data() != nullptr) {
// static shuffle layer
nvinfer1::Dims reshape_dims = lite::ConvertCudaDims(shape_tensor.Data().get(), shape_tensor.ElementNum());
int ret = InferReshapeDims(tensorrt_in_tensors_[0]->getDimensions(), &reshape_dims);
if (ret != RET_OK) {
MS_LOG(ERROR) << "invalid dims for reshape " << op_name_;
return ret;
}
shuffle_layer->setReshapeDimensions(reshape_dims);
} else {
if (tensorrt_in_tensors_.size() != 2) {
MS_LOG(ERROR) << "invalid shape tensor for reshape " << op_name_;
return RET_ERROR;
}
shuffle_layer->setInput(1, *tensorrt_in_tensors_[1]);
}
shuffle_layer->setReshapeDimensions(reshape_dims);
return RET_OK;
}
int ShuffleTensorRT::AddFlattenOp(nvinfer1::IShuffleLayer *shuffle_layer) {
nvinfer1::Dims flatten_dims;
const std::vector<int64_t> &input_shape = in_tensors_[0].Shape();
flatten_dims.nbDims = 2;
flatten_dims.d[0] = input_shape[0];
flatten_dims.d[1] = std::accumulate(input_shape.begin() + 1, input_shape.end(), 1, std::multiplies<int>());
shuffle_layer->setReshapeDimensions(flatten_dims);
return RET_OK;
}
int ShuffleTensorRT::InferReshapeDims(nvinfer1::Dims input_dims, nvinfer1::Dims *reshape_dims) {
int infer_index = -1;
int known_cnt = 1;

View File

@ -39,6 +39,7 @@ class ShuffleTensorRT : public TensorRTOp {
int AddUnsqueezeOp(nvinfer1::IShuffleLayer *shuffle_layer);
int AddTransposeOp(nvinfer1::IShuffleLayer *shuffle_layer);
int AddReshapeOp(nvinfer1::IShuffleLayer *shuffle_layer);
int AddFlattenOp(nvinfer1::IShuffleLayer *shuffle_layer);
int InferReshapeDims(nvinfer1::Dims input_dims, nvinfer1::Dims *reshape_dims);
};
} // namespace mindspore::lite

View File

@ -21,7 +21,11 @@ namespace mindspore::lite {
int SliceTensorRT::IsSupport(const mindspore::schema::Primitive *primitive,
const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if (in_tensors.size() != 4 && in_tensors.size() != 5) {
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
if (in_tensors.size() < STRIDE_INDEX + 1) {
MS_LOG(ERROR) << "Unsupported input tensor size, size is " << in_tensors.size();
return RET_ERROR;
}
@ -29,8 +33,8 @@ int SliceTensorRT::IsSupport(const mindspore::schema::Primitive *primitive,
MS_LOG(ERROR) << "Unsupported output tensor size, size is " << out_tensors.size();
return RET_ERROR;
}
if (in_tensors_[1].Data() == nullptr) {
MS_LOG(ERROR) << "invalid pad tensor for: " << op_name_;
if (in_tensors_[BEGIN_INDEX].Data() == nullptr || in_tensors_[STRIDE_INDEX].Data() == nullptr) {
MS_LOG(ERROR) << "invalid pad or stride tensor for: " << op_name_;
return RET_ERROR;
}
return RET_OK;
@ -42,9 +46,8 @@ int SliceTensorRT::AddInnerOp(nvinfer1::INetworkDefinition *network) {
MS_LOG(ERROR) << "convert StridedSlice failed: " << op_name_;
return RET_ERROR;
}
const mindspore::MSTensor &begin = in_tensors_[1];
// mindspore::MSTensor &end = in_tensors_[2];
const mindspore::MSTensor &stride = in_tensors_[3];
const mindspore::MSTensor &begin = in_tensors_[BEGIN_INDEX];
const mindspore::MSTensor &stride = in_tensors_[STRIDE_INDEX];
nvinfer1::Dims start_dims = lite::ConvertCudaDims(begin.Data().get(), begin.ElementNum());
nvinfer1::Dims size_dims = lite::ConvertCudaDims(out_tensors_[0].Shape());

View File

@ -20,6 +20,8 @@
#include "src/delegate/tensorrt/op/tensorrt_op.h"
namespace mindspore::lite {
constexpr int BEGIN_INDEX = 1;
constexpr int STRIDE_INDEX = 3;
class SliceTensorRT : public TensorRTOp {
public:
SliceTensorRT(const schema::Primitive *primitive, const std::vector<mindspore::MSTensor> &in_tensors,

View File

@ -19,6 +19,10 @@
namespace mindspore::lite {
int SoftMaxTensorRT::IsSupport(const schema::Primitive *primitive, const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
if (primitive->value_type() == schema::PrimitiveType::PrimitiveType_LogSoftmax) {
with_log_ = true;
auto softmax_op = primitive->value_as_LogSoftmax();

View File

@ -42,4 +42,15 @@ void TensorRTOp::set_out_ops(const std::vector<TensorRTOp *> &out_ops) { this->o
const std::vector<TensorRTOp *> &TensorRTOp::in_ops() const { return this->in_ops_; }
const std::vector<TensorRTOp *> &TensorRTOp::out_ops() const { return this->out_ops_; }
bool TensorRTOp::IsShapeKnown() {
if (this->in_tensors_[0].Shape().size() == 0) {
return false;
} else {
if (this->in_tensors_[0].Shape()[0] == -1) {
return false;
}
}
return true;
}
} // namespace mindspore::lite

View File

@ -75,6 +75,8 @@ class TensorRTOp {
const std::vector<TensorRTOp *> &out_ops() const;
protected:
bool IsShapeKnown();
std::vector<nvinfer1::ILayer *> layers_;
const schema::Primitive *op_primitive_;

View File

@ -19,6 +19,10 @@
namespace mindspore::lite {
int UnaryTensorRT::IsSupport(const schema::Primitive *primitive, const std::vector<mindspore::MSTensor> &in_tensors,
const std::vector<mindspore::MSTensor> &out_tensors) {
if (!IsShapeKnown()) {
MS_LOG(ERROR) << "Unsupported input tensor unknown shape: " << op_name_;
return RET_ERROR;
}
if (in_tensors.size() != 1) {
MS_LOG(ERROR) << "Unsupported input tensor size, size is " << in_tensors.size();
}

View File

@ -69,9 +69,6 @@ int TensorRTDelegate::Init() {
op_func_lists_.clear();
op_func_lists_ = {
{schema::PrimitiveType_Activation, GetTensorRTOp<ActivationTensorRT>},
{schema::PrimitiveType_Unsqueeze, GetTensorRTOp<ShuffleTensorRT>},
{schema::PrimitiveType_Squeeze, GetTensorRTOp<ShuffleTensorRT>},
{schema::PrimitiveType_Reshape, GetTensorRTOp<ShuffleTensorRT>},
{schema::PrimitiveType_Concat, GetTensorRTOp<ConcateTensorRT>},
{schema::PrimitiveType_Conv2DFusion, GetTensorRTOp<ConvolutionTensorRT>},
{schema::PrimitiveType_Conv2dTransposeFusion, GetTensorRTOp<DeconvolutionTensorRT>},
@ -81,14 +78,20 @@ int TensorRTDelegate::Init() {
{schema::PrimitiveType_AddFusion, GetTensorRTOp<ElementWiseTensorRT>},
{schema::PrimitiveType_MulFusion, GetTensorRTOp<ElementWiseTensorRT>},
{schema::PrimitiveType_Eltwise, GetTensorRTOp<ElementWiseTensorRT>},
{schema::PrimitiveType_Transpose, GetTensorRTOp<ShuffleTensorRT>},
{schema::PrimitiveType_ReduceFusion, GetTensorRTOp<ReduceTensorRT>},
{schema::PrimitiveType_Sqrt, GetTensorRTOp<UnaryTensorRT>},
{schema::PrimitiveType_Gather, GetTensorRTOp<GatherTensorRT>},
{schema::PrimitiveType_MatMul, GetTensorRTOp<MatMulTensorRT>},
{schema::PrimitiveType_ScaleFusion, GetTensorRTOp<ScaleTensorRT>},
{schema::PrimitiveType_StridedSlice, GetTensorRTOp<SliceTensorRT>},
{schema::PrimitiveType_AvgPoolFusion, GetTensorRTOp<PoolTensorRT>},
{schema::PrimitiveType_PadFusion, GetTensorRTOp<PadTensorRT>},
{schema::PrimitiveType_ReduceFusion, GetTensorRTOp<ReduceTensorRT>},
{schema::PrimitiveType_ScaleFusion, GetTensorRTOp<ScaleTensorRT>},
{schema::PrimitiveType_StridedSlice, GetTensorRTOp<SliceTensorRT>},
{schema::PrimitiveType_Shape, GetTensorRTOp<ShapeTensorRT>},
{schema::PrimitiveType_Unsqueeze, GetTensorRTOp<ShuffleTensorRT>},
{schema::PrimitiveType_Squeeze, GetTensorRTOp<ShuffleTensorRT>},
{schema::PrimitiveType_Reshape, GetTensorRTOp<ShuffleTensorRT>},
{schema::PrimitiveType_Transpose, GetTensorRTOp<ShuffleTensorRT>},
{schema::PrimitiveType_Flatten, GetTensorRTOp<ShuffleTensorRT>},
{schema::PrimitiveType_Sqrt, GetTensorRTOp<UnaryTensorRT>},
};
return RET_OK;
}

View File

@ -158,6 +158,7 @@ int TensorRTSubGraph::BuildTensorRTGraph() {
return RET_ERROR;
}
trt_tensor = lite::ConvertConstantTensor(this->network_, in_tensor);
MS_LOG(INFO) << "auto convert constant tensor for: " << cur_op->GetOpName();
cur_op->AddInnerInTensors(trt_tensor);
}
} else {
@ -178,6 +179,7 @@ int TensorRTSubGraph::BuildTensorRTGraph() {
for (size_t index = 0; index < out_op->outputs().size(); index++) {
if (out_op->outputs()[index] == out_tensor) {
out_op->GetInnerOutTensor()[index]->setName(out_tensor.Name().c_str());
MS_LOG(INFO) << "markOutput for: " << out_tensor.Name();
this->network_->markOutput(*out_op->GetInnerOutTensor()[index]);
}
}

View File

@ -37,8 +37,10 @@ class TensorRTSubGraph : public kernel::Kernel {
trt_specific_weight_nodes_ = {
schema::PrimitiveType_Conv2DFusion, schema::PrimitiveType_ReduceFusion, schema::PrimitiveType_Transpose,
schema::PrimitiveType_Gather, schema::PrimitiveType_Reshape, schema::PrimitiveType_PowFusion,
schema::PrimitiveType_DivFusion, schema::PrimitiveType_MatMul, schema::PrimitiveType_ScaleFusion,
schema::PrimitiveType_MulFusion, schema::PrimitiveType_StridedSlice, schema::PrimitiveType_PadFusion};
schema::PrimitiveType_AddFusion, schema::PrimitiveType_DivFusion, schema::PrimitiveType_SubFusion,
schema::PrimitiveType_MatMul, schema::PrimitiveType_PowFusion, schema::PrimitiveType_Eltwise,
schema::PrimitiveType_ScaleFusion, schema::PrimitiveType_MulFusion, schema::PrimitiveType_StridedSlice,
schema::PrimitiveType_PadFusion};
}
~TensorRTSubGraph() override;

View File

@ -108,7 +108,7 @@ nvinfer1::ITensor *ConvertConstantTensor(nvinfer1::INetworkDefinition *network,
return constant_tensor->getOutput(0);
}
nvinfer1::ITensor *ConvertScalarToITensor(nvinfer1::INetworkDefinition *network, size_t shape_size, void *value) {
nvinfer1::ITensor *ConvertScalarToITensor(nvinfer1::INetworkDefinition *network, size_t shape_size, const void *value) {
nvinfer1::Dims dims = ConvertCudaDims(1, shape_size);
nvinfer1::Weights weights{nvinfer1::DataType::kFLOAT, value, 1};
nvinfer1::IConstantLayer *constant_tensor = network->addConstant(dims, weights);

View File

@ -51,7 +51,7 @@ nvinfer1::ITensor *ConvertConstantTensor(nvinfer1::INetworkDefinition *network,
nvinfer1::ITensor *ConvertTensorWithExpandDims(nvinfer1::INetworkDefinition *network,
const mindspore::MSTensor &ms_tensor, size_t expand_shape_size);
nvinfer1::ITensor *ConvertScalarToITensor(nvinfer1::INetworkDefinition *network, size_t shape_size, void *value);
nvinfer1::ITensor *ConvertScalarToITensor(nvinfer1::INetworkDefinition *network, size_t shape_size, const void *value);
nvinfer1::Weights TransposeWeight(const mindspore::MSTensor &ms_tensor, float **pack_weight);