Skip to content
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

Make the pjrt gpu allocator configurable #5759

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions torch_xla/csrc/runtime/env_vars.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const char* const kEnvNeuronLibraryPath = "NEURON_LIBRARY_PATH";
const char* const kEnvPjrtDistServiceAddr = "PJRT_DIST_SERVICE_ADDR";
const char* const kEnvPjRtLocalProcessCount = "PJRT_LOCAL_PROCESS_COUNT";
const char* const kEnvPjRtLocalRank = "PJRT_LOCAL_PROCESS_RANK";
const char* const kEnvPjrtAllocatorCudaAsync = "PJRT_ALLOCATOR_CUDA_ASYNC";
const char* const kEnvPjrtAllocatorPreallocate = "PJRT_ALLOCATOR_PREALLOCATE";
const char* const kEnvPjrtAllocatorFraction = "PJRT_ALLOCATOR_FRACTION";

} // namespace env
} // namespace runtime
Expand Down
3 changes: 3 additions & 0 deletions torch_xla/csrc/runtime/env_vars.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ extern const char* const kEnvNeuronLibraryPath;
extern const char* const kEnvPjrtDistServiceAddr;
extern const char* const kEnvPjRtLocalProcessCount;
extern const char* const kEnvPjRtLocalRank;
extern const char* const kEnvPjrtAllocatorCudaAsync;
extern const char* const kEnvPjrtAllocatorPreallocate;
extern const char* const kEnvPjrtAllocatorFraction;

} // namespace env
} // namespace runtime
Expand Down
19 changes: 18 additions & 1 deletion torch_xla/csrc/runtime/pjrt_computation_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ xla::Shape host_output_shape(xla::PjRtBuffer* buffer) {
return xla::ShapeUtil::DeviceShapeToHostShape(shape);
}

xla::GpuAllocatorConfig GetGpuAllocatorConfig() {
auto allocator_config = xla::GpuAllocatorConfig{};
if (sys_util::GetEnvString(env::kEnvPjrtAllocatorCudaAsync, "").empty() &&
sys_util::GetEnvString(env::kEnvPjrtAllocatorPreallocate, "").empty() &&
sys_util::GetEnvString(env::kEnvPjrtAllocatorFraction, "").empty()) {
return allocator_config;
}
if (sys_util::GetEnvBool(env::kEnvPjrtAllocatorCudaAsync, false)) {
allocator_config.kind = xla::GpuAllocatorConfig::Kind::kCudaAsync;
}
allocator_config.preallocate =
sys_util::GetEnvBool(env::kEnvPjrtAllocatorPreallocate, true);
allocator_config.memory_fraction =
sys_util::GetEnvDouble(env::kEnvPjrtAllocatorFraction, 0.75);
return allocator_config;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to change the default behavior. If none of kEnvPjrtAllocatorCudaAsync, kEnvPjrtAllocatorPreallocate, kEnvPjrtAllocatorFraction is set, could you just return xla::GpuAllocatorConfig{} as before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If kEnvPjrtAllocatorFraction is set to 0.75, it is the same as xla::GpuAllocatorConfig{}. Should the default value be changed from 0.9 to 0.75?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our previous experience with GPUs, 0.9 is considered a reasonable value for the memory fraction.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, let's use the default value from xla:gpu https://github.com/openxla/xla/blob/7ab5df624ff1d98804999b03b21abecd14ec57a6/xla/pjrt/gpu/gpu_helpers.h#L41-L60.
With the new flags, it gives you the flexibility to choose the best configuration that suits your needs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, if none of kEnvPjrtAllocatorCudaAsync, kEnvPjrtAllocatorPreallocate, kEnvPjrtAllocatorFraction is set, could you just return xla::GpuAllocatorConfig{} as before?

The reason is that with the current implementation if xla:GPU change their default value, then we have to update ours and we may forget to do so or don't know they have changed the values. By just returning xla::GpuAllocatorConfig{}, we can use whatever default value xla:GPU has set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. When none of kEnvPjrtAllocatorCudaAsync, kEnvPjrtAllocatorPreallocate, kEnvPjrtAllocatorFraction is set, just return xla::GpuAllocatorConfig{}.

}

} // namespace

std::string PjRtComputationClient::PjRtDeviceToString(
Expand Down Expand Up @@ -138,7 +155,7 @@ PjRtComputationClient::PjRtComputationClient() {
<< global_process_rank << ", num_nodes=" << global_world_size;
client_ = std::move(xla::GetStreamExecutorGpuClient(
/*asynchronous=*/async,
/*allocator_config=*/xla::GpuAllocatorConfig{},
/*allocator_config=*/GetGpuAllocatorConfig(),
/*node_id=*/global_process_rank,
/*num_nodes=*/global_world_size,
/*allowed_devices=*/allowed_devices,
Expand Down