| 225 | } // namespace |
| 226 | |
| 227 | std::string TypecvtKernel::GetKernelBody(TContext* context) const { |
| 228 | std::stringstream ss; |
| 229 | auto src_dtype_str = |
| 230 | SymbolHelper::gen_valid_dtype(context->getAttrOprand("operand:0").dtype); |
| 231 | auto dst_dtype_str = |
| 232 | SymbolHelper::gen_valid_dtype(context->getAttrOprand("operand:1").dtype); |
| 233 | std::string src_specifier = Utils::cvt_dtype_specifier(src_dtype_str); |
| 234 | std::string dst_specifier = Utils::cvt_dtype_specifier(dst_dtype_str); |
| 235 | ss << R"( |
| 236 | #include "gi_float.h" |
| 237 | #include "gi_int.h" |
| 238 | )"; |
| 239 | GIMathHelper gi_math; |
| 240 | if (Utils::is_float_dtype(src_dtype_str, 16) || |
| 241 | Utils::is_float_dtype(dst_dtype_str, 16)) { |
| 242 | ss << "#include \"gi_float16.h\"\n"; |
| 243 | ss << gi_math.FastFp32toFp16() << "\n"; |
| 244 | ss << gi_math.FastFp16toFp32() << "\n"; |
| 245 | } |
| 246 | ss << GenCommonRet() << " " << GetKernelSignature(context); |
| 247 | std::string body_temp = R"({ |
| 248 | ${init_declare_str} |
| 249 | const Tensor* src_tensor = inputs[0]; |
| 250 | const Tensor* dst_tensor = outputs[0]; |
| 251 | ${src_specifier}* src = (${src_specifier}*)(src_tensor->ptr); |
| 252 | ${dst_specifier}* dst = (${dst_specifier}*)(dst_tensor->ptr); |
| 253 | TINYNN_ASSERT(src); |
| 254 | TINYNN_ASSERT(dst); |
| 255 | |
| 256 | const Layout src_layout = src_tensor->layout; |
| 257 | const Layout dst_layout = dst_tensor->layout; |
| 258 | float src_scale = src_tensor->dtype.param.scale; |
| 259 | float dst_scale = dst_tensor->dtype.param.scale; |
| 260 | |
| 261 | size_t nr_elem = 1; |
| 262 | for (int i = 0; i < src_layout.nr_dim; ++i) { |
| 263 | nr_elem *= src_layout.dims[i]; |
| 264 | } |
| 265 | ${gen_scale} |
| 266 | scale = src_scale/dst_scale; |
| 267 | vscale = GiBroadcastFloat32(scale); |
| 268 | size_t idx = 0; |
| 269 | |
| 270 | for(; idx + SIMD_WIDTH <= nr_elem; idx += SIMD_WIDTH){ |
| 271 | ${gen_cvt} |
| 272 | src += SIMD_WIDTH; |
| 273 | dst += SIMD_WIDTH; |
| 274 | } |
| 275 | |
| 276 | for(;idx < nr_elem;++idx){ |
| 277 | ${gen_cvt_remain} |
| 278 | ++src; |
| 279 | ++dst; |
| 280 | } |
| 281 | return TinyNN_SUCCESS; |
| 282 | })"; |
| 283 | |
| 284 | ss << StringTemplate::StringTemplateArgs() |
nothing calls this directly
no test coverage detected