| 93 | } // anonymous namespace |
| 94 | |
| 95 | std::pair<std::string, std::string> mgb::jit::codegen_opencl( |
| 96 | const InternalGraph& internal_graph, const JITExecutor::Args& args) { |
| 97 | std::string opencl_kernel = R"( |
| 98 | __kernel void {{KERNEL_NAME}} ( |
| 99 | {{KERNEL_SRC_ARGS}} |
| 100 | __write_only image2d_t dst, |
| 101 | __private const int global_size_dim0, |
| 102 | __private const int global_size_dim1, |
| 103 | __private const int wc_size, |
| 104 | __private const int hb_size, |
| 105 | __private const int h, |
| 106 | __private const uint w_size |
| 107 | ) { |
| 108 | #if OPENCL_ENABLE_FP16 |
| 109 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 110 | #endif |
| 111 | |
| 112 | const sampler_t SAMPLER = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP; |
| 113 | |
| 114 | int wc = get_global_id(0); |
| 115 | int hb = get_global_id(1); |
| 116 | |
| 117 | #ifndef NON_UNIFORM_WORK_GROUP |
| 118 | if (wc >= global_size_dim0 || hb >= global_size_dim1) |
| 119 | return; |
| 120 | #endif |
| 121 | |
| 122 | for (; hb < hb_size; hb += global_size_dim1) { |
| 123 | for (; wc < wc_size; wc += global_size_dim0) { |
| 124 | int2 coord = (int2)(wc, hb); |
| 125 | int2 coord_b = (int2)(wc / w_size, hb/h); |
| 126 | {{INTERNAL_DECL_EXPRS}} |
| 127 | {{ASSIGN_EXPRS}} |
| 128 | {{INTERNAL_ASSIGN_EXPRS}} |
| 129 | {{WRITE_IMAGE}}(dst, coord, {{EXP}}); |
| 130 | } |
| 131 | wc = get_global_id(0); |
| 132 | } |
| 133 | } |
| 134 | )"; |
| 135 | |
| 136 | auto input_dtype = args.inputs[0].layout.dtype; |
| 137 | for (size_t i = 0; i < args.inputs.size(); i++) { |
| 138 | mgb_assert( |
| 139 | args.inputs[i].layout.dtype == input_dtype, |
| 140 | "OpenCL jit all oprs should have same dtype"); |
| 141 | } |
| 142 | mgb_assert( |
| 143 | args.outputs.size() == 1 && args.outputs[0].layout.dtype == input_dtype, |
| 144 | "output size should be 1 and output dtype should be same with input"); |
| 145 | mgb_assert( |
| 146 | dtype::Float16() == input_dtype || dtype::Float32() == input_dtype, |
| 147 | "OpenCL jit dtype only support float32 or float16, %s not support", |
| 148 | input_dtype.name()); |
| 149 | auto is_half = dtype::Float16() == input_dtype; |
| 150 | |
| 151 | VarNode2AST var2ast; |
| 152 | str_util::StrReplaceMap source_replace_map; |
nothing calls this directly
no test coverage detected