matrix_bit_code.cc 13.7 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
Yu Yang 已提交
17
#include <map>
Y
Yu Yang 已提交
18

Y
Yancey1989 已提交
19 20 21 22
namespace paddle {
namespace operators {
namespace math {

Y
Yancey1989 已提交
23
template <typename T>
Y
Yu Yang 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
struct MatrixBitCodeFunctorAdd : public boost::static_visitor<void> {
  const framework::Tensor &vec_;
  framework::Tensor *tmat_;

  MatrixBitCodeFunctorAdd(const framework::Tensor &vec, framework::Tensor *tmat)
      : vec_(vec), tmat_(tmat) {}

  template <typename CodeTable>
  void operator()(const CodeTable &code_table) {
    size_t batch_size = tmat_->dims()[0];
    size_t width = tmat_->dims()[1];
    auto *tmat_data = tmat_->data<T>();
    auto *vec_data = vec_.data<T>();
    for (size_t i = 0; i < batch_size; ++i) {
      auto code = code_table.get_code(i);
      int code_length = code.get_length();
      for (int j = 0; j < code_length; ++j) {
        size_t index = code.calc_index(j);
        tmat_data[i * width + j] += vec_data[index];
      }
Y
Yancey1989 已提交
44 45
    }
  }
Y
Yu Yang 已提交
46 47 48 49 50 51 52
};

template <typename T>
void MatrixBitCodeFunctor<T>::Add(const framework::Tensor &vec,
                                  framework::Tensor *tmat) {
  MatrixBitCodeFunctorAdd<T> func(vec, tmat);
  code_table_.apply_visitor(func);
Y
Yancey1989 已提交
53 54
}

Y
Yancey1989 已提交
55
template <typename T>
Y
Yu Yang 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
struct MatrixBitCodeFunctorAddGrad : public boost::static_visitor<void> {
  const framework::Tensor &tmat_;
  framework::Tensor *vec_;
  MatrixBitCodeFunctorAddGrad(const framework::Tensor &tmat,
                              framework::Tensor *vec)
      : tmat_(tmat), vec_(vec) {}

  template <typename CodeTable>
  void operator()(const CodeTable &table) {
    size_t batch_size = tmat_.dims()[0];
    size_t width = tmat_.dims()[1];
    auto *vec_data = vec_->data<T>();
    auto *tmat_data = tmat_.data<T>();
    for (size_t i = 0; i < batch_size; ++i) {
      auto code = table.get_code(i);
      int code_length = code.get_length();
      for (int j = 0; j < code_length; ++j) {
        size_t index = code.calc_index(j);
        vec_data[index] += tmat_data[i * width + j];
      }
Y
Yancey1989 已提交
76 77
    }
  }
Y
Yu Yang 已提交
78 79 80 81 82 83 84
};

template <typename T>
void MatrixBitCodeFunctor<T>::AddGrad(const framework::Tensor &tmat,
                                      framework::Tensor *vec) {
  MatrixBitCodeFunctorAddGrad<T> func(tmat, vec);
  code_table_.apply_visitor(func);
Y
Yancey1989 已提交
85 86
}

J
JiabinYang 已提交
87
template <typename T>
Y
Yu Yang 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
struct MatrixBitCodeFunctorSelectedRowsAddGrad
    : public boost::static_visitor<void> {
  const framework::Tensor &tmat_;
  framework::SelectedRows *vec_;

  MatrixBitCodeFunctorSelectedRowsAddGrad(const framework::Tensor &tmat,
                                          framework::SelectedRows *vec)
      : tmat_(tmat), vec_(vec) {}

  template <typename CodeTable>
  void operator()(const CodeTable &code_table) {
    size_t batch_size = tmat_.dims()[0];
    size_t width = tmat_.dims()[1];
    auto *vec_data = vec_->mutable_value()->template data<T>();
    auto *tmat_data = tmat_.data<T>();
    for (size_t i = 0; i < batch_size; ++i) {
      auto code = code_table.get_code(i);
      int code_length = code.get_length();
      for (int j = 0; j < code_length; ++j) {
        size_t index = code.calc_index(j);
        int64_t row_index = vec_->GetIndexFromId(static_cast<int64_t>(index));
        vec_data[row_index] += tmat_data[i * width + j];
      }
J
JiabinYang 已提交
111 112
    }
  }
Y
Yu Yang 已提交
113 114 115 116 117 118 119
};

template <typename T>
void MatrixBitCodeFunctor<T>::AddGrad(const framework::Tensor &tmat,
                                      framework::SelectedRows *vec) {
  MatrixBitCodeFunctorSelectedRowsAddGrad<T> func(tmat, vec);
  code_table_.apply_visitor(func);
J
JiabinYang 已提交
120 121
}

Y
Yancey1989 已提交
122
template <typename T>
Y
Yu Yang 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
struct MatrixBitCodeFunctorSum : public boost::static_visitor<void> {
  const framework::Tensor &tmat_;
  framework::Tensor *sum_;
  T scale_sum_;

