-
Notifications
You must be signed in to change notification settings - Fork 559
Add helper functions getDefaultXLAGenerator and createXLAGenerator to XLA random number generator #9682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iwknow
wants to merge
17
commits into
pytorch:master
Choose a base branch
from
iwknow:generator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+279
−2
Open
Add helper functions getDefaultXLAGenerator and createXLAGenerator to XLA random number generator #9682
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
07ca239
Add xla random generator.
iwknow 1d67a02
format cpp files
iwknow 2fbef1a
Merge branch 'pytorch:master' into generator
iwknow d60c447
Merge branch 'pytorch:master' into generator
iwknow 6d5d2d2
Merge branch 'pytorch:master' into generator
iwknow fa49099
Merge branch 'pytorch:master' into generator
iwknow 79ff99b
Add helper functions `getDefaultXLAGenerator` and `createXLAGenerator…
iwknow 79d4b42
implement `XLAHooks` and register it to PyTorch when loaded.
iwknow 914d708
format
iwknow 1224531
Revert "implement `XLAHooks` and register it to PyTorch when loaded."
iwknow 80b2078
Add missing include
iwknow 60b408d
Merge branch 'pytorch:master' into generator
iwknow c65ce8f
improve error reporting and function naming.
iwknow e745336
format
iwknow e89e659
Add unit tests for `GetDefaultXLAGenerator` and `CreateXLAGenerator`
iwknow 4cc594f
Improve InitXLAGenVector function and the unit tests accordingly.
iwknow c93e10d
Address feedbacks.
iwknow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,10 +5,115 @@ | |
| #include <ATen/core/Tensor.h> | ||
| #include <c10/core/Device.h> | ||
| #include <c10/core/DeviceType.h> | ||
| #include <c10/core/GeneratorImpl.h> | ||
| #include <c10/core/TensorImpl.h> | ||
| #include <c10/util/CallOnce.h> | ||
| #include <c10/util/intrusive_ptr.h> | ||
|
|
||
| #include <cstring> | ||
| #include <deque> | ||
| #include <vector> | ||
|
|
||
| #include "absl/status/status.h" | ||
| #include "torch_xla/csrc/aten_xla_bridge.h" | ||
| #include "torch_xla/csrc/runtime/computation_client.h" | ||
| #include "torch_xla/csrc/runtime/runtime.h" | ||
| #include "torch_xla/csrc/status.h" | ||
|
|
||
| namespace at { | ||
|
|
||
| namespace detail { | ||
|
|
||
| namespace { | ||
|
|
||
| // Total number of XLA devices in the system. | ||
| static int64_t num_xla_devices; | ||
|
|
||
| // Ensures default_gens_xla is initialized once. | ||
| static std::deque<c10::once_flag> xla_gens_init_flag; | ||
|
|
||
| // Default, global XLA generators, one per XLA device. | ||
| static std::vector<at::Generator> default_gens_xla; | ||
|
|
||
| /* | ||
| * Populates the global variables related to XLA generators | ||
| * Warning: this function must only be called once! | ||
| */ | ||
| static absl::Status InitXLAGenVector() { | ||
| static absl::Status init_status = []() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's leak it (see this example). |
||
| XLA_ASSIGN_OR_RETURN(auto c_client, | ||
| torch_xla::runtime::GetComputationClient()); | ||
| num_xla_devices = static_cast<int64_t>(c_client->GetNumDevices()); | ||
| xla_gens_init_flag.resize(num_xla_devices); | ||
| default_gens_xla.resize(num_xla_devices); | ||
| return absl::OkStatus(); | ||
| }(); | ||
| return init_status; | ||
| } | ||
|
|
||
| // Validates and normalizes an XLA device index. | ||
| // If requested_index == -1, the current device index is used. | ||
| // Returns InvalidArgument if the resolved index is out of range. | ||
| static absl::StatusOr<c10::DeviceIndex> NormalizeXLADeviceIndex( | ||
| c10::DeviceIndex requested_index) { | ||
| c10::DeviceIndex idx = requested_index; | ||
| if (idx == -1) { | ||
| idx = torch_xla::bridge::GetCurrentAtenDevice().index(); | ||
| } | ||
| if (idx < 0 || idx >= num_xla_devices) { | ||
| return absl::InvalidArgumentError( | ||
| "Invalid device index for XLA generator. Provided index: " + | ||
| std::to_string(idx)); | ||
| } | ||
| return idx; | ||
| } | ||
|
|
||
| } // anonymous namespace | ||
|
|
||
| /** | ||
| * PyTorch maintains a collection of default generators that get | ||
| * initialized once. The purpose of these default generators is to | ||
| * maintain a global running state of the pseudo random number generation, | ||
| * when a user does not explicitly mention any generator. | ||
| * GetDefaultXLAGenerator gets the default generator for a particular | ||
| * XLA device. | ||
| */ | ||
| absl::StatusOr<const at::Generator&> GetDefaultXLAGenerator( | ||
| c10::DeviceIndex device_index) { | ||
| XLA_RETURN_IF_ERROR(InitXLAGenVector(), | ||
| "Failed to initialize XLA generators"); | ||
| // Normalize and validate the target device index; default to current device | ||
| // when unspecified | ||
| XLA_ASSIGN_OR_RETURN(c10::DeviceIndex idx, | ||
| NormalizeXLADeviceIndex(device_index), | ||
| "Invalid XLA device index"); | ||
| c10::call_once(xla_gens_init_flag[idx], [&] { | ||
| default_gens_xla[idx] = at::make_generator<XLAGeneratorImpl>(idx); | ||
| default_gens_xla[idx].seed(); | ||
| }); | ||
| return default_gens_xla[idx]; | ||
| } | ||
|
|
||
| /** | ||
| * Utility to create a XLAGeneratorImpl. Returns a shared_ptr | ||
| */ | ||
| absl::StatusOr<at::Generator> CreateXLAGenerator( | ||
| c10::DeviceIndex device_index) { | ||
| XLA_RETURN_IF_ERROR(InitXLAGenVector(), | ||
| "Failed to initialize XLA generators"); | ||
|
Comment on lines
+102
to
+103
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this actually needed for |
||
| // Normalize and validate the target device index; default to current device | ||
| // when unspecified | ||
| XLA_ASSIGN_OR_RETURN(c10::DeviceIndex idx, | ||
| NormalizeXLADeviceIndex(device_index), | ||
| "Invalid XLA device index"); | ||
| auto gen = at::make_generator<XLAGeneratorImpl>(idx); | ||
| auto xla_gen = at::check_generator<XLAGeneratorImpl>(gen); | ||
| xla_gen->set_current_seed(c10::default_rng_seed_val); | ||
| return gen; | ||
| } | ||
|
|
||
| } // namespace detail | ||
| } // namespace at | ||
|
|
||
| namespace at { | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed above, let's not do this.
Let's leave the environment variable initialization to the test runner script.