Skip to content

Commit

Permalink
fix reshape trt condition (#34007)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiweibo committed Jul 7, 2021
1 parent cb73fee commit 0914ff9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
6 changes: 3 additions & 3 deletions paddle/fluid/inference/goapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Paddle Inference golang API 基于 [capi](../capi_exp) 和 cgo 实现,需要
2. 使用`go get`获取golang paddle api

```
# 此处使用上一步记录的CommitId,假设为76e5724
COMMITID=76e5724
# 此处使用上一步记录的CommitId,假设为0722297
COMMITID=0722297
go get -d -v github.com/paddlepaddle/paddle/paddle/fluid/inference/goapi@${COMMITID}
```

Expand All @@ -28,7 +28,7 @@ go1.15新增了`GOMODCACHE`环境变量,`go get`默认会将代码下载到`GO
```bash
eval $(go env | grep GOMODCACHE)
# 按需修改最后的goapi版本号
cd ${GOMODCACHE}/github.com/paddlepaddle/paddle/paddle/fluid/inference/goapi\@v0.0.0-20210517084506-76e5724c16a5/
cd ${GOMODCACHE}/github.com/paddlepaddle/paddle/paddle/fluid/inference/goapi\@v0.0.0-20210623023452-0722297d9b8c/
ln -s ${PADDLE_C_DOWNLOAD_DIR}/paddle_inference_c_install_dir paddle_inference_c
```

Expand Down
15 changes: 8 additions & 7 deletions paddle/fluid/inference/tensorrt/op_teller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -692,15 +692,16 @@ bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8,
if (op_type == "reshape" || op_type == "reshape2") {
if (!desc.HasAttr("shape")) {
return false;
// Paddle-TRT does not support the input tensors: Shape and ShapeTensor
} else if (desc.Input("Shape").size() >= 1 ||
desc.Input("ShapeTensor").size() >= 1) {
}
// Paddle-TRT does not support the input tensors: Shape and ShapeTensor
if (desc.Input("Shape").size() >= 1 ||
desc.Input("ShapeTensor").size() >= 1) {
return false;
} else {
std::vector<int> shape =
BOOST_GET_CONST(std::vector<int>, desc.GetAttr("shape"));
if (shape.size() >= nvinfer1::Dims::MAX_DIMS) return false;
}
std::vector<int> shape =
BOOST_GET_CONST(std::vector<int>, desc.GetAttr("shape"));
if (shape.size() >= nvinfer1::Dims::MAX_DIMS) return false;
if (!with_dynamic_shape && shape[0] == -1) return false;
}

if (op_type == "reduce_sum") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ def _save_models(self, executor, program, scope):
feeded_var_names=list(self.feeds.keys()),
target_vars=self.fetch_list,
executor=executor,
main_program=program,
model_filename="model",
params_filename="params")
main_program=program)

return outs

Expand Down Expand Up @@ -111,8 +109,7 @@ def _get_analysis_config(self,
'''
Return a new object of AnalysisConfig.
'''
config = AnalysisConfig(
os.path.join(self.path, "model"), os.path.join(self.path, "params"))
config = AnalysisConfig(self.path)
config.disable_gpu()
config.switch_specify_input_names(True)
config.switch_ir_optim(True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ def setUp(self):
image_size = fluid.data(
name='image_size', shape=[self.bs, 2], dtype='int32')
boxes, scores = self.append_yolobox(image, image_size)
scores = fluid.layers.reshape(scores, (self.bs, -1))
out = fluid.layers.batch_norm(scores, is_test=True)

self.feeds = {
'image': np.random.random(image_shape).astype('float32'),
Expand All @@ -43,7 +41,7 @@ def setUp(self):
self.enable_trt = True
self.trt_parameters = TRTYoloBoxTest.TensorRTParam(
1 << 30, self.bs, 1, AnalysisConfig.Precision.Float32, False, False)
self.fetch_list = [out, boxes]
self.fetch_list = [scores, boxes]

def set_params(self):
self.bs = 4
Expand Down Expand Up @@ -72,5 +70,51 @@ def test_check_output(self):
PassVersionChecker.IsCompatible('tensorrt_subgraph_pass'))


class TRTYoloBoxFP16Test(InferencePassTest):
def setUp(self):
self.set_params()
with fluid.program_guard(self.main_program, self.startup_program):
image_shape = [self.bs, self.channel, self.height, self.width]
image = fluid.data(name='image', shape=image_shape, dtype='float32')
image_size = fluid.data(
name='image_size', shape=[self.bs, 2], dtype='int32')
boxes, scores = self.append_yolobox(image, image_size)

self.feeds = {
'image': np.random.random(image_shape).astype('float32'),
'image_size': np.array([[416, 416]]).astype('int32'),
}
self.enable_trt = True
self.trt_parameters = TRTYoloBoxFP16Test.TensorRTParam(
1 << 30, self.bs, 1, AnalysisConfig.Precision.Half, False, False)
self.fetch_list = [scores, boxes]

def set_params(self):
self.bs = 1
self.height = 13
self.width = 13
self.class_num = 1
self.anchors = [106, 148, 92, 300, 197, 334]
self.channel = 18
self.conf_thresh = .05
self.downsample_ratio = 32

def append_yolobox(self, image, image_size):
return fluid.layers.yolo_box(
x=image,
img_size=image_size,
class_num=self.class_num,
anchors=self.anchors,
conf_thresh=self.conf_thresh,
downsample_ratio=self.downsample_ratio)

def test_check_output(self):
if core.is_compiled_with_cuda():
use_gpu = True
self.check_output_with_option(use_gpu, flatten=True, rtol=1e-1)
self.assertTrue(
PassVersionChecker.IsCompatible('tensorrt_subgraph_pass'))


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

0 comments on commit 0914ff9

Please sign in to comment.