layout.h 7.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*!
 * \file Layout.h
 *
 */

#ifndef TVM_TL_LAYOUT_LAYOUT_H_
#define TVM_TL_LAYOUT_LAYOUT_H_

#include <tvm/arith/analyzer.h>
10
#include <tvm/arith/iter_affine_map.h>
11
#include <tvm/ffi/object.h>
12
#include <utility>
13

14
15
#include "../support/ffi_aliases.h"

16
17
18
19
20
21
22
23
24
namespace tvm {
namespace tl {

using namespace tir;

class Layout;
class Fragment;

class LayoutNode : public Object {
25
public:
26
27
28
29
30
31
32
33
34
35
36
37
38
  LayoutNode() = default;
  LayoutNode(Array<PrimExpr> input_size, Array<PrimExpr> forward_index);

  size_t InputDim() const { return input_size_.size(); }

  size_t OutputDim() const { return forward_index_.size(); }

  Array<PrimExpr> InputShape() const { return input_size_; }

  Array<PrimExpr> OutputShape() const;

  Array<PrimExpr> GetForwardIndex() const { return forward_index_; }

39
40
  virtual Array<PrimExpr> GetForwardVars() const;

41
  virtual Array<PrimExpr> Forward(const Array<PrimExpr> &vars) const;
42
43

  virtual Layout Inverse() const;
44
  virtual std::pair<Layout, arith::IterMapLevel> InverseWithLevel() const;
45

46
47
48
  virtual std::string DebugOutput() const;

  virtual bool IsEqual(const LayoutNode *other, bool skip_index = false) const;
49

50
  static void RegisterReflection();
51
52
53
  TVM_FFI_DECLARE_OBJECT_INFO("tl.Layout", LayoutNode, Object);
  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind =
      kTVMFFISEqHashKindTreeNode;
54

55
protected:
56
  virtual Map<Var, Range> getVarMap() const;
57
  void UpdateAnalyzer(arith::Analyzer *analyzer) const;
58
59
60
61
62
63
64
65
  Array<PrimExpr> forward_index_;
  Array<PrimExpr> input_size_;
};

/*!
 * \brief Layout reference class.
 */
class Layout : public ObjectRef {
66
public:
67
68
69
  TVM_DLL Layout(Array<IterVar> forward_var, Array<PrimExpr> forward_index);
  TVM_DLL Layout(Array<PrimExpr> input_size, Array<PrimExpr> forward_index);

70
  TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Layout, ObjectRef, LayoutNode);
71
72
73
};

class FragmentNode : public LayoutNode {
74
public:
75
  FragmentNode() = default;
76
77
  FragmentNode(Array<PrimExpr> input_size, Array<PrimExpr> forward_index,
               PrimExpr forward_thread, PrimExpr replicate_size);
78
79
80

  PrimExpr GetForwardThread() const { return forward_thread_; }

81
82
  Array<PrimExpr> GetForwardVars() const final;

83
  Layout Inverse() const final;
84
  std::pair<Layout, arith::IterMapLevel> InverseWithLevel() const final;
85
86
87
88
89

  PrimExpr ThreadExtent() const;

  PrimExpr ReplicateExtent() const { return replicate_size_; };

90
91
  PrimExpr ForwardThread(const Array<PrimExpr> &vars,
                         const Optional<PrimExpr> &rep_var) const;
92

93
  Fragment Repeat(const Array<PrimExpr> &repeats, bool repeat_on_thread,
94
95
96
97
98
99
100
101
                  bool lower_dim_first = true) const;

  Fragment Replicate(int repeats) const;

  Fragment DeReplicate() const;

  Fragment CondenseReplicateVar() const;

102
103
  std::string DebugOutput() const final;

104
  Fragment BindThreadRange(Range thread_range) const;
105
106
107

  Range ThreadRange() const { return thread_range_; }

108
  bool IsEqual(const FragmentNode *other, bool skip_index = false) const;
109

110
111
  bool IsCompletedReplicated() const;

112
  static void RegisterReflection();
113

114
115
116
  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tl.Fragment", FragmentNode, LayoutNode);
  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind =
      kTVMFFISEqHashKindTreeNode;
117

118
protected:
119
  Map<Var, Range> getVarMap() const final;
120
  Range thread_range_;
121
122
123
124
125
126
127
128
  PrimExpr forward_thread_;
  PrimExpr replicate_size_;
};

/*!
 * \brief Fragment reference class.
 */
class Fragment : public Layout {
129
public:
130
131
132
133
  TVM_DLL Fragment(Array<IterVar> forward_var, Array<PrimExpr> forward_index,
                   PrimExpr forward_thread, IterVar thread_replicate);

