matrix_bit_code.cc 7.0 KB
Newer Older
Y
Yancey1989 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

W
weixing02 已提交
15 16
#include "paddle/fluid/operators/math/matrix_bit_code.h"
#include <iostream>
Y
Yancey1989 已提交
17 18 19 20 21 22 23 24
namespace paddle {
namespace operators {
namespace math {

/**
 * CodeTable class should support 3 functions:
 *
 * size_t size()
Y
Yancey1989 已提交
25
 *   return the number of ids
Y
Yancey1989 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 *
 * 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
 *
 */
Y
Yancey1989 已提交
47
template <typename T>
W
weixing02 已提交
48
void MatrixBitCodeFunctor<T>::Add(framework::Tensor* tmat,
Y
Yancey1989 已提交
49 50
                                  const framework::Tensor& vec) {
  SimpleCodeTable code_table(num_classes_);
W
weixing02 已提交
51 52
  size_t batch_size = tmat->dims()[0];
  size_t width = tmat->dims()[1];
Y
Yancey1989 已提交
53 54
  for (size_t i = 0; i < batch_size; ++i) {
    auto code = code_table(static_cast<size_t>(ids_[i]));
Y
Yancey1989 已提交
55
    int code_length = code.get_length();
Y
Yancey1989 已提交
56
    for (int j = 0; j < code_length; ++j) {
Y
Yancey1989 已提交
57
      size_t index = code.calc_index(j);
W
weixing02 已提交
58
      tmat->data<T>()[i * width + j] += vec.data<T>()[index];
Y
Yancey1989 已提交
59 60 61 62
    }
  }
}

Y
Yancey1989 已提交
63
template <typename T>
W
weixing02 已提交
64 65
void MatrixBitCodeFunctor<T>::AddGrad(const framework::Tensor& tmat,
                                      framework::Tensor* vec) {
Y
Yancey1989 已提交
66 67 68 69 70
  SimpleCodeTable code_table(num_classes_);
  size_t batch_size = tmat.dims()[0];
  size_t width = tmat.dims()[1];
  for (size_t i = 0; i < batch_size; ++i) {
    auto code = code_table(static_cast<size_t>(ids_[i]));
Y
Yancey1989 已提交
71 72
    int code_length = code.get_length();
    for (int j = 0; j < code_length; ++j) {
Y
Yancey1989 已提交
73
      size_t index = code.calc_index(j);
W
weixing02 已提交
74
      vec->data<T>()[index] += tmat.data<T>()[i * width + j];
Y
Yancey1989 已提交
75 76
    }
  }
Y
Yancey1989 已提交
77 78
}

Y
Yancey1989 已提交
79
template <typename T>
W
weixing02 已提交
80 81
void MatrixBitCodeFunctor<T>::Sum(const framework::Tensor& tmat,
                                  framework::Tensor* sum, T scale_sum) {
Y
Yancey1989 已提交
82
  SimpleCodeTable code_table(num_classes_);
Y
Yancey1989 已提交
83 84 85
  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 已提交
86
    T sm = static_cast<T>(0.0);
Y
Yancey1989 已提交
87
    auto code = code_table(static_cast<size_t>(ids_[i]));
Y
Yancey1989 已提交
88 89 90 91 92 93
    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];
      }
    }
W
weixing02 已提交
94
    sum->data<T>()[i] = scale_sum * sm;
Y
Yancey1989 已提交
95 96
  }
}
Y
Yancey1989 已提交
97

Y
Yancey1989 已提交
98
template <typename T>
W
weixing02 已提交
99
void MatrixBitCodeFunctor<T>::Mul(framework::Tensor* tmat,
Y
Yancey1989 已提交
100 101
                                  const framework::Tensor& weight,
                                  const framework::Tensor& input) {
Y
Yancey1989 已提交
102
  SimpleCodeTable code_table(num_classes_);
W
weixing02 已提交
103 104
  size_t num_samples = tmat->dims()[0];
  size_t tmat_width = tmat->dims()[1];
Y
Yancey1989 已提交
105
  size_t input_width = input.dims()[1];
W
weixing02 已提交
106 107
  size_t weight_width = weight.dims()[1];
  auto tmat_value = tmat->data<T>();
Y
Yancey1989 已提交
108 109
  auto weight_value = weight.data<T>();
  auto input_value = input.data<T>();
Y
Yancey1989 已提交
110
  for (size_t i = 0; i < num_samples; ++i) {
Y
Yancey1989 已提交
111
    auto code = code_table(static_cast<size_t>(ids_[i]));
Y
Yancey1989 已提交
112 113 114
    int code_length = code.get_length();
    for (int j = 0; j < code_length; ++j) {
      size_t index = code.calc_index(j);
Y
Yancey1989 已提交
115 116
      T sum = static_cast<T>(0.0);
      for (size_t k = 0; k < input_width; ++k) {
Y
Yancey1989 已提交
117 118
        sum += weight_value[weight_width * index + k] *
               input_value[input_width * i + k];
Y
Yancey1989 已提交
119
      }
Y
Yancey1989 已提交
120
      tmat_value[i * tmat_width + j] += sum;
Y
Yancey1989 已提交
121 122 123 124 125
    }
  }
}

