-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add trt convert reshape_op in release/2.1.1 (#33372)
- Loading branch information
Showing
7 changed files
with
200 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* 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 "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 { | ||
|
||
/* | ||
* ReshapeOp | ||
*/ | ||
class ReshapeOpConverter : public OpConverter { | ||
public: | ||
void operator()(const framework::proto::OpDesc& op, | ||
const framework::Scope& scope, bool test_mode) override { | ||
framework::OpDesc op_desc(op, nullptr); | ||
// Declare inputs | ||
auto* input = engine_->GetITensor(op_desc.Input("X")[0]); | ||
const std::vector<int>& shape = | ||
BOOST_GET_CONST(std::vector<int>, op_desc.GetAttr("shape")); | ||
int nbDims_num = shape.size(); | ||
nvinfer1::Dims reshape_dim; | ||
if (engine_->with_dynamic_shape()) { // running the TRT Dynamic Shape mode | ||
reshape_dim.nbDims = nbDims_num; | ||
for (int i = 0; i < nbDims_num; ++i) { | ||
reshape_dim.d[i] = shape[i]; | ||
} | ||
} else { // running the TRT Static Shape mode | ||
reshape_dim.nbDims = nbDims_num - 1; | ||
for (int i = 0; i < nbDims_num - 1; ++i) { | ||
reshape_dim.d[i] = shape[i + 1]; | ||
} | ||
} | ||
auto* layer = TRT_ENGINE_ADD_LAYER(engine_, Shuffle, *input); | ||
layer->setReshapeDimensions(reshape_dim); | ||
auto output_name = op_desc.Output("Out")[0]; | ||
RreplenishLayerAndOutput(layer, "reshape", {output_name}, test_mode); | ||
} | ||
}; | ||
|
||
} // namespace tensorrt | ||
} // namespace inference | ||
} // namespace paddle | ||
|
||
REGISTER_TRT_OP_CONVERTER(reshape, ReshapeOpConverter); |
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
109 changes: 109 additions & 0 deletions
109
python/paddle/fluid/tests/unittests/ir/inference/test_trt_reshape_op.py
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,109 @@ | ||
# Copyright (c) 2020 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. | ||
|
||
from __future__ import print_function | ||
|
||
import unittest | ||
import numpy as np | ||
from inference_pass_test import InferencePassTest | ||
import paddle.fluid as fluid | ||
import paddle.fluid.core as core | ||
from paddle.fluid.core import PassVersionChecker | ||
from paddle.fluid.core import AnalysisConfig | ||
|
||
|
||
class TRTReshapeTest(InferencePassTest): | ||
def setUp(self): | ||
self.bs = 1 | ||
self.input_shape = [32, 15, 24] | ||
self.reshape = [-1, 8, 20, 72] | ||
self.data_shape = [ | ||
self.bs, self.input_shape[0], self.input_shape[1], | ||
self.input_shape[2] | ||
] | ||
with fluid.program_guard(self.main_program, self.startup_program): | ||
data = fluid.data( | ||
name='data', shape=self.data_shape, dtype='float32') | ||
reshape_out = self.append_reshape(data, self.reshape) | ||
out = fluid.layers.batch_norm(reshape_out, is_test=True) | ||
self.feeds = { | ||
'data': np.random.random(self.data_shape).astype('float32'), | ||
} | ||
self.enable_trt = True | ||
self.trt_parameters = TRTReshapeTest.TensorRTParam( | ||
1 << 30, self.bs, 1, AnalysisConfig.Precision.Float32, False, False) | ||
self.fetch_list = [out] | ||
|
||
def append_reshape(self, data, reshape): | ||
return fluid.layers.reshape(data, reshape) | ||
|
||
def test_check_output(self): | ||
if core.is_compiled_with_cuda(): | ||
use_gpu = True | ||
self.check_output_with_option(use_gpu) | ||
self.assertTrue( | ||
PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')) | ||
|
||
|
||
class TRTReshapeTest1(TRTReshapeTest): | ||
def setUp(self): | ||
self.bs = 2 | ||
self.input_shape = [23, 13, 24] | ||
self.reshape = [2, 0, -1, 12] | ||
self.data_shape = [ | ||
self.bs, self.input_shape[0], self.input_shape[1], | ||
self.input_shape[2] | ||
] | ||
with fluid.program_guard(self.main_program, self.startup_program): | ||
data = fluid.data( | ||
name='data', shape=self.data_shape, dtype='float32') | ||
reshape_out = self.append_reshape(data, self.reshape) | ||
out = fluid.layers.batch_norm(reshape_out, is_test=True) | ||
self.feeds = { | ||
'data': np.random.random(self.data_shape).astype('float32'), | ||
} | ||
self.enable_trt = True | ||
self.trt_parameters = TRTReshapeTest.TensorRTParam( | ||
1 << 30, self.bs, 1, AnalysisConfig.Precision.Float32, False, False) | ||
self.fetch_list = [out] | ||
|
||
|
||
class TRTReshapeTest2(TRTReshapeTest): | ||
def setUp(self): | ||
self.bs = 1 | ||
self.input_shape = [14, 48, 27] | ||
self.reshape = [1, 24, 28, 0] | ||
self.data_shape = [ | ||
self.bs, self.input_shape[0], self.input_shape[1], | ||
self.input_shape[2] | ||
] | ||
with fluid.program_guard(self.main_program, self.startup_program): | ||
data = fluid.data( | ||
name='data', shape=self.data_shape, dtype='float32') | ||
bn_out = fluid.layers.batch_norm(data, is_test=True) | ||
out = self.append_reshape(bn_out, self.reshape) | ||
self.feeds = { | ||
'data': np.random.random(self.data_shape).astype('float32'), | ||
} | ||
self.enable_trt = True | ||
self.trt_parameters = TRTReshapeTest.TensorRTParam( | ||
1 << 30, self.bs, 1, AnalysisConfig.Precision.Float32, False, False) | ||
self.dynamic_shape_params = TRTReshapeTest.DynamicShapeParam({ | ||
'data': [1, 3, 8, 8] | ||
}, {'data': [5, 100, 100, 100]}, {'data': [1, 3, 16, 16]}, False) | ||
self.fetch_list = [out] | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |