• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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_COMPILER_XLA_SERVICE_MAYBE_OWNING_DEVICE_MEMORY_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_MAYBE_OWNING_DEVICE_MEMORY_H_
18 
19 #include "absl/types/optional.h"
20 #include "absl/types/variant.h"
21 #include "tensorflow/stream_executor/device_memory_allocator.h"
22 
23 namespace xla {
24 
25 // MaybeOwningDeviceMemory represents either an owned or unowned device memory.
26 // Like std::variant<se::OwningDeviceMemory, DeviceMemory>. When the object goes
27 // output of scope, it will free the underlying memory if it owns it.
28 class MaybeOwningDeviceMemory {
29  public:
30   MaybeOwningDeviceMemory() = default;
MaybeOwningDeviceMemory(tensorflow::se::OwningDeviceMemory owned)31   explicit MaybeOwningDeviceMemory(tensorflow::se::OwningDeviceMemory owned)
32       : mem_(std::move(owned)) {}
MaybeOwningDeviceMemory(tensorflow::se::DeviceMemoryBase unowned)33   explicit MaybeOwningDeviceMemory(tensorflow::se::DeviceMemoryBase unowned)
34       : mem_(unowned) {}
35   MaybeOwningDeviceMemory(MaybeOwningDeviceMemory&&) = default;
36   ~MaybeOwningDeviceMemory() = default;
37 
38   MaybeOwningDeviceMemory& operator=(tensorflow::se::DeviceMemoryBase unowned) {
39     mem_ = unowned;
40     return *this;
41   }
42 
43   MaybeOwningDeviceMemory& operator=(tensorflow::se::OwningDeviceMemory owned) {
44     mem_ = std::move(owned);
45     return *this;
46   }
47 
48   MaybeOwningDeviceMemory& operator=(MaybeOwningDeviceMemory&&) = default;
49 
50   // Fetches the underlying DeviceMemoryBase from a MaybeOwningDeviceMemory. The
51   // caller of this function is *not* responsible for freeing the memory.
52   tensorflow::se::DeviceMemoryBase AsDeviceMemoryBase() const;
53 
54   // Release the tensorflow::se::OwningDeviceMemory without freeing it, and
55   // moves the ownership of the memory buffer from the object to the caller.
56   //
57   // A nullopt is returned if the HasOwnership() == false;
58   absl::optional<tensorflow::se::OwningDeviceMemory> Release();
59 
60   // If the device memory is owned, returns a pointer to the internal
61   // OwningDeviceMemory, otherwise nullptr is returned.
62   const tensorflow::se::OwningDeviceMemory* AsOwningDeviceMemory() const;
63 
64   // Returns true if the device_memory has ownership over underlying memory.
65   bool HasOwnership() const;
66 
67  private:
68   absl::variant<tensorflow::se::OwningDeviceMemory,
69                 tensorflow::se::DeviceMemoryBase>
70       mem_;
71 };
72 
73 }  // namespace xla
74 
75 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_MAYBE_OWNING_DEVICE_MEMORY_H_
76