T2TLayerNormal.cpp 2.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* NiuTrans.Tensor - an open-source tensor library
 * Copyright (C) 2018, 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.
 */

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

xiaotong committed
22
#include <math.h>
23
#include "T2TLayerNormal.h"
xiaotong committed
24 25
#include "T2TUtility.h"
#include "T2TEmbedding.h"
26
#include "../../tensor/core/CHeader.h"
27 28 29

namespace transformer
{
xiaotong committed
30

31 32 33 34
/* constructor */
T2TLN::T2TLN()
{
    devID = -1;
35 36
    mem = NULL;
    d = 0;
37 38 39 40 41 42 43 44 45 46
}

/* de-constructor */
T2TLN::~T2TLN()
{
}

/*
initialize the model
>> argc - number of arguments
xiaotong committed
47
>> argv - list of pointers to the arguments
48 49 50
>> myDevID - device id
>> myMem - the memory pool
*/
51
void T2TLN::InitModel(int argc, char ** argv, int myDevID, XMem * myMem)
52 53 54
{
    devID = myDevID;
    mem = myMem;
xiaotong committed
55

56
    d = 0;
xiaotong committed
57 58
    LoadParamInt(argc, argv, "d", &d, DEFAULT_EMBEDDING_SIZE);

59
    InitTensor1D(&w, d, X_FLOAT, devID, mem);
xiaotong committed
60 61
    InitTensor1D(&b, d, X_FLOAT, devID, mem);

xiaotong committed
62
    w.SetDataRand(1.0F, 1.0F);
xiaotong committed
63
    b.SetZeroAll();
64
}
65

66
/*
67
make the network
68
for each layer representation x, we have
69
y =
70 71 72
>> input - the input tensor
>> return - layer normalization output
*/
xiaotong committed
73
XTensor T2TLN::Make(XTensor &input)
74
{
xiaotong committed
75
    XTensor &x = input;
xiaotong committed
76
    XTensor xn;
77 78 79 80 81 82 83
    XTensor mean;
    XTensor variance;
    XTensor standard;
    XTensor meanFilled;
    XTensor standardFilled;

    /* \mu = (sum_i x_i)/m */
xiaotong committed
84
    mean = ReduceMean(x, x.order - 1);
85 86 87 88 89 90

    /* \sigma = (sum_i (x_i - \mu)^2)/m */
    variance = ReduceVariance(x, x.order - 1, mean);

    /* standard = sqrt(variance) */
    standard = Power(variance, 0.5F);
91 92

    /* unsqueeze mean and standard deviation to fit them into
xiaotong committed
93
       the same shape of x */
94 95 96 97
    meanFilled = Unsqueeze(mean, x.order - 1, x.GetDim(-1));
    standardFilled = Unsqueeze(standard, x.order - 1, x.GetDim(-1));

    /* x' = (x - \mu)/standard */
98
    xn = (x - meanFilled) / standardFilled;
xiaotong committed
99 100

    /* result = x' * w + b   */
101
    return xn * w + b;
102 103 104
}

}