Skip to content

[Paddle-TRT]TensorRT support isnan_v2 #66658

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

Merged
merged 5 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3618,6 +3618,7 @@ USE_TRT_CONVERTER(assign)
USE_TRT_CONVERTER(p_norm)
USE_TRT_CONVERTER(unbind)
USE_TRT_CONVERTER(flip)
USE_TRT_CONVERTER(isnan_v2)
USE_TRT_CONVERTER(share_data)
#if IS_TRT_VERSION_GE(8522)
USE_TRT_CONVERTER(flash_multihead_matmul)
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 @@ -111,6 +111,7 @@ list(
temporal_shift_op.cc
einsum_op.cc
unbind_op.cc
isnan_v2_op.cc
assign_op.cc
p_norm_op.cc
flip_op.cc
Expand Down
54 changes: 54 additions & 0 deletions paddle/fluid/inference/tensorrt/convert/isnan_v2_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* Copyright (c) 2024 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 "paddle/fluid/inference/tensorrt/convert/op_converter.h"

namespace paddle {
namespace inference {
namespace tensorrt {

/*
* IsnanV2 Op
*/
class IsnanV2OpConverter : public OpConverter {
public:
void operator()(const framework::proto::OpDesc& op,
const framework::Scope& scope,
bool test_mode) override {
VLOG(3) << "convert a isnan_v2 op to tensorrt layer";
framework::OpDesc op_desc(op, nullptr);
std::string input_x_name = op_desc.Input("X").front();
std::string output_name = op_desc.Output("Out").front();
auto* input_x_tensor = engine_->GetITensor(input_x_name);
#if IS_TRT_VERSION_GE(10100)
auto* layer = TRT_ENGINE_ADD_LAYER(
engine_, Unary, *input_x_tensor, nvinfer1::UnaryOperation::kISNAN);
ReplenishLayerAndOutput(layer, "isnan_v2", {output_name}, test_mode);
#else
auto* equal_layer =
TRT_ENGINE_ADD_LAYER(engine_,
ElementWise,
*input_x_tensor,
*input_x_tensor,
nvinfer1::ElementWiseOperation::kEQUAL);
auto* layer = TRT_ENGINE_ADD_LAYER(engine_,
Unary,
*equal_layer->getOutput(0),
nvinfer1::UnaryOperation::kNOT);
ReplenishLayerAndOutput(layer, "isnan_v2", {output_name}, test_mode);
#endif
}
};

} // namespace tensorrt
} // namespace inference
} // namespace paddle
REGISTER_TRT_OP_CONVERTER(isnan_v2, IsnanV2OpConverter);
17 changes: 17 additions & 0 deletions paddle/fluid/inference/tensorrt/op_teller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2793,6 +2793,21 @@ struct SimpleOpTypeSetTeller : public Teller {
}
}

if (op_type == "isnan_v2") {
if (!with_dynamic_shape) {
VLOG(3) << "the isnan_v2 does not support "
"static shape yet";
return false;
}
auto* block = desc.Block();
if (block == nullptr) {
VLOG(3) << "The block desc is nullptr, we can't continue to analyze. "
"Developers need to check whether block_desc is passed in "
"the pass.";
return false;
}
}

if (op_type == "p_norm") {
auto* block = desc.Block();
if (block == nullptr) {
Expand Down Expand Up @@ -3077,6 +3092,7 @@ struct SimpleOpTypeSetTeller : public Teller {
"grid_sampler",
"cumsum",
"unbind",
"isnan_v2",
"p_norm",
"assign",
"flip",
Expand Down Expand Up @@ -3250,6 +3266,7 @@ struct SimpleOpTypeSetTeller : public Teller {
"grid_sampler",
"cumsum",
"unbind",
"isnan_v2",
"p_norm",
"assign",
"flip",
Expand Down
164 changes: 164 additions & 0 deletions test/ir/inference/test_trt_convert_isnan_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# Copyright (c) 2024 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 os
import unittest
from functools import partial
from typing import List

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

import paddle.inference as paddle_infer


class TrtConvertIsnanV2Test(TrtLayerAutoScanTest):
def sample_program_configs(self):
def generate_input1(dims):
if dims == 1:
data = np.random.random([3]).astype(np.float32)
mask = np.random.random([3]).astype(np.float32) < 0.3
data[mask] = np.nan
return data
elif dims == 2:
data = np.random.random([3, 64]).astype(np.float32)
mask = np.random.random([3, 64]).astype(np.float32) < 0.3
data[mask] = np.nan
return data
elif dims == 3:
data = np.random.random([3, 64, 64]).astype(np.float32)
mask = np.random.random([3, 64, 64]).astype(np.float32) < 0.3
data[mask] = np.nan
return data
else:
data = np.random.random([1, 3, 64, 64]).astype(np.float32)
mask = np.random.random([1, 3, 64, 64]).astype(np.float32) < 0.3
data[mask] = np.nan
return data

for dims in [1, 2, 3, 4]:
self.dims = dims
ops_config = [
{
"op_type": "isnan_v2",
"op_inputs": {
"X": ["input_data"],
},
"op_outputs": {
"Out": ["isnan_v2_output_data"],
},
"op_attrs": {},
},
{
"op_type": "cast",
"op_inputs": {"X": ["isnan_v2_output_data"]},
"op_outputs": {"Out": ["output_data"]},
"op_attrs": {
"in_dtype": 0,
"out_dtype": 5,
},
},
]
ops = self.generate_op_config(ops_config)

program_config = ProgramConfig(
ops=ops,
weights={},
inputs={
"input_data": TensorConfig(
data_gen=partial(generate_input1, dims)
)
},
outputs=["output_data"],
)

yield program_config

def sample_predictor_configs(
self, program_config
) -> (paddle_infer.Config, List[int], float):
def generate_dynamic_shape(attrs):
if self.dims == 1:
self.dynamic_shape.min_input_shape = {"input_data": [1]}
self.dynamic_shape.max_input_shape = {"input_data": [128]}
self.dynamic_shape.opt_input_shape = {"input_data": [64]}
elif self.dims == 2:
self.dynamic_shape.min_input_shape = {"input_data": [1, 32]}
self.dynamic_shape.max_input_shape = {"input_data": [4, 64]}
self.dynamic_shape.opt_input_shape = {"input_data": [3, 64]}
elif self.dims == 3:
self.dynamic_shape.min_input_shape = {"input_data": [1, 32, 32]}
self.dynamic_shape.max_input_shape = {
"input_data": [10, 64, 64]
}
self.dynamic_shape.opt_input_shape = {"input_data": [3, 64, 64]}
else:
self.dynamic_shape.min_input_shape = {
"input_data": [1, 3, 32, 32]
}
self.dynamic_shape.max_input_shape = {
"input_data": [4, 3, 64, 64]
}
self.dynamic_shape.opt_input_shape = {
"input_data": [1, 3, 64, 64]
}

def generate_trt_nodes_num(attrs, dynamic_shape):
if not dynamic_shape:
return 0, 4
return 1, 2

def clear_dynamic_shape():
self.dynamic_shape.max_input_shape = {}
self.dynamic_shape.min_input_shape = {}
self.dynamic_shape.opt_input_shape = {}

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
program_config.set_input_type(np.float32)
yield self.create_inference_config(), generate_trt_nodes_num(
attrs, False
), 1e-5
self.trt_param.precision = paddle_infer.PrecisionType.Half
program_config.set_input_type(np.float16)
yield self.create_inference_config(), generate_trt_nodes_num(
attrs, False
), (1e-3, 1e-3)

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

def test(self):
if os.name != 'nt':
self.run_test()


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