tiny_dnn  1.0.0
A header only, dependency-free deep learning framework in C++11
tiny_quantized_matmul_kernel.h
1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 // Implements a quantized eight-bit version of the matmul operation.
17 
18 #include "gemmlowp/public/gemmlowp.h"
19 #include "tiny_dnn/core/kernels/tiny_quantization_kernel.h"
20 
21 namespace tiny_dnn {
22 namespace core {
23 namespace kernels {
24 
25 template <bool TransposeA, bool TransposeB, bool TransposeC>
26 void gemmlowp_multiply(const uint8_t* a_data,
27  const uint8_t* b_data,
28  int32_t* c_data,
29  int m,
30  int n,
31  int k,
32  int offset_a,
33  int offset_b,
34  int lda,
35  int ldb,
36  int ldc) {
37  const uint8_t* a_data_as_uint8 = a_data;
38  const uint8_t* b_data_as_uint8 = b_data;
39  int32_t* c_data_as_int32 = c_data;
40  static const gemmlowp::MapOrder ResultOrder =
41  !TransposeC ? gemmlowp::MapOrder::RowMajor : gemmlowp::MapOrder::ColMajor;
42  static const gemmlowp::MapOrder LhsOrder =
43  !TransposeA ? gemmlowp::MapOrder::RowMajor : gemmlowp::MapOrder::ColMajor;
44  static const gemmlowp::MapOrder RhsOrder =
45  !TransposeB ? gemmlowp::MapOrder::RowMajor : gemmlowp::MapOrder::ColMajor;
46  gemmlowp::MatrixMap<const std::uint8_t, LhsOrder> lhs(a_data_as_uint8, m, k,
47  lda);
48  gemmlowp::MatrixMap<const std::uint8_t, RhsOrder> rhs(b_data_as_uint8, k, n,
49  ldb);
50  gemmlowp::MatrixMap<std::int32_t, ResultOrder> result(c_data_as_int32, m, n,
51  ldc);
52  const std::tuple<> empty_pipeline = {};
53  gemmlowp::GemmContext context;
54  gemmlowp::GemmWithOutputPipeline<std::uint8_t, std::int32_t,
55  gemmlowp::DefaultL8R8BitDepthParams>(
56  &context, lhs, rhs, &result, -offset_a, -offset_b, empty_pipeline);
57 }
58 
59 template <class T1, class T2, class Toutput>
60 void tiny_quantized_matmul(const std::vector<T1>& a,
61  const std::vector<T2>& b,
62  std::vector<Toutput>& c,
63  const std::vector<size_t> shape_all,
64  const int32_t offset_a,
65  const int32_t offset_b,
66  const int32_t offset_c,
67  const int32_t mult_c,
68  const int32_t shift_c) {
69 
70  // Make sure that we have valid quantization ranges for the input buffers.
71  // If the difference between the min and max is negative or zero, it makes
72  // it hard to do meaningful intermediate operations on the values.
73 
74  int transpose_a_ = 0;
75  int transpose_b_ = 1;
76  int a_dim_remaining = 1 - transpose_a_;
77  int b_dim_remaining = 1 - transpose_b_;
78 
79  const T1* a_data = &a[0];
80  const T2* b_data = &b[0];
81  Toutput* c_data = &c[0];
82 
83  const bool transpose_c = false;
84  const size_t m = shape_all[a_dim_remaining];
85  const size_t n = shape_all[2 + b_dim_remaining];
86  const size_t k = shape_all[transpose_a_];
87  const size_t lda = shape_all[1];
88  const size_t ldb = shape_all[3];
89  const size_t ldc = n;
90 
91  // The gemmlowp optimized library only works for a particular set of data
92  // types, so check if we meet those requirements and
93  // fall back to a slower reference implementation if not.
94  if (std::is_same<T1, uint8_t>() && std::is_same<T2, uint8_t>() &&
95  std::is_same<Toutput, int32_t>() && (offset_c == 0) && (mult_c == 1) &&
96  (shift_c == 0) && (transpose_c == false)) {
97  if (transpose_a_) {
98  if (transpose_b_) {
99  gemmlowp_multiply<true, true, false>(a_data, b_data, c_data, m, n, k,
100  offset_a, offset_b, lda, ldb,
101  ldc);
102  } else {
103  gemmlowp_multiply<true, false, false>(a_data, b_data, c_data, m, n, k,
104  offset_a, offset_b, lda, ldb,
105  ldc);
106  }
107  } else {
108  if (transpose_b_) {
109  gemmlowp_multiply<false, true, false>(a_data, b_data, c_data, m, n, k,
110  offset_a, offset_b, lda, ldb,
111  ldc);
112  } else {
113  gemmlowp_multiply<false, false, false>(a_data, b_data, c_data, m, n, k,
114  offset_a, offset_b, lda, ldb,
115  ldc);
116  }
117  }
118  } /*else {
119  ReferenceGemm<T1, T2, Toutput>(
120  transpose_a_, transpose_b_, transpose_c, m, n, k, a_data, offset_a,
121  lda, b_data, offset_b, ldb, c_data, shift_c, offset_c, mult_c, ldc);
122  }
123 
124  float min_c_value;
125  float max_c_value;
126  quantization_range_for_multiplication<T1, T2, Toutput>(
127  min_a, max_a, min_b, max_b, &min_c_value, &max_c_value);*/
128  }
129 
130 }
131 }
132 } // namespace tiny_dnn