| 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 | // Routine to dump a problem to a file, and read it back. |
| 8 | |
| 9 | #include <cinttypes> |
| 10 | #include <cstdint> |
| 11 | #include <cstdio> |
| 12 | |
| 13 | #include "kai/ops/gemm/kai_ops.hpp" |
| 14 | #include "gemm_lib.hpp" |
| 15 | |
| 16 | #define MAGIC 0x123456789abc0001 |
| 17 | |
| 18 | struct problem_dump { |
| 19 | int64_t magic; |
| 20 | int64_t input_height; |
| 21 | int64_t input_width; |
| 22 | int64_t input_channels; |
| 23 | int64_t kernel_height; |
| 24 | int64_t kernel_width; |
| 25 | int64_t output_height; |
| 26 | int64_t output_width; |
| 27 | int64_t output_channels; |
| 28 | int64_t padding_top; |
| 29 | int64_t padding_left; |
| 30 | int64_t in_stride_h; |
| 31 | int64_t in_stride_w; |
| 32 | int64_t out_stride_h; |
| 33 | int64_t out_stride_w; |
| 34 | int64_t groups; |
| 35 | int64_t batches; |
| 36 | int64_t multis; |
| 37 | Activation act; |
| 38 | bool use_bias; |
| 39 | }; |
| 40 | |
| 41 | FILE *write_dump(const GemmProblem *p, const char *filename) { |
| 42 | FILE *fp; |
| 43 | |
| 44 | if ((fp = fopen(filename, "wb")) == nullptr) { |
| 45 | printf("Error: can't open dump file %s.\n", filename); |
| 46 | exit(1); |
| 47 | } |
| 48 | |
| 49 | problem_dump pd; |
| 50 | |
| 51 | pd.magic = MAGIC; |
| 52 | pd.input_height = p->input_height; |
| 53 | pd.input_width = p->input_width; |
| 54 | pd.input_channels = p->input_channels; |
| 55 | pd.kernel_height = p->kernel_height; |
| 56 | pd.kernel_width = p->kernel_width; |
| 57 | pd.output_height = p->output_height; |
| 58 | pd.output_width = p->output_width; |
| 59 | pd.output_channels = p->output_channels; |
| 60 | pd.padding_top = p->padding_top; |
| 61 | pd.padding_left = p->padding_left; |
| 62 | pd.in_stride_h = p->in_stride_h; |
| 63 | pd.in_stride_w = p->in_stride_w; |
| 64 | pd.out_stride_h = p->out_stride_h; |
| 65 | pd.out_stride_w = p->out_stride_w; |
| 66 | pd.groups = p->groups; |
| 67 | pd.batches = p->batches; |
| 68 | pd.multis = p->multis; |
| 69 | pd.act = p->act; |
| 70 | pd.use_bias = p->use_bias; |
| 71 | |
| 72 | fwrite(&pd, sizeof(problem_dump), 1, fp); |
| 73 | |
| 74 | return fp; |
| 75 | } |
| 76 | |
| 77 | FILE *read_dump(GemmProblem *p, const char *filename) { |
| 78 | FILE *fp; |
| 79 | |
| 80 | if ((fp = fopen(filename, "rb")) == nullptr) { |
| 81 | printf("Error: can't open dump file %s.\n", filename); |
| 82 | exit(1); |
| 83 | } |
| 84 | |
| 85 | problem_dump pd; |
| 86 | |
| 87 | fread(&pd, sizeof(problem_dump), 1, fp); |
| 88 | |
| 89 | if (pd.magic != MAGIC) { |
| 90 | printf("Bad dump magic: %" PRIx64 " (not %" PRIx64 ").\n", static_cast<uint64_t>(pd.magic), static_cast<uint64_t>(MAGIC)); |
| 91 | printf("This probably means the dump was made with an incompatible version of gemm-linux.\n"); |
| 92 | exit(1); |
| 93 | } |
| 94 | |
| 95 | p->input_height = pd.input_height; |
| 96 | p->input_width = pd.input_width; |
| 97 | p->input_channels = pd.input_channels; |
| 98 | p->kernel_height = pd.kernel_height; |
| 99 | p->kernel_width = pd.kernel_width; |
| 100 | p->output_height = pd.output_height; |
| 101 | p->output_width = pd.output_width; |
| 102 | p->output_channels = pd.output_channels; |
| 103 | p->padding_top = pd.padding_top; |
| 104 | p->padding_left = pd.padding_left; |
| 105 | p->in_stride_h = pd.in_stride_h; |
| 106 | p->in_stride_w = pd.in_stride_w; |
| 107 | p->out_stride_h = pd.out_stride_h; |
| 108 | p->out_stride_w = pd.out_stride_w; |
| 109 | p->groups = pd.groups; |
| 110 | p->batches = pd.batches; |
| 111 | p->multis = pd.multis; |
| 112 | p->act = pd.act; |
| 113 | p->use_bias = pd.use_bias; |
| 114 | |
| 115 | #ifndef SILENT |
| 116 | printf ("===============================================================================\n"); |
| 117 | printf ("Configuration loaded from dump:\n"); |
| 118 | printf (" Convolution: filter %" PRId64 "x%" PRId64 ", dilation %" PRId64 "x%" PRId64 ", stride %" PRId64 "x%" PRId64 ", output %" PRId64 "x%" PRId64 " (%" PRId64 " -> %" PRId64 " channels)\n", |
| 119 | p->kernel_height, p->kernel_width, p->in_stride_h, p->in_stride_w, p->out_stride_h, p->out_stride_w, |
| 120 | p->output_height, p->output_width, p->input_channels, p->output_channels); |
| 121 | printf (" Input: %" PRId64 "x%" PRId64 ", padding %" PRId64 "x%" PRId64 ", groups %" PRId64 ", batches %" PRId64 ", multis %" PRId64 ", bias %d\n", |
| 122 | p->input_height, p->input_width, p->padding_top, p->padding_left, p->groups, p->batches, p->multis, p->use_bias); |
| 123 | printf ("===============================================================================\n"); |
| 124 | #endif |
| 125 | |
| 126 | return fp; |
| 127 | } |