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