|
| 1 | +#ifndef CAFFE2_OPERATORS_BISECT_PERCENTILE_OP_H_ |
| 2 | +#define CAFFE2_OPERATORS_BISECT_PERCENTILE_OP_H_ |
| 3 | + |
| 4 | +#include "caffe2/core/context.h" |
| 5 | +#include "caffe2/core/logging.h" |
| 6 | +#include "caffe2/core/operator.h" |
| 7 | +#include "caffe2/core/tensor.h" |
| 8 | +#include "caffe2/utils/math.h" |
| 9 | + |
| 10 | +namespace caffe2 { |
| 11 | + |
| 12 | +template <class Context> |
| 13 | +class BisectPercentileOp final : public Operator<Context> { |
| 14 | + public: |
| 15 | + USE_OPERATOR_CONTEXT_FUNCTIONS; |
| 16 | + BisectPercentileOp(const OperatorDef& operator_def, Workspace* ws) |
| 17 | + : Operator<Context>(operator_def, ws), |
| 18 | + pct_raw_(OperatorBase::GetRepeatedArgument<float>( |
| 19 | + "percentile_raw", |
| 20 | + vector<float>{})), |
| 21 | + pct_mapping_(OperatorBase::GetRepeatedArgument<float>( |
| 22 | + "percentile_mapping", |
| 23 | + vector<float>{})), |
| 24 | + pct_lower_(OperatorBase::GetRepeatedArgument<float>( |
| 25 | + "percentile_lower", |
| 26 | + vector<float>{})), |
| 27 | + pct_upper_(OperatorBase::GetRepeatedArgument<float>( |
| 28 | + "percentile_upper", |
| 29 | + vector<float>{})), |
| 30 | + pct_lens_( |
| 31 | + OperatorBase::GetRepeatedArgument<int>("lengths", vector<int>{})) { |
| 32 | + CAFFE_ENFORCE_EQ( |
| 33 | + pct_raw_.size(), |
| 34 | + pct_mapping_.size(), |
| 35 | + "Feature (raw) data and percentile value dimension should match."); |
| 36 | + CAFFE_ENFORCE_EQ( |
| 37 | + pct_raw_.size(), |
| 38 | + pct_lower_.size(), |
| 39 | + "Feature (raw) data and lower bound dimension should match."); |
| 40 | + CAFFE_ENFORCE_EQ( |
| 41 | + pct_raw_.size(), |
| 42 | + pct_upper_.size(), |
| 43 | + "Feature (raw) data and upper bound dimension should match."); |
| 44 | + n_features = pct_lens_.size(); |
| 45 | + index.reserve(n_features + 1); |
| 46 | + index[0] = 0; |
| 47 | + for (int i = 1; i <= n_features; ++i) { |
| 48 | + index[i] = index[i - 1] + pct_lens_[i - 1]; |
| 49 | + } |
| 50 | + CAFFE_ENFORCE_EQ( |
| 51 | + index[n_features], // The sum of lengths_data |
| 52 | + pct_raw_.size(), |
| 53 | + "Sum of lengths should be equal to the total number of percentile " |
| 54 | + "mapping data samples"); |
| 55 | + } |
| 56 | + |
| 57 | + bool RunOnDevice() override { |
| 58 | + // Input |
| 59 | + const auto& raw = Input(RAW); |
| 60 | + CAFFE_ENFORCE_EQ(raw.ndim(), 2); |
| 61 | + const auto batch_size = raw.dim(0); |
| 62 | + const auto num_features = raw.dim(1); |
| 63 | + CAFFE_ENFORCE_EQ(num_features, pct_lens_.size()); |
| 64 | + const float* raw_data = raw.template data<float>(); |
| 65 | + |
| 66 | + // Output |
| 67 | + auto* pct = Output(PCT); |
| 68 | + pct->ResizeLike(raw); |
| 69 | + float* pct_output = pct->template mutable_data<float>(); |
| 70 | + |
| 71 | + // Compute percentile for each raw feature value |
| 72 | + int feature_start_index = 0; |
| 73 | + int feature_length = 0; |
| 74 | + int cur_index = 0; |
| 75 | + |
| 76 | + for (int i = 0; i < num_features; ++i) { |
| 77 | + cur_index = i; |
| 78 | + feature_start_index = index[i]; |
| 79 | + feature_length = pct_lens_[i]; |
| 80 | + for (int j = 0; j < batch_size; ++j) { |
| 81 | + pct_output[cur_index] = compute_percentile( |
| 82 | + pct_raw_.begin() + feature_start_index, |
| 83 | + pct_mapping_.begin() + feature_start_index, |
| 84 | + pct_lower_.begin() + feature_start_index, |
| 85 | + pct_upper_.begin() + feature_start_index, |
| 86 | + feature_length, |
| 87 | + raw_data[cur_index]); |
| 88 | + cur_index += num_features; |
| 89 | + } |
| 90 | + } |
| 91 | + return true; |
| 92 | + } |
| 93 | + |
| 94 | + protected: |
| 95 | + INPUT_TAGS(RAW); |
| 96 | + OUTPUT_TAGS(PCT); |
| 97 | + |
| 98 | + private: |
| 99 | + int n_features; |
| 100 | + vector<float> pct_raw_; |
| 101 | + vector<float> pct_mapping_; |
| 102 | + vector<float> pct_lower_; |
| 103 | + vector<float> pct_upper_; |
| 104 | + vector<int> pct_lens_; |
| 105 | + vector<int> index; |
| 106 | + vector<std::map<float, float>> fast_pct; |
| 107 | + |
| 108 | + const float kEPSILON = 1e-10; |
| 109 | + |
| 110 | + int binary_search( |
| 111 | + const std::vector<float>::iterator& data, |
| 112 | + int lo, |
| 113 | + int hi, |
| 114 | + float val) { |
| 115 | + int mid; |
| 116 | + bool low_cond, high_cond; |
| 117 | + |
| 118 | + while (lo < hi) { |
| 119 | + mid = (lo + hi) >> 1; |
| 120 | + low_cond = (data[mid] <= val); |
| 121 | + high_cond = (val < data[mid + 1]); |
| 122 | + if (low_cond && high_cond) { |
| 123 | + return mid; |
| 124 | + } else if (!low_cond) { |
| 125 | + hi = mid - 1; |
| 126 | + } else { |
| 127 | + lo = mid + 1; |
| 128 | + } |
| 129 | + } |
| 130 | + return lo; |
| 131 | + } |
| 132 | + |
| 133 | + float compute_percentile( |
| 134 | + const std::vector<float>::iterator& pct_raw_it, |
| 135 | + const std::vector<float>::iterator& pct_mapping_it, |
| 136 | + const std::vector<float>::iterator& pct_lower_it, |
| 137 | + const std::vector<float>::iterator& pct_upper_it, |
| 138 | + const int size, |
| 139 | + const float val) { |
| 140 | + // Corner cases where no interpolation is needed. |
| 141 | + if (val < pct_raw_it[0]) { |
| 142 | + return 0.; |
| 143 | + } |
| 144 | + if (val > pct_raw_it[size - 1]) { |
| 145 | + return 1.; |
| 146 | + } |
| 147 | + |
| 148 | + float result; |
| 149 | + // Interpolation by binary search |
| 150 | + const auto k = binary_search(pct_raw_it, 0, size - 1, val); |
| 151 | + |
| 152 | + if (pct_raw_it[k] == val) { |
| 153 | + // Exact match |
| 154 | + result = pct_mapping_it[k]; |
| 155 | + } else { |
| 156 | + // interpolation |
| 157 | + float w = (val - pct_raw_it[k]) / |
| 158 | + (pct_raw_it[k + 1] - pct_raw_it[k] + kEPSILON); |
| 159 | + result = (1 - w) * pct_upper_it[k] + w * pct_lower_it[k + 1]; |
| 160 | + } |
| 161 | + return result; |
| 162 | + } |
| 163 | +}; |
| 164 | + |
| 165 | +} // namespace caffe2 |
| 166 | + |
| 167 | +#endif // CAFFE2_OPERATORS_BISECT_PERCENTILE_OP_H_ |
0 commit comments