• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/path.h"
22 #include "tensorflow/core/platform/test.h"
23 
24 namespace xla {
25 
26 namespace {
27 
28 // Writes the given literal to a file in the test temporary directory.
WriteLiteralToTempFile(const LiteralSlice & literal,const string & name)29 void WriteLiteralToTempFile(const LiteralSlice& literal, const 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   string outdir;
34   if (!tensorflow::io::GetTestUndeclaredOutputsDir(&outdir)) {
35     outdir = tensorflow::testing::TmpDir();
36   }
37 
38   auto* env = tensorflow::Env::Default();
39   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 &)51 void OnMiscompare(const LiteralSlice& expected, const LiteralSlice& actual,
52                   const LiteralSlice& mismatches,
53                   const ShapeIndex& /*shape_index*/) {
54   LOG(INFO) << "expected: " << ShapeUtil::HumanString(expected.shape()) << " "
55             << literal_comparison::ToStringTruncated(expected);
56   LOG(INFO) << "actual:   " << ShapeUtil::HumanString(actual.shape()) << " "
57             << literal_comparison::ToStringTruncated(actual);
58   LOG(INFO) << "Dumping literals to temp files...";
59   WriteLiteralToTempFile(expected, "expected");
60   WriteLiteralToTempFile(actual, "actual");
61   WriteLiteralToTempFile(mismatches, "mismatches");
62 }
63 
StatusToAssertion(const Status & s)64 ::testing::AssertionResult StatusToAssertion(const Status& s) {
65   if (s.ok()) {
66     return ::testing::AssertionSuccess();
67   }
68   return ::testing::AssertionFailure() << s.error_message();
69 }
70 
71 }  // namespace
72 
EqualShapes(const Shape & expected,const Shape & actual)73 /* static */ ::testing::AssertionResult LiteralTestUtil::EqualShapes(
74     const Shape& expected, const Shape& actual) {
75   return StatusToAssertion(literal_comparison::EqualShapes(expected, actual));
76 }
77 
EqualShapesAndLayouts(const Shape & expected,const Shape & actual)78 /* static */ ::testing::AssertionResult LiteralTestUtil::EqualShapesAndLayouts(
79     const Shape& expected, const Shape& actual) {
80   if (expected.ShortDebugString() != actual.ShortDebugString()) {
81     return ::testing::AssertionFailure()
82            << "want: " << expected.ShortDebugString()
83            << " got: " << actual.ShortDebugString();
84   }
85   return ::testing::AssertionSuccess();
86 }
87 
Equal(const LiteralSlice & expected,const LiteralSlice & actual)88 /* static */ ::testing::AssertionResult LiteralTestUtil::Equal(
89     const LiteralSlice& expected, const LiteralSlice& actual) {
90   return StatusToAssertion(literal_comparison::Equal(expected, actual));
91 }
92 
Near(const LiteralSlice & expected,const LiteralSlice & actual,const ErrorSpec & error_spec,absl::optional<bool> detailed_message)93 /* static */ ::testing::AssertionResult LiteralTestUtil::Near(
94     const LiteralSlice& expected, const LiteralSlice& actual,
95     const ErrorSpec& error_spec, absl::optional<bool> detailed_message) {
96   return StatusToAssertion(literal_comparison::Near(
97       expected, actual, error_spec, detailed_message, &OnMiscompare));
98 }
99 
NearOrEqual(const LiteralSlice & expected,const LiteralSlice & actual,const absl::optional<ErrorSpec> & error)100 /* static */ ::testing::AssertionResult LiteralTestUtil::NearOrEqual(
101     const LiteralSlice& expected, const LiteralSlice& actual,
102     const absl::optional<ErrorSpec>& error) {
103   if (error.has_value()) {
104     VLOG(1) << "Expects near";
105     return StatusToAssertion(literal_comparison::Near(
106         expected, actual, *error, /*detailed_message=*/absl::nullopt,
107         &OnMiscompare));
108   }
109   VLOG(1) << "Expects equal";
110   return StatusToAssertion(literal_comparison::Equal(expected, actual));
111 }
112 
113 }  // namespace xla
114