Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
兽拳
Paddle
提交
a33e9da9
P
Paddle
项目概览
兽拳
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a33e9da9
编写于
7月 17, 2017
作者:
F
fengjiayi
提交者:
GitHub
7月 17, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2900 from Canpio/dev_ddim_update
Refactor DDim's product() and add slice_ddim()
上级
80a26a63
778a1a9f
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
160 addition
and
86 deletion
+160
-86
paddle/framework/ddim.cc
paddle/framework/ddim.cc
+49
-5
paddle/framework/ddim.h
paddle/framework/ddim.h
+9
-0
paddle/framework/ddim_test.cc
paddle/framework/ddim_test.cc
+20
-0
paddle/framework/dim_test.cu
paddle/framework/dim_test.cu
+82
-81
未找到文件。
paddle/framework/ddim.cc
浏览文件 @
a33e9da9
...
...
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/framework/ddim.h"
#include "paddle/framework/enforce.h"
namespace
paddle
{
namespace
framework
{
...
...
@@ -192,13 +193,56 @@ std::vector<int> vectorize(const DDim& ddim) {
return
result
;
}
struct
ProductVisitor
:
public
boost
::
static_visitor
<
ssize_t
>
{
template
<
int
D
>
ssize_t
operator
()(
const
Dim
<
D
>&
dim
)
{
return
product
(
dim
);
}
};
ssize_t
product
(
const
DDim
&
ddim
)
{
ssize_t
result
=
1
;
std
::
vector
<
int
>
v
=
vectorize
(
ddim
);
for
(
auto
i
:
v
)
{
result
*=
i
;
ProductVisitor
visitor
;
return
boost
::
apply_visitor
(
visitor
,
ddim
);
}
struct
SliceVectorizeVisitor
:
public
boost
::
static_visitor
<>
{
std
::
vector
<
int
>&
vector
;
int
begin
;
int
end
;
SliceVectorizeVisitor
(
std
::
vector
<
int
>&
v
,
int
b
,
int
e
)
:
vector
(
v
),
begin
(
b
),
end
(
e
)
{
PADDLE_ENFORCE
(
begin
<
end
,
"Begin index must be less than end index in ddim slice."
);
PADDLE_ENFORCE
(
begin
>=
0
,
"Begin index can't be less than zero in ddim slice."
);
}
return
result
;
template
<
int
S
>
void
operator
()(
const
Dim
<
S
>&
dim
)
{
if
(
begin
==
0
)
{
vector
.
push_back
(
dim
.
head
);
}
else
{
--
begin
;
}
--
end
;
if
(
end
>
0
)
{
this
->
operator
()(
dim
.
tail
);
}
}
void
operator
()(
const
Dim
<
1
>&
dim
)
{
PADDLE_ENFORCE
(
end
==
1
,
"End index in ddim slice is out of bound."
);
vector
.
push_back
(
dim
.
head
);
}
};
DDim
slice_ddim
(
const
DDim
&
dim
,
int
begin
,
int
end
)
{
std
::
vector
<
int
>
vec
;
vec
.
reserve
(
end
-
begin
);
SliceVectorizeVisitor
visitor
(
vec
,
begin
,
end
);
boost
::
apply_visitor
(
visitor
,
dim
);
return
make_ddim
(
vec
);
}
/// \cond HIDDEN
...
...
paddle/framework/ddim.h
浏览文件 @
a33e9da9
...
...
@@ -81,6 +81,15 @@ std::vector<int> vectorize(const DDim& ddim);
ssize_t
product
(
const
DDim
&
ddim
);
/**
* \brief Slice a ddim
*
* Slice dim with [begin, end).
* e.g. DDim d = make_ddim({1,2,3,4,5});
* slice_ddim(d, 1, 3); ====> {2,3}
*/
DDim
slice_ddim
(
const
DDim
&
dim
,
int
begin
,
int
end
);
/**
* \brief What is the length of this dimension?
*
...
...
paddle/framework/ddim_test.cc
浏览文件 @
a33e9da9
...
...
@@ -52,6 +52,26 @@ TEST(DDim, Equality) {
// product of a DDim
EXPECT_EQ
(
paddle
::
framework
::
product
(
vddim
),
45
);
EXPECT_EQ
(
paddle
::
framework
::
product
(
paddle
::
framework
::
make_ddim
({
3
,
2
,
5
,
3
})),
90
);
// slice a DDim
paddle
::
framework
::
DDim
ddim2
=
paddle
::
framework
::
make_ddim
({
1
,
2
,
3
,
4
,
5
,
6
});
paddle
::
framework
::
DDim
ss
=
paddle
::
framework
::
slice_ddim
(
ddim2
,
2
,
5
);
EXPECT_EQ
(
arity
(
ss
),
3
);
EXPECT_EQ
(
ss
[
0
],
3
);
EXPECT_EQ
(
ss
[
1
],
4
);
EXPECT_EQ
(
ss
[
2
],
5
);
paddle
::
framework
::
DDim
ss2
=
paddle
::
framework
::
slice_ddim
(
ddim2
,
0
,
6
);
EXPECT_EQ
(
arity
(
ss2
),
6
);
EXPECT_EQ
(
ss2
[
0
],
1
);
EXPECT_EQ
(
ss2
[
1
],
2
);
EXPECT_EQ
(
ss2
[
2
],
3
);
EXPECT_EQ
(
ss2
[
3
],
4
);
EXPECT_EQ
(
ss2
[
4
],
5
);
EXPECT_EQ
(
ss2
[
5
],
6
);
}
TEST
(
DDim
,
Print
)
{
...
...
paddle/framework/dim_test.cu
浏览文件 @
a33e9da9
#include <thrust/device_vector.h>
#include <sstream>
#include "paddle/framework/dim.h"
#include "gtest/gtest.h"
#include "paddle/framework/dim.h"
__global__
void
test
(
paddle
::
framework
::
Dim
<
2
>*
o
)
{
o
[
0
]
=
paddle
::
framework
::
make_dim
(
5
,
6
);
o
[
0
]
=
paddle
::
framework
::
make_dim
(
5
,
6
);
}
__global__
void
dyn_idx_gpu
(
int
*
o
)
{
auto
d
=
paddle
::
framework
::
make_dim
(
5
,
6
);
o
[
0
]
=
d
[
1
];
auto
d
=
paddle
::
framework
::
make_dim
(
5
,
6
);
o
[
0
]
=
d
[
1
];
}
TEST
(
Dim
,
Equality
)
{
// construct a Dim on the CPU
auto
a
=
paddle
::
framework
::
make_dim
(
3
,
4
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
0
>
(
a
),
3
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
1
>
(
a
),
4
);
// construct a Dim on the GPU
thrust
::
device_vector
<
paddle
::
framework
::
Dim
<
2
>>
t
(
2
);
test
<<<
1
,
1
>>>
(
thrust
::
raw_pointer_cast
(
t
.
data
()));
a
=
t
[
0
];
EXPECT_EQ
(
paddle
::
framework
::
get
<
0
>
(
a
),
5
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
1
>
(
a
),
6
);
// linearization
auto
b
=
paddle
::
framework
::
make_dim
(
7
,
8
);
EXPECT_EQ
(
paddle
::
framework
::
linearize
(
a
,
b
),
83
);
// product
EXPECT_EQ
(
paddle
::
framework
::
product
(
a
),
30
);
// mutate a Dim
paddle
::
framework
::
get
<
1
>
(
b
)
=
10
;
EXPECT_EQ
(
paddle
::
framework
::
get
<
0
>
(
b
),
7
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
1
>
(
b
),
10
);
// dynamic access
paddle
::
framework
::
get
(
b
,
0
)
=
8
;
b
[
1
]
=
11
;
EXPECT_EQ
(
paddle
::
framework
::
get
<
0
>
(
b
),
8
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
1
>
(
b
),
11
);
EXPECT_EQ
(
paddle
::
framework
::
get
(
b
,
0
),
8
);
EXPECT_EQ
(
b
[
1
],
11
);
// dynamic access on GPU
thrust
::
device_vector
<
int
>
r
(
1
);
dyn_idx_gpu
<<<
1
,
1
>>>
(
thrust
::
raw_pointer_cast
(
r
.
data
()));
int
res
=
r
[
0
];
EXPECT_EQ
(
res
,
6
);
// ex_prefix_mul
paddle
::
framework
::
Dim
<
3
>
c
=
paddle
::
framework
::
ex_prefix_mul
(
paddle
::
framework
::
Dim
<
3
>
(
3
,
4
,
5
));
EXPECT_EQ
(
paddle
::
framework
::
get
<
0
>
(
c
),
1
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
1
>
(
c
),
3
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
2
>
(
c
),
12
);
// generate from an index
auto
size
=
paddle
::
framework
::
make_dim
(
4
,
5
,
2
);
c
=
paddle
::
framework
::
Dim
<
3
>
(
14
,
size
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
0
>
(
c
),
2
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
1
>
(
c
),
3
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
2
>
(
c
),
0
);
c
=
paddle
::
framework
::
Dim
<
3
>
(
25
,
size
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
0
>
(
c
),
1
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
1
>
(
c
),
1
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
2
>
(
c
),
1
);
// construct a Dim on the CPU
auto
a
=
paddle
::
framework
::
make_dim
(
3
,
4
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
0
>
(
a
),
3
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
1
>
(
a
),
4
);
// construct a Dim on the GPU
thrust
::
device_vector
<
paddle
::
framework
::
Dim
<
2
>>
t
(
2
);
test
<<<
1
,
1
>>>
(
thrust
::
raw_pointer_cast
(
t
.
data
()));
a
=
t
[
0
];
EXPECT_EQ
(
paddle
::
framework
::
get
<
0
>
(
a
),
5
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
1
>
(
a
),
6
);
// linearization
auto
b
=
paddle
::
framework
::
make_dim
(
7
,
8
);
EXPECT_EQ
(
paddle
::
framework
::
linearize
(
a
,
b
),
83
);
// product
EXPECT_EQ
(
paddle
::
framework
::
product
(
a
),
30
);
// mutate a Dim
paddle
::
framework
::
get
<
1
>
(
b
)
=
10
;
EXPECT_EQ
(
paddle
::
framework
::
get
<
0
>
(
b
),
7
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
1
>
(
b
),
10
);
// dynamic access
paddle
::
framework
::
get
(
b
,
0
)
=
8
;
b
[
1
]
=
11
;
EXPECT_EQ
(
paddle
::
framework
::
get
<
0
>
(
b
),
8
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
1
>
(
b
),
11
);
EXPECT_EQ
(
paddle
::
framework
::
get
(
b
,
0
),
8
);
EXPECT_EQ
(
b
[
1
],
11
);
// dynamic access on GPU
thrust
::
device_vector
<
int
>
r
(
1
);
dyn_idx_gpu
<<<
1
,
1
>>>
(
thrust
::
raw_pointer_cast
(
r
.
data
()));
int
res
=
r
[
0
];
EXPECT_EQ
(
res
,
6
);
// ex_prefix_mul
paddle
::
framework
::
Dim
<
3
>
c
=
paddle
::
framework
::
ex_prefix_mul
(
paddle
::
framework
::
Dim
<
3
>
(
3
,
4
,
5
));
EXPECT_EQ
(
paddle
::
framework
::
get
<
0
>
(
c
),
1
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
1
>
(
c
),
3
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
2
>
(
c
),
12
);
// generate from an index
auto
size
=
paddle
::
framework
::
make_dim
(
4
,
5
,
2
);
c
=
paddle
::
framework
::
Dim
<
3
>
(
14
,
size
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
0
>
(
c
),
2
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
1
>
(
c
),
3
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
2
>
(
c
),
0
);
c
=
paddle
::
framework
::
Dim
<
3
>
(
25
,
size
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
0
>
(
c
),
1
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
1
>
(
c
),
1
);
EXPECT_EQ
(
paddle
::
framework
::
get
<
2
>
(
c
),
1
);
}
TEST
(
Dim
,
Bool
)
{
auto
a
=
paddle
::
framework
::
make_dim
(
3
,
4
);
auto
b
=
paddle
::
framework
::
make_dim
(
5
,
6
);
auto
c
=
paddle
::
framework
::
make_dim
(
3
,
4
);
// in_bounds check
EXPECT_TRUE
(
paddle
::
framework
::
contained
(
a
,
b
));
EXPECT_FALSE
(
paddle
::
framework
::
contained
(
b
,
a
));
// comparison
EXPECT_TRUE
(
a
==
a
);
EXPECT_FALSE
(
a
==
b
);
EXPECT_TRUE
(
a
==
c
);
auto
a
=
paddle
::
framework
::
make_dim
(
3
,
4
);
auto
b
=
paddle
::
framework
::
make_dim
(
5
,
6
);
auto
c
=
paddle
::
framework
::
make_dim
(
3
,
4
);
// in_bounds check
EXPECT_TRUE
(
paddle
::
framework
::
contained
(
a
,
b
));
EXPECT_FALSE
(
paddle
::
framework
::
contained
(
b
,
a
));
// comparison
EXPECT_TRUE
(
a
==
a
);
EXPECT_FALSE
(
a
==
b
);
EXPECT_TRUE
(
a
==
c
);
}
TEST
(
Dim
,
Print
)
{
{
std
::
stringstream
ss
;
auto
a
=
paddle
::
framework
::
make_dim
(
2
,
3
);
ss
<<
a
;
EXPECT_EQ
(
ss
.
str
(),
"2, 3"
);
}
{
std
::
stringstream
ss
;
ss
<<
paddle
::
framework
::
make_dim
(
8
);
EXPECT_EQ
(
ss
.
str
(),
"8"
);
}
{
std
::
stringstream
ss
;
auto
a
=
paddle
::
framework
::
make_dim
(
2
,
3
);
ss
<<
a
;
EXPECT_EQ
(
ss
.
str
(),
"2, 3"
);
}
{
std
::
stringstream
ss
;
ss
<<
paddle
::
framework
::
make_dim
(
8
);
EXPECT_EQ
(
ss
.
str
(),
"8"
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录