Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
72652845
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
提交
72652845
编写于
1月 03, 2018
作者:
T
tensor-tang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add MKLDNNDeviceContext
上级
cbe25b33
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
127 addition
and
0 deletion
+127
-0
paddle/platform/device_context.cc
paddle/platform/device_context.cc
+64
-0
paddle/platform/device_context.h
paddle/platform/device_context.h
+63
-0
未找到文件。
paddle/platform/device_context.cc
浏览文件 @
72652845
...
...
@@ -176,5 +176,69 @@ cudnnHandle_t CUDNNDeviceContext::cudnn_handle() const { return cudnn_handle_; }
#endif
#ifdef PADDLE_WITH_MKLDNN
MKLDNNDeviceContext
::
MKLDNNDeviceContext
(
CPUPlace
place
)
:
CPUDeviceContext
(
place
),
ready_
(
false
)
{
stream_
.
reset
(
new
mkldnn
::
stream
(
mkldnn
::
stream
::
kind
::
eager
));
engine_
.
reset
(
new
mkldnn
::
engine
(
mkldnn
::
engine
::
cpu
,
0
));
}
template
<
typename
T
>
void
MKLDNNDeviceContext
::
AddElement
(
const
std
::
string
&
op_key
,
const
T
&
value
)
{
if
(
GetElement
<
T
>
(
op_key
))
{
return
;
}
GetElementPool
<
T
>
().
emplace
(
op_key
,
value
);
}
template
<
typename
T
>
const
T
MKLDNNDeviceContext
::
GetElement
(
const
std
::
string
&
op_key
)
const
{
auto
it
=
GetElementPool
<
T
>
().
find
(
op_key
);
return
it
==
GetElementPool
<
T
>
().
end
()
?
nullptr
:
it
->
second
;
}
template
<
>
const
std
::
unordered_map
<
const
std
::
string
,
const
MKLDNNMemoryPtr
,
std
::
hash
<
std
::
string
>>&
MKLDNNDeviceContext
::
GetElementPool
<
MKLDNNMemoryPtr
>
()
const
{
return
memory_pool_
;
}
template
<
>
const
std
::
unordered_map
<
const
std
::
string
,
const
MKLDNNPrimitivePtr
,
std
::
hash
<
std
::
string
>>&
MKLDNNDeviceContext
::
GetElementPool
<
MKLDNNPrimitivePtr
>
()
const
{
return
primitive_pool_
;
}
template
<
>
const
std
::
unordered_map
<
const
std
::
string
,
const
MKLDNNPrimitiveDescPtr
,
std
::
hash
<
std
::
string
>>&
MKLDNNDeviceContext
::
GetElementPool
<
MKLDNNPrimitiveDescPtr
>
()
const
{
return
primitive_desc_pool_
;
}
void
MKLDNNDeviceContext
::
Execute
(
bool
block
)
{
if
(
pipeline_
.
empty
())
{
return
;
}
ResetStream
();
stream_
->
submit
(
pipeline_
).
wait
(
block
);
ready_
=
false
;
pipeline_
.
clear
();
}
void
MKLDNNDeviceContext
::
ResetStream
()
{
if
(
ready_
)
{
return
;
}
// TODO(TJ): change me when mkldnn have specific method to reset this state
stream_
.
reset
(
new
mkldnn
::
stream
(
mkldnn
::
stream
::
kind
::
eager
));
ready_
=
true
;
}
#endif
}
// namespace platform
}
// namespace paddle
paddle/platform/device_context.h
浏览文件 @
72652845
...
...
@@ -21,6 +21,10 @@ limitations under the License. */
#define EIGEN_USE_GPU
#endif
#ifdef PADDLE_WITH_MKLDNN
#include "mkldnn.hpp"
#endif
#include "paddle/platform/enforce.h"
#include "paddle/platform/place.h"
#include "unsupported/Eigen/CXX11/Tensor"
...
...
@@ -117,6 +121,65 @@ class CUDNNDeviceContext : public CUDADeviceContext {
#endif
#ifdef PADDLE_WITH_MKLDNN
using
MKLDNNStream
=
mkldnn
::
stream
;
using
MKLDNNEngine
=
mkldnn
::
engine
;
using
MKLDNNMemory
=
mkldnn
::
memory
;
using
MKLDNNPrimitive
=
mkldnn
::
primitive
;
using
MKLDNNPrimitiveDesc
=
mkldnn
::
handle
<
mkldnn_primitive_desc_t
>
;
typedef
std
::
shared_ptr
<
MKLDNNEngine
>
MKLDNNEnginePtr
;
typedef
std
::
shared_ptr
<
MKLDNNMemory
>
MKLDNNMemoryPtr
;
typedef
std
::
shared_ptr
<
MKLDNNPrimitive
>
MKLDNNPrimitivePtr
;
typedef
std
::
shared_ptr
<
MKLDNNPrimitiveDesc
>
MKLDNNPrimitiveDescPtr
;
class
MKLDNNDeviceContext
:
public
CPUDeviceContext
{
public:
explicit
MKLDNNDeviceContext
(
CPUPlace
place
);
virtual
~
MKLDNNDeviceContext
();
/* \brief Add new element: memory, primitive or primitive desc */
template
<
typename
T
>
void
AddElement
(
const
std
::
string
&
op_key
,
const
T
&
value
);
/* \brief Get existed element: memory, primitive or primitive desc */
template
<
typename
T
>
const
T
GetElement
(
const
std
::
string
&
op_key
)
const
;
/* \brief Get element pool: memory, primitive or primitive desc pool */
template
<
typename
T
>
const
std
::
unordered_map
<
const
std
::
string
,
const
T
,
std
::
hash
<
std
::
string
>>&
GetElementPool
()
const
;
/* \brief Get the active engine */
const
MKLDNNEnginePtr
GetEngine
()
const
{
return
engine_
;
}
/* \brief Submit primitive to pipeline */
void
Submit
(
const
MKLDNNPrimitivePtr
&
p
)
{
pipeline_
.
push_back
(
*
p
);
}
/*! \brief Execute all submitted primitives in pipeline */
void
Execute
(
bool
block
=
true
);
protected:
/*! \brief Reset the stream to prepare next exectue */
void
ResetStream
();
private:
std
::
unordered_map
<
const
std
::
string
,
const
MKLDNNMemoryPtr
,
std
::
hash
<
std
::
string
>>
memory_pool_
;
std
::
unordered_map
<
const
std
::
string
,
const
MKLDNNPrimitivePtr
,
std
::
hash
<
std
::
string
>>
primitive_pool_
;
std
::
unordered_map
<
const
std
::
string
,
const
MKLDNNPrimitiveDescPtr
,
std
::
hash
<
std
::
string
>>
primitive_desc_pool_
;
std
::
vector
<
MKLDNNPrimitive
>
pipeline_
;
std
::
unique_ptr
<
MKLDNNStream
>
stream_
;
MKLDNNEnginePtr
engine_
;
bool
ready_
;
};
#endif
/*! \brief device context pool singleton */
class
DeviceContextPool
{
public:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录