opr_impl.cpp 9.5 KB
Newer Older
1 2 3 4
/**
 * \file dnn/src/cuda/rng/opr_impl.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
6 7 8 9 10 11 12 13 14 15
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

#include "src/common/utils.h"
#include "src/cuda/handle.h"
#include "src/cuda/utils.h"
#include "./opr_impl.h"
16
#include "./kernel.cuh"
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 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

using namespace megdnn;
using namespace cuda;

namespace {
    const char *status2str(curandStatus_t status) {
        switch (status) {
#define C(v) case v: return #v
            C(CURAND_STATUS_SUCCESS);
            C(CURAND_STATUS_VERSION_MISMATCH);
            C(CURAND_STATUS_NOT_INITIALIZED);
            C(CURAND_STATUS_ALLOCATION_FAILED);
            C(CURAND_STATUS_TYPE_ERROR);
            C(CURAND_STATUS_OUT_OF_RANGE);
            C(CURAND_STATUS_LENGTH_NOT_MULTIPLE);
            C(CURAND_STATUS_DOUBLE_PRECISION_REQUIRED);
            C(CURAND_STATUS_LAUNCH_FAILURE);
            C(CURAND_STATUS_PREEXISTING_FAILURE);
            C(CURAND_STATUS_INITIALIZATION_FAILED);
            C(CURAND_STATUS_ARCH_MISMATCH);
            C(CURAND_STATUS_INTERNAL_ERROR);
#undef C
        }
        return "unknown";
    }
#define CURAND_CHECK(expr) \
    do { \
            curandStatus_t status = (expr); \
            MEGDNN_MARK_USED_VAR(&status2str); \
            if (status != CURAND_STATUS_SUCCESS) { \
                megdnn_throw(ssprintf( \
                            "curand call failed: status=%d(%s) call=%s", \
                            status, status2str(status), # expr)); \
        } \
    } while (0)

} // anonymouse namespace

CuRandHandle::CuRandHandle(cudaStream_t stream, uint64_t seed) {
    CURAND_CHECK(curandCreateGenerator(&m_gen, CURAND_RNG_PSEUDO_DEFAULT));
    CURAND_CHECK(curandSetStream(m_gen, stream));
    this->seed(seed);
}

CuRandHandle::~CuRandHandle() {
    if (curandDestroyGenerator(m_gen) != CURAND_STATUS_SUCCESS) {
        megdnn_trap();
    }
}

void CuRandHandle::seed(uint64_t seed) {
    CURAND_CHECK(curandSetPseudoRandomGeneratorSeed(m_gen, seed));
    m_seed = seed;
}

UniformRNGImpl::UniformRNGImpl(Handle *handle):
    UniformRNG(handle),
    m_curand_handle(cuda_stream(handle))
{
}

void UniformRNGImpl::exec(
        _megdnn_tensor_inout dst, _megdnn_workspace workspace) {
    check_exec(dst.layout, workspace.size);
    megdnn_assert(dst.layout.dtype == dtype::Float32(),
            "only float32 supported");
    m_curand_handle.ensure_seed(m_param.seed);
    CURAND_CHECK(curandGenerateUniform(m_curand_handle.gen(),
                dst.ptr<dt_float32>(), dst.layout.total_nr_elems()));
}

GaussianRNGImpl::GaussianRNGImpl(Handle *handle):
    GaussianRNG(handle),
    m_curand_handle(cuda_stream(handle))
{
}

void GaussianRNGImpl::exec(
        _megdnn_tensor_inout dst, _megdnn_workspace workspace) {
    check_exec(dst.layout, workspace.size);
    megdnn_assert(dst.layout.dtype == dtype::Float32(),
            "only float32 supported");
    auto ptr = dst.ptr<dt_float32>();
    auto size = dst.layout.total_nr_elems();
    megdnn_assert(size);
    m_curand_handle.ensure_seed(m_param.seed);
    auto gen = m_curand_handle.gen();
    if (size % 2) {
        auto wk = workspace.ptr<dt_float32>();
        CURAND_CHECK(curandGenerateNormal(gen, wk, 2, m_param.mean,
                    m_param.std));
        cuda_check(cudaMemcpyAsync(
                    ptr + size - 1, wk, sizeof(dt_float32), cudaMemcpyDeviceToDevice,
                    cuda_stream(handle())));
        -- size;
    }

    if (size) {
        CURAND_CHECK(curandGenerateNormal(
                    gen, ptr, size, m_param.mean, m_param.std));
    }
}

size_t GaussianRNGImpl::get_workspace_in_bytes(const TensorLayout &layout) {
    if (layout.total_nr_elems() % 2)
        return 2 * layout.dtype.size();
    return 0;
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
GammaRNGImpl::GammaRNGImpl(Handle *handle):
    GammaRNG(handle),
    m_seed(0),
    m_offset(0),
    m_stream(cuda_stream(handle))
{
}

void GammaRNGImpl::exec(_megdnn_tensor_in shape, _megdnn_tensor_in scale,
        _megdnn_tensor_inout dst, _megdnn_workspace workspace) {
    check_exec(shape.layout, scale.layout ,dst.layout, workspace.size);
    auto size = dst.layout.total_nr_elems();
    megdnn_assert(size);
    ensure_seed(m_param.seed);
    ElemwiseOpParamN<0> ele_param(size);
    switch (dst.layout.dtype.enumv()){
#define cb(_dt)                                                                           \
        case DTypeTrait<_dt>::enumv:                                                      \
        {                                                                                 \
            using ctype = DTypeTrait<_dt>::ctype;                                         \
            run_elemwise<random::GammaKernel<ctype>, ctype, 0>(ele_param, m_stream,       \
                                                {dst, shape, scale, m_seed, m_offset});   \
            break ;                                                                       \
        }
        MEGDNN_FOREACH_COMPUTING_DTYPE_FLOAT(cb)
#undef cb
        default:
            megdnn_throw("bad dtype");
}
    m_offset += 16;
}

PoissonRNGImpl::PoissonRNGImpl(Handle *handle):
    PoissonRNG(handle),
    m_seed(0),
    m_offset(0),
    m_stream(cuda_stream(handle))
{
}

void PoissonRNGImpl::exec(_megdnn_tensor_in lam, _megdnn_tensor_out dst, 
                _megdnn_workspace workspace) {
    check_exec(lam.layout, dst.layout, workspace.size);
    auto size = dst.layout.total_nr_elems();
    megdnn_assert(size);
    ensure_seed(m_param.seed);
    ElemwiseOpParamN<0> ele_param(size);
    switch (dst.layout.dtype.enumv()){
#define cb(_dt)                                                                           \
        case DTypeTrait<_dt>::enumv:                                                      \
        {                                                                                 \
            using ctype = DTypeTrait<_dt>::ctype;                                         \
            run_elemwise<random::PoissonKernel<ctype>, ctype, 0>(ele_param, m_stream,     \
                                                        {dst, lam, m_seed, m_offset});    \
            break;                                                                        \
        }
        MEGDNN_FOREACH_COMPUTING_DTYPE_FLOAT(cb)
#undef cb
        default:
            megdnn_throw("bad dtype");
}
    m_offset += 20;
}

BetaRNGImpl::BetaRNGImpl(Handle *handle):
    BetaRNG(handle),
    m_seed(0),
    m_offset(0),
    m_stream(cuda_stream(handle))
{
}

void BetaRNGImpl::exec(_megdnn_tensor_in alpha, _megdnn_tensor_in beta,_megdnn_tensor_out dst, 
                _megdnn_workspace workspace) {
    check_exec(alpha.layout, beta.layout ,dst.layout, workspace.size);
    auto size = dst.layout.total_nr_elems();
    megdnn_assert(size);
    ensure_seed(m_param.seed);
    ElemwiseOpParamN<0> ele_param(size);
    switch (dst.layout.dtype.enumv()){
#define cb(_dt)                                                                           \
        case DTypeTrait<_dt>::enumv:                                                      \
        {                                                                                 \
            using ctype = DTypeTrait<_dt>::ctype;                                         \
            run_elemwise<random::BetaKernel<ctype>, ctype, 0>(ele_param, m_stream,        \
                                                {dst, alpha, beta, m_seed, m_offset});    \
            break;                                                                        \
        }
        MEGDNN_FOREACH_COMPUTING_DTYPE_FLOAT(cb)
#undef cb
        default:
            megdnn_throw("bad dtype");
}
    m_offset += 32;
}

PermutationRNGImpl::PermutationRNGImpl(Handle *handle):
    PermutationRNG(handle),
    m_seed(0),
    m_offset(0),
    m_stream(cuda_stream(handle))
{
}

void PermutationRNGImpl::exec(
        _megdnn_tensor_inout dst, _megdnn_workspace workspace) {
    check_exec(dst.layout, workspace.size);
    auto size = dst.layout.total_nr_elems();
    megdnn_assert(size);
    ensure_seed(m_param.seed);

    auto wk = workspace.ptr<void>();
    switch (dst.layout.dtype.enumv()){
#define cb(_dt)                                                                           \
        case DTypeTrait<_dt>::enumv:                                                      \
        {                                                                                 \
            using ctype = DTypeTrait<_dt>::ctype;                                         \
            ctype max_size = DTypeTrait<_dt>::max() - 1;                                  \
            megdnn_assert(ctype(size) < max_size);                                        \
            random::permutation_forward<ctype>(dst.ptr<ctype>(), wk, size, m_seed,           \
                                             m_offset, m_stream);                         \
            break;                                                                        \
        }
        cb(::megdnn::dtype::Float32)
        cb(::megdnn::dtype::Int32)
        cb(::megdnn::dtype::Int16)
#undef cb
        default:
            megdnn_throw("bad dtype");
}
    m_offset += 8;
}

size_t PermutationRNGImpl::get_workspace_in_bytes(const TensorLayout &layout){
    size_t size = layout.total_nr_elems();
    return random::get_permutation_workspace_in_bytes(size);
}

264 265
// vim: syntax=cpp.doxygen