|
6 | 6 | #include <c10/core/Device.h> |
7 | 7 | #include <c10/core/DeviceType.h> |
8 | 8 | #include <c10/core/TensorImpl.h> |
| 9 | +#include <c10/core/GeneratorImpl.h> |
9 | 10 | #include <c10/util/intrusive_ptr.h> |
| 11 | +#include <c10/util/CallOnce.h> |
| 12 | + |
| 13 | +// XLA headers |
| 14 | +#include "torch_xla/csrc/runtime/computation_client.h" |
| 15 | +#include "torch_xla/csrc/aten_xla_bridge.h" |
10 | 16 |
|
11 | 17 | #include <cstring> |
| 18 | +#include <deque> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +namespace at { |
| 22 | + |
| 23 | +namespace detail { |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | +// Total number of XLA devices in the system. |
| 28 | +static int64_t num_xla_devices; |
| 29 | + |
| 30 | +// Ensures default_gens_xla is initialized once. |
| 31 | +static std::deque<c10::once_flag> xla_gens_init_flag; |
| 32 | + |
| 33 | +// Default, global XLA generators, one per XLA device. |
| 34 | +static std::vector<at::Generator> default_gens_xla; |
| 35 | + |
| 36 | +/* |
| 37 | + * Populates the global variables related to XLA generators |
| 38 | + * Warning: this function must only be called once! |
| 39 | + */ |
| 40 | +static void initXLAGenVector() { |
| 41 | + // Ensures we only call deviceCount only once. |
| 42 | + static bool num_xla_device_init_flag [[maybe_unused]] = []() { |
| 43 | + // Get local num of XLA devices |
| 44 | + auto maybe_client = torch_xla::runtime::GetComputationClient(); |
| 45 | + if (!maybe_client.ok()) { |
| 46 | + // If runtime client initialization failed, default to 1 device |
| 47 | + num_xla_devices = 1; |
| 48 | + } else { |
| 49 | + auto* client = maybe_client.value(); |
| 50 | + num_xla_devices = static_cast<int64_t>(client->GetNumDevices()); |
| 51 | + } |
| 52 | + xla_gens_init_flag.resize(num_xla_devices); |
| 53 | + default_gens_xla.resize(num_xla_devices); |
| 54 | + return true; |
| 55 | + }(); |
| 56 | +} |
| 57 | + |
| 58 | +} // anonymous namespace |
| 59 | + |
| 60 | +/** |
| 61 | + * PyTorch maintains a collection of default generators that get |
| 62 | + * initialized once. The purpose of these default generators is to |
| 63 | + * maintain a global running state of the pseudo random number generation, |
| 64 | + * when a user does not explicitly mention any generator. |
| 65 | + * getDefaultXLAGenerator gets the default generator for a particular |
| 66 | + * XLA device. |
| 67 | + */ |
| 68 | +const at::Generator& getDefaultXLAGenerator(c10::DeviceIndex device_index) { |
| 69 | + initXLAGenVector(); |
| 70 | + c10::DeviceIndex idx = device_index; |
| 71 | + if (idx == -1) { |
| 72 | + idx = 0; // Default to device 0 for XLA |
| 73 | + } else { |
| 74 | + TORCH_CHECK(idx >= 0 && idx < num_xla_devices); |
| 75 | + } |
| 76 | + c10::call_once(xla_gens_init_flag[idx], [&] { |
| 77 | + default_gens_xla[idx] = at::make_generator<XLAGeneratorImpl>(idx); |
| 78 | + default_gens_xla[idx].seed(); |
| 79 | + }); |
| 80 | + return default_gens_xla[idx]; |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Utility to create a XLAGeneratorImpl. Returns a shared_ptr |
| 85 | + */ |
| 86 | +at::Generator createXLAGenerator(c10::DeviceIndex device_index) { |
| 87 | + initXLAGenVector(); |
| 88 | + c10::DeviceIndex idx = device_index; |
| 89 | + if (idx == -1) { |
| 90 | + idx = torch_xla::bridge::GetCurrentAtenDevice().index(); // Use current XLA device |
| 91 | + } |
| 92 | + TORCH_CHECK(idx >= 0 && idx < num_xla_devices, "The device_index is invalid."); |
| 93 | + auto gen = at::make_generator<XLAGeneratorImpl>(idx); |
| 94 | + auto xla_gen = at::check_generator<XLAGeneratorImpl>(gen); |
| 95 | + xla_gen->set_current_seed(c10::default_rng_seed_val); |
| 96 | + return gen; |
| 97 | +} |
| 98 | + |
| 99 | +} // namespace detail |
| 100 | +} // namespace at |
12 | 101 |
|
13 | 102 | namespace at { |
14 | 103 |
|
|
0 commit comments