Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
43a67a26
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
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看板
提交
43a67a26
编写于
1月 31, 2019
作者:
M
mozga-intel
提交者:
tensor-tang
1月 31, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Enable conv2d operator for a ngraph engine (#15269)
test=develop
上级
a6a1a92e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
290 addition
and
0 deletion
+290
-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/conv2d_op.h
paddle/fluid/operators/ngraph/ops/conv2d_op.h
+235
-0
python/paddle/fluid/tests/unittests/ngraph/test_conv2d_ngraph_op.py
...dle/fluid/tests/unittests/ngraph/test_conv2d_ngraph_op.py
+52
-0
未找到文件。
paddle/fluid/operators/ngraph/ngraph_bridge.cc
浏览文件 @
43a67a26
...
...
@@ -31,6 +31,8 @@ std::map<std::string,
std
::
shared_ptr
<
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
ngraph
::
Node
>>>
)
>>
NgraphBridge
::
NG_NODE_MAP
=
{
{
"conv2d"
,
NG_OPS
::
BuildConv2dNode
},
{
"conv2d_grad"
,
NG_OPS
::
BuildConv2dGradNode
},
{
"elementwise_add"
,
NG_OPS
::
BuildElementwiseAddNode
},
{
"elementwise_add_grad"
,
NG_OPS
::
BuildElementwiseAddGradNode
},
{
"fill_constant"
,
NG_OPS
::
BuildFillConstantNode
},
...
...
paddle/fluid/operators/ngraph/ngraph_ops.h
浏览文件 @
43a67a26
...
...
@@ -22,6 +22,7 @@ limitations under the License. */
#pragma once
#include "ops/binary_unnary_op.h"
#include "ops/conv2d_op.h"
#include "ops/elementwise_add_op.h"
#include "ops/fill_constant_op.h"
#include "ops/mean_op.h"
...
...
paddle/fluid/operators/ngraph/ops/conv2d_op.h
0 → 100644
浏览文件 @
43a67a26
/* 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
{
std
::
shared_ptr
<
ngraph
::
Node
>
GroupedConvolution
(
const
std
::
shared_ptr
<
ngraph
::
Node
>&
data_batch
,
const
std
::
shared_ptr
<
ngraph
::
Node
>&
filters
,
const
ngraph
::
Strides
strides
,
const
ngraph
::
Strides
dilations
,
const
ngraph
::
CoordinateDiff
&
paddings
,
size_t
groups
)
{
auto
&
data_shape
=
data_batch
->
get_shape
();
auto
&
filter_shape
=
filters
->
get_shape
();
ngraph
::
NodeVector
ng_slices
;
for
(
size_t
i
=
0
;
i
<
groups
;
++
i
)
{
size_t
channel_step
=
filter_shape
.
at
(
1
);
const
std
::
vector
<
size_t
>
lower_bound
{
0
,
i
*
channel_step
,
0
,
0
};
const
std
::
vector
<
size_t
>
upper_bound
{
data_shape
.
at
(
0
),
(
i
+
1
)
*
channel_step
,
data_shape
.
at
(
2
),
data_shape
.
at
(
3
)};
auto
data_slice
=
std
::
make_shared
<
ngraph
::
op
::
Slice
>
(
data_batch
,
lower_bound
,
upper_bound
);
size_t
filter_step
=
filter_shape
.
at
(
0
)
/
groups
;
const
std
::
vector
<
size_t
>
filter_lower_bound
{
i
*
filter_step
,
0
,
0
,
0
};
const
std
::
vector
<
size_t
>
filter_upper_bound
{
(
i
+
1
)
*
filter_step
,
filter_shape
.
at
(
1
),
filter_shape
.
at
(
2
),
filter_shape
.
at
(
3
)};
auto
filter_slice
=
std
::
make_shared
<
ngraph
::
op
::
Slice
>
(
filters
,
filter_lower_bound
,
filter_upper_bound
);
auto
ng_conv
=
std
::
make_shared
<
ngraph
::
op
::
Convolution
>
(
data_slice
,
filter_slice
,
strides
,
dilations
,
paddings
,
paddings
);
ng_slices
.
push_back
(
ng_conv
);
}
size_t
concat_axis
=
1
;
return
std
::
make_shared
<
ngraph
::
op
::
Concat
>
(
ng_slices
,
concat_axis
);
}
std
::
shared_ptr
<
ngraph
::
Node
>
GroupedGradConvolutionFilter
(
const
std
::
shared_ptr
<
ngraph
::
Node
>&
data_batch
,
const
std
::
shared_ptr
<
ngraph
::
Node
>&
filters
,
const
std
::
shared_ptr
<
ngraph
::
Node
>&
doutput
,
const
ngraph
::
Strides
strides
,
const
ngraph
::
Strides
dilations
,
const
ngraph
::
CoordinateDiff
&
paddings
,
size_t
groups
)
{
auto
&
data_shape
=
data_batch
->
get_shape
();
auto
&
filter_shape
=
filters
->
get_shape
();
auto
&
out_shape
=
doutput
->
get_shape
();
ngraph
::
NodeVector
ng_slices
;
for
(
size_t
i
=
0
;
i
<
groups
;
++
i
)
{
size_t
channel_step
=
filter_shape
.
at
(
1
);
const
std
::
vector
<
size_t
>
lower_bound
{
0
,
i
*
channel_step
,
0
,
0
};
const
std
::
vector
<
size_t
>
upper_bound
{
data_shape
.
at
(
0
),
(
i
+
1
)
*
channel_step
,
data_shape
.
at
(
2
),
data_shape
.
at
(
3
)};
auto
data_slice
=
std
::
make_shared
<
ngraph
::
op
::
Slice
>
(
data_batch
,
lower_bound
,
upper_bound
);
size_t
filter_step
=
data_shape
.
at
(
0
);
const
std
::
vector
<
size_t
>
filter_lower_bound
{
i
*
filter_step
,
0
,
0
,
0
};
const
std
::
vector
<
size_t
>
filter_upper_bound
{
(
i
+
1
)
*
filter_step
,
filter_shape
.
at
(
1
),
filter_shape
.
at
(
2
),
filter_shape
.
at
(
3
)};
auto
filter_slice
=
std
::
make_shared
<
ngraph
::
op
::
Slice
>
(
filters
,
filter_lower_bound
,
filter_upper_bound
);
const
std
::
vector
<
size_t
>
olower_bound
{
0
,
i
*
filter_step
,
0
,
0
};
const
std
::
vector
<
size_t
>
oupper_bound
{
out_shape
.
at
(
0
),
(
i
+
1
)
*
filter_step
,
out_shape
.
at
(
2
),
out_shape
.
at
(
3
)};
auto
out_slice
=
std
::
make_shared
<
ngraph
::
op
::
Slice
>
(
doutput
,
olower_bound
,
oupper_bound
);
auto
ng_conv
=
std
::
make_shared
<
ngraph
::
op
::
ConvolutionBackpropFilters
>
(
data_slice
,
filter_slice
->
get_shape
(),
out_slice
,
strides
,
dilations
,
paddings
,
paddings
,
ngraph
::
Strides
{
1
,
1
});
ng_slices
.
push_back
(
ng_conv
);
}
size_t
concat_axis
=
0
;
return
std
::
make_shared
<
ngraph
::
op
::
Concat
>
(
ng_slices
,
concat_axis
);
}
std
::
shared_ptr
<
ngraph
::
Node
>
GroupedGradConvolutionData
(
const
std
::
shared_ptr
<
ngraph
::
Node
>&
data_batch
,
const
std
::
shared_ptr
<
ngraph
::
Node
>&
filters
,
const
std
::
shared_ptr
<
ngraph
::
Node
>&
doutput
,
const
ngraph
::
Strides
strides
,
const
ngraph
::
Strides
dilations
,
const
ngraph
::
CoordinateDiff
&
paddings
,
size_t
groups
)
{
auto
&
data_shape
=
data_batch
->
get_shape
();
auto
&
filter_shape
=
filters
->
get_shape
();
auto
&
out_shape
=
doutput
->
get_shape
();
ngraph
::
NodeVector
ng_slices
;
for
(
size_t
i
=
0
;
i
<
groups
;
++
i
)
{
size_t
channel_step
=
filter_shape
.
at
(
1
);
const
std
::
vector
<
size_t
>
lower_bound
{
0
,
i
*
channel_step
,
0
,
0
};
const
std
::
vector
<
size_t
>
upper_bound
{
data_shape
.
at
(
0
),
(
i
+
1
)
*
channel_step
,
data_shape
.
at
(
2
),
data_shape
.
at
(
3
)};
auto
data_slice
=
std
::
make_shared
<
ngraph
::
op
::
Slice
>
(
data_batch
,
lower_bound
,
upper_bound
);
size_t
filter_step
=
data_shape
.
at
(
0
);
const
std
::
vector
<
size_t
>
filter_lower_bound
{
i
*
filter_step
,
0
,
0
,
0
};
const
std
::
vector
<
size_t
>
filter_upper_bound
{
(
i
+
1
)
*
filter_step
,
filter_shape
.
at
(
1
),
filter_shape
.
at
(
2
),
filter_shape
.
at
(
3
)};
auto
filter_slice
=
std
::
make_shared
<
ngraph
::
op
::
Slice
>
(
filters
,
filter_lower_bound
,
filter_upper_bound
);
const
std
::
vector
<
size_t
>
olower_bound
{
0
,
i
*
filter_step
,
0
,
0
};
const
std
::
vector
<
size_t
>
oupper_bound
{
out_shape
.
at
(
0
),
(
i
+
1
)
*
filter_step
,
out_shape
.
at
(
2
),
out_shape
.
at
(
3
)};
auto
out_slice
=
std
::
make_shared
<
ngraph
::
op
::
Slice
>
(
doutput
,
olower_bound
,
oupper_bound
);
auto
ng_conv
=
std
::
make_shared
<
ngraph
::
op
::
ConvolutionBackpropData
>
(
data_slice
->
get_shape
(),
filter_slice
,
out_slice
,
strides
,
dilations
,
paddings
,
paddings
,
ngraph
::
Strides
{
1
,
1
});
ng_slices
.
push_back
(
ng_conv
);
}
size_t
concat_axis
=
1
;
return
std
::
make_shared
<
ngraph
::
op
::
Concat
>
(
ng_slices
,
concat_axis
);
}
void
BuildConv2dNode
(
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
filters
=
paddle
::
platform
::
GetInputNode
(
op
,
"Filter"
,
ngb_node_map
);
auto
input
=
paddle
::
platform
::
GetInputNode
(
op
,
"Input"
,
ngb_node_map
);
std
::
vector
<
int
>
strides
=
op_attrs
.
Get
<
std
::
vector
<
int
>>
(
"strides"
);
std
::
vector
<
int
>
paddings
=
op_attrs
.
Get
<
std
::
vector
<
int
>>
(
"paddings"
);
std
::
vector
<
int
>
dilations
=
op_attrs
.
Get
<
std
::
vector
<
int
>>
(
"dilations"
);
const
ngraph
::
Strides
ng_strides
{
static_cast
<
size_t
>
(
strides
.
at
(
0
)),
static_cast
<
size_t
>
(
strides
.
at
(
1
))};
const
ngraph
::
Strides
ng_dilations
{
static_cast
<
size_t
>
(
dilations
.
at
(
0
)),
static_cast
<
size_t
>
(
dilations
.
at
(
1
))};
const
ngraph
::
CoordinateDiff
ng_paddings
{
static_cast
<
std
::
ptrdiff_t
>
(
paddings
.
at
(
0
)),
static_cast
<
std
::
ptrdiff_t
>
(
paddings
.
at
(
1
))};
int
groups
=
static_cast
<
size_t
>
(
op_attrs
.
Get
<
int
>
(
"groups"
));
PADDLE_ENFORCE_GE
(
groups
,
1
,
"conv groups needs be no less than 1"
);
std
::
shared_ptr
<
ngraph
::
Node
>
result
;
if
(
groups
==
1
)
{
result
=
std
::
make_shared
<
ngraph
::
op
::
Convolution
>
(
input
,
filters
,
ng_strides
,
ng_dilations
,
ng_paddings
,
ng_paddings
);
}
else
{
result
=
GroupedConvolution
(
input
,
filters
,
ng_strides
,
ng_dilations
,
ng_paddings
,
groups
);
}
paddle
::
platform
::
SetOutputNode
(
op
,
"Output"
,
result
,
ngb_node_map
);
}
void
BuildConv2dGradNode
(
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
filter
=
paddle
::
platform
::
GetInputNode
(
op
,
"Filter"
,
ngb_node_map
);
auto
input
=
paddle
::
platform
::
GetInputNode
(
op
,
"Input"
,
ngb_node_map
);
auto
doutput
=
paddle
::
platform
::
GetInputNode
(
op
,
"Output@GRAD"
,
ngb_node_map
);
int
groups
=
op_attrs
.
Get
<
int
>
(
"groups"
);
std
::
vector
<
int
>
strides
=
op_attrs
.
Get
<
std
::
vector
<
int
>>
(
"strides"
);
std
::
vector
<
int
>
paddings
=
op_attrs
.
Get
<
std
::
vector
<
int
>>
(
"paddings"
);
std
::
vector
<
int
>
dilations
=
op_attrs
.
Get
<
std
::
vector
<
int
>>
(
"dilations"
);
const
ngraph
::
Strides
ng_strides
{
static_cast
<
size_t
>
(
strides
.
at
(
0
)),
static_cast
<
size_t
>
(
strides
.
at
(
1
))};
const
ngraph
::
Strides
ng_dilations
{
static_cast
<
size_t
>
(
dilations
.
at
(
0
)),
static_cast
<
size_t
>
(
dilations
.
at
(
1
))};
const
ngraph
::
CoordinateDiff
ng_paddings
{
static_cast
<
std
::
ptrdiff_t
>
(
paddings
.
at
(
0
)),
static_cast
<
std
::
ptrdiff_t
>
(
paddings
.
at
(
1
))};
std
::
shared_ptr
<
ngraph
::
Node
>
dfilter
;
std
::
shared_ptr
<
ngraph
::
Node
>
dinput
;
if
(
groups
==
1
)
{
dfilter
=
std
::
make_shared
<
ngraph
::
op
::
ConvolutionBackpropFilters
>
(
input
,
filter
->
get_shape
(),
doutput
,
ng_strides
,
ng_dilations
,
ng_paddings
,
ng_paddings
,
ngraph
::
Strides
{
1
,
1
});
dinput
=
std
::
make_shared
<
ngraph
::
op
::
ConvolutionBackpropData
>
(
input
->
get_shape
(),
filter
,
doutput
,
ng_strides
,
ng_dilations
,
ng_paddings
,
ng_paddings
,
ngraph
::
Strides
{
1
,
1
});
}
else
{
dfilter
=
GroupedGradConvolutionFilter
(
input
,
filter
,
doutput
,
ng_strides
,
ng_dilations
,
ng_paddings
,
groups
);
dinput
=
GroupedGradConvolutionData
(
input
,
filter
,
doutput
,
ng_strides
,
ng_dilations
,
ng_paddings
,
groups
);
}
paddle
::
platform
::
SetOutputNode
(
op
,
"Filter@GRAD"
,
dfilter
,
ngb_node_map
);
paddle
::
platform
::
SetOutputNode
(
op
,
"Input@GRAD"
,
dinput
,
ngb_node_map
);
}
}
// namespace ngraphs
}
// namespace operators
}
// namespace paddle
python/paddle/fluid/tests/unittests/ngraph/test_conv2d_ngraph_op.py
0 → 100644
浏览文件 @
43a67a26
# 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
import
unittest
from
paddle.fluid.tests.unittests.test_conv2d_op
import
*
class
TestNGRAPH
(
TestConv2dOp
):
def
init_kernel_type
(
self
):
super
(
TestNGRAPH
,
self
).
init_kernel_type
()
class
TestNGRAPHWithPad
(
TestWithPad
):
def
init_kernel_type
(
self
):
super
(
TestNGRAPHWithPad
,
self
).
init_kernel_type
()
class
TestNGRAPHWithStride
(
TestWithStride
):
def
init_kernel_type
(
self
):
super
(
TestNGRAPHWithStride
,
self
).
init_kernel_type
()
class
TestNGRAPHWithGroup
(
TestWithGroup
):
def
init_kernel_type
(
self
):
super
(
TestNGRAPHWithGroup
,
self
).
init_kernel_type
()
class
TestNGRAPHWith1x1
(
TestWith1x1
):
def
init_kernel_type
(
self
):
super
(
TestNGRAPHWith1x1
,
self
).
init_kernel_type
()
class
TestNGRAPHWithInput1x1Filter1x1
(
TestWithInput1x1Filter1x1
):
def
init_kernel_type
(
self
):
super
(
TestNGRAPHWithInput1x1Filter1x1
,
self
).
init_kernel_type
()
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录