• 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/device_memory_allocator.h"
17 
18 #include <string>
19 
20 #include "tensorflow/compiler/xla/status_macros.h"
21 #include "tensorflow/compiler/xla/types.h"
22 #include "tensorflow/compiler/xla/util.h"
23 #include "tensorflow/core/lib/strings/numbers.h"
24 
25 namespace xla {
26 
StreamExecutorMemoryAllocator(const se::Platform * platform,absl::Span<se::StreamExecutor * const> stream_executors)27 StreamExecutorMemoryAllocator::StreamExecutorMemoryAllocator(
28     const se::Platform* platform,
29     absl::Span<se::StreamExecutor* const> stream_executors)
30     : DeviceMemoryAllocator(platform),
31       stream_executors_(stream_executors.begin(), stream_executors.end()) {}
32 
Allocate(int device_ordinal,uint64 size,bool retry_on_failure)33 StatusOr<OwningDeviceMemory> StreamExecutorMemoryAllocator::Allocate(
34     int device_ordinal, uint64 size, bool retry_on_failure) {
35   TF_ASSIGN_OR_RETURN(se::StreamExecutor * stream_executor,
36                       GetStreamExecutor(device_ordinal));
37   se::DeviceMemoryBase result = stream_executor->AllocateArray<uint8>(size);
38   if (size > 0 && result == nullptr) {
39     return ResourceExhausted(
40         "Failed to allocate request for %s (%uB) on device ordinal %d",
41         tensorflow::strings::HumanReadableNumBytes(size), size, device_ordinal);
42   }
43   VLOG(3) << absl::StreamFormat(
44       "Allocated %s (%uB) on device ordinal %d: %p",
45       tensorflow::strings::HumanReadableNumBytes(size), size, device_ordinal,
46       result.opaque());
47   return OwningDeviceMemory(result, device_ordinal, this);
48 }
49 
Deallocate(int device_ordinal,se::DeviceMemoryBase mem)50 Status StreamExecutorMemoryAllocator::Deallocate(int device_ordinal,
51                                                  se::DeviceMemoryBase mem) {
52   if (!mem.is_null()) {
53     TF_ASSIGN_OR_RETURN(se::StreamExecutor * stream_executor,
54                         GetStreamExecutor(device_ordinal));
55     VLOG(3) << absl::StreamFormat("Freeing %p on device ordinal %d",
56                                   mem.opaque(), device_ordinal);
57     stream_executor->Deallocate(&mem);
58   }
59   return Status::OK();
60 }
61 
GetStreamExecutor(int device_ordinal)62 StatusOr<se::StreamExecutor*> StreamExecutorMemoryAllocator::GetStreamExecutor(
63     int device_ordinal) {
64   if (device_ordinal < 0) {
65     return InvalidArgument("device ordinal value (%d) must be non-negative",
66                            device_ordinal);
67   }
68   if (device_ordinal >= stream_executors_.size()) {
69     return InvalidArgument(
70         "device ordinal value (%d) >= number of devices (%u)", device_ordinal,
71         stream_executors_.size());
72   }
73   if (stream_executors_[device_ordinal] == nullptr) {
74     return NotFound("Device %s:%d present but not supported",
75                     platform()->Name(), device_ordinal);
76   }
77   return stream_executors_[device_ordinal];
78 }
79 
AllowsAsynchronousDeallocation() const80 bool StreamExecutorMemoryAllocator::AllowsAsynchronousDeallocation() const {
81   return false;
82 }
83 
84 }  // namespace xla
85