Skip to content

Commit

Permalink
[IFRT] Add ifrt-translate mlir tool for verifying dialect conversions.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ICGog authored and Google-ML-Automation committed Nov 14, 2024
1 parent dde3c51 commit d56e8be
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 6 deletions.
1 change: 1 addition & 0 deletions xla/python/ifrt/ir/ifrt_ir_program.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace ifrt {

char IfrtIRProgram::ID = 0;
char SerializeIfrtIRProgramOptions::ID = 0;
char DeserializeIfrtIRProgramOptions::ID = 0;
char IfrtIRCompileOptions::ID = 0;

absl::StatusOr<std::unique_ptr<IfrtIRCompileOptions>> GetIfrtIRCompileOptions(
Expand Down
15 changes: 15 additions & 0 deletions xla/python/ifrt/ir/ifrt_ir_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<DeserializeIfrtIRProgramOptions, DeserializeOptions> {
explicit DeserializeIfrtIRProgramOptions(mlir::MLIRContext* context)
: context(context) {}

static char ID; // NOLINT

mlir::MLIRContext* context;
};

// CompileOptions for an IFRT IR program.
struct IfrtIRCompileOptions
: llvm::RTTIExtends<IfrtIRCompileOptions, CompileOptions> {
Expand Down
35 changes: 29 additions & 6 deletions xla/python/ifrt/ir/ifrt_ir_program_serdes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,38 @@ class IfrtIRProgramSerDes
// IFRT IR versions or VHLO version are outside of the compatibility window.
absl::StatusOr<std::unique_ptr<Serializable>> Deserialize(
const std::string& serialized,
std::unique_ptr<DeserializeOptions>) override {
std::unique_ptr<DeserializeOptions> options) override {
const auto* deserialize_options =
llvm::dyn_cast_or_null<DeserializeIfrtIRProgramOptions>(options.get());
bool use_existing_context = false;
std::unique_ptr<mlir::MLIRContext> context;
if (!deserialize_options || !deserialize_options->context) {
context = std::make_unique<mlir::MLIRContext>();
} else {
use_existing_context = true;
context =
std::unique_ptr<mlir::MLIRContext>(deserialize_options->context);
}

IfrtIrProgramProto program_proto;
if (!program_proto.ParseFromString(serialized)) {
return absl::InvalidArgumentError("Failed to parse IfrtIrProgramProto");
}
auto context = std::make_unique<mlir::MLIRContext>();
TF_ASSIGN_OR_RETURN(
auto module,
support::ParseMlirModuleString(program_proto.ifrt_program(), *context));

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<IfrtIRProgram>(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<IfrtIRProgram>(module.release());
} else {
return std::make_unique<IfrtIRProgram>(std::move(context),
std::move(module));
}
} else {
// Run the pipeline to convert a versioned IFRT IR program artifact to
// an IFRT IR program.
Expand All @@ -158,8 +175,14 @@ class IfrtIRProgramSerDes
diagnostic_handler.ConsumeStatus().message()));
}

return std::make_unique<IfrtIRProgram>(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<IfrtIRProgram>(module.release());
} else {
return std::make_unique<IfrtIRProgram>(std::move(context),
std::move(module));
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions xla/python/ifrt/ir/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
124 changes: 124 additions & 0 deletions xla/python/ifrt/ir/tests/ifrt-translate.cc
Original file line number Diff line number Diff line change
@@ -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 <memory>
#include <string>

#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<bool> strip_debug_info_option(
"strip_debuginfo", llvm::cl::desc("Strip debug info from all operations"),
llvm::cl::init(false));

// NOLINTNEXTLINE
llvm::cl::opt<std::string> ifrt_version_option(
"ifrt_version", llvm::cl::desc("Target version for IFRT IR serialization"),
llvm::cl::init("current"));

// NOLINTNEXTLINE
llvm::cl::opt<std::string> 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<IfrtIRProgram>(module);
auto serialized_or =
Serialize(*program, std::make_unique<SerializeIfrtIRProgramOptions>(
ifrt_version, atom_program_version));
if (serialized_or.ok()) {
os << serialized_or->SerializeAsString();
return mlir::success();
} else {
return mlir::failure();
}
},
[](mlir::DialectRegistry &registry) {
mlir::registerAllDialects(registry);
mlir::stablehlo::registerAllDialects(registry);
registry.insert<xla::ifrt::IfrtDialect>();
registry.insert<xla::ifrt::VifrtDialect>();
});

mlir::TranslateToMLIRRegistration deserializeRegistration(
"deserialize", "Deserialize VIFRT into an IFRT IR program",
[](llvm::StringRef input,
mlir::MLIRContext *context) -> mlir::OwningOpRef<mlir::ModuleOp> {
Serialized serialized_proto;
if (!serialized_proto.ParseFromString(std::string(input))) {
return nullptr;
}
auto deserialized_program_or = Deserialize<IfrtIRProgram>(
serialized_proto,
std::make_unique<DeserializeIfrtIRProgramOptions>(context));
if (deserialized_program_or.ok()) {
return mlir::OwningOpRef<mlir::ModuleOp>(
deserialized_program_or.value()->mlir_module);
} else {
return nullptr;
}
},
[](mlir::DialectRegistry &registry) {
mlir::registerAllDialects(registry);
mlir::stablehlo::registerAllDialects(registry);
registry.insert<xla::ifrt::IfrtDialect>();
registry.insert<xla::ifrt::VifrtDialect>();
});

} // namespace ifrt
} // namespace xla

int main(int argc, char **argv) {
return mlir::failed(
mlir::mlirTranslateMain(argc, argv, "IFRT IR translate driver\n"));
}

0 comments on commit d56e8be

Please sign in to comment.