Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / gemm / s8x8s32 / ref_gemm_s8x8s32.cpp
1 /*******************************************************************************
2 * Copyright 2018 Intel Corporation
3 *
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
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
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 *******************************************************************************/
16
17 #include <cstdint>
18
19 #include "math_utils.hpp"
20 #include "mkldnn_thread.hpp"
21 #include "utils.hpp"
22
23 #include "../f32/ref_gemm_f32.hpp"
24 #include "jit_generator.hpp"
25
26 namespace mkldnn {
27 namespace impl {
28 namespace cpu {
29
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) {
36
37     if (*M == 0 || *N == 0 || *K == 0)
38         return mkldnn_success;
39
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');
44
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;
49
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);
53
54     if (utils::any_null(dA, dB, dC)) {
55         free(dA);
56         free(dB);
57         free(dC);
58         return mkldnn_out_of_memory;
59     }
60
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; };
63
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]; };
66
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) {
70         da_setter(i, j,
71             static_cast<double>(ia_accessor(i, j)) + static_cast<double>(ao[0]));
72     });
73
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) {
77         db_setter(i, j,
78             static_cast<double>(ib_accessor(i, j)) + static_cast<double>(bo[0]));
79     });
80     double one = 1.0, zero = 0.0;
81     ref_gemm<double>(transa, transb, M, N, K, &one, dA, LDA, dB, LDB, &zero,
82         dC, LDC, nullptr);
83
84     auto i2d = [=] (int32_t v) { return static_cast<double>(v); };
85     auto f2d = [=] (float v)   { return static_cast<double>(v); };
86
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));
92     });
93
94     free(dA);
95     free(dB);
96     free(dC);
97     return mkldnn_success;
98 }
99
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);
106
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);
113
114 }
115 }
116 }