| 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 <assert.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | #include <cmath> |
| 15 | #include <cstdint> |
| 16 | #include <limits> |
| 17 | #include <memory> |
| 18 | #include <random> |
| 19 | #include <type_traits> |
| 20 | |
| 21 | #include "kai/ops/gemm/kai_ops.hpp" |
| 22 | #include "kai/ops/bfloat.hpp" |
| 23 | |
| 24 | // Various other useful functions |
| 25 | |
| 26 | // Compute greatest common divisor using Euclid's algorithm |
| 27 | static inline int gcd(int a, int b) { |
| 28 | for (;;) { |
| 29 | if (a == 0) return b; |
| 30 | b %= a; |
| 31 | if (b == 0) return a; |
| 32 | a %= b; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // Compute lowest common multiple using GCD defined above. |
| 37 | static inline int lcm(int a, int b) { |
| 38 | int temp = gcd(a, b); |
| 39 | |
| 40 | return temp ? (a / temp * b) : 0; |
| 41 | } |
| 42 | |
| 43 | // Round up to the nearest multiple |
| 44 | static inline int round_up_to_nearest_multiple(int val, int multiple) { |
| 45 | val = val + (multiple - 1); |
| 46 | val /= multiple; |
| 47 | return (val * multiple); |
| 48 | } |
| 49 | |
| 50 | /* Memory allocation function */ |
| 51 | void *allocate_aligned_memory(size_t alignment, size_t size); |
| 52 | void free_aligned_memory(void *ptr); |
| 53 | |
| 54 | /* Thread mapping function. This function takes two parameters: our thread |
| 55 | * ID and the number of threads detected in the system. It returns a CPU ID |
| 56 | * to bind this thread to. |
| 57 | * |
| 58 | * Declare this type even in non-threading scenarios so that we don't have |
| 59 | * to #ifdef function signatures. |
| 60 | */ |
| 61 | typedef int (*mapfn)(int, int); |
| 62 | |
| 63 | class GemmRandom { |
| 64 | // std::random_device rd; |
| 65 | std::mt19937 eng; |
| 66 | std::uniform_real_distribution<> distr; |
| 67 | |
| 68 | public: |
| 69 | GemmRandom() : eng(0xf00df00d), distr(-0.9, 1.0) { |
| 70 | eng.seed(1); |
| 71 | } |
| 72 | |
| 73 | double operator()(void) { |
| 74 | return distr(eng); |
| 75 | } |
| 76 | |
| 77 | uint32_t get_int(void) { |
| 78 | return eng(); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | // Generate a random number between -50.00 and +49.99... |
| 83 | template <typename T> |
| 84 | static inline T get_random_number(void) { |
| 85 | static GemmRandom gr; |
| 86 | |
| 87 | return static_cast<T>( gr() ); |
| 88 | // return (T) (((double)(rand() % 100000) / 100.0) - 50.0); |
| 89 | } |
| 90 | |
| 91 | template <> |
| 92 | inline uint8_t get_random_number<>(void) { |
| 93 | return rand() % 256; |
| 94 | } |
| 95 | |
| 96 | template <> |
| 97 | inline int8_t get_random_number<>(void) { |
| 98 | return (rand() % 255) - 127; |
| 99 | } |
| 100 | |
| 101 | template <> |
| 102 | inline uint16_t get_random_number<>(void) { |
| 103 | return rand() % 65535; |
| 104 | } |
| 105 | |
| 106 | template <> |
| 107 | inline int16_t get_random_number<>(void) { |
| 108 | return (rand() % 65535) - 32768; |
| 109 | } |
| 110 | |
| 111 | // Return a NaN for floating point types, or fall back on the above routines for integer types. |
| 112 | template <typename T> |
| 113 | static inline T get_random_number_or_NaN(void) { |
| 114 | #ifdef __ANDROID__ |
| 115 | return get_random_number<T>(); |
| 116 | #else |
| 117 | if (std::numeric_limits<T>::has_quiet_NaN) { |
| 118 | return std::numeric_limits<T>::quiet_NaN(); |
| 119 | } else { |
| 120 | return get_random_number<T>(); |
| 121 | } |
| 122 | #endif |
| 123 | } |
| 124 | |
| 125 | template<typename T> |
| 126 | inline T get_random_number_bits(int bits, bool pos_only=false) { |
| 127 | static GemmRandom gr; |
| 128 | |
| 129 | uint32_t raw_value = gr.get_int(); |
| 130 | int32_t value; |
| 131 | |
| 132 | // Assuming int is 32 bit |
| 133 | if (bits < 31) { |
| 134 | int sign = (raw_value >> 31) && (pos_only == false); |
| 135 | int masked = raw_value & ((1 << bits) - 1); |
| 136 | |
| 137 | if (std::is_unsigned<T>::value) { |
| 138 | sign=0; |
| 139 | } |
| 140 | |
| 141 | value = (sign ? -1 : 1) * masked; |
| 142 | } else { |
| 143 | value = (int32_t) raw_value; |
| 144 | } |
| 145 | |
| 146 | return (T)value; |
| 147 | } |
| 148 | |
| 149 | template <typename T> |
| 150 | static inline T get_compare_threshold(void) { |
| 151 | if (std::is_integral<T>::value) { |
| 152 | return 0; |
| 153 | } else { |
| 154 | return 0.0; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | template<> |
| 160 | inline float get_compare_threshold<float>(void) { |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | template<> |
| 165 | inline __fp16 get_compare_threshold<__fp16>(void) { |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | template<> |
| 170 | inline bfloat16 get_compare_threshold<bfloat16>(void) { |
| 171 | return 0; |
| 172 | } |
| 173 | */ |
| 174 | |
| 175 | // Reference activation implementation |
| 176 | using kai::ops::Activation; |
| 177 | |
| 178 | template<typename T> |
| 179 | inline T activate(Activation act, T value) { |
| 180 | switch(act.type) { |
| 181 | default: |
| 182 | case Activation::Type::None: |
| 183 | return value; |
| 184 | |
| 185 | case Activation::Type::ReLU: |
| 186 | return std::max(value, static_cast<T>(0)); |
| 187 | |
| 188 | case Activation::Type::BoundedReLU: |
| 189 | return std::min(static_cast<T>(act.param1), std::max(value, static_cast<T>(0))); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Descriptor for quantization parameters, with methods to do appropriate conversions |
| 194 | template <typename intT, typename floatT = float> |
| 195 | struct QuantizeParameters { |
| 196 | // Scale factor - how much FP32 range is represented by each increment of the integer type. |
| 197 | floatT m_scale; |
| 198 | // Zero point - which integer value represents zero. |
| 199 | intT m_zeropt; |
| 200 | // Minimum quantized value - might not be relevant type minimum (e.g. s8 excluding -128). |
| 201 | intT m_minval = std::numeric_limits<intT>::min(); |
| 202 | // Maximum quantized value - might not be relevant type maximum (e.g. 12 bit numbers in u16). |
| 203 | intT m_maxval = std::numeric_limits<intT>::max(); |
| 204 | |
| 205 | // This looks a bit like an explosion in a cast factory, but we have to |
| 206 | // keep everything in the floating point type to avoid overflow in the |
| 207 | // integer type. |
| 208 | intT quantize(const floatT v) { |
| 209 | const floatT scaled_f = ::round(v / m_scale); |
| 210 | const floatT moved_f = scaled_f + static_cast<floatT>(m_zeropt); |
| 211 | |
| 212 | return static_cast<intT>(std::min(static_cast<floatT>(m_maxval), std::max(static_cast<floatT>(m_minval), moved_f))); |
| 213 | } |
| 214 | |
| 215 | floatT dequantize(const intT v) { |
| 216 | const floatT moved_f = static_cast<floatT>(v) - static_cast<floatT>(m_zeropt); |
| 217 | |
| 218 | return moved_f * m_scale; |
| 219 | } |
| 220 | |
| 221 | QuantizeParameters() : m_scale(0.0f), m_zeropt(0) {} |
| 222 | |
| 223 | QuantizeParameters(floatT scale, intT zeropt) : m_scale(scale), m_zeropt(zeropt) { } |
| 224 | QuantizeParameters(floatT scale, intT zeropt, intT minval, intT maxval) : |
| 225 | m_scale(scale), m_zeropt(zeropt), m_minval(minval), m_maxval(maxval) { } |
| 226 | |
| 227 | // Forced zero point and fp range |
| 228 | QuantizeParameters(intT zeropt, std::pair<floatT, floatT> range, |
| 229 | intT minval = std::numeric_limits<intT>::min(), |
| 230 | intT maxval = std::numeric_limits<intT>::max()) : m_zeropt(zeropt), m_minval(minval), m_maxval(maxval) { |
| 231 | // Figure out how many representable values appear each side of the forced zero point. |
| 232 | const floatT steps_upper = static_cast<floatT>(maxval) - static_cast<floatT>(zeropt); |
| 233 | const floatT steps_lower = static_cast<floatT>(zeropt) - static_cast<floatT>(minval); |
| 234 | |
| 235 | // Compute corresponding scale values for above and below zero. |
| 236 | // Avoid cases that don't make sense, either because there are no |
| 237 | // values to represent (e.g. minimum value above zero implies there |
| 238 | // are no negative numbers to represent) or there is no space to |
| 239 | // represent them (e.g. unsigned type with forced zero point of 0 |
| 240 | // means we can't represent any negative numbers). |
| 241 | const floatT upper_scale = (range.second <= 0 || steps_upper==0) ? 0 : range.second / steps_upper; |
| 242 | const floatT lower_scale = (range.first >= 0 || steps_lower==0) ? 0 : -range.first / steps_lower; |
| 243 | |
| 244 | // The scale is the highest of these two values. |
| 245 | m_scale = std::max(upper_scale, lower_scale); |
| 246 | } |
| 247 | |
| 248 | // FP range |
| 249 | QuantizeParameters(std::pair<floatT, floatT> range, |
| 250 | intT minval = std::numeric_limits<intT>::min(), |
| 251 | intT maxval = std::numeric_limits<intT>::max()) : m_minval(minval), m_maxval(maxval) { |
| 252 | // Figure out how many steps there are... |
| 253 | const floatT output_steps = static_cast<floatT>(maxval) - static_cast<floatT>(minval); |
| 254 | |
| 255 | // We always have to be able to represent zero, so figure out what the actual minimum value is |
| 256 | const floatT f_minval = std::min(range.first, static_cast<floatT>(0)); |
| 257 | |
| 258 | // And what range of numbers we need to represent (expanded if needed to include 0). |
| 259 | const floatT input_range = std::max(range.second, static_cast<floatT>(0)) - f_minval; |
| 260 | |
| 261 | // Scale is just one over the other... |
| 262 | m_scale = input_range / output_steps; |
| 263 | |
| 264 | // For the zero point, figure out how many steps the lowest output |
| 265 | // value is below zero, and add that many steps on to the bottom of |
| 266 | // the range. Need to do in float type, because this number of |
| 267 | // steps might not be representable in intT (for example, if |
| 268 | // quantizing to a signed type and the range is entirely negative). |
| 269 | m_zeropt = static_cast<intT>(::round(-f_minval / m_scale) + static_cast<floatT>(minval)); |
| 270 | } |
| 271 | |
| 272 | // In order to quantize the bias, we need to combine the scale factors of both the operand matrices. |
| 273 | template<typename other_intT1, typename other_intT2, typename other_floatT> |
| 274 | QuantizeParameters(QuantizeParameters<other_intT1, other_floatT> op1, QuantizeParameters<other_intT2, other_floatT> op2) { |
| 275 | m_scale = op1.m_scale * op2.m_scale; |
| 276 | m_zeropt = 0; |
| 277 | } |
| 278 | |
| 279 | void print(const char *desc) { |
| 280 | #ifndef SILENT |
| 281 | printf("%s quantization parameters: scale=%f, zero point=%d\n",desc,m_scale,m_zeropt); |
| 282 | #endif |
| 283 | } |
| 284 | }; |
| 285 | |
| 286 | |
| 287 | // GemmProblem struct: Stores the parameters for the GEMM problem. |
| 288 | // |
| 289 | // For maximum flexibility, problems are expressed as convolution problems. |
| 290 | // |
| 291 | // It is assumed that input and output activations are in (M)NHWC layout, |
| 292 | // with inputs on the GEMM LHS, while weights are (M)HWIO on the GEMM RHS. |
| 293 | // For now, it is further assumed that 'HW' for input/output, and 'HWI' for |
| 294 | // weights, are packed, i.e. there is no explicit stride specified for |
| 295 | // these. |
| 296 | // |
| 297 | // GEMM problems are mapped to convolution problems as follows: |
| 298 | // |
| 299 | // input_width = output_width = M |
| 300 | // output_channels = N |
| 301 | // input_channels = K |
| 302 | // input_height = output_height = kernel_width = kernel_height = out_stride_h = out_stride_w = 1; |
| 303 | // padding = false (although this should not affect 1x1 kernels) |
| 304 | |
| 305 | // This enum is for the gemm-linux classes - when presented with a convolution problem, what should the wrapper do? |
| 306 | // |
| 307 | // fail: abort |
| 308 | // im2row: allocate an im2row buffer, perform im2row, then perform a normal GEMM |
| 309 | // indirect: create an 'indirect' buffer, request indirect GEMM and pass it in |
| 310 | // convolution: request indirect GEMM, pass in convolution parameters |
| 311 | |
| 312 | enum class ConvStrategy { |
| 313 | fail, |
| 314 | im2row, |
| 315 | indirect, |
| 316 | convolution |
| 317 | }; |
| 318 | |
| 319 | struct strategy_name { |
| 320 | std::string name; |
| 321 | ConvStrategy strat; |
| 322 | }; |
| 323 | |
| 324 | struct WinogradProblem { |
| 325 | unsigned int output_tile_rows = 0u, output_tile_cols = 0u; |
| 326 | std::string input_transform_filter = {}; |
| 327 | std::string weight_transform_filter = {}; |
| 328 | std::string output_transform_filter = {}; |
| 329 | }; |
| 330 | |
| 331 | struct GemmProblem { |
| 332 | // Default parameters are set such that you can get a pure "GEMM" problem by setting: |
| 333 | // input_width=output_width=M, input_channels=K, output_channels=N |
| 334 | // These values therefore default to zero. |
| 335 | // Strides are also set to zero - these get populated appropriately later. |
| 336 | int64_t input_height = 1; |
| 337 | int64_t input_width = 0; |
| 338 | int64_t input_channels = 0; |
| 339 | int64_t kernel_height = 1; |
| 340 | int64_t kernel_width = 1; |
| 341 | int64_t output_height = 1; |
| 342 | int64_t output_width = 0; |
| 343 | int64_t output_channels = 0; |
| 344 | int64_t padding_top = 0; |
| 345 | int64_t padding_left = 0; |
| 346 | // "output stride" - how much we stride across the input array, per output point |
| 347 | int64_t out_stride_h = 1; |
| 348 | int64_t out_stride_w = 1; |
| 349 | // "input stride" - how much we stride across the input array, per kernel point (sometimes called "dilation") |
| 350 | int64_t in_stride_h = 1; |
| 351 | int64_t in_stride_w = 1; |
| 352 | int64_t groups = 1; |
| 353 | int64_t batches = 1; |
| 354 | int64_t multis = 1; |
| 355 | int64_t a_stride = 0; |
| 356 | int64_t b_stride = 0; |
| 357 | bool fast_mode = false; |
| 358 | bool fixed_format = false; |
| 359 | bool time_weight_transform = false; |
| 360 | bool dynamic_scheduling = false; |
| 361 | int64_t dynamic_granule_count = 0; |
| 362 | std::string kernel_filter = {}; |
| 363 | kai::ops::WeightFormat weight_format = kai::ops::WeightFormat::ANY; |
| 364 | int64_t inner_block_size = 0; |
| 365 | int64_t outer_block_size = 0; |
| 366 | bool accumulate = false; // Add to what's already in C. |
| 367 | bool add_problem = false; // Force an add problem. |
| 368 | bool transposed_b = false; // Transpose B if supported |
| 369 | // Activate the input before handing it to the layer: this can enable some |
| 370 | // optimisations. |
| 371 | bool after_relu = false; |
| 372 | // bias note: this flag just tells the infrastructure whether to |
| 373 | // generate the bias. Kernels apply any bias they are given and ignore |
| 374 | // this flag. |
| 375 | bool use_bias = false; |
| 376 | // n_teams note: this flag is just for the infrastructure. |
| 377 | int n_teams = 1; |
| 378 | // min_ms note: also just for the infrastructure. |
| 379 | unsigned int min_ms = 0; |
| 380 | // cache_stats: also just for the infrastructure. |
| 381 | bool cache_stats = false; |
| 382 | bool cache_flush = false; |
| 383 | unsigned int schedule_shape_override = 0; |
| 384 | Activation act = {}; |
| 385 | ConvStrategy strategy = ConvStrategy::fail; |
| 386 | // Winograd specific configuration filters |
| 387 | WinogradProblem winograd_args; |
| 388 | // File to dump problem output to |
| 389 | std::string dump_file = {}; |
| 390 | |
| 391 | |
| 392 | // Is this a valid mapping of a GEMM problem as per the criteria above? |
| 393 | bool is_basic_gemm(void) { |
| 394 | if (kernel_height != 1 || kernel_width != 1) |
| 395 | return false; |
| 396 | |
| 397 | if (out_stride_h != 1 || out_stride_w != 1) |
| 398 | return false; |
| 399 | |
| 400 | if (in_stride_h != 1 || in_stride_w != 1) |
| 401 | return false; |
| 402 | |
| 403 | if (input_height != 1 || output_height != 1) |
| 404 | return false; |
| 405 | |
| 406 | if (input_width != output_width) |
| 407 | return false; |
| 408 | |
| 409 | if (groups != 1) |
| 410 | return false; |
| 411 | |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | // Is this a valid combination of parameters? |
| 416 | bool validate(void) { |
| 417 | // Grouping must divide channels evenly. |
| 418 | if (input_channels % groups) { |
| 419 | printf("Error: groups (%lld) is not a foctor of input_channels (%lld).\n",(long long)groups, (long long)input_channels); |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | if (output_channels % groups) { |
| 424 | printf("Error: groups (%lld) is not a factor of output_channels (%lld).\n",(long long)groups, (long long)output_channels); |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | return true; |
| 429 | } |
| 430 | }; |
| 431 | |
| 432 | template<typename T> |
| 433 | struct DeltaType { |
| 434 | typedef T t; |
| 435 | }; |
| 436 | |
| 437 | template<> |
| 438 | struct DeltaType<int8_t> { |
| 439 | typedef int t; |
| 440 | }; |
| 441 | |
| 442 | template<> |
| 443 | struct DeltaType<int16_t> { |
| 444 | typedef int t; |
| 445 | }; |
| 446 | |
| 447 | // Matrix class: Stores matrices for use by the GEMM kernels. |
| 448 | // Also includes various functions for randomization, setting test patterns, transposing, interleaving, etc. |
| 449 | template <typename T> |
| 450 | class Matrix { |
| 451 | public: |
| 452 | T *data=nullptr; |
| 453 | int M; |
| 454 | int N; |
| 455 | int batches; |
| 456 | int multis; |
| 457 | int stride; |
| 458 | int batch_stride=0; |
| 459 | int multi_stride=0; |
| 460 | const bool is_clone=false; // clones don't own their data |
| 461 | |
| 462 | Matrix(Matrix &) = delete; |
| 463 | Matrix operator=(Matrix &) = delete; |
| 464 | |
| 465 | // Constructor for clones / submatrices |
| 466 | Matrix(T *data, int M, int N, int batches, int multis, int stride, int batch_stride, int multi_stride) |
| 467 | : data(data), M(M), N(N), batches(batches), multis(multis), stride(stride), |
| 468 | batch_stride(batch_stride), multi_stride(multi_stride), is_clone(true) { } |
| 469 | |
| 470 | Matrix(int M, int N, int stride, int batches=1, int multis=1) : M(M), N(N), batches(batches), multis(multis), stride(stride) { |
| 471 | // Do some aligning of batches and multis, this will make sure the right strides are being used. |
| 472 | int batch_size = M*N*sizeof(T); |
| 473 | |
| 474 | // Make batch size an exact number of cache lines. If it already |
| 475 | // is, add an empty line of padding (by adding 0x40 instead of |
| 476 | // 0x3F). |
| 477 | batch_size = (batch_size + 0x40) & (~0x3F); |
| 478 | |
| 479 | batch_stride = batch_size / sizeof(T); |
| 480 | |
| 481 | int multi_size = batch_size * batches; |
| 482 | |
| 483 | // Make multi size an exact number of pages. |
| 484 | multi_size = (multi_size + 0xFFF) & (~0xFFF); |
| 485 | |
| 486 | multi_stride = multi_size / sizeof(T); |
| 487 | |
| 488 | data = (T *)allocate_aligned_memory(4096, (multi_size * (multis-1)) + |
| 489 | (batch_size * (batches-1)) + |
| 490 | (sizeof(T) * (stride * (M-1) + N))); |
| 491 | // printf("New Matrix, size (%d, %d, %d, %d)\n", M, N, batches, multis); |
| 492 | |
| 493 | if (data == nullptr) { |
| 494 | printf("Matrix: memory allocation error.\n"); |
| 495 | exit(1); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | Matrix(int M, int N) : M(M), N(N), batches(1), multis(1), stride(N), batch_stride(0), multi_stride(0) { |
| 500 | data = (T *)allocate_aligned_memory(4096, M * N * sizeof(T)); |
| 501 | } |
| 502 | |
| 503 | ~Matrix() { |
| 504 | if (!is_clone) { |
| 505 | free_aligned_memory(data); |
| 506 | } |
| 507 | // printf("Destroyed Matrix, size (%d, %d)\n", M, N); |
| 508 | } |
| 509 | |
| 510 | // For performance reasons it's expected that normal users will just access data directly (it's public). |
| 511 | // Provide this helper function for non-performance critical stuff like transposes. |
| 512 | T safe_read(int multi, int batch, int y, int x) const { |
| 513 | if ((y>=0) && (x>=0) && (y < M) && (x < N) && (multi < multis) && (batch < batches)) { |
| 514 | return data[multi*multi_stride + batch*batch_stride + y*stride + x]; |
| 515 | } |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | void safe_set(int multi, int batch, int y, int x, T v) { |
| 521 | if ((y>=0) && (x>=0) && (y<M) && (x<N) && (multi<multis) && (batch<batches)) { |
| 522 | data[multi*multi_stride + batch*batch_stride + y*stride + x] = v; |
| 523 | } else { |
| 524 | printf("Invalid write: %d, %d, %d, %d [ %d, %d, %d, %d ]\n",multi,batch,y,x,M,N,multis,batches); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | void dump_out(FILE *fp) { |
| 529 | // Dump data size and matrix dimensions as a check. |
| 530 | size_t data_size = sizeof(T); |
| 531 | fwrite(&data_size, sizeof(size_t), 1, fp); |
| 532 | fwrite(&M, sizeof(M), 1, fp); |
| 533 | fwrite(&N, sizeof(N), 1, fp); |
| 534 | fwrite(&batches, sizeof(batches), 1, fp); |
| 535 | fwrite(&multis, sizeof(multis), 1, fp); |
| 536 | |
| 537 | for (int m=0; m<multis; m++) { |
| 538 | for (int b=0; b<batches; b++) { |
| 539 | for (int y=0; y<M; y++) { |
| 540 | fwrite(data + m*multi_stride + b*batch_stride + y*stride, sizeof(T), N, fp); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | void read_dump(FILE *fp) { |
| 547 | size_t dump_size; |
| 548 | |
| 549 | fread(&dump_size, sizeof(size_t), 1, fp); |
| 550 | |
| 551 | if (dump_size != sizeof(T)) { |
| 552 | printf("read_dump(): Data size mismatch.\n"); |
| 553 | exit(1); |
| 554 | } |
| 555 | |
| 556 | int check_dims[] = { M, N, batches, multis }; |
| 557 | |
| 558 | for (unsigned int i=0; i<4; i++) { |
| 559 | int dim; |
| 560 | fread(&dim, sizeof(dim), 1, fp); |
| 561 | |
| 562 | if (dim != check_dims[i]) { |
| 563 | printf("read_dump(): Incorrect dimension %u.\n", i); |
| 564 | exit(1); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | for (int m=0; m<multis; m++) { |
| 569 | for (int b=0; b<batches; b++) { |
| 570 | for (int y=0; y<M; y++) { |
| 571 | fread(data + m*multi_stride + b*batch_stride + y*stride, sizeof(T), N, fp); |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | void RandomizeBits(int acc_bits, int res_bits=0, bool spam=false) { |
| 578 | if (res_bits == 0) { |
| 579 | res_bits = acc_bits; |
| 580 | } |
| 581 | |
| 582 | for(int m=0; m<multis; m++) { |
| 583 | for(int b=0; b<batches; b++) { |
| 584 | for(int y=0; y<M; y++) { |
| 585 | for(int x=0; x<N; x++) { |
| 586 | // Random number generation outsourced to function above to |
| 587 | // allow it to be specialized for particular types. |
| 588 | double res = get_random_number_bits<double>(acc_bits, std::is_unsigned<T>::value); |
| 589 | if (res_bits < acc_bits) { |
| 590 | int divisor = 1 << (acc_bits - res_bits + 1); |
| 591 | res = res / (double)divisor; |
| 592 | } |
| 593 | data[(m*multi_stride) + (b * batch_stride) + (y*stride) + x] = res; |
| 594 | if (spam) { |
| 595 | printf("Random %d,%d: %f (%f)\n", acc_bits, res_bits, res, (double)data[(m*multi_stride) + (b * batch_stride) + (y*stride) + x]); |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | #ifdef SLOW_RANDOMIZE |
| 604 | void Randomize(void) { |
| 605 | for(int m=0; m<multis; m++) { |
| 606 | for(int b=0; b<batches; b++) { |
| 607 | for(int y=0; y<M; y++) { |
| 608 | for(int x=0; x<N; x++) { |
| 609 | // Random number generation outsourced to function above to |
| 610 | // allow it to be specialized for particular types. |
| 611 | data[(m*multi_stride) + (b * batch_stride) + (y*stride) + x] = get_random_number<T>(); |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | void RandomizeOrNaN(void) { |
| 619 | for(int m=0; m<multis; m++) { |
| 620 | for(int b=0; b<batches; b++) { |
| 621 | for(int y=0; y<M; y++) { |
| 622 | for(int x=0; x<N; x++) { |
| 623 | // Random number generation outsourced to function above to |
| 624 | // allow it to be specialized for particular types. |
| 625 | data[(m*multi_stride) + (b * batch_stride) + (y*stride) + x] = get_random_number_or_NaN<T>(); |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | #else |
| 632 | // Faster randomize option: Get a buffer of prime size to minimize chance of resonance, blat across array. |
| 633 | template <bool NaN> |
| 634 | void Randomize_int(T multiplier) { |
| 635 | const size_t buffer_size = 251; |
| 636 | |
| 637 | T random_buffer[buffer_size]; |
| 638 | |
| 639 | for (unsigned int i=0; i<buffer_size; i++) { |
| 640 | if (NaN) { |
| 641 | random_buffer[i] = get_random_number_or_NaN<T>() * multiplier; |
| 642 | } else { |
| 643 | random_buffer[i] = get_random_number<T>() * multiplier; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | // This class always occupies a contiguous chunk of memory, regardless of strides. |
| 648 | // So compute the total footprint here. |
| 649 | size_t totalsize=(multis-1)*multi_stride + (batches-1)*batch_stride+(M-1)*stride+N; |
| 650 | |
| 651 | for (size_t i=0; i<totalsize; i+=buffer_size) { |
| 652 | const int64_t num = std::min(buffer_size, (totalsize-i)); |
| 653 | |
| 654 | memcpy(reinterpret_cast<void *>(data + i), reinterpret_cast<void *>(random_buffer), num * sizeof(T)); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | void Randomize(T multiplier=static_cast<T>(1)) { |
| 659 | Randomize_int<false>(multiplier); |
| 660 | } |
| 661 | |
| 662 | void RandomizeOrNaN(T multiplier=static_cast<T>(1)) { |
| 663 | Randomize_int<true>(multiplier); |
| 664 | } |
| 665 | #endif |
| 666 | |
| 667 | template<typename T2> |
| 668 | void Mask(T2 msk) { |
| 669 | for(int m=0; m<multis; m++) { |
| 670 | for (int b=0; b<batches; b++) { |
| 671 | for(int y=0; y<M; y++) { |
| 672 | for (int x=0; x<N; x++) { |
| 673 | union ugh { |
| 674 | T v1; |
| 675 | T2 v2; |
| 676 | |
| 677 | ugh() : v1() { } |
| 678 | } u; |
| 679 | |
| 680 | u.v1 = safe_read(m,b,y,x); |
| 681 | u.v2 &= msk; |
| 682 | safe_set(m,b,y,x,u.v1); |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | // Set the matrix to contain the ID matrix |
| 690 | void ID(void) { |
| 691 | for(int m=0; m<multis; m++) { |
| 692 | for (int b=0; b<batches; b++) { |
| 693 | for(int y=0; y<M; y++) { |
| 694 | for (int x=0; x<N; x++) { |
| 695 | T v=0; |
| 696 | |
| 697 | if (y==x) { |
| 698 | v=1; |
| 699 | } |
| 700 | |
| 701 | data[(m*multi_stride) + (b*batch_stride) + (y*stride)+x] = v; |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | void AllOne(void) { |
| 709 | for(int m=0; m<multis; m++) { |
| 710 | for (int b=0; b<batches; b++) { |
| 711 | for(int y=0; y<M; y++) { |
| 712 | for (int x=0; x<N; x++) { |
| 713 | data[(m*multi_stride) + (b*batch_stride) + (y*stride)+x] = 1; |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | // Set the matrix to contain a recognizable test pattern, here just |
| 721 | // increasing numbers in the top row. |
| 722 | void TestPattern(bool enumerate_columns=false, |
| 723 | bool enumerate_rows=false) { |
| 724 | for (int m=0; m<multis; m++) { |
| 725 | for (int b=0; b<batches; b++) { |
| 726 | for(int y=0; y<M; y++) { |
| 727 | for (int x=0; x<N; x++) { |
| 728 | T v=0; |
| 729 | if (!enumerate_columns && !enumerate_rows) { |
| 730 | if (y == 0) { |
| 731 | v = (m*10000) + (b * 100) + x+1; |
| 732 | } |
| 733 | } else if (enumerate_columns && enumerate_rows) { |
| 734 | v = y * N + x + 1; |
| 735 | } else if (enumerate_columns) { |
| 736 | v = x + 1; |
| 737 | } else if (enumerate_rows) { |
| 738 | v = y + 1; |
| 739 | } |
| 740 | |
| 741 | data[(m*multi_stride) + (b*batch_stride) + (y*stride)+x] = v; |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | // Zero the matrix. |
| 749 | void Zero() { |
| 750 | memset((void *)data, 0, ((multis-1) * multi_stride + (batches-1) * batch_stride + (M-1) * stride + N) * sizeof(T)); |
| 751 | } |
| 752 | |
| 753 | |
| 754 | // Copy some other matrix. |
| 755 | template<typename T2> |
| 756 | void Copy(std::shared_ptr<Matrix<T2>> from) { |
| 757 | for (int m=0; m<multis; m++) { |
| 758 | for (int b=0; b<batches; b++) { |
| 759 | for (int y=0; y<M; y++) { |
| 760 | for (int x=0; x<N; x++) { |
| 761 | data[(m*multi_stride) + (b*batch_stride) + (y * stride) + x] = static_cast<T>(from->safe_read(m, b, y, x)); |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | |
| 769 | void Transpose(std::shared_ptr<Matrix> from) { |
| 770 | for (int m=0; m<multis; m++) { |
| 771 | for (int b=0; b<batches; b++) { |
| 772 | for (int y=0; y<M; y++) { |
| 773 | for (int x=0; x<N; x++) { |
| 774 | data[(m*multi_stride) + (b*batch_stride) + (y * stride) + x] = from->safe_read(m, b, x, y); |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | // Set up an interleaved matrix such that each row contains several rows interleaved. |
| 782 | // i.e. for a normal M*N matrix interleaved by 8, size should be (M/8)*(N*8) |
| 783 | void Interleave(std::shared_ptr<Matrix> from, int int_by) { |
| 784 | for (int m=0; m<multis; m++) { |
| 785 | for (int b=0; b<batches; b++) { |
| 786 | for (int y=0; y<M; y++) { |
| 787 | T *out_ptr = data + (y * stride) + (b * batch_stride) + (m * multi_stride); |
| 788 | for (int x=0; x<(N/int_by); x++) { |
| 789 | for (int z = 0; z<int_by; z++) { |
| 790 | *out_ptr++ = from->safe_read(m, b, (y * int_by) + z, x); |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | // As above, but transposes at the same time. |
| 799 | void Interleave_Transposed(std::shared_ptr<Matrix> from, int int_by) { |
| 800 | for (int m=0; m<multis; m++) { |
| 801 | for (int b=0; b<batches; b++) { |
| 802 | for (int y=0; y<M; y++) { |
| 803 | T *out_ptr = data + (y * stride) + (b * batch_stride) + (m * multi_stride); |
| 804 | for (int x=0; x<(N/int_by); x++) { |
| 805 | for (int z = 0; z<int_by; z++) { |
| 806 | *out_ptr++ = from->safe_read(m, b, x, (y * int_by) + z); |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | // Interleave_Blocked copies a block of values at a time instead of just |
| 815 | // one. The main use of this is the gemmlowp with the "dot product" |
| 816 | // instruction, where each operation consumes 4 values, so we need to |
| 817 | // copy blocks of 4 values. |
| 818 | void Interleave_Blocked(std::shared_ptr<Matrix> from, int int_by, int block) { |
| 819 | for (int m=0; m<multis; m++) { |
| 820 | for (int b=0; b<batches; b++) { |
| 821 | for (int y=0; y<M; y++) { |
| 822 | T *out_ptr = data + (y * stride) + (b * batch_stride) + (m * multi_stride); |
| 823 | for (int x=0; x<(N/int_by); x+=block) { |
| 824 | for (int z = 0; z<int_by; z++) { |
| 825 | for (int a = 0; a<block; a++) { |
| 826 | *out_ptr++ = from->safe_read(m, b, (y * int_by) + z, x+a); |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | // As abov, but with a transpose at the same time. |
| 836 | template<typename T2> |
| 837 | void Interleave_Blocked_Transposed(std::shared_ptr<Matrix<T2>> from, int int_by, int block) { |
| 838 | for (int m=0; m<multis; m++) { |
| 839 | for (int b=0; b<batches; b++) { |
| 840 | for (int y=0; y<M; y++) { |
| 841 | T *out_ptr = data + (y * stride) + (b * batch_stride) + (m * multi_stride); |
| 842 | for (int x=0; x<(N/int_by); x+=block) { |
| 843 | for (int z = 0; z<int_by; z++) { |
| 844 | for (int a = 0; a<block; a++) { |
| 845 | *out_ptr++ = from->safe_read(m, b, x+a, (y * int_by) + z); |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | bool Compare(std::shared_ptr<Matrix> with, T threshold=get_compare_threshold<T>()) { |
| 855 | int e=0; |
| 856 | |
| 857 | for(int m=0; m<multis; m++) { |
| 858 | for (int b=0; b<batches; b++) { |
| 859 | for(int y=0; y<M; y++) { |
| 860 | for(int x=0; x<N; x++) { |
| 861 | T golden = safe_read(m, b, y, x); |
| 862 | T test = with->safe_read(m, b, y, x); |
| 863 | |
| 864 | // Make delta always positive so that things don't explode if T is an unsigned integer type. |
| 865 | typename DeltaType<T>::t delta = (golden >= test) ? (typename DeltaType<T>::t)(golden-test) : (typename DeltaType<T>::t)(test-golden); |
| 866 | |
| 867 | int compare_ok = 1; |
| 868 | |
| 869 | if (std::is_integral<T>::value) { |
| 870 | if (delta > threshold) { |
| 871 | compare_ok = 0; |
| 872 | } |
| 873 | } else { |
| 874 | if (delta > threshold || std::isnan(test)) { |
| 875 | compare_ok = 0; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | if (!compare_ok) { |
| 880 | printf("Compare error: m=%d, b=%d, x=%d, y=%d, golden=%g, test=%g, delta=%g, test addr=%p\n",m,b,x,y,(double)golden,(double)test,(double)delta,(void *)(with->data + m*with->multi_stride + b*with->batch_stride + y*with->stride + x)); |
| 881 | e++; |
| 882 | if (e>5000) { |
| 883 | printf("Too many errors, not printing any more.\n"); |
| 884 | goto abort; |
| 885 | } |
| 886 | } else { |
| 887 | // printf("Compare: m=%d, b=%d, x=%d, y=%d, golden=%g, test=%g, delta=%g\n",m,b,x,y,(double)golden, (double)test, (double)delta); |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | abort: |
| 895 | if(e) { |
| 896 | printf("Compare errors :(\n"); |
| 897 | return false; |
| 898 | } else { |
| 899 | printf("Compare OK.\n"); |
| 900 | return true; |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | // Quantization related functions |
| 905 | // This one returns a vector of pairs of min/max values. |
| 906 | // If per_channel is true, the length of the vector is equal to the s |
| 907 | template <bool per_channel=false> |
| 908 | std::vector<std::pair<T, T> > get_range() { |
| 909 | unsigned int l = per_channel ? N : 1; |
| 910 | |
| 911 | std::vector<std::pair<T, T> > r; |
| 912 | |
| 913 | for (unsigned int i=0; i<l; i++) { |
| 914 | r.emplace_back(std::make_pair(std::numeric_limits<T>::max(), std::numeric_limits<T>::min())); |
| 915 | } |
| 916 | |
| 917 | for (int m=0; m<multis; m++) { |
| 918 | for (int b=0; b<batches; b++) { |
| 919 | for (int y=0; y<M; y++) { |
| 920 | for (int x=0; x<N; x++) { |
| 921 | int n = per_channel ? x : 0; |
| 922 | |
| 923 | T v = safe_read(m, b, y, x); |
| 924 | |
| 925 | if (v < r[n].first) { |
| 926 | r[n].first = v; |
| 927 | } |
| 928 | |
| 929 | if (v > r[n].second) { |
| 930 | r[n].second = v; |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | return r; |
| 938 | } |
| 939 | |
| 940 | template<typename T_from> |
| 941 | void Quantize(std::shared_ptr<Matrix<T_from> > from, QuantizeParameters<T, T_from> ¶ms) { |
| 942 | for (int m=0; m<multis; m++) { |
| 943 | for (int b=0; b<batches; b++) { |
| 944 | for (int y=0; y<M; y++) { |
| 945 | for (int x=0; x<N; x++) { |
| 946 | data[(m*multi_stride) + (b*batch_stride) + (y * stride) + x] = params.quantize(from->safe_read(m, b, y, x)); |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | template<typename T_from> |
| 954 | void Quantize(std::shared_ptr<Matrix<T_from> > from, std::vector< QuantizeParameters<T, T_from> > ¶ms) { |
| 955 | if (params.size() == 1) { |
| 956 | return Quantize(from, params[0]); |
| 957 | } |
| 958 | |
| 959 | for (int m=0; m<multis; m++) { |
| 960 | for (int b=0; b<batches; b++) { |
| 961 | for (int y=0; y<M; y++) { |
| 962 | for (int x=0; x<N; x++) { |
| 963 | data[(m*multi_stride) + (b*batch_stride) + (y * stride) + x] = params[x].quantize(from->safe_read(m, b, y, x)); |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | template<typename Tint> |
| 971 | void QuantizeRound(QuantizeParameters<Tint, T> ¶ms) { |
| 972 | for (int m=0; m<multis; m++) { |
| 973 | for (int b=0; b<batches; b++) { |
| 974 | for (int y=0; y<M; y++) { |
| 975 | for (int x=0; x<N; x++) { |
| 976 | T &v = data[(m*multi_stride) + (b*batch_stride) + (y * stride) + x]; |
| 977 | v = params.dequantize(params.quantize(v)); |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | template<typename Tint> |
| 985 | void QuantizeRound(std::vector<QuantizeParameters<Tint, T> > ¶ms) { |
| 986 | if (params.size() == 1) { |
| 987 | return QuantizeRound(params[0]); |
| 988 | } |
| 989 | for (int m=0; m<multis; m++) { |
| 990 | for (int b=0; b<batches; b++) { |
| 991 | for (int y=0; y<M; y++) { |
| 992 | for (int x=0; x<N; x++) { |
| 993 | T &v = data[(m*multi_stride) + (b*batch_stride) + (y * stride) + x]; |
| 994 | v = params[x].dequantize(params[x].quantize(v)); |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | template<typename T_from, typename T_float> |
| 1002 | void Dequantize(std::shared_ptr<Matrix<T_from> > from, QuantizeParameters<T_from, T_float> ¶ms) { |
| 1003 | for (int m=0; m<multis; m++) { |
| 1004 | for (int b=0; b<batches; b++) { |
| 1005 | for (int y=0; y<M; y++) { |
| 1006 | for (int x=0; x<N; x++) { |
| 1007 | data[(m*multi_stride) + (b*batch_stride) + (y * stride) + x] = params.dequantize(from->safe_read(m, b, y, x)); |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | // Return submatrix with a subset of rows |
| 1015 | std::shared_ptr<Matrix<T>> split_rows(int M_start, int num_rows) { |
| 1016 | return std::make_shared<Matrix<T>>(data + (M_start * stride), num_rows, N, batches, multis, stride, batch_stride, multi_stride); |
| 1017 | } |
| 1018 | |
| 1019 | // Return submatrix with a subset of columns |
| 1020 | std::shared_ptr<Matrix<T>> split_cols(int N_start, int num_cols) { |
| 1021 | return std::make_shared<Matrix<T>>(data + N_start, M, num_cols, batches, multis, stride, batch_stride, multi_stride); |
| 1022 | } |
| 1023 | }; |
| 1024 | |
| 1025 | // Enum used to identify which type of quantization is used by a particular kernel. |
| 1026 | enum class QuantizationType |
| 1027 | { |
| 1028 | NONE, |
| 1029 | INTEGER, |
| 1030 | FLOAT |
| 1031 | }; |
| 1032 | |
| 1033 | class Requantize32 : public kai::ops::Requantize32 { |
| 1034 | private: |
| 1035 | std::vector<int32_t> mults; |
| 1036 | std::vector<int32_t> left_shifts; |
| 1037 | std::vector<int32_t> right_shifts; |
| 1038 | |
| 1039 | template<typename Tfloat> |
| 1040 | static std::pair<int32_t, int32_t> get_mul_shift(Tfloat rescale) { |
| 1041 | const Tfloat shiftf = ::round(::log2(0.5f / rescale)); |
| 1042 | const Tfloat multf = ::exp2(31.0f + shiftf)*rescale; |
| 1043 | |
| 1044 | int64_t shift = static_cast<int64_t>(shiftf); |
| 1045 | int64_t mult = static_cast<int64_t>(multf); |
| 1046 | |
| 1047 | if (mult == (1ll << 31)) |
| 1048 | { |
| 1049 | mult /= 2; |
| 1050 | shift--; |
| 1051 | } |
| 1052 | |
| 1053 | // assert(shift >= 0); |
| 1054 | assert(mult <= std::numeric_limits<int32_t>::max()); |
| 1055 | |
| 1056 | return std::pair<int32_t, int32_t>(-shift, mult); |
| 1057 | } |
| 1058 | |
| 1059 | public: |
| 1060 | template<typename Tfloat, typename Tint> |
| 1061 | void set_multipliers(Tfloat multiplier, const std::vector<QuantizeParameters<Tint, Tfloat> > &qp_B) { |
| 1062 | if (qp_B.size()==1) { |
| 1063 | auto p = get_mul_shift(multiplier * qp_B[0].m_scale); |
| 1064 | this->per_channel_requant = false; |
| 1065 | this->per_layer_right_shift = std::min(p.first, 0); |
| 1066 | this->per_layer_left_shift = std::max(p.first, 0); |
| 1067 | this->per_layer_mul = p.second; |
| 1068 | } else { |
| 1069 | auto rounded_size = round_up_to_nearest_multiple(qp_B.size(), 32); |
| 1070 | bool need_left_shift = false; |
| 1071 | this->per_channel_requant = true; |
| 1072 | for (auto &&i : qp_B) { |
| 1073 | auto p = get_mul_shift(multiplier * i.m_scale); |
| 1074 | left_shifts.push_back(std::max(p.first, 0)); |
| 1075 | right_shifts.push_back(std::min(p.first, 0)); |
| 1076 | if (p.first > 0) { |
| 1077 | need_left_shift=true; |
| 1078 | } |
| 1079 | mults.push_back(p.second); |
| 1080 | } |
| 1081 | left_shifts.resize(rounded_size); |
| 1082 | right_shifts.resize(rounded_size); |
| 1083 | mults.resize(rounded_size); |
| 1084 | this->per_channel_right_shifts=right_shifts.data(); |
| 1085 | this->per_channel_left_shifts=need_left_shift ? left_shifts.data() : nullptr; |
| 1086 | this->per_channel_muls=mults.data(); |
| 1087 | } |
| 1088 | } |
| 1089 | }; |
| 1090 | |
| 1091 | |
| 1092 | // Cache flush |
| 1093 | // |
| 1094 | // Flush every CACHE_LINE_SIZE sized block overlapping the stated [base, base+len) region |
| 1095 | #ifdef BARE_METAL |
| 1096 | |
| 1097 | #define CACHE_LINE_SIZE 64 |
| 1098 | |
| 1099 | static inline void do_cache_flush(void *base, size_t len) { |
| 1100 | uintptr_t mask = (CACHE_LINE_SIZE - 1); |
| 1101 | uintptr_t start = reinterpret_cast<uintptr_t>(base); |
| 1102 | uintptr_t end = start + len; |
| 1103 | |
| 1104 | if (len==0) { |
| 1105 | return; |
| 1106 | } |
| 1107 | |
| 1108 | // Align blocks we clear to cache lines; DC CIVAC does not require an |
| 1109 | // aligned address but aligning here is the easiest way of making sure |
| 1110 | // we hit all the correct blocks. |
| 1111 | start &= ~mask; |
| 1112 | |
| 1113 | for (uintptr_t addr = start; addr < end; addr+=CACHE_LINE_SIZE) { |
| 1114 | __asm( |
| 1115 | "dc civac, %[addr]" |
| 1116 | : |
| 1117 | : [addr] "r" (addr) |
| 1118 | : "memory" |
| 1119 | ); |
| 1120 | } |
| 1121 | |
| 1122 | __asm("dsb sy" ::: "memory"); |
| 1123 | } |
| 1124 | |
| 1125 | #endif // BARE_METAL |
| 1126 | |
| 1127 | /* Prototypes from test_suite.cpp */ |
| 1128 | void print_test_suites(); |
| 1129 | bool get_test_config(GemmProblem *p, std::string name, unsigned int ID, std::string implname, bool wg_ok, bool conv_ok); |
| 1130 | |
| 1131 | /* Prototypes from file_dumper.cpp */ |
| 1132 | FILE *write_dump(const GemmProblem *p, const char *filename); |
| 1133 | FILE *read_dump(GemmProblem *p, const char *filename); |