STT-tensorflow/tensorflow/compiler/xla/tests/verified_hlo_module.h
Adrian Kuegel 07aae20777 Move ParseHloString to VerifiedHloModule.
To make this possible, add HloParser interface which only allows
VerifiedHloModule to instantiate a parser and make the private HloParser
class a HloParserImpl child class.
Also use std::string instead of string in hlo_parser.cc.
Finally add the verification at the end of the parsing instead of calling
Verify() at various call sites.

PiperOrigin-RevId: 276055152
Change-Id: I647e7a14e1ff9ae0aa1ba764af8718c753226e6b
2019-10-22 06:54:21 -07:00

66 lines
2.6 KiB
C++

/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_COMPILER_XLA_TESTS_VERIFIED_HLO_MODULE_H_
#define TENSORFLOW_COMPILER_XLA_TESTS_VERIFIED_HLO_MODULE_H_
#include <functional>
#include "absl/strings/string_view.h"
#include "tensorflow/compiler/xla/service/hlo_module.h"
#include "tensorflow/compiler/xla/service/hlo_module_config.h"
#include "tensorflow/compiler/xla/service/hlo_verifier.h"
#include "tensorflow/compiler/xla/shape.h"
#include "tensorflow/compiler/xla/types.h"
#include "tensorflow/core/lib/core/status.h"
namespace xla {
// An HLO module derived class which verifies itself on destruction. This class
// is intended to be used in unit tests. Any verification errors are raised via
// ADD_FAILURE.
class VerifiedHloModule : public HloModule {
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)
: HloModule(name, config),
verifier_(
verifier_layout_sensitive, allow_mixed_precision_in_hlo_verifier,
/*instruction_can_change_layout_func=*/{}, shape_size_function) {}
~VerifiedHloModule() override { VerifyOrAddFailure("in destructor"); }
// Given a string in the HloModule::ToString() format, parses the string and
// builds the VerifiedHloModule in place. Before calling this method, the
// module must be empty (no computations). Finally verifies the module using
// HloVerifier and returns the status.
Status ParseHloStringAndVerifyModule(absl::string_view str);
// Verifies the module and flags any error with ADD_FAILURE. 'message' is
// included in the failure message.
void VerifyOrAddFailure(absl::string_view message);
private:
// Verifies the module using HloVerifier and returns the status.
Status Verify();
HloVerifier verifier_;
};
} // namespace xla
#endif // TENSORFLOW_COMPILER_XLA_TESTS_VERIFIED_HLO_MODULE_H_