• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 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/core/kernels/eigen_attention.h"
17 #include "tensorflow/core/platform/test.h"
18 
19 namespace Eigen {
20 
21 namespace {
EigenApprox(float a,float b)22 void EigenApprox(float a, float b) {
23   ASSERT_TRUE(std::abs(a - b) <= std::min(std::abs(a), std::abs(b)) * 1e-3);
24 }
25 }  // namespace
26 
TEST(EigenAttentionTest,Simple)27 TEST(EigenAttentionTest, Simple) {
28   const ptrdiff_t depth = 3;
29   const ptrdiff_t batch = 10;
30   const ptrdiff_t rows = 32;
31   const ptrdiff_t cols = 48;
32   const ptrdiff_t glimpse_rows = 8;
33   const ptrdiff_t glimpse_cols = 6;
34 
35   Tensor<float, 4> input(depth, rows, cols, batch);
36   input.setRandom();
37 
38   std::vector<IndexPair<float>> offsets;
39   offsets.resize(batch);
40   for (int i = 0; i < batch; ++i) {
41     offsets[i].first = (-5 + i) / 10.0f;
42     offsets[i].second = (5 - i) / 10.0f;
43   }
44 
45   Tensor<float, 4> result(depth, glimpse_rows, glimpse_cols, batch);
46   result = ExtractGlimpses(input, glimpse_rows, glimpse_cols, offsets);
47 
48   for (int b = 0; b < batch; ++b) {
49     for (int c = 0; c < glimpse_cols; ++c) {
50       ptrdiff_t source_c =
51           c + ((1.0f + offsets[b].second) * cols - glimpse_cols) / 2;
52       for (int r = 0; r < glimpse_rows; ++r) {
53         ptrdiff_t source_r =
54             r + ((1.0f + offsets[b].first) * rows - glimpse_rows) / 2;
55         for (int d = 0; d < depth; ++d) {
56           EigenApprox(result(d, r, c, b), input(d, source_r, source_c, b));
57         }
58       }
59     }
60   }
61 }
62 
TEST(EigenAttentionTest,OutOfBoundsGlimpse)63 TEST(EigenAttentionTest, OutOfBoundsGlimpse) {
64   const ptrdiff_t depth = 3;
65   const ptrdiff_t batch = 10;
66   const ptrdiff_t rows = 32;
67   const ptrdiff_t cols = 48;
68   const ptrdiff_t glimpse_rows = 8;
69   const ptrdiff_t glimpse_cols = 6;
70 
71   Tensor<float, 4> input(depth, rows, cols, batch);
72   input.setRandom();
73 
74   std::vector<IndexPair<float>> offsets;
75   offsets.resize(batch);
76   for (int i = 0; i < batch; ++i) {
77     offsets[i].first = (-5 + i) / 2.0f;
78     offsets[i].second = (5 - i) / 2.0f;
79   }
80 
81   Tensor<float, 4> result(depth, glimpse_rows, glimpse_cols, batch);
82   result = ExtractGlimpses(input, glimpse_rows, glimpse_cols, offsets);
83 
84   for (int b = 0; b < batch; ++b) {
85     for (int c = 0; c < glimpse_cols; ++c) {
86       ptrdiff_t source_c =
87           c + ((1.0f + offsets[b].second) * cols - glimpse_cols) / 2;
88       if (source_c < glimpse_cols / 2 || source_c >= cols - glimpse_cols / 2) {
89         continue;
90       }
91       for (int r = 0; r < glimpse_rows; ++r) {
92         ptrdiff_t source_r =
93             r + ((1.0f + offsets[b].first) * rows - glimpse_rows) / 2;
94         if (source_r < glimpse_rows / 2 ||
95             source_r >= rows - glimpse_rows / 2) {
96           continue;
97         }
98         for (int d = 0; d < depth; ++d) {
99           EigenApprox(result(d, r, c, b), input(d, source_r, source_c, b));
100         }
101       }
102     }
103   }
104 }
105 
106 }  // namespace Eigen
107