1 /* Copyright 2019 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_TESTS_VERIFIED_HLO_MODULE_H_ 16 #define TENSORFLOW_COMPILER_XLA_TESTS_VERIFIED_HLO_MODULE_H_ 17 18 #include <functional> 19 20 #include "absl/strings/string_view.h" 21 #include "tensorflow/compiler/xla/service/hlo_module.h" 22 #include "tensorflow/compiler/xla/service/hlo_module_config.h" 23 #include "tensorflow/compiler/xla/service/hlo_verifier.h" 24 #include "tensorflow/compiler/xla/shape.h" 25 #include "tensorflow/compiler/xla/types.h" 26 #include "tensorflow/core/lib/core/status.h" 27 28 namespace xla { 29 30 // An HLO module derived class which verifies itself on destruction. This class 31 // is intended to be used in unit tests. Any verification errors are raised via 32 // ADD_FAILURE. 33 class VerifiedHloModule : public HloModule { 34 public: VerifiedHloModule(const string & name,const HloModuleConfig & config,bool verifier_layout_sensitive,bool allow_mixed_precision_in_hlo_verifier,std::function<int64 (const Shape &)> shape_size_function)35 VerifiedHloModule(const string& name, const HloModuleConfig& config, 36 bool verifier_layout_sensitive, 37 bool allow_mixed_precision_in_hlo_verifier, 38 std::function<int64(const Shape&)> shape_size_function) 39 : HloModule(name, config), 40 verifier_( 41 verifier_layout_sensitive, allow_mixed_precision_in_hlo_verifier, 42 /*instruction_can_change_layout_func=*/{}, shape_size_function) {} 43 ~VerifiedHloModule()44 ~VerifiedHloModule() override { VerifyOrAddFailure("in destructor"); } 45 46 // Given a string in the HloModule::ToString() format, parses the string and 47 // builds the VerifiedHloModule in place. Before calling this method, the 48 // module must be empty (no computations). Finally verifies the module using 49 // HloVerifier and returns the status. 50 Status ParseHloStringAndVerifyModule(absl::string_view str); 51 52 // Verifies the module and flags any error with ADD_FAILURE. 'message' is 53 // included in the failure message. 54 void VerifyOrAddFailure(absl::string_view message); 55 56 private: 57 // Verifies the module using HloVerifier and returns the status. 58 Status Verify(); 59 60 HloVerifier verifier_; 61 }; 62 63 } // namespace xla 64 65 #endif // TENSORFLOW_COMPILER_XLA_TESTS_VERIFIED_HLO_MODULE_H_ 66