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_OPCODE_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_HLO_OPCODE_H_
18
19 #include <iosfwd>
20 #include <string>
21 #include "absl/types/optional.h"
22 #include "tensorflow/compiler/xla/comparison_util.h"
23 #include "tensorflow/compiler/xla/statusor.h"
24 #include "tensorflow/compiler/xla/types.h"
25 #include "tensorflow/compiler/xla/xla_data.pb.h"
26
27 namespace xla {
28
29 // High-level optimizer instruction opcodes -- these are linear-algebra level
30 // opcodes. They are a flattened form of the UnaryOp, BinaryOp, ... opcodes
31 // present in the XLA service protobuf.
32 //
33 // See the XLA documentation for the semantics of each opcode.
34 //
35 // Each entry has the format:
36 // (enum_name, opcode_name, arity)
37 // or
38 // (enum_name, opcode_name, arity, p1 | p2 | ...)
39 //
40 // with p1, p2, ... are members of HloOpcodeProperty. They are combined
41 // using bitwise-or.
42 //
43 // Note: Do not use ':' in opcode names. It is used as a special character
44 // in these places:
45 // - In extended opcode strings (HloInstruction::ExtendedOpcodeString()), to
46 // separate the opcode from the fusion kind
47 // - In fully qualified names (HloInstruction::FullyQualifiedName()), to
48 // separate the qualifiers (name of the computation and potentially the
49 // fusion instruction) from the name
50 #define HLO_OPCODE_LIST(V) \
51 V(kAbs, "abs", 1) \
52 V(kAdd, "add", 2) \
53 V(kAddDependency, "add-dependency", 2) \
54 V(kAfterAll, "after-all", kHloOpcodeIsVariadic) \
55 V(kAllReduce, "all-reduce", kHloOpcodeIsVariadic) \
56 V(kAllToAll, "all-to-all", kHloOpcodeIsVariadic) \
57 V(kAtan2, "atan2", 2) \
58 V(kBatchNormGrad, "batch-norm-grad", 5) \
59 V(kBatchNormInference, "batch-norm-inference", 5) \
60 V(kBatchNormTraining, "batch-norm-training", 3) \
61 V(kBitcast, "bitcast", 1) \
62 V(kBitcastConvert, "bitcast-convert", 1) \
63 V(kBroadcast, "broadcast", 1) \
64 V(kCall, "call", kHloOpcodeIsVariadic) \
65 V(kCeil, "ceil", 1) \
66 V(kCholesky, "cholesky", 1) \
67 V(kClamp, "clamp", 3) \
68 V(kCollectivePermute, "collective-permute", 1) \
69 V(kClz, "count-leading-zeros", 1) \
70 V(kCompare, "compare", 2) \
71 V(kComplex, "complex", 2) \
72 V(kConcatenate, "concatenate", kHloOpcodeIsVariadic) \
73 V(kConditional, "conditional", kHloOpcodeIsVariadic) \
74 V(kConstant, "constant", 0) \
75 V(kConvert, "convert", 1) \
76 V(kConvolution, "convolution", 2) \
77 V(kCopy, "copy", 1) \
78 V(kCos, "cosine", 1) \
79 V(kCustomCall, "custom-call", kHloOpcodeIsVariadic) \
80 V(kDivide, "divide", 2) \
81 V(kDomain, "domain", 1) \
82 V(kDot, "dot", 2) \
83 V(kDynamicSlice, "dynamic-slice", kHloOpcodeIsVariadic) \
84 V(kDynamicUpdateSlice, "dynamic-update-slice", kHloOpcodeIsVariadic) \
85 V(kExp, "exponential", 1) \
86 V(kExpm1, "exponential-minus-one", 1) \
87 V(kFft, "fft", 1) \
88 V(kFloor, "floor", 1) \
89 V(kFusion, "fusion", kHloOpcodeIsVariadic) \
90 V(kGather, "gather", 2) \
91 V(kGetDimensionSize, "get-dimension-size", 1) \
92 V(kGetTupleElement, "get-tuple-element", 1) \
93 V(kImag, "imag", 1) \
94 V(kInfeed, "infeed", 1) \
95 V(kIota, "iota", 0) \
96 V(kIsFinite, "is-finite", 1) \
97 V(kLog, "log", 1) \
98 V(kLog1p, "log-plus-one", 1) \
99 V(kAnd, "and", 2) \
100 V(kNot, "not", 1) \
101 V(kOr, "or", 2) \
102 V(kXor, "xor", 2) \
103 V(kMap, "map", kHloOpcodeIsVariadic) \
104 V(kMaximum, "maximum", 2) \
105 V(kMinimum, "minimum", 2) \
106 V(kMultiply, "multiply", 2) \
107 V(kNegate, "negate", 1) \
108 V(kOutfeed, "outfeed", 2) \
109 V(kPad, "pad", 2) \
110 V(kParameter, "parameter", 0) \
111 V(kPower, "power", 2) \
112 V(kReal, "real", 1) \
113 V(kRecv, "recv", 1) \
114 V(kRecvDone, "recv-done", 1) \
115 V(kReduce, "reduce", kHloOpcodeIsVariadic) \
116 V(kReducePrecision, "reduce-precision", 1) \
117 V(kReduceWindow, "reduce-window", 2) \
118 V(kRemainder, "remainder", 2) \
119 V(kReplicaId, "replica-id", 0) \
120 V(kReshape, "reshape", 1) \
121 V(kReverse, "reverse", 1) \
122 V(kRng, "rng", kHloOpcodeIsVariadic) \
123 V(kRoundNearestAfz, "round-nearest-afz", 1) \
124 V(kRsqrt, "rsqrt", 1) \
125 V(kScatter, "scatter", 3) \
126 V(kSelect, "select", 3) \
127 V(kSelectAndScatter, "select-and-scatter", 3) \
128 V(kSend, "send", 2) \
129 V(kSendDone, "send-done", 1) \
130 V(kShiftLeft, "shift-left", 2) \
131 V(kShiftRightArithmetic, "shift-right-arithmetic", 2) \
132 V(kShiftRightLogical, "shift-right-logical", 2) \
133 V(kSign, "sign", 1) \
134 V(kSin, "sine", 1) \
135 V(kSlice, "slice", 1) \
136 V(kSort, "sort", kHloOpcodeIsVariadic) \
137 V(kSqrt, "sqrt", 1) \
138 V(kSubtract, "subtract", 2) \
139 V(kTanh, "tanh", 1) \
140 V(kTrace, "trace", 1) \
141 V(kTranspose, "transpose", 1) \
142 V(kTriangularSolve, "triangular-solve", 2) \
143 V(kTuple, "tuple", kHloOpcodeIsVariadic) \
144 V(kTupleSelect, "tuple-select", 3) \
145 V(kWhile, "while", 1)
146
147 enum class HloOpcode {
148 #define DECLARE_ENUM(enum_name, opcode_name, ...) enum_name,
149 HLO_OPCODE_LIST(DECLARE_ENUM)
150 #undef DECLARE_ENUM
151 };
152
153 // Arity value that denotes that an operator is variadic.
154 enum {
155 kHloOpcodeIsVariadic = -1,
156 };
157
158 // List of properties associated with opcodes.
159 // Properties are defined as increasing powers of two, so that we can use
160 // bitwise-or to combine properties, and bitwise-and to test for them.
161 enum HloOpcodeProperty {
162 kHloOpcodeIsComparison = 1 << 0,
163 };
164
165 // Returns a string representation of the opcode.
166 string HloOpcodeString(HloOpcode opcode);
167
168 // Retrieves the opcode enum by name if the opcode exists.
169 StatusOr<HloOpcode> StringToHloOpcode(const string& opcode_name);
170
171 inline std::ostream& operator<<(std::ostream& os, HloOpcode opcode) {
172 return os << HloOpcodeString(opcode);
173 }
174
175 // Returns true iff the given opcode is a comparison operation.
176 bool HloOpcodeIsComparison(HloOpcode opcode);
177
178 // Returns true iff the given opcode has variadic operands.
179 bool HloOpcodeIsVariadic(HloOpcode opcode);
180
181 // Returns the arity of opcode. If the opcode is variadic,
182 // returns nullopt.
183 absl::optional<int> HloOpcodeArity(HloOpcode opcode);
184
185 // Returns the number of HloOpcode values.
HloOpcodeCount()186 inline const uint32_t HloOpcodeCount() {
187 #define HLO_COUNT_ONE(...) +1
188 #define HLO_XLIST_LENGTH(list) list(HLO_COUNT_ONE)
189 return HLO_XLIST_LENGTH(HLO_OPCODE_LIST);
190 }
191
192 } // namespace xla
193
194 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_OPCODE_H_
195