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/hlo_module_config.h"
17 
18 #include <atomic>
19 #include <vector>
20 
21 #include "absl/memory/memory.h"
22 #include "absl/strings/str_cat.h"
23 #include "absl/strings/str_join.h"
24 #include "tensorflow/compiler/xla/shape_layout.h"
25 #include "tensorflow/compiler/xla/types.h"
26 
27 namespace xla {
28 
29 using absl::StrAppend;
30 
HloModuleConfig(const ProgramShape & program_shape,bool ignore_layouts)31 HloModuleConfig::HloModuleConfig(const ProgramShape& program_shape,
32                                  bool ignore_layouts)
33     : entry_computation_layout_(
34           ComputationLayout(program_shape, ignore_layouts)) {}
35 
HloModuleConfig(ComputationLayout entry_computation_layout)36 HloModuleConfig::HloModuleConfig(ComputationLayout entry_computation_layout)
37     : entry_computation_layout_(std::move(entry_computation_layout)) {}
38 
SetDefaultComputationLayout(const ProgramShape & program_shape)39 void HloModuleConfig::SetDefaultComputationLayout(
40     const ProgramShape& program_shape) {
41   entry_computation_layout_ = ComputationLayout(program_shape);
42 }
43 
SetComputationLayoutIfExists(const ProgramShape & program_shape)44 void HloModuleConfig::SetComputationLayoutIfExists(
45     const ProgramShape& program_shape) {
46   entry_computation_layout_ = ComputationLayout(program_shape,
47                                                 /*ignore_layouts=*/false);
48 }
49 
compilation_cache_key() const50 string HloModuleConfig::compilation_cache_key() const {
51   string key = absl::StrCat("profiling=", hlo_profiling_enabled());
52   StrAppend(&key, "::(");
53   std::vector<string> params;
54   if (entry_computation_layout_.has_value()) {
55     for (const ShapeLayout& param_layout :
56          entry_computation_layout_->parameter_layouts()) {
57       params.push_back(param_layout.shape().DebugString());
58     }
59     StrAppend(&key, absl::StrJoin(params, ", "), ") => ",
60               entry_computation_layout_->result_shape().SerializeAsString());
61   }
62   if (seed() != 0) {
63     // TODO(b/32083678): force recompilation to reset global state.
64     static std::atomic<int> counter{0};
65     StrAppend(&key, "forcing recompile ", counter++);
66   }
67   if (replica_count() != 1) {
68     StrAppend(&key, "::replica_count=", replica_count());
69   }
70   StrAppend(&key, debug_options_.DebugString());
71   if (intra_op_parallelism_threads() > 0) {
72     StrAppend(&key, "::intra_op_parallelism_threads=",
73               intra_op_parallelism_threads());
74   }
75   StrAppend(&key, "::alias_passthrough_params=", alias_passthrough_params_);
76   return key;
77 }
78 
79 }  // namespace xla
80