  MatrixBitCodeFunctorSum(const framework::Tensor &tmat, framework::Tensor *sum,
                          T scale_sum)
      : tmat_(tmat), sum_(sum), scale_sum_(scale_sum) {}

  template <typename CodeTable>
  void operator()(const CodeTable &code_table) {
    size_t num_samples = tmat_.dims()[0];
    size_t o_width = tmat_.dims()[1];
    auto *tmat_data = tmat_.data<T>();
    auto *sum_data = sum_->data<T>();
    for (size_t i = 0; i < num_samples; ++i) {
      T sm = static_cast<T>(0.0);
      auto code = code_table.get_code(i);
      int code_length = code.get_length();
      for (int j = 0; j < code_length; ++j) {
        if (code.calc_bit(j)) {
          // calc_bit starts from right most bit, while data in tmat[i] is in
          // the
          // reverse order.
          sm += tmat_data[i * o_width + j];
        }
Y
Yancey1989 已提交
149
      }
Y
Yu Yang 已提交
150
      sum_data[i] = scale_sum_ * sm;
Y
Yancey1989 已提交
151 152
    }
  }
Y
Yu Yang 已提交
153 154 155 156 157 158 159
};

template <typename T>
void MatrixBitCodeFunctor<T>::Sum(const framework::Tensor &tmat,
                                  framework::Tensor *sum, T scale_sum) {
  MatrixBitCodeFunctorSum<T> func(tmat, sum, scale_sum);
  code_table_.apply_visitor(func);
Y
Yancey1989 已提交
160
}
Y
Yancey1989 已提交
161

Y
Yancey1989 已提交
162
template <typename T>
Y
Yu Yang 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
struct MatrixBitCodeFunctorMul : public boost::static_visitor<void> {
  framework::Tensor *tmat_;
  const framework::Tensor &weight_;
  const framework::Tensor &input_;

  MatrixBitCodeFunctorMul(framework::Tensor *tmat,
                          const framework::Tensor &weight,
                          const framework::Tensor &input)
      : tmat_(tmat), weight_(weight), input_(input) {}

