Skip to content

[xla:ifrt] Remove references to xla::ifrt::Compile that return LoadedExecutables. #28581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 119 additions & 21 deletions jaxlib/py_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ MakeIfrtDeserializeExecutableOptions(std::optional<CompileOptions> options,
} // namespace

/* static */ absl::StatusOr<nb_class_ptr<PyLoadedExecutable>>
PyClient::CompileIfrtProgram(
PyClient::CompileAndLoadIfrtProgram(
nb_class_ptr<PyClient> client, std::unique_ptr<ifrt::Program> ifrt_program,
std::unique_ptr<ifrt::CompileOptions> ifrt_options) {
auto* pjrt_compatible_client =
Expand Down Expand Up @@ -450,9 +450,10 @@ PyClient::CompileIfrtProgram(
std::optional<std::string> fingerprint;
{
nb::gil_scoped_release gil_release;
TF_ASSIGN_OR_RETURN(ifrt_loaded_executable,
client->ifrt_client_->GetDefaultCompiler()->Compile(
std::move(ifrt_program), std::move(ifrt_options)));
TF_ASSIGN_OR_RETURN(
ifrt_loaded_executable,
client->ifrt_client_->GetDefaultCompiler()->CompileAndLoad(
std::move(ifrt_program), std::move(ifrt_options)));
TF_RETURN_IF_ERROR(ifrt_loaded_executable->GetReadyFuture().Await());
TF_ASSIGN_OR_RETURN(fingerprint, ifrt_loaded_executable->Fingerprint());
}
Expand All @@ -462,10 +463,11 @@ PyClient::CompileIfrtProgram(
std::move(traceback), std::move(fingerprint));
}

/* static */ absl::StatusOr<nb_class_ptr<PyLoadedExecutable>> PyClient::Compile(
nb_class_ptr<PyClient> client, std::string mlir_module,
ifrt::DeviceListRef executable_devices, CompileOptions options,
std::vector<nb::capsule> host_callbacks) {
/* static */ absl::StatusOr<nb_class_ptr<PyLoadedExecutable>>
PyClient::CompileAndLoad(nb_class_ptr<PyClient> client, std::string mlir_module,
ifrt::DeviceListRef executable_devices,
CompileOptions options,
std::vector<nb::capsule> host_callbacks) {
mlir::MLIRContext context;
TF_ASSIGN_OR_RETURN(mlir::OwningOpRef<mlir::ModuleOp> module,
ParseMlirModuleString(mlir_module, context));
Expand All @@ -474,16 +476,17 @@ PyClient::CompileIfrtProgram(
// export it before going to HLO while preserving Shardy ops and attrs.
TF_RETURN_IF_ERROR(ExportShardyForHloRoundTrip(*module));
}
return CompileIfrtProgram(
return CompileAndLoadIfrtProgram(
client, std::make_unique<xla::ifrt::HloProgram>(module.get()),
MakeIfrtCompileOptions(std::move(options), std::move(executable_devices),
std::move(host_callbacks)));
}

/* static */ absl::StatusOr<nb_class_ptr<PyLoadedExecutable>> PyClient::Compile(
nb_class_ptr<PyClient> client, std::string mlir_module,
ifrt::DeviceListRef executable_devices, CompileOptions options,
std::vector<nb::callable> host_callbacks) {
/* static */ absl::StatusOr<nb_class_ptr<PyLoadedExecutable>>
PyClient::CompileAndLoad(nb_class_ptr<PyClient> client, std::string mlir_module,
ifrt::DeviceListRef executable_devices,
CompileOptions options,
std::vector<nb::callable> host_callbacks) {
mlir::MLIRContext context;
TF_ASSIGN_OR_RETURN(mlir::OwningOpRef<mlir::ModuleOp> module,
ParseMlirModuleString(mlir_module, context));
Expand Down Expand Up @@ -511,7 +514,7 @@ PyClient::CompileIfrtProgram(
auto compile_options = std::make_unique<ifrt::XlaCompileOptions>(
std::move(options), std::move(ifrt_loaded_host_callbacks));
#endif
return CompileIfrtProgram(
return CompileAndLoadIfrtProgram(
client, std::make_unique<xla::ifrt::HloProgram>(module.get()),
std::move(compile_options));
}
Expand Down Expand Up @@ -765,7 +768,7 @@ PyType_Slot PyClient::slots_[] = {
std::vector<nb::capsule> host_callbacks) {
ifrt::DeviceListRef executable_devices =
ValueOrThrow(py_executable_devices.ifrt_device_list());
return ValueOrThrow(PyClient::Compile(
return ValueOrThrow(PyClient::CompileAndLoad(
std::move(client),
std::string(mlir_module.c_str(), mlir_module.size()),
std::move(executable_devices), std::move(options),
Expand All @@ -781,7 +784,7 @@ PyType_Slot PyClient::slots_[] = {
std::vector<nb::callable> host_callbacks) {
ifrt::DeviceListRef executable_devices =
ValueOrThrow(py_executable_devices.ifrt_device_list());
return ValueOrThrow(PyClient::Compile(
return ValueOrThrow(PyClient::CompileAndLoad(
std::move(client),
std::string(mlir_module.c_str(), mlir_module.size()),
std::move(executable_devices), std::move(options),
Expand All @@ -797,7 +800,7 @@ PyType_Slot PyClient::slots_[] = {
std::vector<nb::capsule> host_callbacks) {
ifrt::DeviceListRef executable_devices =
ValueOrThrow(py_executable_devices.ifrt_device_list());
return ValueOrThrow(PyClient::Compile(
return ValueOrThrow(PyClient::CompileAndLoad(
std::move(client), std::move(mlir_module),
std::move(executable_devices), std::move(options),
std::move(host_callbacks)));
Expand All @@ -812,7 +815,7 @@ PyType_Slot PyClient::slots_[] = {
std::vector<nb::callable> host_callbacks) {
ifrt::DeviceListRef executable_devices =
ValueOrThrow(py_executable_devices.ifrt_device_list());
return ValueOrThrow(PyClient::Compile(
return ValueOrThrow(PyClient::CompileAndLoad(
std::move(client), std::move(mlir_module),
std::move(executable_devices), std::move(options),
std::move(host_callbacks)));
Expand All @@ -829,7 +832,7 @@ PyType_Slot PyClient::slots_[] = {
ifrt::DeviceListRef executable_devices =
ValueOrThrow(jax::PyDeviceList(nb::tuple(py_executable_devices))
.ifrt_device_list());
return ValueOrThrow(PyClient::Compile(
return ValueOrThrow(PyClient::CompileAndLoad(
std::move(client),
std::string(mlir_module.c_str(), mlir_module.size()),
std::move(executable_devices), std::move(options),
Expand All @@ -844,15 +847,110 @@ PyType_Slot PyClient::slots_[] = {
ifrt::DeviceListRef executable_devices =
ValueOrThrow(jax::PyDeviceList(nb::tuple(py_executable_devices))
.ifrt_device_list());
return ValueOrThrow(PyClient::Compile(
return ValueOrThrow(PyClient::CompileAndLoad(
std::move(client), std::move(mlir_module),
std::move(executable_devices), std::move(options),
std::vector<nb::capsule>()));
},
nb::arg("computation"), nb::arg("executable_devices"),
nb::arg("compile_options") = CompileOptions())
.def(
"compile_and_load",
[](nb_class_ptr<PyClient> client, nb::bytes mlir_module,
jax::PyDeviceList& py_executable_devices, CompileOptions options,
std::vector<nb::capsule> host_callbacks) {
ifrt::DeviceListRef executable_devices =
ValueOrThrow(py_executable_devices.ifrt_device_list());
return ValueOrThrow(PyClient::CompileAndLoad(
std::move(client),
std::string(mlir_module.c_str(), mlir_module.size()),
std::move(executable_devices), std::move(options),
std::move(host_callbacks)));
},
nb::arg("computation"), nb::arg("executable_devices"),
nb::arg("compile_options") = CompileOptions(),
nb::arg("host_callbacks") = std::vector<nb::capsule>())
.def(
"compile_and_load",
[](nb_class_ptr<PyClient> client, nb::bytes mlir_module,
jax::PyDeviceList& py_executable_devices, CompileOptions options,
std::vector<nb::callable> host_callbacks) {
ifrt::DeviceListRef executable_devices =
ValueOrThrow(py_executable_devices.ifrt_device_list());
return ValueOrThrow(PyClient::CompileAndLoad(
std::move(client),
std::string(mlir_module.c_str(), mlir_module.size()),
std::move(executable_devices), std::move(options),
std::move(host_callbacks)));
},
nb::arg("computation"), nb::arg("executable_devices"),
nb::arg("compile_options") = CompileOptions(),
nb::arg("host_callbacks") = std::vector<nb::callable>())
.def(
"compile_and_load",
[](nb_class_ptr<PyClient> client, std::string mlir_module,
jax::PyDeviceList& py_executable_devices, CompileOptions options,
std::vector<nb::capsule> host_callbacks) {
ifrt::DeviceListRef executable_devices =
ValueOrThrow(py_executable_devices.ifrt_device_list());
return ValueOrThrow(PyClient::CompileAndLoad(
std::move(client), std::move(mlir_module),
std::move(executable_devices), std::move(options),
std::move(host_callbacks)));
},
nb::arg("computation"), nb::arg("executable_devices"),
nb::arg("compile_options") = CompileOptions(),
nb::arg("host_callbacks") = std::vector<nb::capsule>())
.def(
"compile_and_load",
[](nb_class_ptr<PyClient> client, std::string mlir_module,
jax::PyDeviceList& py_executable_devices, CompileOptions options,
std::vector<nb::callable> host_callbacks) {
ifrt::DeviceListRef executable_devices =
ValueOrThrow(py_executable_devices.ifrt_device_list());
return ValueOrThrow(PyClient::CompileAndLoad(
std::move(client), std::move(mlir_module),
std::move(executable_devices), std::move(options),
std::move(host_callbacks)));
},
nb::arg("computation"), nb::arg("executable_devices"),
nb::arg("compile_options") = CompileOptions(),
nb::arg("host_callbacks") = std::vector<nb::callable>())
// The following two overloads are for users of deprecated APIs who call
// `backend.compile` but do not have visibility to `DeviceList`.
.def(
"compile_and_load",
[](nb_class_ptr<PyClient> client, nb::bytes mlir_module,
nb::sequence& py_executable_devices, CompileOptions options) {
ifrt::DeviceListRef executable_devices =
ValueOrThrow(jax::PyDeviceList(nb::tuple(py_executable_devices))
.ifrt_device_list());
return ValueOrThrow(PyClient::CompileAndLoad(
std::move(client),
std::string(mlir_module.c_str(), mlir_module.size()),
std::move(executable_devices), std::move(options),
std::vector<nb::capsule>()));
},
nb::arg("computation"), nb::arg("executable_devices"),
nb::arg("compile_options") = CompileOptions())
.def(
"compile_and_load",
[](nb_class_ptr<PyClient> client, std::string mlir_module,
nb::sequence& py_executable_devices, CompileOptions options) {
ifrt::DeviceListRef executable_devices =
ValueOrThrow(jax::PyDeviceList(nb::tuple(py_executable_devices))
.ifrt_device_list());
return ValueOrThrow(PyClient::CompileAndLoad(
std::move(client), std::move(mlir_module),
std::move(executable_devices), std::move(options),
std::vector<nb::capsule>()));
},
nb::arg("computation"), nb::arg("executable_devices"),
nb::arg("compile_options") = CompileOptions())
.def("compile_ifrt_program",
xla::ValueOrThrowWrapper(PyClient::CompileIfrtProgram))
xla::ValueOrThrowWrapper(PyClient::CompileAndLoadIfrtProgram))
.def("compile_and_load_ifrt_program",
xla::ValueOrThrowWrapper(PyClient::CompileAndLoadIfrtProgram))
.def("serialize_executable",
xla::ValueOrThrowWrapper(&PyClient::SerializeExecutable))
.def(
Expand Down
14 changes: 7 additions & 7 deletions jaxlib/py_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,17 @@ class PyClient {
ifrt::Device* device, bool force_copy,
ifrt::Client::HostBufferSemantics host_buffer_semantics);

static absl::StatusOr<nb_class_ptr<PyLoadedExecutable>> CompileIfrtProgram(
nb_class_ptr<PyClient> client,
std::unique_ptr<ifrt::Program> ifrt_program,
std::unique_ptr<ifrt::CompileOptions> ifrt_options);
static absl::StatusOr<nb_class_ptr<PyLoadedExecutable>>
CompileAndLoadIfrtProgram(nb_class_ptr<PyClient> client,
std::unique_ptr<ifrt::Program> ifrt_program,
std::unique_ptr<ifrt::CompileOptions> ifrt_options);

static absl::StatusOr<nb_class_ptr<PyLoadedExecutable>> Compile(
static absl::StatusOr<nb_class_ptr<PyLoadedExecutable>> CompileAndLoad(
nb_class_ptr<PyClient> client, std::string mlir_module,
ifrt::DeviceListRef executable_devices, CompileOptions options,
std::vector<nanobind::capsule> host_callbacks);

static absl::StatusOr<nb_class_ptr<PyLoadedExecutable>> Compile(
static absl::StatusOr<nb_class_ptr<PyLoadedExecutable>> CompileAndLoad(
nb_class_ptr<PyClient> client, std::string mlir_module,
ifrt::DeviceListRef executable_devices, CompileOptions options,
std::vector<nanobind::callable> host_callbacks);
Expand All @@ -193,7 +193,7 @@ class PyClient {
// program through `send_channel_ids` and the results correspond to Recv ops
// through `recv_channel_ids`. It returns the host callback as an opaque
// object whose reference will keep the Python callback alive. The host
// callback can be passed to `PyClient::Compile` or
// callback can be passed to `PyClient::CompileAndLoad` or
// `PyClient::DeserializeExecutable`. The corresponding Send/Recv ops in the
// XLA computation can trigger the execution of this host callback.
// `serializer` is a function that takes `callable` as an argument and returns
Expand Down
Loading