Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
NiuTrans.Tensor
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
8
Issues
8
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
NiuTrans
NiuTrans.Tensor
Commits
1f71eb10
Commit
1f71eb10
authored
Jul 21, 2019
by
张裕浩
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use vector buffer to accelerate reduce operation
parent
f98396a9
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
208 行增加
和
11 行删除
+208
-11
source/tensor/core/reduce/ReduceMax.cpp
+75
-11
source/tensor/core/reduce/ReduceSum.cpp
+0
-0
source/tensor/core/reduce/VectorBuffer.h
+133
-0
没有找到文件。
source/tensor/core/reduce/ReduceMax.cpp
查看文件 @
1f71eb10
...
@@ -21,6 +21,8 @@
...
@@ -21,6 +21,8 @@
#include "../../XTensor.h"
#include "../../XTensor.h"
#include "../../XName.h"
#include "../../XName.h"
#include "../../XBLAS.h"
#include "VectorBuffer.h"
#include "ReduceMax.h"
#include "ReduceMax.h"
#include "ReduceMax.cuh"
#include "ReduceMax.cuh"
...
@@ -76,18 +78,80 @@ void _ReduceMax(const XTensor * input, XTensor * output, int dim)
...
@@ -76,18 +78,80 @@ void _ReduceMax(const XTensor * input, XTensor * output, int dim)
}
}
blockSize
=
stride
*
strideNum
;
blockSize
=
stride
*
strideNum
;
for
(
int
k
=
0
;
k
<
blockNum
;
k
++
){
DTYPE
*
ip
=
(
DTYPE
*
)
input
->
data
+
blockSize
*
k
;
if
(
input
->
dimSizeRDI
[
0
]
%
(
4
*
32
/
sizeof
(
DTYPE
))
==
0
&&
input
->
dimSizeRDI
[
0
]
>=
32
){
DTYPE
*
op
=
(
DTYPE
*
)
output
->
data
+
stride
*
k
;
int
vecBufLength
=
32
/
sizeof
(
DTYPE
);
for
(
int
i
=
0
;
i
<
stride
;
i
++
){
DTYPE
max
=
FLOAT_MIN
;
if
(
dimRDI
==
0
){
DTYPE
*
ipe
=
ip
+
blockSize
;
//data is contiguous in dim 0
for
(
DTYPE
*
ipb
=
ip
+
i
;
ipb
<
ipe
;
ipb
+=
stride
){
for
(
int
i
=
0
;
i
<
blockNum
;
i
++
){
DTYPE
v
=
*
ipb
;
DTYPE
*
ip
=
(
DTYPE
*
)
input
->
data
+
blockSize
*
i
;
if
(
max
<
v
)
DTYPE
*
op
=
(
DTYPE
*
)
output
->
data
+
i
;
max
=
v
;
VectorBuffer
vecBuf
[
4
];
for
(
int
j
=
0
;
j
<
4
;
j
++
){
vecBuf
[
j
]
=
VectorBuffer
::
loadu
((
DTYPE
*
)(
ip
)
+
j
*
vecBufLength
);
}
for
(
int
j
=
1
;
j
<
strideNum
/
32
;
j
++
){
const
DTYPE
*
ptr
=
(
DTYPE
*
)(
ip
+
j
*
vecBufLength
);
vecBuf
[
0
]
=
vecBuf
[
0
].
maxData
(
VectorBuffer
::
loadu
(
ptr
+
0
*
vecBufLength
));
vecBuf
[
1
]
=
vecBuf
[
1
].
maxData
(
VectorBuffer
::
loadu
(
ptr
+
1
*
vecBufLength
));
vecBuf
[
2
]
=
vecBuf
[
2
].
maxData
(
VectorBuffer
::
loadu
(
ptr
+
2
*
vecBufLength
));
vecBuf
[
3
]
=
vecBuf
[
3
].
maxData
(
VectorBuffer
::
loadu
(
ptr
+
3
*
vecBufLength
));
}
vecBuf
[
0
]
=
vecBuf
[
0
].
maxData
(
vecBuf
[
1
]);
vecBuf
[
0
]
=
vecBuf
[
0
].
maxData
(
vecBuf
[
2
]);
vecBuf
[
0
]
=
vecBuf
[
0
].
maxData
(
vecBuf
[
3
]);
DTYPE
maxN
=
DTYPE_MIN
;
for
(
int
k
=
0
;
k
<
vecBufLength
;
k
++
){
maxN
=
MAX
(
maxN
,
vecBuf
[
0
][
k
]);
}
*
op
=
maxN
;
}
}
else
{
//data is separated
for
(
int
i
=
0
;
i
<
blockNum
;
i
++
){
for
(
int
j
=
0
;
j
<
input
->
dimSizeRDI
[
0
]
/
32
;
j
++
){
DTYPE
*
ip
=
(
DTYPE
*
)
input
->
data
+
blockSize
*
i
;
DTYPE
*
op
=
(
DTYPE
*
)
output
->
data
+
stride
*
i
;
VectorBuffer
vecBuf
[
4
];
for
(
int
k
=
0
;
k
<
4
;
k
++
){
vecBuf
[
k
]
=
VectorBuffer
::
loadu
((
DTYPE
*
)(
ip
)
+
(
j
*
4
+
k
)
*
32
/
sizeof
(
DTYPE
));
}
for
(
int
k
=
1
;
k
<
strideNum
;
k
++
){
DTYPE
*
ptr
=
ip
+
k
*
stride
+
(
j
*
4
)
*
vecBufLength
;
vecBuf
[
0
]
=
vecBuf
[
0
].
maxData
(
VectorBuffer
::
loadu
(
ptr
+
0
*
vecBufLength
));
vecBuf
[
1
]
=
vecBuf
[
1
].
maxData
(
VectorBuffer
::
loadu
(
ptr
+
1
*
vecBufLength
));
vecBuf
[
2
]
=
vecBuf
[
2
].
maxData
(
VectorBuffer
::
loadu
(
ptr
+
2
*
vecBufLength
));
vecBuf
[
3
]
=
vecBuf
[
3
].
maxData
(
VectorBuffer
::
loadu
(
ptr
+
3
*
vecBufLength
));
}
for
(
int
k
=
0
;
k
<
4
;
k
++
){
for
(
int
l
=
0
;
l
<
vecBufLength
;
l
++
)
*
(
op
+
j
*
32
+
8
*
k
+
l
)
=
vecBuf
[
k
][
l
];
}
}
}
}
}
//run vector buffer
else
{
for
(
int
k
=
0
;
k
<
blockNum
;
k
++
){
DTYPE
*
ip
=
(
DTYPE
*
)
input
->
data
+
blockSize
*
k
;
DTYPE
*
op
=
(
DTYPE
*
)
output
->
data
+
stride
*
k
;
for
(
int
i
=
0
;
i
<
stride
;
i
++
){
//#if defined(USE_BLAS)
// *(op + i) = *(ip + i + (int)(stride * IAMAX(strideNum, ip + i, stride)));
//#else
DTYPE
max
=
DTYPE_MIN
;
DTYPE
*
ipe
=
ip
+
blockSize
;
for
(
DTYPE
*
ipb
=
ip
+
i
;
ipb
<
ipe
;
ipb
+=
stride
){
DTYPE
v
=
*
ipb
;
if
(
max
<
v
)
max
=
v
;
}
*
(
op
+
i
)
=
max
;
//#endif
}
}
*
(
op
+
i
)
=
max
;
}
}
}
}
}
}
...
...
source/tensor/core/reduce/ReduceSum.cpp
查看文件 @
1f71eb10
差异被折叠。
点击展开。
source/tensor/core/reduce/VectorBuffer.h
0 → 100644
查看文件 @
1f71eb10
#include <cstring>
#include <cmath>
#include "../../XGlobal.h"
namespace
nts
{
class
VectorBuffer
{
private
:
DTYPE
values
[
32
/
sizeof
(
DTYPE
)]
=
{
0
};
public
:
static
int
size
()
{
return
32
/
sizeof
(
DTYPE
);
}
VectorBuffer
()
{}
VectorBuffer
(
DTYPE
val
)
{
for
(
int
i
=
0
;
i
!=
size
();
i
++
)
{
values
[
i
]
=
val
;
}
}
static
VectorBuffer
loadu
(
const
DTYPE
*
ptr
,
bool
isExp
=
false
,
DTYPE
power
=
(
DTYPE
)
1
.
0
F
,
DTYPE
*
bias
=
NULL
)
{
int
count
=
32
/
sizeof
(
DTYPE
);
VectorBuffer
vec
;
if
(
isExp
)
{
if
(
bias
==
NULL
)
{
if
(
power
==
(
DTYPE
)
1
.
0
)
{
for
(
int
i
=
0
;
i
!=
count
;
i
++
)
{
vec
.
values
[
i
]
=
(
DTYPE
)
exp
(
*
(
ptr
+
i
));
}
}
else
if
(
power
==
(
DTYPE
)
2
.
0
)
{
for
(
int
i
=
0
;
i
!=
count
;
i
++
)
{
vec
.
values
[
i
]
=
(
DTYPE
)
exp
((
*
(
ptr
+
i
))
*
(
*
(
ptr
+
i
)));
}
}
else
if
(
power
==
(
DTYPE
)
0
.
5
)
{
for
(
int
i
=
0
;
i
!=
count
;
i
++
)
{
vec
.
values
[
i
]
=
(
DTYPE
)
exp
(
sqrt
(
*
(
ptr
+
i
)));
}
}
else
{
for
(
int
i
=
0
;
i
!=
count
;
i
++
)
{
vec
.
values
[
i
]
=
(
DTYPE
)
exp
(
pow
(
*
(
ptr
+
i
),
power
));
}
}
}
/*is bias == NULL*/
else
{
if
(
power
==
(
DTYPE
)
1
.
0
)
{
for
(
int
i
=
0
;
i
!=
count
;
i
++
)
{
vec
.
values
[
i
]
=
(
DTYPE
)
exp
(
*
(
ptr
+
i
)
-
bias
[
i
]);
}
}
else
if
(
power
==
(
DTYPE
)
2
.
0
)
{
for
(
int
i
=
0
;
i
!=
count
;
i
++
)
{
DTYPE
value
=
*
(
ptr
+
i
)
-
bias
[
i
];
vec
.
values
[
i
]
=
(
DTYPE
)
exp
(
value
*
value
);
}
}
else
if
(
power
==
(
DTYPE
)
0
.
5
)
{
for
(
int
i
=
0
;
i
!=
count
;
i
++
)
{
vec
.
values
[
i
]
=
(
DTYPE
)
exp
(
sqrt
(
*
(
ptr
+
i
)
-
bias
[
i
]));
}
}
else
{
for
(
int
i
=
0
;
i
!=
count
;
i
++
)
{
vec
.
values
[
i
]
=
(
DTYPE
)
exp
(
pow
(
*
(
ptr
+
i
)
-
bias
[
i
],
power
));
}
}
}
}
//isExp
else
{
if
(
bias
==
NULL
)
{
if
(
power
==
(
DTYPE
)
1
.
0
)
{
memcpy
(
vec
.
values
,
ptr
,
count
*
sizeof
(
DTYPE
));
}
else
if
(
power
==
(
DTYPE
)
2
.
0
)
{
for
(
int
i
=
0
;
i
!=
count
;
i
++
)
{
vec
.
values
[
i
]
=
(
*
(
ptr
+
i
))
*
(
*
(
ptr
+
i
));
}
}
else
if
(
power
==
(
DTYPE
)
0
.
5
)
{
for
(
int
i
=
0
;
i
!=
count
;
i
++
)
{
vec
.
values
[
i
]
=
(
DTYPE
)
sqrt
(
*
(
ptr
+
i
));
}
}
else
{
for
(
int
i
=
0
;
i
!=
count
;
i
++
)
{
vec
.
values
[
i
]
=
(
DTYPE
)
pow
(
*
(
ptr
+
i
),
power
);
}
}
}
// if bias == NULL
else
{
if
(
power
==
(
DTYPE
)
1
.
0
)
{
for
(
int
i
=
0
;
i
!=
count
;
i
++
)
{
vec
.
values
[
i
]
=
*
(
ptr
+
i
)
-
bias
[
i
];
}
}
else
if
(
power
==
(
DTYPE
)
2
.
0
)
{
for
(
int
i
=
0
;
i
!=
count
;
i
++
)
{
DTYPE
value
=
*
(
ptr
+
i
)
-
bias
[
i
];
vec
.
values
[
i
]
=
value
*
value
;
}
}
else
if
(
power
==
(
DTYPE
)
0
.
5
)
{
for
(
int
i
=
0
;
i
!=
count
;
i
++
)
{
vec
.
values
[
i
]
=
(
DTYPE
)
sqrt
(
*
(
ptr
+
i
)
-
bias
[
i
]);
}
}
else
{
for
(
int
i
=
0
;
i
!=
count
;
i
++
)
{
vec
.
values
[
i
]
=
(
DTYPE
)
pow
(
*
(
ptr
+
i
)
-
bias
[
i
],
power
);
}
}
}
}
return
vec
;
}
const
DTYPE
&
operator
[](
int
idx
)
const
{
return
values
[
idx
];
}
inline
VectorBuffer
operator
+
(
const
VectorBuffer
&
a
)
{
for
(
int
i
=
0
;
i
!=
a
.
size
();
i
++
)
{
this
->
values
[
i
]
=
a
[
i
]
+
this
->
values
[
i
];
}
return
*
this
;
}
inline
VectorBuffer
maxData
(
const
VectorBuffer
&
a
)
{
for
(
int
i
=
0
;
i
!=
a
.
size
();
i
++
)
{
this
->
values
[
i
]
=
MAX
(
a
[
i
],
this
->
values
[
i
]);
}
return
*
this
;
}
};
}
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论