|
| 1 | +// Copyright (C) 2020-2025 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#include "brgemm_generic.hpp" |
| 6 | + |
| 7 | +#include "common/utils.hpp" |
| 8 | +#include "dnnl_extension_utils.h" |
| 9 | +#include "utils/general_utils.h" |
| 10 | + |
| 11 | +#define PRINT(X) ss << #X << " = " << X << "\n" |
| 12 | +#define EQ(X) X == rhs.X |
| 13 | +#define HASH(X) seed = dnnl::impl::hash_combine(seed, X) |
| 14 | + |
| 15 | +namespace ov::intel_cpu { |
| 16 | + |
| 17 | +bool BrgemmGenericKernelConfig::is_completed() const { |
| 18 | + return !one_of(0, m_M, m_N, m_K, m_LDA, m_LDB, m_LDC) || is_empty(); |
| 19 | +} |
| 20 | + |
| 21 | +bool BrgemmGenericKernelConfig::is_empty() const { |
| 22 | + return everyone_is(0, m_M, m_N, m_K, m_LDA, m_LDB, m_LDC, m_beta); |
| 23 | +} |
| 24 | + |
| 25 | +bool BrgemmGenericKernelConfig::operator==(const BrgemmGenericKernelConfig& rhs) const { |
| 26 | + return EQ(m_beta) && EQ(m_M) && EQ(m_N) && EQ(m_K) && EQ(m_LDA) && EQ(m_LDB) && EQ(m_LDC); |
| 27 | +} |
| 28 | + |
| 29 | +void BrgemmGenericKernelConfig::update(int64_t M, |
| 30 | + int64_t N, |
| 31 | + int64_t K, |
| 32 | + int64_t LDA, |
| 33 | + int64_t LDB, |
| 34 | + int64_t LDC, |
| 35 | + float beta) { |
| 36 | + // If M/N/K is zero, it means that Brgemm won't be executed (in Loop with work_amount = 0, for example) |
| 37 | + // To process this case, we have to make this Config as empty (nullify runtime parameters) |
| 38 | + if (one_of(0, M, N, K)) { |
| 39 | + m_M = 0; |
| 40 | + m_N = 0; |
| 41 | + m_K = 0; |
| 42 | + m_LDA = 0; |
| 43 | + m_LDB = 0; |
| 44 | + m_LDC = 0; |
| 45 | + m_beta = 0; |
| 46 | + } else { |
| 47 | + m_M = M; |
| 48 | + m_N = N; |
| 49 | + m_K = K; |
| 50 | + m_LDA = LDA; |
| 51 | + m_LDB = LDB; |
| 52 | + m_LDC = LDC; |
| 53 | + m_beta = beta; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +size_t BrgemmGenericKernelConfig::compute_hash() const { |
| 58 | + size_t seed = 0; |
| 59 | + HASH(m_M); |
| 60 | + HASH(m_N); |
| 61 | + HASH(m_K); |
| 62 | + HASH(m_LDA); |
| 63 | + HASH(m_LDB); |
| 64 | + HASH(m_LDC); |
| 65 | + HASH(m_beta); |
| 66 | + return seed; |
| 67 | +} |
| 68 | + |
| 69 | +#ifdef SNIPPETS_DEBUG_CAPS |
| 70 | +std::string BrgemmGenericKernelConfig::to_string() const { |
| 71 | + std::stringstream ss; |
| 72 | + PRINT(m_M); |
| 73 | + PRINT(m_N); |
| 74 | + PRINT(m_K); |
| 75 | + PRINT(m_LDA); |
| 76 | + PRINT(m_LDB); |
| 77 | + PRINT(m_LDC); |
| 78 | + PRINT(m_beta); |
| 79 | + return ss.str(); |
| 80 | +} |
| 81 | +#endif |
| 82 | + |
| 83 | +float BrgemmKernelExecutorHelper::get_beta( |
| 84 | + const ov::snippets::lowered::LoopManagerPtr& loop_manager, |
| 85 | + int loop_id, |
| 86 | + const ov::snippets::lowered::ExpandedLoopInfoPtr& current_expanded_loop_info) { |
| 87 | + // Find all Expanded loops with the same Unified loop information -> they were decomposed from this Unified Loop. |
| 88 | + // Note that LoopInfo are normalized and sorted (due to NormalizedLoopIDs pass). |
| 89 | + // It means that previous executed Loops have Loop ID less the current Loop ID. |
| 90 | + // - If there is executed Loop (work_amount > 0) and evaluated before the current -> the current Brgemm should have |
| 91 | + // `beta = 1`. |
| 92 | + // - If there is not this Loop -> the current executed Brgemm should have `beta = 0`. |
| 93 | + if (loop_id > 0) { |
| 94 | + const auto& current_unified_loop_info = current_expanded_loop_info->get_unified_loop_info(); |
| 95 | + // Check the previous Loops |
| 96 | + --loop_id; |
| 97 | + while (loop_id >= 0) { |
| 98 | + const auto& expanded_loop_info = |
| 99 | + loop_manager->get_loop_info<ov::snippets::lowered::ExpandedLoopInfo>(loop_id); |
| 100 | + if (expanded_loop_info->get_unified_loop_info() != current_unified_loop_info) { |
| 101 | + return 0; |
| 102 | + } |
| 103 | + if (expanded_loop_info->get_work_amount() > 0) { |
| 104 | + // there is previous executed Brgemm with `beta = 0` -> the current Brgemm should have `beta = 1` |
| 105 | + return 1; |
| 106 | + } |
| 107 | + --loop_id; |
| 108 | + } |
| 109 | + } |
| 110 | + return 0; |
| 111 | +} |
| 112 | + |
| 113 | +std::tuple<int64_t, int64_t, int64_t, float> BrgemmKernelExecutorHelper::get_runtime_brgemm_params( |
| 114 | + const ov::snippets::lowered::ExpressionPtr& expr, |
| 115 | + const ov::snippets::lowered::LinearIRCPtr& linear_ir) { |
| 116 | + const auto& input_pds = expr->get_input_port_descriptors(); |
| 117 | + const auto& output_pds = expr->get_output_port_descriptors(); |
| 118 | + OV_CPU_JIT_EMITTER_ASSERT((input_pds.size() == 2 || input_pds.size() == 3) && output_pds.size() == 1, |
| 119 | + "Invalid number of in/out port descriptors"); |
| 120 | + |
| 121 | + const auto& in0_shape = snippets::utils::get_planar_vdims(input_pds[0]->get_shape(), input_pds[0]->get_layout()); |
| 122 | + const auto& in1_shape = snippets::utils::get_planar_vdims(input_pds[1]->get_shape(), input_pds[1]->get_layout()); |
| 123 | + const auto& in0_subtensor = input_pds[0]->get_subtensor(); |
| 124 | + const auto& in1_subtensor = input_pds[1]->get_subtensor(); |
| 125 | + |
| 126 | + // Need to update M, K, N |
| 127 | + // 1. If the original value in subtensor is `FULL_DIM`, it means that |
| 128 | + // Brgemm block should process full tensor by this dim -> take dimension from shape |
| 129 | + // 2. Otherwise, Brgemm block processes part of the tensor by this dim |
| 130 | + // (there is blocking by this dimension) -> take from Loop increment |
| 131 | + |
| 132 | + auto M = *++in0_subtensor.rbegin(); |
| 133 | + auto K = *in0_subtensor.rbegin(); |
| 134 | + auto N = *in1_subtensor.rbegin(); |
| 135 | + |
| 136 | + size_t loop_idx = 0; |
| 137 | + const auto& loop_ids = expr->get_loop_ids(); |
| 138 | + const auto& loop_manager = linear_ir->get_loop_manager(); |
| 139 | + auto get_loop_info = [&]() { |
| 140 | + OPENVINO_ASSERT(loop_idx < loop_ids.size(), "Loop is missed"); |
| 141 | + return loop_manager->get_loop_info<ov::snippets::lowered::ExpandedLoopInfo>(loop_ids[loop_idx++]); |
| 142 | + }; |
| 143 | + |
| 144 | + /* ------- Dimension M ----------*/ |
| 145 | + if (ov::snippets::utils::is_full_dim_value(M)) { |
| 146 | + M = *++in0_shape.rbegin(); |
| 147 | + } else { |
| 148 | + const auto& current_expanded_loop_info = get_loop_info(); |
| 149 | + const auto& in_ports = current_expanded_loop_info->get_input_ports(); |
| 150 | + const auto& out_ports = current_expanded_loop_info->get_output_ports(); |
| 151 | + // Quick validation check: Should we check that port is really Brgemm port? |
| 152 | + // If BrgemmCopyB in the Loop by M -> first input port will be BrgemmCopyB with `incremented=false` |
| 153 | + // to avoid extra checks, we validate only first input port |
| 154 | + auto check_port = [&](const ov::snippets::lowered::LoopPort& p) { |
| 155 | + return p.get_dim_idx() == 1 && p.is_processed(); |
| 156 | + }; |
| 157 | + OPENVINO_ASSERT( |
| 158 | + in_ports.size() > 1 && check_port(in_ports[0]) && out_ports.size() == 1 && check_port(out_ports[0]), |
| 159 | + "Incorrect Loop by Brgemm dimension M"); |
| 160 | + M = current_expanded_loop_info->get_work_amount() > 0 ? current_expanded_loop_info->get_increment() : 0; |
| 161 | + input_pds[0]->set_subtensor_dim(1, M); |
| 162 | + output_pds[0]->set_subtensor_dim(1, M); |
| 163 | + } |
| 164 | + |
| 165 | + /* ------- Dimension N ----------*/ |
| 166 | + if (ov::snippets::utils::is_full_dim_value(N)) { |
| 167 | + N = *in1_shape.rbegin(); |
| 168 | + } else { |
| 169 | + const auto& current_expanded_loop_info = get_loop_info(); |
| 170 | + const auto& in_ports = current_expanded_loop_info->get_input_ports(); |
| 171 | + const auto& out_ports = current_expanded_loop_info->get_output_ports(); |
| 172 | + // Quick validation check: Should we check that port is really Brgemm port? |
| 173 | + auto check_port = [&](const ov::snippets::lowered::LoopPort& p) { |
| 174 | + return p.get_dim_idx() == 0 && p.is_processed(); |
| 175 | + }; |
| 176 | + OPENVINO_ASSERT(in_ports.size() >= 2 && !in_ports.front().is_processed() && |
| 177 | + std::all_of(in_ports.cbegin() + 1, in_ports.cend(), check_port) && out_ports.size() == 1 && |
| 178 | + check_port(out_ports.back()), |
| 179 | + "Incorrect Loop by Brgemm dimension N"); |
| 180 | + N = current_expanded_loop_info->get_work_amount() > 0 ? current_expanded_loop_info->get_increment() : 0; |
| 181 | + input_pds[1]->set_subtensor_dim(0, N); |
| 182 | + output_pds[0]->set_subtensor_dim(0, N); |
| 183 | + } |
| 184 | + |
| 185 | + /* ------- Dimension K ----------*/ |
| 186 | + // 1. If Brgemm block processes full dimension K -> `beta = 0` |
| 187 | + // 2. If Brgemm block processes part of the dimension K (there is blocking), need to find |
| 188 | + // the most first executed Brgemm Block in Loops which iterate through dimension K (work_amount > 0). |
| 189 | + // First of them will have `beta = 0`, other - `beta = 1` |
| 190 | + float beta = 0; |
| 191 | + if (ov::snippets::utils::is_full_dim_value(K)) { |
| 192 | + K = *in0_shape.rbegin(); |
| 193 | + } else { |
| 194 | + const auto& current_expanded_loop_info = get_loop_info(); |
| 195 | + const auto& in_ports = current_expanded_loop_info->get_input_ports(); |
| 196 | + const auto& out_ports = current_expanded_loop_info->get_output_ports(); |
| 197 | + // Quick validation check: Should we check that port is really Brgemm port? |
| 198 | + OPENVINO_ASSERT(in_ports.size() >= 2 && in_ports.front().get_dim_idx() == 0 && |
| 199 | + in_ports.front().is_processed() && in_ports.back().get_dim_idx() == 1 && |
| 200 | + in_ports.back().is_processed() && out_ports.size() == 1 && |
| 201 | + !out_ports.front().is_processed(), |
| 202 | + "Incorrect Loop by Brgemm dimension K"); |
| 203 | + K = current_expanded_loop_info->get_work_amount() > 0 ? current_expanded_loop_info->get_increment() : 0; |
| 204 | + input_pds[0]->set_subtensor_dim(0, K); |
| 205 | + input_pds[1]->set_subtensor_dim(1, K); |
| 206 | + if (K > 0) { |
| 207 | + beta = get_beta(loop_manager, static_cast<int>(loop_ids.back()), current_expanded_loop_info); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + return std::make_tuple(M, N, K, beta); |
| 212 | +} |
| 213 | + |
| 214 | +#undef PRINT |
| 215 | +#undef EQ |
| 216 | +#undef HASH |
| 217 | + |
| 218 | +} // namespace ov::intel_cpu |
0 commit comments