opr_impl.h 3.8 KB
Newer Older
1 2 3 4
/**
 * \file dnn/src/cuda/rng/opr_impl.h
 * 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
 *
 * 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.
 */
#pragma once

13
#include <curand.h>
14 15 16 17 18 19 20 21 22 23 24
#include "megdnn/oprs.h"
#include "src/cuda/handle.h"

namespace megdnn {
namespace cuda {

class CuRandHandle {
    curandGenerator_t m_gen;
    uint64_t m_seed;

    CuRandHandle(const CuRandHandle&) = delete;
25
    CuRandHandle& operator=(const CuRandHandle&) = delete;
26

27 28 29
public:
    CuRandHandle(cudaStream_t stream, uint64_t seed = 0);
    ~CuRandHandle();
30

31
    void seed(uint64_t seed);
32

33
    curandGenerator_t gen() const { return m_gen; }
34

35 36 37
    void ensure_seed(uint64_t seed) {
        if (m_seed != seed) {
            this->seed(seed);
38
        }
39
    }
40 41
};

42
class UniformRNGImpl : public UniformRNG {
43 44
    CuRandHandle m_curand_handle;

45 46 47
public:
    UniformRNGImpl(Handle* handle);
    void exec(_megdnn_tensor_inout dst, _megdnn_workspace) override;
48

49
    size_t get_workspace_in_bytes(const TensorLayout&) override { return 0; }
50 51
};

52
class GaussianRNGImpl : public GaussianRNG {
53 54
    CuRandHandle m_curand_handle;

55 56 57 58 59 60 61 62 63 64 65 66 67 68
public:
    GaussianRNGImpl(Handle* handle);

    void exec(_megdnn_tensor_inout dst, _megdnn_workspace) override;

    size_t get_workspace_in_bytes(const TensorLayout& layout) override;
};

class GammaRNGImpl : public GammaRNG {
    uint64_t m_seed, m_offset;
    cudaStream_t m_stream;

public:
    GammaRNGImpl(Handle* handle);
69

70 71
    void exec(_megdnn_tensor_in shape,_megdnn_tensor_in scale, 
              _megdnn_tensor_out dst, _megdnn_workspace) override;
72

73 74 75 76
    size_t get_workspace_in_bytes(const TensorLayout&, const TensorLayout&,
                                  const TensorLayout&) override {
        return 0;
    }
77

78 79 80 81 82 83 84
    void seed(uint64_t seed) { m_seed = seed; }

    void ensure_seed(uint64_t seed) {
        if (m_seed != seed) {
            this->seed(seed);
        }
    }
85 86
};

87 88 89
class BetaRNGImpl : public BetaRNG {
    uint64_t m_seed, m_offset;
    cudaStream_t m_stream;
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
public:
    BetaRNGImpl(Handle* handle);

    void exec(_megdnn_tensor_in alpha,_megdnn_tensor_in beta, 
              _megdnn_tensor_out dst, _megdnn_workspace) override;

    size_t get_workspace_in_bytes(const TensorLayout&, const TensorLayout&,
                                  const TensorLayout&) override {
        return 0;
    }

    void seed(uint64_t seed) { m_seed = seed; }

    void ensure_seed(uint64_t seed) {
        if (m_seed != seed) {
            this->seed(seed);
        }
    }
};
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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
class PoissonRNGImpl : public PoissonRNG {
    uint64_t m_seed, m_offset;
    cudaStream_t m_stream;

public:
    PoissonRNGImpl(Handle* handle);

    void exec(_megdnn_tensor_in lam, _megdnn_tensor_out dst,
              _megdnn_workspace) override;

    size_t get_workspace_in_bytes(const TensorLayout&,
                                  const TensorLayout&) override {
        return 0;
    }

    void seed(uint64_t seed) { m_seed = seed; }

    void ensure_seed(uint64_t seed) {
        if (m_seed != seed) {
            this->seed(seed);
        }
    }
};

class PermutationRNGImpl : public PermutationRNG {
    uint64_t m_seed, m_offset;
    cudaStream_t m_stream;

public:
    PermutationRNGImpl(Handle* handle);

    void exec(_megdnn_tensor_inout dst, _megdnn_workspace) override;

    size_t get_workspace_in_bytes(const TensorLayout& layout) override;

    void seed(uint64_t seed) { m_seed = seed; }

    void ensure_seed(uint64_t seed) {
        if (m_seed != seed) {
            this->seed(seed);
        }
    }
};

}  // namespace cuda
}  // namespace megdnn
// vim: syntax=cpp.doxygen