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/core/kernels/data/prefetch_autotuner.h"
17
18 #include "tensorflow/core/framework/model.h"
19
20 namespace tensorflow {
21 namespace data {
22
PrefetchAutotuner(int64_t initial_buffer_size,int64_t buffer_size_min)23 PrefetchAutotuner::PrefetchAutotuner(int64_t initial_buffer_size,
24 int64_t buffer_size_min)
25 : buffer_limit_(initial_buffer_size) {
26 if (initial_buffer_size == model::kAutotune) {
27 mode_ = Mode::kUpswing;
28 buffer_limit_ = std::max(int64_t{1}, buffer_size_min);
29 }
30 }
31
32 namespace {
33 // Determines what strategy to use for increasing the buffer size limit. For
34 // limits less than the threshold, an exponential increase is used, while for
35 // limits greater than or equal to the threshold, a linear increase is used.
36 size_t kBufferLimitThreshold = 2048;
37 } // namespace
38
RecordConsumption(size_t current_buffer_size)39 void PrefetchAutotuner::RecordConsumption(size_t current_buffer_size) {
40 switch (mode_) {
41 case Mode::kDisabled:
42 return;
43 case Mode::kUpswing:
44 if (static_cast<int64_t>(current_buffer_size) == buffer_limit_) {
45 mode_ = Mode::kDownswing;
46 }
47 return;
48 case Mode::kDownswing:
49 if (current_buffer_size == 0) {
50 if (buffer_limit_ >= static_cast<int64_t>(kBufferLimitThreshold)) {
51 buffer_limit_ += kBufferLimitThreshold;
52 } else {
53 buffer_limit_ *= 2;
54 }
55 mode_ = Mode::kUpswing;
56 }
57 return;
58 }
59 }
60
61 } // namespace data
62 } // namespace tensorflow
63