Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
36aeb30d
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
12 个月 前同步成功
通知
692
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
36aeb30d
编写于
9月 21, 2017
作者:
D
dangqingqing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove LoDTensor in some operators' InferShape and refine ShareLoD function.
上级
3ec48480
变更
31
隐藏空白更改
内联
并排
Showing
31 changed file
with
93 addition
and
127 deletion
+93
-127
paddle/framework/operator.cc
paddle/framework/operator.cc
+7
-8
paddle/framework/operator.h
paddle/framework/operator.h
+15
-36
paddle/operators/accuracy_op.cc
paddle/operators/accuracy_op.cc
+2
-2
paddle/operators/add_op.cc
paddle/operators/add_op.cc
+1
-1
paddle/operators/concat_op.cc
paddle/operators/concat_op.cc
+1
-1
paddle/operators/cos_sim_op.cc
paddle/operators/cos_sim_op.cc
+6
-8
paddle/operators/cross_entropy_op.cc
paddle/operators/cross_entropy_op.cc
+1
-1
paddle/operators/dropout_op.cc
paddle/operators/dropout_op.cc
+1
-1
paddle/operators/elementwise_mul_op.cc
paddle/operators/elementwise_mul_op.cc
+4
-6
paddle/operators/fill_zeros_like_op.cc
paddle/operators/fill_zeros_like_op.cc
+2
-2
paddle/operators/gather_op.cc
paddle/operators/gather_op.cc
+2
-2
paddle/operators/gaussian_random_op.cc
paddle/operators/gaussian_random_op.cc
+1
-1
paddle/operators/lookup_table_op.cc
paddle/operators/lookup_table_op.cc
+3
-3
paddle/operators/mean_op.cc
paddle/operators/mean_op.cc
+2
-2
paddle/operators/minus_op.cc
paddle/operators/minus_op.cc
+2
-2
paddle/operators/mul_op.cc
paddle/operators/mul_op.cc
+4
-6
paddle/operators/pad_op.cc
paddle/operators/pad_op.cc
+3
-3
paddle/operators/prelu_op.cc
paddle/operators/prelu_op.cc
+4
-4
paddle/operators/reshape_op.cc
paddle/operators/reshape_op.cc
+3
-3
paddle/operators/rowwise_add_op.cc
paddle/operators/rowwise_add_op.cc
+4
-4
paddle/operators/scale_op.cc
paddle/operators/scale_op.cc
+2
-2
paddle/operators/scatter_op.cc
paddle/operators/scatter_op.cc
+3
-4
paddle/operators/sequence_avg_pool_op.cc
paddle/operators/sequence_avg_pool_op.cc
+2
-3
paddle/operators/sgd_op.cc
paddle/operators/sgd_op.cc
+1
-1
paddle/operators/sigmoid_op.cc
paddle/operators/sigmoid_op.cc
+3
-4
paddle/operators/softmax_op.cc
paddle/operators/softmax_op.cc
+2
-3
paddle/operators/split_op.cc
paddle/operators/split_op.cc
+1
-1
paddle/operators/squared_l2_distance_op.cc
paddle/operators/squared_l2_distance_op.cc
+5
-7
paddle/operators/sum_op.cc
paddle/operators/sum_op.cc
+3
-3
paddle/operators/top_k_op.cc
paddle/operators/top_k_op.cc
+2
-2
paddle/operators/uniform_random_op.cc
paddle/operators/uniform_random_op.cc
+1
-1
未找到文件。
paddle/framework/operator.cc
浏览文件 @
36aeb30d
...
...
@@ -207,23 +207,22 @@ const std::vector<const Tensor*> InferShapeContext::MultiInput<Tensor>(
}
template
<
>
Tensor
*
Execution
Context
::
Output
<
Tensor
>
(
const
std
::
string
&
name
)
const
{
auto
*
var
=
OutputVar
(
name
);
return
var
==
nullptr
?
nullptr
:
const_cast
<
Tensor
*>
(
GetTensorFromVar
(
var
)
);
Tensor
*
InferShape
Context
::
Output
<
Tensor
>
(
const
std
::
string
&
name
)
const
{
auto
var
=
OutputVar
(
name
);
return
var
==
nullptr
?
nullptr
:
var
->
GetMutable
<
LoDTensor
>
(
);
}
template
<
>
std
::
vector
<
Tensor
*>
Execution
Context
::
MultiOutput
<
Tensor
>
(
std
::
vector
<
Tensor
*>
InferShape
Context
::
MultiOutput
<
Tensor
>
(
const
std
::
string
&
name
)
const
{
auto
names
=
op
().
Outputs
(
name
);
std
::
vector
<
Tensor
*>
res
;
res
.
reserve
(
names
.
size
());
std
::
transform
(
names
.
begin
(),
names
.
end
(),
std
::
back_inserter
(
res
),
[
&
](
const
std
::
string
&
sub_name
)
{
auto
var
=
scope
().
FindVar
(
sub_name
);
return
var
==
nullptr
?
nullptr
:
const_cast
<
Tensor
*>
(
GetTensorFromVar
(
var
));
auto
var
=
scope_
.
FindVar
(
sub_name
);
return
var
==
nullptr
?
nullptr
:
var
->
GetMutable
<
LoDTensor
>
();
});
return
res
;
}
...
...
paddle/framework/operator.h
浏览文件 @
36aeb30d
...
...
@@ -277,9 +277,9 @@ class InferShapeContext {
return
res
;
}
std
::
vector
<
const
Variable
*>
MultiOutputVar
(
const
std
::
string
&
name
)
const
{
std
::
vector
<
Variable
*>
MultiOutputVar
(
const
std
::
string
&
name
)
const
{
auto
names
=
op_
.
Outputs
(
name
);
std
::
vector
<
const
Variable
*>
res
;
std
::
vector
<
Variable
*>
res
;
res
.
reserve
(
names
.
size
());
std
::
transform
(
names
.
begin
(),
names
.
end
(),
std
::
back_inserter
(
res
),
[
this
](
const
std
::
string
&
name
)
{
...
...
@@ -336,12 +336,19 @@ class InferShapeContext {
return
&
var
->
Get
<
Tensor
>
();
}
void
ShareLoD
(
const
std
::
string
&
in
,
const
std
::
string
&
out
)
const
{
PADDLE_ENFORCE
(
InputVar
(
in
)
->
IsType
<
LoDTensor
>
(),
"The Input(%s) must be LoDTensor."
,
in
);
PADDLE_ENFORCE
(
OutputVar
(
out
)
->
IsType
<
LoDTensor
>
(),
"The Output(%s) must be LoDTensor."
,
out
);
Output
<
LoDTensor
>
(
out
)
->
set_lod
(
Input
<
LoDTensor
>
(
in
)
->
lod
());
void
ShareLoD
(
const
std
::
string
&
in
,
const
std
::
string
&
out
,
size_t
i
=
0
,
size_t
j
=
0
)
const
{
PADDLE_ENFORCE_LT
(
i
,
InputSize
(
in
));
PADDLE_ENFORCE_LT
(
j
,
OutputSize
(
out
));
auto
*
in_var
=
MultiInputVar
(
in
)[
i
];
auto
*
out_var
=
MultiOutputVar
(
out
)[
j
];
PADDLE_ENFORCE
(
in_var
->
IsType
<
LoDTensor
>
(),
"The %d-th input of Input(%s) must be LoDTensor."
,
in
);
PADDLE_ENFORCE
(
out_var
->
IsType
<
LoDTensor
>
(),
"The %d-th output of Output(%s) must be LoDTensor."
,
out
);
auto
in_tensor
=
in_var
->
Get
<
LoDTensor
>
();
auto
*
out_tensor
=
out_var
->
GetMutable
<
LoDTensor
>
();
out_tensor
->
set_lod
(
in_tensor
.
lod
());
}
private:
...
...
@@ -388,38 +395,10 @@ class ExecutionContext : public InferShapeContext {
return
device_context_
;
}
// redefine Output function,
// use Variable::Get instead of Variable::GetMutable
template
<
typename
T
>
T
*
Output
(
const
std
::
string
&
name
)
const
{
auto
var
=
OutputVar
(
name
);
return
var
==
nullptr
?
nullptr
:
const_cast
<
T
*>
(
&
var
->
Get
<
T
>
());
}
// redefine MultiOutput function.
// use Variable::Get instead of Variable::GetMutable
template
<
typename
T
>
std
::
vector
<
T
*>
MultiOutput
(
const
std
::
string
&
name
)
const
{
auto
names
=
op
().
Outputs
(
name
);
std
::
vector
<
T
*>
res
;
res
.
reserve
(
names
.
size
());
std
::
transform
(
names
.
begin
(),
names
.
end
(),
std
::
back_inserter
(
res
),
[
&
](
const
std
::
string
&
sub_name
)
{
return
Output
<
T
>
(
sub_name
);
});
return
res
;
}
private:
const
platform
::
DeviceContext
&
device_context_
;
};
template
<>
Tensor
*
ExecutionContext
::
Output
<
Tensor
>
(
const
std
::
string
&
name
)
const
;
template
<>
std
::
vector
<
Tensor
*>
ExecutionContext
::
MultiOutput
<
Tensor
>
(
const
std
::
string
&
name
)
const
;
class
OpKernel
{
public:
/**
...
...
paddle/operators/accuracy_op.cc
浏览文件 @
36aeb30d
...
...
@@ -39,8 +39,8 @@ class AccuracyOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_EQ
(
inference
->
dims
()[
0
],
label
->
dims
()[
0
],
"inference size must be the same as label size"
);
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Accuracy"
)
->
Resize
({
1
});
ctx
.
ShareLoD
(
"Inference"
,
"Accuracy"
);
ctx
.
Output
<
framework
::
Tensor
>
(
"Accuracy"
)
->
Resize
({
1
});
ctx
.
ShareLoD
(
"Inference"
,
/*->*/
"Accuracy"
);
}
};
...
...
paddle/operators/add_op.cc
浏览文件 @
36aeb30d
...
...
@@ -33,7 +33,7 @@ class AddOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_EQ
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
(),
ctx
.
Input
<
Tensor
>
(
"Y"
)
->
dims
(),
"Two input of Add Op's dimension must be same."
);
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
)
->
Resize
(
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
());
}
};
...
...
paddle/operators/concat_op.cc
浏览文件 @
36aeb30d
...
...
@@ -29,7 +29,7 @@ class ConcatOp : public framework::OperatorWithKernel {
"Output(Out) of ConcatOp should not be null."
);
auto
ins
=
ctx
.
MultiInput
<
framework
::
Tensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
size_t
axis
=
static_cast
<
size_t
>
(
ctx
.
Attr
<
int
>
(
"axis"
));
size_t
n
=
ins
.
size
();
...
...
paddle/operators/cos_sim_op.cc
浏览文件 @
36aeb30d
...
...
@@ -54,10 +54,10 @@ class CosSimOp : public framework::OperatorWithKernel {
" just 1 (which will be broadcasted to match Input(X))."
);
// resize tensor
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
)
->
Resize
({
x_dims
[
0
],
1
});
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"XNorm"
)
->
Resize
({
x_dims
[
0
],
1
});
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"YNorm"
)
->
Resize
({
y_dims
[
0
],
1
});
ctx
.
ShareLoD
(
"X"
,
"Out"
);
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
({
x_dims
[
0
],
1
});
ctx
.
Output
<
framework
::
Tensor
>
(
"XNorm"
)
->
Resize
({
x_dims
[
0
],
1
});
ctx
.
Output
<
framework
::
Tensor
>
(
"YNorm"
)
->
Resize
({
y_dims
[
0
],
1
});
ctx
.
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
};
...
...
@@ -143,10 +143,8 @@ class CosSimOpGrad : public framework::OperatorWithKernel {
"Shape of Input(Out@Grad) must be [X.Dim(0), 1]."
);
// resize tensor
auto
*
x_grad
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
y_grad
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"Y"
));
auto
*
x_grad
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
y_grad
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
if
(
x_grad
)
x_grad
->
Resize
(
x_dims
);
if
(
y_grad
)
y_grad
->
Resize
(
y_dims
);
}
...
...
paddle/operators/cross_entropy_op.cc
浏览文件 @
36aeb30d
...
...
@@ -52,7 +52,7 @@ class CrossEntropyOp : public framework::OperatorWithKernel {
}
ctx
.
Output
<
LoDTensor
>
(
"Y"
)
->
Resize
({
x
->
dims
()[
0
],
1
});
ctx
.
ShareLoD
(
"X"
,
"Y"
);
ctx
.
ShareLoD
(
"X"
,
/*->*/
"Y"
);
}
};
...
...
paddle/operators/dropout_op.cc
浏览文件 @
36aeb30d
...
...
@@ -38,7 +38,7 @@ class DropoutOp : public framework::OperatorWithKernel {
if
(
ctx
.
Attr
<
int
>
(
"is_training"
)
==
1
)
{
ctx
.
Output
<
LoDTensor
>
(
"Mask"
)
->
Resize
(
dims
);
}
ctx
.
ShareLoD
(
"X"
,
"Out"
);
ctx
.
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
};
...
...
paddle/operators/elementwise_mul_op.cc
浏览文件 @
36aeb30d
...
...
@@ -37,8 +37,8 @@ class ElementWiseMulOp : public framework::OperatorWithKernel {
auto
y_dim
=
ctx
.
Input
<
Tensor
>
(
"Y"
)
->
dims
();
PADDLE_ENFORCE_GE
(
x_dim
.
size
(),
y_dim
.
size
(),
"Rank of first input must >= rank of second input."
)
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
)
->
Resize
(
x_dim
);
ctx
.
ShareLoD
(
"X"
,
"Out"
);
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
(
x_dim
);
ctx
.
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
};
...
...
@@ -91,10 +91,8 @@ class ElementWiseMulOpGrad : public framework::OperatorWithKernel {
auto
x_dims
=
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
();
auto
y_dims
=
ctx
.
Input
<
Tensor
>
(
"Y"
)
->
dims
();
auto
out_dims
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
))
->
dims
();
auto
*
x_grad
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
y_grad
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"Y"
));
auto
*
x_grad
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
y_grad
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
PADDLE_ENFORCE_GE
(
x_dims
.
size
(),
y_dims
.
size
(),
"Rank of first input must >= rank of second input."
)
...
...
paddle/operators/fill_zeros_like_op.cc
浏览文件 @
36aeb30d
...
...
@@ -28,9 +28,9 @@ class FillZerosLikeOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
OutputVar
(
"Y"
),
"Output(Y) of FillZerosLikeOp should not be null."
);
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Y"
)
->
Resize
(
ctx
.
Output
<
framework
::
Tensor
>
(
"Y"
)
->
Resize
(
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
)
->
dims
());
ctx
.
ShareLoD
(
"X"
,
"Y"
);
ctx
.
ShareLoD
(
"X"
,
/*->*/
"Y"
);
}
};
...
...
paddle/operators/gather_op.cc
浏览文件 @
36aeb30d
...
...
@@ -35,7 +35,7 @@ class GatherOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_GE
(
batch_size
,
0
,
"Batch size must be >0"
);
framework
::
DDim
output_dims
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
());
output_dims
[
0
]
=
batch_size
;
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
)
->
Resize
(
output_dims
);
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
(
output_dims
);
}
};
...
...
@@ -45,7 +45,7 @@ class GatherGradOp : public framework::OperatorWithKernel {
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
auto
X_grad
=
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
X_grad
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
X
=
ctx
.
Input
<
Tensor
>
(
"X"
);
X_grad
->
Resize
(
X
->
dims
());
...
...
paddle/operators/gaussian_random_op.cc
浏览文件 @
36aeb30d
...
...
@@ -48,7 +48,7 @@ class GaussianRandomOp : public framework::OperatorWithKernel {
ctx
.
OutputVar
(
"Out"
),
"Output(Out) of GaussianRandomOp should not be null."
);
auto
*
tensor
=
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
);
auto
*
tensor
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
auto
dims
=
Attr
<
std
::
vector
<
int
>>
(
"dims"
);
std
::
vector
<
int64_t
>
temp
;
temp
.
reserve
(
dims
.
size
());
...
...
paddle/operators/lookup_table_op.cc
浏览文件 @
36aeb30d
...
...
@@ -32,10 +32,10 @@ class LookupTableOp : public framework::OperatorWithKernel {
auto
table_t
=
ctx
.
Input
<
Tensor
>
(
"W"
);
auto
ids_t
=
ctx
.
Input
<
Tensor
>
(
"Ids"
);
auto
output_t
=
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
);
auto
output_t
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
output_t
->
Resize
({
ids_t
->
dims
()[
0
],
table_t
->
dims
()[
1
]});
ctx
.
ShareLoD
(
"Ids"
,
"Out"
);
ctx
.
ShareLoD
(
"Ids"
,
/*->*/
"Out"
);
}
};
...
...
@@ -69,7 +69,7 @@ class LookupTableOpGrad : public framework::OperatorWithKernel {
void
InferShape
(
const
framework
::
InferShapeContext
&
context
)
const
override
{
auto
table
=
context
.
Input
<
Tensor
>
(
"W"
);
auto
d_table
=
context
.
Output
<
framework
::
LoD
Tensor
>
(
framework
::
GradVarName
(
"W"
));
context
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"W"
));
d_table
->
Resize
(
table
->
dims
());
}
};
...
...
paddle/operators/mean_op.cc
浏览文件 @
36aeb30d
...
...
@@ -27,7 +27,7 @@ class MeanOp : public framework::OperatorWithKernel {
"Input(X) of MeanOp should not be null."
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
OutputVar
(
"Out"
),
"Output(Out) of MeanOp should not be null."
);
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
)
->
Resize
({
1
});
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
({
1
});
}
};
...
...
@@ -48,7 +48,7 @@ class MeanGradOp : public framework::OperatorWithKernel {
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
framework
::
GradVarName
(
"X"
))
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
))
->
Resize
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
());
}
};
...
...
paddle/operators/minus_op.cc
浏览文件 @
36aeb30d
...
...
@@ -40,8 +40,8 @@ class MinusOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_EQ
(
left_tensor
->
numel
(),
right_tensor
->
numel
(),
"Minus operator must take two tensor with same num of elements"
);
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
)
->
Resize
(
left_tensor
->
dims
());
ctx
.
ShareLoD
(
"X"
,
"Out"
);
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
(
left_tensor
->
dims
());
ctx
.
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
};
...
...
paddle/operators/mul_op.cc
浏览文件 @
36aeb30d
...
...
@@ -53,9 +53,9 @@ class MulOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_EQ
(
x_mat_dims
[
1
],
y_mat_dims
[
0
],
"First matrix's width must be equal with second matrix's height."
);
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
)
->
Resize
(
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
(
{
x_mat_dims
[
0
],
y_mat_dims
[
1
]});
ctx
.
ShareLoD
(
"X"
,
"Out"
);
ctx
.
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
};
...
...
@@ -109,10 +109,8 @@ class MulOpGrad : public framework::OperatorWithKernel {
auto
x_dims
=
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
();
auto
y_dims
=
ctx
.
Input
<
Tensor
>
(
"Y"
)
->
dims
();
auto
out_dims
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
))
->
dims
();
auto
*
x_grad
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
y_grad
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"Y"
));
auto
*
x_grad
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
y_grad
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
auto
x_mat_dims
=
framework
::
flatten_to_2d
(
x_dims
,
Attr
<
int
>
(
"x_num_col_dims"
));
...
...
paddle/operators/pad_op.cc
浏览文件 @
36aeb30d
...
...
@@ -39,12 +39,12 @@ class PadOp : public framework::OperatorWithKernel {
for
(
int
i
=
0
;
i
<
x_dim
.
size
();
++
i
)
{
out_dims
[
i
]
=
x_dim
[
i
]
+
paddings
[
i
*
2
]
+
paddings
[
i
*
2
+
1
];
}
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
)
->
Resize
(
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
(
framework
::
make_ddim
(
out_dims
));
if
(
out_dims
[
0
]
==
x_dim
[
0
])
{
// Only pass LoD when the first dimension is equal between
// output and input.
ctx
.
ShareLoD
(
"X"
,
"Out"
);
ctx
.
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
}
};
...
...
@@ -106,7 +106,7 @@ class PadOpGrad : public framework::OperatorWithKernel {
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
framework
::
GradVarName
(
"Out"
)),
"Input(Out@GRAD) should not be null"
);
auto
x_dims
=
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
();
auto
*
x_g
=
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
x_g
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
if
(
x_g
!=
nullptr
)
{
x_g
->
Resize
(
x_dims
);
}
...
...
paddle/operators/prelu_op.cc
浏览文件 @
36aeb30d
...
...
@@ -36,9 +36,9 @@ class PReluOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
OutputVar
(
"Out"
),
"Output(Out) should not be null"
);
auto
*
out
=
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
out
->
Resize
(
in
->
dims
());
ctx
.
ShareLoD
(
"X"
,
"Out"
);
ctx
.
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
};
...
...
@@ -72,11 +72,11 @@ class PReluGradOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"X"
),
"Input(X) must not be null."
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
framework
::
GradVarName
(
"Out"
)),
"Input(Out@GRAD) should not be null"
);
auto
*
dx
=
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
dx
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
x
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
*
dalpha
=
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
framework
::
GradVarName
(
"Alpha"
));
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Alpha"
));
auto
*
alpha
=
ctx
.
Input
<
framework
::
Tensor
>
(
"Alpha"
);
dx
->
Resize
(
x
->
dims
());
...
...
paddle/operators/reshape_op.cc
浏览文件 @
36aeb30d
...
...
@@ -50,11 +50,11 @@ class ReshapeOp : public framework::OperatorWithKernel {
std
::
transform
(
shape
.
begin
(),
shape
.
end
(),
shape_int64
.
begin
(),
[](
int
a
)
{
return
static_cast
<
int64_t
>
(
a
);
});
auto
out_dims
=
framework
::
make_ddim
(
shape_int64
);
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
)
->
Resize
(
out_dims
);
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
(
out_dims
);
if
(
shape
[
0
]
==
in
->
dims
()[
0
])
{
// Only pass LoD when the first dimension is equal between
// output and input.
ctx
.
ShareLoD
(
"X"
,
"Out"
);
ctx
.
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
}
};
...
...
@@ -99,7 +99,7 @@ class ReshapeGradOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
framework
::
GradVarName
(
"Out"
)),
"Input(Out@GRAD) shouldn't be null."
);
auto
dims
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
)
->
dims
();
auto
*
d_in
=
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
d_in
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
d_in
->
Resize
(
dims
);
}
};
...
...
paddle/operators/rowwise_add_op.cc
浏览文件 @
36aeb30d
...
...
@@ -44,8 +44,8 @@ class RowwiseAddOp : public framework::OperatorWithKernel {
framework
::
slice_ddim
(
x_dims
,
num_col_dims
,
x_dims
.
size
()),
b_dims
,
"The width of two operands must be same"
);
PADDLE_ENFORCE_EQ
(
ctx
.
OutputSize
(
"Out"
),
1
,
"The output size must be 1"
);
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
)
->
Resize
(
x_dims
);
ctx
.
ShareLoD
(
"X"
,
"Out"
);
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
(
x_dims
);
ctx
.
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
};
...
...
@@ -84,8 +84,8 @@ class RowwiseAddGradOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_EQ
(
framework
::
slice_ddim
(
x_dims
,
num_col_dims
,
x_dims
.
size
()),
b_dims
,
"The width of two operands must be same"
);
auto
*
dx
=
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
db
=
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
framework
::
GradVarName
(
"b"
));
auto
*
dx
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
db
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"b"
));
if
(
dx
)
dx
->
Resize
(
x_dims
);
if
(
db
)
db
->
Resize
(
b_dims
);
}
...
...
paddle/operators/scale_op.cc
浏览文件 @
36aeb30d
...
...
@@ -33,9 +33,9 @@ class ScaleOp : public framework::OperatorWithKernel {
"Output(Out) of ScaleOp should not be null."
);
auto
*
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
out
->
Resize
(
in
->
dims
());
ctx
.
ShareLoD
(
"X"
,
"Out"
);
// ctx.ShareLoD("X", /*->*/
"Out");
}
};
...
...
paddle/operators/scatter_op.cc
浏览文件 @
36aeb30d
...
...
@@ -44,7 +44,7 @@ class ScatterOp : public framework::OperatorWithKernel {
framework
::
DDim
data_dim
(
ctx
.
Input
<
Tensor
>
(
"Updates"
)
->
dims
());
for
(
int
i
=
1
;
i
<
data_dim
.
size
();
++
i
)
PADDLE_ENFORCE_EQ
(
data_dim
[
i
],
ctx
.
Input
<
Tensor
>
(
"Updates"
)
->
dims
()[
i
]);
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
)
->
Resize
(
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
(
ctx
.
Input
<
Tensor
>
(
"Ref"
)
->
dims
());
}
};
...
...
@@ -56,10 +56,9 @@ class ScatterGradOp : public framework::OperatorWithKernel {
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
auto
*
dUpdates
=
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
framework
::
GradVarName
(
"Updates"
));
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Updates"
));
auto
*
Updates
=
ctx
.
Input
<
Tensor
>
(
"Updates"
);
auto
*
dRef
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"Ref"
));
auto
*
dRef
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Ref"
));
auto
*
Ref
=
ctx
.
Input
<
Tensor
>
(
"Ref"
);
dRef
->
Resize
(
Ref
->
dims
());
...
...
paddle/operators/sequence_avg_pool_op.cc
浏览文件 @
36aeb30d
...
...
@@ -38,7 +38,7 @@ class SequenceAvgPoolOp : public framework::OperatorWithKernel {
/*batch size = */
static_cast
<
int64_t
>
(
lod
[
0
].
size
()
-
1
),
"The first dimension of Input(X) must be large than batch size."
);
dims
[
0
]
=
lod
[
0
].
size
()
-
1
;
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
)
->
Resize
({
dims
});
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
({
dims
});
}
};
...
...
@@ -74,8 +74,7 @@ class SequenceAvgPoolGradOp : public framework::OperatorWithKernel {
for
(
int64_t
i
=
1
;
i
<
og_dims
.
size
();
++
i
)
{
PADDLE_ENFORCE_EQ
(
og_dims
[
i
],
x_dims
[
i
],
"The dimension mismatch."
);
}
auto
*
x_grad
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
x_grad
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
x_grad
->
Resize
(
x_dims
);
}
};
...
...
paddle/operators/sgd_op.cc
浏览文件 @
36aeb30d
...
...
@@ -33,7 +33,7 @@ class SGDOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_EQ
(
ctx
.
Input
<
Tensor
>
(
"param"
)
->
dims
(),
ctx
.
Input
<
Tensor
>
(
"grad"
)
->
dims
(),
"Two input of SGD Op's dimension must be same."
);
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"param_out"
)
ctx
.
Output
<
framework
::
Tensor
>
(
"param_out"
)
->
Resize
(
ctx
.
Input
<
Tensor
>
(
"param"
)
->
dims
());
}
};
...
...
paddle/operators/sigmoid_op.cc
浏览文件 @
36aeb30d
...
...
@@ -28,9 +28,8 @@ class SigmoidOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
OutputVar
(
"Y"
),
"Output(Y) of SigmoidOp should not be null."
);
ctx
.
Output
<
framework
::
LoDTensor
>
(
"Y"
)
->
Resize
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
());
ctx
.
ShareLoD
(
"X"
,
"Y"
);
ctx
.
Output
<
framework
::
Tensor
>
(
"Y"
)
->
Resize
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
());
ctx
.
ShareLoD
(
"X"
,
/*->*/
"Y"
);
}
};
...
...
@@ -51,7 +50,7 @@ class SigmoidOpGrad : public framework::OperatorWithKernel {
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
framework
::
GradVarName
(
"X"
))
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
))
->
Resize
(
ctx
.
Input
<
Tensor
>
(
"Y"
)
->
dims
());
}
};
...
...
paddle/operators/softmax_op.cc
浏览文件 @
36aeb30d
...
...
@@ -30,8 +30,7 @@ class SoftmaxOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
().
size
()
==
2UL
,
"The input of softmax op must be a matrix."
);
ctx
.
Output
<
framework
::
LoDTensor
>
(
"Y"
)
->
Resize
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
());
ctx
.
Output
<
framework
::
Tensor
>
(
"Y"
)
->
Resize
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
());
}
};
...
...
@@ -77,7 +76,7 @@ class SoftmaxOpGrad : public framework::OperatorWithKernel {
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Y"
))
->
dims
(),
"Input(Y) and its gradients should have a same shape."
);
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
framework
::
GradVarName
(
"X"
))
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
))
->
Resize
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
());
}
};
...
...
paddle/operators/split_op.cc
浏览文件 @
36aeb30d
...
...
@@ -27,7 +27,7 @@ class SplitOp : public framework::OperatorWithKernel {
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
// infershape
auto
*
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
outs
=
ctx
.
MultiOutput
<
framework
::
LoD
Tensor
>
(
"Out"
);
auto
outs
=
ctx
.
MultiOutput
<
framework
::
Tensor
>
(
"Out"
);
size_t
axis
=
static_cast
<
size_t
>
(
ctx
.
Attr
<
int
>
(
"axis"
));
size_t
num
=
static_cast
<
size_t
>
(
ctx
.
Attr
<
int
>
(
"num"
));
std
::
vector
<
int
>
sections
=
...
...
paddle/operators/squared_l2_distance_op.cc
浏览文件 @
36aeb30d
...
...
@@ -54,10 +54,10 @@ class SquaredL2DistanceOp : public framework::OperatorWithKernel {
"First dimension of target must be equal to input "
"or to 1."
);
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"sub_result"
)
ctx
.
Output
<
framework
::
Tensor
>
(
"sub_result"
)
->
Resize
({
x_dims
[
0
],
x
->
numel
()
/
x_dims
[
0
]});
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
)
->
Resize
({
x_dims
[
0
],
1
});
ctx
.
ShareLoD
(
"X"
,
"Out"
);
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
({
x_dims
[
0
],
1
});
ctx
.
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
};
...
...
@@ -104,10 +104,8 @@ class SquaredL2DistanceGradOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_EQ
(
out_dims
[
1
],
1
,
"Second dimension of output gradient "
"must be 1."
);
auto
*
x_grad
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
y_grad
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"Y"
));
auto
*
x_grad
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
y_grad
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
if
(
x_grad
)
x_grad
->
Resize
(
x_dims
);
if
(
y_grad
)
y_grad
->
Resize
(
y_dims
);
}
...
...
paddle/operators/sum_op.cc
浏览文件 @
36aeb30d
...
...
@@ -28,7 +28,7 @@ class SumOp : public framework::OperatorWithKernel {
"Output(Out) of SumOp should not be null."
);
auto
ins
=
ctx
.
MultiInput
<
framework
::
Tensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
);
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
int
N
=
ins
.
size
();
auto
in_dim
=
ins
[
0
]
->
dims
();
...
...
@@ -39,7 +39,7 @@ class SumOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE
(
in_dim
==
dim
,
"Input tensors must have same shape"
);
}
out
->
Resize
(
in_dim
);
ctx
.
ShareLoD
(
ctx
.
op
().
Inputs
(
"X"
)[
0
],
"Out"
);
ctx
.
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
};
...
...
@@ -65,7 +65,7 @@ class SumGradOp : public framework::OperatorWithKernel {
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
auto
outputs
=
ctx
.
MultiOutput
<
framework
::
LoD
Tensor
>
(
framework
::
GradVarName
(
"X"
));
ctx
.
MultiOutput
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
dims
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
))
->
dims
();
for
(
auto
output
:
outputs
)
{
output
->
Resize
(
dims
);
...
...
paddle/operators/top_k_op.cc
浏览文件 @
36aeb30d
...
...
@@ -40,8 +40,8 @@ class TopkOp : public framework::OperatorWithKernel {
framework
::
DDim
dims
=
input
->
dims
();
dims
[
dims
.
size
()
-
1
]
=
k
;
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
)
->
Resize
(
dims
);
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Indices"
)
->
Resize
(
dims
);
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
(
dims
);
ctx
.
Output
<
framework
::
Tensor
>
(
"Indices"
)
->
Resize
(
dims
);
}
};
...
...
paddle/operators/uniform_random_op.cc
浏览文件 @
36aeb30d
...
...
@@ -54,7 +54,7 @@ class UniformRandomOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE
(
Attr
<
float
>
(
"min"
)
<
Attr
<
float
>
(
"max"
),
"uniform_random's min must less then max"
);
auto
*
tensor
=
ctx
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
);
auto
*
tensor
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
auto
dims
=
Attr
<
std
::
vector
<
int
>>
(
"dims"
);
std
::
vector
<
int64_t
>
temp
;
temp
.
reserve
(
dims
.
size
());
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录