XLink.cpp 19.9 KB
Newer Older
1
/* NiuTrans.Tensor - an open-source tensor library
2
 * Copyright (C) 2018, Natural Language Processing Lab, Northeastern University.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * 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.
 */

/*
 * $Created by: XIAO Tong (xiaotong@mail.neu.edu.cn) 2018-07-04
 */

#include <stdio.h>
#include "XLink.h"
#include "XName.h"

namespace nts{ // namespace nts(NiuTrans.Tensor)

28
int XLink::paramSize = PARAM_UNTI_SIZE;
29 30 31 32 33 34 35 36 37 38 39

/* constuctor */
XLink::XLink()
{
    head   = NULL;
    tails  = NULL;
    params = NULL;
    tailNum  = 0;
    paramNum = 0;
    type[0] = 0;
    typeID = 0;
40
    caculator = NULL;
41 42 43 44 45
}
    
/* deconstructor */
XLink::~XLink()
{
46 47 48 49
    if(tails != NULL)
        delete[] tails;
    if(params != NULL)
        delete[] (char*)params;
50 51 52 53 54 55 56 57 58 59 60 61 62
}

/* reset it */
void XLink::Reset()
{
    delete[] tails;
    delete[] (char*)params;
    head   = NULL;
    tails  = NULL;
    params = NULL;
    tailNum  = 0;
    paramNum = 0;
    type[0]  = 0;
63 64
    typeID   = 0;
    caculator = NULL;
65 66 67 68 69 70 71 72 73
}

/* clear it */
void XLink::Clear()
{
    head   = NULL;
    tailNum  = 0;
    paramNum = 0;
    type[0]  = 0;
74 75
    typeID   = 0;
    caculator = NULL;
76 77 78 79 80 81 82 83 84
}

/* reset tails */
void XLink::ClearTail()
{
    tailNum = 0;
}

/*
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
clear the outgoing node list of tensor node
>> node - the node to be cleared
*/
void XLink::ClearOutgoing(XTensor * node)
{
    if(node == NULL)
        return;
    
    XLink &outgo = node->outgo;
    
    for(int i = 0; i < outgo.tailNum; i++){
        
        /* for each parent node */
        XTensor * parent = outgo.tails[i];
        XLink &parentIncome = parent->income;
        
        CheckNTErrors(parentIncome.tailNum > 0, "The node must have incoming edges!");
        
        /* we check for each parent node and remove the link to current node */
        for(int j = 0; j < parentIncome.tailNum; j++){
            if(parentIncome.tails[j] == node){
                memcpy(parentIncome.tails + j, parentIncome.tails + j + 1,
                       sizeof(XTensor*) * (parentIncome.tailNum - 1 - j));
                parentIncome.tailNum--;
                break;
            }
        }
    }
    
    outgo.ClearTail();
xiaotong committed
115 116
    outgo.typeID = 0;
    outgo.type[0] = 0;
117 118
    delete[] (char*)outgo.params;
    outgo.params = NULL;
119 120 121
}

/*
122 123 124 125 126 127 128 129 130 131 132 133
clear the incoming node list of tensor node
>> node - the node to be cleared
*/
void XLink::ClearIncoming(XTensor * node)
{
    if(node == NULL)
        return;

    XLink &income = node->income;

    for(int i = 0; i < income.tailNum; i++){

134
        /* for each incoming node */
135 136 137 138 139 140 141 142
        XTensor * child = income.tails[i];
        XLink &childOutgo = child->outgo;

        CheckNTErrors(childOutgo.tailNum > 0, "The node must have outgoing edges!");

        /* we check for each child node and remove the link to current node */
        for(int j = 0; j < childOutgo.tailNum; j++){
            if(childOutgo.tails[j] == node){
143 144
                memcpy(childOutgo.tails + j, childOutgo.tails + j + 1,
                       sizeof(XTensor*) * (childOutgo.tailNum - 1 - j));
145 146 147 148 149 150 151 152 153 154
                childOutgo.tailNum--;
                break;
            }
        }

        if(child->isTmp && childOutgo.tailNum == 0)
            delete child;
    }

    income.ClearTail();
xiaotong committed
155 156
    income.typeID = 0;
    income.type[0] = 0;
157 158
    delete[] (char*)income.params;
    income.params = NULL;
159 160 161 162 163 164 165 166 167 168 169
}

/* 
set edge type name 
>> id - id of the type
*/
void XLink::SetType(int id)
{
    type[0] = 0;
    strcpy(type, GetOPName(id));
    typeID = id;
170 171 172
    if(id != 0){
        CheckNTErrors(strcmp(type, "NULL"), "illegal edge type name!");
    }
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
}

/* 
set head 
>> h - pointer to the head tensor
*/
void XLink::SetHead(XTensor * h)
{
    head = h;
}

/* 
add a tail
>> t - pointer to the tail tensor
*/
void XLink::AddTail(XTensor * t)
{
    XTensor ** ts = tails;
    tails = new XTensor*[tailNum + 1];
    memcpy(tails, ts, sizeof(XTensor*) * tailNum);
    tails[tailNum++] = t;
    delete[] ts;
}

/* 
add two tails in one time 
>> t1 - pointer to the tail tensor
>> t2 - pointer to another tail tensor
*/
void XLink::AddTwoTails(XTensor * t1, XTensor * t2)
{
    XTensor ** ts = tails;
    tails = new XTensor*[tailNum + 2];
    memcpy(tails, ts, sizeof(XTensor*) * tailNum);
    tails[tailNum++] = t1;
    tails[tailNum++] = t2;
    delete[] ts;
}

/* 
add a parameter 
>> param - parameter in default type
*/
void XLink::AddParam(DTYPE param)
{
    void * ps = params;
219
    params = new char[(paramNum + 1) * paramSize];
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    memcpy(params, ps, paramNum * paramSize);
    DTYPE * p = (DTYPE*)((char*)params + paramNum * paramSize);
    *p = param;
    paramNum++;
    delete[] (char*)ps;
}

/* 
add a parameter 
>> param - pointer to the parameter
>> size - size of the parameter
*/
void XLink::AddParam(void * param, int size)
{
    void * ps = params;
235
    params = new char[(paramNum + 1) * paramSize];
236 237 238 239 240 241
    memcpy(params, ps, paramNum * paramSize);
    char * p = (char*)params + paramNum * paramSize;
    memcpy(p, param, size);
    paramNum++;
    delete[] (char*)ps;
}
242

243
/* 
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
get a paramter in default type 
>> i - id of the parameter
<< return - the parameter in default type
*/
DTYPE XLink::GetParam(int i)
{
    CheckNTErrors(params != NULL, "parameter array cannot be empty!");
    char * p = (char*)params + i * paramSize;
    return *(DTYPE*)p;
}

/* 
get a paramter in integer 
>> i - id of the parameter
<< return - the parameter in integer
*/
int XLink::GetParamInt(int i)
{
    CheckNTErrors(params != NULL, "parameter array cannot be empty!");
    char * p = (char*)params + i * paramSize;
    return *(int*)p;
}
266 267 268 269 270 271 272 273 274 275 276 277

/* 
get a paramter in integer 
>> i - id of the parameter
<< return - the parameter in integer
*/
void * XLink::GetParamPointer(int i)
{
    CheckNTErrors(params != NULL, "parameter array cannot be empty!");
    char * p = (char*)params + i * paramSize;
    return *(int **)p;
}
278 279 280 281 282 283 284 285 286 287 288 289 290 291
    
/*
get a parameter in MATRIX_TRANS_TYPE
>> i - id of the parameter
<< return - the parameter in MATRIX_TRANS_TYPE
*/
MATRIX_TRANS_TYPE XLink::GetParamTrans(int i)
{
    CheckNTErrors(params != NULL, "parameter array cannot be empty!");
    char * p = (char*)params + i * paramSize;
    return *(MATRIX_TRANS_TYPE*)p;
}

/* 
292 293 294 295 296 297 298 299 300 301 302
create a hyperedge with two input tensors and a output tensor 
>> t1 - a tail tensor
>> t2 - another tail tensor
>> h - head tensor
>> id - id of the edge type
*/
void XLink::MakeLink(const XTensor * t1, const XTensor * t2, XTensor * h, int id)
{
    if(h == NULL)
        return;
    
xuchen committed
303 304 305
    if (!t1->enableGrad)
        return;

306 307 308
    TensorList list(2);
    list.Add((XTensor*)t1);
    list.Add((XTensor*)t2);
309 310 311 312

    MakeLink(&list, h, id);
}

313 314 315 316 317 318 319 320 321 322 323 324 325
/*
create a hyperedge with two input tensors and a output tensor
>> t1 - a tail tensor
>> t2 - the second tail tensor
>> t3 - the third tail tensor
>> h - head tensor
>> id - id of the edge type
*/
void XLink::MakeLink(const XTensor * t1, const XTensor * t2, const XTensor * t3,XTensor * h, int id)
{
    if (h == NULL)
        return;

xuchen committed
326 327 328
    if (!t1->enableGrad || !t2->enableGrad)
        return;
    
329 330 331 332
    TensorList list(3);
    list.Add((XTensor*)t1);
    list.Add((XTensor*)t2);
    list.Add((XTensor*)t3);
333 334 335 336

    MakeLink(&list, h, id);
}

337 338 339 340 341 342
/* 
create a hyper edge with a list of tensors and a output tensor 
>> list - a list of input tensors
>> h - head tensor
>> id - id of the edge type
*/
343
void XLink::MakeLink(const TensorList * list, XTensor * h, int id)
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
{
    /* forward */
    XLink &income = h->income;
    income.Reset();
    income.SetHead(h);
    income.SetType(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 == NULL || outgo.head == t, 
365
                     "Wrong head of the hyperedge!");
366
        outgo.SetHead(t);
367 368 369 370 371
        outgo.AddTail(h);
    }
}

