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/platform/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 std::string & name)28 void WriteLiteralToTempFile(const LiteralSlice& literal,
29 const std::string& name) {
30 // Bazel likes for tests to write "debugging outputs" like these to
31 // TEST_UNDECLARED_OUTPUTS_DIR. This plays well with tools that inspect test
32 // results, especially when they're run on remote machines.
33 std::string outdir;
34 if (!tensorflow::io::GetTestUndeclaredOutputsDir(&outdir)) {
35 outdir = tensorflow::testing::TmpDir();
36 }
37
38 auto* env = tensorflow::Env::Default();
39 std::string filename = tensorflow::io::JoinPath(
40 outdir, absl::StrFormat("tempfile-%d-%s", env->NowMicros(), name));
41 TF_CHECK_OK(tensorflow::WriteBinaryProto(env, absl::StrCat(filename, ".pb"),
42 literal.ToProto()));
43 TF_CHECK_OK(tensorflow::WriteStringToFile(env, absl::StrCat(filename, ".txt"),
44 literal.ToString()));
45 LOG(ERROR) << "wrote Literal to " << name << " file: " << filename
46 << ".{pb,txt}";
47 }
48
49 // Callback helper that dumps literals to temporary files in the event of a
50 // miscomparison.
OnMiscompare(const LiteralSlice & expected,const LiteralSlice & actual,const LiteralSlice & mismatches,const ShapeIndex &,const literal_comparison::ErrorBuckets &)51 void OnMiscompare(const LiteralSlice& expected, const LiteralSlice& actual,
52 const LiteralSlice& mismatches,
53 const ShapeIndex& /*shape_index*/,
54 const literal_comparison::ErrorBuckets& /*error_buckets*/) {
55 LOG(INFO) << "expected: " << ShapeUtil::HumanString(expected.shape()) << " "
56 << literal_comparison::ToStringTruncated(expected);
57 LOG(INFO) << "actual: " << ShapeUtil::HumanString(actual.shape()) << " "
58 << literal_comparison::ToStringTruncated(actual);
59 LOG(INFO) << "Dumping literals to temp files...";
60 WriteLiteralToTempFile(expected, "expected");
61 WriteLiteralToTempFile(actual, "actual");
62 WriteLiteralToTempFile(mismatches, "mismatches");
63 }
64
StatusToAssertion(const Status & s)65 ::testing::AssertionResult StatusToAssertion(const Status& s) {
66 if (s.ok()) {
67 return ::testing::AssertionSuccess();
68 }
69 return ::testing::AssertionFailure() << s.error_message();
70 }
71
72 } // namespace
73
EqualShapes(const Shape & expected,const Shape & actual)74 /* static */ ::testing::AssertionResult LiteralTestUtil::EqualShapes(
75 const Shape& expected, const Shape& actual) {
76 return StatusToAssertion(literal_comparison::EqualShapes(expected, actual));
77 }
78
EqualShapesAndLayouts(const Shape & expected,const Shape & actual)79 /* static */ ::testing::AssertionResult LiteralTestUtil::EqualShapesAndLayouts(
80 const Shape& expected, const Shape& actual) {
81 if (expected.ShortDebugString() != actual.ShortDebugString()) {
82 return ::testing::AssertionFailure()
83 << "want: " << expected.ShortDebugString()
84 << " got: " << actual.ShortDebugString();
85 }
86 return ::testing::AssertionSuccess();
87 }
88
Equal(const LiteralSlice & expected,const LiteralSlice & actual)89 /* static */ ::testing::AssertionResult LiteralTestUtil::Equal(
90 const LiteralSlice& expected, const LiteralSlice& actual) {
91 return StatusToAssertion(literal_comparison::Equal(expected, actual));
92 }
93
Near(const LiteralSlice & expected,const LiteralSlice & actual,const ErrorSpec & error_spec,std::optional<bool> detailed_message)94 /* static */ ::testing::AssertionResult LiteralTestUtil::Near(
95 const LiteralSlice& expected, const LiteralSlice& actual,
96 const ErrorSpec& error_spec, std::optional<bool> detailed_message) {
97 return StatusToAssertion(literal_comparison::Near(
98 expected, actual, error_spec, detailed_message, &OnMiscompare));
99 }
100
NearOrEqual(const LiteralSlice & expected,const LiteralSlice & actual,const std::optional<ErrorSpec> & error)101 /* static */ ::testing::AssertionResult LiteralTestUtil::NearOrEqual(
102 const LiteralSlice& expected, const LiteralSlice& actual,
103 const std::optional<ErrorSpec>& error) {
104 if (error.has_value()) {
105 VLOG(1) << "Expects near";
106 return StatusToAssertion(literal_comparison::Near(
107 expected, actual, *error, /*detailed_message=*/std::nullopt,
108 &OnMiscompare));
109 }
110 VLOG(1) << "Expects equal";
111 return StatusToAssertion(literal_comparison::Equal(expected, actual));
112 }
113
114 } // namespace xla
115