OnehotAndIndex.cu 4.24 KB
Newer Older
1 2 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/* 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.
 */

/*
 * $Created by: Xu Chen (email: hello_master1954@163.com) 2018-07-31
 */

#include "OnehotAndIndex.cuh"
#include "../../XDevice.h"


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

#ifdef USE_CUDA

/* 
convert onehot tensor to index tensor (kernel version) 

>> onehotData - the data pointer of the onehot tensor
>> indexData - the data pointer of the index tensor
>> blockNum - the number of block
>> stride - stride of a data block
*/
__global__
void KernelOnehotToIndex(int * onehotData, int * indexData, int blockNum, int stride)
{
    /* block id */
    int i = blockDim.x * blockIdx.x + threadIdx.x;

    /* offset in each block */
    int offset = blockDim.y * blockIdx.y + threadIdx.y;

    if (i >= blockNum || offset >= stride)
        return;

    int * od = onehotData + i * stride;
    int * id = indexData + i;

    if (od[offset] != 0)
        *id = offset;
}

/* 
convert onehot tensor to index tensor (cuda version) 

>> onehot - onehot tensor, which value is 0 or 1
>> index - index tensor, which value is an integer num
>> size - the last dimension size of the onehot tensor
*/
void _CudaOnehotToIndex(XTensor * onehot, XTensor * index, int size)
{
    int devID = onehot->devID;

    int blockNum = index->unitNum;
    int stride = size;

    int cudaGrids[3];
    int cudaBlocks[3];

    int devIDBackup;
    ProtectCudaDev(devID, devIDBackup);

    GDevs.GetCudaThread2D(devID, blockNum, stride, MAX_INT, cudaGrids, cudaBlocks);

    dim3 blocks(cudaGrids[0], cudaGrids[1]);
    dim3 threads(cudaBlocks[0], cudaBlocks[1]);

    int * onehotData = (int *)onehot->data;
    int * indexData = (int *)index->data;

    KernelOnehotToIndex<<<blocks, threads >>>(onehotData, indexData, blockNum, stride);

    BacktoCudaDev(devID, devIDBackup);
}

/* 
convert index tensor to onehot tensor (kernel version) 

>> onehotData - the data pointer of the onehot tensor
>> indexData - the data pointer of the index tensor
>> blockNum - the number of block
>> stride - stride of a data block
*/
__global__
99
void KernelIndexToOnehot(DTYPE * onehotData, int * indexData, int blockNum, int stride, float confidence, float lowconfidence)
100 101 102 103 104 105 106 107 108 109
{
    /* block id */
    int i = blockDim.x * blockIdx.x + threadIdx.x;

    /* offset in each block */
    int offset = blockDim.y * blockIdx.y + threadIdx.y;

    if (i >= blockNum || offset >= stride)
        return;

110
    DTYPE * od = onehotData + i * stride;
111 112

    int id = indexData[i];
113 114 115 116 117 118 119 120

    //od[id] = 2.0;
    //onehotData[i * stride + id] = 0.1;
    if (offset == id)
        od[offset] = confidence;
    else{
        od[offset] = lowconfidence;
    }
121 122 123 124 125 126 127 128 129
}

/* 
convert index tensor to onehot tensor (cuda version) 

>> index - index tensor, which value is an integer num
>> onehot - onehot tensor, which value is 0 or 1
>> size - the last dimension size of the onehot tensor
*/
130
void _CudaIndexToOnehot(XTensor * index, XTensor * onehot, int size, float confidence, float lowconfidence)
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
{
    int devID = onehot->devID;

    int blockNum = index->unitNum;
    int stride = size;

    int cudaGrids[3];
    int cudaBlocks[3];

    int devIDBackup;
    ProtectCudaDev(devID, devIDBackup);

    GDevs.GetCudaThread2D(devID, blockNum, stride, MAX_INT, cudaGrids, cudaBlocks);

    dim3 blocks(cudaGrids[0], cudaGrids[1]);
    dim3 threads(cudaBlocks[0], cudaBlocks[1]);

148
    DTYPE * onehotData = (DTYPE *)onehot->data;
149 150
    int * indexData = (int *)index->data;

151
    KernelIndexToOnehot<<<blocks, threads >>>(onehotData, indexData, blockNum, stride, confidence, lowconfidence);
152 153 154 155 156 157

    BacktoCudaDev(devID, devIDBackup);
}

#endif // USE_CUDA

158
} // namespace nts(NiuTrans.Tensor)