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