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 #include "tensorflow/compiler/xla/client/lib/arithmetic.h" 17 18 #include <string> 19 20 #include "tensorflow/compiler/xla/client/computation_builder.h" 21 #include "tensorflow/compiler/xla/shape_util.h" 22 #include "tensorflow/compiler/xla/status_macros.h" 23 #include "tensorflow/compiler/xla/types.h" 24 #include "tensorflow/compiler/xla/xla_data.pb.h" 25 #include "tensorflow/core/lib/strings/strcat.h" 26 27 namespace xla { 28 namespace { 29 using InstructionGenerator = 30 ComputationDataHandle (*)(ComputationBuilder*, const ComputationDataHandle&, 31 const ComputationDataHandle&); 32 33 Computation CreateScalarComputation(const string& name, PrimitiveType type, 34 ComputationBuilder* builder, 35 InstructionGenerator generator) { 36 std::unique_ptr<ComputationBuilder> b; 37 if (type == PRED) { 38 b = builder->CreateSubBuilder(name); 39 } else { 40 b = builder->CreateSubBuilder( 41 tensorflow::strings::StrCat(name, "_", PrimitiveType_Name(type))); 42 } 43 44 const Shape scalar = ShapeUtil::MakeShape(type, {}); 45 auto lhs = b->Parameter(0, scalar, "lhs"); 46 auto rhs = b->Parameter(1, scalar, "rhs"); 47 generator(b.get(), lhs, rhs); 48 return b->BuildAndNoteError(); 49 } 50 } // namespace 51 52 Computation CreateScalarAddComputation(PrimitiveType type, 53 ComputationBuilder* builder) { 54 return CreateScalarComputation( 55 "add", type, builder, 56 [](ComputationBuilder* b, const ComputationDataHandle& lhs, 57 const ComputationDataHandle& rhs) { return b->Add(lhs, rhs); }); 58 } 59 60 Computation CreateScalarMultiplyComputation(PrimitiveType type, 61 ComputationBuilder* builder) { 62 return CreateScalarComputation( 63 "add", type, builder, 64 [](ComputationBuilder* b, const ComputationDataHandle& lhs, 65 const ComputationDataHandle& rhs) { return b->Mul(lhs, rhs); }); 66 } 67 68 Computation CreateScalarGeComputation(PrimitiveType type, 69 ComputationBuilder* builder) { 70 return CreateScalarComputation( 71 "ge", type, builder, 72 [](ComputationBuilder* b, const ComputationDataHandle& lhs, 73 const ComputationDataHandle& rhs) { return b->Ge(lhs, rhs); }); 74 } 75 76 Computation CreateScalarMaxComputation(PrimitiveType type, 77 ComputationBuilder* builder) { 78 return CreateScalarComputation( 79 "max", type, builder, 80 [](ComputationBuilder* b, const ComputationDataHandle& lhs, 81 const ComputationDataHandle& rhs) { return b->Max(lhs, rhs); }); 82 } 83 84 Computation CreateScalarMinComputation(PrimitiveType type, 85 ComputationBuilder* builder) { 86 return CreateScalarComputation( 87 "min", type, builder, 88 [](ComputationBuilder* b, const ComputationDataHandle& lhs, 89 const ComputationDataHandle& rhs) { return b->Min(lhs, rhs); }); 90 } 91 92 Computation CreateScalarAndComputation(ComputationBuilder* builder) { 93 return CreateScalarComputation( 94 "and", PRED, builder, 95 [](ComputationBuilder* b, const ComputationDataHandle& lhs, 96 const ComputationDataHandle& rhs) { return b->And(lhs, rhs); }); 97 } 98 99 Computation CreateScalarOrComputation(ComputationBuilder* builder) { 100 return CreateScalarComputation( 101 "or", PRED, builder, 102 [](ComputationBuilder* b, const ComputationDataHandle& lhs, 103 const ComputationDataHandle& rhs) { return b->Or(lhs, rhs); }); 104 } 105 106 StatusOr<ComputationDataHandle> Any(const ComputationDataHandle& predicates, 107 ComputationBuilder* builder) { 108 auto f = builder->ConstantR0<bool>(false); 109 Computation logical_or = CreateScalarOrComputation(builder); 110 TF_ASSIGN_OR_RETURN(std::unique_ptr<Shape> predicates_shape, 111 builder->GetShape(predicates)); 112 std::vector<int64> all_dimensions(ShapeUtil::Rank(*predicates_shape)); 113 std::iota(all_dimensions.begin(), all_dimensions.end(), 0); 114 return builder->Reduce(predicates, f, logical_or, all_dimensions); 115 } 116 117 } // namespace xla 118