matrix_bit_code.cc 2.5 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/* 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)))
*/
template <class CodeTable, class Op, typename T, typename Place>
static void AddByBitCodeT(Op op, CodeTable code_table,
                          const framework::Tensor& codes, framework::Tensor& a,
                          framework::Tensor& b) {
  size_t num_classes = code_table.size();
  size_t max_code_length = code_table.get_max_code_length();
  size_t num_sample = a.dims()[0].size();
  size_t width = a.dims()[1].size();

  for (size_t i = 0; i < num_sample; ++i) {
    auto code = code_table(codes.data<T>()[i]) int code_length =
        code.get_length();
    for (int j = 0; j < code_length; + j) {
      size_t index = code.calc_index(j);
      op(a<T>.data()[i * width + j], b<T>.data()[index]);
    }
  }
}

/* For j < codeLength:
   a(i, j) += b(0, index(i, j))
*/
template <typename T, typename Place>
void AddByBitCode(size_t num_classes, const framework::Tensor& codes,
                  framework::Tensor& a, const framework::Tensor& b) {
  auto op = [](T& t, T& v) { t += v; };
  AddByBitCodeT<T, Place>(op, SimpleCodeTable(num_classes), codes, a, b);
}

}  // namespace math
}  // namespace operators
}  // namespace paddle