• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/llvm_compiler.h"
17 #include "tensorflow/core/platform/denormal.h"
18 
19 #ifdef __FAST_MATH__
20 #error "Don't build XLA with -ffast-math"
21 #endif
22 
23 namespace xla {
RunHloPassesOnModuleGroup(HloModuleGroup * module_group,absl::Span<se::StreamExecutor * const> executors,DeviceMemoryAllocator * device_allocator)24 Status LLVMCompiler::RunHloPassesOnModuleGroup(
25     HloModuleGroup* module_group,
26     absl::Span<se::StreamExecutor* const> executors,
27     DeviceMemoryAllocator* device_allocator) {
28   return Unimplemented(
29       "Model partitioning not implemented for the CPU/GPU compilers!");
30 }
31 
32 StatusOr<std::vector<std::unique_ptr<Executable>>>
RunBackendOnModuleGroup(std::unique_ptr<HloModuleGroup> module_group,std::vector<std::vector<se::StreamExecutor * >> stream_exec,DeviceMemoryAllocator * device_allocator)33 LLVMCompiler::RunBackendOnModuleGroup(
34     std::unique_ptr<HloModuleGroup> module_group,
35     std::vector<std::vector<se::StreamExecutor*>> stream_exec,
36     DeviceMemoryAllocator* device_allocator) {
37   return Unimplemented(
38       "Model partitioning not implemented for the CPU/GPU compilers!");
39 }
40 
Compile(std::unique_ptr<HloModuleGroup> module_group,std::vector<std::vector<se::StreamExecutor * >> stream_execs,DeviceMemoryAllocator * device_allocator)41 StatusOr<std::vector<std::unique_ptr<Executable>>> LLVMCompiler::Compile(
42     std::unique_ptr<HloModuleGroup> module_group,
43     std::vector<std::vector<se::StreamExecutor*>> stream_execs,
44     DeviceMemoryAllocator* device_allocator) {
45   // Tensorflow tries to enable the following behaviors in all its threads:
46   //
47   //  - Denormals are zero (DAZ): roughly, operations treat denormal floats as
48   //    zero.
49   //  - Flush denormals to zero (FTZ): roughly, operations produce zero instead
50   //    of denormal floats.
51   //
52   // In theory enabling these shouldn't matter since the compiler should ideally
53   // not leak its environment into generated code, but we turn off DAZ and FTZ
54   // to get some defense-in-depth.
55   tensorflow::port::ScopedDontFlushDenormal dont_flush_denormals;
56 
57   std::vector<std::unique_ptr<Executable>> result;
58   std::vector<std::unique_ptr<HloModule>> modules =
59       module_group->ConsumeModules();
60   for (size_t i = 0; i < modules.size(); i++) {
61     if (stream_execs[i].size() != 1) {
62       return Unimplemented(
63           "Model partitioning not implemented for the CPU/GPU compilers!");
64     }
65 
66     TF_ASSIGN_OR_RETURN(modules[i],
67                         RunHloPasses(std::move(modules[i]), stream_execs[i][0],
68                                      device_allocator));
69     TF_ASSIGN_OR_RETURN(std::unique_ptr<Executable> executable,
70                         RunBackend(std::move(modules[i]), stream_execs[i][0],
71                                    device_allocator));
72     result.push_back(std::move(executable));
73   }
74 
75   return {std::move(result)};
76 }
77 }  // namespace xla
78