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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_HLO_PARSER_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_HLO_PARSER_H_ 18 19 #include <memory> 20 #include <vector> 21 22 #include "absl/strings/string_view.h" 23 #include "tensorflow/compiler/xla/service/hlo_computation.h" 24 #include "tensorflow/compiler/xla/service/hlo_instruction.h" 25 #include "tensorflow/compiler/xla/service/hlo_lexer.h" 26 #include "tensorflow/compiler/xla/service/hlo_module.h" 27 #include "tensorflow/compiler/xla/statusor.h" 28 #include "tensorflow/compiler/xla/xla_data.pb.h" 29 30 namespace xla { 31 32 // For details about the syntax accepted by this parser, see 33 // g3doc/hlo_parser.md. 34 35 // Given a string in the HloModule::ToString() format, parses the string and 36 // creates a HloModule with the given config. 37 // Note: Tests derived from HloTestBase should use 38 // ParseAndReturnVerifiedModule() instead! 39 StatusOr<std::unique_ptr<HloModule>> ParseAndReturnUnverifiedModule( 40 absl::string_view str, const HloModuleConfig& config); 41 42 // Given a string in the HloModule::ToString() format, parses the string and 43 // creates a HloModule with default config. 44 // Note: Tests derived from HloTestBase should use 45 // ParseAndReturnVerifiedModule() instead! 46 StatusOr<std::unique_ptr<HloModule>> ParseAndReturnUnverifiedModule( 47 absl::string_view str); 48 49 // Parses sharding from str. str is supposed to contain the body of the 50 // sharding, i.e. just the rhs of the "sharding={...}" attribute string, e.g., 51 // "{replicated}". 52 StatusOr<HloSharding> ParseSharding(absl::string_view str); 53 54 // Parses frontend attributes from str. str is supposed to contain the body of 55 // the frontend attributes , i.e. just the rhs of the 56 // "frontend_attributes={...}" attribute string, e.g., 57 // "{attr_a=a,attr_b=b}". 58 StatusOr<FrontendAttributes> ParseFrontendAttributes(absl::string_view str); 59 60 // Parses parameter replication from str. str is supposed to contain the body of 61 // the parameter replication, i.e. just the rhs of the 62 // "parameter_replication={...}" attribute string, e.g., "{true, false}". 63 StatusOr<std::vector<bool>> ParseParameterReplication(absl::string_view str); 64 65 // Parses the result of window_util::ToString(const Window&). 66 StatusOr<Window> ParseWindow(absl::string_view str); 67 68 // Parses the result of ConvolutionDimensionNumbersToString(), e.g. 69 // "b0f_0io->b0f". 70 StatusOr<ConvolutionDimensionNumbers> ParseConvolutionDimensionNumbers( 71 absl::string_view str); 72 73 // Parses the result of PaddingConfigToString(), e.g. "0_0x1_1". 74 StatusOr<PaddingConfig> ParsePaddingConfig(absl::string_view str); 75 76 // Parses and returns a Shape::ToString-format string. 77 StatusOr<Shape> ParseShape(absl::string_view str); 78 79 // Parses and returns a std::vector<ReplicaGroup> from str. str is supposed to 80 // contain a list of the replica groups, i.e. just the rhs of the 81 // "replica_groups={...}" attribute string, e.g., "{{0,1}, {2,3}}". 82 StatusOr<std::vector<ReplicaGroup>> ParseReplicaGroupsOnly( 83 absl::string_view str); 84 85 class HloParser { 86 public: 87 // Runs the parser and constructs the resulting HLO in the given (empty) 88 // HloModule. Returns the error status in case an error occurred. 89 virtual Status Run(HloModule* module) = 0; ~HloParser()90 virtual ~HloParser() {} 91 92 private: 93 static std::unique_ptr<HloParser> CreateHloParserForTests( 94 absl::string_view str); 95 friend class VerifiedHloModule; 96 }; 97 98 } // namespace xla 99 100 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_PARSER_H_ 101