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_ALGEBRAIC_SIMPLIFIER_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_ALGEBRAIC_SIMPLIFIER_H_ 18 19 #include <utility> 20 21 #include "tensorflow/compiler/xla/service/hlo_module.h" 22 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h" 23 24 namespace xla { 25 26 // A pass which performs AlgebraicSimplications. 27 class AlgebraicSimplifier : public HloPassInterface { 28 public: 29 // Given shapes 'from_shape' and 'to_shape', determines if it is valid to 30 // bitcast from 'from_shape' to 'to_shape' after considering platform 31 // dependent effects on layout like alignment restrictions. Precondition: the 32 // two shapes have layouts, the same number of elements and 33 // ShapeUtil::ReshapeIsBitcast returns true. 34 using ValidBitcastCallback = 35 std::function<bool(const Shape& from_shape, const Shape& to_shape)>; 36 37 // If is_layout_sensitive is true, then the simplifier preserves layout during 38 // transformation. Otherwise, layout is ignored. If valid_bitcast_callback 39 // returns true, then the pass will replace reshapes and transposes with 40 // bitcasts. 41 AlgebraicSimplifier(bool is_layout_sensitive, 42 ValidBitcastCallback valid_bitcast_callback, 43 bool enable_dot_strength_reduction = true, 44 bool enable_conv_simplification = true) 45 : is_layout_sensitive_(is_layout_sensitive), 46 valid_bitcast_callback_(std::move(valid_bitcast_callback)), 47 enable_dot_strength_reduction_(enable_dot_strength_reduction), 48 enable_conv_simplification_(enable_conv_simplification) {} 49 ~AlgebraicSimplifier() override = default; 50 tensorflow::StringPiece name() const override { return "algsimp"; } 51 52 // Run algebraic simplification on the given computation. Returns whether the 53 // computation was changed. 54 StatusOr<bool> Run(HloModule* module) override; 55 56 private: 57 bool is_layout_sensitive_; 58 ValidBitcastCallback valid_bitcast_callback_; 59 60 // Enable dot simplication on platforms where it is profitable. 61 bool enable_dot_strength_reduction_; 62 63 // Enable convolution simplication on platforms where it is profitable. 64 bool enable_conv_simplification_; 65 }; 66 67 } // namespace xla 68 69 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_ALGEBRAIC_SIMPLIFIER_H_ 70