From d56e8bee75f914f4617a4b9b3583fadec667c8aa Mon Sep 17 00:00:00 2001 From: Ionel Gog Date: Wed, 13 Nov 2024 10:10:14 -0800 Subject: [PATCH] [IFRT] Add ifrt-translate mlir tool for verifying dialect conversions. This tool will run MLIR lit IFRT IR serialization and deserialization tests. In order to add support for this tool (and for some other possible cases), this change adds an optional `DeserializeIfrtIRProgramOptions`, which contains a pointer to an existing MLIRContext. If option is not null then the program is deserialized in this context, and the returned `IfrtIRProgram` doesn't own the context. Otherwise, the program is deserialized in a new context that is owned by the returned `IfrtIRProgram`. PiperOrigin-RevId: 696178259 --- xla/python/ifrt/ir/ifrt_ir_program.cc | 1 + xla/python/ifrt/ir/ifrt_ir_program.h | 15 +++ xla/python/ifrt/ir/ifrt_ir_program_serdes.cc | 35 +++++- xla/python/ifrt/ir/tests/BUILD | 23 ++++ xla/python/ifrt/ir/tests/ifrt-translate.cc | 124 +++++++++++++++++++ 5 files changed, 192 insertions(+), 6 deletions(-) create mode 100644 xla/python/ifrt/ir/tests/ifrt-translate.cc diff --git a/xla/python/ifrt/ir/ifrt_ir_program.cc b/xla/python/ifrt/ir/ifrt_ir_program.cc index 26d6688c76b954..fb0f594ef1f37b 100644 --- a/xla/python/ifrt/ir/ifrt_ir_program.cc +++ b/xla/python/ifrt/ir/ifrt_ir_program.cc @@ -38,6 +38,7 @@ namespace ifrt { char IfrtIRProgram::ID = 0; char SerializeIfrtIRProgramOptions::ID = 0; +char DeserializeIfrtIRProgramOptions::ID = 0; char IfrtIRCompileOptions::ID = 0; absl::StatusOr> GetIfrtIRCompileOptions( diff --git a/xla/python/ifrt/ir/ifrt_ir_program.h b/xla/python/ifrt/ir/ifrt_ir_program.h index 21e51e39205a0d..5e67ba494ad25e 100644 --- a/xla/python/ifrt/ir/ifrt_ir_program.h +++ b/xla/python/ifrt/ir/ifrt_ir_program.h @@ -73,6 +73,21 @@ struct SerializeIfrtIRProgramOptions std::string atom_program_version; }; +// Options for deserializing IFRT IR programs. +// If `context` is not nullptr then deserialization will create a new MLIR +// context, which will be owned by the deserialized program. Otherwise, the +// deserialization will use the provided MLIR context and the returned program +// will not own a MLIR context. +struct DeserializeIfrtIRProgramOptions + : llvm::RTTIExtends { + explicit DeserializeIfrtIRProgramOptions(mlir::MLIRContext* context) + : context(context) {} + + static char ID; // NOLINT + + mlir::MLIRContext* context; +}; + // CompileOptions for an IFRT IR program. struct IfrtIRCompileOptions : llvm::RTTIExtends { diff --git a/xla/python/ifrt/ir/ifrt_ir_program_serdes.cc b/xla/python/ifrt/ir/ifrt_ir_program_serdes.cc index 949ec31cd6e0cb..bcc03f07c06c30 100644 --- a/xla/python/ifrt/ir/ifrt_ir_program_serdes.cc +++ b/xla/python/ifrt/ir/ifrt_ir_program_serdes.cc @@ -131,12 +131,23 @@ class IfrtIRProgramSerDes // IFRT IR versions or VHLO version are outside of the compatibility window. absl::StatusOr> Deserialize( const std::string& serialized, - std::unique_ptr) override { + std::unique_ptr options) override { + const auto* deserialize_options = + llvm::dyn_cast_or_null(options.get()); + bool use_existing_context = false; + std::unique_ptr context; + if (!deserialize_options || !deserialize_options->context) { + context = std::make_unique(); + } else { + use_existing_context = true; + context = + std::unique_ptr(deserialize_options->context); + } + IfrtIrProgramProto program_proto; if (!program_proto.ParseFromString(serialized)) { return absl::InvalidArgumentError("Failed to parse IfrtIrProgramProto"); } - auto context = std::make_unique(); TF_ASSIGN_OR_RETURN( auto module, support::ParseMlirModuleString(program_proto.ifrt_program(), *context)); @@ -144,8 +155,14 @@ class IfrtIRProgramSerDes if (program_proto.ifrt_version().empty()) { // The program was not versioned on serialization. The whole IFRT IR // program was serialized to bytecode. - return std::make_unique(std::move(context), - std::move(module)); + if (use_existing_context) { + // Release the point s.t. the existing context is not freed. + context.release(); + return std::make_unique(module.release()); + } else { + return std::make_unique(std::move(context), + std::move(module)); + } } else { // Run the pipeline to convert a versioned IFRT IR program artifact to // an IFRT IR program. @@ -158,8 +175,14 @@ class IfrtIRProgramSerDes diagnostic_handler.ConsumeStatus().message())); } - return std::make_unique(std::move(context), - std::move(module)); + if (use_existing_context) { + // Release the point s.t. the existing context is not freed. + context.release(); + return std::make_unique(module.release()); + } else { + return std::make_unique(std::move(context), + std::move(module)); + } } } diff --git a/xla/python/ifrt/ir/tests/BUILD b/xla/python/ifrt/ir/tests/BUILD index 14071d41811bc4..a154eed9e47460 100644 --- a/xla/python/ifrt/ir/tests/BUILD +++ b/xla/python/ifrt/ir/tests/BUILD @@ -43,6 +43,29 @@ xla_cc_binary( ], ) +xla_cc_binary( + name = "ifrt-translate", + testonly = True, + srcs = ["ifrt-translate.cc"], + deps = [ + "//xla/python/ifrt:serdes", + "//xla/python/ifrt/ir", + "//xla/python/ifrt/ir:ifrt_ir_program", + "//xla/python/ifrt/ir:ifrt_ir_program_serdes", # build_cleaner: keep + "//xla/python/ifrt/ir:version", + "//xla/python/ifrt/ir:vifrt_ops", + "@llvm-project//llvm:Support", + "@llvm-project//mlir:AllPassesAndDialects", + "@llvm-project//mlir:IR", + "@llvm-project//mlir:Pass", + "@llvm-project//mlir:Support", + "@llvm-project//mlir:Transforms", + "@llvm-project//mlir:TranslateLib", + "@stablehlo//:register", + "@stablehlo//:version", + ], +) + cc_library( name = "executable_impl_test_base", testonly = True, diff --git a/xla/python/ifrt/ir/tests/ifrt-translate.cc b/xla/python/ifrt/ir/tests/ifrt-translate.cc new file mode 100644 index 00000000000000..ae91e552a83f0e --- /dev/null +++ b/xla/python/ifrt/ir/tests/ifrt-translate.cc @@ -0,0 +1,124 @@ +/* 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 +#include + +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/raw_ostream.h" +#include "mlir/IR/BuiltinOps.h" +#include "mlir/IR/MLIRContext.h" +#include "mlir/InitAllDialects.h" +#include "mlir/Pass/PassManager.h" +#include "mlir/Support/LLVM.h" +#include "mlir/Tools/mlir-translate/MlirTranslateMain.h" +#include "mlir/Tools/mlir-translate/Translation.h" +#include "mlir/Transforms/Passes.h" +#include "stablehlo/dialect/Register.h" +#include "stablehlo/dialect/Version.h" +#include "xla/python/ifrt/ir/ifrt_dialect.h" +#include "xla/python/ifrt/ir/ifrt_ir_program.h" +#include "xla/python/ifrt/ir/version.h" +#include "xla/python/ifrt/ir/vifrt_ops.h" +#include "xla/python/ifrt/serdes.h" + +namespace xla { +namespace ifrt { + +// NOLINTNEXTLINE +llvm::cl::opt strip_debug_info_option( + "strip_debuginfo", llvm::cl::desc("Strip debug info from all operations"), + llvm::cl::init(false)); + +// NOLINTNEXTLINE +llvm::cl::opt ifrt_version_option( + "ifrt_version", llvm::cl::desc("Target version for IFRT IR serialization"), + llvm::cl::init("current")); + +// NOLINTNEXTLINE +llvm::cl::opt atom_program_version_option( + "atom_program_version", + llvm::cl::desc("Target version for atom program serialization"), + llvm::cl::init("current")); + +mlir::TranslateFromMLIRRegistration serializeRegistration( + "serialize", "Serialize IFRT IR program into a VIFRT artifact", + [](mlir::ModuleOp module, llvm::raw_ostream &os) -> mlir::LogicalResult { + std::string ifrt_version = ifrt_version_option.getValue(); + if (ifrt_version == "current") { + ifrt_version = Version::getCurrentVersion().toString(); + } + std::string atom_program_version = atom_program_version_option.getValue(); + if (atom_program_version == "current") { + atom_program_version = + ::mlir::vhlo::Version::getCurrentVersion().toString(); + } + if (strip_debug_info_option) { + mlir::PassManager pm(module->getContext()); + pm.addPass(mlir::createStripDebugInfoPass()); + if (mlir::failed(pm.run(module))) + return module.emitError("failed to strip debuginfo"); + } + + auto program = std::make_unique(module); + auto serialized_or = + Serialize(*program, std::make_unique( + ifrt_version, atom_program_version)); + if (serialized_or.ok()) { + os << serialized_or->SerializeAsString(); + return mlir::success(); + } else { + return mlir::failure(); + } + }, + [](mlir::DialectRegistry ®istry) { + mlir::registerAllDialects(registry); + mlir::stablehlo::registerAllDialects(registry); + registry.insert(); + registry.insert(); + }); + +mlir::TranslateToMLIRRegistration deserializeRegistration( + "deserialize", "Deserialize VIFRT into an IFRT IR program", + [](llvm::StringRef input, + mlir::MLIRContext *context) -> mlir::OwningOpRef { + Serialized serialized_proto; + if (!serialized_proto.ParseFromString(std::string(input))) { + return nullptr; + } + auto deserialized_program_or = Deserialize( + serialized_proto, + std::make_unique(context)); + if (deserialized_program_or.ok()) { + return mlir::OwningOpRef( + deserialized_program_or.value()->mlir_module); + } else { + return nullptr; + } + }, + [](mlir::DialectRegistry ®istry) { + mlir::registerAllDialects(registry); + mlir::stablehlo::registerAllDialects(registry); + registry.insert(); + registry.insert(); + }); + +} // namespace ifrt +} // namespace xla + +int main(int argc, char **argv) { + return mlir::failed( + mlir::mlirTranslateMain(argc, argv, "IFRT IR translate driver\n")); +}