tvm
Loading...
Searching...
No Matches
layout.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 */
23#ifndef TVM_TIRX_LAYOUT_H_
24#define TVM_TIRX_LAYOUT_H_
25
26#include <tvm/ffi/container/array.h>
27#include <tvm/ffi/container/tuple.h>
28#include <tvm/ffi/enum.h>
29#include <tvm/ffi/function.h>
30#include <tvm/ffi/object.h>
31#include <tvm/ir/expr.h>
32#include <tvm/ir/module.h>
33#include <tvm/ir/prim/op.h>
34#include <tvm/sym/analyzer.h>
36
37#include <algorithm>
38
39namespace tvm {
40
41namespace tirx {
42
43class Layout;
44class TileLayout;
45class Iter;
46using ffi::Array;
47using ffi::Tuple;
48
50class LayoutObj : public ffi::Object {
51 public:
52 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
53 TVM_FFI_DECLARE_OBJECT_INFO("tirx.Layout", LayoutObj, ffi::Object);
54};
55
57class Layout : public ffi::ObjectRef {
58 public:
61
63 TVM_TIRX_DLL PrimExpr GetSize(ffi::Optional<ffi::String> axis_name = std::nullopt) const;
64
66 TVM_TIRX_DLL PrimExpr GetSpan(ffi::Optional<ffi::String> axis_name = std::nullopt) const;
67
77 TVM_TIRX_DLL ffi::Map<ffi::String, PrimExpr> Apply(ffi::Array<PrimExpr> coord,
78 sym::AnalyzerObj* analyzer) const;
79 TVM_TIRX_DLL ffi::Map<ffi::String, PrimExpr> Apply(PrimExpr coord,
80 sym::AnalyzerObj* analyzer) const;
81 TVM_TIRX_DLL ffi::Map<ffi::String, PrimExpr> Apply(const ffi::Array<PrimExpr>& coord,
82 const ffi::Array<PrimExpr>& shape,
83 sym::AnalyzerObj* analyzer) const;
84
87
89 TVM_TIRX_DLL Layout Tile(const TileLayout& outer, const ffi::Array<PrimExpr>& outer_shape,
90 const ffi::Array<PrimExpr>& inner_shape) const;
91
93};
94
95// subscope, scope, iter -> fused_iter
96using FAxisFuser = ffi::TypedFunction<ffi::Optional<Iter>(ThreadScope, ThreadScope, Iter)>;
97
98// Axis
99class AxisObj : public ffi::EnumObj {
100 public:
102 bool IsThreadAxis() const {
103 static ffi::reflection::TypeAttrColumn state_column(ffi::type_attr::kEnumState);
104 static ffi::EnumState state =
105 ffi::Any(state_column[_GetOrAllocRuntimeTypeIndex()]).as_or_throw<ffi::EnumState>();
106 auto column = state->attrs.Get("thread");
107 auto thread = column ? column->Get(ffi::GetRef<ffi::ObjectRef>(this)) : std::nullopt;
108 TVM_FFI_ICHECK(thread.has_value())
109 << "Axis '" << _str_index << "' has no thread classification";
110 return thread->cast<bool>();
111 }
112
114 bool IsMemoryAxis() const { return !IsThreadAxis(); }
115
117 TVM_TIRX_DLL ffi::Optional<ffi::String> GetBackend() const;
118
120 TVM_TIRX_DLL ffi::Optional<ThreadScope> GetScope() const;
121
123 TVM_TIRX_DLL ffi::Optional<ThreadScope> GetSubscope() const;
124
126 TVM_TIRX_DLL ffi::Optional<FAxisFuser> GetFuser() const;
127
128 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.Axis", AxisObj, ffi::EnumObj);
129};
130
131class Axis : public ffi::Enum {
132 public:
134 const ffi::String& name() const { return get()->_str_index; }
135
141 TVM_TIRX_DLL static Axis Get(const ffi::String& name);
142
144};
145
146class IterObj : public ffi::Object {
147 public:
148 explicit IterObj(ffi::UnsafeInit tag) : extent(tag), stride(tag), axis(tag) {}
149
151 : extent(std::move(extent)), stride(std::move(stride)), axis(std::move(axis)) {}
152
156
157 static void RegisterReflection() {
158 namespace refl = tvm::ffi::reflection;
159 refl::ObjectDef<IterObj>()
160 .def_ro("extent", &IterObj::extent)
161 .def_ro("stride", &IterObj::stride)
162 .def_ro("axis", &IterObj::axis);
163 }
164
165 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
166 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.Iter", IterObj, ffi::Object);
167};
168
169class Iter : public ffi::ObjectRef {
170 public:
171 explicit Iter(PrimExpr extent, PrimExpr stride, Axis axis) {
172 auto n = ffi::make_object<IterObj>(std::move(extent), std::move(stride), std::move(axis));
173 data_ = std::move(n);
174 }
176};
177
178class TileLayoutObj : public LayoutObj {
179 public:
180 ffi::Array<Iter> shard;
181 ffi::Array<Iter> replica;
182 ffi::Map<Axis, PrimExpr> offset;
183
184 static void RegisterReflection() {
185 namespace refl = tvm::ffi::reflection;
186 refl::ObjectDef<TileLayoutObj>()
187 .def_ro("shard", &TileLayoutObj::shard)
188 .def_ro("replica", &TileLayoutObj::replica)
189 .def_ro("offset", &TileLayoutObj::offset);
190 }
191
193};
194
195class TileLayout : public Layout {
196 public:
197 explicit TileLayout(ffi::Array<Iter> shard, ffi::Array<Iter> replica,
198 ffi::Map<Axis, PrimExpr> offset)
199 : Layout(ffi::UnsafeInit{}) {
200 auto n = ffi::make_object<TileLayoutObj>();
201 n->shard = std::move(shard);
202 n->replica = std::move(replica);
203 n->offset = std::move(offset);
204 data_ = std::move(n);
205 }
206
208 ffi::Array<PrimExpr> GetShardShape() const {
209 return get()->shard.Map([](const Iter& iter) { return iter->extent; });
210 }
211
219 TVM_TIRX_DLL bool IsContiguous(ffi::Optional<sym::Analyzer> analyzer = std::nullopt) const;
220
222 bool IsTrivial() const {
223 if (get()->shard.size() > 1) return false;
224 if (get()->shard.size() == 1) {
225 if (!get()->shard[0]->axis->IsMemoryAxis() || !prim::IsOne(get()->shard[0]->stride))
226 return false;
227 }
228 return get()->replica.size() == 0 && get()->offset.size() == 0;
229 }
230
232 bool HasThreadAxis() const {
233 return std::any_of(get()->shard.begin(), get()->shard.end(),
234 [](const Iter& iter) { return iter->axis->IsThreadAxis(); });
235 }
236
238 TVM_TIRX_DLL ffi::Optional<Tuple<ThreadScope, ThreadScope>> GetScope() const;
239
241 TVM_TIRX_DLL static TileLayout DefaultLayout(ffi::Array<PrimExpr> shape);
242
245};
246
247// ComposeLayout
249 public:
251 explicit ComposeLayoutObj(ffi::UnsafeInit) : tile_layout(ffi::UnsafeInit{}) {}
252
258 // Cached swizzle masks: inner_mask = (1 << swizzle_len) - 1;
259 // outer_mask = inner_mask << atom_len. Set by the ComposeLayout ctor.
262
263 static void RegisterReflection() {
264 namespace refl = tvm::ffi::reflection;
265 refl::ObjectDef<ComposeLayoutObj>()
266 .def_ro("per_element", &ComposeLayoutObj::per_element)
267 .def_ro("swizzle_len", &ComposeLayoutObj::swizzle_len)
268 .def_ro("atom_len", &ComposeLayoutObj::atom_len)
269 .def_ro("swizzle_inner", &ComposeLayoutObj::swizzle_inner)
270 .def_ro("inner_mask", &ComposeLayoutObj::inner_mask)
271 .def_ro("outer_mask", &ComposeLayoutObj::outer_mask)
272 .def_ro("tile_layout", &ComposeLayoutObj::tile_layout);
273 }
274
276};
277
278class ComposeLayout : public Layout {
279 public:
280 TVM_TIRX_DLL explicit ComposeLayout(int per_element, int swizzle_len, int atom_len,
281 TileLayout tile_layout, bool swizzle_inner = true);
282
285};
286
287} // namespace tirx
288} // namespace tvm
289
290#endif // TVM_TIRX_LAYOUT_H_
Base expr nodes in TVM.
Shared primitive construction and constant helpers.
#define TVM_TIRX_DLL
Definition base.h:55
Algebra expression simplifications.
Typed reference/view over any Expr whose ExprObj::ty is PrimType.
Definition expr.h:187
Coordinated symbolic analyses over an explicitly synchronized premise environment.
Definition analyzer.h:556
Definition layout.h:99
bool IsMemoryAxis() const
Check if the axis is a memory axis.
Definition layout.h:114
TVM_TIRX_DLL ffi::Optional< ffi::String > GetBackend() const
The backend owning this axis, if any.
TVM_TIRX_DLL ffi::Optional< FAxisFuser > GetFuser() const
Get the fuser of the (thread) axis.
TVM_TIRX_DLL ffi::Optional< ThreadScope > GetScope() const
Get the scope of the (thread) axis.
bool IsThreadAxis() const
Check if the axis is a thread axis.
Definition layout.h:102
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.Axis", AxisObj, ffi::EnumObj)
TVM_TIRX_DLL ffi::Optional< ThreadScope > GetSubscope() const
Get the subscope of the (thread) axis.
Definition layout.h:131
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Axis, ffi::Enum, AxisObj)
static TVM_TIRX_DLL Axis Get(const ffi::String &name)
Get or create the axis with the given name.
const ffi::String & name() const
Return the canonical axis name.
Definition layout.h:134
Definition layout.h:248
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.ComposeLayout", ComposeLayoutObj, LayoutObj)
int per_element
Definition layout.h:253
TileLayout tile_layout
Definition layout.h:257
int swizzle_len
Definition layout.h:254
ComposeLayoutObj(TileLayout tile_layout)
Definition layout.h:250
int atom_len
Definition layout.h:255
static void RegisterReflection()
Definition layout.h:263
int inner_mask
Definition layout.h:260
int outer_mask
Definition layout.h:261
bool swizzle_inner
Definition layout.h:256
ComposeLayoutObj(ffi::UnsafeInit)
Definition layout.h:251
Definition layout.h:278
TVM_DEFINE_OBJECT_REF_COW_METHOD(ComposeLayoutObj)
TVM_TIRX_DLL ComposeLayout(int per_element, int swizzle_len, int atom_len, TileLayout tile_layout, bool swizzle_inner=true)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ComposeLayout, Layout, ComposeLayoutObj)
Definition layout.h:146
static void RegisterReflection()
Definition layout.h:157
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition layout.h:165
PrimExpr extent
Definition layout.h:153
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.Iter", IterObj, ffi::Object)
IterObj(PrimExpr extent, PrimExpr stride, Axis axis)
Definition layout.h:150
Axis axis
Definition layout.h:155
PrimExpr stride
Definition layout.h:154
IterObj(ffi::UnsafeInit tag)
Definition layout.h:148
Definition layout.h:169
Iter(PrimExpr extent, PrimExpr stride, Axis axis)
Definition layout.h:171
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Iter, ffi::ObjectRef, IterObj)
Base object for tensor-program coordinate layouts.
Definition layout.h:50
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition layout.h:52
TVM_FFI_DECLARE_OBJECT_INFO("tirx.Layout", LayoutObj, ffi::Object)
Managed reference to a tensor-program coordinate layout.
Definition layout.h:57
TVM_TIRX_DLL Layout Tile(const TileLayout &outer, const ffi::Array< PrimExpr > &outer_shape, const ffi::Array< PrimExpr > &inner_shape) const
Tile the current layout with a given layout.
TVM_TIRX_DLL ffi::Map< ffi::String, PrimExpr > Apply(ffi::Array< PrimExpr > coord, sym::AnalyzerObj *analyzer) const
Apply a layout using caller-owned analysis state.
TVM_TIRX_DLL ffi::Map< ffi::String, PrimExpr > Apply(const ffi::Array< PrimExpr > &coord, const ffi::Array< PrimExpr > &shape, sym::AnalyzerObj *analyzer) const
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Layout, ffi::ObjectRef, LayoutObj)
TVM_TIRX_DLL Layout Canonicalize() const
Turn the layout to canonical form.
TVM_TIRX_DLL PrimExpr GetSpan(ffi::Optional< ffi::String > axis_name=std::nullopt) const
Get the span of the layout (of some axis)
TVM_TIRX_DLL PrimExpr GetSize(ffi::Optional< ffi::String > axis_name=std::nullopt) const
Get the size of the layout (of some axis)
TVM_TIRX_DLL void CheckWellFormed() const
Verify if the layout is well-formed.
TVM_TIRX_DLL ffi::Map< ffi::String, PrimExpr > Apply(PrimExpr coord, sym::AnalyzerObj *analyzer) const
Canonical thread-scope identity, named as backend.scope.
Definition thread_scope.h:55
Definition layout.h:178
ffi::Array< Iter > replica
Definition layout.h:181
static void RegisterReflection()
Definition layout.h:184
ffi::Array< Iter > shard
Definition layout.h:180
ffi::Map< Axis, PrimExpr > offset
Definition layout.h:182
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.TileLayout", TileLayoutObj, LayoutObj)
Definition layout.h:195
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TileLayout, Layout, TileLayoutObj)
static TVM_TIRX_DLL TileLayout DefaultLayout(ffi::Array< PrimExpr > shape)
Get the default layout for the shape.
bool HasThreadAxis() const
Has Thread Axis.
Definition layout.h:232
bool IsTrivial() const
Is the layout trivial (pure memory, identical mapping)
Definition layout.h:222
TileLayout(ffi::Array< Iter > shard, ffi::Array< Iter > replica, ffi::Map< Axis, PrimExpr > offset)
Definition layout.h:197
TVM_TIRX_DLL ffi::Optional< Tuple< ThreadScope, ThreadScope > > GetScope() const
Get the scope pair of the layout.
ffi::Array< PrimExpr > GetShardShape() const
Get the shape of the shard.
Definition layout.h:208
TVM_DEFINE_OBJECT_REF_COW_METHOD(TileLayoutObj)
TVM_TIRX_DLL bool IsContiguous(ffi::Optional< sym::Analyzer > analyzer=std::nullopt) const
Whether the tile describes contiguous row-major memory strides.
IRModule that holds the functions and type definitions.
bool IsOne(const PrimExpr &x)
Check whether x is a constant integer 1.
Definition op.h:1044
ffi::TypedFunction< ffi::Optional< Iter >(ThreadScope, ThreadScope, Iter)> FAxisFuser
Definition layout.h:96
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
Enum identities for backend-defined thread hierarchies.