Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
720b55cb
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看板
提交
720b55cb
编写于
12月 17, 2018
作者:
T
tensor-tang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
enable crf decoding and layer norm refer code
上级
64a90b2f
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
119 addition
and
14 deletion
+119
-14
paddle/fluid/operators/crf_decoding_op.h
paddle/fluid/operators/crf_decoding_op.h
+4
-5
paddle/fluid/operators/jit/helper.cc
paddle/fluid/operators/jit/helper.cc
+4
-0
paddle/fluid/operators/jit/kernel_base.h
paddle/fluid/operators/jit/kernel_base.h
+18
-1
paddle/fluid/operators/jit/refer/CMakeLists.txt
paddle/fluid/operators/jit/refer/CMakeLists.txt
+2
-0
paddle/fluid/operators/jit/refer/refer.cc
paddle/fluid/operators/jit/refer/refer.cc
+3
-0
paddle/fluid/operators/jit/refer/refer.h
paddle/fluid/operators/jit/refer/refer.h
+80
-0
paddle/fluid/operators/jit/test.cc
paddle/fluid/operators/jit/test.cc
+1
-1
paddle/fluid/operators/layer_norm_op.h
paddle/fluid/operators/layer_norm_op.h
+7
-7
未找到文件。
paddle/fluid/operators/crf_decoding_op.h
浏览文件 @
720b55cb
...
@@ -16,7 +16,7 @@ limitations under the License. */
...
@@ -16,7 +16,7 @@ limitations under the License. */
#include <limits>
#include <limits>
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/
math/jit_kernel
.h"
#include "paddle/fluid/operators/
jit/kernels
.h"
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/operators/math/math_function.h"
namespace
paddle
{
namespace
paddle
{
...
@@ -82,10 +82,9 @@ class CRFDecodingOpKernel : public framework::OpKernel<T> {
...
@@ -82,10 +82,9 @@ class CRFDecodingOpKernel : public framework::OpKernel<T> {
Tensor
track
;
Tensor
track
;
int
*
track_value
=
int
*
track_value
=
track
.
mutable_data
<
int
>
(
emission_dims
,
platform
::
CPUPlace
());
track
.
mutable_data
<
int
>
(
emission_dims
,
platform
::
CPUPlace
());
const
auto
&
ker
=
math
::
jitkernel
::
KernelPool
::
Instance
()
auto
ker
=
jit
::
Get
<
jit
::
crfdecoding
,
jit
::
CRFDecoding
,
platform
::
CPUPlace
>
(
.
template
Get
<
math
::
jitkernel
::
CRFDecodeKernel
<
T
>
>
(
tag_num
);
static_cast
<
int
>
(
tag_num
));
ker
(
static_cast
<
int
>
(
seq_len
),
x
,
w
,
alpha_value
,
track_value
,
tag_num
);
ker
->
Compute
(
static_cast
<
int
>
(
seq_len
),
x
,
w
,
alpha_value
,
track_value
);
T
max_score
=
-
std
::
numeric_limits
<
T
>::
max
();
T
max_score
=
-
std
::
numeric_limits
<
T
>::
max
();
int
max_i
=
0
;
int
max_i
=
0
;
for
(
size_t
i
=
0
;
i
<
tag_num
;
++
i
)
{
for
(
size_t
i
=
0
;
i
<
tag_num
;
++
i
)
{
...
...
paddle/fluid/operators/jit/helper.cc
浏览文件 @
720b55cb
...
@@ -42,6 +42,8 @@ const char* to_string(KernelType kt) {
...
@@ -42,6 +42,8 @@ const char* to_string(KernelType kt) {
ONE_CASE
(
gruh1
);
ONE_CASE
(
gruh1
);
ONE_CASE
(
gruhtpart1
);
ONE_CASE
(
gruhtpart1
);
ONE_CASE
(
gruhtpart2
);
ONE_CASE
(
gruhtpart2
);
ONE_CASE
(
crfdecoding
);
ONE_CASE
(
layernorm
);
default:
default:
PADDLE_THROW
(
"Not support type: %d"
,
kt
);
PADDLE_THROW
(
"Not support type: %d"
,
kt
);
return
"NOT JITKernel"
;
return
"NOT JITKernel"
;
...
@@ -64,6 +66,8 @@ KernelType to_kerneltype(const std::string& act) {
...
@@ -64,6 +66,8 @@ KernelType to_kerneltype(const std::string& act) {
}
else
if
(
lower
==
"tanh"
||
lower
==
"vtanh"
)
{
}
else
if
(
lower
==
"tanh"
||
lower
==
"vtanh"
)
{
return
vtanh
;
return
vtanh
;
}
}
PADDLE_THROW
(
"Not support type: %s, or forget to add this case"
,
act
);
return
non_kernel
;
return
non_kernel
;
}
}
...
...
paddle/fluid/operators/jit/kernel_base.h
浏览文件 @
720b55cb
...
@@ -37,7 +37,9 @@ typedef enum {
...
@@ -37,7 +37,9 @@ typedef enum {
lstmc1h1
,
lstmc1h1
,
gruh1
,
gruh1
,
gruhtpart1
,
gruhtpart1
,
gruhtpart2
gruhtpart2
,
crfdecoding
,
layernorm
}
KernelType
;
}
KernelType
;
template
<
typename
T
>
template
<
typename
T
>
...
@@ -109,6 +111,21 @@ struct GRUTuples {
...
@@ -109,6 +111,21 @@ struct GRUTuples {
typedef
void
(
*
func_type
)(
gru_t
*
,
const
gru_attr_t
*
);
typedef
void
(
*
func_type
)(
gru_t
*
,
const
gru_attr_t
*
);
};
};
template
<
typename
T
>
struct
CRFDecodingTuples
{
typedef
T
data_type
;
typedef
int
attr_type
;
typedef
void
(
*
func_type
)(
const
int
,
const
T
*
,
const
T
*
,
T
*
,
int
*
,
int
);
};
template
<
typename
T
>
struct
LayerNormTuples
{
typedef
T
data_type
;
typedef
int
attr_type
;
typedef
void
(
*
func_type
)(
T
*
,
T
*
,
T
*
,
T
*
,
const
T
*
,
const
T
*
,
int
,
const
float
,
int
);
};
// Just for adding to kernel pool without template
// Just for adding to kernel pool without template
class
Kernel
{
class
Kernel
{
public:
public:
...
...
paddle/fluid/operators/jit/refer/CMakeLists.txt
浏览文件 @
720b55cb
...
@@ -23,3 +23,5 @@ USE_JITKERNEL_REFER(lstmc1h1)
...
@@ -23,3 +23,5 @@ USE_JITKERNEL_REFER(lstmc1h1)
USE_JITKERNEL_REFER
(
gruh1
)
USE_JITKERNEL_REFER
(
gruh1
)
USE_JITKERNEL_REFER
(
gruhtpart1
)
USE_JITKERNEL_REFER
(
gruhtpart1
)
USE_JITKERNEL_REFER
(
gruhtpart2
)
USE_JITKERNEL_REFER
(
gruhtpart2
)
USE_JITKERNEL_REFER
(
crfdecoding
)
USE_JITKERNEL_REFER
(
layernorm
)
paddle/fluid/operators/jit/refer/refer.cc
浏览文件 @
720b55cb
...
@@ -42,4 +42,7 @@ REGISTER_REFER_KERNEL(gruh1, GRUH1);
...
@@ -42,4 +42,7 @@ REGISTER_REFER_KERNEL(gruh1, GRUH1);
REGISTER_REFER_KERNEL
(
gruhtpart1
,
GRUHtPart1
);
REGISTER_REFER_KERNEL
(
gruhtpart1
,
GRUHtPart1
);
REGISTER_REFER_KERNEL
(
gruhtpart2
,
GRUHtPart2
);
REGISTER_REFER_KERNEL
(
gruhtpart2
,
GRUHtPart2
);
REGISTER_REFER_KERNEL
(
crfdecoding
,
CRFDecoding
);
REGISTER_REFER_KERNEL
(
layernorm
,
LayerNorm
);
#undef REGISTER_REFER_KERNEL
#undef REGISTER_REFER_KERNEL
paddle/fluid/operators/jit/refer/refer.h
浏览文件 @
720b55cb
...
@@ -13,6 +13,9 @@
...
@@ -13,6 +13,9 @@
* limitations under the License. */
* limitations under the License. */
#pragma once
#pragma once
#include <cmath>
#include <limits>
#include "paddle/fluid/operators/jit/helper.h"
#include "paddle/fluid/operators/jit/helper.h"
#include "paddle/fluid/operators/jit/kernel_base.h"
#include "paddle/fluid/operators/jit/kernel_base.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/enforce.h"
...
@@ -242,6 +245,80 @@ void GRUHtPart2(gru_t* step, const gru_attr_t* attr) {
...
@@ -242,6 +245,80 @@ void GRUHtPart2(gru_t* step, const gru_attr_t* attr) {
}
}
}
}
template
<
typename
T
>
void
CRFDecoding
(
const
int
seq_len
,
const
T
*
x
,
const
T
*
w
,
T
*
alpha
,
int
*
track
,
int
right
)
{
constexpr
int
state_trans_base_idx
=
2
;
for
(
int
i
=
0
;
i
<
right
;
++
i
)
{
alpha
[
i
]
=
w
[
i
]
+
x
[
i
];
}
for
(
int
k
=
1
;
k
<
seq_len
;
++
k
)
{
for
(
int
i
=
0
;
i
<
right
;
++
i
)
{
T
max_score
=
-
std
::
numeric_limits
<
T
>::
max
();
int
max_j
=
0
;
for
(
int
j
=
0
;
j
<
right
;
++
j
)
{
T
score
=
alpha
[(
k
-
1
)
*
right
+
j
]
+
w
[(
j
+
state_trans_base_idx
)
*
right
+
i
];
if
(
score
>
max_score
)
{
max_score
=
score
;
max_j
=
j
;
}
}
alpha
[
k
*
right
+
i
]
=
max_score
+
x
[
k
*
right
+
i
];
track
[
k
*
right
+
i
]
=
max_j
;
}
}
}
template
<
typename
T
>
void
LayerNorm
(
T
*
x
,
T
*
out
,
T
*
mean
,
T
*
var
,
const
T
*
scale
,
const
T
*
bias
,
int
height
,
const
float
epsilon
,
int
right
)
{
// get mean
for
(
int
i
=
0
;
i
<
height
;
i
++
)
{
T
sum
=
0.0
;
int
offset
=
i
*
right
;
for
(
int
j
=
0
;
j
<
right
;
j
++
)
{
sum
+=
x
[
offset
+
j
];
}
mean
[
i
]
=
sum
/
right
;
}
// get variance
for
(
int
i
=
0
;
i
<
height
;
i
++
)
{
T
sum
=
0.0
;
int
offset
=
i
*
right
;
for
(
int
j
=
0
;
j
<
right
;
j
++
)
{
sum
+=
(
x
[
offset
+
j
]
-
mean
[
i
])
*
(
x
[
offset
+
j
]
-
mean
[
i
]);
}
var
[
i
]
=
sum
/
right
;
}
for
(
int
i
=
0
;
i
<
height
;
i
++
)
{
int
offset
=
i
*
right
;
T
sqrt_var
=
std
::
sqrt
(
var
[
i
]
+
(
T
)
epsilon
);
for
(
int
j
=
0
;
j
<
right
;
j
++
)
{
out
[
offset
+
j
]
=
(
x
[
offset
+
j
]
-
mean
[
i
])
/
sqrt_var
;
}
}
if
(
scale
)
{
for
(
int
i
=
0
;
i
<
height
;
i
++
)
{
int
offset
=
i
*
right
;
for
(
int
j
=
0
;
j
<
right
;
j
++
)
{
out
[
offset
+
j
]
*=
scale
[
j
];
}
}
}
if
(
bias
)
{
for
(
int
i
=
0
;
i
<
height
;
i
++
)
{
int
offset
=
i
*
right
;
for
(
int
j
=
0
;
j
<
right
;
j
++
)
{
out
[
offset
+
j
]
+=
bias
[
j
];
}
}
}
}
#define DECLARE_REFER_KERNEL(name, tuples) \
#define DECLARE_REFER_KERNEL(name, tuples) \
template <typename T> \
template <typename T> \
class name##Kernel : public ReferKernel<tuples<T>> { \
class name##Kernel : public ReferKernel<tuples<T>> { \
...
@@ -275,6 +352,9 @@ DECLARE_REFER_KERNEL(GRUH1, GRUTuples);
...
@@ -275,6 +352,9 @@ DECLARE_REFER_KERNEL(GRUH1, GRUTuples);
DECLARE_REFER_KERNEL
(
GRUHtPart1
,
GRUTuples
);
DECLARE_REFER_KERNEL
(
GRUHtPart1
,
GRUTuples
);
DECLARE_REFER_KERNEL
(
GRUHtPart2
,
GRUTuples
);
DECLARE_REFER_KERNEL
(
GRUHtPart2
,
GRUTuples
);
DECLARE_REFER_KERNEL
(
CRFDecoding
,
CRFDecodingTuples
);
DECLARE_REFER_KERNEL
(
LayerNorm
,
LayerNormTuples
);
#undef DECLARE_REFER_KERNEL
#undef DECLARE_REFER_KERNEL
}
// namespace refer
}
// namespace refer
...
...
paddle/fluid/operators/jit/test.cc
浏览文件 @
720b55cb
...
@@ -515,7 +515,7 @@ TEST(JITKernel, gruhtpart2) {
...
@@ -515,7 +515,7 @@ TEST(JITKernel, gruhtpart2) {
TestGRUKernel
<
jit
::
gruhtpart2
,
double
,
paddle
::
platform
::
CPUPlace
>
();
TestGRUKernel
<
jit
::
gruhtpart2
,
double
,
paddle
::
platform
::
CPUPlace
>
();
}
}
// TODO(
TJ): refine the tests template
// TODO(
yihua/TJ): add crf decoding and layer norm unit tests
TEST
(
JITKernel
,
pool
)
{
TEST
(
JITKernel
,
pool
)
{
// TODO(TJ): add some test
// TODO(TJ): add some test
...
...
paddle/fluid/operators/layer_norm_op.h
浏览文件 @
720b55cb
...
@@ -19,7 +19,7 @@ limitations under the License. */
...
@@ -19,7 +19,7 @@ limitations under the License. */
#include "paddle/fluid/operators/math/blas.h"
#include "paddle/fluid/operators/math/blas.h"
#if !defined(PADDLE_WITH_CUDA) && !defined(_WIN32) && !defined(__APPLE__) && \
#if !defined(PADDLE_WITH_CUDA) && !defined(_WIN32) && !defined(__APPLE__) && \
!defined(__OSX__)
!defined(__OSX__)
#include "paddle/fluid/operators/
math/jit_kernel
.h"
#include "paddle/fluid/operators/
jit/kernels
.h"
#endif
#endif
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/operators/math/math_function.h"
...
@@ -229,12 +229,12 @@ class LayerNormKernel : public framework::OpKernel<T> {
...
@@ -229,12 +229,12 @@ class LayerNormKernel : public framework::OpKernel<T> {
PADDLE_ENFORCE_EQ
(
scale
->
numel
(),
right
);
PADDLE_ENFORCE_EQ
(
scale
->
numel
(),
right
);
PADDLE_ENFORCE_EQ
(
bias
->
numel
(),
right
);
PADDLE_ENFORCE_EQ
(
bias
->
numel
(),
right
);
const
auto
&
ker
=
math
::
jitkernel
::
KernelPool
::
Instance
()
auto
ker
=
.
template
Get
<
math
::
jitkernel
::
LayerNormKernel
<
T
>
>
(
jit
::
Get
<
jit
::
layernorm
,
jit
::
LayerNormTuples
,
platform
::
CPUPlace
>
(
static_cast
<
int
>
(
right
)
);
right
);
ker
->
Compute
(
x
.
data
<
T
>
(),
out
.
data
<
T
>
(),
mean
->
data
<
T
>
(),
var
->
data
<
T
>
(),
ker
(
x
.
data
<
T
>
(),
out
.
data
<
T
>
(),
mean
->
data
<
T
>
(),
var
->
data
<
T
>
(),
scale
->
data
<
T
>
(),
bias
->
data
<
T
>
(),
static_cast
<
int
>
(
left
),
scale
->
data
<
T
>
(),
bias
->
data
<
T
>
(),
static_cast
<
int
>
(
left
),
static_cast
<
const
float
>
(
epsilon
)
);
static_cast
<
const
float
>
(
epsilon
),
right
);
#endif
#endif
}
}
};
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录