  TVM_DLL Fragment(Array<PrimExpr> input_size, Array<PrimExpr> forward_index,
134
135
                   PrimExpr forward_thread, PrimExpr replicate_size,
                   Optional<Var> replicate_var);
136

137
  TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Fragment, Layout, FragmentNode);
138
139
140
141
};

Var InputPlaceholder(size_t idx);
Var ReplicationPlaceholder();
142
IterVar make_itervar(std::string name, PrimExpr dom);
143
144
145

Fragment makeGemmFragment8x8();
Fragment makeGemmFragment8x8Transposed();
146
147
Fragment makeGemmFragmentC(const int block_m, const int block_n,
                           const int warp_m, const int warp_n,
148
                           const int element_size);
149
150
151
Fragment makeGemmSparseFragmentC(const int block_m, const int block_n,
                                 const int warp_m, const int warp_n,
                                 const int element_size);
152
153
154
155
156
157
158
159
Fragment makeGemmFragmentCCDNA(const int block_m, const int block_n,
                               const int warp_m, const int warp_n,
                               const int element_size);
Fragment makeGemmFragmentCHopper(const int block_m, const int block_n,
                                 const int warp_m, const int warp_n,
                                 const int element_size);
Fragment makeGemmFragmentA(const int block_m, const int block_n,
                           const int block_k, const int warp_m,
160
161
                           const int warp_n, const int element_size,
                           bool transposed = false);
162
163
Fragment makeGemmFragmentB(const int block_m, const int block_n,
                           const int block_k, const int warp_m,
164
                           const int warp_n, bool transposed = false);
165
166
167

Fragment makeGemmFragmentACDNA(const int block_m, const int block_n,
                               const int block_k, const int warp_m,
168
                               const int warp_n, const int element_size,
169
                               const int k_pack, bool transposed = false);
170
171
172
173

// Default Memory Layout
Layout makeGemmLayoutLinear(int stride, int continuous);
Layout makeGemmABLayoutPadded(int stride, int continuous, int element_size);
174
Layout makeGemmABLayout(int mat_stride, int mat_continuous, int continuity,
175
                        int element_size, bool k_inner = true);
176
Layout makeGemmABLayoutHopper(int mat_stride, int mat_continuous,
177
178
                              int continuity, int element_size,
                              bool k_inner = true);
179
Layout makeGemmABLayoutSm100(int mat_stride, int mat_continuous, int continuity,
180
                             int element_size, bool k_inner = true);
181
Layout makeGemmABLayoutCDNA(int stride, int continuous, int element_size,
182
                            int kPack);
183
184
185
186
187
188
189
190

Fragment makeGemmVoltaFragmentC(const int block_m, const int block_n,
                                const int warp_m, const int warp_n,
                                const int element_size);
Fragment makeGemmVoltaFragmentA(const int block_m, const int block_n,
                                const int block_k, const int warp_m,
                                const int warp_n);
Layout makeGemmVoltaABLayout(int stride, int continuous, bool is_a,
191
                             bool k_inner = true);
192

193
194
195
196
197
Layout makeTensorOpMultiplicand(int mat_stride, int mat_continuous,
                                int elementsize, int crosswise);
Layout makeGemmSparseAmpereABLayout(int mat_stride, int mat_continuous,
                                    int elementsize);

198
199
Layout makeFullBankSwizzleLayout(int stride, int continuous, int element_size);
Layout makeHalfBankSwizzleLayout(int stride, int continuous, int element_size);
200
201
Layout makeQuarterBankSwizzleLayout(int stride, int continuous,
                                    int element_size);
202
203
204

namespace attr {
// BlockAttr, Containing the layout for all the buffers in the block
205
206
constexpr const char *kLayoutMap = "layout_map";
} // namespace attr
207

208
209
} // namespace tl
} // namespace tvm
210

211
#endif // TVM_TL_LAYOUT_LAYOUT_H_