Skip to content
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

Fork xla::ExecuteOptions into xla::ifrt::ExecuteOptions #17725

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
7 changes: 7 additions & 0 deletions xla/python/ifrt/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ cc_library(
":attribute_map",
":device_proto_cc",
":dtype_proto_cc",
":execute_options_proto_cc",
":remap_plan_proto_cc",
":serdes",
":shape_proto_cc",
Expand All @@ -109,6 +110,7 @@ cc_library(
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/base",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/container:inlined_vector",
"@com_google_absl//absl/container:node_hash_set",
"@com_google_absl//absl/functional:function_ref",
Expand Down Expand Up @@ -165,6 +167,11 @@ xla_cc_test(
],
)

tf_proto_library(
name = "execute_options_proto",
srcs = ["execute_options.proto"],
)

xla_cc_test(
name = "future_test",
size = "small",
Expand Down
25 changes: 25 additions & 0 deletions xla/python/ifrt/executable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,36 @@ limitations under the License.

#include "xla/python/ifrt/executable.h"

#include "absl/status/statusor.h"
#include "xla/python/ifrt/execute_options.pb.h"

namespace xla {
namespace ifrt {

char Executable::ID = 0;
char LoadedExecutable::ID = 0;

absl::StatusOr<xla::ifrt::ExecuteOptionsProto> ExecuteOptions::ToProto() const {
ExecuteOptionsProto proto;

proto.set_launch_id(launch_id);
proto.mutable_non_donatable_input_indices()->Add(
non_donatable_input_indices.begin(), non_donatable_input_indices.end());

return proto;
}

absl::StatusOr<xla::ifrt::ExecuteOptions> ExecuteOptions::FromProto(
const xla::ifrt::ExecuteOptionsProto& proto) {
ExecuteOptions options;

options.launch_id = proto.launch_id();
options.non_donatable_input_indices.insert(
proto.non_donatable_input_indices().begin(),
proto.non_donatable_input_indices().end());

return options;
}

} // namespace ifrt
} // namespace xla
27 changes: 25 additions & 2 deletions xla/python/ifrt/executable.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitations under the License.
#include <string>
#include <vector>

#include "absl/container/flat_hash_set.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
Expand All @@ -33,6 +34,7 @@ limitations under the License.
#include "xla/python/ifrt/attribute_map.h"
#include "xla/python/ifrt/device.h"
#include "xla/python/ifrt/device_list.h"
#include "xla/python/ifrt/execute_options.pb.h"
#include "xla/python/ifrt/future.h"
#include "xla/tsl/concurrency/ref_count.h"

Expand Down Expand Up @@ -104,6 +106,28 @@ class Executable : public llvm::RTTIExtends<Executable, llvm::RTTIRoot> {
static char ID; // NOLINT
};

struct ExecuteOptions {
// If non-zero, identifies this execution as part of a potentially
// multi-device launch. This can be used to detect scheduling errors, e.g. if
// multi-host programs are launched in different orders on different hosts,
// the launch IDs may be used by the runtime to detect the mismatch.
int32_t launch_id = 0;

// A set of indices denoting the input arrays that should not be donated. An
// input array may be non-donable, for example, if it is referenced more than
// once. Since such runtime information is not available at compile time, the
// compiler might mark the input as `may-alias`, which could lead IFRT to
// donate the input array when it should not. By defining this set of indices,
// a higher-level IFRT caller can instruct IFRT client not to donate specific
// input arrays.
absl::flat_hash_set<int> non_donatable_input_indices;

absl::StatusOr<ExecuteOptionsProto> ToProto() const;

static absl::StatusOr<ExecuteOptions> FromProto(
const ExecuteOptionsProto& proto);
};

// Wraps a computation that has been fully compiled and loaded for execution.
class LoadedExecutable
: public llvm::RTTIExtends<LoadedExecutable, llvm::RTTIRoot> {
Expand Down Expand Up @@ -176,8 +200,7 @@ class LoadedExecutable

// `LoadedExecutable` methods.

// Short-term alias.
using ExecuteOptions = ::xla::ExecuteOptions;
using ExecuteOptions = xla::ifrt::ExecuteOptions;

// Result from an execution.
struct ExecuteResult {
Expand Down
26 changes: 26 additions & 0 deletions xla/python/ifrt/execute_options.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* 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.
==============================================================================*/

syntax = "proto3";

package xla.ifrt;

message ExecuteOptionsProto {
bool untuple_result = 2;
int32 launch_id = 3;
repeated int32 non_donatable_input_indices = 7;

reserved 1, 4 to 6, 8;
}
1 change: 1 addition & 0 deletions xla/python/ifrt_proxy/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ tf_proto_library(
"//xla/pjrt:execute_options_proto",
"//xla/python/ifrt:attribute_map_proto",
"//xla/python/ifrt:dtype_proto",
"//xla/python/ifrt:execute_options_proto",
"//xla/python/ifrt:remap_plan_proto",
"//xla/python/ifrt:serdes_proto",
"//xla/python/ifrt:shape_proto",
Expand Down
4 changes: 2 additions & 2 deletions xla/python/ifrt_proxy/common/ifrt_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ syntax = "proto3";
package xla.ifrt.proxy;

import "google/protobuf/any.proto";
import "xla/pjrt/execute_options.proto";
import "xla/python/ifrt/attribute_map.proto";
import "xla/python/ifrt/dtype.proto";
import "xla/python/ifrt/execute_options.proto";
import "xla/python/ifrt/remap_plan.proto";
import "xla/python/ifrt/serdes.proto";
import "xla/python/ifrt/shape.proto";
Expand Down Expand Up @@ -428,7 +428,7 @@ message LoadedExecutableMetadataResponse {
message LoadedExecutableExecuteRequest {
fixed64 loaded_executable_handle = 1;
repeated fixed64 args_handles = 2;
xla.ExecuteOptionsProto execute_options = 3;
xla.ifrt.ExecuteOptionsProto execute_options = 3;
repeated int32 device_ids = 4;
}
message LoadedExecutableExecuteResponse {
Expand Down
10 changes: 6 additions & 4 deletions xla/python/pjrt_ifrt/pjrt_executable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,11 @@ PjRtLoadedExecutable::Execute(
const bool returned_future_supported =
pjrt_loaded_executable_->IsReturnedFutureSupported();

auto opts = options;
xla::ExecuteOptions opts;
opts.untuple_result = true;
opts.launch_id = options.launch_id;
opts.use_major_to_minor_data_layout_for_callbacks = true;
opts.non_donatable_input_indices = options.non_donatable_input_indices;

if (!all_loaded_host_callbacks_->empty() && !returned_future_supported) {
return Internal(
Expand All @@ -565,9 +569,7 @@ PjRtLoadedExecutable::Execute(
contexts.push_back(CreateHostCallbackStateAndAppendSendRecvCallbacks(
host_send_recv_callback->host_callback(),
/*host_memory_for_device_manager=*/nullptr, send_callbacks,
recv_callbacks,
/*use_major_to_minor_data_layout_for_callbacks=*/
options.use_major_to_minor_data_layout_for_callbacks));
recv_callbacks, opts.use_major_to_minor_data_layout_for_callbacks));
}
}
opts.send_callbacks = host_callback_states->send_callbacks;
Expand Down
4 changes: 1 addition & 3 deletions xla/python/py_executable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,11 @@ PyLoadedExecutable::PyLoadedExecutable(
if (next_) {
next_->prev_ = this;
}
options_.untuple_result = true;
if (fingerprint_) {
options_.launch_id = tsl::Fingerprint32(*fingerprint_);
VLOG(1) << "Fingerprint for executable " << ifrt_loaded_executable_->name()
<< ": " << *fingerprint_;
}
options_.use_major_to_minor_data_layout_for_callbacks = true;
}

PyLoadedExecutable::~PyLoadedExecutable() {
Expand Down Expand Up @@ -203,7 +201,7 @@ void PopulateExecuteShardedResults(

template <typename ArgT, typename ArgAdapter = ShardedBufferAdapter<ArgT>>
absl::StatusOr<PyExecuteResults> ExecuteShardedOnLocalDevicesInternal(
const ExecuteOptions& options, const nb_class_ptr<PyClient>& client,
const ifrt::ExecuteOptions& options, const nb_class_ptr<PyClient>& client,
ifrt::LoadedExecutable* ifrt_loaded_executable, absl::Span<const ArgT> args,
std::optional<std::vector<PjRtFuture<>>>& returned_futures,
bool attach_status_to_results) {
Expand Down
4 changes: 2 additions & 2 deletions xla/python/py_executable.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class PyLoadedExecutable {
return exec->shared_ptr_pjrt_loaded_executable();
}

const ExecuteOptions& options() const { return options_; }
const ifrt::ExecuteOptions& options() const { return options_; }
const std::optional<std::string>& fingerprint() const { return fingerprint_; }

// Keep `obj` alive as long as PyLoadedExecutable.
Expand All @@ -246,7 +246,7 @@ class PyLoadedExecutable {
std::optional<std::string> fingerprint_;

// The options to pass to `executable_.Execute`.
ExecuteOptions options_;
ifrt::ExecuteOptions options_;

// Python objects to keep alive as requested by user.
std::vector<nanobind::object> keepalives_;
Expand Down
Loading