1 /*******************************************************************************
2 * Copyright 2018 Intel Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
19 #include "math_utils.hpp"
20 #include "mkldnn_thread.hpp"
23 #include "../f32/ref_gemm_f32.hpp"
24 #include "jit_generator.hpp"
30 template <typename b_dt>
31 mkldnn_status_t ref_gemm_s8x8s32(const char *transa, const char *transb,
32 const char *offsetc, const int *M, const int *N, const int *K,
33 const float *alpha, const int8_t *A, const int *LDA, const int8_t *ao,
34 const b_dt *B, const int *LDB, const int8_t *bo, const float *beta,
35 int32_t *C, const int *LDC, const int32_t *co) {
37 if (*M == 0 || *N == 0 || *K == 0)
38 return mkldnn_success;
40 bool OCisR = (*offsetc == 'R' || *offsetc == 'r');
41 bool OCisC = (*offsetc == 'C' || *offsetc == 'c');
42 bool AisN = (*transa == 'N' || *transa == 'n');
43 bool BisN = (*transb == 'N' || *transb == 'n');
45 int m = *M, n = *N, k = *K, lda = *LDA, ldb = *LDB, ldc = *LDC;
46 size_t sizeA = AisN ? lda * k : lda * m;
47 size_t sizeB = BisN ? ldb * n : ldb * k;
48 size_t sizeC = ldc * n;
50 double *dA = (double *)malloc(sizeA * sizeof(double), PAGE_4K);
51 double *dB = (double *)malloc(sizeB * sizeof(double), PAGE_4K);
52 double *dC = (double *)malloc(sizeC * sizeof(double), PAGE_4K);
54 if (utils::any_null(dA, dB, dC)) {
58 return mkldnn_out_of_memory;
61 auto da_setter = [=] (int i, int j, double v) { dA[j * lda + i] = v; };
62 auto db_setter = [=] (int i, int j, double v) { dB[j * ldb + i] = v; };
64 auto ia_accessor = [=] (int i, int j) { return A[j * lda + i]; };
65 auto ib_accessor = [=] (int i, int j) { return B[j * ldb + i]; };
67 const int a_rows = AisN ? m : k;
68 const int a_cols = AisN ? k : m;
69 mkldnn::impl::parallel_nd(a_cols, a_rows, [&](int j, int i) {
71 static_cast<double>(ia_accessor(i, j)) + static_cast<double>(ao[0]));
74 const int b_rows = BisN ? k : n;
75 const int b_cols = BisN ? n : k;
76 mkldnn::impl::parallel_nd(b_cols, b_rows, [&](int j, int i) {
78 static_cast<double>(ib_accessor(i, j)) + static_cast<double>(bo[0]));
80 double one = 1.0, zero = 0.0;
81 ref_gemm<double>(transa, transb, M, N, K, &one, dA, LDA, dB, LDB, &zero,
84 auto i2d = [=] (int32_t v) { return static_cast<double>(v); };
85 auto f2d = [=] (float v) { return static_cast<double>(v); };
87 mkldnn::impl::parallel_nd(n, m, [&] (int j, int i) {
88 double coffset = OCisR ? i2d(co[j]) : OCisC ? i2d(co[i]) : i2d(co[0]);
89 double val = ((*beta == 0.0f) ? 0.0 : f2d(*beta) * i2d(C[i + j * ldc]))
90 + f2d(*alpha) * dC[i + j * ldc] + coffset;
91 C[i + j * ldc] = math::out_round<int32_t>(math::saturate<int32_t>(val));
97 return mkldnn_success;
100 template mkldnn_status_t ref_gemm_s8x8s32<uint8_t>(
101 const char *transa, const char *transb, const char *offsetc,
102 const int *M, const int *N, const int *K,
103 const float *alpha, const int8_t *A, const int *LDA, const int8_t *ao,
104 const uint8_t *B, const int *LDB, const int8_t *bo,
105 const float *beta, int32_t *C, const int *LDC, const int32_t *co);
107 template mkldnn_status_t ref_gemm_s8x8s32<int8_t>(
108 const char *transa, const char *transb, const char *offsetc,
109 const int *M, const int *N, const int *K,
110 const float *alpha, const int8_t *A, const int *LDA, const int8_t *ao,
111 const int8_t *B, const int *LDB, const int8_t *bo,
112 const float *beta, int32_t *C, const int *LDC, const int32_t *co);