Skip to content

Commit 12b62d1

Browse files
Add TpuCompilationRequestProto and helper function to serialize Compile() arguments.
PiperOrigin-RevId: 320980526 Change-Id: Iacdb521e36cd2245c3eac6580efb72135f6b5ae5
1 parent 909c073 commit 12b62d1

3 files changed

Lines changed: 97 additions & 0 deletions

File tree

‎tensorflow/core/tpu/kernels/tpu_compile.proto‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package tensorflow.tpu;
1919
import "tensorflow/compiler/tf2xla/host_compute_metadata.proto";
2020
import "tensorflow/compiler/xla/service/hlo.proto";
2121
import "tensorflow/compiler/xla/xla_data.proto";
22+
import "tensorflow/core/framework/attr_value.proto";
23+
import "tensorflow/core/framework/function.proto";
2224
import "tensorflow/core/framework/tensor.proto";
2325
import "tensorflow/core/framework/tensor_shape.proto";
2426
import "tensorflow/core/framework/types.proto";
@@ -142,3 +144,42 @@ message TpuAotCompilationRequestProto {
142144
// XLA compiler compilation result.
143145
XlaCompilationResultProto compilation_result = 7;
144146
}
147+
148+
// TPU compilation request for compiling computations into XLA HLO IR and build
149+
// TPU programs.
150+
message TpuCompilationRequestProto {
151+
// A flag reserved for using experimental version of the compilation. By
152+
// default the value should be false.
153+
bool use_experimental = 1;
154+
155+
// Use mlir to lower computation(s) to Hlo.
156+
bool use_mlir = 2;
157+
158+
// If true, returns hlo metadatas.
159+
bool return_hlo_protos = 3;
160+
161+
// If true, unloads cache on session close.
162+
bool unload_cache_on_session_close = 4;
163+
164+
// Compilation metadata.
165+
TPUCompileMetadataProto metadata = 5;
166+
167+
// Computation argument shapes.
168+
repeated TensorShapeProto arg_shapes = 6;
169+
170+
// Input tensor that gives const guarantee to the TF runtime.
171+
repeated TensorProto guaranteed_constants = 7;
172+
173+
// MLIR module definition.
174+
optional string mlir_module = 8;
175+
176+
// A set of named functions used as the input to lowering to Hlo when mlir is
177+
// not used.
178+
optional FunctionDefLibrary fdef_lib = 9;
179+
180+
// The version of the graph definition used to lower TF function to Hlo.
181+
optional int32 graph_def_version = 10;
182+
183+
// Function containing the computation to compile.
184+
optional NameAttrList function = 11;
185+
}

‎tensorflow/core/tpu/kernels/tpu_compile_op_support.cc‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,5 +435,55 @@ StatusOr<TpuAotCompilationRequestProto> CreateTpuAotCompilationRequest(
435435
VLOG(1) << "TpuAotCompilationRequest:\n" << aot_request.DebugString();
436436
return aot_request;
437437
}
438+
439+
StatusOr<TpuCompilationRequestProto> CreateTpuCompilationRequest(
440+
const std::variant<MlirToHloArgs, FunctionToHloArgs>& computation,
441+
const TPUCompileMetadataProto& metadata,
442+
const std::vector<TensorShape>& arg_shapes) {
443+
VLOG(1) << "CreateTpuCompilationRequest.";
444+
TpuCompilationRequestProto compilation_request;
445+
bool use_mlir = computation.index() == 0;
446+
compilation_request.set_use_mlir(use_mlir);
447+
if (use_mlir) {
448+
VLOG(1) << "Serializing MlirModule";
449+
const MlirToHloArgs& mlir_computation = std::get<0>(computation);
450+
*compilation_request.mutable_mlir_module() = mlir_computation.mlir_module;
451+
} else {
452+
VLOG(1) << "Serializing FunctionDefinitionLibrary";
453+
const FunctionToHloArgs& function_computation = std::get<1>(computation);
454+
*compilation_request.mutable_fdef_lib() =
455+
function_computation.flib_def->ToProto();
456+
compilation_request.set_graph_def_version(
457+
function_computation.graph_def_version);
458+
*compilation_request.mutable_function() = *function_computation.function;
459+
// TODO(b/160937500): serializing and copying large guaranteed_constants can
460+
// be a perf hit. There is a future work to refactor the compilation layer
461+
// to avoid passing guaranteed_constants over C_API.
462+
if (function_computation.guaranteed_constants.index() == 0) {
463+
absl::Span<const TensorProto* const> guaranteed_constants =
464+
std::get<0>(function_computation.guaranteed_constants);
465+
for (const TensorProto* constant : guaranteed_constants) {
466+
*compilation_request.add_guaranteed_constants() = *constant;
467+
}
468+
} else {
469+
CHECK_EQ(function_computation.guaranteed_constants.index(), 1);
470+
const OpInputList& guaranteed_constants =
471+
*std::get<1>(function_computation.guaranteed_constants);
472+
for (const Tensor& constant : guaranteed_constants) {
473+
constant.AsProtoTensorContent(
474+
compilation_request.add_guaranteed_constants());
475+
}
476+
}
477+
}
478+
479+
for (const TensorShape& shape : arg_shapes) {
480+
shape.AsProto(compilation_request.add_arg_shapes());
481+
}
482+
483+
*(compilation_request.mutable_metadata()) = metadata;
484+
485+
VLOG(1) << "TpuCompilationRequest:\n" << compilation_request.DebugString();
486+
return compilation_request;
487+
}
438488
} // namespace tpu
439489
} // namespace tensorflow

‎tensorflow/core/tpu/kernels/tpu_compile_op_support.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ CreateTpuAotCompilationRequest(
147147
const std::vector<std::vector<std::pair<int, bool>>>&
148148
per_core_variable_indices,
149149
const absl::optional<xla::DeviceAssignment>& device_assignment);
150+
151+
se::port::StatusOr<TpuCompilationRequestProto> CreateTpuCompilationRequest(
152+
const std::variant<MlirToHloArgs, FunctionToHloArgs>& computation,
153+
const TPUCompileMetadataProto& metadata,
154+
const std::vector<TensorShape>& arg_shapes);
155+
150156
} // namespace tpu
151157
} // namespace tensorflow
152158

0 commit comments

Comments
 (0)