/* 
372 373 374 375 376
create a hyper edge with a input tensors and a list of output tensors
>> h - a input tensor
>> list - a list of output tensors
>> id - id of the edge type
*/
377
void XLink::MakeLink(XTensor * t, TensorList * list, int id)
378
{
xuchen committed
379 380 381
    if (!t->enableGrad)
        return;

382 383 384 385 386 387 388 389 390 391 392 393 394 395
    /* forward */
    for(int i = 0; i < list->count; i++){
        XTensor * h = (XTensor*)list->GetItem(i);
        if(h == NULL)
            continue;
        XLink &income = h->income;
        income.Reset();
        income.SetHead(h);
        income.SetType(id);
        income.AddTail(t);
    }

    /* backward */
    XLink &outgo = t->outgo;
396
    outgo.SetHead(t);
397 398 399 400 401 402 403 404 405 406
    CheckNTErrors(outgo.head == NULL || outgo.head == t, "Wrong head of the hyperedge!");
    for(int i = 0; i < list->count; i++){
        XTensor * t = (XTensor*)list->GetItem(i);
        if(t == NULL)
            continue;
        outgo.AddTail(t);
    }
}

/* 
407 408 409 410 411 412
add parameters 
>> h - head
>> param - parameter we want introduce
*/
void XLink::AddParamToHead(XTensor * h, DTYPE param)
{
413
    CheckNTErrors(h != NULL, "head tensor cannot be empty!");
414 415 416 417 418 419 420 421 422 423
    h->income.AddParam(param);
}

