-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cherry pick] add cast trt convert (#44837)
* add cast trt convert * skip cast trt convert when input dtype is bool * code format * fix bug * update unittest * fix bug
- Loading branch information
Showing
5 changed files
with
317 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,66 @@ | ||
# Add TRT tests | ||
nv_library(tensorrt_converter | ||
SRCS matmul_op.cc conv2d_op.cc fc_op.cc pool2d_op.cc elementwise_op.cc | ||
batch_norm_op.cc activation_op.cc softmax_op.cc concat_op.cc dropout_op.cc group_norm_op.cc | ||
pad_op.cc split_op.cc prelu_op.cc leaky_relu_op.cc gelu_op.cc layer_norm_op.cc multihead_matmul_op.cc | ||
shuffle_channel_op.cc swish_op.cc instance_norm_op.cc stack_op.cc transpose_op.cc flatten_op.cc flatten_contiguous_range_op.cc | ||
emb_eltwise_layernorm.cc skip_layernorm.cc scale_op.cc slice_op.cc hard_sigmoid_op.cc hard_swish_op.cc clip_op.cc | ||
gather_op.cc | ||
bilinear_interp_v2_op.cc | ||
anchor_generator_op.cc | ||
yolo_box_op.cc | ||
roi_align_op.cc | ||
affine_channel_op.cc | ||
multiclass_nms_op.cc | ||
multiclass_nms3_op.cc | ||
nearest_interp_op.cc | ||
reshape_op.cc | ||
reduce_op.cc | ||
gather_nd_op.cc | ||
tile_op.cc | ||
conv3d_op.cc | ||
mish_op.cc | ||
nearest_interp_v2_op.cc | ||
pool3d_op.cc | ||
deformable_conv_op.cc | ||
preln_emb_eltwise_layernorm.cc | ||
strided_slice_op.cc | ||
preln_skip_layernorm.cc | ||
roll_op.cc | ||
DEPS tensorrt_engine tensorrt_plugin operator scope framework_proto op_registry) | ||
nv_library( | ||
tensorrt_converter | ||
SRCS matmul_op.cc | ||
conv2d_op.cc | ||
fc_op.cc | ||
pool2d_op.cc | ||
elementwise_op.cc | ||
batch_norm_op.cc | ||
activation_op.cc | ||
softmax_op.cc | ||
concat_op.cc | ||
dropout_op.cc | ||
group_norm_op.cc | ||
pad_op.cc | ||
split_op.cc | ||
prelu_op.cc | ||
leaky_relu_op.cc | ||
gelu_op.cc | ||
layer_norm_op.cc | ||
multihead_matmul_op.cc | ||
shuffle_channel_op.cc | ||
swish_op.cc | ||
instance_norm_op.cc | ||
stack_op.cc | ||
transpose_op.cc | ||
flatten_op.cc | ||
flatten_contiguous_range_op.cc | ||
emb_eltwise_layernorm.cc | ||
skip_layernorm.cc | ||
scale_op.cc | ||
slice_op.cc | ||
hard_sigmoid_op.cc | ||
hard_swish_op.cc | ||
clip_op.cc | ||
gather_op.cc | ||
bilinear_interp_v2_op.cc | ||
cast_op.cc | ||
anchor_generator_op.cc | ||
yolo_box_op.cc | ||
roi_align_op.cc | ||
affine_channel_op.cc | ||
multiclass_nms_op.cc | ||
multiclass_nms3_op.cc | ||
nearest_interp_op.cc | ||
reshape_op.cc | ||
reduce_op.cc | ||
gather_nd_op.cc | ||
tile_op.cc | ||
conv3d_op.cc | ||
mish_op.cc | ||
nearest_interp_v2_op.cc | ||
pool3d_op.cc | ||
deformable_conv_op.cc | ||
preln_emb_eltwise_layernorm.cc | ||
strided_slice_op.cc | ||
preln_skip_layernorm.cc | ||
roll_op.cc | ||
DEPS tensorrt_engine tensorrt_plugin operator scope framework_proto | ||
op_registry) | ||
|
||
nv_test(test_op_converter SRCS test_op_converter.cc DEPS | ||
paddle_framework ${GLOB_OPERATOR_DEPS} tensorrt_engine tensorrt_converter) | ||
nv_test( | ||
test_op_converter | ||
SRCS test_op_converter.cc | ||
DEPS paddle_framework ${GLOB_OPERATOR_DEPS} tensorrt_engine | ||
tensorrt_converter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* Copyright (c) 2022 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 framework { | ||
class Scope; | ||
|
||
namespace proto { | ||
class OpDesc; | ||
} // namespace proto | ||
} // namespace framework | ||
} // namespace paddle | ||
|
||
namespace paddle { | ||
namespace inference { | ||
namespace tensorrt { | ||
|
||
class CastOpConverter : public OpConverter { | ||
public: | ||
void operator()(const framework::proto::OpDesc& op, | ||
const framework::Scope& scope, | ||
bool test_mode) override { | ||
VLOG(3) << "convert a cast op to tensorrt"; | ||
framework::OpDesc op_desc(op, nullptr); | ||
|
||
auto* input = engine_->GetITensor(op_desc.Input("X")[0]); | ||
auto out_dtype = BOOST_GET_CONST(int, op_desc.GetAttr("out_dtype")); | ||
|
||
auto* layer = TRT_ENGINE_ADD_LAYER(engine_, Identity, *input); | ||
|
||
switch (out_dtype) { | ||
case 2: // INT32 = 2 | ||
layer->getOutput(0)->setType(nvinfer1::DataType::kINT32); | ||
break; | ||
case 4: // FP16 = 4 | ||
layer->getOutput(0)->setType(nvinfer1::DataType::kHALF); | ||
break; | ||
case 5: // FP32 = 5 | ||
layer->getOutput(0)->setType(nvinfer1::DataType::kFLOAT); | ||
break; | ||
default: | ||
LOG(ERROR) << "Unable to convert a fluid data type(" << out_dtype | ||
<< ") to a nvinfer DataType"; | ||
break; | ||
} | ||
|
||
auto output_name = op_desc.Output("Out")[0]; | ||
RreplenishLayerAndOutput(layer, "cast", {output_name}, test_mode); | ||
} | ||
}; | ||
|
||
} // namespace tensorrt | ||
} // namespace inference | ||
} // namespace paddle | ||
|
||
REGISTER_TRT_OP_CONVERTER(cast, CastOpConverter); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.