SobolEngineOpsUtils.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /// This file contains some tensor-agnostic operations to be used in the
  2. /// core functions of the `SobolEngine`
  3. #include <ATen/core/Tensor.h>
  4. #ifndef AT_PER_OPERATOR_HEADERS
  5. #include <ATen/Functions.h>
  6. #else
  7. #include <ATen/ops/arange.h>
  8. #include <ATen/ops/mul.h>
  9. #include <ATen/ops/pow.h>
  10. #endif
  11. namespace at {
  12. namespace native {
  13. namespace sobol_utils {
  14. /// Function to return the minimum of number of bits to represent the integer `n`
  15. inline int64_t bit_length(const int64_t n) {
  16. int64_t nbits, nloc;
  17. for (nloc = n, nbits = 0; nloc > 0; nloc /= 2, nbits++);
  18. return nbits;
  19. }
  20. /// Function to get the position of the rightmost zero in the bit representation of an integer
  21. /// This value is the zero-indexed position
  22. inline int64_t rightmost_zero(const int64_t n) {
  23. int64_t z, i;
  24. for (z = n, i = 0; z % 2 == 1; z /= 2, i++);
  25. return i;
  26. }
  27. /// Function to get a subsequence of bits in the representation of an integer starting from
  28. /// `pos` and of length `length`
  29. inline int64_t bitsubseq(const int64_t n, const int64_t pos, const int64_t length) {
  30. return (n >> pos) & ((1 << length) - 1);
  31. }
  32. /// Function to perform the inner product between a batched square matrix and a power of 2 vector
  33. inline at::Tensor cdot_pow2(const at::Tensor& bmat) {
  34. at::Tensor inter = at::arange(bmat.size(-1) - 1, -1, -1, bmat.options());
  35. inter = at::pow(2, inter).expand_as(bmat);
  36. return at::mul(inter, bmat).sum(-1);
  37. }
  38. /// All definitions below this point are data. These are constant, and should not be modified
  39. /// without notice
  40. constexpr int64_t MAXDIM = 21201;
  41. constexpr int64_t MAXDEG = 18;
  42. constexpr int64_t MAXBIT = 30;
  43. constexpr int64_t LARGEST_NUMBER = 1 << MAXBIT;
  44. constexpr float RECIPD = 1.0 / LARGEST_NUMBER;
  45. extern const int64_t poly[MAXDIM];
  46. extern const int64_t initsobolstate[MAXDIM][MAXDEG];
  47. } // namespace sobol_utils
  48. } // namespace native
  49. } // namespace at