/* 
add an integer parameter 
>> h - head
>> param - parameter we want introduce
*/
void XLink::AddParamToHeadInt(XTensor * h, int param)
{
424
    CheckNTErrors(h != NULL, "head tensor cannot be empty!");
425 426 427 428
    h->income.AddParam(&param, sizeof(int));
}

/* 
429 430 431 432 433 434
add a MATRIX_TRANS_TYPE parameter 
>> h - head
>> param - parameter we want introduce
*/
void XLink::AddParamToHeadTrans(XTensor * h, MATRIX_TRANS_TYPE param)
{
435
    CheckNTErrors(h != NULL, "head tensor cannot be empty!");
436 437 438 439 440 441 442 443 444 445
    h->income.AddParam(&param, sizeof(MATRIX_TRANS_TYPE));
}

/* 
add a boolean parameter 
>> h - head
>> param - parameter we want introduce
*/
void XLink::AddParamToHeadBool(XTensor * h, bool param)
{
446
    CheckNTErrors(h != NULL, "head tensor cannot be empty!");
447 448 449 450 451 452 453 454 455 456
    h->income.AddParam(&param, sizeof(bool));
}

/* 
add a pointer parameter 
>> h - head
>> param - parameter we want introduce
*/
void XLink::AddParamToHeadPointer(XTensor * h, void * param)
{
457
    CheckNTErrors(h != NULL, "head tensor cannot be empty!");
458 459 460 461 462
    h->income.AddParam(&param, sizeof(param));
}


