| 115 | } // anonymous namespace |
| 116 | |
| 117 | std::pair<std::string, std::string> mgb::jit::codegen_cuda( |
| 118 | const InternalGraph& internal_graph, const JITExecutor::Args& args, |
| 119 | bool copy_param_to_dev) { |
| 120 | std::string cuda_kernel = |
| 121 | R"( |
| 122 | #include <cuda_fp16.h> |
| 123 | |
| 124 | struct Uint32Fastdiv { |
| 125 | unsigned int m_mul, m_divisor, m_divisor_is_not_1, m_inc_dividend, m_shift; |
| 126 | |
| 127 | static const unsigned int MAX_DIVIDEND = ~0u - 1; |
| 128 | }; |
| 129 | |
| 130 | template <int ndim> |
| 131 | struct ParamElemVisitor { |
| 132 | int m_stride[ndim]; |
| 133 | |
| 134 | //! m_shape_highdim[i] = original_shape[i + 1] |
| 135 | Uint32Fastdiv m_shape_highdim[ndim > 1 ? ndim - 1 : 1]; |
| 136 | static const int NDIM = ndim; |
| 137 | }; |
| 138 | |
| 139 | struct Data { |
| 140 | void* inputs[{{NR_INPS}}]; |
| 141 | {{OUTPUT_DTYPE}}* output; |
| 142 | }; |
| 143 | |
| 144 | struct PEVisitors { |
| 145 | ParamElemVisitor<{{NDIM}}> m[{{NR_INPS}}]; |
| 146 | }; |
| 147 | |
| 148 | template<typename T> |
| 149 | static __forceinline__ __device__ T jit_log_sum_exp(T x, T y) { |
| 150 | T a, b; |
| 151 | a = x < y ? x : y; |
| 152 | b = x < y ? y : x; |
| 153 | return T(b + log1pf(expf(a - b))); |
| 154 | } |
| 155 | |
| 156 | )"; |
| 157 | |
| 158 | cuda_kernel += copy_param_to_dev ? R"( |
| 159 | extern "C" __global__ void {{KERNEL_NAME}} (Data* data_ptr, size_t num_elements, PEVisitors* visitors_ptr) { |
| 160 | Data data = *data_ptr; |
| 161 | PEVisitors visitors = *visitors_ptr; |
| 162 | )" |
| 163 | : R"( |
| 164 | extern "C" __global__ void {{KERNEL_NAME}} (Data data, size_t num_elements, |
| 165 | PEVisitors visitors) { )"; |
| 166 | |
| 167 | cuda_kernel += R"( |
| 168 | unsigned int global_idx = blockIdx.x * blockDim.x + threadIdx.x; |
| 169 | unsigned int delta = blockDim.x * gridDim.x; |
| 170 | unsigned int tmp_idx; |
| 171 | |
| 172 | {{DECL_EXPRS}} |
| 173 | {{INTERNAL_DECL_EXPRS}} |
| 174 | {{DECL_fastdiv_offset}} |
nothing calls this directly
no test coverage detected