  template <typename CodeTable>
  void operator()(const CodeTable &code_table) {
    auto blas =
        GetBlas<platform::CPUDeviceContext, T>(platform::CPUDeviceContext());
    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_value = tmat_->data<T>();
    auto weight_value = weight_.data<T>();
    auto input_value = input_.data<T>();
    for (size_t i = 0; i < num_samples; ++i) {
      auto code = code_table.get_code(i);
      int code_length = code.get_length();
      const T *input_row = input_value + input_width * i;
      for (int j = 0; j < code_length; ++j) {
        size_t index = code.calc_index(j);
        const T *weight_row = weight_value + weight_width * index;
        T sum = blas.DOT(input_width, weight_row, input_row);
        tmat_value[i * tmat_width + j] += sum;
      }
Y
Yancey1989 已提交
194 195
    }
  }
Y
Yu Yang 已提交
196 197 198 199 200 201 202 203
};

template <typename T>
void MatrixBitCodeFunctor<T>::Mul(framework::Tensor *tmat,
                                  const framework::Tensor &weight,
                                  const framework::Tensor &input) {
  MatrixBitCodeFunctorMul<T> func(tmat, weight, input);
  code_table_.apply_visitor(func);
Y
Yancey1989 已提交
204 205
}

Y
Yu Yang 已提交
206 207 208 209 210 211
template <typename T, size_t N>
class ReservedVector : public std::vector<T> {
 public:
  ReservedVector() { this->reserve(N); }
};

Y
Yancey1989 已提交
212
template <typename T>
Y
Yu Yang 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
struct MatrixBitCodeFunctorMulGradWeight : public boost::static_visitor<void> {
  const framework::Tensor &tmat_;
  framework::Tensor *weight_;
  const framework::Tensor &input_;
  MatrixBitCodeFunctorMulGradWeight(const framework::Tensor &tmat,
                                    framework::Tensor *weight,
                                    const framework::Tensor &input)
      : tmat_(tmat), weight_(weight), input_(input) {}
  template <typename CodeTable>
  void operator()(const CodeTable &code_table) {
    auto blas =
        GetBlas<platform::CPUDeviceContext, T>(platform::CPUDeviceContext());
    size_t num_samples = tmat_.dims()[0];
    size_t input_width = input_.dims()[1];
    size_t tmat_width = tmat_.dims()[1];
    size_t weight_width = weight_->dims()[1];
    auto tmat_value = tmat_.data<T>();
    auto weight_value = weight_->data<T>();
    auto input_value = input_.data<T>();

    std::map<int, ReservedVector<std::pair<T, const T *>, 8u>> ops;
    for (size_t i = 0; i < num_samples; ++i) {
      auto code = code_table.get_code(i);
      int code_length = code.get_length();
      const T *input_value_row = input_value + input_width * i;
      const T *tmat_row = tmat_value + i * tmat_width;
      for (int j = 0; j < code_length; ++j) {
        ops[code.calc_index(j)].emplace_back(tmat_row[j], input_value_row);
      }
J
JiabinYang 已提交
242
    }
Y
Yu Yang 已提交
243 244 245 246 247 248 249 250
    for (auto &op : ops) {
      auto &op_in_row = op.second;
      for (auto &pair : op_in_row) {
        auto &scale = pair.first;
        auto *input_row = pair.second;
        T *weight_row = weight_value + op.first * weight_width;
        blas.AXPY(input_width, scale, input_row, weight_row);
      }
Y
Yancey1989 已提交
251
    }
Y
Yancey1989 已提交
252
  }
Y
Yu Yang 已提交
253 254 255 256 257 258 259 260
};

template <typename T>
void MatrixBitCodeFunctor<T>::MulGradWeight(const framework::Tensor &tmat,
                                            framework::Tensor *weight,
                                            const framework::Tensor &input) {
  MatrixBitCodeFunctorMulGradWeight<T> func(tmat, weight, input);
  code_table_.apply_visitor(func);
Y
Yancey1989 已提交
261
}
Y
Yancey1989 已提交
262

J
JiabinYang 已提交
263
template <typename T>
Y
Yu Yang 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
struct MatrixBitCodeFunctorMulGradWeightSR
    : public boost::static_visitor<void> {
  const framework::Tensor &tmat_;
  framework::SelectedRows *weight_;
  const framework::Tensor &input_;

  MatrixBitCodeFunctorMulGradWeightSR(const framework::Tensor &tmat,
                                      framework::SelectedRows *weight,
                                      const framework::Tensor &input)
      : tmat_(tmat), weight_(weight), input_(input) {}

  template <typename CodeTable>
  void operator()(const CodeTable &code_table) {
    auto blas =
        GetBlas<platform::CPUDeviceContext, T>(platform::CPUDeviceContext());
    size_t num_samples = tmat_.dims()[0];
    size_t input_width = input_.dims()[1];
    size_t tmat_width = tmat_.dims()[1];
    size_t weight_width = weight_->value().dims()[1];
    auto tmat_value = tmat_.data<T>();
    auto weight_value = weight_->mutable_value()->data<T>();
    auto input_value = input_.data<T>();

    std::unordered_map<int, std::vector<std::pair<T, const T *>>> ops;
    ops.reserve(weight_->rows().size());

    for (size_t i = 0; i < num_samples; ++i) {
      auto code = code_table.get_code(i);
      int code_length = code.get_length();
      const T *input_value_row = input_value + input_width * i;
      const T *tmat_row = tmat_value + i * tmat_width;
      for (int j = 0; j < code_length; ++j) {
        ops[code.calc_index(j)].emplace_back(tmat_row[j], input_value_row);
      }
J
JiabinYang 已提交
298 299
    }

Y
Yu Yang 已提交
300 301 302 303 304 305 306 307
    for (auto &row : weight_->rows()) {
      auto &op_in_row = ops[row];
      for (auto &pair : op_in_row) {
        auto &scale = pair.first;
        auto *input_row = pair.second;
        blas.AXPY(input_width, scale, input_row, weight_value);
      }
      weight_value += weight_width;
J
JiabinYang 已提交
308 309
    }
  }
Y
Yu Yang 已提交
310 311 312 313 314 315 316 317
};

template <typename T>
void MatrixBitCodeFunctor<T>::MulGradWeight(const framework::Tensor &tmat,
                                            framework::SelectedRows *weight,
                                            const framework::Tensor &input) {
  MatrixBitCodeFunctorMulGradWeightSR<T> func(tmat, weight, input);
  code_table_.apply_visitor(func);
J
JiabinYang 已提交
318
}
J
JiabinYang 已提交
319

Y
Yancey1989 已提交
320
template <typename T>
Y
Yu Yang 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
struct MatrixBitCodeFunctorMulGradError : public boost::static_visitor<void> {
  const framework::Tensor &tmat_;
  const framework::Tensor &weight_;
  framework::Tensor *input_;

