Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
NiuTrans.Tensor
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
杨迪
NiuTrans.Tensor
Commits
eb325d83
Commit
eb325d83
authored
Feb 11, 2020
by
liyinqiao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support scalar.
Support scalar tensor for Sub operation.
parent
3d6f1230
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
68 行增加
和
157 行删除
+68
-157
source/tensor/core/arithmetic/Sub.cpp
+60
-152
source/tensor/core/arithmetic/Sum.cpp
+5
-5
source/tensor/core/arithmetic/Sum.h
+3
-0
没有找到文件。
source/tensor/core/arithmetic/Sub.cpp
查看文件 @
eb325d83
...
...
@@ -16,16 +16,15 @@
*/
/*
* $Created by:
Xu Chen (email: hello_master1954@163.com) 2018-08-0
1
* $Created by:
Li Yinqiao (email: li.yin.qiao.2012@hotmail.com) 2020-02-1
1
*/
#include "../../XTensor.h"
#include "../../XName.h"
#include "../../XUtility.h"
#include "../shape/IsSameShaped.h"
#include "Sum.h"
#include "SumDim.h"
#include "../math/ScaleAndShift.h"
#include "Sub.h"
#include "Sub.cuh"
#include "SubDim.h"
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
...
...
@@ -39,80 +38,7 @@ tensor subtraction c = a - b * \beta
*/
void
_Sub
(
const
XTensor
*
a
,
const
XTensor
*
b
,
XTensor
*
c
,
DTYPE
beta
)
{
CheckNTErrors
(
a
&&
b
&&
c
,
"Empty tensor input!"
);
CheckNTErrors
(
a
->
unitNum
==
b
->
unitNum
&&
a
->
unitNum
==
c
->
unitNum
,
"Unmatched tensors in addition!"
);
CheckNTErrors
(
a
->
dataType
==
b
->
dataType
&&
a
->
dataType
==
c
->
dataType
,
"Unmatched tensors in addition!"
);
CheckDev
(
a
->
devID
,
b
->
devID
);
if
(
a
->
devID
>=
0
||
b
->
devID
>=
0
||
c
->
devID
>=
0
)
{
#ifdef USE_CUDA
if
(
a
==
c
)
{
int
P2PAccesible
=
0
;
#ifdef CUDA_UVA
cudaDeviceCanAccessPeer
(
&
P2PAccesible
,
a
->
devID
,
b
->
devID
);
#endif
if
((
a
->
devID
<
0
&&
b
->
devID
>=
0
)
||
(
a
->
devID
>=
0
&&
b
->
devID
<
0
)
||
(
a
->
devID
>=
0
&&
b
->
devID
>=
0
&&
a
->
devID
!=
b
->
devID
&&
!
P2PAccesible
))
{
ShowNTErrors
(
"Cannot run this method on multiple devices simultaneously!"
);
}
else
_CudaSub
(
a
,
b
,
c
,
beta
);
}
else
_CudaSub
(
a
,
b
,
c
,
beta
);
#endif
}
else
{
if
(
!
a
->
isSparse
&&
!
b
->
isSparse
)
{
CheckNTErrors
(
!
c
->
isSparse
,
"Illegal use of sparse tensor in addition!"
);
if
(
a
->
dataType
==
DEFAULT_DTYPE
&&
b
->
dataType
==
DEFAULT_DTYPE
&&
c
->
dataType
==
DEFAULT_DTYPE
)
{
DTYPE
*
ap
=
(
DTYPE
*
)
a
->
data
;
DTYPE
*
bp
=
(
DTYPE
*
)
b
->
data
;
DTYPE
*
cp
=
(
DTYPE
*
)
c
->
data
;
/* unrolling */
int
num
=
a
->
unitNum
;
if
(
num
%
4
==
0
)
{
for
(
int
i
=
0
;
i
<
num
;
i
+=
4
)
{
cp
[
i
]
=
ap
[
i
]
-
bp
[
i
]
*
beta
;
cp
[
i
+
1
]
=
ap
[
i
+
1
]
-
bp
[
i
+
1
]
*
beta
;
cp
[
i
+
2
]
=
ap
[
i
+
2
]
-
bp
[
i
+
2
]
*
beta
;
cp
[
i
+
3
]
=
ap
[
i
+
3
]
-
bp
[
i
+
3
]
*
beta
;
}
}
else
if
(
num
%
2
==
0
)
{
for
(
int
i
=
0
;
i
<
num
;
i
+=
2
)
{
cp
[
i
]
=
ap
[
i
]
-
bp
[
i
]
*
beta
;
cp
[
i
+
1
]
=
ap
[
i
+
1
]
-
bp
[
i
+
1
]
*
beta
;
}
}
else
{
for
(
int
i
=
0
;
i
<
num
;
i
++
)
{
cp
[
i
]
=
ap
[
i
]
-
bp
[
i
]
*
beta
;
}
}
}
else
{
// TODO!!
ShowNTErrors
(
"TODO!"
);
}
}
else
{
// TODO!!
ShowNTErrors
(
"TODO!"
);
}
}
_Sum
(
a
,
b
,
c
,
-
beta
);
}
/*
...
...
@@ -140,35 +66,6 @@ void SubMe(XTensor& a, const XTensor& b, DTYPE beta)
{
_Sub
(
&
a
,
&
b
,
&
a
,
beta
);
}
/*
return a dimension if the subtraction is performed as SubDim (in more details in SubDim.h)
>> a - a tensor
>> b - another tensor for subtraction
*/
int
GetSubDimIndex
(
const
XTensor
&
a
,
const
XTensor
&
b
)
{
if
(
a
.
order
<
b
.
order
)
return
-
1
;
if
(
IsSameShaped
(
a
,
b
))
return
-
1
;
int
hitCount
=
0
;
int
hitDim
=
-
1
;
for
(
int
i
=
0
;
i
<
b
.
order
;
i
++
){
if
(
b
.
dimSize
[
b
.
order
-
1
-
i
]
==
1
)
continue
;
else
if
(
b
.
dimSize
[
b
.
order
-
1
-
i
]
==
a
.
dimSize
[
a
.
order
-
1
-
i
]){
hitCount
++
;
hitDim
=
a
.
order
-
b
.
order
+
i
;
}
}
if
(
hitCount
==
1
)
return
hitDim
;
else
return
-
1
;
}
/*
tensor subtraction c = a - b * \beta (return an XTensor structure)
...
...
@@ -184,33 +81,38 @@ XTensor Sub(const XTensor &a, const XTensor &b, DTYPE beta)
XTensor
c
(
&
a
);
c
.
SetTMPFlag
();
int
n
=
GetSubDimIndex
(
a
,
b
);
if
(
b
.
order
==
0
){
DTYPE
shift
=
-
(
b
.
Get0D
()
*
beta
);
ScaleAndShift
(
a
,
c
,
1.0
F
,
shift
);
}
else
{
int
n
=
GetBroadcastDimIndex
(
a
,
b
);
if
(
n
==
-
1
){
/* call _Sub function */
_Sub
(
&
a
,
&
b
,
&
c
,
beta
);
if
(
n
==
-
1
){
/* call _Sub function */
_Sub
(
&
a
,
&
b
,
&
c
,
beta
);
/* tensor connections */
if
(
a
.
enableGrad
&&
b
.
enableGrad
)
{
XLink
::
MakeLink
(
&
a
,
&
b
,
&
c
,
MATH_SUB
);
XLink
::
AddParamToHead
(
&
c
,
beta
);
/* tensor connections */
if
(
a
.
enableGrad
&&
b
.
enableGrad
)
{
XLink
::
MakeLink
(
&
a
,
&
b
,
&
c
,
MATH_SUB
);
XLink
::
AddParamToHead
(
&
c
,
beta
);
}
}
}
else
if
(
n
>=
0
&&
n
<
a
.
order
){
/* call _SubDim function */
_SubDim
(
&
a
,
&
b
,
&
c
,
n
,
beta
);
/* tensor connections */
if
(
a
.
enableGrad
&&
b
.
enableGrad
)
{
XLink
::
MakeLink
(
&
a
,
&
b
,
&
c
,
MATH_SUBDIM
);
XLink
::
AddParamToHeadInt
(
&
c
,
n
);
XLink
::
AddParamToHead
(
&
c
,
beta
);
else
if
(
n
>=
0
&&
n
<
a
.
order
){
/* call _SumDim function to do the SubDim operation */
_SumDim
(
&
a
,
&
b
,
&
c
,
n
,
-
beta
);
/* tensor connections */
if
(
a
.
enableGrad
&&
b
.
enableGrad
)
{
XLink
::
MakeLink
(
&
a
,
&
b
,
&
c
,
MATH_SUBDIM
);
XLink
::
AddParamToHeadInt
(
&
c
,
n
);
XLink
::
AddParamToHead
(
&
c
,
beta
);
}
}
else
{
ShowNTErrors
(
"Something is wrong!"
);
}
}
else
{
ShowNTErrors
(
"Something is wrong!"
);
}
return
c
;
}
...
...
@@ -228,31 +130,37 @@ void Sub(const XTensor &a, const XTensor &b, XTensor &c, DTYPE beta)
InitTensorV2
(
&
c
,
&
a
);
}
int
n
=
GetSubDimIndex
(
a
,
b
);
if
(
b
.
order
==
0
){
DTYPE
shift
=
-
(
b
.
Get0D
()
*
beta
);
ScaleAndShift
(
a
,
c
,
1.0
F
,
shift
);
}
else
{
int
n
=
GetBroadcastDimIndex
(
a
,
b
);
if
(
n
==
-
1
)
{
/* call _Sub function */
_Sub
(
&
a
,
&
b
,
&
c
,
beta
);
if
(
n
==
-
1
)
{
/* call _Sub function */
_Sub
(
&
a
,
&
b
,
&
c
,
beta
);
if
(
a
.
enableGrad
&&
b
.
enableGrad
)
{
/* tensor connections */
XLink
::
MakeLink
(
&
a
,
&
b
,
&
c
,
MATH_SUB
);
XLink
::
AddParamToHead
(
&
c
,
beta
);
if
(
a
.
enableGrad
&&
b
.
enableGrad
)
{
/* tensor connections */
XLink
::
MakeLink
(
&
a
,
&
b
,
&
c
,
MATH_SUB
);
XLink
::
AddParamToHead
(
&
c
,
beta
);
}
}
}
else
if
(
n
>=
0
&&
n
<
a
.
order
)
{
/* call _SubDim function */
_SubDim
(
&
a
,
&
b
,
&
c
,
n
,
beta
);
if
(
a
.
enableGrad
&&
b
.
enableGrad
)
{
/* tensor connections */
XLink
::
MakeLink
(
&
a
,
&
b
,
&
c
,
MATH_SUBDIM
);
XLink
::
AddParamToHeadInt
(
&
c
,
n
);
XLink
::
AddParamToHead
(
&
c
,
beta
);
else
if
(
n
>=
0
&&
n
<
a
.
order
)
{
/* call _SumDim function to do the SubDim operation */
_SumDim
(
&
a
,
&
b
,
&
c
,
n
,
-
beta
);
if
(
a
.
enableGrad
&&
b
.
enableGrad
)
{
/* tensor connections */
XLink
::
MakeLink
(
&
a
,
&
b
,
&
c
,
MATH_SUBDIM
);
XLink
::
AddParamToHeadInt
(
&
c
,
n
);
XLink
::
AddParamToHead
(
&
c
,
beta
);
}
}
else
{
ShowNTErrors
(
"Something is wrong!"
);
}
}
else
{
ShowNTErrors
(
"Something is wrong!"
);
}
}
...
...
source/tensor/core/arithmetic/Sum.cpp
查看文件 @
eb325d83
...
...
@@ -177,11 +177,11 @@ void SumMe(XTensor& a, const XTensor& b, DTYPE beta)
}
/*
return a dimension if the
sum is performed as SumDim (in more details in SumDim.h
)
return a dimension if the
operation is performed as broadcast(e.g. SumDim function
)
>> a - a tensor
>> b - another tensor for
sum
>> b - another tensor for
operation
*/
int
Get
SumDimIndex
(
const
XTensor
&
a
,
const
XTensor
&
b
)
int
Get
BroadcastDimIndex
(
const
XTensor
&
a
,
const
XTensor
&
b
)
{
if
(
a
.
order
<
b
.
order
)
return
-
1
;
...
...
@@ -229,7 +229,7 @@ XTensor Sum(const XTensor &a, const XTensor &b, DTYPE beta)
ScaleAndShift
(
a
,
c
,
1.0
F
,
shift
);
}
else
{
int
n
=
Get
Sum
DimIndex
(
a
,
b
);
int
n
=
Get
Broadcast
DimIndex
(
a
,
b
);
if
(
n
==
-
1
){
/* call _Sum function */
...
...
@@ -277,7 +277,7 @@ void Sum(const XTensor &a, const XTensor &b, XTensor &c, DTYPE beta)
ScaleAndShift
(
a
,
c
,
1.0
F
,
shift
);
}
else
{
int
n
=
Get
Sum
DimIndex
(
a
,
b
);
int
n
=
Get
Broadcast
DimIndex
(
a
,
b
);
if
(
n
==
-
1
)
{
/* call _Sum function */
...
...
source/tensor/core/arithmetic/Sum.h
查看文件 @
eb325d83
...
...
@@ -26,6 +26,9 @@
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
/* return a dimension if the operation is performed as broadcast(e.g. SumDim function) */
int
GetBroadcastDimIndex
(
const
XTensor
&
a
,
const
XTensor
&
b
);
/* tensor summation c = a + b * \beta */
void
_Sum
(
const
XTensor
*
a
,
const
XTensor
*
b
,
XTensor
*
c
,
DTYPE
beta
=
(
DTYPE
)
1
.
0
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论