utils.h 2.42 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
5
6
#pragma once

#include <torch/extension.h>

#define CHECK_CPU(x) AT_ASSERTM(x.device().is_cpu(), #x " must be CPU tensor")
#define CHECK_INPUT(x) AT_ASSERTM(x, "Input mismatch")
rusty1s's avatar
matmul  
rusty1s committed
7
8
9

#define AT_DISPATCH_HAS_VALUE(optional_value, ...)                             \
  [&] {                                                                        \
10
    if (optional_value.has_value()) {                                          \
rusty1s's avatar
matmul  
rusty1s committed
11
12
      const bool HAS_VALUE = true;                                             \
      return __VA_ARGS__();                                                    \
13
    } else {                                                                   \
rusty1s's avatar
matmul  
rusty1s committed
14
15
16
17
      const bool HAS_VALUE = false;                                            \
      return __VA_ARGS__();                                                    \
    }                                                                          \
  }()
rusty1s's avatar
choice  
rusty1s committed
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

template <typename scalar_t>
torch::Tensor from_vector(const std::vector<scalar_t> &vec,
                          bool inplace = false) {
  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();
}

torch::Tensor choice(int64_t population, int64_t num_samples,
                     bool replace = false,
                     torch::optional<torch::Tensor> weight = torch::nullopt) {

  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) {
    const auto out = torch::empty(num_samples, at::kLong);
    auto *out_data = out.data_ptr<int64_t>();
    for (int64_t i = 0; i < num_samples; i++) {
      out_data[i] = rand() % population;
    }
    return out;

  } else {
    // Sample without replacement via Robert Floyd algorithm:
    // https://www.nowherenearithaca.com/2013/05/
    // robert-floyds-tiny-and-beautiful.html
    std::unordered_set<int64_t> values;
    for (int64_t i = population - num_samples; i < population; i++) {
      if (!values.insert(rand() % i).second)
        values.insert(i);
    }
    const auto out = torch::empty(num_samples, at::kLong);
    auto *out_data = out.data_ptr<int64_t>();
    int64_t i = 0;
    for (const auto &value : values) {
rusty1s's avatar
rusty1s committed
59
      out_data[i] = value;
rusty1s's avatar
choice  
rusty1s committed
60
61
62
63
64
      i++;
    }
    return out;
  }
}