1 /* Copyright 2021 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 #ifndef TENSORFLOW_COMPILER_XLA_CLIENT_VALUE_INFERENCE_H_ 16 #define TENSORFLOW_COMPILER_XLA_CLIENT_VALUE_INFERENCE_H_ 17 18 #include <optional> 19 20 #include "absl/container/flat_hash_map.h" 21 #include "tensorflow/compiler/xla/client/xla_builder.h" 22 #include "tensorflow/compiler/xla/literal.h" 23 #include "tensorflow/compiler/xla/literal_util.h" 24 #include "tensorflow/compiler/xla/service/dfs_hlo_visitor.h" 25 #include "tensorflow/compiler/xla/service/hlo_computation.h" 26 #include "tensorflow/compiler/xla/service/hlo_evaluator.h" 27 #include "tensorflow/compiler/xla/service/hlo_opcode.h" 28 #include "tensorflow/compiler/xla/util.h" 29 #include "tensorflow/compiler/xla/xla_data.pb.h" 30 31 namespace xla { 32 // OptionalLiteral is an augmented literal class which returns optional 33 // values for each index (the value can be either valid or invalid). The 34 // implementation keeps two literals, a value literal, holding both the valid 35 // and garabage value, and a masking literal representing if a value is valid or 36 // garbage. 37 class OptionalLiteral { 38 public: OptionalLiteral(Literal value,Literal mask)39 explicit OptionalLiteral(Literal value, Literal mask) 40 : value_(std::move(value)), mask_(std::move(mask)) {} 41 42 template <typename NativeT> 43 std::optional<NativeT> Get(absl::Span<const int64_t> element_index, 44 ShapeIndex shape_index = {}) const { 45 if (mask_.Get<bool>(element_index, shape_index)) { 46 return std::nullopt; 47 } else { 48 return value_.Get<NativeT>(element_index, shape_index); 49 } 50 } 51 52 // Returns true if all values in this literal slice are value. AllValid()53 bool AllValid() { return mask_.IsAll(0); } 54 55 // Get value out of this slice if all values are valid. Otherwise returns 56 // nullopt. GetValue()57 std::optional<LiteralSlice> GetValue() { 58 if (!AllValid()) { 59 return std::nullopt; 60 } 61 return LiteralSlice(value_); 62 } 63 64 private: 65 Literal value_; 66 Literal mask_; 67 }; 68 69 enum ValueInferenceMode { 70 // Inference the constant value itself. 71 kValue = 0, 72 // Inference upper-bound and lower-bound of the value. Bounds are inclusive. 73 kUpperBound, 74 kLowerBound, 75 }; 76 77 class ValueInference { 78 public: 79 // ValueInference analyzes values in XlaOp answers following questions: 80 // - What's the upper-bound of each value in a tensor. 81 // - What's the lower-bound of each value in a tensor. 82 // - What's the constant value of each tensor. 83 // - Whether or not each value in a tensor is dynamic. ValueInference(XlaBuilder * builder)84 explicit ValueInference(XlaBuilder* builder) : builder_(builder) { 85 CHECK(builder_); 86 } 87 StatusOr<Literal> AnalyzeIsDynamic(XlaOp op); 88 // Returns an OptionalLiteral. Each individual value of the literal is 89 // the concrete constant value if it can be inferred, otherwise a nullopt. 90 StatusOr<OptionalLiteral> AnalyzeConstant(XlaOp op, ValueInferenceMode mode); 91 92 // Returns underlying xla builder. builder()93 XlaBuilder* builder() { return builder_; } 94 95 private: 96 // Given an op handle, returns a simplified version of the handle inside a 97 // int64_t Literal. If the a -1 value for the handle means invalid 98 // simplification and the result shouldn't be used. 99 StatusOr<Literal> SimplifyOp(int64_t handle); 100 101 // Perform CSE on a given handle, and return an equivalent handle if seen 102 // before. Otherwise, returns nullopt. 103 StatusOr<std::optional<int64_t>> CseOpHandle(int64_t handle); 104 XlaBuilder* builder_; 105 HloEvaluator evaluator_; 106 // A map from instruction_hash to handle that helps perform CSE. 107 absl::flat_hash_map<int64_t, int64_t> cse_map_; 108 }; 109 } // namespace xla 110 111 #endif // TENSORFLOW_COMPILER_XLA_CLIENT_VALUE_INFERENCE_H_ 112