MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / codegen_cuda

Method codegen_cuda

src/jit/impl/nvrtc/codegen_cuda.cpp:117–252  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

115} // anonymous namespace
116
117std::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
124struct 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
130template <int ndim>
131struct 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
139struct Data {
140 void* inputs[{{NR_INPS}}];
141 {{OUTPUT_DTYPE}}* output;
142};
143
144struct PEVisitors {
145 ParamElemVisitor<{{NDIM}}> m[{{NR_INPS}}];
146};
147
148template<typename T>
149static __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"(
159extern "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"(
164extern "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}}

Callers

nothing calls this directly

Calls 15

gen_input_codeFunction · 0.85
to_stringFunction · 0.85
append_replace_mapFunction · 0.85
gen_fastdiv_offsetFunction · 0.85
dtype_to_cstrFunction · 0.85
getFunction · 0.85
write_fileMethod · 0.80
gen_opr_astFunction · 0.70
addMethod · 0.45
outputMethod · 0.45
code_genMethod · 0.45

Tested by

no test coverage detected