sampler.cc 4.0 KB
Newer Older
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
W
wanghaoshuang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

A
Abhinav Arora 已提交
15
#include "paddle/fluid/operators/math/sampler.h"
16
#include <glog/logging.h>
17 18 19 20
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
21
#include "paddle/fluid/framework/generator.h"
22 23

namespace paddle {
24 25
namespace operators {
namespace math {
26 27 28

Sampler::~Sampler() {}

29 30 31
UniformSampler::UniformSampler(int64_t range, unsigned int seed)
    : Sampler(range, seed), inv_range_(1.0 / (range + 1)) {
  random_engine_ = std::make_shared<std::mt19937_64>(seed_);
W
wanghaoshuang 已提交
32 33 34
  dist_ = std::make_shared<std::uniform_int_distribution<>>(0, range);
}

35 36 37 38 39 40
int64_t UniformSampler::Sample() const {
  return framework::Generator::GetInstance()->is_init_py
             ? (*dist_)(framework::Generator::GetInstance()->GetCPUEngine())
             : (*dist_)(*random_engine_);
  // return (*dist_)(*random_engine_);
}
41

42
float UniformSampler::Probability(int64_t value) const { return inv_range_; }
43

44
LogUniformSampler::LogUniformSampler(int64_t range, unsigned int seed)
W
wanghaoshuang 已提交
45
    : Sampler(range, seed), log_range_(log(range + 1)) {
46
  random_engine_ = std::make_shared<std::mt19937_64>(seed_);
W
wanghaoshuang 已提交
47 48
  dist_ = std::make_shared<std::uniform_real_distribution<>>(0, 1);
}
49 50

int64_t LogUniformSampler::Sample() const {
51 52 53 54
  // Got Log Uniform distribution from uniform distribution by
  // inverse_transform_sampling method
  // More details:
  // https://wanghaoshuang.github.io/2017/11/Log-uniform-distribution-sampler/
55 56 57 58 59
  auto cur_random =
      framework::Generator::GetInstance()->is_init_py
          ? (*dist_)(framework::Generator::GetInstance()->GetCPUEngine())
          : (*dist_)(*random_engine_);
  const int64_t value = static_cast<int64_t>(exp(cur_random * log_range_)) - 1;
60 61 62 63 64
  // Mathematically, value should be <= range_, but might not be due to some
  // floating point roundoff, so we mod by range_.
  return value % range_;
}

65
float LogUniformSampler::Probability(int64_t value) const {
66 67 68 69 70 71 72
  // Given f(x) = 1/[(x+1) * log_range_]
  // The value's  probability  is integral of f(x) from value to (value + 1)
  // More details:
  // https://wanghaoshuang.github.io/2017/11/Log-uniform-distribution-sampler
  return (log((value + 2.0) / (value + 1.0))) / log_range_;
}

73 74
CustomSampler::CustomSampler(int64_t range, const float *probabilities,
                             const int *alias, const float *alias_probabilities,
75 76
                             unsigned int seed)
    : Sampler(range, seed) {
77
  random_engine_ = std::make_shared<std::mt19937>(seed_);
78 79 80
  real_dist_ = std::make_shared<std::uniform_real_distribution<>>(0, 1);
  int_dist_ = std::make_shared<std::uniform_int_distribution<>>(0, range);

81 82 83
  alias_probs_ = alias_probabilities;
  probs_ = probabilities;
  alias_ = alias;
84 85 86
}

int64_t CustomSampler::Sample() const {
87 88 89 90 91 92 93 94
  auto index =
      framework::Generator::GetInstance()->is_init_py
          ? (*int_dist_)(framework::Generator::GetInstance()->GetCPUEngine())
          : (*int_dist_)(*random_engine_);
  auto p =
      framework::Generator::GetInstance()->is_init_py
          ? (*real_dist_)(framework::Generator::GetInstance()->GetCPUEngine())
          : (*real_dist_)(*random_engine_);
95
  if (p > alias_probs_[index]) {
96 97 98 99 100 101 102 103
    int alias = alias_[index];

    if (alias == exceptional_val) {
      LOG(WARNING) << "WARNING: CustomSampler get alias " << exceptional_val;
      return index;
    }

    return alias;
104 105 106 107 108
  } else {
    return index;
  }
}

109
float CustomSampler::Probability(int64_t value) const { return probs_[value]; }
110 111 112

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