MCPcopy Create free account
hub / github.com/MegEngine/MegCC / GetKernelBody

Method GetKernelBody

compiler/lib/KernelGen/GeneralIntrinsic/Reduce.cpp:625–704  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

623}
624
625std::string ReduceKernel::GetKernelBody(TContext* context) const {
626 auto mode = context->getAttrStr("mode");
627 int axis = context->getAttrInt("axis");
628 auto input = context->getAttrOprand("operand:0");
629 auto src_dtype = input.dtype;
630 std::stringstream writer;
631 writer << R"(
632#include "gi_float.h"
633#include "gi_int.h"
634 )";
635 if (src_dtype == "f16") {
636 writer << R"(
637#include "gi_float16.h"
638static inline gi_float16_t max(gi_float16_t a,gi_float16_t b){return a>b?a:b;}
639static inline gi_float16_t min(gi_float16_t a,gi_float16_t b){return a<b?a:b;}
640 )";
641 GIMathHelper gi_math;
642 if ("MIN" == mode) {
643 writer << gi_math.FastFp32toFp16() << "\n";
644 writer << gi_math.GiReduceMinNanFloat16() << "\n";
645 } else if ("MAX" == mode) {
646 writer << gi_math.FastFp32toFp16() << "\n";
647 writer << gi_math.GiReduceMaxNanFloat16() << "\n";
648 } else if ("SUM" == mode) {
649 writer << gi_math.FastFp32toFp16() << "\n";
650 writer << gi_math.GiReduceAddFloat16() << "\n";
651 } else if ("SUM_SQR" == mode) {
652 writer << gi_math.FastFp32toFp16() << "\n";
653 writer << gi_math.GiReduceAddFloat16() << "\n";
654 writer << gi_math.GiMultiplyAddFloat16() << "\n";
655 } else if ("MEAN" == mode) {
656 writer << gi_math.FastFp32toFp16() << "\n";
657 writer << gi_math.GiReduceAddFloat16() << "\n";
658 } else if ("PRODUCT" == mode) {
659 writer << gi_math.FastFp32toFp16() << "\n";
660 writer << gi_math.GiReduceMultiplyFloat16() << "\n";
661 }
662 } else {
663 writer << R"(
664static inline float max(float a,float b){return a>b?a:b;}
665static inline float min(float a,float b){return a<b?a:b;}
666 )";
667 }
668 writer << GenCommonRet() << " " << GetKernelSignature(context);
669 std::string tmp_body = R"({
670 const size_t SIMD_WIDTH = ${simd_width};
671 const size_t axis = ${axis};
672 ${src_specifier}* src = (${src_specifier}*)inputs[0]->ptr;
673 ${src_specifier}* dst = (${src_specifier}*)outputs[0]->ptr;
674 Layout in_layout = inputs[0]->layout;
675 size_t A = 1, B = 0, C = 1;
676 for (size_t i = 0; i < axis; ++ i)
677 A *= in_layout.dims[i];
678 B = in_layout.dims[axis];
679 for (size_t i = axis + 1; i < in_layout.nr_dim; ++ i){
680 C *= in_layout.dims[i];
681 }
682 if(C == 1){

Callers

nothing calls this directly

Calls 14

GenCommonRetFunction · 0.85
StringTemplateArgsClass · 0.85
cvt_dtype_specifierFunction · 0.85
get_dtype_sizeFunction · 0.85
getAttrStrMethod · 0.80
getAttrOprandMethod · 0.80
FastFp32toFp16Method · 0.80
GiReduceMinNanFloat16Method · 0.80
GiReduceMaxNanFloat16Method · 0.80
GiReduceAddFloat16Method · 0.80
GiMultiplyAddFloat16Method · 0.80

Tested by

no test coverage detected