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

#pragma once
W
weixing02 已提交
16 17 18
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/platform/device_context.h"
Y
Yancey1989 已提交
19

D
dzhwinter 已提交
20 21 22 23 24
#if defined(_WIN32)
#include <intrin.h>
#include <windows.h>
#endif  // _WIN32

Y
Yancey1989 已提交
25 26 27
namespace paddle {
namespace operators {
namespace math {
W
weixing02 已提交
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
/**
 * SimpleCodeTable class should support 3 functions:
 *
 * size_t size()
 *   return the number of ids
 *
 * int get_max_code_length()
 *   return the maximal code length
 *
 * SimpleCode operator()(size_t i)
 *   return the i-th code. Code class is descriebed below.
 *
 * SimpleCode class should support 3 functions:
 *
 * int get_length()
 *   return the length of the code
 *
 * size_t cal_index(int bit)
 *   bit ranges from 0 to get_length() - 1
 *   return the index for the (1+bit) level parent
 *
 * bool calc_bit(int bit)
 *   return true if the bit level parent is the right child of (1+bit) level
 *   parent
 *
 */
Y
Yancey1989 已提交
54 55 56 57 58 59

/**
 * return the 1-based index of the highest bit set
 *
 * for x > 0:
 * \f[
W
weixing02 已提交
60
 *    FindLastSet(x) = 1 + \floor*{\log_{2}x}
Y
Yancey1989 已提交
61 62
 * \f]
 */
D
dzhwinter 已提交
63
#if !defined(_WIN32)
Y
Yancey1989 已提交
64 65 66 67 68 69
inline constexpr size_t FindLastSet(size_t x) {
  return std::is_same<size_t, unsigned int>::value
             ? (x ? 8 * sizeof(x) - __builtin_clz(x) : 0)
             : (std::is_same<size_t, unsigned long>::value  // NOLINT
                    ? (x ? 8 * sizeof(x) - __builtin_clzl(x) : 0)
                    : (x ? 8 * sizeof(x) - __builtin_clzll(x) : 0));
D
dzhwinter 已提交
70

D
dzhwinter 已提交
71 72 73
#else
// windows don't have built-in clz, ctz function
template <typename T>
D
dzhwinter 已提交
74
inline int  ctz(const T& value) {
D
dzhwinter 已提交
75 76
  DWORD trailing_zero = 0;
  if (_BitScanForward(&trailing_zero, value)) {
D
dzhwinter 已提交
77
    return static_cast<int>(trailing_zero);
D
dzhwinter 已提交
78
  } else {
D
dzhwinter 已提交
79
    return static_cast<int>(0);
D
dzhwinter 已提交
80 81 82 83
  }
}

template <typename T>
D
dzhwinter 已提交
84
inline int  clz(const T& value) {
D
dzhwinter 已提交
85 86
  DWORD leadning_zero = 0;
  if (_BitScanReverse(&leadning_zero, value)) {
D
dzhwinter 已提交
87
    return static_cast<int>(sizeof(T) * 8 - leadning_zero);
D
dzhwinter 已提交
88
  } else {
D
dzhwinter 已提交
89
    return static_cast<int>(0);
D
dzhwinter 已提交
90 91 92
  }
}

D
dzhwinter 已提交
93 94
inline size_t FindLastSet(size_t x) {
  return sizeof(size_t) * 8 - clz(x);
D
dzhwinter 已提交
95 96
}
#endif  // !_WIN32
Y
Yancey1989 已提交
97 98 99 100
}

struct SimpleCode {
  SimpleCode(size_t code, size_t num_classes) : c_(code + num_classes) {}
G
guosheng 已提交
101
  /**
102 103 104 105 106 107 108
   * Here the id of root shoud be 1 rather than 0, thus the encoding of class c
   * is `c + num_classes` and all siblings can get the same weight indice using
   * prefixes.
   * Weight index is the prefixes of encoding, thus leave out the right most
   * bit in calc_index.
   * Binary classification path is the suffixes of encoding, thus leave out the
   * left most bit in calc_bit.
G
guosheng 已提交
109
   */
Y
Yancey1989 已提交
110 111 112 113 114
  inline size_t calc_index(int bit) const { return (c_ >> (bit + 1)) - 1; }
  inline bool calc_bit(int bit) const { return c_ & (1 << bit); }
  inline int get_length() const { return FindLastSet(c_) - 1; }

 private:
115
  size_t c_;
Y
Yancey1989 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129
};

struct SimpleCodeTable {
  explicit SimpleCodeTable(size_t num_classes) : num_classes_(num_classes) {}
  SimpleCode operator()(size_t code) const {
    return SimpleCode(code, num_classes_);
  }
  size_t size() const { return num_classes_; }
  int get_max_code_length() const { return FindLastSet(num_classes_ - 1); }

 private:
  size_t num_classes_;
};

Y
Yancey1989 已提交
130
template <typename T>
Y
Yancey1989 已提交
131 132
class MatrixBitCodeFunctor {
 public:
Y
Yancey1989 已提交
133 134
  explicit MatrixBitCodeFunctor(size_t num_classes, const int64_t* ids)
      : num_classes_(num_classes), ids_(ids) {}
Y
Yancey1989 已提交
135 136 137
  /* For j < code_length
       tmat(i, j) += vec(0, index(i, j))
  */
W
weixing02 已提交
138
  void Add(framework::Tensor* tmat, const framework::Tensor& vec);
Y
Yancey1989 已提交
139

Y
Yancey1989 已提交
140 141 142
  /* For j < code_length
       vec(0, index(i, j)) += tmat(i, j)
  */
W
weixing02 已提交
143
  void AddGrad(const framework::Tensor& tmat, framework::Tensor* vec);
Y
Yancey1989 已提交
144 145

  /* For j < code_length
Y
Yancey1989 已提交
146
    sum(i, 0) = \sum_j bit(i, j) * tmat(i, j)
Y
Yancey1989 已提交
147
  */
W
weixing02 已提交
148
  void Sum(const framework::Tensor& tmat, framework::Tensor* sum, T scale_sum);
Y
Yancey1989 已提交
149

Y
Yancey1989 已提交
150 151 152
  /* For j < code_length
       tmat(i, j) -= bit(i, j)
  */
W
weixing02 已提交
153
  void Sub(framework::Tensor* tmat);
Y
Yancey1989 已提交
154 155 156
  /* For j < code_length
       input.row(i) += tmat(i, j) * weight.row(index(i, j))
  */
W
weixing02 已提交
157
  void Mul(framework::Tensor* tmat, const framework::Tensor& weight,
Y
Yancey1989 已提交
158
           const framework::Tensor& input);
Y
Yancey1989 已提交
159

Y
Yancey1989 已提交
160 161 162
  /* For index(i, j) >= 0:
      weight.row(index(i, j)) += tmat(i, j) * input.row(i)
  */
W
weixing02 已提交
163
  void MulGradWeight(const framework::Tensor& tmat, framework::Tensor* weight,
Y
Yancey1989 已提交
164 165 166 167
                     const framework::Tensor& input);
  /* For j < code_length
    input.row(i) += tmat(i, j) * weight.row(index(i, j))
  */
Y
Yancey1989 已提交
168
  void MulGradError(const framework::Tensor& tmat,
W
weixing02 已提交
169 170
                    const framework::Tensor& weight, framework::Tensor* input);

Y
Yancey1989 已提交
171 172
  size_t num_classes_;
  const int64_t* ids_;
Y
Yancey1989 已提交
173
};
Y
Yancey1989 已提交
174 175 176
}  // namespace math
}  // namespace operators
}  // namespace paddle