• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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_STREAM_EXECUTOR_GPU_REDZONE_ALLOCATOR_H_
17 #define TENSORFLOW_STREAM_EXECUTOR_GPU_REDZONE_ALLOCATOR_H_
18 
19 #include <vector>
20 
21 #include "tensorflow/core/lib/math/math_util.h"
22 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
23 #include "tensorflow/stream_executor/device_memory_allocator.h"
24 #include "tensorflow/stream_executor/gpu/asm_compiler.h"
25 
26 namespace stream_executor {
27 
28 // An allocator that allocates a bit of extra memory around the beginning/end of
29 // every allocation and can check that this memory is unmodified.
30 //
31 // This can be used to check for out-of-bounds writes, and, if the redzone is
32 // filled with a sufficiently "ugly" pattern, may also be able to check for
33 // out-of-bounds reads.  The default fill pattern of -1 is an unusual NaN
34 // pattern when interpreted as a floating-point number, so hopefully works for
35 // out-of-bounds reads and writes in those cases.
36 //
37 // This class implements ScratchAllocator, so can be used to allocate temp
38 // memory for cudnn convolutions.
39 class RedzoneAllocator : public ScratchAllocator {
40  public:
41   static const int64 kDefaultMemoryLimit = 1LL << 32;  // 4GB
42   static const int64 kDefaultRedzoneSize =
43       1LL << 23;  // 8MiB per side, 16MiB total.
44   static const uint8 kDefaultRedzonePattern = -1;
45   RedzoneAllocator(Stream* stream, DeviceMemoryAllocator* memory_allocator,
46                    GpuAsmOpts gpu_compilation_opts_,
47                    int64 memory_limit = kDefaultMemoryLimit,
48                    int64 redzone_size = kDefaultRedzoneSize,
49                    uint8 redzone_pattern = kDefaultRedzonePattern);
50 
51   // Redzones don't count towards the memory limit.
GetMemoryLimitInBytes()52   int64 GetMemoryLimitInBytes() override { return memory_limit_; }
53 
TotalAllocatedBytesExcludingRedzones()54   int64 TotalAllocatedBytesExcludingRedzones() const {
55     return allocated_bytes_excluding_redzones_;
56   }
57 
58   port::StatusOr<DeviceMemory<uint8>> AllocateBytes(int64 byte_size) override;
59 
60   // Non-empty redzone check status implies that there was a write into a
61   // redzone, with a string communicating the location of the write.
62   struct RedzoneCheckStatus {
63     RedzoneCheckStatus() = default;
64 
RedzoneCheckStatusRedzoneCheckStatus65     RedzoneCheckStatus(absl::string_view buffer_name, void* user_buffer_address,
66                        int64 offset, uint64 expected_value, uint64 actual_value)
67         : buffer_name(buffer_name),
68           user_buffer_address(user_buffer_address),
69           offset(offset),
70           expected_value(expected_value),
71           actual_value(actual_value) {}
72 
OKRedzoneCheckStatus73     static RedzoneCheckStatus OK() { return {}; }
74 
okRedzoneCheckStatus75     bool ok() { return user_buffer_address == nullptr; }
76 
77     std::string RedzoneFailureMsg() const;
78 
79     string buffer_name = {};
80     void* user_buffer_address = nullptr;
81     int64 offset = 0;
82     uint64 expected_value = 0;
83     uint64 actual_value = 0;
84   };
85 
86   // Determines whether redzones around all allocated buffers are unmodified.
87   //
88   // Reinitializes redzones to the expected value, so that the same buffer
89   // could be reused for multiple checks.
90   //
91   // Returns:
92   //
93   //  - RedzoneCheckStatus::OK() if everything went well.
94   //  - RedzoneCheckStatus with a non-empty error message iff a write into a
95   //    redzone has been detected.
96   //  - A stream error, if loading or launching the kernel has failed.
97   port::StatusOr<RedzoneCheckStatus> CheckRedzones() const;
98 
99  private:
100   const int device_ordinal_;
101   Stream* stream_;
102 
103   // Memory limit of the allocator in bytes.
104   const int64 memory_limit_;
105 
106   // Redzone size on *one side* of allocation in bytes.
107   //
108   // Must be a multiple of kXlaAllocatedBufferAlignBytes, otherwise the buffers
109   // returned to users will be misaligned.
110   const int64 redzone_size_;
111 
112   const uint8 redzone_pattern_;
113   DeviceMemoryAllocator* memory_allocator_;
114   GpuAsmOpts gpu_compilation_opts_;
115 
116   // The second element of the pair is the size of the user allocation.  This
117   // isn't necessarily just first.size() - 2 * redzone_size_ because when the
118   // user allocation size is not a multiple of 4 bytes, we round up the size of
119   // the RHS redzone.
120   std::vector<std::pair<OwningDeviceMemory, int64>> allocated_buffers_;
121 
122   int64 allocated_bytes_excluding_redzones_ = 0;
123 };
124 
125 }  // namespace stream_executor
126 
127 #endif  // TENSORFLOW_STREAM_EXECUTOR_GPU_REDZONE_ALLOCATOR_H_
128