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