forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtesting_object.h
More file actions
290 lines (235 loc) · 9.27 KB
/
testing_object.h
File metadata and controls
290 lines (235 loc) · 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef TVM_FFI_TESTING_OBJECT_H_
#define TVM_FFI_TESTING_OBJECT_H_
#include <tvm/ffi/container/array.h>
#include <tvm/ffi/memory.h>
#include <tvm/ffi/object.h>
#include <tvm/ffi/reflection/registry.h>
#include <tvm/ffi/string.h>
namespace tvm {
namespace ffi {
namespace testing {
// We deliberately pad extra
// in the header to test cases
// where the object subclass address
// do not align with the base object address
// not handling properly will cause buffer overflow
class BasePad {
public:
int64_t extra[4];
};
class TNumberObj : public BasePad, public Object {
public:
// declare as one slot, with float as overflow
static constexpr uint32_t _type_child_slots = 1;
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
static constexpr const char* _type_key = "test.Number";
TVM_FFI_DECLARE_BASE_OBJECT_INFO(TNumberObj, Object);
};
class TNumber : public ObjectRef {
public:
TVM_FFI_DEFINE_OBJECT_REF_METHODS(TNumber, ObjectRef, TNumberObj);
};
class TIntObj : public TNumberObj {
public:
int64_t value;
TIntObj(int64_t value) : value(value) {}
int64_t GetValue() const { return value; }
static constexpr const char* _type_key = "test.Int";
inline static void RegisterReflection();
TVM_FFI_DECLARE_FINAL_OBJECT_INFO(TIntObj, TNumberObj);
};
class TInt : public TNumber {
public:
explicit TInt(int64_t value) { data_ = make_object<TIntObj>(value); }
static TInt StaticAdd(TInt lhs, TInt rhs) { return TInt(lhs->value + rhs->value); }
TVM_FFI_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TInt, TNumber, TIntObj);
};
inline void TIntObj::RegisterReflection() {
namespace refl = tvm::ffi::reflection;
refl::ObjectDef<TIntObj>()
.def_ro("value", &TIntObj::value)
.def_static("static_add", &TInt::StaticAdd, "static add method");
// define extra type attributes
refl::TypeAttrDef<TIntObj>()
.def("test.GetValue", &TIntObj::GetValue)
.attr("test.size", sizeof(TIntObj));
}
class TFloatObj : public TNumberObj {
public:
double value;
TFloatObj(double value) : value(value) {}
double Add(double other) const { return value + other; }
static void RegisterReflection() {
namespace refl = tvm::ffi::reflection;
refl::ObjectDef<TFloatObj>()
.def_ro("value", &TFloatObj::value, "float value field", refl::DefaultValue(10.0))
.def("sub",
[](const TFloatObj* self, double other) -> double { return self->value - other; })
.def("add", &TFloatObj::Add, "add method");
}
static constexpr const char* _type_key = "test.Float";
TVM_FFI_DECLARE_FINAL_OBJECT_INFO(TFloatObj, TNumberObj);
};
class TFloat : public TNumber {
public:
explicit TFloat(double value) { data_ = make_object<TFloatObj>(value); }
TVM_FFI_DEFINE_OBJECT_REF_METHODS(TFloat, TNumber, TFloatObj);
};
class TPrimExprObj : public Object {
public:
std::string dtype;
double value;
TPrimExprObj(std::string dtype, double value) : dtype(dtype), value(value) {}
static void RegisterReflection() {
namespace refl = tvm::ffi::reflection;
refl::ObjectDef<TPrimExprObj>()
.def_rw("dtype", &TPrimExprObj::dtype, "dtype field", refl::DefaultValue("float"))
.def_ro("value", &TPrimExprObj::value, "value field", refl::DefaultValue(0))
.def("sub", [](TPrimExprObj* self, double other) -> double {
// this is ok because TPrimExprObj is declared asmutable
return self->value - other;
});
}
static constexpr const char* _type_key = "test.PrimExpr";
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
static constexpr bool _type_mutable = true;
TVM_FFI_DECLARE_FINAL_OBJECT_INFO(TPrimExprObj, Object);
};
class TPrimExpr : public ObjectRef {
public:
explicit TPrimExpr(std::string dtype, double value) {
data_ = make_object<TPrimExprObj>(dtype, value);
}
TVM_FFI_DEFINE_OBJECT_REF_METHODS(TPrimExpr, ObjectRef, TPrimExprObj);
};
class TVarObj : public Object {
public:
std::string name;
TVarObj(std::string name) : name(name) {}
static void RegisterReflection() {
namespace refl = tvm::ffi::reflection;
refl::ObjectDef<TVarObj>().def_ro("name", &TVarObj::name,
refl::AttachFieldFlag::SEqHashIgnore());
}
static constexpr const char* _type_key = "test.Var";
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindFreeVar;
TVM_FFI_DECLARE_FINAL_OBJECT_INFO(TVarObj, Object);
};
class TVar : public ObjectRef {
public:
explicit TVar(std::string name) { data_ = make_object<TVarObj>(name); }
TVM_FFI_DEFINE_OBJECT_REF_METHODS(TVar, ObjectRef, TVarObj);
};
class TFuncObj : public Object {
public:
Array<TVar> params;
Array<ObjectRef> body;
String comment;
TFuncObj(Array<TVar> params, Array<ObjectRef> body, String comment)
: params(params), body(body), comment(comment) {}
static void RegisterReflection() {
namespace refl = tvm::ffi::reflection;
refl::ObjectDef<TFuncObj>()
.def_ro("params", &TFuncObj::params, refl::AttachFieldFlag::SEqHashDef())
.def_ro("body", &TFuncObj::body)
.def_ro("comment", &TFuncObj::comment, refl::AttachFieldFlag::SEqHashIgnore());
}
static constexpr const char* _type_key = "test.Func";
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
TVM_FFI_DECLARE_FINAL_OBJECT_INFO(TFuncObj, Object);
};
class TFunc : public ObjectRef {
public:
explicit TFunc(Array<TVar> params, Array<ObjectRef> body, String comment) {
data_ = make_object<TFuncObj>(params, body, comment);
}
TVM_FFI_DEFINE_OBJECT_REF_METHODS(TFunc, ObjectRef, TFuncObj);
};
class TCustomFuncObj : public Object {
public:
Array<TVar> params;
Array<ObjectRef> body;
String comment;
TCustomFuncObj(Array<TVar> params, Array<ObjectRef> body, String comment)
: params(params), body(body), comment(comment) {}
bool SEqual(const TCustomFuncObj* other,
ffi::TypedFunction<bool(AnyView, AnyView, bool, AnyView)> cmp) const {
if (!cmp(params, other->params, true, "params")) {
std::cout << "custom s_equal failed params" << std::endl;
return false;
}
if (!cmp(body, other->body, false, "body")) {
std::cout << "custom s_equal failed body" << std::endl;
return false;
}
return true;
}
uint64_t SHash(uint64_t type_key_hash, ffi::TypedFunction<uint64_t(AnyView, bool)> hash) const {
uint64_t hash_value = type_key_hash;
hash_value = tvm::ffi::details::StableHashCombine(hash_value, hash(params, true));
hash_value = tvm::ffi::details::StableHashCombine(hash_value, hash(body, false));
return hash_value;
}
static void RegisterReflection() {
namespace refl = tvm::ffi::reflection;
refl::ObjectDef<TCustomFuncObj>()
.def_ro("params", &TCustomFuncObj::params)
.def_ro("body", &TCustomFuncObj::body)
.def_ro("comment", &TCustomFuncObj::comment);
refl::TypeAttrDef<TCustomFuncObj>()
.def("__s_equal__", &TCustomFuncObj::SEqual)
.def("__s_hash__", &TCustomFuncObj::SHash);
}
static constexpr const char* _type_key = "test.CustomFunc";
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindCustomTreeNode;
TVM_FFI_DECLARE_FINAL_OBJECT_INFO(TCustomFuncObj, Object);
};
class TCustomFunc : public ObjectRef {
public:
explicit TCustomFunc(Array<TVar> params, Array<ObjectRef> body, String comment) {
data_ = make_object<TCustomFuncObj>(params, body, comment);
}
TVM_FFI_DEFINE_OBJECT_REF_METHODS(TCustomFunc, ObjectRef, TCustomFuncObj);
};
} // namespace testing
template <>
inline constexpr bool use_default_type_traits_v<testing::TPrimExpr> = true;
template <>
struct TypeTraits<testing::TPrimExpr>
: public ObjectRefWithFallbackTraitsBase<testing::TPrimExpr, StrictBool, int64_t, double,
String> {
TVM_FFI_INLINE static testing::TPrimExpr ConvertFallbackValue(StrictBool value) {
return testing::TPrimExpr("bool", static_cast<double>(value));
}
TVM_FFI_INLINE static testing::TPrimExpr ConvertFallbackValue(int64_t value) {
return testing::TPrimExpr("int64", static_cast<double>(value));
}
TVM_FFI_INLINE static testing::TPrimExpr ConvertFallbackValue(double value) {
return testing::TPrimExpr("float32", static_cast<double>(value));
}
// hack into the dtype to store string
TVM_FFI_INLINE static testing::TPrimExpr ConvertFallbackValue(String value) {
return testing::TPrimExpr(value, 0);
}
};
} // namespace ffi
} // namespace tvm
#endif // TVM_FFI_TESTING_OBJECT_H_