Unsqueeze.cu 11.3 KB
Newer Older
xiaotong committed
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) 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: XIAO Tong (email: xiaotong@mail.neu.edu.cn) 2018-04-24
*/

22 23
#include "../../XDevice.h"
#include "../../XTensor.h"
xiaotong committed
24 25 26 27 28 29 30 31 32 33 34
#include "Unsqueeze.h"
#include "Unsqueeze.cuh"

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

#ifdef  USE_CUDA

/*
insert a dimension by copying the blocks for n times (where n is the size of the inerted dimension)
>> s - pointer to the source data array
>> blockSize - size of a block
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

>> totalSize - total size of the blocks (i.e., blockSIze * n)
>> t - pointer to the target data array
>> n - number of blocks to copy data
*/
template<class T>
__global__
void KernelUnsqueezeFlat(void * s, int blockSize, int totalSize, void * t, int n)
{
    /* index of data items */
    int i = blockDim.x * blockIdx.x + threadIdx.x;

    if (i >= blockSize)
        return;

    T value = ((T*)s)[i];
    T * tData = (T*)t;

    __syncthreads();

    for (int k = i; k < totalSize; k += blockSize)
        tData[k] = value;
}

/*
insert a dimension by copying the blocks for n times (where n is the size of the inerted dimension)
>> s - pointer to the source data array
>> blockSize - size of a block

>> totalSize - total size of the blocks (i.e., blockSIze * n)
>> t - pointer to the target data array
>> n - number of blocks to copy data
*/
template<class T>
__global__
void KernelUnsqueezeFlatBigram(void * s, int blockSize, int totalSize, void * t, int n)
{
    /* index of data items */
    int i = (blockDim.x * blockIdx.x + threadIdx.x) * 2;

    if (i >= blockSize)
        return;

    T value = ((T*)s)[i];
    T value2 = ((T*)s)[i + 1];
    T * tData = (T*)t;

    __syncthreads();

    for (int k = i; k < totalSize; k += blockSize){
        tData[k] = value;
        tData[k + 1] = value2;
    }
}

/*
insert a dimension by copying the blocks for n times (where n is the size of the inerted dimension)
>> s - pointer to the source data array
>> blockSize - size of a block

>> totalSize - total size of the blocks (i.e., blockSIze * n)
>> t - pointer to the target data array
>> n - number of blocks to copy data
*/
template<class T>
__global__
void KernelUnsqueezeFlat2D(void * s, int blockSize, int totalSize, void * t, int n)
{
    __shared__ T data[MAX_CUDA_THREAD_NUM_PER_BLOCK];
    __shared__ int offsets[MAX_CUDA_THREAD_NUM_PER_BLOCK];

    /* index of data items */
    int i = blockDim.x * blockIdx.x + threadIdx.x;

    /* index of data items */
    int j = blockDim.y * blockIdx.y + threadIdx.y;

    if (i >= blockSize || j >= n)
        return;

    if(threadIdx.y == 0)
        data[threadIdx.x] = ((T*)s)[i];
    if(threadIdx.x == 0)
        offsets[threadIdx.y] = blockSize * j;

    __syncthreads();

    ((T*)t)[offsets[threadIdx.y] + i] = data[threadIdx.x];
}

/*
insert a dimension by copying the blocks for n times (where n is the size of the inerted dimension)
>> s - pointer to the source data array
>> blockSize - size of a block
xiaotong committed
129
>> blockNum - number of the blocks
130
>> totalSize - total size of the blocks (i.e., blockSize * n)
xiaotong committed
131
>> t - pointer to the target data array
132
>> n - number of blocks to copy data
xiaotong committed
133 134 135
*/
template<class T>
__global__
136
void KernelUnsqueeze(void * s, int blockSize, int blockNum, int totalSize, void * t, int n)
xiaotong committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
{
    /* index of data items */
    int i = blockDim.x * blockIdx.x + threadIdx.x;

    /* block index */
    int j = blockDim.y * blockIdx.y + threadIdx.y;

    if (i >= blockSize || j >= blockNum)
        return;

    MTYPE offset = blockSize * j;
    T value = ((T*)s)[offset + i];
    T * tData = (T*)t + offset * n;

    __syncthreads();

153
    for (int k = i; k < totalSize; k += blockSize)
xiaotong committed
154 155 156 157
        tData[k] = value;
}

