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