Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
c9e874fc
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
c9e874fc
编写于
12月 23, 2020
作者:
J
Jacek Czaja
提交者:
GitHub
12月 23, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[oneDNN] Unit test for checking oneDNN caching (#29606)
上级
09b6e719
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
184 addition
and
0 deletion
+184
-0
paddle/fluid/operators/CMakeLists.txt
paddle/fluid/operators/CMakeLists.txt
+1
-0
paddle/fluid/operators/mkldnn/caching_tests.cmake
paddle/fluid/operators/mkldnn/caching_tests.cmake
+1
-0
paddle/fluid/operators/mkldnn/test_mkldnn_caching.cc
paddle/fluid/operators/mkldnn/test_mkldnn_caching.cc
+169
-0
paddle/fluid/platform/device_context.cc
paddle/fluid/platform/device_context.cc
+10
-0
paddle/fluid/platform/device_context.h
paddle/fluid/platform/device_context.h
+3
-0
未找到文件。
paddle/fluid/operators/CMakeLists.txt
浏览文件 @
c9e874fc
...
...
@@ -155,6 +155,7 @@ cc_test(op_debug_string_test SRCS op_debug_string_test.cc DEPS elementwise_add_o
if
(
WITH_MKLDNN
)
include
(
mkldnn/inplace_op_tests.cmake
)
include
(
mkldnn/caching_tests.cmake
)
include
(
mkldnn/nhwc_op_tests.cmake
)
endif
()
...
...
paddle/fluid/operators/mkldnn/caching_tests.cmake
0 → 100644
浏览文件 @
c9e874fc
cc_test
(
test_mkldnn_caching SRCS mkldnn/test_mkldnn_caching.cc DEPS op_registry elementwise_add_op activation_op softmax_op softmax scope device_context enforce
)
paddle/fluid/operators/mkldnn/test_mkldnn_caching.cc
0 → 100644
浏览文件 @
c9e874fc
// Copyright (c) 2020 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 <map>
#include <random>
#include <string>
#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
);
USE_OP_DEVICE_KERNEL
(
elementwise_add
,
MKLDNN
);
USE_OP
(
relu
);
USE_OP_DEVICE_KERNEL
(
relu
,
MKLDNN
);
USE_OP
(
softmax
);
USE_OP_DEVICE_KERNEL
(
softmax
,
MKLDNN
);
namespace
paddle
{
namespace
operators
{
struct
InputVars
{
std
::
string
name
;
framework
::
LoDTensor
*
tensor
;
};
class
CacheTester
{
public:
CacheTester
()
{
// Clear oneDNN cache
auto
&
pool
=
platform
::
DeviceContextPool
::
Instance
();
platform
::
CPUPlace
place
;
onednn_dev_ctx_
=
dynamic_cast
<
platform
::
MKLDNNDeviceContext
*>
(
pool
.
Get
(
place
));
onednn_dev_ctx_
->
ResetBlobMap
();
}
bool
Analyze
(
unsigned
short
int
num_entries
)
{
// Number of created objects in cache should be as expected (num_entries)
return
onednn_dev_ctx_
->
GetCachedObjectsNumber
()
==
num_entries
;
}
private:
platform
::
MKLDNNDeviceContext
*
onednn_dev_ctx_
;
};
template
<
typename
T
>
void
RunOperator
(
const
platform
::
Place
&
place
,
const
std
::
string
&
op_type
,
const
framework
::
DDim
&
dims
,
const
std
::
string
&
output_name
,
bool
inplace
=
false
)
{
framework
::
Scope
scope
;
std
::
map
<
const
std
::
string
,
int
>
num_inputs
=
{
{
"softmax"
,
1
},
{
"relu"
,
1
},
{
"elementwise_add"
,
2
}};
std
::
string
first_input
=
inplace
==
true
?
output_name
:
"x"
;
std
::
vector
<
InputVars
>
input_names
=
{
{
first_input
,
scope
.
Var
(
first_input
)
->
GetMutable
<
framework
::
LoDTensor
>
()},
{
"x1"
,
num_inputs
[
op_type
]
>
1
?
scope
.
Var
(
"x1"
)
->
GetMutable
<
framework
::
LoDTensor
>
()
:
nullptr
},
{
"x2"
,
num_inputs
[
op_type
]
>
2
?
scope
.
Var
(
"x2"
)
->
GetMutable
<
framework
::
LoDTensor
>
()
:
nullptr
},
{
"x3"
,
num_inputs
[
op_type
]
>
3
?
scope
.
Var
(
"x3"
)
->
GetMutable
<
framework
::
LoDTensor
>
()
:
nullptr
},
{
"x4"
,
num_inputs
[
op_type
]
>
4
?
scope
.
Var
(
"x4"
)
->
GetMutable
<
framework
::
LoDTensor
>
()
:
nullptr
}};
auto
*
y
=
scope
.
Var
(
output_name
)
->
GetMutable
<
framework
::
LoDTensor
>
();
// Initialize input data
std
::
uniform_real_distribution
<
T
>
dist
(
static_cast
<
T
>
(
10.0
),
static_cast
<
T
>
(
20.0
));
std
::
mt19937
engine
;
size_t
numel
=
static_cast
<
size_t
>
(
framework
::
product
(
dims
));
for
(
int
i
=
0
;
i
<
num_inputs
[
op_type
];
++
i
)
{
input_names
[
i
].
tensor
->
Resize
(
dims
);
auto
data_ptr
=
input_names
[
i
].
tensor
->
mutable_data
<
T
>
(
place
);
for
(
size_t
i
=
0
;
i
<
numel
;
++
i
)
{
data_ptr
[
i
]
=
dist
(
engine
);
}
}
// Initialize output
y
->
Resize
(
dims
);
auto
y_ptr
=
y
->
mutable_data
<
T
>
(
place
);
for
(
size_t
i
=
0
;
i
<
numel
;
++
i
)
{
y_ptr
[
i
]
=
static_cast
<
T
>
(
0
);
}
auto
&
pool
=
platform
::
DeviceContextPool
::
Instance
();
auto
op
=
num_inputs
[
op_type
]
>
1
?
framework
::
OpRegistry
::
CreateOp
(
op_type
,
{{
"X"
,
{
first_input
}},
{
"Y"
,
{
"x1"
}}},
{{
"Out"
,
{
output_name
}}},
{{
"use_mkldnn"
,
{
true
}}})
:
framework
::
OpRegistry
::
CreateOp
(
op_type
,
{{
"X"
,
{
first_input
}}},
{{
"Out"
,
{
output_name
}}},
{{
"use_mkldnn"
,
{
true
}}});
op
->
Run
(
scope
,
place
);
pool
.
Get
(
place
)
->
Wait
();
}
TEST
(
test_softmax_reuse_cache
,
cpu_place
)
{
framework
::
DDim
dims
({
32
,
64
});
platform
::
CPUPlace
p
;
CacheTester
ct
;
RunOperator
<
float
>
(
p
,
"softmax"
,
dims
,
"softmax_out"
);
RunOperator
<
float
>
(
p
,
"softmax"
,
dims
,
"softmax_out"
);
PADDLE_ENFORCE_EQ
(
ct
.
Analyze
(
4
),
true
,
platform
::
errors
::
InvalidArgument
(
"Wrong number of cached oneDNN objects"
));
}
TEST
(
test_softmax_noreuse_cache
,
cpu_place
)
{
framework
::
DDim
dims
({
32
,
64
});
platform
::
CPUPlace
p
;
CacheTester
ct
;
RunOperator
<
float
>
(
p
,
"softmax"
,
dims
,
"softmax_out"
);
RunOperator
<
float
>
(
p
,
"softmax"
,
dims
,
"softmax_out2"
);
PADDLE_ENFORCE_EQ
(
ct
.
Analyze
(
8
),
true
,
platform
::
errors
::
InvalidArgument
(
"Wrong number of cached oneDNN objects"
));
}
TEST
(
test_softmax_inplace_cache
,
cpu_place
)
{
framework
::
DDim
dims
({
32
,
64
});
platform
::
CPUPlace
p
;
CacheTester
ct
;
RunOperator
<
float
>
(
p
,
"softmax"
,
dims
,
"softmax_out"
);
RunOperator
<
float
>
(
p
,
"softmax"
,
dims
,
"softmax_out"
,
true
);
PADDLE_ENFORCE_EQ
(
ct
.
Analyze
(
4
),
true
,
platform
::
errors
::
InvalidArgument
(
"Wrong number of cached oneDNN objects"
));
}
TEST
(
test_elementwise_add_reuse_cache
,
cpu_place
)
{
framework
::
DDim
dims
({
32
,
64
});
platform
::
CPUPlace
p
;
CacheTester
ct
;
RunOperator
<
float
>
(
p
,
"elementwise_add"
,
dims
,
"elementwise_add_out"
);
RunOperator
<
float
>
(
p
,
"relu"
,
dims
,
"elementwise_add_out"
,
true
);
PADDLE_ENFORCE_EQ
(
ct
.
Analyze
(
8
),
true
,
platform
::
errors
::
InvalidArgument
(
"Wrong number of cached oneDNN objects"
));
}
}
// namespace operators
}
// namespace paddle
paddle/fluid/platform/device_context.cc
浏览文件 @
c9e874fc
...
...
@@ -581,6 +581,16 @@ void MKLDNNDeviceContext::SetBlob(const std::string& name,
return
;
}
unsigned
int
MKLDNNDeviceContext
::
GetCachedObjectsNumber
(
void
)
{
unsigned
int
num_entries
=
0
;
for
(
auto
const
&
l3
:
*
p_blobmap_
)
{
for
(
auto
const
&
l2
:
*
(
l3
.
second
))
{
num_entries
+=
(
l2
.
second
)
->
size
();
}
}
return
num_entries
;
}
MKLDNNDeviceContext
::
BlobPtr_t
<
void
>
MKLDNNDeviceContext
::
GetBlob
(
const
std
::
string
&
name
)
const
{
BlobMap
*
pMap
=
p_blobmap_
.
get
();
...
...
paddle/fluid/platform/device_context.h
浏览文件 @
c9e874fc
...
...
@@ -564,6 +564,9 @@ class MKLDNNDeviceContext : public CPUDeviceContext {
// Set data to blob (i.e. name/data pair). Create blob if not existing
void
SetBlob
(
const
std
::
string
&
name
,
std
::
shared_ptr
<
void
>
data
)
const
;
// Calculate number of oneDNN objects cached
unsigned
int
GetCachedObjectsNumber
(
void
);
// Find a saved blob. Return nullptr if not found
std
::
shared_ptr
<
void
>
GetBlob
(
const
std
::
string
&
name
)
const
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录