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/xla/service/compile_only_service.h"
17
18 #include <string>
19 #include <utility>
20 #include <vector>
21
22 #include "absl/strings/str_cat.h"
23 #include "tensorflow/compiler/xla/debug_options_flags.h"
24 #include "tensorflow/compiler/xla/service/backend.h"
25 #include "tensorflow/compiler/xla/service/computation_layout.h"
26 #include "tensorflow/compiler/xla/service/dump.h"
27 #include "tensorflow/compiler/xla/service/platform_util.h"
28 #include "tensorflow/compiler/xla/status_macros.h"
29 #include "tensorflow/compiler/xla/types.h"
30 #include "tensorflow/compiler/xla/util.h"
31 #include "tensorflow/core/lib/gtl/cleanup.h"
32 #include "tensorflow/core/lib/io/path.h"
33 #include "tensorflow/core/platform/host_info.h"
34 #include "tensorflow/core/platform/logging.h"
35 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
36
37 namespace xla {
38
39 /* static */ StatusOr<std::unique_ptr<CompileOnlyService>>
NewService(se::Platform * platform)40 CompileOnlyService::NewService(se::Platform* platform) {
41 ServiceOptions default_options;
42 default_options.set_platform(platform);
43 return NewService(default_options);
44 }
45
46 /* static */ StatusOr<std::unique_ptr<CompileOnlyService>>
NewService(const ServiceOptions & options)47 CompileOnlyService::NewService(const ServiceOptions& options) {
48 se::Platform* platform = options.platform();
49 if (platform == nullptr) {
50 TF_ASSIGN_OR_RETURN(platform, PlatformUtil::GetDefaultPlatform());
51 }
52
53 TF_ASSIGN_OR_RETURN(auto compiler, Compiler::GetForPlatform(platform));
54
55 std::unique_ptr<CompileOnlyService> service(
56 new CompileOnlyService(options, compiler));
57 return std::move(service);
58 }
59
CompileOnlyService(const ServiceOptions & options,Compiler * compiler)60 CompileOnlyService::CompileOnlyService(const ServiceOptions& options,
61 Compiler* compiler)
62 : Service(options, /*execute_backend=*/nullptr), compiler_(compiler) {}
63
64 StatusOr<std::vector<std::unique_ptr<AotCompilationResult>>>
CompileAheadOfTime(const absl::Span<const AotXlaComputationInstance> computations,const AotCompilationOptions & options,std::unique_ptr<AotCompilationMetadata> * metadata)65 CompileOnlyService::CompileAheadOfTime(
66 const absl::Span<const AotXlaComputationInstance> computations,
67 const AotCompilationOptions& options,
68 std::unique_ptr<AotCompilationMetadata>* metadata) {
69 std::vector<std::unique_ptr<HloModule>> hlo_modules;
70
71 const DebugOptions& debug_options = options.debug_options();
72 ExecutionOptions execution_options;
73 *execution_options.mutable_debug_options() = debug_options;
74 // Capture replica_count, num_cores, and device_assignment in ExecutionOptions
75 // to later save in a proto dump.
76 if (options.replica_count() > 0) {
77 execution_options.set_num_replicas(options.replica_count());
78 if (options.has_static_device_assignment()) {
79 CHECK_EQ(options.replica_count(),
80 options.static_device_assignment().replica_count());
81 }
82 }
83 if (options.num_cores() > 0) {
84 execution_options.set_num_partitions(options.num_cores());
85 if (options.has_static_device_assignment()) {
86 CHECK_EQ(options.num_cores(),
87 options.static_device_assignment().computation_count());
88 }
89 }
90 if (options.has_static_device_assignment()) {
91 TF_RETURN_IF_ERROR(options.static_device_assignment().Serialize(
92 execution_options.mutable_device_assignment()));
93 }
94 execution_options.set_use_spmd_partitioning(options.use_spmd_partitioning());
95 execution_options.set_deduplicate_hlo(options.deduplicate_hlo());
96 for (const AotXlaComputationInstance& instance : computations) {
97 TF_RET_CHECK(instance.computation.has_host_program_shape());
98 *execution_options.mutable_shape_with_output_layout() =
99 instance.result_layout->ToProto();
100
101 TF_ASSIGN_OR_RETURN(
102 std::unique_ptr<HloModuleConfig> module_config,
103 CreateModuleConfig(
104 ProgramShape(instance.computation.host_program_shape()),
105 instance.argument_layouts, &execution_options, &options));
106
107 TF_ASSIGN_OR_RETURN(
108 std::unique_ptr<HloModule> hlo_module,
109 HloModule::CreateFromProto(instance.computation, *module_config));
110 DumpHloModuleIfEnabled(*hlo_module, "before_optimizations");
111 hlo_modules.push_back(std::move(hlo_module));
112 }
113
114 execution_options.clear_shape_with_output_layout();
115 DumpExecutionOptions(execution_options, debug_options);
116
117 return compiler_->CompileAheadOfTime(
118 absl::make_unique<HloModuleGroup>(hlo_modules[0]->name(),
119 absl::MakeSpan(hlo_modules)),
120 options, metadata);
121 }
122
123 } // namespace xla
124