-
Notifications
You must be signed in to change notification settings - Fork 74k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[XLA:GPU] Extract useful utils from ir_emitter_triton_test into their…
… own file. PiperOrigin-RevId: 642300620
- Loading branch information
1 parent
8a69fee
commit c7515ca
Showing
5 changed files
with
199 additions
and
99 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* 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/service/gpu/triton_test_utils.h" | ||
|
||
#include <cstdint> | ||
#include <memory> | ||
#include <string> | ||
#include <variant> | ||
#include <vector> | ||
|
||
#include "absl/status/status.h" | ||
#include "absl/strings/string_view.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
#include "mlir/IR/MLIRContext.h" // from @llvm-project | ||
#include "xla/hlo/ir/hlo_casting_utils.h" | ||
#include "xla/hlo/ir/hlo_instructions.h" | ||
#include "xla/service/gpu/fusions/triton.h" | ||
#include "xla/service/gpu/gpu_device_info_for_tests.h" | ||
#include "xla/service/gpu/hlo_fusion_analysis.h" | ||
#include "xla/service/gpu/ir_emission_utils.h" | ||
#include "xla/service/gpu/ir_emitter_triton.h" | ||
#include "xla/service/gpu/matmul_utils.h" | ||
#include "xla/service/gpu/triton_fusion_analysis.h" | ||
#include "xla/status_macros.h" | ||
#include "xla/stream_executor/device_description.h" | ||
#include "xla/tests/filecheck.h" | ||
#include "xla/tests/verified_hlo_module.h" | ||
#include "tsl/platform/statusor.h" | ||
|
||
namespace xla::gpu { | ||
bool TritonTest::SkipBF16Tests() { | ||
if (std::holds_alternative<stream_executor::RocmComputeCapability>( | ||
GpuComputeComp())) { | ||
auto rcc = device_desc().rocm_compute_capability(); | ||
return !rcc.has_bf16_dtype_support(); | ||
} | ||
return false; | ||
} | ||
|
||
stream_executor::GpuComputeCapability TritonTest::CudaAmpereOrRocm() { | ||
if (std::holds_alternative<stream_executor::RocmComputeCapability>( | ||
GpuComputeComp())) { | ||
return stream_executor::GpuComputeCapability{ | ||
device_desc().rocm_compute_capability()}; | ||
} else { | ||
return stream_executor::GpuComputeCapability{ | ||
stream_executor::CudaComputeCapability{ | ||
stream_executor::CudaComputeCapability::AMPERE, 0}}; | ||
} | ||
} | ||
|
||
absl::Status TritonFilecheckTest::CreateTritonIrAndFileCheck( | ||
absl::string_view hlo_text, const TritonGemmConfig& config, | ||
std::vector<int64_t> output_tile_sizes, TritonIrEmitter emitter, | ||
absl::string_view triton_fusion_name, absl::string_view filecheck_pattern) { | ||
TF_ASSIGN_OR_RETURN(std::unique_ptr<VerifiedHloModule> verified_module, | ||
ParseAndReturnVerifiedModule(hlo_text)); | ||
|
||
auto* computation = | ||
verified_module->GetComputationWithName(triton_fusion_name); | ||
auto* fusion = Cast<HloFusionInstruction>(computation->FusionInstruction()); | ||
TF_RET_CHECK(computation != nullptr); | ||
TF_ASSIGN_OR_RETURN(auto analysis, | ||
TritonFusionAnalysis::Execute(*computation)); | ||
|
||
auto fusion_analysis = HloFusionAnalysis::Create(fusion, &device_desc()); | ||
|
||
if (fusion_analysis.fusion_backend_config().kind() == | ||
kTritonSoftmaxFusionKind) { | ||
TritonFusion triton_fusion(fusion_analysis); | ||
if (auto launch_config = triton_fusion.launch_config()) { | ||
output_tile_sizes = launch_config->output_tile_sizes; | ||
} | ||
} | ||
|
||
mlir::MLIRContext context; | ||
TF_ASSIGN_OR_RETURN( | ||
auto module, | ||
CreateTritonModule(analysis, "triton_fn", fusion, | ||
TestGpuDeviceInfo::RTXA6000DeviceInfo(), config, | ||
output_tile_sizes, emitter, context)); | ||
|
||
std::string out; | ||
llvm::raw_string_ostream os(out); | ||
module->print(os); | ||
TF_ASSIGN_OR_RETURN(bool succeeded, RunFileCheck(out, filecheck_pattern)); | ||
if (!succeeded) { | ||
return absl::InternalError("FileCheck failed."); | ||
} | ||
return absl::OkStatus(); | ||
} | ||
|
||
} // namespace xla::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* 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. | ||
==============================================================================*/ | ||
|
||
#ifndef XLA_SERVICE_GPU_TRITON_TEST_UTILS_H_ | ||
#define XLA_SERVICE_GPU_TRITON_TEST_UTILS_H_ | ||
|
||
#include <cstdint> | ||
#include <vector> | ||
|
||
#include "absl/status/status.h" | ||
#include "absl/strings/string_view.h" | ||
#include "xla/service/gpu/ir_emitter_triton.h" | ||
#include "xla/service/gpu/matmul_utils.h" | ||
#include "xla/service/gpu/tests/gpu_codegen_test.h" | ||
#include "xla/stream_executor/device_description.h" | ||
|
||
namespace xla::gpu { | ||
|
||
class TritonTest : public GpuCodegenTest { | ||
public: | ||
stream_executor::CudaComputeCapability GetCudaComputeCapability() { | ||
return backend() | ||
.default_stream_executor() | ||
->GetDeviceDescription() | ||
.cuda_compute_capability(); | ||
} | ||
|
||
const stream_executor::GpuComputeCapability& GpuComputeComp() { | ||
return device_desc().gpu_compute_capability(); | ||
} | ||
|
||
bool SkipBF16Tests(); | ||
stream_executor::GpuComputeCapability CudaAmpereOrRocm(); | ||
|
||
protected: | ||
const stream_executor::DeviceDescription& device_desc() { | ||
return backend().default_stream_executor()->GetDeviceDescription(); | ||
} | ||
}; | ||
|
||
class TritonFilecheckTest : public TritonTest { | ||
public: | ||
absl::Status CreateTritonIrAndFileCheck( | ||
absl::string_view hlo_text, const TritonGemmConfig& config, | ||
std::vector<int64_t> output_tile_sizes, TritonIrEmitter emitter, | ||
absl::string_view triton_fusion_name, | ||
absl::string_view filecheck_pattern); | ||
}; | ||
|
||
} // namespace xla::gpu | ||
|
||
#endif // XLA_SERVICE_GPU_TRITON_TEST_UTILS_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