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
bdf5c952
Commit
bdf5c952
authored
Feb 19, 2020
by
xuchen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update setdata by template and optimize some interface
parent
bd05b21b
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
18 个修改的文件
包含
77 行增加
和
505 行删除
+77
-505
source/Main.cpp
+0
-152
source/network/XBackwardLoss.cpp
+1
-10
source/tensor/Main.cpp
+0
-169
source/tensor/XTensor.cpp
+14
-87
source/tensor/XTensor.h
+5
-0
source/tensor/core/CHeader.h
+2
-2
source/tensor/core/getandset/OnehotAndIndex.cpp
+1
-1
source/tensor/core/getandset/SetData.cpp
+0
-0
source/tensor/core/getandset/SetData.cu
+0
-0
source/tensor/core/getandset/SetData.cuh
+10
-17
source/tensor/core/getandset/SetData.h
+8
-23
source/tensor/function/DropoutWithIndex.cpp
+1
-1
source/tensor/function/Loss.cpp
+1
-9
source/tensor/test/TDropout.cpp
+6
-6
source/tensor/test/TReduceSum.cpp
+15
-15
source/tensor/test/TSetData.cpp
+5
-5
source/tensor/test/TSpread.cpp
+2
-2
source/tensor/test/TSumDim.cpp
+6
-6
没有找到文件。
source/Main.cpp
查看文件 @
bdf5c952
...
...
@@ -63,155 +63,3 @@ int main( int argc, const char ** argv )
return
0
;
}
void
BackwardTest
()
{
XNet
net
;
XTensor
a
;
XTensor
b
;
XTensor
c
;
a
.
enableGrad
=
true
;
b
.
enableGrad
=
false
;
c
.
enableGrad
=
false
;
XTensor
mean
;
XTensor
origin
;
InitTensor2DV2
(
&
a
,
2
,
3
);
InitTensor1DV2
(
&
b
,
2
);
a
.
SetZeroAll
();
b
.
SetZeroAll
();
a
.
Set2D
(
1.0
F
,
0
,
0
);
a
.
Set2D
(
2.0
F
,
0
,
1
);
a
.
Set2D
(
3.0
F
,
0
,
2
);
a
.
Set2D
(
4.0
F
,
1
,
0
);
a
.
Set2D
(
5.0
F
,
1
,
1
);
a
.
Set2D
(
6.0
F
,
1
,
2
);
b
.
Set1D
(
2.0
F
,
0
);
b
.
Set1D
(
1.0
F
,
1
);
DivDim
(
a
,
b
,
c
,
0
);
c
.
Dump
(
stderr
,
"c:"
);
auto
loss
=
CrossEntropy
(
c
,
a
);
//XLink::ShowNetwork(stderr, &c);
net
.
Backward
(
loss
);
a
.
grad
->
Dump
(
stderr
);
}
void
TransposeTest
()
{
#ifdef USE_CUDA
XMem
mem0
(
0
,
UNI_FREE
,
MILLION
*
64
,
1024
,
MILLION
*
64
);
//XMem mem1(1, UNI_FREE, MILLION * 64, 1024, MILLION * 64);
XTensor
x
;
XTensor
y
;
XTensor
z
;
int
loops
=
2000
;
int
B
=
3
*
2
*
4
;
int
K
=
8
*
1
;
int
N
=
50
;
int
H
=
512
*
4
;
int
nnn
=
GDevs
.
nGPU
;
InitTensor3DV2
(
&
x
,
B
,
N
,
H
,
X_FLOAT
,
0
);
InitTensor4DV2
(
&
y
,
K
,
B
,
N
,
H
/
K
,
X_FLOAT
,
0
);
InitTensor3DV2
(
&
z
,
B
,
N
,
H
,
X_FLOAT
,
0
);
cudaEvent_t
ctime0
;
cudaEvent_t
ctime1
;
cudaEvent_t
ctime2
;
cudaEvent_t
ctime3
;
cudaEvent_t
ctime4
;
cudaEvent_t
ctime5
;
float
elapsedSplit
=
0.0
;
float
elapsedMerge
=
0.0
;
float
elapsedSum
=
0.0
;
cudaEventCreate
(
&
ctime0
);
cudaEventCreate
(
&
ctime1
);
cudaEventCreate
(
&
ctime2
);
cudaEventCreate
(
&
ctime3
);
cudaEventCreate
(
&
ctime4
);
cudaEventCreate
(
&
ctime5
);
cudaEventRecord
(
ctime0
,
0
);
double
time0
=
GetClock
();
for
(
int
i
=
0
;
i
<
loops
;
i
++
)
_Split
(
&
x
,
&
y
,
2
,
K
);
double
time1
=
GetClock
();
cudaEventRecord
(
ctime1
,
0
);
cudaEventSynchronize
(
ctime1
);
cudaEventElapsedTime
(
&
elapsedSplit
,
ctime0
,
ctime1
);
cudaEventRecord
(
ctime2
,
0
);
double
time2
=
GetClock
();
for
(
int
i
=
0
;
i
<
loops
;
i
++
)
_Merge
(
&
y
,
&
x
,
3
);
double
time3
=
GetClock
();
cudaEventRecord
(
ctime3
,
0
);
cudaEventSynchronize
(
ctime3
);
cudaEventElapsedTime
(
&
elapsedMerge
,
ctime2
,
ctime3
);
cudaEventRecord
(
ctime4
,
0
);
double
time4
=
GetClock
();
for
(
int
i
=
0
;
i
<
loops
;
i
++
)
_Sum
(
&
x
,
&
z
,
&
x
);
double
time5
=
GetClock
();
cudaEventRecord
(
ctime5
,
0
);
cudaEventSynchronize
(
ctime5
);
cudaEventElapsedTime
(
&
elapsedSum
,
ctime4
,
ctime5
);
fprintf
(
stderr
,
"split:%f merge:%f sum:%f
\n
"
,
time1
-
time0
,
time3
-
time2
,
time5
-
time4
);
fprintf
(
stderr
,
"split:%f merge:%f sum:%f
\n
"
,
elapsedSplit
,
elapsedMerge
,
elapsedSum
);
#endif
}
void
SumDimTest
()
{
XTensor
x
;
XTensor
y
;
XTensor
z
;
int
a
=
5
;
int
b
=
7
;
int
c
=
3
;
InitTensor3DV2
(
&
x
,
a
,
b
,
c
,
X_FLOAT
,
-
1
);
InitTensor1DV2
(
&
y
,
c
,
X_FLOAT
,
-
1
);
InitTensor3DV2
(
&
z
,
a
,
b
,
c
,
X_FLOAT
,
-
1
);
x
.
SetZeroAll
();
y
.
SetZeroAll
();
z
.
SetZeroAll
();
DTYPE
*
data
=
new
DTYPE
[
x
.
unitNum
];
for
(
int
i
=
0
;
i
<
x
.
unitNum
;
i
++
)
data
[
i
]
=
(
DTYPE
)
i
;
x
.
SetData
(
data
,
x
.
unitNum
);
for
(
int
i
=
0
;
i
<
y
.
unitNum
;
i
++
)
data
[
i
]
=
-
(
DTYPE
)
i
;
y
.
SetData
(
data
,
y
.
unitNum
);
_SumDim
(
&
x
,
&
y
,
&
z
,
2
);
z
.
Dump
(
stderr
,
"z:"
);
delete
[]
data
;
}
source/network/XBackwardLoss.cpp
查看文件 @
bdf5c952
...
...
@@ -33,7 +33,6 @@
namespace
nts
{
/* compute dE/dx of a node */
void
XLossGrad
::
MakeGrad
(
XTensor
*
node
,
bool
isEfficient
)
{
...
...
@@ -53,15 +52,7 @@ void XLossGrad::MakeGrad(XTensor * node, bool isEfficient)
XTensor
*
dedy
=
output
->
grad
;
if
(
income
.
tailNum
==
1
)
{
if
(
dedy
->
dataType
==
X_FLOAT
)
_SetDataFixedFloat
(
dedy
,
1.0
F
);
else
if
(
dedy
->
dataType
==
X_DOUBLE
)
_SetDataFixedDouble
(
dedy
,
1.0
);
else
if
(
dedy
->
dataType
==
X_INT
)
_SetDataFixedInt
(
dedy
,
1
);
else
ShowNTErrors
(
"TODO"
);
dedy
->
SetDataFixed
(
1
);
return
;
}
...
...
source/tensor/Main.cpp
deleted
100644 → 0
查看文件 @
bd05b21b
/* NiuTrans.Tensor - an open-source tensor library
* Copyright (C) 2017, Natural Language Processing Lab, Northestern University.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
*
* This is the entrance of the low-level tensor library : NiuTrans.Tensor
*
* $Created by: XIAO Tong (xiaotong@mail.neu.edu.cn) 2015-12-14
*
*/
#include <stdio.h>
#include <math.h>
#include <time.h>
#include "XTensor.h"
#include "XDevice.h"
#include "./test/Test.h"
#include "./core/CHeader.h"
#include "./XBLAS.h"
#include "./core/sort/TopK.h"
#include "./core/movement/Gather.h"
//#define CRTDBG_MAP_ALLOC
//#include <stdlib.h>
//#include <crtdbg.h>
using
namespace
nts
;
void
SmallTest
();
void
TransposeTest
();
void
PowerTest
();
int
main
(
int
argc
,
const
char
**
argv
)
{
//PowerTest();
//LittleTest();
//T2TTest();
//T2TTest2();
//return 0;
//_CrtSetBreakAlloc(123);
/* a tiny test */
//SmallTest();
//_CrtDumpMemoryLeaks();
//return 0;
if
(
argc
>
1
&&
!
strcmp
(
argv
[
1
],
"-test"
))
Test
();
else
{
fprintf
(
stderr
,
"Thanks for using NiuTrans.Tensor! This is a library that eases the
\n
"
);
fprintf
(
stderr
,
"use of tensors. All you need is to ...
\n\n
"
);
fprintf
(
stderr
,
"Run this program with
\"
-test
\"
for unit test!
\n
"
);
}
//_CrtDumpMemoryLeaks();
return
0
;
}
void
myRead
(
XTensor
*
tensor
,
const
char
*
filename
,
const
char
*
label
)
{
FILE
*
file
=
fopen
(
filename
,
"rb"
);
if
(
file
==
NULL
)
printf
(
"%s
\n
"
,
filename
);
tensor
->
Read
(
file
,
label
);
}
void
myDump
(
XTensor
*
tensor
,
const
char
*
filename
,
const
char
*
label
)
{
FILE
*
file
=
fopen
(
filename
,
"wb"
);
if
(
file
==
NULL
)
printf
(
"%s
\n
"
,
filename
);
tensor
->
Dump
(
file
,
label
);
}
void
PowerTest
()
{
XTensor
input
;
XTensor
output
;
InitTensor2D
(
&
input
,
256
,
10000
,
X_FLOAT
,
0
);
InitTensor2D
(
&
output
,
256
,
10000
,
X_FLOAT
,
0
);
myRead
(
&
input
,
"1.txt"
,
""
);
_Power
(
&
input
,
&
output
,
2
);
output
.
Dump
(
stderr
,
""
,
200
);
}
void
SmallTest
()
{
XTensor
a
;
XTensor
b
;
XTensor
c
;
XTensor
d
;
InitTensor2D
(
&
a
,
2
,
2
);
InitTensor2D
(
&
b
,
2
,
2
);
a
.
SetZeroAll
();
b
.
SetZeroAll
();
a
.
Set2D
(
1.0
F
,
0
,
0
);
a
.
Set2D
(
2.0
F
,
1
,
1
);
b
=
Sum
(
a
,
Multiply
(
a
,
a
));
/* this is prohibited !!!!!!!!!!!!! */
//XTensor c = a * b + a;
//XTensor d = a + b + c.Lin(0.5F);
c
=
a
*
b
+
a
;
d
=
a
+
b
+
c
.
Lin
(
0.5
F
);
XLink
::
CheckNetwork
(
&
d
);
//XLink::ShowNetwork(stderr, &d);
a
.
Dump
(
stderr
,
"a:"
);
b
.
Dump
(
stderr
,
"b:"
);
c
.
Dump
(
stderr
,
"c:"
);
d
.
Dump
(
stderr
,
"d:"
);
}
void
TransposeTest
()
{
XTensor
a
;
XTensor
b
;
int
I
=
2
;
int
J
=
3
;
InitTensor4D
(
&
a
,
2
,
3
,
4
,
5
);
int
*
dims
=
new
int
[
a
.
order
];
memcpy
(
dims
,
a
.
dimSize
,
sizeof
(
int
)
*
a
.
order
);
dims
[
I
]
=
a
.
dimSize
[
J
];
dims
[
J
]
=
a
.
dimSize
[
I
];
InitTensor
(
&
b
,
4
,
dims
);
a
.
SetZeroAll
();
b
.
SetZeroAll
();
float
*
data
=
new
float
[
a
.
unitNum
];
for
(
int
i
=
0
;
i
<
a
.
unitNum
;
i
++
)
data
[
i
]
=
(
float
)
i
;
a
.
SetData
(
data
,
a
.
unitNum
,
0
);
_Transpose
(
&
a
,
&
b
,
I
,
J
);
b
.
Dump
(
stderr
,
"b:"
);
delete
[]
data
;
}
source/tensor/XTensor.cpp
查看文件 @
bdf5c952
...
...
@@ -526,6 +526,8 @@ void XTensor::SetDevice(int myDevId, XMem * myMem)
}
else
{
myMem
=
GMems
.
GetMem
(
myDevId
);
FlushToMem
(
myMem
);
isInGlobalMem
=
false
;
}
}
...
...
@@ -818,6 +820,16 @@ void XTensor::Range(DTYPE lower, DTYPE upper, DTYPE step)
_SetDataRange
(
this
,
lower
,
upper
,
step
);
}
/* generate data items with a fixed value */
template
<
class
T
>
void
XTensor
::
SetDataFixed
(
T
num
)
{
_SetDataFixed
(
this
,
num
);
}
template
void
XTensor
::
SetDataFixed
<
int
>
(
int
);
template
void
XTensor
::
SetDataFixed
<
float
>
(
float
);
template
void
XTensor
::
SetDataFixed
<
double
>
(
double
);
/*
set the tensor items by a uniform distribution in range [lower, upper]
>> lower - lower value of the range
...
...
@@ -825,62 +837,7 @@ set the tensor items by a uniform distribution in range [lower, upper]
*/
void
XTensor
::
SetDataRand
(
DTYPE
lower
,
DTYPE
upper
)
{
// TODO: GPU code!!!!!!!
if
(
data
==
NULL
)
return
;
// srand((unsigned)time(0));
DTYPE
variance
=
upper
-
lower
;
void
*
d
=
NULL
;
if
(
dataType
==
X_FLOAT
)
{
d
=
new
float
[
unitNum
];
for
(
int
i
=
0
;
i
<
unitNum
;
i
++
)
{
DTYPE
value
=
lower
+
variance
*
(
float
)
rand
()
/
RAND_MAX
;
*
((
float
*
)
d
+
i
)
=
value
;
}
}
else
if
(
dataType
==
X_DOUBLE
)
{
d
=
new
double
[
unitNum
];
for
(
int
i
=
0
;
i
<
unitNum
;
i
++
)
{
*
((
double
*
)
d
+
i
)
=
lower
+
variance
*
rand
()
/
RAND_MAX
;
}
}
else
{
ShowNTErrors
(
"Data type must be X_FLOAT or X_Double!"
);
}
SetData
(
d
,
unitNum
);
if
(
dataType
==
X_FLOAT
)
{
delete
[]
(
float
*
)
d
;
}
else
{
delete
[]
(
double
*
)
d
;
}
}
/* a gauss distribution (Box-Muller method) */
double
GaussRand
(
DTYPE
mean
,
DTYPE
standardDeviation
)
{
// TODO: GPU code!!!!!!!
static
double
u
,
v
;
static
int
phase
=
0
;
double
z
;
double
pi
=
3.141592654
;
if
(
phase
==
0
){
u
=
(
rand
()
+
1.0
)
/
(
RAND_MAX
+
1.0
);
v
=
(
rand
()
+
1.0
)
/
(
RAND_MAX
+
1.0
);
z
=
sqrt
(
-
2.0
*
log
(
u
))
*
sin
(
2.0
*
pi
*
v
);
}
else
{
z
=
sqrt
(
-
2.0
*
log
(
u
))
*
cos
(
2.0
*
pi
*
v
);
}
phase
=
1
-
phase
;
return
mean
+
(
z
*
standardDeviation
);
_SetDataRand
(
this
,
lower
,
upper
);
}
/*
...
...
@@ -890,37 +847,7 @@ set the tensor items by a normal distribution
*/
void
XTensor
::
SetDataRandn
(
DTYPE
mean
,
DTYPE
standardDeviation
)
{
// TODO: cuda code!!!!!!!
if
(
data
==
NULL
)
return
;
// srand((unsigned)time(0));
void
*
d
=
NULL
;
if
(
dataType
==
X_FLOAT
)
{
d
=
new
float
[
unitNum
];
for
(
int
i
=
0
;
i
<
unitNum
;
i
++
)
{
*
((
float
*
)
d
+
i
)
=
(
float
)
GaussRand
(
mean
,
standardDeviation
);
}
}
else
if
(
dataType
==
X_DOUBLE
)
{
d
=
new
double
[
unitNum
];
for
(
int
i
=
0
;
i
<
unitNum
;
i
++
)
{
*
((
double
*
)
d
+
i
)
=
GaussRand
(
mean
,
standardDeviation
);
}
}
else
{
ShowNTErrors
(
"Data type must be X_FLOAT or X_Double!"
);
}
SetData
(
d
,
unitNum
);
if
(
dataType
==
X_FLOAT
)
{
delete
[]
(
float
*
)
d
;
}
else
{
delete
[]
(
double
*
)
d
;
}
_SetDataRandN
(
this
,
mean
,
standardDeviation
);
}
/*
...
...
source/tensor/XTensor.h
查看文件 @
bdf5c952
...
...
@@ -28,6 +28,7 @@
#ifndef __XTENSOR_H__
#define __XTENSOR_H__
#include <math.h>
#include "XGlobal.h"
#include "XMem.h"
#include "XPRunner.h"
...
...
@@ -303,6 +304,10 @@ public:
/* generate data items with a range by start, end and the step */
void
Range
(
DTYPE
lower
,
DTYPE
upper
,
DTYPE
step
);
/* generate data items with a fixed value */
template
<
class
T
>
void
SetDataFixed
(
T
num
);
/* set tensor items by a uniform distribution */
void
SetDataRand
(
DTYPE
lower
=
0
.
0
F
,
DTYPE
upper
=
1
.
0
F
);
...
...
source/tensor/core/CHeader.h
查看文件 @
bdf5c952
...
...
@@ -91,9 +91,9 @@
#include "sort/Sort.h"
#include "sort/TopK.h"
#include "utilities/XMatrixSegment.h"
#include "utilities/FlushToMem.h"
#include "utilities/CheckData.h"
#include "utilities/FlushToMem.h"
#include "utilities/SetAscendingOrder.h"
#include "utilities/XMatrixSegment.h"
#endif // __CHEADER_H__
source/tensor/core/getandset/OnehotAndIndex.cpp
查看文件 @
bdf5c952
...
...
@@ -116,7 +116,7 @@ void _IndexToOnehot(const XTensor * index, XTensor * onehot,
float
confidence
=
1
-
labelSmoothingP
;
float
lowconfidence
=
labelSmoothingP
/
size
;
_SetDataFixedFloat
(
onehot
,
lowconfidence
);
onehot
->
SetDataFixed
(
lowconfidence
);
#ifdef USE_CUDA
if
(
onehot
->
devID
>=
0
&&
index
->
devID
>=
0
)
{
...
...
source/tensor/core/getandset/SetData.cpp
查看文件 @
bdf5c952
差异被折叠。
点击展开。
source/tensor/core/getandset/SetData.cu
查看文件 @
bdf5c952
差异被折叠。
点击展开。
source/tensor/core/getandset/SetData.cuh
查看文件 @
bdf5c952
...
...
@@ -28,31 +28,24 @@
namespace nts { // namespace nts(NiuTrans.Tensor)
/* generate data items with a fixed value p (in int) */
void _CudaSetDataFixedInt(XTensor * tensor, int p);
/* generate data items with a fixed value */
template<class T>
void _CudaSetDataFixed(XTensor * tensor, T value);
/* generate data items with a fixed value p (in float) */
void _CudaSetDataFixedFloat(XTensor * tensor, float p);
/* generate data items with a fixed value p (in double) */
void _CudaSetDataFixedDouble(XTensor * tensor, double p);
/* generate data items with a fixed value p (in float) only
if the condition entry is non-zero */
void _CudaSetDataFixedCondFloat(XTensor * tensor, XTensor * condition, float p);
/* generate data items with a fixed value p (in int) only
if the condition entry is non-zero */
void _CudaSetDataFixedCondInt(XTensor * tensor, XTensor * condition, int p);
/* generate data items with a fixed value p
only if the condition entry is non-zero */
template<class T>
void _CudaSetDataFixedCond(XTensor * tensor, XTensor * condition, T p);
/* set data items along with a given dimension (and keep the remaining items unchanged) */
void _CudaSetDataDim(XTensor * tensor, int beg, int len, int dim, DTYPE p);
template<class T>
void _CudaSetDataDim(XTensor * tensor, int beg, int len, int dim, T p);
/* modify data items along with a given index and dimension (and keep the remaining items unchanged) */
void _CudaSetDataIndexed(XTensor * source, XTensor * modify, int dim, int index);
/* generate data as lower triangular matrics for last two dimensions (cuda version) */
void _CudaSetDataLowTri(XTensor * tensor, DTYPE
p
, int shift);
void _CudaSetDataLowTri(XTensor * tensor, DTYPE
value
, int shift);
/* generate data items with a uniform distribution in [lower, upper] */
void _CudaSetDataRand(const XTensor * tensor, DTYPE lower, DTYPE upper);
...
...
source/tensor/core/getandset/SetData.h
查看文件 @
bdf5c952
...
...
@@ -30,32 +30,17 @@ namespace nts { // namespace nts(NiuTrans.Tensor)
/* generate data items with a xavier initialization */
void
_SetDataFanInOut
(
XTensor
*
tensor
,
DTYPE
gain
=
1
.
0
F
);
/* generate data items with a fixed value p */
void
_SetDataFixed
(
XTensor
*
tensor
,
void
*
valuePointer
);
/* generate data items with a fixed value */
template
<
class
T
>
void
_SetDataFixed
(
XTensor
*
tensor
,
T
value
);
/* generate data items with a fixed value p (in default type) */
void
SetDataFixed
(
XTensor
&
tensor
,
DTYPE
p
);
/* generate data items with a fixed value p (in integer) */
void
SetDataFixedInt
(
XTensor
&
tensor
,
int
p
);
/* generate data items with a fixed value p (in int) */
void
_SetDataFixedInt
(
XTensor
*
tensor
,
int
p
);
/* generate data items with a fixed value p (in float) */
void
_SetDataFixedFloat
(
XTensor
*
tensor
,
float
p
);
/* generate data items with a fixed value p (in double) */
void
_SetDataFixedDouble
(
XTensor
*
tensor
,
double
p
);
/* generate data items with a fixed value p only if the condition entry is non-zero */
void
_SetDataFixedCond
(
XTensor
*
tensor
,
XTensor
*
condition
,
DTYPE
p
);
/* generate data items with a fixed value p only if the condition entry is non-zero */
void
_SetDataFixedCondInt
(
XTensor
*
tensor
,
XTensor
*
condition
,
int
p
);
/* generate data items with a fixed value only if the condition entry is non-zero */
template
<
class
T
>
void
_SetDataFixedCond
(
XTensor
*
tensor
,
XTensor
*
condition
,
T
value
);
/* set data items along with a given dimension (and keep the remaining items unchanged) */
void
_SetDataDim
(
XTensor
*
tensor
,
int
beg
,
int
len
,
int
dim
,
DTYPE
p
);
template
<
class
T
>
void
_SetDataDim
(
XTensor
*
tensor
,
int
beg
,
int
len
,
int
dim
,
T
p
);
/* modify data items along with a given index and dimension (and keep the remaining items unchanged) */
void
_SetDataIndexed
(
XTensor
*
source
,
XTensor
*
modify
,
int
dim
,
int
index
);
...
...
source/tensor/function/DropoutWithIndex.cpp
查看文件 @
bdf5c952
...
...
@@ -70,7 +70,7 @@ XTensor DropoutWithIndex(const XTensor &x, XTensor &maskIndex, DTYPE scale)
InitTensor1DV2
(
&
c
,
x
.
unitNum
,
x
.
dataType
,
x
.
devID
,
x
.
mem
);
_SetDataFixedFloat
(
&
c
,
1.0
F
);
c
.
SetDataFixed
(
1.0
);
_DropoutWithIndex
(
&
x
,
&
maskIndex
,
&
c
);
...
...
source/tensor/function/Loss.cpp
查看文件 @
bdf5c952
...
...
@@ -383,15 +383,7 @@ void _LossBackward(XTensor * dedy, XTensor * t, XTensor * y,
int
leadDim
,
int
tBeg
,
int
tLen
,
int
yBeg
)
{
if
(
t
==
NULL
){
if
(
dedy
->
dataType
==
X_FLOAT
)
_SetDataFixedFloat
(
dedy
,
1.0
F
);
else
if
(
dedy
->
dataType
==
X_DOUBLE
)
_SetDataFixedDouble
(
dedy
,
1.0
);
else
if
(
dedy
->
dataType
==
X_INT
)
_SetDataFixedInt
(
dedy
,
1
);
else
{
ShowNTErrors
(
"TODO"
);
}
dedy
->
SetDataFixed
(
1
);
return
;
}
...
...
source/tensor/test/TDropout.cpp
查看文件 @
bdf5c952
...
...
@@ -50,7 +50,7 @@ bool TestDropout1()
XTensor
yUser
;
/* initialize variables */
_SetDataFixedFloat
(
x
,
1.0
F
);
x
->
SetDataFixed
(
1
);
y
->
SetZeroAll
();
/* call Dropout function */
...
...
@@ -88,7 +88,7 @@ bool TestDropout1()
XTensor
yUserGPU
;
/* initialize variables */
_SetDataFixedFloat
(
xGPU
,
1.0
F
);
xGPU
->
SetDataFixed
(
1
);
yGPU
->
SetZeroAll
();
/* call Dropout function */
...
...
@@ -157,10 +157,10 @@ bool TestDropout2()
XTensor
*
dedy
=
NewTensorV2
(
order
,
dimSize
);
/* initialize variables */
_SetDataFixedFloat
(
x
,
1.0
F
);
x
->
SetDataFixed
(
1.0
);
y
->
SetZeroAll
();
dedx
->
SetZeroAll
();
_SetDataFixedFloat
(
dedy
,
1.5
F
);
dedy
->
SetDataFixed
(
1.5
);
/* call Dropout function */
float
dropProb
=
0.5
F
;
...
...
@@ -183,10 +183,10 @@ bool TestDropout2()
XTensor
*
dedyGPU
=
NewTensorV2
(
order
,
dimSize
,
X_FLOAT
,
1.0
F
,
0
);
/* initialize variables */
_SetDataFixedFloat
(
xGPU
,
1.0
F
);
xGPU
->
SetDataFixed
(
1.0
);
yGPU
->
SetZeroAll
();
dedxGPU
->
SetZeroAll
();
_SetDataFixedFloat
(
dedyGPU
,
1.5
F
);
dedyGPU
->
SetDataFixed
(
1.5
);
/* call Dropout function */
_Dropout
(
xGPU
,
yGPU
,
seed
,
dropProb
);
...
...
source/tensor/test/TReduceSum.cpp
查看文件 @
bdf5c952
...
...
@@ -195,8 +195,8 @@ bool TestReduceSum2()
XTensor
tUser
;
/* initialize variables */
_SetDataFixedFloat
(
s
,
1.0
F
);
_SetDataFixedFloat
(
answer
,
(
float
)
s
->
GetDim
(
1
));
s
->
SetDataFixed
(
1
);
answer
->
SetDataFixed
(
s
->
GetDim
(
1
));
/* call ReduceSum function */
_ReduceSum
(
s
,
t
,
1
);
...
...
@@ -215,7 +215,7 @@ bool TestReduceSum2()
XTensor
tUserGPU
;
/* initialize variables */
_SetDataFixedFloat
(
sGPU
,
1.0
F
);
sGPU
->
SetDataFixed
(
1
);
/* call ReduceSum function */
_ReduceSum
(
sGPU
,
tGPU
,
1
);
...
...
@@ -284,8 +284,8 @@ bool TestReduceSum3()
XTensor
tUser
;
/* initialize variables */
_SetDataFixedFloat
(
s
,
1.0
F
);
_SetDataFixedFloat
(
answer
,
(
float
)
s
->
GetDim
(
1
));
s
->
SetDataFixed
(
1
);
answer
->
SetDataFixed
(
s
->
GetDim
(
1
));
/* call ReduceSum function */
_ReduceSum
(
s
,
t
,
1
);
...
...
@@ -304,7 +304,7 @@ bool TestReduceSum3()
XTensor
tUserGPU
;
/* initialize variables */
_SetDataFixedFloat
(
sGPU
,
1.0
F
);
sGPU
->
SetDataFixed
(
1
);
/* call ReduceSum function */
_ReduceSum
(
sGPU
,
tGPU
,
1
);
...
...
@@ -373,8 +373,8 @@ bool TestReduceSum4()
XTensor
tUser
;
/* initialize variables */
_SetDataFixedFloat
(
s
,
1.0
F
);
_SetDataFixedFloat
(
answer
,
(
float
)
s
->
GetDim
(
1
));
s
->
SetDataFixed
(
1
);
answer
->
SetDataFixed
(
s
->
GetDim
(
1
));
/* call ReduceSum function */
_ReduceSum
(
s
,
t
,
1
);
...
...
@@ -393,7 +393,7 @@ bool TestReduceSum4()
XTensor
tUserGPU
;
/* initialize variables */
_SetDataFixedFloat
(
sGPU
,
1.0
F
);
sGPU
->
SetDataFixed
(
1
);
/* call ReduceSum function */
_ReduceSum
(
sGPU
,
tGPU
,
1
);
...
...
@@ -464,8 +464,8 @@ bool TestReduceSum5()
XTensor
tUser
;
/* initialize variables */
_SetDataFixedFloat
(
s
,
1.0
F
);
_SetDataFixedFloat
(
answer
,
(
float
)
s
->
GetDim
(
1
));
s
->
SetDataFixed
(
1
);
answer
->
SetDataFixed
(
s
->
GetDim
(
1
));
/* call ReduceSum function */
_ReduceSum
(
s
,
t
,
1
);
...
...
@@ -484,7 +484,7 @@ bool TestReduceSum5()
XTensor
tUserGPU
;
/* initialize variables */
_SetDataFixedFloat
(
sGPU
,
1.0
F
);
sGPU
->
SetDataFixed
(
1
);
/* call ReduceSum function */
_ReduceSum
(
sGPU
,
tGPU
,
1
);
...
...
@@ -556,8 +556,8 @@ bool TestReduceSum6()
XTensor
tUser
;
/* initialize variables */
_SetDataFixedFloat
(
s
,
1.0
F
);
_SetDataFixedFloat
(
answer
,
(
float
)
s
->
GetDim
(
1
));
s
->
SetDataFixed
(
1
);
answer
->
SetDataFixed
(
s
->
GetDim
(
1
));
/* call ReduceSum function */
_ReduceSum
(
s
,
t
,
1
);
...
...
@@ -576,7 +576,7 @@ bool TestReduceSum6()
XTensor
tUserGPU
;
/* initialize variables */
_SetDataFixedFloat
(
sGPU
,
1.0
F
);
sGPU
->
SetDataFixed
(
1
);
/* call ReduceSum function */
_ReduceSum
(
sGPU
,
tGPU
,
1
);
...
...
source/tensor/test/TSetData.cpp
查看文件 @
bdf5c952
...
...
@@ -119,7 +119,7 @@ bool TestSetData2()
XTensor
*
modify
=
NewTensorV2
(
dataOrder
,
dataDimSize
);
/* Initialize variables */
_SetDataFixedFloat
(
s
,
1.0
F
);
s
->
SetDataFixed
(
1
);
modify
->
SetData
(
data
,
dataUnitNum
);
/* call SetDataIndexed function */
...
...
@@ -137,7 +137,7 @@ bool TestSetData2()
XTensor
*
modifyGPU
=
NewTensorV2
(
dataOrder
,
dataDimSize
,
X_FLOAT
,
1.0
F
,
0
);
/* Initialize variables */
_SetDataFixedFloat
(
sGPU
,
1.0
F
);
sGPU
->
SetDataFixed
(
1
);
modifyGPU
->
SetData
(
data
,
dataUnitNum
);
/* call SetDataIndexed function */
...
...
@@ -212,11 +212,11 @@ bool TestSetData3()
XTensor
*
modify
=
NewTensorV2
(
dataOrder
,
dataDimSize
);
/* Initialize variables */
_SetDataFixedFloat
(
s
,
1.0
F
);
s
->
SetDataFixed
(
1
);
modify
->
SetData
(
data
,
dataUnitNum
);
/* call SetDataIndexed function */
_SetDataFixedFloat
(
s
,
1.0
F
);
s
->
SetDataFixed
(
1
);
_SetDataIndexed
(
s
,
modify
,
1
,
1
);
/* check results */
...
...
@@ -231,7 +231,7 @@ bool TestSetData3()
XTensor
*
modifyGPU
=
NewTensorV2
(
dataOrder
,
dataDimSize
,
X_FLOAT
,
1.0
F
,
0
);
/* Initialize variables */
_SetDataFixedFloat
(
sGPU
,
1.0
F
);
sGPU
->
SetDataFixed
(
1
);
modifyGPU
->
SetData
(
data
,
dataUnitNum
);
/* call SetDataIndexed function */
...
...
source/tensor/test/TSpread.cpp
查看文件 @
bdf5c952
...
...
@@ -91,7 +91,7 @@ bool TestSpread1()
XTensor
*
modify
=
NewTensorV2
(
dataOrder
,
dataDimSize
);
/* Initialize variables */
_SetDataFixedFloat
(
s
,
0.0
F
);
s
->
SetZeroAll
(
);
modify
->
SetData
(
data
,
dataUnitNum
);
/* call _Spread function */
...
...
@@ -109,7 +109,7 @@ bool TestSpread1()
XTensor
*
modifyGPU
=
NewTensorV2
(
dataOrder
,
dataDimSize
,
X_FLOAT
,
1.0
F
,
0
);
/* Initialize variables */
_SetDataFixedFloat
(
sGPU
,
0.0
F
);
sGPU
->
SetZeroAll
(
);
modifyGPU
->
SetData
(
data
,
dataUnitNum
);
/* call _Spread function */
...
...
source/tensor/test/TSumDim.cpp
查看文件 @
bdf5c952
...
...
@@ -296,8 +296,8 @@ bool TestSumDim3()
/* initialize variables */
a
->
SetZeroAll
();
cMe
->
SetZeroAll
();
_SetDataFixedFloat
(
b
,
1.0
F
);
_SetDataFixedFloat
(
answer
,
1.0
F
);
b
->
SetDataFixed
(
1
);
answer
->
SetDataFixed
(
1
);
/* call SumDim function */
_SumDim
(
a
,
b
,
c
,
1
);
...
...
@@ -323,7 +323,7 @@ bool TestSumDim3()
/* Initialize variables */
aGPU
->
SetZeroAll
();
cMe
->
SetZeroAll
();
_SetDataFixedFloat
(
bGPU
,
1.0
F
);
bGPU
->
SetDataFixed
(
1
);
/* call sum function */
_SumDim
(
aGPU
,
bGPU
,
cGPU
,
1
);
...
...
@@ -405,8 +405,8 @@ bool TestSumDim4()
/* initialize variables */
a
->
SetZeroAll
();
cMe
->
SetZeroAll
();
_SetDataFixedFloat
(
b
,
1.0
F
);
_SetDataFixedFloat
(
answer
,
1.0
F
);
b
->
SetDataFixed
(
1
);
answer
->
SetDataFixed
(
1
);
/* call SumDim function */
_SumDim
(
a
,
b
,
c
,
1
);
...
...
@@ -432,7 +432,7 @@ bool TestSumDim4()
/* Initialize variables */
aGPU
->
SetZeroAll
();
cMe
->
SetZeroAll
();
_SetDataFixedFloat
(
bGPU
,
1.0
F
);
bGPU
->
SetDataFixed
(
1
);
/* call sum function */
_SumDim
(
aGPU
,
bGPU
,
cGPU
,
1
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论