Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
1198ccae
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
1198ccae
编写于
2月 11, 2019
作者:
M
mozga-intel
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Enable batch_norm operator for a ngraph engine
test=develop
上级
b80bcbb4
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
210 addition
and
0 deletion
+210
-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/batch_norm_op.h
paddle/fluid/operators/ngraph/ops/batch_norm_op.h
+150
-0
paddle/fluid/platform/ngraph_helper.h
paddle/fluid/platform/ngraph_helper.h
+20
-0
python/paddle/fluid/tests/unittests/ngraph/test_batch_norm_ngraph_op.py
...fluid/tests/unittests/ngraph/test_batch_norm_ngraph_op.py
+37
-0
未找到文件。
paddle/fluid/operators/ngraph/ngraph_bridge.cc
浏览文件 @
1198ccae
...
...
@@ -34,6 +34,8 @@ std::map<std::string,
{
"accuracy"
,
NG_OPS
::
BuildAccuracyNode
},
{
"conv2d"
,
NG_OPS
::
BuildConv2dNode
},
{
"conv2d_grad"
,
NG_OPS
::
BuildConv2dGradNode
},
{
"batch_norm"
,
NG_OPS
::
BuildBatchNormNode
},
{
"batch_norm_grad"
,
NG_OPS
::
BuildBatchNormGradNode
},
{
"elementwise_add"
,
NG_OPS
::
BuildElementwiseAddNode
},
{
"elementwise_add_grad"
,
NG_OPS
::
BuildElementwiseAddGradNode
},
{
"fill_constant"
,
NG_OPS
::
BuildFillConstantNode
},
...
...
paddle/fluid/operators/ngraph/ngraph_ops.h
浏览文件 @
1198ccae
...
...
@@ -22,6 +22,7 @@ limitations under the License. */
#pragma once
#include "ops/accuracy_op.h"
#include "ops/batch_norm_op.h"
#include "ops/binary_unary_op.h"
#include "ops/conv2d_op.h"
#include "ops/elementwise_add_op.h"
...
...
paddle/fluid/operators/ngraph/ops/batch_norm_op.h
0 → 100644
浏览文件 @
1198ccae
/*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/operators/ngraph/ops/elementwise_node.h"
#include "paddle/fluid/operators/ngraph/ops/elementwise_scalar_op.h"
#include "paddle/fluid/platform/ngraph_helper.h"
namespace
paddle
{
namespace
operators
{
namespace
ngraphs
{
void
BuildBatchNormNode
(
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
&
data_layout
=
op_attrs
.
Get
<
std
::
string
>
(
"data_layout"
);
auto
bias
=
paddle
::
platform
::
GetInputNode
(
op
,
"Bias"
,
ngb_node_map
);
auto
mean
=
paddle
::
platform
::
GetInputNode
(
op
,
"Mean"
,
ngb_node_map
);
auto
variance
=
paddle
::
platform
::
GetInputNode
(
op
,
"Variance"
,
ngb_node_map
);
auto
scale
=
paddle
::
platform
::
GetInputNode
(
op
,
"Scale"
,
ngb_node_map
);
auto
x
=
paddle
::
platform
::
GetInputNode
(
op
,
"X"
,
ngb_node_map
);
const
bool
is_test
=
op_attrs
.
Get
<
bool
>
(
"is_test"
);
const
float
epsilon
=
op_attrs
.
Get
<
float
>
(
"epsilon"
);
const
float
momentum
=
op_attrs
.
Get
<
float
>
(
"momentum"
);
if
(
data_layout
==
"NHWC"
)
{
x
=
paddle
::
platform
::
Nhwc2Nchw
(
x
);
}
std
::
shared_ptr
<
ngraph
::
Node
>
mean_out
,
saved_mean
,
saved_variance
,
variance_out
,
y
;
if
(
!
is_test
)
{
auto
BN
=
std
::
make_shared
<
ngraph
::
op
::
BatchNormTraining
>
(
epsilon
,
scale
,
bias
,
x
);
y
=
std
::
make_shared
<
ngraph
::
op
::
GetOutputElement
>
(
BN
,
0
);
saved_mean
=
std
::
make_shared
<
ngraph
::
op
::
GetOutputElement
>
(
BN
,
1
);
saved_variance
=
std
::
make_shared
<
ngraph
::
op
::
GetOutputElement
>
(
BN
,
2
);
mean_out
=
std
::
make_shared
<
ngraph
::
op
::
Add
>
(
paddle
::
operators
::
ngraphs
::
ElementwiseScalar
<
ngraph
::
op
::
Multiply
>
(
momentum
,
mean
),
paddle
::
operators
::
ngraphs
::
ElementwiseScalar
<
ngraph
::
op
::
Multiply
>
(
1.
-
momentum
,
saved_mean
));
variance_out
=
std
::
make_shared
<
ngraph
::
op
::
Add
>
(
paddle
::
operators
::
ngraphs
::
ElementwiseScalar
<
ngraph
::
op
::
Multiply
>
(
momentum
,
variance
),
paddle
::
operators
::
ngraphs
::
ElementwiseScalar
<
ngraph
::
op
::
Multiply
>
(
1.
-
momentum
,
saved_variance
));
if
(
data_layout
==
"NHWC"
)
{
y
=
paddle
::
platform
::
Nchw2Nhwc
(
y
);
}
paddle
::
platform
::
SetOutputNode
(
op
,
"MeanOut"
,
mean_out
,
ngb_node_map
);
paddle
::
platform
::
SetOutputNode
(
op
,
"VarianceOut"
,
variance_out
,
ngb_node_map
);
paddle
::
platform
::
SetOutputNode
(
op
,
"SavedMean"
,
saved_mean
,
ngb_node_map
);
paddle
::
platform
::
SetOutputNode
(
op
,
"SavedVariance"
,
saved_variance
,
ngb_node_map
);
paddle
::
platform
::
SetOutputNode
(
op
,
"Y"
,
y
,
ngb_node_map
);
}
else
{
y
=
std
::
make_shared
<
ngraph
::
op
::
BatchNormInference
>
(
epsilon
,
scale
,
bias
,
x
,
mean
,
variance
);
paddle
::
platform
::
SetOutputNode
(
op
,
"Y"
,
y
,
ngb_node_map
);
}
}
void
BuildBatchNormGradNode
(
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
&
data_layout
=
op_attrs
.
Get
<
std
::
string
>
(
"data_layout"
);
auto
bias
=
paddle
::
platform
::
GetInputNode
(
op
,
"Bias"
,
ngb_node_map
);
auto
saved_mean
=
paddle
::
platform
::
GetInputNode
(
op
,
"SavedMean"
,
ngb_node_map
);
auto
saved_variance
=
paddle
::
platform
::
GetInputNode
(
op
,
"SavedVariance"
,
ngb_node_map
);
auto
scale
=
paddle
::
platform
::
GetInputNode
(
op
,
"Scale"
,
ngb_node_map
);
auto
x
=
paddle
::
platform
::
GetInputNode
(
op
,
"X"
,
ngb_node_map
);
auto
dy
=
paddle
::
platform
::
GetInputNode
(
op
,
"Y@GRAD"
,
ngb_node_map
);
auto
x_shape
=
x
->
get_shape
();
auto
dy_shape
=
dy
->
get_shape
();
PADDLE_ENFORCE
(
x_shape
.
size
()
==
2
||
x_shape
.
size
()
==
4
,
"BN grap input size needs to be 2 or 4"
);
PADDLE_ENFORCE_EQ
(
x_shape
.
size
(),
dy_shape
.
size
(),
"BN grap input and delta size needs to be equal"
);
if
(
x_shape
.
size
()
==
2
)
{
x
=
std
::
make_shared
<
ngraph
::
op
::
Reshape
>
(
x
,
ngraph
::
AxisVector
{
0
,
1
},
ngraph
::
Shape
{
x_shape
.
at
(
0
),
x_shape
.
at
(
1
),
1
,
1
});
dy
=
std
::
make_shared
<
ngraph
::
op
::
Reshape
>
(
dy
,
ngraph
::
AxisVector
{
0
,
1
},
ngraph
::
Shape
{
dy_shape
.
at
(
0
),
dy_shape
.
at
(
1
),
1
,
1
});
}
if
(
data_layout
==
"NHWC"
)
{
x
=
paddle
::
platform
::
Nhwc2Nchw
(
dy
);
dy
=
paddle
::
platform
::
Nhwc2Nchw
(
dy
);
}
const
float
epsilon
=
op_attrs
.
Get
<
float
>
(
"epsilon"
);
auto
bn_bprop
=
std
::
make_shared
<
ngraph
::
op
::
BatchNormTrainingBackprop
>
(
epsilon
,
scale
,
bias
,
x
,
saved_mean
,
saved_variance
,
dy
);
std
::
shared_ptr
<
ngraph
::
Node
>
dx
=
std
::
make_shared
<
ngraph
::
op
::
GetOutputElement
>
(
bn_bprop
,
0
);
auto
dscale
=
std
::
make_shared
<
ngraph
::
op
::
GetOutputElement
>
(
bn_bprop
,
1
);
auto
dbias
=
std
::
make_shared
<
ngraph
::
op
::
GetOutputElement
>
(
bn_bprop
,
2
);
paddle
::
platform
::
SetOutputNode
(
op
,
"Bias@GRAD"
,
dbias
,
ngb_node_map
);
paddle
::
platform
::
SetOutputNode
(
op
,
"Scale@GRAD"
,
dscale
,
ngb_node_map
);
if
(
x_shape
.
size
()
==
2
)
{
paddle
::
platform
::
SetOutputNode
(
op
,
"X@GRAD"
,
paddle
::
platform
::
NgReshaper
(
dx
,
x_shape
),
ngb_node_map
);
}
else
{
if
(
data_layout
==
"NHWC"
)
{
dx
=
paddle
::
platform
::
Nchw2Nhwc
(
dx
);
}
paddle
::
platform
::
SetOutputNode
(
op
,
"X@GRAD"
,
dx
,
ngb_node_map
);
}
}
}
// namespace ngraphs
}
// namespace operators
}
// namespace paddle
paddle/fluid/platform/ngraph_helper.h
浏览文件 @
1198ccae
...
...
@@ -23,6 +23,26 @@ limitations under the License. */
namespace
paddle
{
namespace
platform
{
std
::
shared_ptr
<
ngraph
::
Node
>
Nhwc2Nchw
(
std
::
shared_ptr
<
ngraph
::
Node
>
in
)
{
auto
in_shape
=
in
->
get_shape
();
in_shape
[
0
]
=
in
->
get_shape
()[
0
];
in_shape
[
1
]
=
in
->
get_shape
()[
3
];
in_shape
[
2
]
=
in
->
get_shape
()[
1
];
in_shape
[
3
]
=
in
->
get_shape
()[
2
];
ngraph
::
AxisVector
axis_vec
=
{
0
,
3
,
1
,
2
};
return
std
::
make_shared
<
ngraph
::
op
::
Reshape
>
(
in
,
axis_vec
,
in_shape
);
}
std
::
shared_ptr
<
ngraph
::
Node
>
Nchw2Nhwc
(
std
::
shared_ptr
<
ngraph
::
Node
>
in
)
{
auto
in_shape
=
in
->
get_shape
();
in_shape
[
0
]
=
in
->
get_shape
()[
0
];
in_shape
[
1
]
=
in
->
get_shape
()[
2
];
in_shape
[
2
]
=
in
->
get_shape
()[
3
];
in_shape
[
3
]
=
in
->
get_shape
()[
1
];
ngraph
::
AxisVector
axis_vec
=
{
0
,
2
,
3
,
1
};
return
std
::
make_shared
<
ngraph
::
op
::
Reshape
>
(
in
,
axis_vec
,
in_shape
);
}
ngraph
::
Shape
FlattenTo2d
(
ngraph
::
Shape
sh
,
int
num
)
{
auto
x1
=
std
::
accumulate
(
std
::
begin
(
sh
),
std
::
begin
(
sh
)
+
num
,
1
,
std
::
multiplies
<
size_t
>
());
...
...
python/paddle/fluid/tests/unittests/ngraph/test_batch_norm_ngraph_op.py
0 → 100644
浏览文件 @
1198ccae
# 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_batch_norm_op
import
TestBatchNormOpTraining
,
TestBatchNormOpInference
class
TestNGRAPHBatchNormOpTraining
(
TestBatchNormOpTraining
):
def
init_kernel_type
(
self
):
super
(
TestNGRAPHBatchNormOpTraining
,
self
).
init_kernel_type
()
class
TestNGRAPHBatchNormOpInference
(
TestBatchNormOpInference
):
def
init_kernel_type
(
self
):
super
(
TestNGRAPHBatchNormOpInference
,
self
).
init_kernel_type
()
class
TestNGRAPHBatchNormOpWithReluInference
(
TestBatchNormOpInference
):
def
init_kernel_type
(
self
):
super
(
TestNGRAPHBatchNormOpWithReluInference
,
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录