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();
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 
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_
Managed NDArray. The array is backed by reference counted blocks.
Definition: ndarray.h:51
Base class of all object reference.
Definition: object.h:515
base class of all object containers.
Definition: object.h:167
Definition: memory_manager.h:55
virtual Buffer Alloc(size_t nbytes, size_t alignment, DLDataType type_hint)=0
Allocate a buffer given a size, alignment and type.
virtual size_t UsedMemory() const =0
The amount of memory currently allocated.
virtual void Free(const Buffer &buffer)=0
Free a buffer allocated by the allocator.
AllocatorType type() const
Return the allocator type.
Definition: memory_manager.h:67
Allocator(AllocatorType type)
Definition: memory_manager.h:57
NDArray Empty(std::vector< int64_t > shape, DLDataType dtype, Device dev)
Allocate an empty NDArray using from the allocator.
virtual ~Allocator()=default
Definition: memory_manager.h:88
static Allocator * GetOrCreateAllocator(Device dev, AllocatorType type)
Get or create an allocator given the context and allocator type.
static MemoryManager * Global()
static Allocator * GetAllocator(Device dev)
Get an allocator given the context.
An object representing a storage allocation.
Definition: memory_manager.h:114
TVM_DECLARE_FINAL_OBJECT_INFO(StorageObj, Object)
static constexpr const char * _type_key
Definition: memory_manager.h:131
~StorageObj()
Definition: memory_manager.h:125
NDArray AllocNDArray(size_t offset, std::vector< int64_t > shape, DLDataType dtype)
Allocate an NDArray from a given piece of storage.
Buffer buffer
The index into the VM function table.
Definition: memory_manager.h:117
static constexpr const uint32_t _type_index
Definition: memory_manager.h:130
static void Deleter(Object *ptr)
The deleter for an NDArray when allocated from underlying storage.
reference to storage.
Definition: memory_manager.h:136
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(Storage, ObjectRef, StorageObj)
AllocatorType
Definition: memory_manager.h:50
@ kPooled
Definition: memory_manager.h:52
@ kNaive
Definition: memory_manager.h:51
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:1763
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
DLDevice Device
Definition: ndarray.h:43
A device-independent managed NDArray abstraction.
A managed object in the TVM runtime.
@ kDynamic
Type index is allocated during runtime.
Definition: object.h:80
Definition: memory_manager.h:41
Device device
The context of the allocated buffers.
Definition: memory_manager.h:47
size_t size
The size of the block.
Definition: memory_manager.h:45
void * data
The pointer to the allocated block of memory.
Definition: memory_manager.h:43