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/aot/runtime.h" 17 18 #include "tensorflow/core/framework/allocator.h" 19 #include "tensorflow/core/platform/test.h" 20 21 namespace tensorflow { 22 namespace tfcompile { 23 namespace runtime { 24 namespace { 25 26 TEST(Runtime, AlignmentValue) { 27 // We've chosen 32 byte alignment for the tfcompile runtime to mimic the 28 // regular tensorflow allocator, which was chosen to play nicely with Eigen. 29 // The tfcompile runtime also has a requirement that comes from the xla 30 // generated code, on the relation: buffer_size >= 16 ? 2 * sizeof(void*) : 8 31 // So any value that we choose must abide by that constraint as well. 32 EXPECT_EQ(kAlign, Allocator::kAllocatorAlignment); 33 } 34 35 TEST(Runtime, AlignedBufferBytes) { 36 EXPECT_EQ(aligned_buffer_bytes(nullptr, 0), 0); 37 38 static constexpr intptr_t sizesA[1] = {-1}; 39 EXPECT_EQ(aligned_buffer_bytes(sizesA, 1), 0); 40 41 static constexpr intptr_t sizesB[1] = {3}; 42 EXPECT_EQ(aligned_buffer_bytes(sizesB, 1), 32); 43 44 static constexpr intptr_t sizesC[1] = {32}; 45 EXPECT_EQ(aligned_buffer_bytes(sizesC, 1), 32); 46 47 static constexpr intptr_t sizesD[7] = {1, -1, 32, -1, 64, 2, 3}; 48 EXPECT_EQ(aligned_buffer_bytes(sizesD, 7), 192); 49 } 50 51 void* add_ptr(void* base, uintptr_t delta) { 52 return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(base) + delta); 53 } 54 55 // To test MallocContiguousBuffers and FreeContiguous, we just check for 56 // expected nullptrs, and write to each byte of allocated memory. We rely on 57 // the leak checker to tell us if there's an inconsistency between malloc and 58 // free. We also check the contiguous property. 59 TEST(Runtime, MallocFreeContiguousBuffers) { 60 // Test empty sizes. 61 void* base = MallocContiguousBuffers(nullptr, 0, nullptr, false); 62 EXPECT_EQ(base, nullptr); 63 FreeContiguous(base); 64 65 // Test non-empty sizes with 0 sum. 66 static constexpr intptr_t sizesA[1] = {-1}; 67 void* bufA[1]; 68 base = MallocContiguousBuffers(sizesA, 1, bufA, false); 69 EXPECT_EQ(base, nullptr); 70 EXPECT_EQ(bufA[0], nullptr); 71 FreeContiguous(base); 72 73 // Test non-empty sizes with non-0 sum. 74 static constexpr intptr_t sizesB[1] = {3}; 75 void* bufB[1]; 76 base = MallocContiguousBuffers(sizesB, 1, bufB, false); 77 EXPECT_NE(base, nullptr); 78 EXPECT_EQ(bufB[0], add_ptr(base, 0)); 79 char* bufB0_bytes = static_cast<char*>(bufB[0]); 80 bufB0_bytes[0] = 'A'; 81 bufB0_bytes[1] = 'B'; 82 bufB0_bytes[2] = 'C'; 83 FreeContiguous(base); 84 85 // Test non-empty sizes with non-0 sum, and annotate_initialized. 86 static constexpr intptr_t sizesC[1] = {3}; 87 void* bufC[1]; 88 base = MallocContiguousBuffers(sizesC, 1, bufC, true); 89 EXPECT_NE(base, nullptr); 90 EXPECT_EQ(bufC[0], add_ptr(base, 0)); 91 char* bufC0_bytes = static_cast<char*>(bufC[0]); 92 bufC0_bytes[0] = 'A'; 93 bufC0_bytes[1] = 'B'; 94 bufC0_bytes[2] = 'C'; 95 FreeContiguous(base); 96 97 // Test mixed sizes. 98 static constexpr intptr_t sizesD[7] = {1, -1, 32, -1, 64, 2, 3}; 99 void* bufD[7]; 100 base = MallocContiguousBuffers(sizesD, 7, bufD, false); 101 EXPECT_NE(base, nullptr); 102 EXPECT_EQ(bufD[0], add_ptr(base, 0)); 103 EXPECT_EQ(bufD[1], nullptr); 104 EXPECT_EQ(bufD[2], add_ptr(base, 32)); 105 EXPECT_EQ(bufD[3], nullptr); 106 EXPECT_EQ(bufD[4], add_ptr(base, 64)); 107 EXPECT_EQ(bufD[5], add_ptr(base, 128)); 108 EXPECT_EQ(bufD[6], add_ptr(base, 160)); 109 for (int i = 0; i < 7; ++i) { 110 const intptr_t size = sizesD[i]; 111 if (size != -1) { 112 char* bufD_bytes = static_cast<char*>(bufD[i]); 113 for (size_t j = 0; j < size; ++j) { 114 bufD_bytes[j] = 'A' + j; 115 } 116 } 117 } 118 FreeContiguous(base); 119 } 120 121 } // namespace 122 } // namespace runtime 123 } // namespace tfcompile 124 } // namespace tensorflow 125