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/tests/literal_test_util.h"
17
18 #include "absl/strings/str_format.h"
19 #include "tensorflow/compiler/xla/literal_comparison.h"
20 #include "tensorflow/core/lib/io/path.h"
21 #include "tensorflow/core/platform/test.h"
22
23 namespace xla {
24
25 namespace {
26
27 // Writes the given literal to a file in the test temporary directory.
WriteLiteralToTempFile(const LiteralSlice & literal,const string & name)28 void WriteLiteralToTempFile(const LiteralSlice& literal, const string& name) {
29 auto get_hostname = [] {
30 char hostname[1024];
31 gethostname(hostname, sizeof hostname);
32 hostname[sizeof hostname - 1] = 0;
33 return string(hostname);
34 };
35 int64 now_usec = tensorflow::Env::Default()->NowMicros();
36 string filename = tensorflow::io::JoinPath(
37 tensorflow::testing::TmpDir(),
38 absl::StrFormat("tempfile-%s-%x-%s", get_hostname(), now_usec, name));
39 TF_CHECK_OK(tensorflow::WriteBinaryProto(tensorflow::Env::Default(), filename,
40 literal.ToProto()));
41 LOG(ERROR) << "wrote to " << name << " file: " << filename;
42 }
43
44 // Callback helper that dumps literals to temporary files in the event of a
45 // miscomparison.
OnMiscompare(const LiteralSlice & expected,const LiteralSlice & actual,const LiteralSlice & mismatches)46 void OnMiscompare(const LiteralSlice& expected, const LiteralSlice& actual,
47 const LiteralSlice& mismatches) {
48 LOG(INFO) << "expected: " << ShapeUtil::HumanString(expected.shape()) << " "
49 << literal_comparison::ToStringTruncated(expected);
50 LOG(INFO) << "actual: " << ShapeUtil::HumanString(actual.shape()) << " "
51 << literal_comparison::ToStringTruncated(actual);
52 LOG(INFO) << "Dumping literals to temp files...";
53 WriteLiteralToTempFile(expected, "expected");
54 WriteLiteralToTempFile(actual, "actual");
55 WriteLiteralToTempFile(mismatches, "mismatches");
56 }
57
StatusToAssertion(const Status & s)58 ::testing::AssertionResult StatusToAssertion(const Status& s) {
59 if (s.ok()) {
60 return ::testing::AssertionSuccess();
61 }
62 return ::testing::AssertionFailure() << s.error_message();
63 }
64
65 } // namespace
66
EqualShapes(const Shape & expected,const Shape & actual)67 /* static */ ::testing::AssertionResult LiteralTestUtil::EqualShapes(
68 const Shape& expected, const Shape& actual) {
69 return StatusToAssertion(literal_comparison::EqualShapes(expected, actual));
70 }
71
EqualShapesAndLayouts(const Shape & expected,const Shape & actual)72 /* static */ ::testing::AssertionResult LiteralTestUtil::EqualShapesAndLayouts(
73 const Shape& expected, const Shape& actual) {
74 if (expected.ShortDebugString() != actual.ShortDebugString()) {
75 return ::testing::AssertionFailure()
76 << "want: " << expected.ShortDebugString()
77 << " got: " << actual.ShortDebugString();
78 }
79 return ::testing::AssertionSuccess();
80 }
81
Equal(const LiteralSlice & expected,const LiteralSlice & actual)82 /* static */ ::testing::AssertionResult LiteralTestUtil::Equal(
83 const LiteralSlice& expected, const LiteralSlice& actual) {
84 return StatusToAssertion(literal_comparison::Equal(expected, actual));
85 }
86
Near(const LiteralSlice & expected,const LiteralSlice & actual,const ErrorSpec & error_spec,absl::optional<bool> detailed_message)87 /* static */ ::testing::AssertionResult LiteralTestUtil::Near(
88 const LiteralSlice& expected, const LiteralSlice& actual,
89 const ErrorSpec& error_spec, absl::optional<bool> detailed_message) {
90 return StatusToAssertion(literal_comparison::Near(
91 expected, actual, error_spec, detailed_message, &OnMiscompare));
92 }
93
NearOrEqual(const LiteralSlice & expected,const LiteralSlice & actual,const absl::optional<ErrorSpec> & error)94 /* static */ ::testing::AssertionResult LiteralTestUtil::NearOrEqual(
95 const LiteralSlice& expected, const LiteralSlice& actual,
96 const absl::optional<ErrorSpec>& error) {
97 if (error.has_value()) {
98 VLOG(1) << "Expects near";
99 return StatusToAssertion(literal_comparison::Near(
100 expected, actual, *error, /*detailed_message=*/absl::nullopt,
101 &OnMiscompare));
102 }
103 VLOG(1) << "Expects equal";
104 return StatusToAssertion(literal_comparison::Equal(expected, actual));
105 }
106
107 } // namespace xla
108