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