template <typename T>
Y
Yancey1989 已提交
126
void MatrixBitCodeFunctor<T>::MulGradWeight(const framework::Tensor& tmat,
W
weixing02 已提交
127
                                            framework::Tensor* weight,
Y
Yancey1989 已提交
128
                                            const framework::Tensor& input) {
Y
Yancey1989 已提交
129
  SimpleCodeTable code_table(num_classes_);
Y
Yancey1989 已提交
130 131
  size_t num_samples = tmat.dims()[0];
  size_t input_width = input.dims()[1];
W
weixing02 已提交
132 133
  size_t tmat_width = tmat.dims()[1];
  size_t weight_width = weight->dims()[1];
Y
Yancey1989 已提交
134
  auto tmat_value = tmat.data<T>();
W
weixing02 已提交
135
  auto weight_value = weight->data<T>();
Y
Yancey1989 已提交
136
  auto input_value = input.data<T>();
Y
Yancey1989 已提交
137
  for (size_t i = 0; i < num_samples; ++i) {
Y
Yancey1989 已提交
138
    auto code = code_table(static_cast<size_t>(ids_[i]));
Y
Yancey1989 已提交
139 140 141
    int code_length = code.get_length();
    for (int j = 0; j < code_length; ++j) {
      size_t index = code.calc_index(j);
Y
Yancey1989 已提交
142

Y
Yancey1989 已提交
143
      for (size_t k = 0; k < input_width; ++k) {
W
weixing02 已提交
144 145
        weight_value[weight_width * index + k] +=
            tmat_value[i * tmat_width + j] * input_value[input_width * i + k];
Y
Yancey1989 已提交
146
      }
Y
Yancey1989 已提交
147
    }
Y
Yancey1989 已提交
148
  }
Y
Yancey1989 已提交
149
}
Y
Yancey1989 已提交
150 151

template <typename T>
Y
Yancey1989 已提交
152
void MatrixBitCodeFunctor<T>::MulGradError(const framework::Tensor& tmat,
Y
Yancey1989 已提交
153
                                           const framework::Tensor& weight,
W
weixing02 已提交
154
                                           framework::Tensor* input) {
Y
Yancey1989 已提交
155
  SimpleCodeTable code_table(num_classes_);
Y
Yancey1989 已提交
156
  size_t num_samples = tmat.dims()[0];
W
weixing02 已提交
157 158
  size_t tmat_width = tmat.dims()[1];
  size_t input_width = input->dims()[1];
Y
Yancey1989 已提交
159
  size_t weight_width = weight.dims()[1];
Y
Yancey1989 已提交
160 161
  auto tmat_value = tmat.data<T>();
  auto weight_value = weight.data<T>();
W
weixing02 已提交
162
  auto input_value = input->data<T>();
Y
Yancey1989 已提交
163

Y
Yancey1989 已提交
164
  for (size_t i = 0; i < num_samples; ++i) {
Y
Yancey1989 已提交
165
    auto code = code_table(static_cast<size_t>(ids_[i]));
Y
Yancey1989 已提交
166 167
    int code_length = code.get_length();
    for (int j = 0; j < code_length; ++j) {
Y
Yancey1989 已提交
168 169 170
      size_t index = code.calc_index(j);

      for (size_t k = 0; k < input_width; ++k) {
W
weixing02 已提交
171 172 173
        input_value[input_width * i + k] +=
            tmat_value[i * tmat_width + j] *
            weight_value[weight_width * index + k];
Y
Yancey1989 已提交
174 175 176 177 178 179
      }
    }
  }
}

template <typename T>
W
weixing02 已提交
180
void MatrixBitCodeFunctor<T>::Sub(framework::Tensor* tmat) {
Y
Yancey1989 已提交
181
  SimpleCodeTable code_table(num_classes_);
W
weixing02 已提交
182 183
  size_t num_samples = tmat->dims()[0];
  size_t o_width = tmat->dims()[1];
Y
Yancey1989 已提交
184 185 186 187 188
  for (size_t i = 0; i < num_samples; ++i) {
    auto code = code_table(static_cast<size_t>(ids_[i]));
    int code_length = code.get_length();
    for (int j = 0; j < code_length; ++j) {
      if (code.calc_bit(j)) {
W
weixing02 已提交
189
        tmat->data<T>()[i * o_width + j] -= 1;
Y
Yancey1989 已提交
190 191 192
      }
    }
  }
Y
Yancey1989 已提交
193 194
}

W
weixing02 已提交
195 196 197 198 199 200 201 202 203 204 205
template <typename T>
void MatrixBitCodeFunctor<T>::OutGrad(framework::Tensor* tmat,
                                      const framework::Tensor& input) {
  size_t num_samples = tmat->dims()[0];
  size_t code_length = tmat->dims()[1];
  for (size_t i = 0; i < num_samples; ++i)
    for (size_t j = 0; j < code_length; ++j) {
      tmat->data<T>()[i * code_length + j] = input.data<T>()[i];
    }
}

Y
Yancey1989 已提交
206 207 208
template class MatrixBitCodeFunctor<float>;
template class MatrixBitCodeFunctor<double>;

Y
Yancey1989 已提交
209 210 211
}  // namespace math
}  // namespace operators
}  // namespace paddle