/* 
463 464 465 466 467 468 469 470
replace a node with another, i.e., we redirect the links to the new node 
>> oldOne - the node to be replaced
>> newOne - the new node
*/
void XLink::Replace(const XTensor * oldOne, XTensor * newOne)
{
    if(oldOne == NULL || newOne == NULL)
        return;
471 472 473
    
    XLink &newIncome = newOne->income;
    XLink &newOutgo  = newOne->outgo;
474

475
    XLink::ClearOutgoing(newOne);
476 477
    XLink::ClearIncoming(newOne);

478
    /* incoming nodes */
xiaotong committed
479 480 481 482
    if(oldOne->income.typeID != 0){
        if(newIncome.tailNum < oldOne->income.tailNum){
            delete[] newIncome.tails;
            newIncome.tails = new XTensor*[oldOne->income.tailNum];
483
        }
xiaotong committed
484 485 486 487 488 489
        
        newIncome.SetType(oldOne->income.typeID);
        newIncome.head = newOne;
        newIncome.tailNum = oldOne->income.tailNum;
        memcpy(newIncome.tails, oldOne->income.tails, sizeof(XTensor*) * newIncome.tailNum);

490 491 492 493 494
        int paraArraySize = oldOne->income.paramNum * oldOne->income.paramSize;
        newIncome.params = new char[paraArraySize];
        memcpy(newIncome.params, oldOne->income.params, paraArraySize);
        newIncome.paramNum = oldOne->income.paramNum;

xiaotong committed
495 496 497 498 499 500 501 502 503 504 505 506
        /* update the link to each child node */
        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;
                }
            }
507

xiaotong committed
508 509 510
            if(childOutgo.tailNum > 0){
                CheckNTErrors(hit, "No proper node found in child.outgo edge!");
            }
511 512
        }
    }
513 514 515 516 517
    
    if(newOutgo.tailNum < oldOne->outgo.tailNum){
        delete[] newOutgo.tails;
        newOutgo.tails = new XTensor*[oldOne->outgo.tailNum];
    }
518

519
    /* outgoing nodes */
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
    newOutgo.head = newOne;
    newOutgo.tailNum = oldOne->outgo.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!");
        }
    }
}

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623

