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_CLIENT_LIB_ARITHMETIC_H_ 17 #define TENSORFLOW_COMPILER_XLA_CLIENT_LIB_ARITHMETIC_H_ 18 19 #include <memory> 20 21 #include "tensorflow/compiler/xla/client/xla_builder.h" 22 #include "tensorflow/compiler/xla/client/xla_computation.h" 23 #include "tensorflow/compiler/xla/xla_data.pb.h" 24 25 namespace xla { 26 27 using XlaOpGenerator = std::function<XlaOp(XlaOp, XlaOp)>; 28 29 // Creates a scalar computation based on a lambda and returns it. 30 XlaComputation CreateScalarComputation(const std::string& name, 31 PrimitiveType type, XlaBuilder* builder, 32 XlaOpGenerator generator); 33 34 // Creates a scalar add computation and returns it. 35 XlaComputation CreateScalarAddComputation(PrimitiveType type, 36 XlaBuilder* builder); 37 38 // Creates a scalar multiply computation and returns it. 39 XlaComputation CreateScalarMultiplyComputation(PrimitiveType type, 40 XlaBuilder* builder); 41 42 // Creates a scalar ge computation and returns it. 43 XlaComputation CreateScalarGeComputation(PrimitiveType type, 44 XlaBuilder* builder); 45 46 // Creates a scalar max computation and returns it. 47 XlaComputation CreateScalarMaxComputation(PrimitiveType type, 48 XlaBuilder* builder); 49 50 // Creates a scalar min computation and returns it. 51 XlaComputation CreateScalarMinComputation(PrimitiveType type, 52 XlaBuilder* builder); 53 54 // Creates a scalar logical AND computation and returns it. 55 XlaComputation CreateScalarAndComputation(PrimitiveType type, 56 XlaBuilder* builder); 57 58 // Creates a scalar logical OR computation and returns it. 59 XlaComputation CreateScalarOrComputation(PrimitiveType type, 60 XlaBuilder* builder); 61 62 // This is to be used for general purpose "identity" like reductions with zero 63 // for any type (ie. boolean operations for PRED and Add for real numbers). 64 // As an example, this operation can be used for a situation of: 65 // x_type = type(x) 66 // op = CreateScalarIdentityWithZeroComputation(x_type) 67 // ASSERT_TRUE(op(x, 0) == x) 68 // 69 // This functionality is used for operations that are similar to a slice, 70 // gather, or broadcast, but are created through a reduction. 71 XlaComputation CreateScalarIdentityWithZeroComputation(PrimitiveType type, 72 XlaBuilder* builder); 73 74 // Returns whether any predicate in "predicates" is set. 75 // 76 // Note: if predicates is zero-sized, Any() vacuously returns false. 77 XlaOp Any(XlaOp predicates); 78 79 // Returns the argmax of `input` along `axis`. `output_type` is the type to 80 // use for the output. In case of ties always prefers smaller index. 81 XlaOp ArgMax(XlaOp input, PrimitiveType output_type, int axis); 82 83 // Returns the argmin of `input` along `axis`. `output_type` is the type to 84 // use for the output. In case of ties always prefers smaller index. 85 XlaOp ArgMin(XlaOp input, PrimitiveType output_type, int axis); 86 87 // Dispatch to ArgMin or ArgMax above, depending on bool. 88 XlaOp ArgMinMax(XlaOp input, PrimitiveType output_type, int axis, bool is_min); 89 90 } // namespace xla 91 92 #endif // TENSORFLOW_COMPILER_XLA_CLIENT_LIB_ARITHMETIC_H_ 93