xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/compiler.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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/compiler.h"
17 
18 #include <string>
19 #include <utility>
20 
21 #include "tensorflow/compiler/xla/types.h"
22 #include "tensorflow/compiler/xla/util.h"
23 #include "tensorflow/core/platform/logging.h"
24 
25 namespace xla {
26 
27 /* static */ absl::Mutex Compiler::platform_compiler_mutex_(absl::kConstInit);
28 
29 std::vector<std::unique_ptr<tensorflow::protobuf::Message>>
ComputeBackendConfigs(const HloInstruction & hlo,se::StreamExecutor * executor) const30 Compiler::ComputeBackendConfigs(const HloInstruction& hlo,
31                                 se::StreamExecutor* executor) const {
32   CHECK(executor != nullptr);
33   return {};
34 }
35 
36 std::unique_ptr<tensorflow::protobuf::Message>
ComputeDefaultBackendConfig(const HloInstruction & hlo,se::StreamExecutor * executor) const37 Compiler::ComputeDefaultBackendConfig(const HloInstruction& hlo,
38                                       se::StreamExecutor* executor) const {
39   CHECK(executor != nullptr);
40   return nullptr;
41 }
42 
43 // Define a default version where metadata is not used.
44 StatusOr<std::vector<std::unique_ptr<AotCompilationResult>>>
CompileAheadOfTime(std::unique_ptr<HloModuleGroup> module_group,const AotCompilationOptions & options,std::unique_ptr<AotCompilationMetadata> * metadata)45 Compiler::CompileAheadOfTime(
46     std::unique_ptr<HloModuleGroup> module_group,
47     const AotCompilationOptions& options,
48     std::unique_ptr<AotCompilationMetadata>* metadata) {
49   if (metadata != nullptr) {
50     return Unimplemented(
51         "Populating AotCompilationMetadata is not implemented on this "
52         "compiler.");
53   }
54   return CompileAheadOfTime(std::move(module_group), options);
55 }
56 
57 /* static */ std::map<se::Platform::Id, Compiler::CompilerFactory>*
GetPlatformCompilerFactories()58 Compiler::GetPlatformCompilerFactories() {
59   static auto* r = new std::map<se::Platform::Id, CompilerFactory>;
60   return r;
61 }
62 
63 /* static */
64 std::map<se::Platform::Id, std::unique_ptr<Compiler>>*
GetPlatformCompilers()65 Compiler::GetPlatformCompilers() {
66   static auto* r = new std::map<se::Platform::Id, std::unique_ptr<Compiler>>;
67   return r;
68 }
69 
RegisterCompilerFactory(se::Platform::Id platform_id,std::function<std::unique_ptr<Compiler> ()> compiler_factory)70 /* static */ void Compiler::RegisterCompilerFactory(
71     se::Platform::Id platform_id,
72     std::function<std::unique_ptr<Compiler>()> compiler_factory) {
73   absl::MutexLock lock(&platform_compiler_mutex_);
74   auto* factories = GetPlatformCompilerFactories();
75   CHECK(factories->find(platform_id) == factories->end())
76       << "Compiler factory already registered for platform";
77   (*factories)[platform_id] = std::move(compiler_factory);
78 }
79 
GetForPlatform(const se::Platform * platform)80 /* static */ StatusOr<Compiler*> Compiler::GetForPlatform(
81     const se::Platform* platform) {
82   absl::MutexLock lock(&platform_compiler_mutex_);
83 
84   auto* compilers = GetPlatformCompilers();
85   // See if we already instantiated a compiler for this platform.
86   {
87     auto it = compilers->find(platform->id());
88     if (it != compilers->end()) {
89       return it->second.get();
90     }
91 
92     // If not, we just fall through to try to create one with a registered
93     // factory.
94   }
95 
96   auto* factories = GetPlatformCompilerFactories();
97   auto it = factories->find(platform->id());
98   if (it == factories->end()) {
99     return NotFound(
100         "could not find registered compiler for platform %s -- was support for "
101         "that platform linked in?",
102         platform->Name());
103   }
104 
105   // And then we invoke the factory, placing the result into the mapping.
106   compilers->insert(std::make_pair(platform->id(), it->second()));
107   return compilers->at(platform->id()).get();
108 }
109 
AotCompilationOptions()110 AotCompilationOptions::AotCompilationOptions()
111     : debug_options_(GetDebugOptionsFromFlags()) {}
112 
113 }  // namespace xla
114