matrix_bit_code.cc 7.7 KB
Newer Older
Y
Yancey1989 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "matrix_bit_code.h"

namespace paddle {
namespace operators {
namespace math {

/**
 * CodeTable class should support 3 functions:
 *
 * size_t size()
 *   return the number of codes
 *
 * int getMaxCodeLength()
 *   return the maximal code length
 *
 * Code operator()(size_t i)
 *   return the i-th code. Code class is descriebed below.
 *
 * Code class should support 3 functions:
 *
 * int getLength()
 *   return the length of the code
 *
 * bool calcIndex(int bit)
 *   bit ranges from 0 to getLength() - 1
 *   return the index for the (1+bit) level parent
 *
 * bool calcBit(int bit)
 *   return true if the bit level parent is the right child of (1+bit) level
 *   parent
 *
 */

/*
   for i:
     for j < codeLength:
       op(a(i, j), b(0, index(i, j)))
*/
Y
Yancey1989 已提交
53 54 55
template <typename T, class CodeTable, class Op>
static void AddByBitCodeT(Op op, CodeTable code_table, const int64_t* codes,
                          const framework::Tensor& tmat,
Y
Yancey1989 已提交
56 57 58
                          const framework::Tensor& vec) {
  size_t num_sample = tmat.dims()[0];
  size_t width = vec.dims()[1];
Y
Yancey1989 已提交
59 60

  for (size_t i = 0; i < num_sample; ++i) {
Y
Yancey1989 已提交
61
    auto code = code_table(static_cast<size_t>(codes[i]));
Y
Yancey1989 已提交
62
    int code_length = code.get_length();
Y
Yancey1989 已提交
63
    for (int j = 0; j < code_length; ++j) {
Y
Yancey1989 已提交
64
      size_t index = code.calc_index(j);
Y
Yancey1989 已提交
65 66 67
      auto t = tmat.data<T>()[i * width + j];
      auto v = vec.data<T>()[index];
      op(t, v);
Y
Yancey1989 已提交
68 69 70 71
    }
  }
}

Y
Yancey1989 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
template <typename T, class CodeTable>
void SubByBitCodeT(CodeTable code_table, const int64_t* codes,
                   framework::Tensor& tmat) {
  // size_t max_code_length = code_table.get_max_code_length();
  size_t num_samples = tmat.dims()[0];
  size_t o_width = tmat.dims()[1];
  for (size_t i = 0; i < num_samples; ++i) {
    auto code = code_table(static_cast<size_t>(codes[i]));
    int code_length = code.get_length();
    for (int j = 0; j < code_length; ++j) {
      if (code.calc_bit(j)) {
        tmat.data<T>()[i * o_width + j] -= 1;
      }
    }
  }
Y
Yancey1989 已提交
87 88
}

Y
Yancey1989 已提交
89 90 91
template <typename T, class CodeTable>
void SumByBitCodeT(CodeTable code_table, const int64_t* codes,
                   framework::Tensor& tmat, framework::Tensor& sum,
Y
Yancey1989 已提交
92
                   const T& scale_sum) {
Y
Yancey1989 已提交
93
  // size_t max_code_length = code_table.get_max_code_length();
Y
Yancey1989 已提交
94 95 96
  size_t num_samples = tmat.dims()[0];
  size_t o_width = tmat.dims()[1];
  for (size_t i = 0; i < num_samples; ++i) {
Y
Yancey1989 已提交
97 98
    T sm = static_cast<T>(0.0);
    auto code = code_table(static_cast<size_t>(codes[i]));
Y
Yancey1989 已提交
99 100 101 102 103 104 105 106 107
    int code_length = code.get_length();
    for (int j = 0; j < code_length; ++j) {
      if (code.calc_bit(j)) {
        sm += tmat.data<T>()[i * o_width + j];
      }
    }
    sum.data<T>()[i] = scale_sum * sm;
  }
}
Y
Yancey1989 已提交
108

Y
Yancey1989 已提交
109
template <typename T>
Y
Yancey1989 已提交
110 111 112 113 114
void MatrixBitCodeFunctor<T>::Add(size_t num_classes, const int64_t* codes,
                                  framework::Tensor& tmat,
                                  const framework::Tensor& vec) {
  auto op = [](T& t, const T& v) { t += v; };
  AddByBitCodeT<T>(op, SimpleCodeTable(num_classes), codes, tmat, vec);
Y
Yancey1989 已提交
115 116
}

Y
Yancey1989 已提交
117 118 119 120 121 122 123
template <typename T>
void MatrixBitCodeFunctor<T>::AddGrad(size_t num_classes, const int64_t* codes,
                                      framework::Tensor& tmat,
                                      framework::Tensor& vec) {
  auto op = [](T& t, T& v) { v += t; };
  AddByBitCodeT<T>(op, SimpleCodeTable(num_classes), codes, tmat, vec);
}
Y
Yancey1989 已提交
124

Y
Yancey1989 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
template <typename T>
void MatrixBitCodeFunctor<T>::Sum(size_t num_classes, const int64_t* codes,
                                  framework::Tensor& tmat,
                                  framework::Tensor& sum, T scale_sum) {
  SumByBitCodeT<T>(SimpleCodeTable(num_classes), codes, tmat, sum, scale_sum);
}

template <typename T>
void MatrixBitCodeFunctor<T>::Mul(size_t num_classes, const int64_t* codes,
                                  framework::Tensor& tmat,
                                  const framework::Tensor& weight,
                                  const framework::Tensor& input) {
  size_t num_samples = tmat.dims()[0];
  size_t tmat_width = tmat.dims()[1];
  size_t input_width = input.dims()[1];
  size_t weight_width = weight.dims()[1];
  auto tmat_p = tmat.data<T>();
  auto weight_p = weight.data<T>();
  auto input_p = input.data<T>();
  auto code_table = SimpleCodeTable(num_classes);
Y
Yancey1989 已提交
145
  for (size_t i = 0; i < num_samples; ++i) {
Y
Yancey1989 已提交
146
    auto code = code_table(static_cast<size_t>(codes[i]));
Y
Yancey1989 已提交
147 148 149
    int code_length = code.get_length();
    for (int j = 0; j < code_length; ++j) {
      size_t index = code.calc_index(j);
Y
Yancey1989 已提交
150 151 152 153 154 155 156

      T sum = static_cast<T>(0.0);
      for (size_t k = 0; k < input_width; ++k) {
        sum +=
            weight_p[weight_width * index + k] * input_p[input_width * i + k];
      }
      tmat_p[i * tmat_width + j] += sum;
Y
Yancey1989 已提交
157 158 159 160 161
    }
  }
}

template <typename T>
Y
Yancey1989 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
void MatrixBitCodeFunctor<T>::MulGradWeight(size_t num_classes,
                                            const int64_t* codes,
                                            const framework::Tensor& tmat,
                                            framework::Tensor& weight,
                                            const framework::Tensor& input) {
  size_t num_samples = tmat.dims()[0];
  size_t input_width = input.dims()[1];
  size_t weight_width = weight.dims()[1];
  auto tmat_p = tmat.data<T>();
  auto weight_p = weight.data<T>();
  auto input_p = input.data<T>();
  auto code_table = SimpleCodeTable(num_classes);
  for (size_t i = 0; i < num_samples; ++i) {
    auto code = code_table(static_cast<size_t>(codes[i]));
    int code_length = code.get_length();
    for (int j = 0; j < code_length; ++j) {
      size_t index = code.calc_index(j);
Y
Yancey1989 已提交
179

Y
Yancey1989 已提交
180 181 182 183
      for (size_t k = 0; k < input_width; ++k) {
        weight_p[weight_width * index * k] +=
            tmat_p[i * weight_width * j] * input_p[input_width * i + k];
      }
Y
Yancey1989 已提交
184
    }
Y
Yancey1989 已提交
185
  }
Y
Yancey1989 已提交
186
}
Y
Yancey1989 已提交
187 188

template <typename T>
Y
Yancey1989 已提交
189 190 191 192 193
void MatrixBitCodeFunctor<T>::MulGradError(size_t num_classes,
                                           const int64_t* codes,
                                           const framework::Tensor& tmat,
                                           const framework::Tensor& weight,
                                           framework::Tensor& input) {
Y
Yancey1989 已提交
194
  size_t num_samples = tmat.dims()[0];
Y
Yancey1989 已提交
195 196 197 198 199 200 201
  size_t input_width = input.dims()[1];
  size_t weight_width = weight.dims()[1];
  auto tmat_p = tmat.data<T>();
  auto weight_p = weight.data<T>();
  auto input_p = input.data<T>();
  auto code_table = SimpleCodeTable(num_classes);

Y
Yancey1989 已提交
202
  for (size_t i = 0; i < num_samples; ++i) {
Y
Yancey1989 已提交
203
    auto code = code_table(static_cast<size_t>(codes[i]));
Y
Yancey1989 已提交
204 205
    int code_length = code.get_length();
    for (int j = 0; j < code_length; ++j) {
Y
Yancey1989 已提交
206 207 208 209 210
      size_t index = code.calc_index(j);

      for (size_t k = 0; k < input_width; ++k) {
        input_p[weight_width * index * k] +=
            tmat_p[i * weight_width * j] * weight_p[weight_width * i + k];
Y
Yancey1989 已提交
211 212 213 214 215 216
      }
    }
  }
}

template <typename T>
Y
Yancey1989 已提交
217 218
void MatrixBitCodeFunctor<T>::Sub(size_t num_classes, const int64_t* codes,
                                  framework::Tensor& tmat) {
Y
Yancey1989 已提交
219 220 221
  SubByBitCodeT<T>(SimpleCodeTable(num_classes), codes, tmat);
}

Y
Yancey1989 已提交
222 223 224
template class MatrixBitCodeFunctor<float>;
template class MatrixBitCodeFunctor<double>;

Y
Yancey1989 已提交
225 226 227
}  // namespace math
}  // namespace operators
}  // namespace paddle