utils.h 4.47 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
#pragma once

3
#include "../extensions.h"
4
#include "parallel_hashmap/phmap.h"
rusty1s's avatar
rusty1s committed
5
6
7

#define CHECK_CPU(x) AT_ASSERTM(x.device().is_cpu(), #x " must be CPU tensor")
#define CHECK_INPUT(x) AT_ASSERTM(x, "Input mismatch")
Sean Liu's avatar
Sean Liu committed
8
#define CHECK_LT(low, high) AT_ASSERTM(low < high, "low must be smaller than high")
rusty1s's avatar
matmul  
rusty1s committed
9
10
11

#define AT_DISPATCH_HAS_VALUE(optional_value, ...)                             \
  [&] {                                                                        \
12
    if (optional_value.has_value()) {                                          \
rusty1s's avatar
matmul  
rusty1s committed
13
14
      const bool HAS_VALUE = true;                                             \
      return __VA_ARGS__();                                                    \
15
    } else {                                                                   \
rusty1s's avatar
matmul  
rusty1s committed
16
17
18
19
      const bool HAS_VALUE = false;                                            \
      return __VA_ARGS__();                                                    \
    }                                                                          \
  }()
rusty1s's avatar
choice  
rusty1s committed
20
21

template <typename scalar_t>
rusty1s's avatar
rusty1s committed
22
23
inline torch::Tensor from_vector(const std::vector<scalar_t> &vec,
                                 bool inplace = false) {
rusty1s's avatar
choice  
rusty1s committed
24
25
26
27
28
29
  const auto size = (int64_t)vec.size();
  const auto out = torch::from_blob((scalar_t *)vec.data(), {size},
                                    c10::CppTypeToScalarType<scalar_t>::value);
  return inplace ? out : out.clone();
}

rusty1s's avatar
rusty1s committed
30
31
template <typename key_t, typename scalar_t>
inline c10::Dict<key_t, torch::Tensor>
32
from_vector(const phmap::flat_hash_map<key_t, std::vector<scalar_t>> &vec_dict,
rusty1s's avatar
rusty1s committed
33
34
35
36
37
38
39
            bool inplace = false) {
  c10::Dict<key_t, torch::Tensor> out_dict;
  for (const auto &kv : vec_dict)
    out_dict.insert(kv.first, from_vector<scalar_t>(kv.second, inplace));
  return out_dict;
}

40
41
42
43
44
45
46
47
48
49
50
51
inline int64_t uniform_randint(int64_t low, int64_t high) {
  CHECK_LT(low, high);
  auto options = torch::TensorOptions().dtype(torch::kInt64);
  auto ret = torch::randint(low, high, {1}, options);
  auto ptr = ret.data_ptr<int64_t>();
  return *ptr;
}

inline int64_t uniform_randint(int64_t high) {
  return uniform_randint(0, high);
}

rusty1s's avatar
rusty1s committed
52
53
54
inline torch::Tensor
choice(int64_t population, int64_t num_samples, bool replace = false,
       torch::optional<torch::Tensor> weight = torch::nullopt) {
rusty1s's avatar
choice  
rusty1s committed
55

rusty1s's avatar
rusty1s committed
56
57
58
  if (population == 0 || num_samples == 0)
    return torch::empty({0}, at::kLong);

rusty1s's avatar
choice  
rusty1s committed
59
60
61
62
63
64
65
  if (!replace && num_samples >= population)
    return torch::arange(population, at::kLong);

  if (weight.has_value())
    return torch::multinomial(weight.value(), num_samples, replace);

  if (replace) {
Matthias Fey's avatar
Matthias Fey committed
66
    const auto out = torch::empty({num_samples}, at::kLong);
rusty1s's avatar
choice  
rusty1s committed
67
68
    auto *out_data = out.data_ptr<int64_t>();
    for (int64_t i = 0; i < num_samples; i++) {
69
      out_data[i] = uniform_randint(population);
rusty1s's avatar
choice  
rusty1s committed
70
71
72
73
74
75
76
    }
    return out;

  } else {
    // Sample without replacement via Robert Floyd algorithm:
    // https://www.nowherenearithaca.com/2013/05/
    // robert-floyds-tiny-and-beautiful.html
Matthias Fey's avatar
Matthias Fey committed
77
    const auto out = torch::empty({num_samples}, at::kLong);
rusty1s's avatar
choice  
rusty1s committed
78
    auto *out_data = out.data_ptr<int64_t>();
rusty1s's avatar
rusty1s committed
79
80
    std::unordered_set<int64_t> samples;
    for (int64_t i = population - num_samples; i < population; i++) {
81
      int64_t sample = uniform_randint(i);
rusty1s's avatar
rusty1s committed
82
83
84
85
86
      if (!samples.insert(sample).second) {
        sample = i;
        samples.insert(sample);
      }
      out_data[i - population + num_samples] = sample;
rusty1s's avatar
choice  
rusty1s committed
87
88
89
90
    }
    return out;
  }
}
rusty1s's avatar
rusty1s committed
91
92
93
94
95

template <bool replace>
inline void
uniform_choice(const int64_t population, const int64_t num_samples,
               const int64_t *idx_data, std::vector<int64_t> *samples,
96
               phmap::flat_hash_map<int64_t, int64_t> *to_local_node) {
rusty1s's avatar
rusty1s committed
97
98
99
100
101
102

  if (population == 0 || num_samples == 0)
    return;

  if (replace) {
    for (int64_t i = 0; i < num_samples; i++) {
103
      const int64_t &v = idx_data[uniform_randint(population)];
rusty1s's avatar
rusty1s committed
104
105
106
107
108
109
110
111
112
113
114
115
      if (to_local_node->insert({v, samples->size()}).second)
        samples->push_back(v);
    }
  } else if (num_samples >= population) {
    for (int64_t i = 0; i < population; i++) {
      const int64_t &v = idx_data[i];
      if (to_local_node->insert({v, samples->size()}).second)
        samples->push_back(v);
    }
  } else {
    std::unordered_set<int64_t> indices;
    for (int64_t i = population - num_samples; i < population; i++) {
116
      int64_t j = uniform_randint(i);
rusty1s's avatar
rusty1s committed
117
118
119
120
121
122
123
124
125
126
      if (!indices.insert(j).second) {
        j = i;
        indices.insert(j);
      }
      const int64_t &v = idx_data[j];
      if (to_local_node->insert({v, samples->size()}).second)
        samples->push_back(v);
    }
  }
}