1 /* Copyright 2019 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 #ifndef TENSORFLOW_COMPILER_XLA_TOOLS_HLO_MODULE_LOADER_H_ 17 #define TENSORFLOW_COMPILER_XLA_TOOLS_HLO_MODULE_LOADER_H_ 18 19 #include <functional> 20 #include <memory> 21 #include <string> 22 23 #include "tensorflow/compiler/xla/service/hlo_module.h" 24 #include "tensorflow/compiler/xla/statusor.h" 25 26 namespace xla { 27 namespace hlo_module_loader_details { 28 29 struct Config { ConfigConfig30 Config() {} 31 int64_t num_replicas = 1; 32 int64_t num_partitions = 1; 33 }; 34 35 } // namespace hlo_module_loader_details 36 37 // Given a string composed by multiple lines, strip the log headers, if present 38 // at the beginning of each line. 39 std::string StripLogHeaders(const std::string& hlo_string); 40 41 // Loads an HLO module from a string. 42 // The data can have the followings formats: 43 // 1) A binary of text proto file, the proto should be in xla.HloProto type. It 44 // can be a binary proto (format must be "pb"), or a text proto (format must 45 // be "pbtxt"). 46 // 2) A hlo text dump, the string should be in HloModule::ToString() format 47 // (format must be "txt" or "hlo"). The input data can also contain log 48 // headers, which will be stripped. 49 // The ovr_config data can be used to override certain fields of the 50 // HloModuleConfig. 51 // The HloModuleConfig is passed to config_modifier_hook for custom 52 // modifications before use. 53 StatusOr<std::unique_ptr<HloModule>> LoadModuleFromData( 54 const std::string& data, const std::string& format, 55 hlo_module_loader_details::Config ovr_config = 56 hlo_module_loader_details::Config(), 57 const std::function<void(HloModuleConfig*)>& config_modifier_hook = {}); 58 59 // Loads an HLO module from file. 60 // The file can be one of the followings: 61 // 1) A binary of text proto file, the proto should be in xla.HloProto type. It 62 // can be a binary proto (with .pb extension), or a text proto (with a .pbtxt 63 // extension). 64 // 2) A hlo text dump, the string should be in HloModule::ToString() format 65 // (with a .hlo or .txt extension). A text file can also contain log headers, 66 // which will be stripped. 67 // If the format is specified (not empty), it overrides the one guessed from the 68 // file extension. The ovr_config data can be used to override certain fields of 69 // the HloModuleConfig. 70 // The HloModuleConfig is passed to config_modifier_hook for custom 71 // modifications before use. 72 StatusOr<std::unique_ptr<HloModule>> LoadModuleFromFile( 73 const std::string& path, 74 hlo_module_loader_details::Config ovr_config = 75 hlo_module_loader_details::Config(), 76 std::string format = "", 77 const std::function<void(HloModuleConfig*)>& config_modifier_hook = {}); 78 79 } // namespace xla 80 81 #endif // TENSORFLOW_COMPILER_XLA_TOOLS_HLO_MODULE_LOADER_H_ 82