Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
d71396bf
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
694
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,发现更多精彩内容 >>
提交
d71396bf
编写于
9月 05, 2017
作者:
F
fengjiayi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add global function `flatten_to_2d()`
上级
69fbc542
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
33 addition
and
34 deletion
+33
-34
paddle/framework/ddim.cc
paddle/framework/ddim.cc
+8
-6
paddle/framework/ddim.h
paddle/framework/ddim.h
+2
-2
paddle/framework/eigen.h
paddle/framework/eigen.h
+2
-5
paddle/framework/tensor_impl.h
paddle/framework/tensor_impl.h
+1
-5
paddle/operators/mul_op.cc
paddle/operators/mul_op.cc
+20
-16
未找到文件。
paddle/framework/ddim.cc
浏览文件 @
d71396bf
...
...
@@ -247,12 +247,6 @@ ssize_t product(const DDim& ddim) {
return
boost
::
apply_visitor
(
visitor
,
ddim
);
}
ssize_t
product
(
const
DDim
&
ddim
,
int
begin
,
int
end
)
{
ProductVisitor
visitor
;
DDim
sliced_ddim
=
slice_ddim
(
ddim
,
begin
,
end
);
return
boost
::
apply_visitor
(
visitor
,
sliced_ddim
);
}
/// \cond HIDDEN
struct
ArityVisitor
:
boost
::
static_visitor
<
int
>
{
...
...
@@ -289,5 +283,13 @@ std::ostream& operator<<(std::ostream& os, const DDim& ddim) {
DDim
::
DDim
(
std
::
initializer_list
<
int
>
init_list
)
{
*
this
=
make_ddim
(
init_list
);
}
DDim
flatten_to_2d
(
const
DDim
&
src
,
int
num_row_dims
)
{
int
rank
=
src
.
size
();
return
make_ddim
(
{
static_cast
<
int
>
(
product
(
slice_ddim
(
src
,
0
,
rank
-
num_row_dims
))),
static_cast
<
int
>
(
product
(
slice_ddim
(
src
,
rank
-
num_row_dims
,
rank
)))});
}
}
// namespace framework
}
// namespace paddle
paddle/framework/ddim.h
浏览文件 @
d71396bf
...
...
@@ -96,8 +96,6 @@ std::vector<int> vectorize(const DDim& ddim);
ssize_t
product
(
const
DDim
&
ddim
);
ssize_t
product
(
const
DDim
&
ddim
,
int
begin
,
int
end
);
/**
* \brief Slice a ddim
*
...
...
@@ -117,6 +115,8 @@ int arity(const DDim& ddim);
std
::
ostream
&
operator
<<
(
std
::
ostream
&
,
const
DDim
&
);
DDim
flatten_to_2d
(
const
DDim
&
src
,
int
num_row_dims
);
}
// namespace framework
}
// namespace paddle
...
...
paddle/framework/eigen.h
浏览文件 @
d71396bf
...
...
@@ -68,11 +68,8 @@ struct EigenMatrix : public EigenTensor<T, 2, MajorType, IndexType> {
int
rank
=
tensor
.
dims_
.
size
();
PADDLE_ENFORCE
(
num_row_dims
>
0
&&
num_row_dims
<
rank
,
"`num_row_dims` must be between (0, rank_of_tensor)."
);
return
EigenMatrix
::
From
(
tensor
,
make_ddim
({
static_cast
<
int
>
(
product
(
tensor
.
dims_
,
0
,
rank
-
num_row_dims
)),
static_cast
<
int
>
(
product
(
tensor
.
dims_
,
rank
-
num_row_dims
,
rank
))}));
return
EigenMatrix
::
From
(
tensor
,
flatten_to_2d
(
tensor
.
dims
(),
num_row_dims
));
}
};
...
...
paddle/framework/tensor_impl.h
浏览文件 @
d71396bf
...
...
@@ -152,11 +152,7 @@ template <typename T>
inline
Tensor
FlattenToMatrix
(
const
Tensor
&
src
,
int
num_row_dims
)
{
Tensor
res
;
res
.
ShareDataWith
<
T
>
(
src
);
DDim
src_dim
=
src
.
dims
();
int
rank
=
src_dim
.
size
();
res
.
Resize
(
make_ddim
(
{
static_cast
<
int
>
(
product
(
src_dim
,
0
,
rank
-
num_row_dims
)),
static_cast
<
int
>
(
product
(
src_dim
,
rank
-
num_row_dims
,
rank
))}));
res
.
Resize
(
flatten_to_2d
(
src
.
dims
(),
num_row_dims
));
return
res
;
}
...
...
paddle/operators/mul_op.cc
浏览文件 @
d71396bf
...
...
@@ -25,27 +25,27 @@ class MulOp : public framework::OperatorWithKernel {
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
auto
x_dim
=
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
();
auto
y_dim
=
ctx
.
Input
<
Tensor
>
(
"Y"
)
->
dims
();
auto
x_dim
s
=
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
();
auto
y_dim
s
=
ctx
.
Input
<
Tensor
>
(
"Y"
)
->
dims
();
int
x_num_row_dims
=
GetAttr
<
int
>
(
"x_num_row_dims"
);
int
y_num_row_dims
=
GetAttr
<
int
>
(
"y_num_row_dims"
);
PADDLE_ENFORCE
(
x_dim
.
size
()
>
x_num_row_dims
,
PADDLE_ENFORCE
(
x_dim
s
.
size
()
>
x_num_row_dims
,
"The rank of input tensor X(%s) should be larger than "
"`mul_op`'s `x_num_row_dims`."
,
ctx
.
op
().
Input
(
"X"
));
PADDLE_ENFORCE
(
y_dim
.
size
()
>
y_num_row_dims
,
PADDLE_ENFORCE
(
y_dim
s
.
size
()
>
y_num_row_dims
,
"The rank of input tensor Y(%s) should be larger than "
"`mul_op`'s `y_num_row_dims`."
,
ctx
.
op
().
Input
(
"Y"
));
auto
x_mat_dims
=
framework
::
flatten_to_2d
(
x_dims
,
x_num_row_dims
);
auto
y_mat_dims
=
framework
::
flatten_to_2d
(
y_dims
,
y_num_row_dims
);
PADDLE_ENFORCE_EQ
(
product
(
x_dim
,
x_dim
.
size
()
-
x_num_row_dims
,
x_dim
.
size
()),
product
(
y_dim
,
0
,
y_dim
.
size
()
-
y_num_row_dims
),
x_mat_dims
[
1
],
y_mat_dims
[
0
],
"First matrix's width must be equal with second matrix's height."
);
ctx
.
Output
<
Tensor
>
(
"Out"
)
->
Resize
(
{
static_cast
<
int
>
(
product
(
x_dim
,
0
,
x_dim
.
size
()
-
x_num_row_dims
)),
static_cast
<
int
>
(
product
(
y_dim
,
y_dim
.
size
()
-
y_num_row_dims
,
y_dim
.
size
()))});
ctx
.
Output
<
Tensor
>
(
"Out"
)
->
Resize
({
x_mat_dims
[
0
],
y_mat_dims
[
1
]});
}
};
...
...
@@ -96,14 +96,18 @@ class MulOpGrad : public framework::OperatorWithKernel {
auto
out_dims
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
))
->
dims
();
auto
*
x_grad
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
y_grad
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
PADDLE_ENFORCE
(
product
(
x_dims
,
0
,
x_dims
.
size
()
-
GetAttr
<
int
>
(
"x_num_row_dims"
))
==
out_dims
[
0
],
auto
x_mat_dims
=
framework
::
flatten_to_2d
(
x_dims
,
GetAttr
<
int
>
(
"x_num_row_dims"
));
auto
y_mat_dims
=
framework
::
flatten_to_2d
(
y_dims
,
GetAttr
<
int
>
(
"y_num_row_dims"
));
PADDLE_ENFORCE_EQ
(
x_mat_dims
[
0
],
out_dims
[
0
],
"The first dimension of Out@GRAD must equal to the first dimension of "
"the first operand."
);
PADDLE_ENFORCE
(
product
(
y_dims
,
y_dims
.
size
()
-
GetAttr
<
int
>
(
"y_num_row_dims"
),
y_dims
.
size
())
==
out_dims
[
1
],
PADDLE_ENFORCE_EQ
(
y_mat_dims
[
1
],
out_dims
[
1
],
"The second dimension of Out@GRAD must equal to the second "
"dimension of the second operand."
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录