| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #pragma once |
| 8 | |
| 9 | #include <cstdio> |
| 10 | |
| 11 | #include "gemm_lib.hpp" // for transpose_matrix() |
| 12 | #include "kai/ops/bfloat.hpp" |
| 13 | |
| 14 | /* |
| 15 | * Type to use for accumulation - for some type combinations, we need to |
| 16 | * accumulate in something higher precision and downconvert at the end. |
| 17 | * |
| 18 | * This mechanism supports this by defining an accumulation type based on |
| 19 | * the output type. At present, we just use the output type except for |
| 20 | * BF16, where we accumulate in float. |
| 21 | */ |
| 22 | namespace { |
| 23 | |
| 24 | template <typename T> |
| 25 | struct accumulator_type { |
| 26 | typedef T type; |
| 27 | }; |
| 28 | |
| 29 | template<> |
| 30 | struct accumulator_type<bfloat16> { |
| 31 | typedef float type; |
| 32 | }; |
| 33 | |
| 34 | template<> |
| 35 | struct accumulator_type<__fp16> { |
| 36 | typedef float type; |
| 37 | }; |
| 38 | |
| 39 | } // anonymous namespace |
| 40 | |
| 41 | template <typename To, typename Tr> |
| 42 | struct gemm_transposeB { |
| 43 | private: |
| 44 | const GemmProblem * const p; |
| 45 | |
| 46 | std::shared_ptr<Matrix <To> > A_trans={}; |
| 47 | std::shared_ptr<Matrix <To> > B_trans={}; |
| 48 | std::shared_ptr<Matrix <Tr> > C; |
| 49 | std::shared_ptr<Matrix <Tr> > bias; |
| 50 | |
| 51 | public: |
| 52 | static int get_m_block() { return 1; } |
| 53 | static int get_n_block() { return 1; } |
| 54 | static int get_k_block() { return 1; } |
| 55 | |
| 56 | typedef To operand_type; |
| 57 | typedef Tr result_type; |
| 58 | |
| 59 | gemm_transposeB(std::shared_ptr<Matrix <To> > &A, |
| 60 | std::shared_ptr<Matrix <To> > &B, |
| 61 | std::shared_ptr<Matrix <Tr> > &C, |
| 62 | std::shared_ptr<Matrix <Tr> > &bias, |
| 63 | const GemmProblem * const p, bool do_init) : p(p), C(C), bias(bias) { |
| 64 | // Create transposed weight matrix - the "width" of this is the squashed combination of 'HWI' (with I |
| 65 | // divided by the number of groups), the height is 'O' (output channels) |
| 66 | auto gemm_K = (p->input_channels / p->groups) * p->kernel_height * p->kernel_width; |
| 67 | |
| 68 | B_trans = std::make_shared<Matrix <To> >(p->output_channels, gemm_K, gemm_K, p->batches, p->multis); |
| 69 | |
| 70 | if (do_init) { |
| 71 | B_trans->Transpose(B); |
| 72 | } |
| 73 | |
| 74 | A_trans = A; |
| 75 | } |
| 76 | |
| 77 | int64_t Run() { |
| 78 | int lda = A_trans->stride; |
| 79 | int ldb = B_trans->stride; |
| 80 | int ldc = C->stride; |
| 81 | |
| 82 | // Note that this function now computes a noddy 9-loop grouped 2D convolution operation rather than a GEMM. |
| 83 | // |
| 84 | // For pure MM cases, the parameters will be set such that the two are equivalent. |
| 85 | |
| 86 | const int64_t group_inch = (p->input_channels / p->groups); |
| 87 | const int64_t group_outch = (p->output_channels / p->groups); |
| 88 | |
| 89 | typename accumulator_type<Tr>::type max_pos=0; |
| 90 | typename accumulator_type<Tr>::type max_neg=0; |
| 91 | |
| 92 | for (int64_t m=0; m<p->multis; m++) { |
| 93 | for (int64_t b=0; b<p->batches; b++) { |
| 94 | const To *a_base = (A_trans->data + (m * A_trans->multi_stride) + (b * A_trans->batch_stride)); |
| 95 | const To *b_base = (B_trans->data + (m * B_trans->multi_stride)); |
| 96 | Tr *c_base = (C->data + (m * C->multi_stride) + (b * C->batch_stride)); |
| 97 | |
| 98 | for (int64_t out_y=0; out_y<p->output_height; out_y++) { |
| 99 | for (int64_t out_x=0; out_x<p->output_width; out_x++) { |
| 100 | // GEMM 'm' equivalent - squash output height/width into a single dimension |
| 101 | int64_t m_val = (out_y * p->output_width) + out_x; |
| 102 | |
| 103 | for (int64_t group=0; group<p->groups; group++) { |
| 104 | for (int64_t out_ch=(group * group_outch); out_ch<((group + 1) * group_outch); out_ch++) { |
| 105 | typename accumulator_type<Tr>::type v=0, pos=0, neg=0; |
| 106 | Tr &out_ref = c_base[(m_val * ldc) + out_ch]; |
| 107 | |
| 108 | if (p->add_problem) { |
| 109 | // Adds do a pointwise add of A to C, then multiply by the value at the top of |
| 110 | // the corresponding column of B (which turns into a row as this is a transposed |
| 111 | // implementation). This multiply is the first half of a fused batchnorm (the |
| 112 | // second half is handled by the bias add below). |
| 113 | int64_t input_y = (out_y * p->out_stride_h); |
| 114 | int64_t input_x = (out_x * p->out_stride_w); |
| 115 | |
| 116 | // Squash input x/y into single 'row' value (for (MN)HWC format; M,N taken care of by outer loops) |
| 117 | int64_t in_m_val = (input_y * p->input_width) + input_x; |
| 118 | |
| 119 | v = out_ref; |
| 120 | v += a_base[(in_m_val * lda) + out_ch]; |
| 121 | v *= b_base[(out_ch * ldb)]; |
| 122 | } else { |
| 123 | if (p->accumulate) { |
| 124 | v = out_ref; |
| 125 | } |
| 126 | |
| 127 | for (int64_t kern_y=0; kern_y<p->kernel_height; kern_y++) { |
| 128 | for (int64_t kern_x=0; kern_x<p->kernel_width; kern_x++) { |
| 129 | int64_t input_y = (out_y * p->out_stride_h) + kern_y * p->in_stride_h - p->padding_top; |
| 130 | int64_t input_x = (out_x * p->out_stride_w) + kern_x * p->in_stride_w - p->padding_left; |
| 131 | |
| 132 | // Check for out of bounds cases. |
| 133 | if (input_y < 0 || input_y >= p->input_height || input_x < 0 || input_x >= p->input_width) { |
| 134 | // TODO: may need to perform some padding multiplies in the non-zero padding case. |
| 135 | // For now, just skip this kernel point entirely. |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | // Squash input x/y into single 'row' value (for (MN)HWC format; M,N taken care of by outer loops) |
| 140 | int64_t in_m_val = (input_y * p->input_width) + input_x; |
| 141 | |
| 142 | for (int64_t in_ch=(group * group_inch); in_ch<((group + 1) * group_inch); in_ch++) { |
| 143 | // GEMM 'k' equivalent - squash kernel position and input channel into a single dimension |
| 144 | // Use this to access weights/B. |
| 145 | int64_t k_val = ((kern_y * p->kernel_width) + kern_x) * group_inch + (in_ch - (group * group_inch)); |
| 146 | |
| 147 | typename accumulator_type<Tr>::type val = a_base[(in_m_val * lda) + in_ch] * b_base[(out_ch * ldb) + k_val]; |
| 148 | |
| 149 | if (val > 0) { |
| 150 | pos += val; |
| 151 | } else { |
| 152 | neg += val; |
| 153 | } |
| 154 | |
| 155 | v += val; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (bias) { |
| 162 | typename accumulator_type<Tr>::type bd = bias->data[(m * bias->multi_stride) + out_ch]; |
| 163 | v += bd; |
| 164 | |
| 165 | if (bd > 0) { |
| 166 | pos += bd; |
| 167 | } else { |
| 168 | neg += bd; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (pos > max_pos) { |
| 173 | max_pos = pos; |
| 174 | } |
| 175 | if (neg < max_neg) { |
| 176 | max_neg = neg; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | v = activate(p->act, v); |
| 181 | |
| 182 | out_ref = v; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | #ifndef SILENT |
| 191 | printf("Reference computed: maximum positive contribution %f, negative %f\n", (float)max_pos, (float)max_neg); |
| 192 | #endif |
| 193 | |
| 194 | return std::max<int64_t>(max_pos, -max_neg); |
| 195 | } |
| 196 | }; |