Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Paddle Inference] Add add eye trt converter #48937

Closed
wants to merge 17 commits into from
1 change: 1 addition & 0 deletions paddle/fluid/inference/api/analysis_predictor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2303,6 +2303,7 @@ USE_TRT_CONVERTER(one_hot);
USE_TRT_CONVERTER(one_hot_v2);
USE_TRT_CONVERTER(swish);
USE_TRT_CONVERTER(silu);
USE_TRT_CONVERTER(eye);
USE_TRT_CONVERTER(group_norm);
USE_TRT_CONVERTER(instance_norm);
USE_TRT_CONVERTER(layer_norm);
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/tensorrt/convert/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ list(
silu_op.cc
instance_norm_op.cc
stack_op.cc
eye_op.cc
transpose_op.cc
flatten_op.cc
flatten_contiguous_range_op.cc
Expand Down
110 changes: 110 additions & 0 deletions paddle/fluid/inference/tensorrt/convert/eye_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed 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 <algorithm>
#include <iostream>
#include <iterator>

#include "paddle/fluid/inference/tensorrt/convert/op_converter.h"

namespace paddle {
namespace framework {
class Scope;

namespace proto {
class OpDesc;
} // namespace proto
} // namespace framework
} // namespace paddle

namespace paddle {
namespace inference {
namespace tensorrt {

/*
* EyeOp.
*/
class EyeOpConverter : public OpConverter {
public:
void operator()(const framework::proto::OpDesc& op,
const framework::Scope& scope,
bool test_mode) override {
VLOG(3) << "convert a fluid eye op with tensorrt Constant layer";
framework::OpDesc op_desc(op, nullptr);

// Declare inputs attr
const int num_rows = PADDLE_GET_CONST(int, op_desc.GetAttr("num_rows"));
int num_columns = PADDLE_GET_CONST(int, op_desc.GetAttr("num_columns"));
const int dtype = PADDLE_GET_CONST(int, op_desc.GetAttr("dtype"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以改成

    auto dtype = static_cast<framework::proto::VarType::Type>(
        PADDLE_GET_CONST(int, op_desc.GetAttr("dtype")));


// Set data dim
nvinfer1::Dims input_shape;
input_shape.nbDims = 2;
if (-1 == num_columns) {
num_columns = num_rows;
}
input_shape.d[0] = num_rows;
input_shape.d[1] = num_columns;
const int data_len = num_rows * num_columns;
const int num_min = std::min(num_rows, num_columns);

// Set data type
void* trt_data = nullptr;
nvinfer1::DataType nv_type = nvinfer1::DataType::kFLOAT;
if (2 == dtype) {
nv_type = nvinfer1::DataType::kINT32;
std::unique_ptr<int32_t[]> data(new int32_t[data_len]());
for (int i = 0; i < num_min; i++) {
data[i * num_columns + i] = 1;
}
trt_data = static_cast<void*>(data.get());
} else if (4 == dtype) {
nv_type = nvinfer1::DataType::kHALF;
std::unique_ptr<float16[]> data(new float16[data_len]());
for (int i = 0; i < num_min; i++) {
data[i * num_columns + i] = 1;
}
trt_data = static_cast<void*>(data.get());
} else if (5 == dtype) {
nv_type = nvinfer1::DataType::kFLOAT;
std::unique_ptr<float[]> data(new float[data_len]());
for (int i = 0; i < num_min; i++) {
data[i * num_columns + i] = 1;
}
trt_data = static_cast<void*>(data.get());
} else {
paddle::platform::errors::InvalidArgument(
"Paddle-TRT loads weighths failed, found not supported data type "
"%s.",
dtype);
}

auto* layer =
TRT_ENGINE_ADD_LAYER(engine_,
Constant,
input_shape,
nvinfer1::Weights{nv_type, trt_data, data_len});

std::string output_name = op_desc.Output("Out").front();

RreplenishLayerAndOutput(layer, "eye", {output_name}, test_mode);
}
};

} // namespace tensorrt
} // namespace inference
} // namespace paddle

USE_OP_ITSELF(eye);
REGISTER_TRT_OP_CONVERTER(eye, EyeOpConverter);
2 changes: 2 additions & 0 deletions paddle/fluid/inference/tensorrt/op_teller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2492,6 +2492,7 @@ struct SimpleOpTypeSetTeller : public Teller {
"silu",
"celu",
"split",
"eye",
"instance_norm",
"gelu",
"layer_norm",
Expand Down Expand Up @@ -2635,6 +2636,7 @@ struct SimpleOpTypeSetTeller : public Teller {
"silu",
"celu",
"split",
"eye",
"instance_norm",
"gelu",
"layer_norm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,20 +185,20 @@ def generate_op_config(
if 'outputs_dtype' in op_config:
ops.append(
OpConfig(
type=op_config['op_type'],
inputs=op_config['op_inputs'],
outputs=op_config['op_outputs'],
attrs=op_config['op_attrs'],
outputs_dtype=op_config['outputs_dtype'],
type=op_config.get('op_type', {}),
inputs=op_config.get('op_inputs', {}),
outputs=op_config.get('op_outputs', {}),
attrs=op_config.get('op_attrs', {}),
outputs_dtype=op_config.get('outputs_dtype', {}),
)
)
else:
ops.append(
OpConfig(
type=op_config['op_type'],
inputs=op_config['op_inputs'],
outputs=op_config['op_outputs'],
attrs=op_config['op_attrs'],
type=op_config.get('op_type', {}),
inputs=op_config.get('op_inputs', {}),
outputs=op_config.get('op_outputs', {}),
attrs=op_config.get('op_attrs', {}),
)
)
return ops
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

#
# Licensed 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.

import unittest
from typing import List, Tuple

import numpy as np
from program_config import ProgramConfig
from trt_layer_auto_scan_test import TrtLayerAutoScanTest

import paddle.inference as paddle_infer


class TrtConvertLeakyEyeTest(TrtLayerAutoScanTest):
def is_program_valid(self, program_config: ProgramConfig) -> bool:
return True

def sample_program_configs(self):
def generate_input_attr():
return np.random.randint(1, 320, size=2)
# if np.random.random() > 0.5:
# return np.random.randint(1, 320, size=2)
# return np.random.randint(1, 320), -1

for dtype in [2, 4, 5]:
for _ in range(6):

num_rows, num_columns = generate_input_attr()
attr_dic = {
"num_rows": num_rows,
"num_columns": num_columns,
"dtype": dtype,
}

ops_config = [
{
"op_type": "eye",
"op_outputs": {
"Out": ["out_data"],
},
"op_attrs": attr_dic,
}
]
ops = self.generate_op_config(ops_config)
program_config = ProgramConfig(
ops=ops,
weights={},
inputs={},
outputs=["out_data"],
)

yield program_config

def sample_predictor_configs(
self, program_config
) -> Tuple[paddle_infer.Config, List[int], float]:
def generate_dynamic_shape(attrs):
pass

def clear_dynamic_shape():
pass

def generate_trt_nodes_num(attrs, dynamic_shape):
return 1, 2

attrs = [
program_config.ops[i].attrs for i in range(len(program_config.ops))
]

# for static_shape
clear_dynamic_shape()
self.trt_param.precision = paddle_infer.PrecisionType.Float32
yield self.create_inference_config(), generate_trt_nodes_num(
attrs, False
), 1e-5
self.trt_param.precision = paddle_infer.PrecisionType.Half
yield self.create_inference_config(), generate_trt_nodes_num(
attrs, False
), 1e-3

# for dynamic_shape
generate_dynamic_shape(attrs)
self.trt_param.precision = paddle_infer.PrecisionType.Float32
yield self.create_inference_config(), generate_trt_nodes_num(
attrs, True
), 1e-5
self.trt_param.precision = paddle_infer.PrecisionType.Half
yield self.create_inference_config(), generate_trt_nodes_num(
attrs, True
), 1e-3

def test(self):
self.run_test()


if __name__ == "__main__":
unittest.main()