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