Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0942f77e
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2305
Star
20932
Fork
5423
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
0942f77e
编写于
6月 06, 2023
作者:
B
bukejiyu
提交者:
GitHub
6月 06, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[inference][trt] update roll op 2 gather layer (#53984)
* update roll convert
上级
f276f5d5
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
54 addition
and
34 deletion
+54
-34
paddle/fluid/inference/tensorrt/convert/roll_op.cc
paddle/fluid/inference/tensorrt/convert/roll_op.cc
+50
-33
test/ir/inference/test_trt_convert_roll.py
test/ir/inference/test_trt_convert_roll.py
+4
-1
未找到文件。
paddle/fluid/inference/tensorrt/convert/roll_op.cc
浏览文件 @
0942f77e
...
...
@@ -26,50 +26,67 @@ class RollOpConverter : public OpConverter {
void
operator
()(
const
framework
::
proto
::
OpDesc
&
op
,
const
framework
::
Scope
&
scope
,
bool
test_mode
)
override
{
VLOG
(
4
)
<<
"convert roll op to tensorrt
Slice
layer"
;
VLOG
(
4
)
<<
"convert roll op to tensorrt
Gather
layer"
;
framework
::
OpDesc
op_desc
(
op
,
nullptr
);
auto
*
input
=
engine_
->
GetITensor
(
op_desc
.
Input
(
"X"
)[
0
]);
nvinfer1
::
Dims
input_dims
=
input
->
getDimensions
();
std
::
vector
<
int64_t
>
axis
=
PADDLE_GET_CONST
(
std
::
vector
<
int64_t
>
,
op_desc
.
GetAttr
(
"axis"
));
std
::
vector
<
int64_t
>
shifts
=
PADDLE_GET_CONST
(
std
::
vector
<
int64_t
>
,
op_desc
.
GetAttr
(
"shifts"
));
nvinfer1
::
Dims
start
;
start
.
nbDims
=
input_dims
.
nbDims
;
for
(
int
i
=
0
;
i
<
start
.
nbDims
;
i
++
)
{
start
.
d
[
i
]
=
0
;
}
int
axis_size
=
axis
.
size
();
nvinfer1
::
ITensor
*
input_shape_tensor
=
Shape
(
input
);
nvinfer1
::
ILayer
*
layer
=
nullptr
;
for
(
int
i
=
0
;
i
<
axis_size
;
i
++
)
{
start
.
d
[
axis
[
i
]]
=
(
-
shifts
[
i
])
%
input_dims
.
d
[
axis
[
i
]];
auto
axi
=
static_cast
<
int32_t
>
(
axis
[
i
]);
auto
shift
=
static_cast
<
int32_t
>
(
shifts
[
i
]);
nvinfer1
::
ITensor
*
input_axis
=
GetEleTensorOfShape
(
input_shape_tensor
,
axi
);
nvinfer1
::
ITensor
*
input_shift
=
Add1DConstantLayer
(
shift
);
// 1.sub_value mod input_axis
auto
input1
=
Sub
(
input_axis
,
input_shift
);
auto
tmp_div_res
=
FloorDiv
(
input1
,
input_axis
);
auto
tmp_prod_res
=
Prod
(
tmp_div_res
,
input_axis
);
auto
start
=
Sub
(
input1
,
tmp_prod_res
);
// 2.avoid start less than 0,start mod input_axis
start
=
Sum
(
start
,
input_axis
);
auto
tmp_div_res1
=
FloorDiv
(
start
,
input_axis
);
auto
tmp_prod_res1
=
Prod
(
tmp_div_res1
,
input_axis
);
start
=
Sub
(
start
,
tmp_prod_res1
);
auto
zero_tensor
=
Add1DConstantLayer
(
0
);
auto
step
=
Add1DConstantLayer
(
1
);
// 3.make index_tensor0
auto
quotient_tensor
=
FloorDiv
(
Sub
(
input_axis
,
start
),
step
);
auto
*
start1
=
GetEleTensorOfShape
(
start
,
0
,
true
);
auto
fill_layer0
=
TRT_ENGINE_ADD_LAYER
(
engine_
,
Fill
,
nvinfer1
::
Dims
{},
nvinfer1
::
FillOperation
::
kLINSPACE
);
fill_layer0
->
setInput
(
0
,
*
quotient_tensor
);
fill_layer0
->
setInput
(
1
,
*
start1
);
fill_layer0
->
setInput
(
2
,
*
step
);
auto
*
index_tensor0
=
fill_layer0
->
getOutput
(
0
);
// 4.make index_tensor1
quotient_tensor
=
FloorDiv
(
Sub
(
start
,
zero_tensor
),
step
);
auto
*
start2
=
Add1DConstantLayer
(
0
,
""
,
true
);
auto
fill_layer1
=
TRT_ENGINE_ADD_LAYER
(
engine_
,
Fill
,
nvinfer1
::
Dims
{},
nvinfer1
::
FillOperation
::
kLINSPACE
);
fill_layer1
->
setInput
(
0
,
*
quotient_tensor
);
fill_layer1
->
setInput
(
1
,
*
start2
);
fill_layer1
->
setInput
(
2
,
*
step
);
auto
*
index_tensor1
=
fill_layer1
->
getOutput
(
0
);
std
::
vector
<
nvinfer1
::
ITensor
*>
itensors
;
itensors
.
push_back
(
index_tensor0
);
itensors
.
push_back
(
index_tensor1
);
nvinfer1
::
ITensor
*
concat_input_tensor
=
Concat
(
itensors
);
if
(
layer
==
nullptr
)
{
layer
=
TRT_ENGINE_ADD_LAYER
(
engine_
,
Gather
,
*
input
,
*
concat_input_tensor
,
axi
);
}
else
{
layer
=
TRT_ENGINE_ADD_LAYER
(
engine_
,
Gather
,
*
layer
->
getOutput
(
0
),
*
concat_input_tensor
,
axi
);
}
}
nvinfer1
::
Dims
stride
;
stride
.
nbDims
=
input_dims
.
nbDims
;
for
(
int
i
=
0
;
i
<
stride
.
nbDims
;
i
++
)
{
stride
.
d
[
i
]
=
1
;
}
nvinfer1
::
Dims
size
;
size
.
nbDims
=
input_dims
.
nbDims
;
for
(
int
i
=
0
;
i
<
size
.
nbDims
;
i
++
)
{
size
.
d
[
i
]
=
1
;
}
auto
output_name
=
op_desc
.
Output
(
"Out"
)[
0
];
auto
shape_layer
=
TRT_ENGINE_ADD_LAYER
(
engine_
,
Shape
,
*
input
);
auto
*
layer
=
TRT_ENGINE_ADD_LAYER
(
engine_
,
Slice
,
*
input
,
start
,
size
,
stride
);
layer
->
setInput
(
2
,
*
shape_layer
->
getOutput
(
0
));
#if IS_TRT_VERSION_GE(7000)
layer
->
setMode
(
nvinfer1
::
SliceMode
::
kWRAP
);
#endif
RreplenishLayerAndOutput
(
layer
,
"roll"
,
{
output_name
},
test_mode
);
}
};
...
...
test/ir/inference/test_trt_convert_roll.py
浏览文件 @
0942f77e
...
...
@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import
random
import
unittest
from
functools
import
partial
from
typing
import
Any
,
Dict
,
List
...
...
@@ -33,8 +34,10 @@ class TrtConvertRollTest(TrtLayerAutoScanTest):
return
True
def
sample_program_configs
(
self
):
self
.
trt_param
.
workspace_size
=
random
.
randint
(
1024
,
1
<<
30
)
def
generate_input1
(
attrs
:
List
[
Dict
[
str
,
Any
]]):
return
np
.
ones
([
1
,
56
,
56
,
192
]).
astype
(
np
.
float32
)
return
np
.
random
.
random
([
1
,
56
,
56
,
192
]).
astype
(
np
.
float32
)
for
axis
in
[[
1
,
2
]]:
for
shifts
in
[[
-
1
,
-
1
],
[
-
3
,
-
3
]]:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录