• 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/compiler/xla/service/gpu/buffer_allocations.h"
17 
18 #include <utility>
19 
20 #include "absl/memory/memory.h"
21 #include "tensorflow/compiler/xla/map_util.h"
22 #include "tensorflow/compiler/xla/service/gpu/gpu_constants.h"
23 #include "tensorflow/compiler/xla/status_macros.h"
24 #include "tensorflow/compiler/xla/types.h"
25 #include "tensorflow/compiler/xla/util.h"
26 #include "tensorflow/core/lib/core/errors.h"
27 #include "tensorflow/core/lib/gtl/map_util.h"
28 #include "tensorflow/core/lib/strings/numbers.h"
29 #include "tensorflow/core/platform/logging.h"
30 #include "tensorflow/core/platform/types.h"
31 
32 namespace xla {
33 namespace gpu {
34 
TearDown(const std::set<se::DeviceMemoryBase> & live_addresses,absl::Span<const BufferAllocation> allocations)35 Status BufferAllocations::TearDown(
36     const std::set<se::DeviceMemoryBase>& live_addresses,
37     absl::Span<const BufferAllocation> allocations) {
38   // Deallocate temporary buffers, taking care to try to deallocate all of them
39   // even if one of the deallocations fails.
40   Status status;
41   const int64 num_buffers = allocations.size();
42   for (BufferAllocation::Index i = 0; i < num_buffers; ++i) {
43     const BufferAllocation& allocation = allocations[i];
44     se::DeviceMemoryBase buffer_address = GetDeviceAddress(allocation.index());
45     // Deallocate buffers marked "maybe_live_out" but aren't actually live out,
46     // and temp buffers.
47     if ((allocation.maybe_live_out() &&
48          !live_addresses.count(buffer_address)) ||
49         allocation.IsPreallocatedTempBuffer()) {
50       auto dealloc_result =
51           memory_allocator_->Deallocate(device_ordinal_, buffer_address);
52       if (!dealloc_result.ok() && status.ok()) {
53         status = dealloc_result;
54       }
55     }
56   }
57   return status;
58 }
59 
GetDeviceAddress(BufferAllocation::Index buffer_index) const60 se::DeviceMemoryBase BufferAllocations::GetDeviceAddress(
61     BufferAllocation::Index buffer_index) const {
62   CHECK_GE(buffer_index, 0);
63   CHECK_LT(buffer_index, buffers_.size());
64   return buffers_[buffer_index];
65 }
66 
GetMutableDeviceAddress(BufferAllocation::Index buffer_index)67 se::DeviceMemoryBase& BufferAllocations::GetMutableDeviceAddress(
68     BufferAllocation::Index buffer_index) {
69   CHECK_GE(buffer_index, 0);
70   CHECK_LT(buffer_index, buffers_.size());
71   return buffers_[buffer_index];
72 }
73 
GetDeviceAddress(const BufferAllocation::Slice & buffer_slice) const74 se::DeviceMemoryBase BufferAllocations::GetDeviceAddress(
75     const BufferAllocation::Slice& buffer_slice) const {
76   se::DeviceMemoryBase base = GetDeviceAddress(buffer_slice.index());
77   CHECK_LE(buffer_slice.offset(), base.size());
78   CHECK_LE(buffer_slice.offset() + buffer_slice.size(), base.size());
79   return se::DeviceMemoryBase(
80       static_cast<char*>(base.opaque()) + buffer_slice.offset(),
81       buffer_slice.size());
82 }
83 
ShouldEmitLiteralInLlvmIr(const Literal & literal)84 bool ShouldEmitLiteralInLlvmIr(const Literal& literal) {
85   // LLVM can sometimes do interesting optimizations using scalar constants.
86   return ShapeUtil::IsScalar(literal.shape());
87 }
88 
89 }  // namespace gpu
90 }  // namespace xla
91