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/cpu/cpu_options.h"
17
18 #include "absl/strings/numbers.h"
19 #include "absl/strings/str_split.h"
20 #include "tensorflow/core/lib/strings/numbers.h"
21
22 namespace {
23
24 const char* const kXlaOptimizeForSizeCpuOption = "xla_cpu_optimize_for_size";
25 const char* const kLlvmIrDotTilingFactor = "xla_llvm_dot_tiling_factor";
26 const char* const kXlaForceEnableExperimentalLlvmIrGemm =
27 "xla_force_enable_experimental_llvm_ir_gemm";
28 const char* const kLlvmIrGemmTileSize = "xla_llvm_ir_gemm_tile_size";
29
30 } // namespace
31
32 namespace xla {
33 namespace cpu {
34 namespace options {
35
OptimizeForSizeRequested(const HloModuleConfig & config)36 bool OptimizeForSizeRequested(const HloModuleConfig& config) {
37 const auto& extra_options_map =
38 config.debug_options().xla_backend_extra_options();
39 return extra_options_map.count(kXlaOptimizeForSizeCpuOption) > 0;
40 }
41
VectorizedReduceDisabled(const HloModuleConfig & config)42 bool VectorizedReduceDisabled(const HloModuleConfig& config) {
43 const auto& extra_options_map =
44 config.debug_options().xla_backend_extra_options();
45 return extra_options_map.count(kXlaOptimizeForSizeCpuOption) > 0;
46 }
47
LlvmIrGemvTilingFactor(const HloModuleConfig & config)48 absl::optional<int64> LlvmIrGemvTilingFactor(const HloModuleConfig& config) {
49 const auto& extra_options_map =
50 config.debug_options().xla_backend_extra_options();
51 auto it = extra_options_map.find(kLlvmIrDotTilingFactor);
52 int64 tiling_factor;
53 if (it != extra_options_map.end() &&
54 absl::SimpleAtoi(it->second, &tiling_factor)) {
55 return tiling_factor;
56 }
57 return absl::nullopt;
58 }
59
ForceEnableExperimentalLlvmIrGemm(const HloModuleConfig & config)60 bool ForceEnableExperimentalLlvmIrGemm(const HloModuleConfig& config) {
61 const auto& extra_options_map =
62 config.debug_options().xla_backend_extra_options();
63 return extra_options_map.count(kXlaForceEnableExperimentalLlvmIrGemm) > 0;
64 }
65
RemoveSuffix(absl::string_view str,absl::string_view suffix)66 static absl::string_view RemoveSuffix(absl::string_view str,
67 absl::string_view suffix) {
68 CHECK_GE(str.size(), suffix.size());
69 CHECK_EQ(str.substr(str.size() - suffix.size()), suffix);
70 return str.substr(0, str.size() - suffix.size());
71 }
72
LlvmIrGemmTileSize(const HloModuleConfig & config)73 absl::optional<std::tuple<int64, int64, int64>> LlvmIrGemmTileSize(
74 const HloModuleConfig& config) {
75 const auto& extra_options_map =
76 config.debug_options().xla_backend_extra_options();
77 auto it = extra_options_map.find(kLlvmIrGemmTileSize);
78 if (it == extra_options_map.end()) {
79 return absl::nullopt;
80 }
81
82 std::vector<string> tile_components = absl::StrSplit(it->second, ':');
83 CHECK_EQ(tile_components.size(), 3);
84
85 int64 tile_size_m;
86 int64 tile_size_k;
87 int64 tile_size_n_in_vector_width;
88
89 CHECK(absl::SimpleAtoi(tile_components[0], &tile_size_m));
90 CHECK(absl::SimpleAtoi(tile_components[1], &tile_size_k));
91
92 absl::string_view tile_size_n_in_vector_width_str =
93 RemoveSuffix(tile_components[2], "*vectwidth");
94
95 CHECK(absl::SimpleAtoi(tile_size_n_in_vector_width_str,
96 &tile_size_n_in_vector_width));
97
98 return std::tuple<int64, int64, int64>(tile_size_m, tile_size_k,
99 tile_size_n_in_vector_width);
100 }
101
102 } // namespace options
103 } // namespace cpu
104 } // namespace xla
105