1 /* Copyright 2018 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/jit/xla_compilation_cache.h"
17
18 #include "tensorflow/compiler/jit/flags.h"
19 #include "tensorflow/compiler/tf2xla/shape_util.h"
20 #include "tensorflow/compiler/xla/client/client_library.h"
21 #include "tensorflow/core/platform/test.h"
22 #include "tensorflow/core/platform/test_benchmark.h"
23
24 namespace tensorflow {
25 namespace {
26
27 using SignatureHash = XlaCompilationCache::Signature::Hash;
28
TEST(XlaCompilationCacheTest,SignatureEquality)29 TEST(XlaCompilationCacheTest, SignatureEquality) {
30 NameAttrList fn;
31 fn.set_name("afunction");
32 std::vector<XlaCompiler::Argument> args(1);
33 args[0].kind = XlaCompiler::Argument::kConstant;
34 args[0].type = DT_INT32;
35 args[0].shape = TensorShape({4, 0});
36 args[0].constant_value = Tensor(DT_INT32, {4, 0});
37 TF_ASSERT_OK_AND_ASSIGN(XlaCompilationCache::Signature s1,
38 XlaCompilationCache::BuildSignature(fn, args));
39
40 args[0].type = DT_FLOAT;
41 args[0].constant_value = Tensor(DT_FLOAT, {4, 0});
42 TF_ASSERT_OK_AND_ASSIGN(XlaCompilationCache::Signature s2,
43 XlaCompilationCache::BuildSignature(fn, args));
44
45 args[0].shape = TensorShape({0, 4});
46 args[0].constant_value = Tensor(DT_FLOAT, {0, 4});
47 TF_ASSERT_OK_AND_ASSIGN(XlaCompilationCache::Signature s3,
48 XlaCompilationCache::BuildSignature(fn, args));
49
50 std::vector<XlaCompilationCache::Signature> signatures = {s1, s2, s3};
51 for (int i = 0; i < signatures.size(); ++i) {
52 for (int j = 0; j < signatures.size(); ++j) {
53 EXPECT_EQ(i == j, signatures[i] == signatures[j])
54 << "s1: " << signatures[i].HumanString() << "\n"
55 << "s2: " << signatures[j].HumanString();
56 EXPECT_EQ(i == j,
57 signatures[i].HumanString() == signatures[j].HumanString())
58 << "s1: " << signatures[i].HumanString() << "\n"
59 << "s2: " << signatures[j].HumanString();
60 EXPECT_EQ(i == j, SignatureHash()(signatures[i]) ==
61 SignatureHash()(signatures[j]))
62 << "s1: " << signatures[i].HumanString() << "\n"
63 << "s1_hash: " << SignatureHash()(signatures[i]) << "\n"
64 << "s2: " << signatures[j].HumanString() << "\n"
65 << "s2_hash: " << SignatureHash()(signatures[j]);
66 }
67 }
68 }
69
TEST(XlaCompilationCacheTest,SignatureUniqueness)70 TEST(XlaCompilationCacheTest, SignatureUniqueness) {
71 NameAttrList fn;
72 fn.set_name("afunction");
73 std::vector<XlaCompiler::Argument> args(2);
74 args[0].kind = XlaCompiler::Argument::kConstant;
75 args[0].type = DT_INT32;
76 args[0].constant_value = Tensor(DT_INT32, {4, 0});
77
78 args[1].kind = XlaCompiler::Argument::kParameter;
79 args[1].type = DT_INT32;
80 args[1].shape = TensorShape({4, 0});
81
82 TF_ASSERT_OK_AND_ASSIGN(XlaCompilationCache::Signature s1,
83 XlaCompilationCache::BuildSignature(fn, args));
84
85 using std::swap; // go/using-std-swap
86 swap(args[0], args[1]);
87 TF_ASSERT_OK_AND_ASSIGN(XlaCompilationCache::Signature s2,
88 XlaCompilationCache::BuildSignature(fn, args));
89
90 EXPECT_NE(s1.HumanString(), s2.HumanString());
91 EXPECT_NE(SignatureHash()(s1), SignatureHash()(s2));
92 EXPECT_FALSE(s1 == s2);
93 }
94
BM_BuildSignature(::testing::benchmark::State & state)95 void BM_BuildSignature(::testing::benchmark::State& state) {
96 const int n_args = state.range(0);
97
98 NameAttrList fn;
99 fn.set_name("afunction");
100 for (int i = 0; i < n_args; i++) {
101 (*fn.mutable_attr())[absl::StrCat("T", i)].set_type(DT_FLOAT);
102 }
103 std::vector<XlaCompiler::Argument> args(n_args);
104 for (int i = 0; i < n_args; i++) {
105 args[i].kind = (((i % 3) == 0) ? XlaCompiler::Argument::kConstant
106 : XlaCompiler::Argument::kParameter);
107 args[i].type = DT_INT32;
108 args[i].shape = TensorShape({4, 0});
109 args[i].constant_value = Tensor(DT_INT32, {4, 0});
110 }
111
112 for (auto i : state) {
113 StatusOr<XlaCompilationCache::Signature> s =
114 XlaCompilationCache::BuildSignature(fn, args);
115 CHECK(s.ok());
116 XlaCompilationCache::Signature sig = std::move(s.ValueOrDie());
117 }
118 }
119 BENCHMARK(BM_BuildSignature)->Arg(0)->Arg(1)->Arg(2)->Arg(5)->Arg(10);
120
121 } // namespace
122 } // namespace tensorflow
123