• 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 #ifndef TENSORFLOW_CORE_KERNELS_DATA_PREFETCH_AUTOTUNER_H_
17 #define TENSORFLOW_CORE_KERNELS_DATA_PREFETCH_AUTOTUNER_H_
18 
19 #include "tensorflow/core/platform/types.h"
20 
21 namespace tensorflow {
22 namespace data {
23 
24 // PrefetchAutotuner dynamically adjusts the buffer size of a prefetch iterator.
25 //
26 // PrefetchAutotuner attempts to find the minimum buffer size such that there is
27 // always at least 1 element in the prefetch queue every time the downstream
28 // iterator calls GetNext().
29 //
30 // One common failure mode of input pipelines is being throughput bound. No
31 // amount of prefetching can address that performance mode. In order to guard
32 // against this condition, PrefetchAutotuner will only increase the buffer_limit
33 // if the prefetching thread is able to successfully fill the buffer at its
34 // current size.
35 //
36 // Note: in the current implementation, we never decrease the buffer_limit().
37 // This should change in the future!
38 //
39 // PrefetchAutotuner is NOT thread safe.
40 class PrefetchAutotuner {
41  public:
42   static const int64 kAutoTune = -1;
43 
44   explicit PrefetchAutotuner(int64 initial_buffer_size);
45 
buffer_limit()46   int64 buffer_limit() const { return buffer_limit_; }
47 
48   void RecordConsumption(size_t current_buffer_size);
RecordEmpty()49   void RecordEmpty() { RecordConsumption(0); }
50 
51  private:
52   // PrefetchAutotuner operates as a state machine.
53   enum class Mode {
54     // Disables the autotuning.
55     kDisabled,
56 
57     // We have increased the size of the buffer, and will transition to
58     // kDownswing if we successfully fill the buffer.
59     kUpswing,
60 
61     // We have successfully filled a buffer of this size. If we ever block the
62     // downstream iterator, we should increase the buffer size.
63     kDownswing,
64   };
65 
66   int64 buffer_limit_;
67   Mode mode_ = Mode::kDisabled;
68 };
69 
70 }  // namespace data
71 }  // namespace tensorflow
72 
73 #endif  // TENSORFLOW_CORE_KERNELS_DATA_PREFETCH_AUTOTUNER_H_
74