xref: /aosp_15_r20/external/tensorflow/tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 #ifndef TENSORFLOW_CORE_COMMON_RUNTIME_GPU_GPU_BFC_ALLOCATOR_H_
17 #define TENSORFLOW_CORE_COMMON_RUNTIME_GPU_GPU_BFC_ALLOCATOR_H_
18 
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include "tensorflow/core/common_runtime/bfc_allocator.h"
25 #include "tensorflow/core/common_runtime/device/device_mem_allocator.h"
26 #include "tensorflow/core/common_runtime/gpu/gpu_virtual_mem_allocator.h"
27 #include "tensorflow/core/platform/thread_annotations.h"
28 #include "tensorflow/core/platform/types.h"
29 #include "tensorflow/core/protobuf/config.pb.h"
30 
31 namespace tensorflow {
32 
33 // A GPU memory allocator that implements a 'best-fit with coalescing'
34 // algorithm.
35 class GPUBFCAllocator : public BFCAllocator {
36  public:
37   // See BFCAllocator::Options.
38   struct Options {
39     // Overridden by TF_FORCE_GPU_ALLOW_GROWTH if that envvar is set.
40     bool allow_growth = false;
41 
42     // If nullopt, defaults to TF_ENABLE_GPU_GARBAGE_COLLECTION, or true if that
43     // envvar is not present.
44     //
45     // Note:
46     //
47     //  - BFCAllocator defaults garbage_collection to false, not true.
48     //  - this is not the same override behavior as TF_FORCE_GPU_ALLOW_GROWTH.
49     absl::optional<bool> garbage_collection;
50 
51     double fragmentation_fraction = 0;
52     bool allow_retry_on_failure = true;
53   };
54 
55   GPUBFCAllocator(std::unique_ptr<SubAllocator> sub_allocator,
56                   size_t total_memory, const string& name, const Options& opts);
57 
~GPUBFCAllocator()58   ~GPUBFCAllocator() override {}
59 
60   TF_DISALLOW_COPY_AND_ASSIGN(GPUBFCAllocator);
61 };
62 
63 }  // namespace tensorflow
64 
65 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_GPU_GPU_BFC_ALLOCATOR_H_
66