tvm
memory_manager.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
24 #ifndef TVM_RUNTIME_MEMORY_MEMORY_MANAGER_H_
25 #define TVM_RUNTIME_MEMORY_MEMORY_MANAGER_H_
26 
27 #include <tvm/runtime/base.h>
28 #include <tvm/runtime/tensor.h>
29 
30 #include <functional>
31 #include <memory>
32 #include <mutex>
33 #include <string>
34 #include <unordered_map>
35 #include <vector>
36 
37 namespace tvm {
38 namespace runtime {
39 namespace memory {
40 
42  kNaive = 1,
44 };
45 
46 struct Buffer {
48  void* data{nullptr};
50  size_t size{0};
55 };
56 
58  public:
59  explicit Allocator(AllocatorType type) : type_(type) {}
60  virtual ~Allocator() = default;
68  Tensor Empty(ffi::Shape shape, DLDataType dtype, Device dev,
69  ffi::Optional<ffi::String> mem_scope = std::nullopt);
71  inline AllocatorType type() const { return type_; }
79  virtual Buffer Alloc(Device dev, size_t nbytes, size_t alignment, DLDataType type_hint) = 0;
87  virtual Buffer Alloc(Device dev, ffi::Shape shape, DLDataType type_hint,
88  const std::string& mem_scope = "");
89 
97  virtual void* CreateView(const Buffer& buffer, ffi::Shape shape, DLDataType type_hint,
98  const std::string& mem_scope = "global") {
99  return buffer.data;
100  }
101 
106  virtual void FreeView(Device dev, void* data) {}
107 
111  virtual void Free(const Buffer& buffer) = 0;
113  virtual void Clear();
117  virtual size_t UsedMemory() const = 0;
118 
119  protected:
121  virtual bool AllowMemoryScope(const std::string& mem_scope) const;
122 
123  private:
124  AllocatorType type_;
125 };
126 
128  public:
145  static void Clear();
146 
147  private:
148  MemoryManager() {}
149 
150  protected:
151  std::mutex mu_;
152  std::unordered_map<Device, std::unordered_map<AllocatorType, std::unique_ptr<Allocator>>>
154 };
155 
157 class StorageObj : public ffi::Object {
158  public:
162  Allocator* allocator = nullptr;
163 
165  TVM_RUNTIME_DLL Tensor AllocTensor(int64_t offset, ffi::Shape shape, DLDataType dtype);
166 
168  TVM_RUNTIME_DLL Tensor AllocTensorScoped(int64_t offset, ffi::Shape shape, DLDataType dtype,
169  ffi::String scope = "global");
170 
172  if (allocator) {
174  }
175  }
176 
177  static constexpr const bool _type_mutable = true;
178  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("vm.Storage", StorageObj, ffi::Object);
179 };
180 
182 class Storage : public ffi::ObjectRef {
183  public:
184  TVM_RUNTIME_DLL explicit Storage(Buffer buffer, Allocator* allocator);
185 
187 };
188 
189 } // namespace memory
190 
191 using memory::Allocator;
194 using memory::StorageObj;
195 
196 } // namespace runtime
197 } // namespace tvm
198 
199 #endif // TVM_RUNTIME_MEMORY_MEMORY_MANAGER_H_
Managed Tensor. The array is backed by reference counted blocks.
Definition: tensor.h:49
Definition: memory_manager.h:57
virtual void Free(const Buffer &buffer)=0
Free a buffer allocated by the allocator.
virtual Buffer Alloc(Device dev, ffi::Shape shape, DLDataType type_hint, const std::string &mem_scope="")
Allocate a buffer given a shape and type.
virtual size_t UsedMemory() const =0
The amount of memory currently allocated.
AllocatorType type() const
Return the allocator type.
Definition: memory_manager.h:71
virtual bool AllowMemoryScope(const std::string &mem_scope) const
Check if the given memory scope is allowed to allocate by the allocator.
Allocator(AllocatorType type)
Definition: memory_manager.h:59
virtual Buffer Alloc(Device dev, size_t nbytes, size_t alignment, DLDataType type_hint)=0
Allocate a buffer given a size, alignment and type.
virtual void Clear()
Clear the allocated memory.
virtual void FreeView(Device dev, void *data)
Release the view .
Definition: memory_manager.h:106
virtual void * CreateView(const Buffer &buffer, ffi::Shape shape, DLDataType type_hint, const std::string &mem_scope="global")
Create a view for the buffer given a shape, type and scope.
Definition: memory_manager.h:97
Tensor Empty(ffi::Shape shape, DLDataType dtype, Device dev, ffi::Optional< ffi::String > mem_scope=std::nullopt)
Allocate an empty Tensor using from the allocator.
Definition: memory_manager.h:127
static void Clear()
Clear the allocators.
std::mutex mu_
Definition: memory_manager.h:151
static TVM_RUNTIME_DLL MemoryManager * Global()
static TVM_RUNTIME_DLL Allocator * GetOrCreateAllocator(Device dev, AllocatorType type)
Get or create an allocator given the context and allocator type.
static TVM_RUNTIME_DLL Allocator * GetAllocator(Device dev, AllocatorType type)
Get an allocator given the context.
std::unordered_map< Device, std::unordered_map< AllocatorType, std::unique_ptr< Allocator > > > allocators_
Definition: memory_manager.h:153
An object representing a storage allocation.
Definition: memory_manager.h:157
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("vm.Storage", StorageObj, ffi::Object)
Buffer buffer
The index into the VM function table.
Definition: memory_manager.h:160
Allocator * allocator
The allocator where the storage buffer is allocated from.
Definition: memory_manager.h:162
~StorageObj()
Definition: memory_manager.h:171
static constexpr const bool _type_mutable
Definition: memory_manager.h:177
TVM_RUNTIME_DLL Tensor AllocTensorScoped(int64_t offset, ffi::Shape shape, DLDataType dtype, ffi::String scope="global")
Allocate an Tensor with memory scope from a given piece of storage.
TVM_RUNTIME_DLL Tensor AllocTensor(int64_t offset, ffi::Shape shape, DLDataType dtype)
Allocate an Tensor from a given piece of storage.
reference to storage.
Definition: memory_manager.h:182
TVM_RUNTIME_DLL Storage(Buffer buffer, Allocator *allocator)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Storage, ffi::ObjectRef, StorageObj)
AllocatorType
Definition: memory_manager.h:41
@ kNaive
Definition: memory_manager.h:42
@ kPooled
Definition: memory_manager.h:43
Tensor shape(const Tensor &src, PrimType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:2010
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
DLDevice Device
Definition: device_api.h:43
#define TVM_RUNTIME_DLL
Definition: base.h:92
A device-independent managed Tensor abstraction.
Definition: memory_manager.h:46
Device device
The context of the allocated buffers.
Definition: memory_manager.h:52
AllocatorType alloc_type
The allocator that created this buffer.
Definition: memory_manager.h:54
void * data
The pointer to the allocated block of memory.
Definition: memory_manager.h:48
size_t size
The size of the block.
Definition: memory_manager.h:50