-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce derived classes CudaKernel and RocmKernel
This change makes `GpuKernel` an abstract base class and moves its implementation into the derived classes `CudaKernel` and `RocmKernel`. This avoids having two implementations for the same functions and also reduces the exposure of gpu_types.h which we want to get rid of. I'm also adding some basic tests for the new classes. PiperOrigin-RevId: 679003532
- Loading branch information
1 parent
88648d3
commit 7e2ff2b
Showing
11 changed files
with
334 additions
and
51 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* Copyright 2019 The OpenXLA Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
// The CUDA implementation of the StreamExecutor functionality. | ||
// CUDA inclusions are ideally confined to this implementation file. | ||
// | ||
// The notions from the StreamExecutor basically correspond to the CUDA streams | ||
// programming model provided by the libcuda.so driver APIs, so we don't have | ||
// to do much more than wrap the calls to the libraries appropriately. | ||
#ifndef XLA_STREAM_EXECUTOR_CUDA_CUDA_KERNEL_H_ | ||
#define XLA_STREAM_EXECUTOR_CUDA_CUDA_KERNEL_H_ | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
|
||
#include "absl/status/statusor.h" | ||
#include "xla/stream_executor/gpu/gpu_executor.h" | ||
#include "xla/stream_executor/gpu/gpu_kernel.h" | ||
#include "xla/stream_executor/gpu/gpu_types.h" | ||
#include "xla/stream_executor/launch_dim.h" | ||
#include "tsl/platform/logging.h" | ||
|
||
namespace stream_executor::gpu { | ||
|
||
class CudaKernel : public GpuKernel { | ||
public: | ||
explicit CudaKernel(GpuExecutor* gpu_executor) | ||
: gpu_executor_(gpu_executor) {} | ||
|
||
// Note that the function is unloaded when the module is unloaded, and the | ||
// module that the function is contained in is owned by the GpuExecutor. | ||
~CudaKernel() override { gpu_executor_->UnloadKernel(this); } | ||
|
||
// As arity cannot be reflected upon using the CUDA API, the arity is | ||
// explicitly set during the GpuExecutor::GetKernel initialization process. | ||
void set_arity(unsigned arity) { arity_ = arity; } | ||
unsigned Arity() const override { return arity_; } | ||
|
||
absl::StatusOr<int32_t> GetMaxOccupiedBlocksPerCore( | ||
ThreadDim threads, size_t dynamic_shared_memory_bytes) const override; | ||
|
||
// Simple accessor methods. | ||
GpuFunctionHandle gpu_function() const override { return gpu_function_; } | ||
void set_gpu_function(GpuFunctionHandle gpu_function) { | ||
gpu_function_ = gpu_function; | ||
} | ||
|
||
private: | ||
GpuExecutor* gpu_executor_ = nullptr; | ||
|
||
CUfunction gpu_function_ = nullptr; // wrapped CUDA kernel handle | ||
unsigned arity_ = 0; // number of formal parameters the kernel takes | ||
}; | ||
|
||
} // namespace stream_executor::gpu | ||
|
||
#endif // XLA_STREAM_EXECUTOR_CUDA_CUDA_KERNEL_H_ |
This file contains 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* Copyright 2024 The OpenXLA Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include "xla/stream_executor/cuda/cuda_kernel.h" | ||
|
||
#include <gtest/gtest.h> | ||
#include "third_party/gpus/cuda/include/cuda.h" | ||
#include "xla/stream_executor/cuda/cuda_runtime.h" | ||
#include "xla/stream_executor/gpu/gpu_executor.h" | ||
#include "xla/stream_executor/gpu/gpu_test_kernels.h" | ||
#include "xla/stream_executor/launch_dim.h" | ||
#include "xla/stream_executor/platform.h" | ||
#include "xla/stream_executor/platform_manager.h" | ||
#include "tsl/platform/status_matchers.h" | ||
#include "tsl/platform/statusor.h" | ||
#include "tsl/platform/test.h" | ||
|
||
namespace stream_executor::gpu { | ||
namespace { | ||
using testing::Ge; | ||
using tsl::testing::IsOkAndHolds; | ||
|
||
TEST(CudaKernelTest, GetMaxOccupiedBlocksPerCore) { | ||
TF_ASSERT_OK_AND_ASSIGN(Platform * platform, | ||
PlatformManager::PlatformWithName("CUDA")); | ||
TF_ASSERT_OK_AND_ASSIGN(StreamExecutor * executor, | ||
platform->ExecutorForDevice(0)); | ||
GpuExecutor* gpu_executor = ExtractGpuExecutor(executor); | ||
|
||
CudaKernel cuda_kernel(gpu_executor); | ||
cuda_kernel.set_arity(3); | ||
|
||
TF_ASSERT_OK_AND_ASSIGN( | ||
CUfunction function, | ||
CudaRuntime::GetFuncBySymbol(internal::GetAddI32Kernel())); | ||
|
||
cuda_kernel.set_gpu_function(function); | ||
|
||
EXPECT_EQ(cuda_kernel.Arity(), 3); | ||
EXPECT_EQ(cuda_kernel.gpu_function(), function); | ||
|
||
EXPECT_THAT(cuda_kernel.GetMaxOccupiedBlocksPerCore( | ||
ThreadDim(1, 1, 1), /*dynamic_shared_memory_bytes=*/0), | ||
IsOkAndHolds(Ge(1))); | ||
} | ||
|
||
} // namespace | ||
} // namespace stream_executor::gpu |
This file contains 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 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 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 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
Oops, something went wrong.