tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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_VM_MEMORY_MANAGER_H_
25 #define TVM_RUNTIME_VM_MEMORY_MANAGER_H_
26 
28 #include <tvm/runtime/ndarray.h>
29 #include <tvm/runtime/object.h>
30 
31 #include <functional>
32 #include <memory>
33 #include <mutex>
34 #include <unordered_map>
35 #include <vector>
36 
37 namespace tvm {
38 namespace runtime {
39 namespace vm {
40 
41 struct Buffer {
43  void* data{nullptr};
45  size_t size{0};
48 };
49 
51  kNaive = 1,
53 };
54 
55 class Allocator {
56  public:
57  explicit Allocator(AllocatorType type) : type_(type) {}
58  virtual ~Allocator() = default;
65  NDArray Empty(std::vector<int64_t> shape, DLDataType dtype, Device dev);
67  inline AllocatorType type() const { return type_; }
74  virtual Buffer Alloc(size_t nbytes, size_t alignment, DLDataType type_hint) = 0;
78  virtual void Free(const Buffer& buffer) = 0;
82  virtual size_t UsedMemory() const = 0;
83 
84  private:
85  AllocatorType type_;
86 };
87 
89  public:
90  static MemoryManager* Global();
97  static Allocator* GetOrCreateAllocator(Device dev, AllocatorType type);
103  static Allocator* GetAllocator(Device dev);
104 
105  private:
106  MemoryManager() {}
107 
108  private:
109  std::mutex mu_;
110  std::unordered_map<Device, std::unique_ptr<Allocator>> allocators_;
111 };
112 
114 class StorageObj : public Object {
115  public:
118 
120  NDArray AllocNDArray(size_t offset, std::vector<int64_t> shape, DLDataType dtype);
121 
123  static void Deleter(Object* ptr);
124 
126  auto alloc = MemoryManager::Global()->GetAllocator(buffer.device);
127  alloc->Free(buffer);
128  }
129 
130  static constexpr const uint32_t _type_index = TypeIndex::kDynamic;
131  static constexpr const char* _type_key = "vm.Storage";
133 };
134 
136 class Storage : public ObjectRef {
137  public:
138  explicit Storage(Buffer buffer);
139 
141 };
142 
143 } // namespace vm
144 } // namespace runtime
145 } // namespace tvm
146 
147 #endif // TVM_RUNTIME_VM_MEMORY_MANAGER_H_
Definition: memory_manager.h:41
AllocatorType type() const
Return the allocator type.
Definition: memory_manager.h:67
Device device
The context of the allocated buffers.
Definition: memory_manager.h:47
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Definition: memory_manager.h:52
AllocatorType
Definition: memory_manager.h:50
base class of all object containers.
Definition: object.h:167
static MemoryManager * Global()
Managed NDArray. The array is backed by reference counted blocks.
Definition: ndarray.h:51
Definition: memory_manager.h:51
#define TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:744
A device-independent managed NDArray abstraction.
Buffer buffer
The index into the VM function table.
Definition: memory_manager.h:117
static Allocator * GetAllocator(Device dev)
Get an allocator given the context.
void * data
The pointer to the allocated block of memory.
Definition: memory_manager.h:43
Tensor shape(const Tensor &src, DataType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:1768
DLDevice Device
Definition: ndarray.h:43
Base class of all object reference.
Definition: object.h:511
A managed object in the TVM runtime.
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:671
virtual void Free(const Buffer &buffer)=0
Free a buffer allocated by the allocator.
Definition: memory_manager.h:88
size_t size
The size of the block.
Definition: memory_manager.h:45
~StorageObj()
Definition: memory_manager.h:125
An object representing a storage allocation.
Definition: memory_manager.h:114
Definition: memory_manager.h:55
Type index is allocated during runtime.
Definition: object.h:80
reference to storage.
Definition: memory_manager.h:136
Allocator(AllocatorType type)
Definition: memory_manager.h:57