Skip to content

Commit a83ac24

Browse files
committed
Format and Buffer data structure (#1)
1 parent d061d7f commit a83ac24

File tree

2 files changed

+275
-0
lines changed

2 files changed

+275
-0
lines changed

include/tvm/tir/sparse.h

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
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+
/*!
21+
* \brief tvm/tir/sparse.h
22+
* \brief sparse axes and buffers.
23+
*/
24+
#ifndef TVM_TIR_SPARSE_H_
25+
#define TVM_TIR_SPARSE_H_
26+
27+
#include <tvm/ir/expr.h>
28+
#include <tvm/runtime/container/array.h>
29+
#include <tvm/runtime/container/string.h>
30+
#include <tvm/tir/buffer.h>
31+
#include <tvm/tir/var.h>
32+
33+
namespace tvm {
34+
namespace tir {
35+
namespace sparse {
36+
37+
/*!
38+
* \brief Base type for axis in sparse formats.
39+
*/
40+
class AxisNode : public Object {
41+
public:
42+
/* name of current axis. */
43+
String name;
44+
/* length of current axis. For sparse axis, length refers to the upperbound of
45+
* the current axis. */
46+
PrimExpr length;
47+
static constexpr const char* _type_key = "tir.sparse.Axis";
48+
TVM_DECLARE_BASE_OBJECT_INFO(AxisNode, Object);
49+
};
50+
51+
/*!
52+
* \brief Managed reference to AxisNode.
53+
* \sa AxisNode
54+
*/
55+
class Axis : public ObjectRef {
56+
public:
57+
TVM_DEFINE_OBJECT_REF_METHODS(Axis, ObjectRef, AxisNode);
58+
};
59+
60+
/*!
61+
* \brief Root of Axis Dependency Tree.
62+
*/
63+
class RootAxisNode : public Object {
64+
public:
65+
static constexpr const char* _type_key = "tir.sparse.RootAxis";
66+
TVM_DECLARE_FINAL_OBJECT_INFO(RootAxisNode, Object);
67+
};
68+
69+
/*!
70+
* \brief Managed reference to RootAxisNode.
71+
* \sa RootAxisNode
72+
*/
73+
class RootAxis : public ObjectRef {
74+
public:
75+
TVM_DEFINE_OBJECT_REF_METHODS(RootAxis, ObjectRef, RootAxisNode);
76+
};
77+
78+
/*!
79+
* \brief Dense axis whose column indices are consecutive.
80+
*/
81+
class DenseAxisNode : public AxisNode {
82+
public:
83+
static constexpr const char* _type_key = "tir.sparse.DenseAxis";
84+
TVM_DECLARE_BASE_OBJECT_INFO(DenseAxisNode, AxisNode);
85+
};
86+
87+
/*!
88+
* \brief Managed reference to DenseAxisNode.
89+
* \sa DenseAxisNode
90+
*/
91+
class DenseAxis : public Axis {
92+
public:
93+
TVM_DEFINE_OBJECT_REF_METHODS(DenseAxis, Axis, DenseAxisNode);
94+
};
95+
96+
/*!
97+
* \brief Dense axis with fixed length per row.
98+
*/
99+
class DenseFixedAxisNode : public DenseAxisNode {
100+
public:
101+
static constexpr const char* _type_key = "tir.sparse.DenseFixedAxis";
102+
TVM_DECLARE_FINAL_OBJECT_INFO(DenseFixedAxisNode, DenseAxisNode);
103+
};
104+
105+
/*!
106+
* \brief Managed reference to DenseFixedAxisNode.
107+
* \sa DenseFixedAxisNode
108+
*/
109+
class DenseFixedAxis : public DenseAxis {
110+
public:
111+
TVM_DEFINE_OBJECT_REF_METHODS(DenseFixedAxis, DenseAxis, DenseFixedAxisNode);
112+
};
113+
114+
class DenseVariableAxisNode : public DenseAxisNode {
115+
public:
116+
Buffer indptr;
117+
static constexpr const char* _type_key = "tir.sparse.DenseVariableAxis";
118+
TVM_DECLARE_FINAL_OBJECT_INFO(DenseVariableAxisNode, DenseAxisNode);
119+
};
120+
121+
/*!
122+
* \brief Dense axis whose length is dependent on its predecessors on the axis
123+
* dependency tree.
124+
*/
125+
class DenseVariableAxis : public DenseAxis {
126+
public:
127+
TVM_DEFINE_OBJECT_REF_METHODS(DenseVariableAxis, DenseAxis,
128+
DenseVariableAxisNode);
129+
};
130+
131+
/*!
132+
* \brief Sparse axis whose column indices is not consecutive.
133+
*/
134+
class SparseAxisNode : public AxisNode {
135+
public:
136+
static constexpr const char* _type_key = "tir.sparse.SparseAxis";
137+
TVM_DECLARE_BASE_OBJECT_INFO(SparseAxisNode, AxisNode);
138+
};
139+
140+
/*!
141+
* \brief Managed reference to SparseAxisNode.
142+
* \sa SparseAxisNode
143+
*/
144+
class SparseAxis : public Axis {
145+
public:
146+
TVM_DEFINE_OBJECT_REF_METHODS(SparseAxis, Axis, SparseAxisNode);
147+
};
148+
149+
/*!
150+
* \brief Sparse axis with fixed number of non-zero columns per row.
151+
*/
152+
class SparseFixedAxisNode : public SparseAxisNode {
153+
public:
154+
Buffer indices;
155+
/* fixed number of columns of current sparse axis. */
156+
PrimExpr num_cols;
157+
static constexpr const char* _type_key = "tir.sparse.SparseFixedAxis";
158+
TVM_DECLARE_FINAL_OBJECT_INFO(SparseFixedAxisNode, SparseAxisNode);
159+
};
160+
161+
/*!
162+
* \brief Managed reference to SparseFixedAxisNode.
163+
* \sa SparseFixedAxisNode
164+
*/
165+
class SparseFixedAxis : public SparseAxis {
166+
public:
167+
TVM_DEFINE_OBJECT_REF_METHODS(SparseFixedAxis, SparseAxis,
168+
SparseFixedAxisNode);
169+
};
170+
171+
/*!
172+
* \brief Sparse axis with variable number of non-zero columns per row.
173+
*/
174+
class SparseVariableAxisNode : public SparseAxisNode {
175+
public:
176+
Buffer indptr, indices;
177+
static constexpr const char* _type_key = "tir.sparse.SparseVariabledAxis";
178+
TVM_DECLARE_FINAL_OBJECT_INFO(SparseVariableAxisNode, SparseAxisNode);
179+
};
180+
181+
/*!
182+
* \brief Managed reference to SparseVariableAxisNode.
183+
* \sa SparseVariableAxisNode
184+
*/
185+
class SparseVariableAxis : public SparseAxis {
186+
public:
187+
TVM_DEFINE_OBJECT_REF_METHODS(SparseVariableAxis, SparseAxis,
188+
SparseVariableAxisNode);
189+
};
190+
191+
/*!
192+
* \brief Axis Dependency Tree.
193+
*/
194+
class AxisTreeNode : public Object {
195+
public:
196+
// parent refers to the parent axis of current axis tree.
197+
Optional<AxisTree> parent;
198+
Axis axis;
199+
Array<AxisTree> children;
200+
static constexpr const char* _type_key = "tir.sparse.AxisTree";
201+
TVM_DECLARE_FINAL_OBJECT_INFO(AxisTreeNode, Object);
202+
};
203+
204+
/*!
205+
* \brief Managed reference to AxisRefNode.
206+
* \sa AxisTreeNode
207+
*/
208+
class AxisTree : public ObjectRef {
209+
public:
210+
TVM_DEFINE_OBJECT_REF_METHODS(AxisTree, ObjectRef, AxisTreeNode);
211+
};
212+
213+
/*!
214+
* \brief Class of sparse buffer.
215+
*/
216+
class SparseBufferNode : public Object {
217+
public:
218+
/* Root of Axis Dependency Tree. */
219+
AxisTree root;
220+
/* Axes */
221+
Array<Axis> axes;
222+
/* Number of dimensions */
223+
int ndim;
224+
/* Buffer corresponding to flattened value */
225+
Buffer data;
226+
static constexpr const char* _type_key = "tir.sparse.SparseBufferNode";
227+
TVM_DECLARE_FINAL_OBJECT_INFO(SparseBufferNode, Object);
228+
};
229+
230+
/*!
231+
* \brief Managed reference to SparseBufferNode.
232+
* \sa SparseBufferNode
233+
*/
234+
class SparseBuffer : public ObjectRef {
235+
public:
236+
TVM_DEFINE_OBJECT_REF_METHODS(SparseBuffer, ObjectRef, SparseBufferNode);
237+
};
238+
239+
} // namespace sparse
240+
} // namespace tir
241+
} // namespace tvm
242+
243+
#endif // TVM_TIR_BUFFER_H_

src/tir/ir/sparse.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
/*!
21+
* \file sparse.cc
22+
* \brief buffers and formats in sparse tir.
23+
*/
24+
#include <tvm/tir/buffer.h>
25+
26+
namespace tvm {
27+
namespace tir {
28+
29+
// TODO(zihao/ruihang)
30+
31+
} // namespace tir
32+
} // namespace tvm

0 commit comments

Comments
 (0)