  MatrixBitCodeFunctorMulGradError(const framework::Tensor &tmat,
                                   const framework::Tensor &weight,
                                   framework::Tensor *input)
      : tmat_(tmat), weight_(weight), input_(input) {}
  template <typename CodeTable>
  void operator()(const CodeTable &code_table) {
    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_value = tmat_.data<T>();
    auto weight_value = weight_.data<T>();
    auto input_value = input_->data<T>();

    for (size_t i = 0; i < num_samples; ++i) {
      auto code = code_table.get_code(i);
      int code_length = code.get_length();
      for (int j = 0; j < code_length; ++j) {
        size_t index = code.calc_index(j);

        for (size_t k = 0; k < input_width; ++k) {
          input_value[input_width * i + k] +=
              tmat_value[i * tmat_width + j] *
              weight_value[weight_width * index + k];
        }
Y
Yancey1989 已提交
351 352 353
      }
    }
  }
Y
Yu Yang 已提交
354 355 356 357 358 359 360 361
};

template <typename T>
void MatrixBitCodeFunctor<T>::MulGradError(const framework::Tensor &tmat,
                                           const framework::Tensor &weight,
                                           framework::Tensor *input) {
  MatrixBitCodeFunctorMulGradError<T> func(tmat, weight, input);
  code_table_.apply_visitor(func);
Y
Yancey1989 已提交
362 363 364
}

template <typename T>
Y
Yu Yang 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
struct MatrixBitCodeFunctorSub : public boost::static_visitor<void> {
  framework::Tensor *tmat_;

  explicit MatrixBitCodeFunctorSub(framework::Tensor *tmat) : tmat_(tmat) {}

  template <typename CodeTable>
  void operator()(const CodeTable &code_table) {
    size_t num_samples = tmat_->dims()[0];
    size_t o_width = tmat_->dims()[1];
    auto *tmat_data = tmat_->data<T>();
    for (size_t i = 0; i < num_samples; ++i) {
      auto code = code_table.get_code(i);
      int code_length = code.get_length();
      for (int j = 0; j < code_length; ++j) {
        if (code.calc_bit(j)) {
          tmat_data[i * o_width + j] -= 1;
        }
Y
Yancey1989 已提交
382 383 384
      }
    }
  }
Y
Yu Yang 已提交
385 386 387 388 389 390
};

template <typename T>
void MatrixBitCodeFunctor<T>::Sub(framework::Tensor *tmat) {
  MatrixBitCodeFunctorSub<T> func(tmat);
  code_table_.apply_visitor(func);
Y
Yancey1989 已提交
391 392
}

Y
Yancey1989 已提交
393 394 395
template class MatrixBitCodeFunctor<float>;
template class MatrixBitCodeFunctor<double>;

Y
Yancey1989 已提交
396 397 398
}  // namespace math
}  // namespace operators
}  // namespace paddle