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 <memory> 17 #include <unordered_map> 18 19 #include "tensorflow/core/common_runtime/pending_counts.h" 20 #include "tensorflow/core/platform/test.h" 21 22 namespace tensorflow { 23 24 TEST(PendingCounts, Simple) { 25 const int C = 300; 26 PendingCounts::Layout layout; 27 std::vector<PendingCounts::Handle> h(C); 28 for (int id = 0; id < C; id++) { 29 h[id] = layout.CreateHandle(id, id); 30 } 31 32 PendingCounts c(layout); 33 for (int id = 0; id < C; id++) { 34 c.set_initial_count(h[id], id); 35 } 36 for (int id = 0; id < C; id++) { 37 EXPECT_EQ(c.pending(h[id]), id); 38 EXPECT_EQ(c.dead_count(h[id]), 0); 39 } 40 41 for (int id = 0; id < C; id++) { 42 c.increment_dead_count(h[id]); 43 // The dead count is no longer updated once pending is 0. 44 EXPECT_EQ(c.dead_count(h[id]), (id == 0) ? 0 : 1); 45 } 46 47 EXPECT_EQ(c.decrement_pending(h[1], 1), 0); 48 EXPECT_EQ(c.decrement_pending(h[3], 1), 2); 49 EXPECT_EQ(c.decrement_pending(h[3], 1), 1); 50 c.decrement_pending(h[5], 1); 51 c.decrement_pending(h[5], 3); 52 c.decrement_pending(h[170], 1); 53 c.decrement_pending(h[170], 13); 54 EXPECT_EQ(c.pending(h[1]), 0); 55 EXPECT_EQ(c.pending(h[3]), 1); 56 EXPECT_EQ(c.pending(h[5]), 1); 57 EXPECT_EQ(c.pending(h[170]), 156); 58 } 59 60 TEST(PendingCounts, CopyConstructor) { 61 const int C = 300; 62 PendingCounts::Layout layout; 63 std::vector<PendingCounts::Handle> h(C); 64 for (int id = 0; id < C; id++) { 65 h[id] = layout.CreateHandle(id, id); 66 } 67 PendingCounts c(layout); 68 for (int id = 0; id < C; id++) { 69 c.set_initial_count(h[id], id); 70 } 71 PendingCounts c2(c); 72 for (int id = 0; id < C; id++) { 73 EXPECT_EQ(c.pending(h[id]), c2.pending(h[id])); 74 EXPECT_EQ(c.dead_count(h[id]), c2.dead_count(h[id])); 75 } 76 } 77 78 TEST(PendingCounts, MarkLiveShowsUpAsCount) { 79 PendingCounts::Layout layout; 80 PendingCounts::Handle handles[2]; 81 handles[0] = layout.CreateHandle(5, 4); 82 handles[1] = layout.CreateHandle(15, 4); 83 for (int id = 0; id < 2; id++) { 84 PendingCounts::Handle h = handles[id]; 85 // Test for both packed and large. 86 int count = (id == 0) ? 5 : 15; 87 88 PendingCounts c(layout); 89 c.set_initial_count(h, count); 90 EXPECT_EQ(c.pending(h), count); 91 c.mark_live(h); 92 EXPECT_EQ(c.pending(h), count - 1); 93 // mark_live should be idempotent 94 c.mark_live(h); 95 EXPECT_EQ(c.pending(h), count - 1); 96 97 c.decrement_pending(h, count - 1); 98 EXPECT_EQ(c.pending(h), 0); 99 100 // mark_live should be idempotent 101 c.mark_live(h); 102 EXPECT_EQ(c.pending(h), 0); 103 c.mark_started(h); 104 c.mark_live(h); 105 EXPECT_EQ(c.pending(h), 0); 106 c.mark_completed(h); 107 c.mark_live(h); 108 EXPECT_EQ(c.pending(h), 0); 109 } 110 } 111 112 TEST(PendingCounts, StateIsCorrect) { 113 const int C = 20; 114 PendingCounts::Layout layout; 115 std::vector<PendingCounts::Handle> handles(C); 116 for (int id = 0; id < C; id++) { 117 handles[id] = layout.CreateHandle(id, id); 118 } 119 PendingCounts c(layout); 120 for (int id = 0; id < C; id++) { 121 c.set_initial_count(handles[id], id); 122 } 123 124 for (int id = 0; id < C; id++) { 125 PendingCounts::Handle h = handles[id]; 126 while (c.pending(h) > 0) { 127 EXPECT_EQ(c.node_state(h), PendingCounts::PENDING_NOTREADY); 128 c.decrement_pending(h, 1); 129 } 130 EXPECT_EQ(c.node_state(h), PendingCounts::PENDING_READY); 131 c.mark_started(h); 132 EXPECT_EQ(c.node_state(h), PendingCounts::STARTED); 133 c.mark_completed(h); 134 EXPECT_EQ(c.node_state(h), PendingCounts::COMPLETED); 135 } 136 } 137 138 TEST(PendingCounts, AdjustForActivation) { 139 PendingCounts::Layout layout; 140 PendingCounts::Handle handles[2]; 141 handles[0] = layout.CreateHandle(5, 4); 142 handles[1] = layout.CreateHandle(15, 4); 143 for (int id = 0; id < 2; id++) { 144 PendingCounts::Handle h = handles[id]; 145 // Test for both packed and large. 146 int count = (id == 0) ? 5 : 15; 147 int pending, dead; 148 149 PendingCounts c(layout); 150 c.set_initial_count(h, count); 151 EXPECT_EQ(c.pending(h), count); 152 153 // Don't increment the dead count this time 154 c.adjust_for_activation(h, false, &pending, &dead); 155 EXPECT_EQ(c.pending(h), count - 1); 156 EXPECT_EQ(c.pending(h), pending); 157 EXPECT_EQ(c.dead_count(h), 0); 158 EXPECT_EQ(c.dead_count(h), dead); 159 160 // Increment the dead count this time 161 c.adjust_for_activation(h, true, &pending, &dead); 162 EXPECT_EQ(c.pending(h), count - 2); 163 EXPECT_EQ(c.pending(h), pending); 164 EXPECT_EQ(c.dead_count(h), dead); 165 EXPECT_EQ(c.dead_count(h), 1); 166 } 167 } 168 169 } // namespace tensorflow 170