File: GeneratorImpl.cpp

package info (click to toggle)
pytorch-cuda 2.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 161,620 kB
  • sloc: python: 1,278,832; cpp: 900,322; ansic: 82,710; asm: 7,754; java: 3,363; sh: 2,811; javascript: 2,443; makefile: 597; ruby: 195; xml: 84; objc: 68
file content (113 lines) | stat: -rw-r--r-- 3,318 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <c10/core/GeneratorImpl.h>
#include <random>

#if defined(__SGX_ENABLED__)
#include <sgx_trts.h>
#endif

#ifndef _WIN32
#include <fcntl.h>
#include <unistd.h>
#else
#include <chrono>
#endif

namespace c10 {

/**
 * GeneratorImpl class implementation
 */
GeneratorImpl::GeneratorImpl(Device device_in, DispatchKeySet key_set)
    : device_{device_in}, key_set_(key_set) {}

/**
 * Clone this generator. Note that clone() is the only
 * method for copying for Generators in ATen.
 */
c10::intrusive_ptr<GeneratorImpl> GeneratorImpl::clone() const {
  auto res = this->clone_impl();
  c10::raw::intrusive_ptr::incref(res);
  c10::raw::weak_intrusive_ptr::incref(res);
  return c10::intrusive_ptr<GeneratorImpl>::reclaim(res);
}

void GeneratorImpl::graphsafe_set_state(
    const c10::intrusive_ptr<c10::GeneratorImpl>& state) {
  TORCH_CHECK_NOT_IMPLEMENTED(
      false, "graphsafe_set_state is not supported in this Generator");
}

c10::intrusive_ptr<c10::GeneratorImpl> GeneratorImpl::graphsafe_get_state()
    const {
  TORCH_CHECK_NOT_IMPLEMENTED(
      false, "graphsafe_get_state is not supported in this Generator");
}

/**
 * Gets the device of a generator.
 */
Device GeneratorImpl::device() const {
  return device_;
}

namespace detail {

/**
 * Gets a random number for /dev/urandom
 * Note this is a legacy method (from THRandom.cpp)
 * FIXME: use std::random_device with entropy information
 */
#if !defined(_WIN32)
static uint64_t readURandomLong() {
  int randDev = open("/dev/urandom", O_RDONLY);
  TORCH_CHECK(randDev >= 0, "Unable to open /dev/urandom");
  uint64_t randValue{};
  ssize_t readBytes = read(randDev, &randValue, sizeof(randValue));
  close(randDev);
  TORCH_CHECK(
      readBytes >= (ssize_t)sizeof(randValue),
      "Unable to read from /dev/urandom");
  return randValue;
}
#endif // _WIN32

/**
 * Gets a non deterministic random number number from either the
 * /dev/urandom or the current time. For CUDA, gets random from
 * std::random_device and adds a transformation on it. For Intel SGX
 * platform use sgx_read_rand as reading from /dev/urandom is
 * prohibited on that platform.
 *
 * FIXME: The behavior in this function is from legacy code
 * (THRandom_seed/THCRandom_seed) and is probably not the right thing to do,
 * even though our tests pass. Figure out if tests get perturbed
 * - when the same algorithm is used for all backends. Note that the current
 * behavior is different for CPU, CUDA and Windows CPU.
 * - when using C++11 std objects, such as std::random_device
 * - when constructing a 64 bit seed properly, rather than static casting
 *   a 32 bit number to 64 bit.
 */
uint64_t getNonDeterministicRandom(bool is_cuda) {
  uint64_t s = 0;
  if (!is_cuda) {
#ifdef _WIN32
    s = (uint64_t)std::chrono::high_resolution_clock::now()
            .time_since_epoch()
            .count();
#elif defined(__SGX_ENABLED__)
    TORCH_CHECK(
        sgx_read_rand(reinterpret_cast<uint8_t*>(&s), sizeof(s)) == SGX_SUCCESS,
        "Could not generate random number with sgx_read_rand.");
#else
    s = readURandomLong();
#endif
  } else {
    std::random_device rd;
    // limit to 53 bits to ensure unique representation in double
    s = ((((uint64_t)rd()) << 32) + rd()) & 0x1FFFFFFFFFFFFF;
  }
  return s;
}

} // namespace detail
} // namespace c10