-
Notifications
You must be signed in to change notification settings - Fork 490
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[XLA:CPU] Move dot emitter to new kernel API
PiperOrigin-RevId: 720189399
- Loading branch information
1 parent
fd2988c
commit b8a356a
Showing
13 changed files
with
325 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* Copyright 2025 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/backends/cpu/codegen/dot_kernel_emitter.h" | ||
|
||
#include <memory> | ||
#include <utility> | ||
|
||
#include "absl/log/log.h" | ||
#include "absl/status/statusor.h" | ||
#include "absl/strings/str_cat.h" | ||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/IR/LLVMContext.h" | ||
#include "xla/backends/cpu/codegen/kernel_api_ir_builder.h" | ||
#include "xla/backends/cpu/codegen/target_machine_features.h" | ||
#include "xla/codegen/kernel_definition.h" | ||
#include "xla/codegen/kernel_spec.h" | ||
#include "xla/codegen/llvm_ir_kernel_source.h" | ||
#include "xla/hlo/ir/hlo_instruction.h" | ||
#include "xla/service/buffer_assignment.h" | ||
#include "xla/service/cpu/dot_op_emitter.h" | ||
#include "xla/service/hlo_module_config.h" | ||
#include "xla/service/llvm_ir/ir_array.h" | ||
#include "xla/stream_executor/launch_dim.h" | ||
#include "xla/tsl/platform/errors.h" | ||
#include "xla/tsl/platform/statusor.h" | ||
#include "xla/util.h" | ||
|
||
namespace xla::cpu { | ||
|
||
static bool IsDotCodegenStrategy(DotImplementationStrategy strategy) { | ||
switch (strategy) { | ||
case DotImplementationStrategy::kNaiveLlvmIr: | ||
case DotImplementationStrategy::kTiledLlvmIrGemv: | ||
case DotImplementationStrategy::kTiledLlvmIrGemm: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
DotKernelEmitter::DotKernelEmitter(const HloInstruction* instr, | ||
const BufferAssignment* buffer_assignment, | ||
const TargetMachineFeatures* target_machine) | ||
: instr_(instr), | ||
buffer_assignment_(buffer_assignment), | ||
target_machine_(target_machine) {} | ||
|
||
absl::StatusOr<KernelDefinition> DotKernelEmitter::EmitKernelDefinition() { | ||
const HloModuleConfig& config = instr_->GetModule()->config(); | ||
|
||
DotImplementationStrategy strategy = | ||
GetDotImplementationStrategy(config, *instr_, *target_machine_); | ||
|
||
if (!IsDotCodegenStrategy(strategy)) { | ||
return Internal("Unsupported dot implementation strategy"); | ||
} | ||
|
||
auto ctx = std::make_unique<llvm::LLVMContext>(); | ||
|
||
const HloModule* hlo_module = instr_->GetModule(); | ||
if (hlo_module == nullptr) { | ||
return Internal("HloModule is null"); | ||
} | ||
|
||
KernelApiIrBuilder kernel_api_ir_builder( | ||
*ctx, | ||
KernelApiIrBuilder::Options::FromHloModuleConfig(hlo_module->config())); | ||
|
||
std::unique_ptr<llvm::Module> llvm_module = KernelApiIrBuilder::CreateModule( | ||
absl::StrCat(instr_->name(), "_elemental_kernel_module"), *ctx); | ||
|
||
TF_ASSIGN_OR_RETURN(KernelApiIrBuilder::KernelPrototype kernel_prototype, | ||
kernel_api_ir_builder.EmitKernelPrototype( | ||
*llvm_module, instr_, buffer_assignment_, "_kernel")); | ||
|
||
llvm::IRBuilder<> builder(*ctx); | ||
builder.SetInsertPoint( | ||
kernel_prototype.function->getEntryBlock().getTerminator()); | ||
|
||
llvm_ir::IrArray lhs_array = kernel_prototype.arguments[0]; | ||
llvm_ir::IrArray rhs_array = kernel_prototype.arguments[1]; | ||
llvm_ir::IrArray target_array = kernel_prototype.results[0]; | ||
|
||
TF_RETURN_IF_ERROR(EmitDotOperation( | ||
*instr_, target_array, lhs_array, rhs_array, | ||
/*addend_array=*/nullptr, /*executable_run_options_value=*/nullptr, | ||
&builder, config, *target_machine_, | ||
/*allow_runtime_calls=*/false)); | ||
|
||
auto source = std::make_unique<LlvmIrKernelSource>(std::move(ctx), | ||
std::move(llvm_module)); | ||
|
||
KernelSpec spec(kernel_prototype.function->getName(), se::ThreadDim(), | ||
std::move(kernel_prototype.buffer_uses)); | ||
|
||
return KernelDefinition(std::move(spec), std::move(source)); | ||
} | ||
|
||
} // namespace xla::cpu |
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,45 @@ | ||
/* Copyright 2025 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_BACKENDS_CPU_CODEGEN_DOT_KERNEL_EMITTER_H_ | ||
#define XLA_BACKENDS_CPU_CODEGEN_DOT_KERNEL_EMITTER_H_ | ||
|
||
#include "absl/status/statusor.h" | ||
#include "xla/backends/cpu/codegen/target_machine_features.h" | ||
#include "xla/codegen/kernel_definition.h" | ||
#include "xla/codegen/kernel_emitter.h" | ||
#include "xla/hlo/ir/hlo_instruction.h" | ||
#include "xla/service/buffer_assignment.h" | ||
|
||
namespace xla::cpu { | ||
|
||
class DotKernelEmitter final : public KernelEmitter { | ||
public: | ||
DotKernelEmitter(const HloInstruction* instr, | ||
const BufferAssignment* buffer_assignment, | ||
const TargetMachineFeatures* target_machine); | ||
|
||
absl::StatusOr<KernelDefinition> EmitKernelDefinition() override; | ||
|
||
private: | ||
const HloInstruction* instr_; | ||
|
||
const BufferAssignment* buffer_assignment_ = nullptr; | ||
const TargetMachineFeatures* target_machine_ = nullptr; | ||
}; | ||
|
||
} // namespace xla::cpu | ||
|
||
#endif // XLA_BACKENDS_CPU_CODEGEN_DOT_KERNEL_EMITTER_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,68 @@ | ||
# 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. | ||
# ============================================================================== | ||
|
||
from absl.testing import absltest | ||
import numpy as np | ||
|
||
from xla.backends.cpu import testlib as testlib_cpu | ||
from xla.backends.cpu.testlib import utilities | ||
from xla.codegen import testlib as testlib_base | ||
from xla.codegen.testlib import utilities as base_utilities | ||
|
||
create_literal = base_utilities.create_literal_from_np | ||
HloInstruction = testlib_base.HloInstruction | ||
HloOpcode = testlib_base.HloOpcode | ||
|
||
|
||
class DotKernelRunnerTest(absltest.TestCase): | ||
|
||
def test_dot_kernel_emitter(self): | ||
lhs_np = np.array([[1, 2], [3, 4]], dtype=np.float32) | ||
rhs_np = np.array([[5, 6], [7, 8]], dtype=np.float32) | ||
|
||
lhs_literal = create_literal(lhs_np) | ||
rhs_literal = create_literal(rhs_np) | ||
|
||
output_literal = create_literal(np.ndarray((2, 2), dtype=np.float32)) | ||
|
||
lhs_param = HloInstruction.create_parameter(0, lhs_literal.shape(), "lhs") | ||
rhs_param = HloInstruction.create_parameter(1, rhs_literal.shape(), "rhs") | ||
|
||
dot_dimension_numbers = testlib_base.DotDimensionNumbers([1], [0], [], []) | ||
hlo_op = HloInstruction.create_dot( | ||
output_literal.shape(), lhs_param, rhs_param, dot_dimension_numbers | ||
) | ||
|
||
hlo_module, buffer_assignment = utilities.build_hlo_module( | ||
hlo_op, lhs_param, rhs_param | ||
) | ||
jit_compiler = testlib_cpu.JitCompiler() | ||
|
||
emitter = testlib_cpu.DotKernelEmitter( | ||
hlo_module.get_root_instruction(), | ||
buffer_assignment, | ||
jit_compiler.get_target_machine(), | ||
) | ||
|
||
runner = testlib_cpu.KernelRunner.create( | ||
emitter.emit_kernel_definition(), jit_compiler | ||
) | ||
|
||
runner.call([lhs_literal, rhs_literal, output_literal]) | ||
np.testing.assert_equal(np.asarray(output_literal), lhs_np @ rhs_np) | ||
|
||
|
||
if __name__ == "__main__": | ||
absltest.main() |
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
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.