/*
copy a node with another, i.e., we add the links to the new node
>> src - the node to be copied
>> tgt - the new node
*/
void XLink::Copy(const XTensor * reference, XTensor * target)
{
    if (reference == NULL || target == NULL)
        return;

    XLink &newIncome = target->income;
    XLink &newOutgo = target->outgo;

    XLink::ClearOutgoing(target);
    XLink::ClearIncoming(target);

    /* incoming nodes */
    if (reference->income.typeID != 0) {
        if (newIncome.tailNum < reference->income.tailNum) {
            delete[] newIncome.tails;
            newIncome.tails = new XTensor*[reference->income.tailNum];
        }

        newIncome.SetType(reference->income.typeID);
        newIncome.head = target;
        newIncome.tailNum = reference->income.tailNum;
        memcpy(newIncome.tails, reference->income.tails, sizeof(XTensor*) * newIncome.tailNum);

        int paraArraySize = reference->income.paramNum * reference->income.paramSize;
        newIncome.params = new char[paraArraySize];
        memcpy(newIncome.params, reference->income.params, paraArraySize);
        newIncome.paramNum = reference->income.paramNum;

        /* update the link to each child node */
        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] == reference) {
                    //childOutgo.tails[j] = target;
                    childOutgo.AddTail(target);
                    hit = true;
                    break;
                }
            }

            if (childOutgo.tailNum > 0) {
                CheckNTErrors(hit, "No proper node found in child.outgo edge!");
            }
        }
    }

    if (newOutgo.tailNum < reference->outgo.tailNum) {
        delete[] newOutgo.tails;
        newOutgo.tails = new XTensor*[reference->outgo.tailNum];
    }

    /* outgoing nodes */
    newOutgo.head = target;
    newOutgo.tailNum = reference->outgo.tailNum;
    memcpy(newOutgo.tails, reference->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] == reference) {
                //parentIncome.tails[j] = target;
                parentIncome.AddTail(target);
                hit = true;
            }
        }

        if (parentIncome.tailNum > 0) {
            CheckNTErrors(hit, "No proper node found in parent.income edge!");
        }
    }
}
624 625 626 627 628 629 630 631 632 633 634 635
/* 
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;
636
    TensorList tails(tailNum);
637 638 639 640 641
    for(int i = 0; i < tailNum; i++){
        XTensor * tail = (XTensor*)reference->income.tails[i];
        tails.Add(tail);
    }

642
    MakeLink(&tails, target, reference->income.typeID);
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688

    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;
689
            XLink & parentIncome = parent->income;
690
            bool hit = false;
691 692
            for(int j = 0; j < parentIncome.tailNum; j++){
                if(parentIncome.tails[j] == root){
693 694 695 696
                    hit = true;
                    break;
                }
            }
697
            CheckNTErrors(hit, "Wrong incoming edge!");
698 699 700 701 702 703 704 705 706 707
        }
    }

    for(int i = 0; i < income.tailNum; i++){
        XTensor * child = income.tails[i];
        CheckNetwork(child);
    }
}

/* 
708 709 710 711 712 713 714 715 716
show a node 
>> file - file to dump information
>> root - pointer to the node
*/
void XLink::ShowNode(FILE * file, XTensor * node)
{
    fprintf(file, "node %d - ", node->id);

    XLink &income = node->income;
717 718 719 720
    if(income.head == NULL){
        fprintf(file, "income[%d]: null ", income.tailNum);
    }
    else{
721
        fprintf(file, "income[%d, %s]: ", income.tailNum, GetOPName(income.typeID));
722 723 724 725 726 727 728 729
        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);
        }
    }
730
    fprintf(stderr, ", ");
731

732
    XLink &outgo = node->outgo;
733
    if(outgo.head == NULL || outgo.tailNum == 0){
734 735 736
        fprintf(file, "outgo[%d]: null ", outgo.tailNum);
    }
    else{
737
        fprintf(file, "outgo[%d]: ", outgo.tailNum);
738 739 740 741 742 743 744 745 746 747 748
        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");
}
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771

/* 
search for a node in a top-down manner by its name 
>> top - the top most node
<< return - the node we found
*/
XTensor * XLink::SearchNode(XTensor * top, const char * name)
{
    if(!strcmp(top->name, name))
        return top;

    XLink &incoming = top->income;

    for(int i = 0; i < incoming.tailNum; i++){
        XTensor * child = incoming.tails[i];
        XTensor * hit = SearchNode(child, name);
        if(hit != NULL)
            return hit;
    }

    return NULL;
}

772 773 774
    
} // namespace nts(NiuTrans.Tensor)