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

Method codegen_opencl

src/jit/impl/tiny_opencl/codegen_opencl.cpp:95–194  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

93} // anonymous namespace
94
95std::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;

Callers

nothing calls this directly

Calls 14

to_stringFunction · 0.85
append_replace_mapFunction · 0.85
gen_opr_astFunction · 0.70
sizeMethod · 0.45
nameMethod · 0.45
addMethod · 0.45
outputMethod · 0.45
code_genMethod · 0.45
atMethod · 0.45
digestMethod · 0.45

Tested by

no test coverage detected