Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
312500dc
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 2 年 前同步成功
通知
2325
Star
20933
Fork
5424
代码
文件
提交
分支
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看板
提交
312500dc
编写于
1月 30, 2019
作者:
M
mozga-intel
提交者:
tensor-tang
1月 30, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Enable pool2d operator for a ngraph engine (#15395)
* Enable pool2d operator for a ngraph engine test=develop * Update test=develop
上级
b4c24f3f
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
228 addition
and
0 deletion
+228
-0
paddle/fluid/operators/ngraph/ngraph_bridge.cc
paddle/fluid/operators/ngraph/ngraph_bridge.cc
+2
-0
paddle/fluid/operators/ngraph/ngraph_ops.h
paddle/fluid/operators/ngraph/ngraph_ops.h
+1
-0
paddle/fluid/operators/ngraph/ops/pool2d_op.h
paddle/fluid/operators/ngraph/ops/pool2d_op.h
+174
-0
python/paddle/fluid/tests/unittests/ngraph/test_pool2d_ngraph_op.py
...dle/fluid/tests/unittests/ngraph/test_pool2d_ngraph_op.py
+51
-0
未找到文件。
paddle/fluid/operators/ngraph/ngraph_bridge.cc
浏览文件 @
312500dc
...
@@ -38,6 +38,8 @@ std::map<std::string,
...
@@ -38,6 +38,8 @@ std::map<std::string,
{
"mean_grad"
,
NG_OPS
::
BuildMeanGradNode
},
{
"mean_grad"
,
NG_OPS
::
BuildMeanGradNode
},
{
"mul"
,
NG_OPS
::
BuildMulNode
},
{
"mul"
,
NG_OPS
::
BuildMulNode
},
{
"mul_grad"
,
NG_OPS
::
BuildMulGradNode
},
{
"mul_grad"
,
NG_OPS
::
BuildMulGradNode
},
{
"pool2d"
,
NG_OPS
::
BuildPool2dNode
},
{
"pool2d_grad"
,
NG_OPS
::
BuildPool2dGradNode
},
{
"softmax"
,
NG_OPS
::
BuildSoftmaxNode
},
{
"softmax"
,
NG_OPS
::
BuildSoftmaxNode
},
{
"softmax_grad"
,
NG_OPS
::
BuildSoftmaxGradNode
},
{
"softmax_grad"
,
NG_OPS
::
BuildSoftmaxGradNode
},
{
"scale"
,
NG_OPS
::
BuildScaleNode
},
{
"scale"
,
NG_OPS
::
BuildScaleNode
},
...
...
paddle/fluid/operators/ngraph/ngraph_ops.h
浏览文件 @
312500dc
...
@@ -26,6 +26,7 @@ limitations under the License. */
...
@@ -26,6 +26,7 @@ limitations under the License. */
#include "ops/fill_constant_op.h"
#include "ops/fill_constant_op.h"
#include "ops/mean_op.h"
#include "ops/mean_op.h"
#include "ops/mul_op.h"
#include "ops/mul_op.h"
#include "ops/pool2d_op.h"
#include "ops/scale_op.h"
#include "ops/scale_op.h"
#include "ops/softmax_op.h"
#include "ops/softmax_op.h"
#include "ops/top_k_op.h"
#include "ops/top_k_op.h"
paddle/fluid/operators/ngraph/ops/pool2d_op.h
0 → 100644
浏览文件 @
312500dc
/*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. */
#pragma once
#include <string>
#include <vector>
#include "ngraph/ngraph.hpp"
#include "paddle/fluid/platform/ngraph_helper.h"
namespace
paddle
{
namespace
operators
{
namespace
ngraphs
{
void
BuildPool2dNode
(
const
std
::
shared_ptr
<
paddle
::
framework
::
OperatorBase
>&
op
,
std
::
shared_ptr
<
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
ngraph
::
Node
>>>
ngb_node_map
)
{
auto
op_attrs
=
paddle
::
framework
::
AttrReader
(
op
->
Attrs
());
auto
x
=
paddle
::
platform
::
GetInputNode
(
op
,
"X"
,
ngb_node_map
);
auto
x_shape
=
x
->
get_shape
();
std
::
string
pooling_type
=
op_attrs
.
Get
<
std
::
string
>
(
"pooling_type"
);
std
::
vector
<
int
>
ksize
=
op_attrs
.
Get
<
std
::
vector
<
int
>>
(
"ksize"
);
std
::
vector
<
int
>
strides
=
op_attrs
.
Get
<
std
::
vector
<
int
>>
(
"strides"
);
std
::
vector
<
int
>
paddings
=
op_attrs
.
Get
<
std
::
vector
<
int
>>
(
"paddings"
);
PADDLE_ENFORCE_EQ
(
x_shape
.
size
()
-
2
,
ksize
.
size
(),
"Handling 2d pooling only"
);
if
(
op_attrs
.
Get
<
bool
>
(
"global_pooling"
))
{
for
(
size_t
i
=
0
;
i
<
ksize
.
size
();
++
i
)
{
paddings
[
i
]
=
0
;
ksize
[
i
]
=
static_cast
<
int
>
(
x_shape
.
at
(
i
+
2
));
}
}
ngraph
::
Shape
ng_padding_below
{
static_cast
<
size_t
>
(
paddings
.
at
(
0
)),
static_cast
<
size_t
>
(
paddings
.
at
(
1
))};
ngraph
::
Shape
ng_padding_above
{
static_cast
<
size_t
>
(
paddings
.
at
(
0
)),
static_cast
<
size_t
>
(
paddings
.
at
(
1
))};
ngraph
::
Shape
ng_ksize_shape
{
static_cast
<
size_t
>
(
ksize
.
at
(
0
)),
static_cast
<
size_t
>
(
ksize
.
at
(
1
))};
ngraph
::
Strides
ng_strides
{
static_cast
<
size_t
>
(
strides
.
at
(
0
)),
static_cast
<
size_t
>
(
strides
.
at
(
1
))};
auto
ComputeCeiledOutput
=
[](
size_t
in
,
size_t
k
,
size_t
p
,
size_t
s
)
{
return
(
in
-
k
+
2
*
p
)
/
s
+
1
;
};
if
(
op_attrs
.
Get
<
bool
>
(
"ceil_mode"
))
{
auto
dummy_out
=
paddle
::
platform
::
GetOutputNode
(
op
,
"Out"
,
ngb_node_map
);
auto
dummpy_shape
=
dummy_out
->
get_shape
();
for
(
size_t
i
=
0
;
i
<
ng_padding_above
.
size
();
++
i
)
{
auto
desired_size
=
ComputeCeiledOutput
(
x_shape
[
i
+
2
],
ksize
[
i
],
paddings
[
i
],
strides
[
i
]);
if
(
desired_size
!=
dummpy_shape
[
i
+
2
])
{
ng_padding_above
[
i
]
+=
strides
[
i
];
}
}
}
bool
padding_exclusive
=
op_attrs
.
Get
<
bool
>
(
"exclusive"
);
if
(
pooling_type
==
"max"
)
{
auto
pool2d
=
std
::
make_shared
<
ngraph
::
op
::
MaxPool
>
(
x
,
ng_ksize_shape
,
ng_strides
,
ng_padding_below
,
ng_padding_above
);
paddle
::
platform
::
SetOutputNode
(
op
,
"Out"
,
pool2d
,
ngb_node_map
);
}
else
if
(
pooling_type
==
"avg"
)
{
std
::
shared_ptr
<
ngraph
::
Node
>
pool2d
;
if
(
op_attrs
.
Get
<
bool
>
(
"adaptive"
))
{
auto
ComputeAdaptive
=
[](
size_t
in
,
size_t
k
)
{
return
std
::
floor
(
in
/
k
);
};
ng_strides
[
0
]
=
x_shape
.
size
()
==
4
?
ComputeAdaptive
(
x_shape
[
3
],
ksize
[
0
])
:
ng_strides
[
0
];
ng_strides
[
1
]
=
x_shape
.
size
()
==
4
?
ComputeAdaptive
(
x_shape
[
3
],
ksize
[
0
])
:
ng_strides
[
1
];
pool2d
=
std
::
make_shared
<
ngraph
::
op
::
AvgPool
>
(
x
,
ng_ksize_shape
,
ng_strides
);
}
else
{
pool2d
=
std
::
make_shared
<
ngraph
::
op
::
AvgPool
>
(
x
,
ng_ksize_shape
,
ng_strides
,
ng_padding_below
,
ng_padding_above
,
!
padding_exclusive
);
}
paddle
::
platform
::
SetOutputNode
(
op
,
"Out"
,
pool2d
,
ngb_node_map
);
}
else
{
PADDLE_THROW
(
"Support max and avg pooling only"
);
}
}
void
BuildPool2dGradNode
(
const
std
::
shared_ptr
<
paddle
::
framework
::
OperatorBase
>&
op
,
std
::
shared_ptr
<
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
ngraph
::
Node
>>>
ngb_node_map
)
{
auto
op_attrs
=
paddle
::
framework
::
AttrReader
(
op
->
Attrs
());
auto
out
=
paddle
::
platform
::
GetInputNode
(
op
,
"Out"
,
ngb_node_map
);
auto
dout
=
paddle
::
platform
::
GetInputNode
(
op
,
"Out@GRAD"
,
ngb_node_map
);
auto
x
=
paddle
::
platform
::
GetInputNode
(
op
,
"X"
,
ngb_node_map
);
auto
x_shape
=
x
->
get_shape
();
std
::
string
pooling_type
=
op_attrs
.
Get
<
std
::
string
>
(
"pooling_type"
);
std
::
vector
<
int
>
ksize
=
op_attrs
.
Get
<
std
::
vector
<
int
>>
(
"ksize"
);
std
::
vector
<
int
>
strides
=
op_attrs
.
Get
<
std
::
vector
<
int
>>
(
"strides"
);
std
::
vector
<
int
>
paddings
=
op_attrs
.
Get
<
std
::
vector
<
int
>>
(
"paddings"
);
PADDLE_ENFORCE_EQ
(
x_shape
.
size
()
-
2
,
ksize
.
size
(),
"Handling 2d pooling only"
);
if
(
op_attrs
.
Get
<
bool
>
(
"global_pooling"
))
{
for
(
size_t
i
=
0
;
i
<
ksize
.
size
();
++
i
)
{
paddings
[
i
]
=
0
;
ksize
[
i
]
=
static_cast
<
int
>
(
x_shape
.
at
(
i
+
2
));
}
}
ngraph
::
Shape
ng_padding_below
{
static_cast
<
size_t
>
(
paddings
.
at
(
0
)),
static_cast
<
size_t
>
(
paddings
.
at
(
1
))};
ngraph
::
Shape
ng_padding_above
{
static_cast
<
size_t
>
(
paddings
.
at
(
0
)),
static_cast
<
size_t
>
(
paddings
.
at
(
1
))};
ngraph
::
Shape
ng_ksize_shape
{
static_cast
<
size_t
>
(
ksize
.
at
(
0
)),
static_cast
<
size_t
>
(
ksize
.
at
(
1
))};
ngraph
::
Strides
ng_strides
{
static_cast
<
size_t
>
(
strides
.
at
(
0
)),
static_cast
<
size_t
>
(
strides
.
at
(
1
))};
bool
padding_exclusive
=
op_attrs
.
Get
<
bool
>
(
"exclusive"
);
if
(
pooling_type
==
"max"
)
{
auto
pool2d_grad
=
std
::
make_shared
<
ngraph
::
op
::
MaxPoolBackprop
>
(
x
,
dout
,
out
,
ng_ksize_shape
,
ng_strides
,
ng_padding_below
,
ng_padding_above
);
paddle
::
platform
::
SetOutputNode
(
op
,
"X@GRAD"
,
pool2d_grad
,
ngb_node_map
);
}
else
if
(
pooling_type
==
"avg"
)
{
std
::
shared_ptr
<
ngraph
::
Node
>
pool2d_grad
;
if
(
op_attrs
.
Get
<
bool
>
(
"adaptive"
))
{
auto
ComputeAdaptive
=
[](
size_t
in
,
size_t
k
)
{
return
std
::
floor
(
in
/
k
);
};
ng_strides
[
0
]
=
x_shape
.
size
()
==
4
?
ComputeAdaptive
(
x_shape
[
3
],
ksize
[
0
])
:
ng_strides
[
0
];
ng_strides
[
1
]
=
x_shape
.
size
()
==
4
?
ComputeAdaptive
(
x_shape
[
3
],
ksize
[
0
])
:
ng_strides
[
1
];
pool2d_grad
=
std
::
make_shared
<
ngraph
::
op
::
AvgPoolBackprop
>
(
x
->
get_shape
(),
dout
,
ng_ksize_shape
,
ng_strides
,
ng_padding_below
,
ng_padding_above
,
!
padding_exclusive
);
}
else
{
pool2d_grad
=
std
::
make_shared
<
ngraph
::
op
::
AvgPoolBackprop
>
(
x
->
get_shape
(),
dout
,
ng_ksize_shape
,
ng_strides
,
ng_padding_below
,
ng_padding_above
,
!
padding_exclusive
);
}
paddle
::
platform
::
SetOutputNode
(
op
,
"X@GRAD"
,
pool2d_grad
,
ngb_node_map
);
}
else
{
PADDLE_THROW
(
"Support max and avg pooling only"
);
}
}
}
// namespace ngraphs
}
// namespace operators
}
// namespace paddle
python/paddle/fluid/tests/unittests/ngraph/test_pool2d_ngraph_op.py
0 → 100644
浏览文件 @
312500dc
# 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.
from
__future__
import
print_function
from
paddle.fluid.tests.unittests.test_pool2d_op
import
*
class
TestNGRAPHPool2D_Op
(
TestPool2D_Op
):
def
init_test_case
(
self
):
super
(
TestNGRAPHPool2D_Op
,
self
).
init_test_case
()
class
TestNGRAPHCase1
(
TestCase1
):
def
init_test_case
(
self
):
super
(
TestNGRAPHCase1
,
self
).
init_test_case
()
class
TestNGRAPHCase2
(
TestCase2
):
def
init_test_case
(
self
):
super
(
TestNGRAPHCase2
,
self
).
init_test_case
()
class
TestNGRAPHCase3
(
TestCase3
):
def
init_pool_type
(
self
):
super
(
TestNGRAPHCase3
,
self
).
init_pool_type
()
class
TestNGRAPHCase4
(
TestCase4
):
def
init_pool_type
(
self
):
super
(
TestNGRAPHCase4
,
self
).
init_pool_type
()
class
TestNGRAPHCase5
(
TestCase5
):
def
init_pool_type
(
self
):
super
(
TestNGRAPHCase5
,
self
).
init_pool_type
()
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录