/*
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 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 219 220 221 222 223 224 225 226
insert a dimension by copying the blocks for n times (where n is the size of the inerted dimension)
This is special case where we actually copy a v-dimentional column vector by n times to form a v * n matrix
>> s - pointer to the source data array
>> rowNum - number of rows (i.e., dimension size of s)
>> colNum - number of columns (i.e., number of copies)
>> t - pointer to the target data array
*/
template<class T>
__global__
void KernelUnsqueezeByCol(void * s, int rowNum, int colNum, void * t)
{
    __shared__ T values[MAX_CUDA_THREAD_NUM_PER_BLOCK];
    __shared__ T * ts[MAX_CUDA_THREAD_NUM_PER_BLOCK];

    /* column index */
    int i = blockDim.x * blockIdx.x + threadIdx.x;

    /* row index */
    int j = blockDim.y * blockIdx.y + threadIdx.y;

    if (i >= colNum || j >= rowNum)
        return;

    if(threadIdx.x == 0){
        values[threadIdx.y] = ((T*)s)[j];
        ts[threadIdx.y] = (T*)t + colNum * j;
    }

    __syncthreads();

    ts[threadIdx.y][i] = values[threadIdx.y];
}

/*
insert a dimension by copying the blocks for n times (where n is the size of the inerted dimension)
This is special case where we actually copy a v-dimentional column vector by n times to form a v * n matrix
And a row is very big so that it occupies the cuda threads in a block
>> s - pointer to the source data array
>> rowNum - number of rows (i.e., dimension size of s)
>> colNum - number of columns (i.e., number of copies)
>> t - pointer to the target data array
*/
template<class T>
__global__
void KernelUnsqueezeByColBigRow(void * s, int rowNum, int colNum, void * t)
{
    __shared__ T value;
    __shared__ T * tData;

    /* column index */
    int i = blockDim.x * blockIdx.x + threadIdx.x;

    /* row index */
    int j = blockDim.y * blockIdx.y + threadIdx.y;

    if (i >= colNum || j >= rowNum)
        return;

    if (threadIdx.x == 0) {
        value = ((T*)s)[j];
        tData = (T*)t + colNum * j;
    }

    __syncthreads();

    tData[i] = value;
}

