|
| 1 | +/*************************************************************************** |
| 2 | +Copyright (c) 2020,2025 The OpenBLAS Project |
| 3 | +All rights reserved. |
| 4 | +Redistribution and use in source and binary forms, with or without |
| 5 | +modification, are permitted provided that the following conditions are |
| 6 | +met: |
| 7 | +1. Redistributions of source code must retain the above copyright |
| 8 | +notice, this list of conditions and the following disclaimer. |
| 9 | +2. Redistributions in binary form must reproduce the above copyright |
| 10 | +notice, this list of conditions and the following disclaimer in |
| 11 | +the documentation and/or other materials provided with the |
| 12 | +distribution. |
| 13 | +3. Neither the name of the OpenBLAS project nor the names of |
| 14 | +its contributors may be used to endorse or promote products |
| 15 | +derived from this software without specific prior written permission. |
| 16 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | +ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBLAS PROJECT OR CONTRIBUTORS BE |
| 20 | +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 25 | +USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | +*****************************************************************************/ |
| 27 | +#include <stdio.h> |
| 28 | +#include <stdint.h> |
| 29 | +#include "../common.h" |
| 30 | + |
| 31 | +#include "test_helpers.h" |
| 32 | + |
| 33 | +#define SGEMV BLASFUNC(sgemv) |
| 34 | +#define SBGEMV BLASFUNC(sbgemv) |
| 35 | +#define SBGEMV_LARGEST 256 |
| 36 | + |
| 37 | +int |
| 38 | +main (int argc, char *argv[]) |
| 39 | +{ |
| 40 | + blasint k; |
| 41 | + int i, j, l; |
| 42 | + blasint x, y; |
| 43 | + int ret = 0; |
| 44 | + int loop = SBGEMV_LARGEST; |
| 45 | + char transA = 'N'; |
| 46 | + float alpha = 1.0, beta = 0.0; |
| 47 | + |
| 48 | + for (beta = 0; beta < 3; beta += 1) { |
| 49 | + for (alpha = 0; alpha < 3; alpha += 1) { |
| 50 | + for (l = 0; l < 2; l++) { // l = 1 to test inc_x & inc_y not equal to one. |
| 51 | + for (x = 1; x <= loop; x++) |
| 52 | + { |
| 53 | + k = (x == 0) ? 0 : l + 1; |
| 54 | + float *A = (float *)malloc_safe(x * x * sizeof(FLOAT)); |
| 55 | + float *B = (float *)malloc_safe(x * sizeof(FLOAT) << l); |
| 56 | + float *C = (float *)malloc_safe(x * sizeof(FLOAT) << l); |
| 57 | + bfloat16 *AA = (bfloat16 *)malloc_safe(x * x * sizeof(bfloat16)); |
| 58 | + bfloat16 *BB = (bfloat16 *)malloc_safe(x * sizeof(bfloat16) << l); |
| 59 | + float *DD = (float *)malloc_safe(x * sizeof(FLOAT)); |
| 60 | + float *CC = (float *)malloc_safe(x * sizeof(FLOAT) << l); |
| 61 | + if ((A == NULL) || (B == NULL) || (C == NULL) || (AA == NULL) || (BB == NULL) || |
| 62 | + (DD == NULL) || (CC == NULL)) |
| 63 | + return 1; |
| 64 | + blasint one = 1; |
| 65 | + |
| 66 | + for (j = 0; j < x; j++) |
| 67 | + { |
| 68 | + for (i = 0; i < x; i++) |
| 69 | + { |
| 70 | + A[j * x + i] = ((FLOAT) rand () / (FLOAT) RAND_MAX) + 0.5; |
| 71 | + sbstobf16_(&one, &A[j*x+i], &one, &AA[j * x + i], &one); |
| 72 | + } |
| 73 | + B[j << l] = ((FLOAT) rand () / (FLOAT) RAND_MAX) + 0.5; |
| 74 | + sbstobf16_(&one, &B[j << l], &one, &BB[j << l], &one); |
| 75 | + |
| 76 | + CC[j << l] = C[j << l] = ((FLOAT) rand () / (FLOAT) RAND_MAX) + 0.5; |
| 77 | + } |
| 78 | + |
| 79 | + for (y = 0; y < 2; y++) |
| 80 | + { |
| 81 | + if (y == 0) { |
| 82 | + transA = 'N'; |
| 83 | + } else { |
| 84 | + transA = 'T'; |
| 85 | + } |
| 86 | + |
| 87 | + memset(CC, 0, x * sizeof(FLOAT) << l); |
| 88 | + memset(DD, 0, x * sizeof(FLOAT)); |
| 89 | + memset(C, 0, x * sizeof(FLOAT) << l); |
| 90 | + |
| 91 | + SGEMV (&transA, &x, &x, &alpha, A, &x, B, &k, &beta, C, &k); |
| 92 | + SBGEMV (&transA, &x, &x, &alpha, (bfloat16*) AA, &x, (bfloat16*) BB, &k, &beta, CC, &k); |
| 93 | + |
| 94 | + for (int i = 0; i < x; i ++) DD[i] *= beta; |
| 95 | + |
| 96 | + for (j = 0; j < x; j++) |
| 97 | + for (i = 0; i < x; i++) |
| 98 | + if (transA == 'N') { |
| 99 | + DD[i] += alpha * float16to32 (AA[j * x + i]) * float16to32 (BB[j << l]); |
| 100 | + } else if (transA == 'T') { |
| 101 | + DD[j] += alpha * float16to32 (AA[j * x + i]) * float16to32 (BB[i << l]); |
| 102 | + } |
| 103 | + |
| 104 | + for (j = 0; j < x; j++) { |
| 105 | + if (!is_close(CC[j << l], C[j << l], 0.01, 0.001)) { |
| 106 | + ret++; |
| 107 | + } |
| 108 | + if (!is_close(CC[j << l], DD[j], 0.001, 0.0001)) { |
| 109 | + ret++; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + free(A); |
| 114 | + free(B); |
| 115 | + free(C); |
| 116 | + free(AA); |
| 117 | + free(BB); |
| 118 | + free(DD); |
| 119 | + free(CC); |
| 120 | + } // x |
| 121 | + } // l |
| 122 | + } // alpha |
| 123 | + } // beta |
| 124 | + |
| 125 | + if (ret != 0) |
| 126 | + fprintf (stderr, "FATAL ERROR SBGEMV - Return code: %d\n", ret); |
| 127 | + return ret; |
| 128 | +} |
0 commit comments