Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
53b0e427
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看板
提交
53b0e427
编写于
8月 17, 2017
作者:
H
hedaoyuan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add EigenGemm.
上级
9eaef753
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
211 addition
and
65 deletion
+211
-65
paddle/function/EigenGemm.cpp
paddle/function/EigenGemm.cpp
+92
-0
paddle/function/GemmFunctor.cpp
paddle/function/GemmFunctor.cpp
+85
-0
paddle/function/GemmFunctor.h
paddle/function/GemmFunctor.h
+34
-65
未找到文件。
paddle/function/EigenGemm.cpp
0 → 100644
浏览文件 @
53b0e427
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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 <glog/logging.h>
#include "unsupported/Eigen/CXX11/Tensor"
namespace
paddle
{
template
<
class
T
>
struct
EigenBlasGemm
{
typedef
Eigen
::
TensorMap
<
Eigen
::
Tensor
<
T
,
2
,
Eigen
::
RowMajor
,
int
>
,
Eigen
::
Aligned
>
Matrix
;
static
void
compute
(
const
bool
transA
,
const
bool
transB
,
const
int
M
,
const
int
N
,
const
int
K
,
const
T
alpha
,
const
T
*
A
,
const
int
lda
,
const
T
*
B
,
const
int
ldb
,
const
T
beta
,
T
*
C
,
const
int
ldc
)
{
Eigen
::
array
<
int
,
2
>
sizeA
;
if
(
transA
)
{
sizeA
[
0
]
=
K
;
sizeA
[
1
]
=
M
;
CHECK_EQ
(
M
,
lda
);
}
else
{
sizeA
[
0
]
=
M
;
sizeA
[
1
]
=
K
;
CHECK_EQ
(
K
,
lda
);
}
Eigen
::
array
<
int
,
2
>
sizeB
;
if
(
transB
)
{
sizeB
[
0
]
=
N
;
sizeB
[
1
]
=
K
;
CHECK_EQ
(
K
,
ldb
);
}
else
{
sizeB
[
0
]
=
K
;
sizeB
[
1
]
=
N
;
CHECK_EQ
(
N
,
ldb
);
}
Eigen
::
array
<
int
,
2
>
sizeC
;
sizeC
[
0
]
=
M
;
sizeC
[
1
]
=
N
;
CHECK_EQ
(
N
,
ldc
);
const
Matrix
a
(
const_cast
<
T
*>
(
A
),
sizeA
);
const
Matrix
b
(
const_cast
<
T
*>
(
B
),
sizeB
);
Matrix
c
(
C
,
sizeC
);
typedef
typename
Eigen
::
Tensor
<
T
,
2
>::
DimensionPair
DimPair
;
Eigen
::
array
<
DimPair
,
1
>
dims
;
dims
[
0
]
=
DimPair
(
1
,
0
);
dims
[
0
].
first
=
transA
?
0
:
1
;
dims
[
0
].
second
=
transB
?
1
:
0
;
Eigen
::
DefaultDevice
device
;
if
(
alpha
==
T
(
1
)
&&
beta
==
T
(
0
))
{
c
.
device
(
device
)
=
a
.
contract
(
b
,
dims
);
}
else
if
(
alpha
==
T
(
1
)
&&
beta
==
T
(
1
))
{
c
.
device
(
device
)
+=
a
.
contract
(
b
,
dims
);
}
else
{
c
.
device
(
device
)
=
c
.
constant
(
alpha
)
*
a
.
contract
(
b
,
dims
)
+
c
.
constant
(
beta
)
*
c
;
}
}
};
#ifdef PADDLE_TYPE_DOUBLE
template
class
EigenBlasGemm
<
double
>;
#else
template
class
EigenBlasGemm
<
float
>;
#endif
}
// namespace paddle
paddle/function/GemmFunctor.cpp
0 → 100644
浏览文件 @
53b0e427
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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 "GemmFunctor.h"
#include "paddle/math/MathFunctions.h"
namespace
paddle
{
template
<
class
T
>
struct
BlasGemm
<
DEVICE_TYPE_CPU
,
T
>
{
static
void
compute
(
const
bool
transA
,
const
bool
transB
,
const
int
M
,
const
int
N
,
const
int
K
,
const
T
alpha
,
const
T
*
A
,
const
int
lda
,
const
T
*
B
,
const
int
ldb
,
const
T
beta
,
T
*
C
,
const
int
ldc
)
{
gemm
<
T
>
(
transA
==
false
?
CblasNoTrans
:
CblasTrans
,
transB
==
false
?
CblasNoTrans
:
CblasTrans
,
M
,
N
,
K
,
alpha
,
A
,
lda
,
B
,
ldb
,
beta
,
C
,
ldc
);
}
};
template
<
class
T
>
struct
BlasGemm
<
DEVICE_TYPE_GPU
,
T
>
{
static
void
compute
(
const
bool
transA
,
const
bool
transB
,
const
int
M
,
const
int
N
,
const
int
K
,
const
T
alpha
,
const
T
*
A
,
const
int
lda
,
const
T
*
B
,
const
int
ldb
,
const
T
beta
,
T
*
C
,
const
int
ldc
)
{
hl_matrix_mul
((
T
*
)
A
,
transA
==
false
?
HPPL_OP_N
:
HPPL_OP_T
,
(
T
*
)
B
,
transB
==
false
?
HPPL_OP_N
:
HPPL_OP_T
,
C
,
M
,
N
,
K
,
alpha
,
beta
,
lda
,
ldb
,
ldc
);
}
};
template
class
BlasGemm
<
DEVICE_TYPE_CPU
,
real
>;
template
class
BlasGemm
<
DEVICE_TYPE_GPU
,
real
>;
}
// namespace paddle
paddle/function/GemmFunctor.h
浏览文件 @
53b0e427
...
@@ -14,7 +14,7 @@ limitations under the License. */
...
@@ -14,7 +14,7 @@ limitations under the License. */
#pragma once
#pragma once
#include "
paddle/math/MathFunctions
.h"
#include "
TensorType
.h"
namespace
paddle
{
namespace
paddle
{
...
@@ -24,73 +24,42 @@ namespace paddle {
...
@@ -24,73 +24,42 @@ namespace paddle {
// of MatMulFunction, we need to consider the reconstruction of hl_matrix_mul
// of MatMulFunction, we need to consider the reconstruction of hl_matrix_mul
// interface.
// interface.
template
<
DeviceType
Device
,
class
T
>
template
<
DeviceType
Device
,
class
T
>
class
GemmFunctor
{
struct
BlasGemm
{
public:
static
void
compute
(
const
bool
transA
,
void
operator
()(
const
CBLAS_TRANSPOSE
transA
,
const
bool
transB
,
const
CBLAS_TRANSPOSE
TransB
,
const
int
M
,
const
int
M
,
const
int
N
,
const
int
N
,
const
int
K
,
const
int
K
,
const
T
alpha
,
const
T
alpha
,
const
T
*
A
,
const
T
*
A
,
const
int
lda
,
const
int
lda
,
const
T
*
B
,
const
T
*
B
,
const
int
ldb
,
const
int
ldb
,
const
T
beta
,
const
T
beta
,
T
*
C
,
T
*
C
,
const
int
ldc
);
const
int
ldc
);
};
};
// TODO(hedaoyuan): Since the definition of the real type in the Paddle
// conflicts with the Eigen library, so compile the Eigen code can not
// include the Paddle header file. And need an EigenBlasGemm template class
// that does not contain the DeviceType parameter.
// I will fix this problem and merge BlasGemm and EigenBlasGemm into one.
template
<
class
T
>
template
<
class
T
>
class
GemmFunctor
<
DEVICE_TYPE_CPU
,
T
>
{
struct
EigenBlasGemm
{
public:
static
void
compute
(
const
bool
transA
,
void
operator
()(
const
CBLAS_TRANSPOSE
transA
,
const
bool
transB
,
const
CBLAS_TRANSPOSE
TransB
,
const
int
M
,
const
int
M
,
const
int
N
,
const
int
N
,
const
int
K
,
const
int
K
,
const
T
alpha
,
const
T
alpha
,
const
T
*
A
,
const
T
*
A
,
const
int
lda
,
const
int
lda
,
const
T
*
B
,
const
T
*
B
,
const
int
ldb
,
const
int
ldb
,
const
T
beta
,
const
T
beta
,
T
*
C
,
T
*
C
,
const
int
ldc
);
const
int
ldc
)
{
gemm
<
T
>
(
transA
,
TransB
,
M
,
N
,
K
,
alpha
,
A
,
lda
,
B
,
ldb
,
beta
,
C
,
ldc
);
}
};
template
<
class
T
>
class
GemmFunctor
<
DEVICE_TYPE_GPU
,
T
>
{
public:
void
operator
()(
const
CBLAS_TRANSPOSE
transA
,
const
CBLAS_TRANSPOSE
TransB
,
const
int
M
,
const
int
N
,
const
int
K
,
const
T
alpha
,
const
T
*
A
,
const
int
lda
,
const
T
*
B
,
const
int
ldb
,
const
T
beta
,
T
*
C
,
const
int
ldc
)
{
hl_matrix_mul
((
T
*
)
A
,
transA
==
CblasNoTrans
?
HPPL_OP_N
:
HPPL_OP_T
,
(
T
*
)
B
,
TransB
==
CblasNoTrans
?
HPPL_OP_N
:
HPPL_OP_T
,
C
,
M
,
N
,
K
,
alpha
,
beta
,
lda
,
ldb
,
ldc
);
}
};
};
}
// namespace paddle
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录