Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
8bb824bb
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
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看板
提交
8bb824bb
编写于
9月 12, 2018
作者:
T
tensor-tang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine infershape hasinput and hasoutput
上级
c4394bc5
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
197 addition
and
311 deletion
+197
-311
paddle/fluid/framework/operator.cc
paddle/fluid/framework/operator.cc
+155
-119
paddle/fluid/framework/shape_runtime_infer.h
paddle/fluid/framework/shape_runtime_infer.h
+0
-86
paddle/fluid/operators/attention_lstm_op.cc
paddle/fluid/operators/attention_lstm_op.cc
+17
-18
paddle/fluid/operators/fusion_gru_op.cc
paddle/fluid/operators/fusion_gru_op.cc
+10
-12
paddle/fluid/operators/fusion_infershape_define.h
paddle/fluid/operators/fusion_infershape_define.h
+0
-60
paddle/fluid/operators/fusion_lstm_op.cc
paddle/fluid/operators/fusion_lstm_op.cc
+15
-16
未找到文件。
paddle/fluid/framework/operator.cc
浏览文件 @
8bb824bb
...
...
@@ -21,7 +21,6 @@ limitations under the License. */
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/shape_inference.h"
#include "paddle/fluid/framework/shape_runtime_infer.h"
#include "paddle/fluid/framework/var_type.h"
#include "paddle/fluid/platform/profiler.h"
...
...
@@ -459,147 +458,184 @@ bool OpSupportGPU(const std::string& op_type) {
return
false
;
}
bool
RuntimeInferShapeContext
::
HasInput
(
const
std
::
string
&
name
)
const
{
if
(
!
op_
.
HasInputs
(
name
))
{
return
false
;
}
auto
&
ins
=
Inputs
(
name
);
size_t
length
=
ins
.
size
();
if
(
length
==
0
)
{
return
false
;
}
PADDLE_ENFORCE_EQ
(
length
,
1UL
,
"Input %s should not have more than one inputs"
,
name
);
auto
ipt
=
ins
[
0
];
auto
*
var
=
ipt
==
kEmptyVarName
?
nullptr
:
scope_
.
FindVar
(
ipt
);
return
var
!=
nullptr
;
}
class
RuntimeInferShapeContext
:
public
InferShapeContext
{
public:
RuntimeInferShapeContext
(
const
OperatorBase
&
op
,
const
Scope
&
scope
)
:
op_
(
op
),
scope_
(
scope
)
{}
bool
RuntimeInferShapeContext
::
HasOutput
(
const
std
::
string
&
name
)
const
{
if
(
!
op_
.
HasOutputs
(
name
))
{
return
false
;
}
auto
&
outs
=
Outputs
(
name
);
size_t
length
=
outs
.
size
();
if
(
length
==
0
)
{
return
false
;
}
PADDLE_ENFORCE_EQ
(
length
,
1UL
,
"Output %s should not have more than one inputs"
,
name
);
auto
ipt
=
outs
[
0
];
auto
*
var
=
ipt
==
kEmptyVarName
?
nullptr
:
scope_
.
FindVar
(
ipt
);
return
var
!=
nullptr
;
}
bool
HasInput
(
const
std
::
string
&
name
)
const
override
{
// has only one input
const
auto
&
ins
=
op_
.
Inputs
();
auto
it
=
ins
.
find
(
name
);
if
(
it
==
ins
.
end
())
{
return
false
;
}
const
auto
&
in
=
it
->
second
;
bool
RuntimeInferShapeContext
::
HasInputs
(
const
std
::
string
&
name
)
const
{
if
(
!
op_
.
HasInputs
(
name
))
{
return
false
;
}
auto
inputs
=
op_
.
Inputs
(
name
);
if
(
inputs
.
empty
())
{
return
false
;
}
for
(
auto
&
input
:
inputs
)
{
if
(
scope_
.
FindVar
(
input
)
==
nullptr
)
{
if
(
in
.
size
()
!=
1
||
in
[
0
]
==
kEmptyVarName
)
{
return
false
;
}
return
scope_
.
FindVar
(
in
[
0
])
!=
nullptr
;
}
return
true
;
}
bool
RuntimeInferShapeContext
::
HasOutputs
(
const
std
::
string
&
name
)
const
{
if
(
!
op_
.
HasOutputs
(
name
))
{
return
false
;
bool
HasOutput
(
const
std
::
string
&
name
)
const
override
{
// has only one output
const
auto
&
outs
=
op_
.
Outputs
();
auto
it
=
outs
.
find
(
name
);
if
(
it
==
outs
.
end
())
{
return
false
;
}
const
auto
&
out
=
it
->
second
;
if
(
out
.
size
()
!=
1
||
out
[
0
]
==
kEmptyVarName
)
{
return
false
;
}
return
scope_
.
FindVar
(
out
[
0
])
!=
nullptr
;
}
auto
outputs
=
op_
.
Outputs
(
name
);
if
(
outputs
.
empty
())
{
return
false
;
bool
HasInputs
(
const
std
::
string
&
name
)
const
override
{
if
(
!
op_
.
HasInputs
(
name
))
{
return
false
;
}
auto
inputs
=
op_
.
Inputs
(
name
);
if
(
inputs
.
empty
())
{
return
false
;
}
for
(
auto
&
input
:
inputs
)
{
if
(
scope_
.
FindVar
(
input
)
==
nullptr
)
{
return
false
;
}
}
return
true
;
}
for
(
auto
&
output
:
outputs
)
{
if
(
scope_
.
FindVar
(
output
)
==
nullptr
)
{
bool
HasOutputs
(
const
std
::
string
&
name
)
const
override
{
if
(
!
op_
.
HasOutputs
(
name
))
{
return
false
;
}
auto
outputs
=
op_
.
Outputs
(
name
);
if
(
outputs
.
empty
())
{
return
false
;
}
for
(
auto
&
output
:
outputs
)
{
if
(
scope_
.
FindVar
(
output
)
==
nullptr
)
{
return
false
;
}
}
return
true
;
}
return
true
;
}
void
RuntimeInferShapeContext
::
ShareLoD
(
const
std
::
string
&
in
,
const
std
::
string
&
out
,
size_t
i
,
size_t
j
)
const
{
PADDLE_ENFORCE_LT
(
i
,
Inputs
(
in
).
size
());
PADDLE_ENFORCE_LT
(
j
,
Outputs
(
out
).
size
());
Variable
*
in_var
=
scope_
.
FindVar
(
Inputs
(
in
)[
i
]);
Variable
*
out_var
=
scope_
.
FindVar
(
Outputs
(
out
)[
j
]);
if
(
!
in_var
->
IsType
<
LoDTensor
>
())
return
;
PADDLE_ENFORCE
(
out_var
->
IsType
<
LoDTensor
>
(),
"The %d-th output of Output(%s) must be LoDTensor."
,
j
,
out
);
auto
in_tensor
=
in_var
->
Get
<
LoDTensor
>
();
auto
*
out_tensor
=
out_var
->
GetMutable
<
LoDTensor
>
();
out_tensor
->
set_lod
(
in_tensor
.
lod
());
AttrReader
Attrs
()
const
override
{
return
AttrReader
(
op_
.
Attrs
());
}
const
std
::
vector
<
std
::
string
>&
Inputs
(
const
std
::
string
&
name
)
const
override
{
return
op_
.
Inputs
(
name
);
}
const
std
::
vector
<
std
::
string
>&
Outputs
(
const
std
::
string
&
name
)
const
override
{
return
op_
.
Outputs
(
name
);
}
void
ShareLoD
(
const
std
::
string
&
in
,
const
std
::
string
&
out
,
size_t
i
=
0
,
size_t
j
=
0
)
const
override
{
PADDLE_ENFORCE_LT
(
i
,
Inputs
(
in
).
size
());
PADDLE_ENFORCE_LT
(
j
,
Outputs
(
out
).
size
());
Variable
*
in_var
=
scope_
.
FindVar
(
Inputs
(
in
)[
i
]);
Variable
*
out_var
=
scope_
.
FindVar
(
Outputs
(
out
)[
j
]);
if
(
!
in_var
->
IsType
<
LoDTensor
>
())
return
;
PADDLE_ENFORCE
(
out_var
->
IsType
<
LoDTensor
>
(),
"The %d-th output of Output(%s) must be LoDTensor."
,
j
,
out
);
auto
in_tensor
=
in_var
->
Get
<
LoDTensor
>
();
auto
*
out_tensor
=
out_var
->
GetMutable
<
LoDTensor
>
();
out_tensor
->
set_lod
(
in_tensor
.
lod
());
// TODO(dzhwinter) : reuse ShareLoD in most operators.
// Need to call ShareLayout explicitly in sequence related ops.
// Shall we have a better method to shared info between in/out Tensor?
#ifdef PADDLE_WITH_MKLDNN
// Fix me: ugly workaround below
// Correct solution:
// set_layout() should NOT be called here (i.e. ShareLoD). Instead,
// layout of output tensor should be set "manually" in Compute()
// of each OPKernel. The reason layout should NOT be shared between
// input and output "automatically" (now by InferShape()->ShareLoD())
// is that layout transform may occur after InferShape().
// Workaround:
// Skip set_layout() when input layout is kMKLDNN
// This is to avoid kMKLDNN is populated wrongly into a non-MKLDNN
// OPKernel. In all MKLDNN OPkernel, set_layout(kMKLDNN) should be called
// in Compute()
if
(
in_tensor
.
layout
()
!=
DataLayout
::
kMKLDNN
)
// Fix me: ugly workaround below
// Correct solution:
// set_layout() should NOT be called here (i.e. ShareLoD). Instead,
// layout of output tensor should be set "manually" in Compute()
// of each OPKernel. The reason layout should NOT be shared between
// input and output "automatically" (now by InferShape()->ShareLoD())
// is that layout transform may occur after InferShape().
// Workaround:
// Skip set_layout() when input layout is kMKLDNN
// This is to avoid kMKLDNN is populated wrongly into a non-MKLDNN
// OPKernel. In all MKLDNN OPkernel, set_layout(kMKLDNN) should be called
// in Compute()
if
(
in_tensor
.
layout
()
!=
DataLayout
::
kMKLDNN
)
#endif
out_tensor
->
set_layout
(
in_tensor
.
layout
());
}
void
ShareLayout
(
const
std
::
string
&
in
,
const
std
::
string
&
out
,
size_t
i
=
0
,
size_t
j
=
0
)
const
{
PADDLE_ENFORCE_LT
(
i
,
Inputs
(
in
).
size
());
PADDLE_ENFORCE_LT
(
j
,
Outputs
(
out
).
size
());
Variable
*
in_var
=
scope_
.
FindVar
(
Inputs
(
in
)[
i
]);
Variable
*
out_var
=
scope_
.
FindVar
(
Outputs
(
out
)[
j
]);
if
(
!
in_var
->
IsType
<
LoDTensor
>
())
return
;
PADDLE_ENFORCE
(
out_var
->
IsType
<
LoDTensor
>
(),
"The %d-th output of Output(%s) must be LoDTensor."
,
j
,
out
);
auto
in_tensor
=
in_var
->
Get
<
LoDTensor
>
();
auto
*
out_tensor
=
out_var
->
GetMutable
<
LoDTensor
>
();
out_tensor
->
set_layout
(
in_tensor
.
layout
());
}
}
void
RuntimeInferShapeContext
::
ShareLayout
(
const
std
::
string
&
in
,
const
std
::
string
&
out
,
size_t
i
,
size_t
j
)
const
{
PADDLE_ENFORCE_LT
(
i
,
Inputs
(
in
).
size
());
PADDLE_ENFORCE_LT
(
j
,
Outputs
(
out
).
size
());
Variable
*
in_var
=
scope_
.
FindVar
(
Inputs
(
in
)[
i
]);
Variable
*
out_var
=
scope_
.
FindVar
(
Outputs
(
out
)[
j
]);
if
(
!
in_var
->
IsType
<
LoDTensor
>
())
return
;
PADDLE_ENFORCE
(
out_var
->
IsType
<
LoDTensor
>
(),
"The %d-th output of Output(%s) must be LoDTensor."
,
j
,
out
);
auto
in_tensor
=
in_var
->
Get
<
LoDTensor
>
();
auto
*
out_tensor
=
out_var
->
GetMutable
<
LoDTensor
>
();
out_tensor
->
set_layout
(
in_tensor
.
layout
());
}
DDim
RuntimeInferShapeContext
::
GetDim
(
const
std
::
string
&
name
)
const
{
Variable
*
var
=
scope_
.
FindVar
(
name
);
PADDLE_ENFORCE_NOT_NULL
(
var
);
if
(
var
->
IsType
<
LoDTensor
>
())
{
return
var
->
Get
<
LoDTensor
>
().
dims
();
}
else
if
(
var
->
IsType
<
SelectedRows
>
())
{
return
var
->
Get
<
SelectedRows
>
().
GetCompleteDims
();
}
else
{
PADDLE_THROW
(
"Only LoDTensor/SelectedRows support 'GetDim', but Variable %s's "
"type_id is %s."
,
name
,
var
->
Type
().
name
());
bool
IsRuntime
()
const
override
{
return
true
;
}
protected:
DDim
GetDim
(
const
std
::
string
&
name
)
const
override
{
Variable
*
var
=
scope_
.
FindVar
(
name
);
PADDLE_ENFORCE_NOT_NULL
(
var
);
if
(
var
->
IsType
<
LoDTensor
>
())
{
return
var
->
Get
<
LoDTensor
>
().
dims
();
}
else
if
(
var
->
IsType
<
SelectedRows
>
())
{
return
var
->
Get
<
SelectedRows
>
().
GetCompleteDims
();
}
else
{
PADDLE_THROW
(
"Only LoDTensor/SelectedRows support 'GetDim', but Variable %s's "
"type_id is %s."
,
name
,
var
->
Type
().
name
());
}
}
}
void
RuntimeInferShapeContext
::
SetDim
(
const
std
::
string
&
name
,
const
DDim
&
dim
)
{
Variable
*
var
=
scope_
.
FindVar
(
name
);
if
(
var
->
IsType
<
LoDTensor
>
())
{
var
->
GetMutable
<
LoDTensor
>
()
->
Resize
(
dim
);
}
else
if
(
var
->
IsType
<
SelectedRows
>
())
{
var
->
GetMutable
<
SelectedRows
>
()
->
set_height
(
dim
[
0
]);
}
else
{
PADDLE_THROW
(
"Variable %s type_id %s, expect LoDTensor/SelectedRows."
,
name
,
var
->
Type
().
name
());
std
::
vector
<
DDim
>
GetRepeatedDims
(
const
std
::
string
&
name
)
const
override
{
PADDLE_THROW
(
"Only compile time support this method"
);
}
}
void
SetDim
(
const
std
::
string
&
name
,
const
DDim
&
dim
)
override
{
Variable
*
var
=
scope_
.
FindVar
(
name
);
if
(
var
->
IsType
<
LoDTensor
>
())
{
var
->
GetMutable
<
LoDTensor
>
()
->
Resize
(
dim
);
}
else
if
(
var
->
IsType
<
SelectedRows
>
())
{
var
->
GetMutable
<
SelectedRows
>
()
->
set_height
(
dim
[
0
]);
}
else
{
PADDLE_THROW
(
"Variable %s type_id %s, expect LoDTensor/SelectedRows."
,
name
,
var
->
Type
().
name
());
}
}
void
SetRepeatedDims
(
const
std
::
string
&
name
,
const
std
::
vector
<
DDim
>&
dims
)
override
{
PADDLE_THROW
(
"Only compile time support this method"
);
}
proto
::
VarType
::
Type
GetVarType
(
const
std
::
string
&
name
)
const
override
{
auto
*
var
=
scope_
.
FindVar
(
name
);
return
ToVarType
(
var
->
Type
());
}
InferShapeVarPtr
GetVarPtr
(
const
std
::
string
&
name
)
override
{
return
scope_
.
FindVar
(
name
);
}
private:
const
OperatorBase
&
op_
;
const
Scope
&
scope_
;
};
static
void
CheckTensorNANOrInf
(
const
std
::
string
&
name
,
const
framework
::
Tensor
&
tensor
)
{
...
...
paddle/fluid/framework/shape_runtime_infer.h
已删除
100644 → 0
浏览文件 @
c4394bc5
/* Copyright (c) 2016 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. */
#pragma once
#include <string>
#include <vector>
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/shape_inference.h"
#include "paddle/fluid/framework/var_type.h"
namespace
paddle
{
namespace
framework
{
class
RuntimeInferShapeContext
:
public
InferShapeContext
{
public:
RuntimeInferShapeContext
(
const
OperatorBase
&
op
,
const
Scope
&
scope
)
:
op_
(
op
),
scope_
(
scope
)
{}
bool
HasInput
(
const
std
::
string
&
name
)
const
override
;
bool
HasOutput
(
const
std
::
string
&
name
)
const
override
;
bool
HasInputs
(
const
std
::
string
&
name
)
const
override
;
bool
HasOutputs
(
const
std
::
string
&
name
)
const
override
;
const
OperatorBase
&
OpBase
()
const
{
return
op_
;
}
const
Scope
&
InferScope
()
const
{
return
scope_
;
}
AttrReader
Attrs
()
const
override
{
return
AttrReader
(
op_
.
Attrs
());
}
const
std
::
vector
<
std
::
string
>&
Inputs
(
const
std
::
string
&
name
)
const
override
{
return
op_
.
Inputs
(
name
);
}
const
std
::
vector
<
std
::
string
>&
Outputs
(
const
std
::
string
&
name
)
const
override
{
return
op_
.
Outputs
(
name
);
}
void
ShareLoD
(
const
std
::
string
&
in
,
const
std
::
string
&
out
,
size_t
i
=
0
,
size_t
j
=
0
)
const
override
;
void
ShareLayout
(
const
std
::
string
&
in
,
const
std
::
string
&
out
,
size_t
i
=
0
,
size_t
j
=
0
)
const
;
bool
IsRuntime
()
const
override
{
return
true
;
}
protected:
DDim
GetDim
(
const
std
::
string
&
name
)
const
override
;
void
SetDim
(
const
std
::
string
&
name
,
const
DDim
&
dim
)
override
;
std
::
vector
<
DDim
>
GetRepeatedDims
(
const
std
::
string
&
name
)
const
override
{
PADDLE_THROW
(
"Only compile time support this method"
);
}
void
SetRepeatedDims
(
const
std
::
string
&
name
,
const
std
::
vector
<
DDim
>&
dims
)
override
{
PADDLE_THROW
(
"Only compile time support this method"
);
}
proto
::
VarType
::
Type
GetVarType
(
const
std
::
string
&
name
)
const
override
{
auto
*
var
=
scope_
.
FindVar
(
name
);
return
ToVarType
(
var
->
Type
());
}
InferShapeVarPtr
GetVarPtr
(
const
std
::
string
&
name
)
override
{
return
scope_
.
FindVar
(
name
);
}
private:
const
OperatorBase
&
op_
;
const
Scope
&
scope_
;
};
}
// namespace framework
}
// namespace paddle
paddle/fluid/operators/attention_lstm_op.cc
浏览文件 @
8bb824bb
...
...
@@ -14,7 +14,6 @@ limitations under the License. */
#include "paddle/fluid/operators/attention_lstm_op.h"
#include <string>
#include "paddle/fluid/operators/fusion_infershape_define.h"
#include "paddle/fluid/operators/math/blas.h"
#include "paddle/fluid/operators/math/cpu_vec.h"
#include "paddle/fluid/operators/math/fc_compute.h"
...
...
@@ -24,28 +23,28 @@ namespace paddle {
namespace
operators
{
void
AttentionLSTMOp
::
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
{
FUSION_INFERSHAPE_INIT
;
PADDLE_ENFORCE
(
fair_input
(
"X"
),
"Assert only one Input(X) of AttentionLSTM."
);
PADDLE_ENFORCE
(
fair_i
nput
(
"C0"
),
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Assert only one Input(X) of AttentionLSTM."
);
PADDLE_ENFORCE
(
ctx
->
HasI
nput
(
"C0"
),
"Assert only one Input(C0) of AttentionLSTM."
);
PADDLE_ENFORCE
(
fair_i
nput
(
"LSTMWeight"
),
PADDLE_ENFORCE
(
ctx
->
HasI
nput
(
"LSTMWeight"
),
"Assert only one Input(LSTMWeight) of AttentionLSTM."
);
PADDLE_ENFORCE
(
fair_i
nput
(
"LSTMBias"
),
PADDLE_ENFORCE
(
ctx
->
HasI
nput
(
"LSTMBias"
),
"Assert only one Input(LSTMBias) of AttentionLSTM."
);
PADDLE_ENFORCE
(
fair_i
nput
(
"AttentionWeight"
),
PADDLE_ENFORCE
(
ctx
->
HasI
nput
(
"AttentionWeight"
),
"Assert only one Input(AttentionWeight) of AttentionLSTM."
);
PADDLE_ENFORCE
(
fair_o
utput
(
"Hidden"
),
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"Hidden"
),
"Assert only one Output(Hidden) of AttentionLSTM."
);
PADDLE_ENFORCE
(
fair_o
utput
(
"Cell"
),
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"Cell"
),
"Assert only one Output(Cell) of AttentionLSTM."
);
PADDLE_ENFORCE
(
fair_o
utput
(
"AttentionedX"
),
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"AttentionedX"
),
"Assert only one Output(AttentionedX) of AttentionLSTM."
);
PADDLE_ENFORCE
(
fair_o
utput
(
"AttentionFCOut"
),
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"AttentionFCOut"
),
"Assert only one Output(AttentionFCOut) of AttentionLSTM."
);
PADDLE_ENFORCE
(
fair_o
utput
(
"LSTMX"
),
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"LSTMX"
),
"Assert only one Output(LSTMX) of AttentionLSTM."
);
PADDLE_ENFORCE
(
fair_o
utput
(
"LSTMOUT"
),
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"LSTMOUT"
),
"Assert only one Output(LSTMOUT) of AttentionLSTM."
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
...
...
@@ -66,7 +65,7 @@ void AttentionLSTMOp::InferShape(framework::InferShapeContext* ctx) const {
auto
c_dims
=
ctx
->
GetInputDim
(
"C0"
);
PADDLE_ENFORCE_EQ
(
c_dims
.
size
(),
2
,
"Input(C0)'s rank must be 2."
);
PADDLE_ENFORCE_EQ
(
c_dims
[
1
],
D
,
"C0 dims should be N x %d."
,
D
);
if
(
fair_i
nput
(
"H0"
))
{
if
(
ctx
->
HasI
nput
(
"H0"
))
{
auto
h_dims
=
ctx
->
GetInputDim
(
"H0"
);
PADDLE_ENFORCE
(
h_dims
==
c_dims
,
"The dimension of Input(H0) and Input(C0) "
...
...
@@ -80,7 +79,7 @@ void AttentionLSTMOp::InferShape(framework::InferShapeContext* ctx) const {
"AttentionWeight shapes must be (%d + %d) * 1."
,
M
,
D
);
PADDLE_ENFORCE_EQ
(
atten_w_dims
[
1
],
1
,
"AttentionWeight shapes must be (%d + %d) * 1."
,
M
,
D
);
if
(
fair_i
nput
(
"AttentionBias"
))
{
if
(
ctx
->
HasI
nput
(
"AttentionBias"
))
{
auto
atten_b_dims
=
ctx
->
GetInputDim
(
"AttentionBias"
);
PADDLE_ENFORCE_EQ
(
atten_b_dims
.
size
(),
2
,
"Input(AttentionBias)'s rank must be 2."
);
...
...
@@ -90,7 +89,7 @@ void AttentionLSTMOp::InferShape(framework::InferShapeContext* ctx) const {
"AttentionBias shapes must be 1 * 1."
);
}
if
(
fair_i
nput
(
"AttentionScalar"
))
{
if
(
ctx
->
HasI
nput
(
"AttentionScalar"
))
{
auto
dims
=
ctx
->
GetInputDim
(
"AttentionScalar"
);
PADDLE_ENFORCE_EQ
(
dims
.
size
(),
2
,
"Input(AttentionScalar)'s rank must be 2."
);
...
...
@@ -98,10 +97,10 @@ void AttentionLSTMOp::InferShape(framework::InferShapeContext* ctx) const {
PADDLE_ENFORCE_EQ
(
dims
[
1
],
1
,
"AttentionScalar shapes must be 1 * 1."
);
}
if
(
fair_i
nput
(
"AttentionScalarBias"
))
{
if
(
ctx
->
HasI
nput
(
"AttentionScalarBias"
))
{
auto
dims
=
ctx
->
GetInputDim
(
"AttentionScalarBias"
);
PADDLE_ENFORCE
(
fair_i
nput
(
"AttentionScalar"
),
ctx
->
HasI
nput
(
"AttentionScalar"
),
"AttentionScalar should not be null when have AttentionScalarBias."
);
PADDLE_ENFORCE_EQ
(
dims
.
size
(),
2
,
"Input(AttentionScalarBias)'s rank must be 2."
);
...
...
paddle/fluid/operators/fusion_gru_op.cc
浏览文件 @
8bb824bb
...
...
@@ -15,7 +15,6 @@ limitations under the License. */
#include "paddle/fluid/operators/fusion_gru_op.h"
#include <cstring> // for memcpy
#include <string>
#include "paddle/fluid/operators/fusion_infershape_define.h"
#include "paddle/fluid/operators/math/blas.h"
#include "paddle/fluid/operators/math/cpu_vec.h"
#include "paddle/fluid/operators/math/fc_compute.h"
...
...
@@ -26,14 +25,13 @@ namespace paddle {
namespace
operators
{
void
FusionGRUOp
::
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
{
FUSION_INFERSHAPE_INIT
;
PADDLE_ENFORCE
(
fair_input
(
"X"
),
"Assert only one Input(X) of GRU."
);
PADDLE_ENFORCE
(
fair_input
(
"WeightX"
),
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Assert only one Input(X) of GRU."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"WeightX"
),
"Assert only one Input(WeightX) of GRU."
);
PADDLE_ENFORCE
(
fair_i
nput
(
"WeightH"
),
PADDLE_ENFORCE
(
ctx
->
HasI
nput
(
"WeightH"
),
"Assert only one Input(WeightH) of GRU."
);
PADDLE_ENFORCE
(
fair_o
utput
(
"XX"
),
"Assert only one Output(XX) of GRU."
);
PADDLE_ENFORCE
(
fair_o
utput
(
"Hidden"
),
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"XX"
),
"Assert only one Output(XX) of GRU."
);
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"Hidden"
),
"Assert only one Output(Hidden) of GRU."
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
...
...
@@ -60,12 +58,12 @@ void FusionGRUOp::InferShape(framework::InferShapeContext* ctx) const {
"should be 3 * %d."
,
frame_size
);
if
(
fair_i
nput
(
"H0"
))
{
if
(
ctx
->
HasI
nput
(
"H0"
))
{
auto
h0_dims
=
ctx
->
GetInputDim
(
"H0"
);
PADDLE_ENFORCE_EQ
(
h0_dims
[
1
],
frame_size
,
"The width of H0 must be equal to frame_size."
);
}
if
(
fair_i
nput
(
"Bias"
))
{
if
(
ctx
->
HasI
nput
(
"Bias"
))
{
auto
b_dims
=
ctx
->
GetInputDim
(
"Bias"
);
PADDLE_ENFORCE_EQ
(
b_dims
.
size
(),
2
,
"The rank of Input(Bias) should be 2."
);
PADDLE_ENFORCE_EQ
(
b_dims
[
0
],
1
,
...
...
@@ -81,11 +79,11 @@ void FusionGRUOp::InferShape(framework::InferShapeContext* ctx) const {
xx_width
=
wx_dims
[
1
];
}
else
{
xx_width
=
x_dims
[
1
]
>
wx_dims
[
1
]
?
wx_dims
[
1
]
:
x_dims
[
1
];
PADDLE_ENFORCE
(
fair_o
utput
(
"ReorderedH0"
),
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"ReorderedH0"
),
"Assert only one Output(ReorderedH0) of GRU."
);
PADDLE_ENFORCE
(
fair_o
utput
(
"BatchedInput"
),
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"BatchedInput"
),
"Assert only one Output(BatchedInput) of GRU."
);
PADDLE_ENFORCE
(
fair_o
utput
(
"BatchedOut"
),
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"BatchedOut"
),
"Assert only one Output(BatchedOut) of GRU."
);
ctx
->
SetOutputDim
(
"BatchedInput"
,
{
x_dims
[
0
],
wx_dims
[
1
]});
ctx
->
SetOutputDim
(
"BatchedOut"
,
out_dims
);
...
...
paddle/fluid/operators/fusion_infershape_define.h
已删除
100644 → 0
浏览文件 @
c4394bc5
/* Copyright (c) 2018 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. */
#ifndef PADDLE_FLUID_OPERATORS_FUSION_INFERSHAPE_DEFINE_H_
#define PADDLE_FLUID_OPERATORS_FUSION_INFERSHAPE_DEFINE_H_
#include <string>
#include "paddle/fluid/framework/shape_runtime_infer.h"
namespace
paddle
{
namespace
operators
{
#define FUSION_INFERSHAPE_INIT \
auto* runtime_ctx = dynamic_cast<framework::RuntimeInferShapeContext*>(ctx); \
if (runtime_ctx == nullptr) { \
LOG(FATAL) << "Should have runtime infer context"; \
} \
const auto& ins = runtime_ctx->OpBase().Inputs(); \
const auto& outs = runtime_ctx->OpBase().Outputs(); \
const auto& scope = runtime_ctx->InferScope(); \
const auto ins_end = ins.end(); \
const auto outs_end = outs.end(); \
auto fair_input = [&](const std::string& name) -> bool { \
auto it = ins.find(name); \
if (it == ins_end) { \
return false; \
} \
const auto& in = it->second; \
if (in.size() != 1 || in[0] == framework::kEmptyVarName) { \
return false; \
} \
return scope.FindVar(in[0]) != nullptr; \
}; \
auto fair_output = [&](const std::string& name) -> bool { \
auto it = outs.find(name); \
if (it == outs_end) { \
return false; \
} \
const auto& out = it->second; \
if (out.size() != 1 || out[0] == framework::kEmptyVarName) { \
return false; \
} \
return scope.FindVar(out[0]) != nullptr; \
}
}
// namespace operators
}
// namespace paddle
#endif // PADDLE_FLUID_OPERATORS_FUSION_INFERSHAPE_DEFINE_H_
paddle/fluid/operators/fusion_lstm_op.cc
浏览文件 @
8bb824bb
...
...
@@ -14,7 +14,6 @@ limitations under the License. */
#include "paddle/fluid/operators/fusion_lstm_op.h"
#include <string>
#include "paddle/fluid/operators/fusion_infershape_define.h"
#include "paddle/fluid/operators/math/blas.h"
#include "paddle/fluid/operators/math/cpu_vec.h"
#include "paddle/fluid/operators/math/fc_compute.h"
...
...
@@ -25,23 +24,23 @@ namespace paddle {
namespace
operators
{
void
FusionLSTMOp
::
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
{
FUSION_INFERSHAPE_INIT
;
PADDLE_ENFORCE
(
fair_input
(
"X"
),
"Assert only one Input(X) of LSTM."
);
PADDLE_ENFORCE
(
fair_input
(
"WeightX"
),
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Assert only one Input(X) of LSTM."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"WeightX"
),
"Assert only one Input(WeightX) of LSTM."
);
PADDLE_ENFORCE
(
fair_i
nput
(
"WeightH"
),
PADDLE_ENFORCE
(
ctx
->
HasI
nput
(
"WeightH"
),
"Assert only one Input(WeightH) of LSTM."
);
PADDLE_ENFORCE
(
fair_i
nput
(
"Bias"
),
"Assert only one Input(Bias) of LSTM."
);
PADDLE_ENFORCE
(
fair_o
utput
(
"XX"
),
"Assert only one Output(XX) of LSTM."
);
PADDLE_ENFORCE
(
fair_o
utput
(
"Hidden"
),
PADDLE_ENFORCE
(
ctx
->
HasI
nput
(
"Bias"
),
"Assert only one Input(Bias) of LSTM."
);
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"XX"
),
"Assert only one Output(XX) of LSTM."
);
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"Hidden"
),
"Assert only one Output(Hidden) of LSTM."
);
PADDLE_ENFORCE
(
fair_output
(
"Cell"
),
"Assert only one Output(Cell) of LSTM."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Cell"
),
"Assert only one Output(Cell) of LSTM."
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
PADDLE_ENFORCE_EQ
(
x_dims
.
size
(),
2
,
"Input(X)'s rank must be 2."
);
if
(
fair_i
nput
(
"H0"
))
{
PADDLE_ENFORCE
(
fair_i
nput
(
"C0"
),
if
(
ctx
->
HasI
nput
(
"H0"
))
{
PADDLE_ENFORCE
(
ctx
->
HasI
nput
(
"C0"
),
"Input(Cell) and Input(Hidden) of LSTM should not "
"be null at the same time."
);
auto
h_dims
=
ctx
->
GetInputDim
(
"H0"
);
...
...
@@ -93,15 +92,15 @@ void FusionLSTMOp::InferShape(framework::InferShapeContext* ctx) const {
xx_width
=
wx_dims
[
1
];
}
else
{
xx_width
=
x_dims
[
1
]
>
wx_dims
[
1
]
?
wx_dims
[
1
]
:
x_dims
[
1
];
PADDLE_ENFORCE
(
fair_o
utput
(
"BatchedInput"
),
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"BatchedInput"
),
"Assert only one Output(BatchedInput) of LSTM."
);
PADDLE_ENFORCE
(
fair_o
utput
(
"BatchedHidden"
),
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"BatchedHidden"
),
"Assert only one Output(BatchedHidden) of LSTM."
);
PADDLE_ENFORCE
(
fair_o
utput
(
"BatchedCell"
),
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"BatchedCell"
),
"Assert only one Output(BatchedCell) of LSTM."
);
PADDLE_ENFORCE
(
fair_o
utput
(
"ReorderedH0"
),
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"ReorderedH0"
),
"Assert only one Output(ReorderedH0) of LSTM"
);
PADDLE_ENFORCE
(
fair_o
utput
(
"ReorderedC0"
),
PADDLE_ENFORCE
(
ctx
->
HasO
utput
(
"ReorderedC0"
),
"Assert only one Output(ReorderedC0) of LSTM."
);
ctx
->
SetOutputDim
(
"BatchedInput"
,
{
x_dims
[
0
],
wx_dims
[
1
]});
ctx
->
SetOutputDim
(
"BatchedHidden"
,
out_dims
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录