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
Emmay
NiuTrans.Tensor
Commits
f8ddb15b
Commit
f8ddb15b
authored
Jul 11, 2018
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
new XLink methods
parent
bc12dbd2
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
185 行增加
和
7 行删除
+185
-7
source/tensor/Main.cpp
+5
-0
source/tensor/XLink.cpp
+158
-6
source/tensor/XLink.h
+12
-0
source/tensor/XTensor.cpp
+9
-0
source/tensor/core/math/ScaleAndShift.cpp
+1
-1
没有找到文件。
source/tensor/Main.cpp
查看文件 @
f8ddb15b
...
...
@@ -78,7 +78,12 @@ void SmallTest()
XTensor
c
=
a
*
b
+
a
;
int
nnn
=
1
;
XTensor
d
=
a
+
b
+
c
.
Lin
(
0.5
F
);
XLink
::
CheckNetwork
(
&
d
);
XLink
::
ShowNetwork
(
stderr
,
&
b
);
a
.
Dump
(
stderr
,
"a: "
);
b
.
Dump
(
stderr
,
"b: "
);
...
...
source/tensor/XLink.cpp
查看文件 @
f8ddb15b
...
...
@@ -100,10 +100,11 @@ void XLink::ClearIncoming(XTensor * node)
childOutgo
.
tails
+
j
+
1
,
(
childOutgo
.
tailNum
-
1
-
j
)
*
sizeof
(
XTensor
*
));
childOutgo
.
tailNum
--
;
break
;
}
}
if
(
childOutgo
.
tailNum
==
0
)
if
(
child
->
isTmp
&&
child
Outgo
.
tailNum
==
0
)
delete
child
;
}
...
...
@@ -120,7 +121,7 @@ void XLink::SetType(int id)
type
[
0
]
=
0
;
strcpy
(
type
,
GetOPName
(
id
));
typeID
=
id
;
CheckNTErrors
(
!
strcmp
(
type
,
"NULL"
),
"illegal edge type name!"
);
CheckNTErrors
(
strcmp
(
type
,
"NULL"
),
"illegal edge type name!"
);
}
/*
...
...
@@ -199,7 +200,7 @@ create a hyperedge with two input tensors and a output tensor
*/
void
XLink
::
MakeLink
(
const
XTensor
*
t1
,
const
XTensor
*
t2
,
XTensor
*
h
,
int
id
)
{
if
(
h
!
=
NULL
)
if
(
h
=
=
NULL
)
return
;
XList
list
(
2
);
...
...
@@ -225,14 +226,19 @@ void XLink::MakeLink(XList * list, XTensor * h, int id)
for
(
int
i
=
0
;
i
<
list
->
count
;
i
++
){
XTensor
*
t
=
(
XTensor
*
)
list
->
GetItem
(
i
);
if
(
t
==
NULL
)
continue
;
income
.
AddTail
(
t
);
}
/* backward */
for
(
int
i
=
0
;
i
<
list
->
count
;
i
++
){
XTensor
*
t
=
(
XTensor
*
)
list
->
GetItem
(
i
);
if
(
t
==
NULL
)
continue
;
XLink
&
outgo
=
t
->
outgo
;
CheckNTErrors
(
outgo
.
head
!=
t
,
"Wrong head of the hyperedge!"
);
CheckNTErrors
(
outgo
.
head
==
NULL
||
outgo
.
head
==
t
,
"Wrong head of the hyperedge!"
);
outgo
.
AddTail
(
h
);
}
}
...
...
@@ -278,6 +284,7 @@ void XLink::Replace(const XTensor * oldOne, XTensor * newOne)
delete
[]
newIncome
.
tails
;
/* incoming nodes for the new node */
newIncome
.
head
=
newOne
;
newIncome
.
tailNum
=
oldOne
->
income
.
tailNum
;
newIncome
.
tails
=
new
XTensor
*
[
newIncome
.
tailNum
];
memcpy
(
newIncome
.
tails
,
oldOne
->
income
.
tails
,
sizeof
(
XTensor
*
)
*
newIncome
.
tailNum
);
...
...
@@ -286,27 +293,172 @@ void XLink::Replace(const XTensor * oldOne, XTensor * newOne)
for
(
int
i
=
0
;
i
<
newIncome
.
tailNum
;
i
++
){
XTensor
*
child
=
newIncome
.
tails
[
i
];
XLink
&
childOutgo
=
child
->
outgo
;
bool
hit
=
false
;
for
(
int
j
=
0
;
j
<
childOutgo
.
tailNum
;
j
++
){
if
(
childOutgo
.
tails
[
j
]
==
oldOne
){
childOutgo
.
tails
[
j
]
=
newOne
;
hit
=
true
;
break
;
}
}
if
(
childOutgo
.
tailNum
>
0
){
CheckNTErrors
(
hit
,
"No proper node found in child.outgo edge!"
);
}
}
/* outgoing nodes for the new node */
newOutgo
.
tailNum
=
oldOne
->
income
.
tailNum
;
newOutgo
.
head
=
newOne
;
newOutgo
.
tailNum
=
oldOne
->
outgo
.
tailNum
;
newOutgo
.
tails
=
new
XTensor
*
[
newOutgo
.
tailNum
];
memcpy
(
newOutgo
.
tails
,
oldOne
->
income
.
tails
,
sizeof
(
XTensor
*
)
*
newOutgo
.
tailNum
);
memcpy
(
newOutgo
.
tails
,
oldOne
->
outgo
.
tails
,
sizeof
(
XTensor
*
)
*
newOutgo
.
tailNum
);
/* update the link to each parent node */
for
(
int
i
=
0
;
i
<
newOutgo
.
tailNum
;
i
++
){
XTensor
*
parent
=
newOutgo
.
tails
[
i
];
XLink
&
parentIncome
=
parent
->
income
;
bool
hit
=
false
;
for
(
int
j
=
0
;
j
<
parentIncome
.
tailNum
;
j
++
){
if
(
parentIncome
.
tails
[
j
]
==
oldOne
){
parentIncome
.
tails
[
j
]
=
newOne
;
hit
=
true
;
}
}
if
(
parentIncome
.
tailNum
>
0
){
CheckNTErrors
(
hit
,
"No proper node found in parent.income edge!"
);
}
}
}
/*
copy incoming edges of a given node
>> reference - the node we copy from
>> target - where we copy to
*/
void
XLink
::
CopyIncoming
(
const
XTensor
*
reference
,
XTensor
*
target
)
{
CheckNTErrors
(
reference
&&
target
,
"Empty input tensors!"
);
ClearIncoming
(
target
);
int
tailNum
=
reference
->
income
.
tailNum
;
XList
tails
(
tailNum
);
for
(
int
i
=
0
;
i
<
tailNum
;
i
++
){
XTensor
*
tail
=
(
XTensor
*
)
reference
->
income
.
tails
[
i
];
tails
.
Add
(
tail
);
}
MakeLink
(
&
tails
,
target
,
reference
->
id
);
int
paraNum
=
reference
->
income
.
paramNum
;
target
->
income
.
paramNum
=
paraNum
;
delete
[]
(
char
*
)
target
->
income
.
params
;
int
size
=
paraNum
*
reference
->
income
.
paramSize
;
target
->
income
.
params
=
new
char
[
size
];
memcpy
(
target
->
income
.
params
,
reference
->
income
.
params
,
size
);
}
/*
check the correctness of the network encoded in a root node (tensor)
>> root - pointer to the root node
*/
void
XLink
::
CheckNetwork
(
XTensor
*
root
)
{
XLink
&
income
=
root
->
income
;
if
(
income
.
head
==
NULL
){
CheckNTErrors
(
income
.
tailNum
==
0
,
"Wrong number of the incoming edge tails!"
);
}
else
{
for
(
int
i
=
0
;
i
<
income
.
tailNum
;
i
++
){
XTensor
*
child
=
income
.
tails
[
i
];
if
(
child
==
NULL
)
continue
;
XLink
&
childOutgo
=
child
->
outgo
;
bool
hit
=
false
;
for
(
int
j
=
0
;
j
<
childOutgo
.
tailNum
;
j
++
){
if
(
childOutgo
.
tails
[
j
]
==
root
){
hit
=
true
;
break
;
}
}
CheckNTErrors
(
hit
,
"Wrong outgoing edge!"
);
}
}
XLink
&
outgo
=
root
->
outgo
;
if
(
outgo
.
head
==
NULL
){
CheckNTErrors
(
outgo
.
tailNum
==
0
,
"Wrong number of the incoming edge tails!"
);
}
else
{
for
(
int
i
=
0
;
i
<
outgo
.
tailNum
;
i
++
){
XTensor
*
parent
=
outgo
.
tails
[
i
];
if
(
parent
==
NULL
)
continue
;
XLink
&
parentOutgo
=
parent
->
outgo
;
bool
hit
=
false
;
for
(
int
j
=
0
;
j
<
parentOutgo
.
tailNum
;
j
++
){
if
(
parentOutgo
.
tails
[
j
]
==
root
){
hit
=
true
;
break
;
}
}
CheckNTErrors
(
hit
,
"Wrong outgoing edge!"
);
}
}
for
(
int
i
=
0
;
i
<
income
.
tailNum
;
i
++
){
XTensor
*
child
=
income
.
tails
[
i
];
CheckNetwork
(
child
);
}
}
/*
show the network encoded in a root node (tensor)
>> file - file to dump information
>> root - pointer to the root node
*/
void
XLink
::
ShowNetwork
(
FILE
*
file
,
XTensor
*
root
)
{
fprintf
(
file
,
"node %d - "
,
root
->
id
);
XLink
&
income
=
root
->
income
;
if
(
income
.
head
==
NULL
){
fprintf
(
file
,
"income[%d]: null "
,
income
.
tailNum
);
}
else
{
fprintf
(
file
,
"income[%d]: "
,
income
.
tailNum
);
for
(
int
i
=
0
;
i
<
income
.
tailNum
;
i
++
){
XTensor
*
child
=
income
.
tails
[
i
];
if
(
child
==
NULL
)
fprintf
(
file
,
"na "
);
else
fprintf
(
file
,
"%d "
,
child
->
id
);
}
}
XLink
&
outgo
=
root
->
outgo
;
if
(
outgo
.
head
==
NULL
){
fprintf
(
file
,
"outgo[%d]: null "
,
outgo
.
tailNum
);
}
else
{
fprintf
(
file
,
"outgo[%d]: "
,
income
.
tailNum
);
for
(
int
i
=
0
;
i
<
outgo
.
tailNum
;
i
++
){
XTensor
*
parent
=
outgo
.
tails
[
i
];
if
(
parent
==
NULL
)
fprintf
(
file
,
"na "
);
else
fprintf
(
file
,
"%d "
,
parent
->
id
);
}
}
fprintf
(
stderr
,
"
\n
"
);
for
(
int
i
=
0
;
i
<
income
.
tailNum
;
i
++
){
XTensor
*
child
=
income
.
tails
[
i
];
ShowNetwork
(
file
,
child
);
}
}
...
...
source/tensor/XLink.h
查看文件 @
f8ddb15b
...
...
@@ -133,6 +133,18 @@ struct XLink
/* replace a node with another, i.e., we redirect the links to the new node */
static
void
Replace
(
const
XTensor
*
oldOne
,
XTensor
*
newOne
);
/* copy links of a given node */
static
void
CopyIncoming
(
const
XTensor
*
reference
,
XTensor
*
target
);
/* check the correctness of the network encoded in a root node (tensor) */
static
void
CheckNetwork
(
XTensor
*
root
);
/* show the network encoded in a root node (tensor) */
static
void
ShowNetwork
(
FILE
*
file
,
XTensor
*
root
);
};
}
// namespace nts(NiuTrans.Tensor)
...
...
source/tensor/XTensor.cpp
查看文件 @
f8ddb15b
...
...
@@ -222,6 +222,13 @@ XTensor::XTensor(const XTensor &reference)
CopyValues
(
&
reference
,
this
);
}
if
(
reference
.
isTmp
)
XLink
::
Replace
(
&
reference
,
this
);
else
{
CheckNTErrors
(
outgo
.
tailNum
==
0
,
"The node has outgoing edge to other nodes!"
);
XLink
::
CopyIncoming
(
&
reference
,
this
);
}
isInit
=
false
;
isTmp
=
false
;
}
...
...
@@ -305,6 +312,8 @@ XTensor& XTensor::operator= (const XTensor& tensor)
isInit
=
true
;
isTmp
=
false
;
CheckNTErrors
(
outgo
.
tailNum
==
0
,
"The node has outgoing edge to other nodes!"
);
/* create tensor links for the new tensor */
XLink
::
Replace
(
&
tensor
,
this
);
...
...
source/tensor/core/math/ScaleAndShift.cpp
查看文件 @
f8ddb15b
...
...
@@ -104,7 +104,7 @@ XTensor ScaleAndShift(const XTensor &a, DTYPE scale, DTYPE shift)
_ScaleAndShift
(
&
a
,
&
b
,
scale
,
shift
);
/* tensor connections */
XLink
::
MakeLink
(
&
a
,
NULL
,
&
b
,
MATH_S
UM
);
XLink
::
MakeLink
(
&
a
,
NULL
,
&
b
,
MATH_S
CALEANDSHIFT
);
XLink
::
AddParamToHead
(
&
b
,
scale
);
XLink
::
AddParamToHead
(
&
b
,
shift
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论