Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
f5641000
P
Paddle
项目概览
PaddlePaddle
/
Paddle
12 个月 前同步成功
通知
2292
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
f5641000
编写于
6月 28, 2019
作者:
Z
Zeng Jinle
提交者:
GitHub
6月 28, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add a unittest to inplace elementwise_add (#18385)
* add_elementwise_add_inplace_test,test=develop * rename file, test=develop
上级
43f64a17
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
144 addition
and
2 deletion
+144
-2
paddle/fluid/operators/elementwise/CMakeLists.txt
paddle/fluid/operators/elementwise/CMakeLists.txt
+2
-0
paddle/fluid/operators/elementwise/test_elementwise_add_op_inplace.cc
.../operators/elementwise/test_elementwise_add_op_inplace.cc
+136
-0
paddle/fluid/operators/math/blas_impl.h
paddle/fluid/operators/math/blas_impl.h
+6
-2
未找到文件。
paddle/fluid/operators/elementwise/CMakeLists.txt
浏览文件 @
f5641000
include
(
operators
)
include
(
operators
)
register_operators
()
register_operators
()
cc_test
(
test_elementwise_add_op_inplace SRCS test_elementwise_add_op_inplace.cc DEPS op_registry elementwise_add_op scope device_context enforce executor
)
paddle/fluid/operators/elementwise/test_elementwise_add_op_inplace.cc
0 → 100644
浏览文件 @
f5641000
// Copyright (c) 2019 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.
#include <algorithm>
#include <cstdlib>
#include <memory>
#include <random>
#include "gtest/gtest.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/place.h"
USE_OP
(
elementwise_add
);
namespace
paddle
{
namespace
operators
{
static
void
Memcpy
(
void
*
dst
,
const
void
*
src
,
size_t
n
,
bool
copy_to_gpu
)
{
if
(
copy_to_gpu
)
{
#ifdef PADDLE_WITH_CUDA
PADDLE_ENFORCE
(
cudaMemcpy
(
dst
,
src
,
n
,
cudaMemcpyHostToDevice
));
#else
PADDLE_THROW
(
"Not compiled with cuda"
);
#endif
}
else
{
std
::
memcpy
(
dst
,
src
,
n
);
}
}
template
<
typename
T
>
bool
TestMain
(
const
platform
::
Place
&
place
,
const
framework
::
DDim
&
dims
,
bool
inplace
)
{
framework
::
Scope
scope
;
auto
*
x
=
scope
.
Var
(
"x"
)
->
GetMutable
<
framework
::
LoDTensor
>
();
auto
*
y
=
scope
.
Var
(
"y"
)
->
GetMutable
<
framework
::
LoDTensor
>
();
auto
*
z
=
scope
.
Var
(
"z"
)
->
GetMutable
<
framework
::
LoDTensor
>
();
x
->
Resize
(
dims
);
y
->
Resize
(
dims
);
z
->
Resize
(
dims
);
size_t
numel
=
static_cast
<
size_t
>
(
framework
::
product
(
dims
));
auto
x_ptr
=
x
->
mutable_data
<
T
>
(
place
);
auto
y_ptr
=
y
->
mutable_data
<
T
>
(
place
);
auto
z_ptr
=
z
->
mutable_data
<
T
>
(
place
);
std
::
uniform_real_distribution
<
T
>
dist
(
static_cast
<
T
>
(
10.0
),
static_cast
<
T
>
(
20.0
));
std
::
mt19937
engine
;
std
::
vector
<
T
>
x_data
(
numel
),
y_data
(
numel
),
z_data
(
numel
);
std
::
vector
<
T
>
sum_result
(
numel
);
for
(
size_t
i
=
0
;
i
<
numel
;
++
i
)
{
x_data
[
i
]
=
dist
(
engine
);
y_data
[
i
]
=
dist
(
engine
);
sum_result
[
i
]
=
x_data
[
i
]
+
y_data
[
i
];
z_data
[
i
]
=
-
1.0
;
// set some data that is not existed
}
auto
bytes
=
sizeof
(
T
)
*
numel
;
bool
is_gpu_place
=
platform
::
is_gpu_place
(
place
);
Memcpy
(
x_ptr
,
x_data
.
data
(),
bytes
,
is_gpu_place
);
Memcpy
(
y_ptr
,
y_data
.
data
(),
bytes
,
is_gpu_place
);
Memcpy
(
z_ptr
,
z_data
.
data
(),
bytes
,
is_gpu_place
);
const
char
*
out_name
=
inplace
?
"x"
:
"z"
;
auto
op
=
framework
::
OpRegistry
::
CreateOp
(
"elementwise_add"
,
{{
"X"
,
{
"x"
}},
{
"Y"
,
{
"y"
}}},
{{
"Out"
,
{
out_name
}}},
{});
op
->
Run
(
scope
,
place
);
platform
::
DeviceContextPool
::
Instance
().
Get
(
place
)
->
Wait
();
framework
::
LoDTensor
cpu_out
;
auto
&
out_tensor
=
scope
.
FindVar
(
out_name
)
->
Get
<
framework
::
LoDTensor
>
();
PADDLE_ENFORCE
(
scope
.
kids
().
empty
());
if
(
inplace
)
{
PADDLE_ENFORCE_EQ
(
&
out_tensor
,
x
);
}
else
{
PADDLE_ENFORCE_EQ
(
&
out_tensor
,
z
);
}
if
(
is_gpu_place
)
{
framework
::
TensorCopySync
(
out_tensor
,
platform
::
CPUPlace
(),
&
cpu_out
);
}
else
{
cpu_out
=
out_tensor
;
}
auto
*
out_ptr
=
cpu_out
.
data
<
T
>
();
bool
is_equal
=
std
::
equal
(
out_ptr
,
out_ptr
+
numel
,
sum_result
.
data
());
return
is_equal
;
}
TEST
(
test_elementwise_add_inplace
,
cpu_place
)
{
framework
::
DDim
dims
({
32
,
64
});
platform
::
CPUPlace
p
;
ASSERT_TRUE
(
TestMain
<
float
>
(
p
,
dims
,
true
));
}
TEST
(
test_elementwise_add_not_inplace
,
cpu_place
)
{
framework
::
DDim
dims
({
32
,
64
});
platform
::
CPUPlace
p
;
ASSERT_TRUE
(
TestMain
<
float
>
(
p
,
dims
,
false
));
}
#ifdef PADDLE_WITH_CUDA
TEST
(
test_elementwise_add_inplace
,
gpu_place
)
{
framework
::
DDim
dims
({
32
,
64
});
platform
::
CUDAPlace
p
(
0
);
ASSERT_TRUE
(
TestMain
<
float
>
(
p
,
dims
,
true
));
}
TEST
(
test_elementwise_add_not_inplace
,
gpu_place
)
{
framework
::
DDim
dims
({
32
,
64
});
platform
::
CUDAPlace
p
(
0
);
ASSERT_TRUE
(
TestMain
<
float
>
(
p
,
dims
,
false
));
}
#endif
}
// namespace operators
}
// namespace paddle
paddle/fluid/operators/math/blas_impl.h
浏览文件 @
f5641000
...
@@ -422,8 +422,12 @@ void Blas<platform::CPUDeviceContext>::VADD(int n, const T *x, const T *y,
...
@@ -422,8 +422,12 @@ void Blas<platform::CPUDeviceContext>::VADD(int n, const T *x, const T *y,
#ifdef PADDLE_WITH_MKLML
#ifdef PADDLE_WITH_MKLML
CBlas
<
T
>::
VADD
(
n
,
x
,
y
,
z
);
CBlas
<
T
>::
VADD
(
n
,
x
,
y
,
z
);
#else
#else
this
->
template
VCOPY
<
T
>(
n
,
y
,
z
);
if
(
x
==
z
)
{
this
->
template
AXPY
<
T
>(
n
,
1.
,
x
,
z
);
this
->
template
AXPY
<
T
>(
n
,
1.
,
y
,
z
);
}
else
{
this
->
template
VCOPY
<
T
>(
n
,
y
,
z
);
this
->
template
AXPY
<
T
>(
n
,
1.
,
x
,
z
);
}
#endif
#endif
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录