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 "tensorflow/compiler/xla/ptr_util.h"
22 #include "tensorflow/compiler/xla/shape_layout.h"
23 #include "tensorflow/compiler/xla/types.h"
24 #include "tensorflow/core/lib/strings/str_util.h"
25 #include "tensorflow/core/lib/strings/strcat.h"
26 
27 namespace xla {
28 
29 using tensorflow::strings::StrAppend;
30 
HloModuleConfig()31 HloModuleConfig::HloModuleConfig() {}
32 
HloModuleConfig(const ProgramShape & program_shape)33 HloModuleConfig::HloModuleConfig(const ProgramShape& program_shape)
34     : entry_computation_layout_(program_shape) {}
35 
SetDefaultComputationLayout(const ProgramShape & program_shape)36 void HloModuleConfig::SetDefaultComputationLayout(
37     const ProgramShape& program_shape) {
38   entry_computation_layout_ = ComputationLayout(program_shape);
39 }
40 
compilation_cache_key() const41 string HloModuleConfig::compilation_cache_key() const {
42   string key =
43       tensorflow::strings::StrCat("profiling=", hlo_profiling_enabled_);
44   StrAppend(&key, "::(");
45   std::vector<string> params;
46   for (const ShapeLayout& param_layout :
47        entry_computation_layout_->parameter_layouts()) {
48     params.push_back(param_layout.shape().DebugString());
49   }
50   StrAppend(&key, tensorflow::str_util::Join(params, ", "), ") => ",
51             entry_computation_layout_->result_shape().SerializeAsString());
52   if (seed() != 0) {
53     // TODO(b/32083678): force recompilation to reset global state.
54     static std::atomic<int> counter{0};
55     StrAppend(&key, "forcing recompile ", counter++);
56   }
57   if (replica_count() != 1) {
58     StrAppend(&key, "::replica_count=", replica_count());
59   }
60   StrAppend(&key, debug_options_.DebugString());
61   if (intra_op_parallelism_threads() > 0) {
62     StrAppend(&key, "::intra_op_parallelism_threads=",
63               intra_op_parallelism_threads());
64   }
65   return key;
66 }
67 
68 }  // namespace xla
69