Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
c93a624b
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
1 年多 前同步成功
通知
696
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
c93a624b
编写于
4月 28, 2018
作者:
Q
Qingsheng Li
提交者:
GitHub
4月 28, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #10052 from ktlichkid/fix-10026
Added kernel to Beam Search OP
上级
c2e47560
79be1bb3
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
44 addition
and
48 deletion
+44
-48
paddle/fluid/operators/beam_search_op.cc
paddle/fluid/operators/beam_search_op.cc
+29
-13
paddle/fluid/operators/beam_search_op.h
paddle/fluid/operators/beam_search_op.h
+15
-35
未找到文件。
paddle/fluid/operators/beam_search_op.cc
浏览文件 @
c93a624b
...
...
@@ -195,10 +195,9 @@ std::string ItemToString(const BeamSearch::Item &item) {
return
stream
.
str
();
}
class
BeamSearchProtoAndCheckerMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
class
BeamSearchOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
BeamSearch
ProtoAndChecker
Maker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
BeamSearch
Op
Maker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
// inputs and outputs stored in proto
AddInput
(
"pre_ids"
,
"ids in previous step"
);
...
...
@@ -222,20 +221,32 @@ class BeamSearchProtoAndCheckerMaker
}
};
class
BeamSearch
InferShape
:
public
framework
::
InferShapeBase
{
class
BeamSearch
Op
:
public
framework
::
OperatorWithKernel
{
public:
void
operator
()(
framework
::
InferShapeContext
*
context
)
const
override
{
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
for
(
const
std
::
string
&
arg
:
std
::
vector
<
std
::
string
>
({
"pre_ids"
,
"ids"
,
"scores"
}))
{
PADDLE_ENFORCE
(
c
ontext
->
HasInput
(
arg
)
,
"BeamSearch need input argument '%s'"
,
arg
);
PADDLE_ENFORCE
(
c
tx
->
HasInput
(
arg
),
"BeamSearch need input argument '%s'"
,
arg
);
}
for
(
const
std
::
string
&
arg
:
std
::
vector
<
std
::
string
>
({
"selected_ids"
,
"selected_scores"
}))
{
PADDLE_ENFORCE
(
c
ontext
->
HasOutput
(
arg
),
PADDLE_ENFORCE
(
c
tx
->
HasOutput
(
arg
),
"BeamSearch need output argument '%s'"
,
arg
);
}
}
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
framework
::
OpKernelType
kt
=
framework
::
OpKernelType
(
framework
::
ToDataType
(
ctx
.
Input
<
framework
::
LoDTensor
>
(
"pre_ids"
)
->
type
()),
platform
::
CPUPlace
());
return
kt
;
}
};
class
BeamSearchInferVarType
:
public
framework
::
VarTypeInference
{
...
...
@@ -254,8 +265,13 @@ class BeamSearchInferVarType : public framework::VarTypeInference {
}
// namespace operators
}
// namespace paddle
REGISTER_OPERATOR
(
beam_search
,
paddle
::
operators
::
BeamSearchOp
,
paddle
::
operators
::
BeamSearchProtoAndCheckerMaker
,
paddle
::
operators
::
BeamSearchInferShape
,
paddle
::
operators
::
BeamSearchInferVarType
,
paddle
::
framework
::
EmptyGradOpMaker
);
namespace
ops
=
paddle
::
operators
;
REGISTER_OPERATOR
(
beam_search
,
ops
::
BeamSearchOp
,
ops
::
BeamSearchOpMaker
,
ops
::
BeamSearchInferVarType
);
REGISTER_OP_CPU_KERNEL
(
beam_search
,
ops
::
BeamSearchOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
,
ops
::
BeamSearchOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
,
ops
::
BeamSearchOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int
>
,
ops
::
BeamSearchOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int64_t
>
);
paddle/fluid/operators/beam_search_op.h
浏览文件 @
c93a624b
...
...
@@ -192,49 +192,29 @@ std::ostream& operator<<(std::ostream& os, const BeamSearch::Item& item);
std
::
string
ItemToString
(
const
BeamSearch
::
Item
&
item
);
class
BeamSearchOp
:
public
framework
::
OperatorBase
{
template
<
typename
DeviceContext
,
typename
T
>
class
BeamSearchOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
BeamSearchOp
(
const
std
::
string
&
type
,
const
framework
::
VariableNameMap
&
inputs
,
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
)
:
OperatorBase
(
type
,
inputs
,
outputs
,
attrs
)
{}
BeamSearchOp
(
const
BeamSearchOp
&
o
)
:
framework
::
OperatorBase
(
static_cast
<
const
framework
::
OperatorBase
&>
(
o
))
{
PADDLE_THROW
(
"Not Implemented"
);
}
private:
void
RunImpl
(
const
framework
::
Scope
&
scope
,
const
platform
::
Place
&
dev_place
)
const
override
{
auto
ids_var
=
scope
.
FindVar
(
Input
(
"ids"
));
auto
scores_var
=
scope
.
FindVar
(
Input
(
"scores"
));
auto
pre_ids_var
=
scope
.
FindVar
(
Input
(
"pre_ids"
));
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
ids_var
=
context
.
Input
<
framework
::
LoDTensor
>
(
"ids"
);
auto
*
scores_var
=
context
.
Input
<
framework
::
LoDTensor
>
(
"scores"
);
auto
*
pre_ids_var
=
context
.
Input
<
framework
::
LoDTensor
>
(
"pre_ids"
);
PADDLE_ENFORCE_NOT_NULL
(
ids_var
);
PADDLE_ENFORCE_NOT_NULL
(
scores_var
);
PADDLE_ENFORCE_NOT_NULL
(
pre_ids_var
);
auto
&
ids
=
ids_var
->
Get
<
framework
::
LoDTensor
>
();
auto
&
scores
=
scores_var
->
Get
<
framework
::
LoDTensor
>
();
auto
&
pre_ids
=
pre_ids_var
->
Get
<
framework
::
LoDTensor
>
();
size_t
level
=
Attr
<
int
>
(
"level"
);
size_t
beam_size
=
Attr
<
int
>
(
"beam_size"
);
int
end_id
=
Attr
<
int
>
(
"end_id"
);
BeamSearch
alg
(
ids
,
scores
,
level
,
beam_size
,
end_id
);
auto
selected_ids_var
=
scope
.
FindVar
(
Output
(
"selected_ids"
));
auto
selected_scores_var
=
scope
.
FindVar
(
Output
(
"selected_scores"
));
size_t
level
=
context
.
Attr
<
int
>
(
"level"
);
size_t
beam_size
=
context
.
Attr
<
int
>
(
"beam_size"
);
int
end_id
=
context
.
Attr
<
int
>
(
"end_id"
);
BeamSearch
alg
(
*
ids_var
,
*
scores_var
,
level
,
beam_size
,
end_id
);
auto
selected_ids_var
=
context
.
Output
<
framework
::
LoDTensor
>
(
"selected_ids"
);
auto
selected_scores_var
=
context
.
Output
<
framework
::
LoDTensor
>
(
"selected_scores"
);
PADDLE_ENFORCE_NOT_NULL
(
selected_ids_var
);
PADDLE_ENFORCE_NOT_NULL
(
selected_scores_var
);
auto
&
selected_ids_tensor
=
*
selected_ids_var
->
GetMutable
<
framework
::
LoDTensor
>
();
auto
&
selected_scores_tensor
=
*
selected_scores_var
->
GetMutable
<
framework
::
LoDTensor
>
();
alg
(
pre_ids
,
&
selected_ids_tensor
,
&
selected_scores_tensor
);
alg
(
*
pre_ids_var
,
selected_ids_var
,
selected_scores_var
);
}
};
}
// namespace operators
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录