Skip to content

Commit 8c48f2b

Browse files
committed
[RUNTIME] Introduce new object protocol.
This PR introduces a new object protocol to unify the node and object. We also updated the existing runtime::vm code to make use of the new system. Update to the node will be done in a follow up PR. Other changes: - Remove object related code in json serializer as that code logic was not complete and we have a separate serializer for VM, can revisit later.
1 parent 068c148 commit 8c48f2b

19 files changed

Lines changed: 838 additions & 384 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ cpplint:
7070
python3 3rdparty/dmlc-core/scripts/lint.py vta cpp vta/include vta/src
7171
python3 3rdparty/dmlc-core/scripts/lint.py topi cpp topi/include;
7272
python3 3rdparty/dmlc-core/scripts/lint.py nnvm cpp nnvm/include nnvm/src;
73-
python3 3rdparty/dmlc-core/scripts/lint.py tvm cpp include src verilog\
73+
python3 3rdparty/dmlc-core/scripts/lint.py tvm cpp include src \
7474
examples/extension/src examples/graph_executor/src
7575

7676
pylint:

include/tvm/node/node.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace runtime {
4242
// forward declaration
4343
class NDArray;
4444
// forward declaration
45-
class Object;
45+
class ObjectRef;
4646
} // namespace runtime
4747

4848
/*!
@@ -63,7 +63,7 @@ class TVM_DLL AttrVisitor {
6363
virtual void Visit(const char* key, DataType* value) = 0;
6464
virtual void Visit(const char* key, NodeRef* value) = 0;
6565
virtual void Visit(const char* key, runtime::NDArray* value) = 0;
66-
virtual void Visit(const char* key, runtime::Object* value) = 0;
66+
virtual void Visit(const char* key, runtime::ObjectRef* value) = 0;
6767
template<typename ENum,
6868
typename = typename std::enable_if<std::is_enum<ENum>::value>::type>
6969
void Visit(const char* key, ENum* ptr) {

include/tvm/runtime/memory.h

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
/*!
20+
* \file tvm/runtime/memory.h
21+
* \brief Runtime memory management.
22+
*/
23+
#ifndef TVM_RUNTIME_MEMORY_H_
24+
#define TVM_RUNTIME_MEMORY_H_
25+
26+
#include <utility>
27+
#include <type_traits>
28+
#include "object.h"
29+
30+
namespace tvm {
31+
namespace runtime {
32+
/*!
33+
* \brief Allocate an object using default allocator.
34+
* \param args arguments to the constructor.
35+
* \tparam T the node type.
36+
* \return The NodePtr to the allocated object.
37+
*/
38+
template<typename T, typename... Args>
39+
inline ObjectPtr<T> make_object(Args&&... args);
40+
41+
// Detail implementations after this
42+
//
43+
// The current design allows swapping the
44+
// allocator pattern when necessary.
45+
//
46+
// Possible future allocator optimizations:
47+
// - Arena allocator that gives ownership of memory to arena (deleter_= nullptr)
48+
// - Thread-local object pools: one pool per size and alignment requirement.
49+
// - Can specialize by type of object to give the specific allocator to each object.
50+
51+
/*!
52+
* \brief Base class of object allocators that implements make.
53+
* Use curiously recurring template pattern.
54+
*
55+
* \tparam Derived The derived class.
56+
*/
57+
template<typename Derived>
58+
class ObjAllocatorBase {
59+
public:
60+
/*!
61+
* \tparam T The type to be allocated.
62+
* \tparam Args The constructor signature.
63+
* \param args The arguments.
64+
*/
65+
template<typename T, typename... Args>
66+
inline ObjectPtr<T> make(Args&&... args) {
67+
using Handler = typename Derived::template Handler<T>;
68+
static_assert(std::is_base_of<Object, T>::value,
69+
"make_node can only be used to create NodeBase");
70+
T* ptr = Handler::New(static_cast<Derived*>(this),
71+
std::forward<Args>(args)...);
72+
ptr->type_index_ = T::type_index();
73+
ptr->deleter_ = Handler::Deleter();
74+
return ObjectPtr<T>(ptr);
75+
}
76+
};
77+
78+
// Simple allocator that uses new/delete.
79+
class SimpleObjAllocator :
80+
public ObjAllocatorBase<SimpleObjAllocator> {
81+
public:
82+
template<typename T>
83+
class Handler {
84+
public:
85+
template<typename... Args>
86+
static T* New(SimpleObjAllocator*, Args&&... args) {
87+
return new T(std::forward<Args>(args)...);
88+
}
89+
90+
static Object::FDeleter Deleter() {
91+
return Deleter_;
92+
}
93+
94+
private:
95+
static void Deleter_(Object* ptr) {
96+
delete static_cast<T*>(ptr);
97+
}
98+
};
99+
};
100+
101+
template<typename T, typename... Args>
102+
inline ObjectPtr<T> make_object(Args&&... args) {
103+
return SimpleObjAllocator().make<T>(std::forward<Args>(args)...);
104+
}
105+
106+
} // namespace runtime
107+
} // namespace tvm
108+
#endif // TVM_RUNTIME_MEMORY_H_

0 commit comments

Comments
 (0)