/*
xiaotong committed
227 228 229 230 231 232
insert a dimension by copying the blocks for x times (where x is the size of the inerted dimension)
>> a - input tensor
>> b - output tensor
>> dim - where to insert the dimension
>> dSize - size of the newly-inserted dimension
*/
233
void _CudaUnsqueeze(const XTensor * a, XTensor * b, int dim, int dSize)
xiaotong committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
{
    int blockSize = 1;
    int blockNumA = 1;
    int blockNumB = 1;
    int dimRDI = b->order - dim - 1;
    for (int i = 0; i < dimRDI; i++)
        blockSize *= a->dimSizeRDI[i];

    blockNumA = a->unitNum / blockSize;
    blockNumB = b->unitNum / blockSize;

    CheckNTErrors((blockNumA * dSize == blockNumB), "Unmatched tensors!");;

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

    int devIDBackup = 0;
    ProtectCudaDev(a->devID, devIDBackup);

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    if (dimRDI == 0) {
        GDevs.GetCudaThread2D(a->devID, dSize, blockNumA, MAX_INT, cudaGrids, cudaBlocks);

        if (a->dataType == X_FLOAT && b->dataType == X_FLOAT) {
            if (cudaBlocks[1] == 1)
                KernelUnsqueezeByColBigRow<float> << <dim3(cudaGrids[0], cudaGrids[1]), dim3(cudaBlocks[0], cudaBlocks[1]) >> >
                                                     (a->data, blockNumA, dSize, b->data);
            else
                KernelUnsqueezeByCol<float> << <dim3(cudaGrids[0], cudaGrids[1]), dim3(cudaBlocks[0], cudaBlocks[1]) >> >
                                               (a->data, blockNumA, dSize, b->data);
        }
        else if (a->dataType == X_INT && b->dataType == X_INT) {
            if (cudaBlocks[1] == 1)
                KernelUnsqueezeByColBigRow<int> << <dim3(cudaGrids[0], cudaGrids[1]), dim3(cudaBlocks[0], cudaBlocks[1]) >> >
                                                   (a->data, blockNumA, dSize, b->data);
            else
                KernelUnsqueezeByCol<int> << <dim3(cudaGrids[0], cudaGrids[1]), dim3(cudaBlocks[0], cudaBlocks[1]) >> >
                                             (a->data, blockNumA, dSize, b->data);
        }
        else {
            ShowNTErrors("TODO!");
        }

        
    }
    else if(blockNumA > 1){
279 280
        GDevs.GetCudaThread2D(a->devID, blockSize, blockNumA, MAX_INT, cudaGrids, cudaBlocks);

281
        if (a->dataType == X_FLOAT && b->dataType == X_FLOAT) {
282 283 284
            KernelUnsqueeze<float> << <dim3(cudaGrids[0], cudaGrids[1]), dim3(cudaBlocks[0], cudaBlocks[1]) >> >
                                      (a->data, blockSize, blockNumA, blockSize * dSize, b->data, dSize);
        }
285
        else if (a->dataType == X_INT && b->dataType == X_INT) {
286 287 288 289 290 291 292 293 294 295
            KernelUnsqueeze<int> << <dim3(cudaGrids[0], cudaGrids[1]), dim3(cudaBlocks[0], cudaBlocks[1]) >> >
                                    (a->data, blockSize, blockNumA, blockSize * dSize, b->data, dSize);
        }
        else {
            ShowNTErrors("TODO!");
        }
    }
    else if(blockNumA == 1 && blockSize < MAX_CUDA_THREAD_NUM_PER_BLOCK){
        GDevs.GetCudaThread2D(a->devID, blockSize, dSize, MAX_CUDA_THREAD_NUM_PER_BLOCK/4, cudaGrids, cudaBlocks);

296
        if (a->dataType == X_FLOAT && b->dataType == X_FLOAT) {
297 298 299
            KernelUnsqueezeFlat2D<float> << <dim3(cudaGrids[0], cudaGrids[1]), dim3(cudaBlocks[0], cudaBlocks[1]) >> >
                                          (a->data, blockSize, blockSize * dSize, b->data, dSize);
        }
300
        else if (a->dataType == X_INT && b->dataType == X_INT) {
301 302 303 304 305 306 307 308 309 310
            KernelUnsqueezeFlat2D<int> << <dim3(cudaGrids[0], cudaGrids[1]), dim3(cudaBlocks[0], cudaBlocks[1]) >> >
                                        (a->data, blockSize, blockSize * dSize, b->data, dSize);
        }
        else {
            ShowNTErrors("TODO!");
        }
    }
    else if(blockNumA == 1 && blockSize % 2 == 0){
        GDevs.GetCudaThread(a->devID, blockSize/2, cudaGrids, cudaBlocks);

311
        if (a->dataType == X_FLOAT && b->dataType == X_FLOAT) {
312 313 314
            KernelUnsqueezeFlatBigram<float> << <dim3(cudaGrids[0]), dim3(cudaBlocks[0]) >> >
                                                (a->data, blockSize, blockSize * dSize, b->data, dSize);
        }
315
        else if (a->dataType == X_INT && b->dataType == X_INT) {
316 317 318 319 320 321
            KernelUnsqueezeFlatBigram<int> << <dim3(cudaGrids[0]), dim3(cudaBlocks[0]) >> >
                                              (a->data, blockSize, blockSize * dSize, b->data, dSize);
        }
        else {
            ShowNTErrors("TODO!");
        }
xiaotong committed
322
    }
323 324 325
    else if(blockNumA == 1){
        GDevs.GetCudaThread(a->devID, blockSize, cudaGrids, cudaBlocks);

326
        if (a->dataType == X_FLOAT && b->dataType == X_FLOAT) {
327 328 329
            KernelUnsqueezeFlat<float> << <dim3(cudaGrids[0]), dim3(cudaBlocks[0]) >> >
                                          (a->data, blockSize, blockSize * dSize, b->data, dSize);
        }
330
        else if (a->dataType == X_INT && b->dataType == X_INT) {
331 332 333 334 335 336
            KernelUnsqueezeFlat<int> << <dim3(cudaGrids[0]), dim3(cudaBlocks[0]) >> >
                                        (a->data, blockSize, blockSize * dSize, b->data, dSize);
        }
        else {
            ShowNTErrors("TODO!");
        }
xiaotong committed
337
    }
338 339
    else{
        ShowNTErrors("Something is wrong!");
xiaotong committed
340 341 342 343 344 345 346
    }

    BacktoCudaDev(a->devID, devIDBackup);
}

#endif // USE_CUDA
} // namespace nts(NiuTrans.Tensor)