1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/compiler/aot/compile.h"
17
18 #include <memory>
19 #include <string>
20 #include <utility>
21 #include <vector>
22
23 #include "tensorflow/compiler/aot/flags.h"
24 #include "tensorflow/compiler/tf2xla/tf2xla.h"
25 #include "tensorflow/compiler/tf2xla/tf2xla_util.h"
26 #include "tensorflow/compiler/xla/client/client_library.h"
27 #include "tensorflow/compiler/xla/client/compile_only_client.h"
28 #include "tensorflow/compiler/xla/client/xla_computation.h"
29 #include "tensorflow/compiler/xla/service/cpu/cpu_compiler.h"
30 #include "tensorflow/compiler/xla/statusor.h"
31 #include "tensorflow/compiler/xla/util.h"
32 #include "tensorflow/compiler/xla/xla_data.pb.h"
33 #include "tensorflow/core/framework/graph.pb.h"
34 #include "tensorflow/core/lib/core/errors.h"
35 #include "tensorflow/core/lib/io/path.h"
36 #include "tensorflow/core/lib/strings/proto_serialization.h"
37 #include "tensorflow/core/platform/env.h"
38 #include "tensorflow/core/platform/logging.h"
39 #include "tensorflow/core/platform/types.h"
40
41 namespace tensorflow {
42 namespace tfcompile {
43
44 namespace {
45
46 // Compiles the XLA computation into executable code.
CompileXla(xla::CompileOnlyClient * client,const xla::XlaComputation & computation,const xla::cpu::CpuAotCompilationOptions & aot_opts,CompileResult * compile_result)47 Status CompileXla(xla::CompileOnlyClient* client,
48 const xla::XlaComputation& computation,
49 const xla::cpu::CpuAotCompilationOptions& aot_opts,
50 CompileResult* compile_result) {
51 // Retrieves arg and result layouts from the computation.
52 // TODO(toddw): Should we let the user choose the major/minor ordering?
53 xla::StatusOr<std::unique_ptr<xla::ProgramShape>> pshape_or =
54 client->GetComputationShape(computation);
55 if (!pshape_or.ok()) {
56 return errors::Unknown("Couldn't get XLA program shape: ",
57 pshape_or.status().error_message());
58 }
59 compile_result->program_shape = pshape_or.ValueOrDie()->ToProto();
60 xla::ProgramShapeProto* pshape = &compile_result->program_shape;
61
62 // AotXlaComputationInstance::argument_layouts is a vector of Shape
63 // pointers. Accumulate the Shape objects themselves in a separate vector
64 // while building the vector of pointers.
65 std::vector<const xla::Shape*> arg_layout_ptrs(pshape->parameters_size());
66 std::vector<xla::Shape> arg_layouts(pshape->parameters_size());
67 for (int i = 0; i < pshape->parameters_size(); ++i) {
68 arg_layouts[i] = xla::Shape(*pshape->mutable_parameters(i));
69 arg_layout_ptrs[i] = &arg_layouts[i];
70 }
71 xla::CompileOnlyClient::AotXlaComputationInstance instance;
72 instance.computation = &computation;
73 instance.argument_layouts = std::move(arg_layout_ptrs);
74 xla::Shape result_shape(pshape->result());
75 instance.result_layout = &result_shape;
76 xla::StatusOr<std::vector<std::unique_ptr<xla::AotCompilationResult>>>
77 aot_or = client->CompileAheadOfTime({instance}, aot_opts);
78 if (!aot_or.ok()) {
79 return errors::Unknown("XLA compilation failed: ",
80 aot_or.status().error_message());
81 }
82 compile_result->aot =
83 xla::unique_ptr_static_cast<xla::cpu::CpuAotCompilationResult>(
84 std::move(aot_or.ValueOrDie().back()));
85 compile_result->entry_point = aot_opts.entry_point_name();
86 compile_result->pointer_size =
87 xla::CompileOnlyClient::PointerSizeForTriple(aot_opts.triple());
88 return Status::OK();
89 }
90
91 } // namespace
92
CompileGraph(const GraphDef & graph_def,const tf2xla::Config & config,const MainFlags & flags,CompileResult * compile_result)93 Status CompileGraph(const GraphDef& graph_def, const tf2xla::Config& config,
94 const MainFlags& flags, CompileResult* compile_result) {
95 // Converts the graph into an XLA computation, and compiles the
96 // computation.
97 // TODO(toddw): Should we let the user pick the XLA cpu vs. gpu client?
98 se::Platform* cpu_platform =
99 se::MultiPlatformManager::PlatformWithName("Host").ValueOrDie();
100 xla::CompileOnlyClient* client =
101 xla::ClientLibrary::GetOrCreateCompileOnlyClient(cpu_platform)
102 .ValueOrDie();
103 xla::XlaComputation computation;
104 TF_RETURN_IF_ERROR(
105 ConvertGraphDefToXla(graph_def, config, client, &computation));
106 if (!flags.out_session_module.empty()) {
107 TF_ASSIGN_OR_RETURN(std::unique_ptr<xla::HloSnapshot> module,
108 computation.Snapshot());
109 // Serialize the HloSnapshot deterministically so that all the outputs of a
110 // tf_library genrule are deterministic.
111 const size_t size = module->ByteSizeLong();
112 auto serialized = absl::make_unique<char[]>(size);
113 TF_RET_CHECK(
114 SerializeToBufferDeterministic(*module, serialized.get(), size));
115 TF_RETURN_IF_ERROR(
116 WriteStringToFile(Env::Default(), flags.out_session_module,
117 absl::string_view(serialized.get(), size)));
118 }
119 xla::cpu::CpuAotCompilationOptions aot_opts(
120 flags.target_triple, flags.target_cpu, flags.target_features,
121 flags.entry_point,
122 xla::cpu::CpuAotCompilationOptions::RelocationModel::BigPic);
123
124 return CompileXla(client, computation, aot_opts, compile_result);
125 }
126
127 } // namespace tfcompile
128 } // namespace tensorflow
129