tvm
Loading...
Searching...
No Matches
tile_primitive.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_TILE_PRIMITIVE_H_
24#define TVM_TIRX_TILE_PRIMITIVE_H_
25
26#include <tvm/ffi/object.h>
27#include <tvm/ir/op.h>
28#include <tvm/target/target.h>
29#include <tvm/tirx/exec_scope.h>
30#include <tvm/tirx/stmt.h>
31#include <tvm/tirx/var.h>
32
33namespace tvm {
34namespace tirx {
35
45class LambdaExprNode : public ffi::Object {
46 public:
51
53 PrimExpr Apply(const Array<PrimExpr>& indices) const;
54
55 static void RegisterReflection() {
56 namespace refl = tvm::ffi::reflection;
57 refl::ObjectDef<LambdaExprNode>()
58 .def_ro("vars", &LambdaExprNode::vars, refl::AttachFieldFlag::SEqHashDefRecursive())
59 .def_ro("pred", &LambdaExprNode::pred);
60 }
61
63 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.LambdaExpr", LambdaExprNode, ffi::Object);
64};
65
70class LambdaExpr : public ffi::ObjectRef {
71 public:
72 explicit LambdaExpr(Array<Var> vars, PrimExpr pred);
73
75};
76
82using FArgSanitizer = ffi::TypedFunction<void(tvm::Op, ffi::Array<ffi::ObjectRef>)>;
83
84namespace callback {
86constexpr const char* kPrivateAlloc = "private_alloc";
90constexpr const char* kDeviceInitStmt = "device_init_stmt";
94constexpr const char* kHostInitStmt = "host_init_stmt";
98constexpr const char* kPostBufferDefStmt = "post_buffer_def_stmt";
99} // namespace callback
100
104class DispatchContextNode : public ffi::Object {
105 public:
111 ffi::Map<ffi::String, IterVar> launch_params;
113 ffi::Map<Var, Range> var_range_map;
117 ffi::Map<ffi::String, ffi::ObjectRef> callbacks;
119 ffi::Map<ffi::String, ffi::ObjectRef> shared_state;
129 ffi::Map<ffi::String, ffi::Array<PrimExpr>> inter;
131 ffi::Map<ffi::String, ffi::Array<PrimExpr>> intra;
133 ffi::String scope_kind;
134
135 static void RegisterReflection() {
136 namespace refl = tvm::ffi::reflection;
137 refl::ObjectDef<DispatchContextNode>()
138 .def_ro("target", &DispatchContextNode::target)
139 .def_ro("exec_scope", &DispatchContextNode::exec_scope)
140 .def_ro("launch_params", &DispatchContextNode::launch_params)
141 .def_ro("var_range_map", &DispatchContextNode::var_range_map)
142 .def_ro("alloc_only", &DispatchContextNode::alloc_only)
143 .def_ro("callbacks", &DispatchContextNode::callbacks)
144 .def_ro("shared_state", &DispatchContextNode::shared_state)
145 .def_ro("inter", &DispatchContextNode::inter)
146 .def_ro("intra", &DispatchContextNode::intra)
147 .def_ro("scope_kind", &DispatchContextNode::scope_kind);
148 }
149
152
154 void AddInitStmt(Stmt stmt, bool host = false);
155
158
160 void SharedStateSet(ffi::String key, ffi::ObjectRef value);
161
163 ffi::Optional<ffi::ObjectRef> SharedStateGet(ffi::String key);
164
165 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.DispatchContext", DispatchContextNode, ffi::Object);
166};
167
171class DispatchContext : public ffi::ObjectRef {
172 public:
174 ffi::Map<ffi::String, IterVar> launch_params = {},
175 ffi::Map<Var, Range> var_range_map = {}, bool alloc_only = false,
176 ffi::Map<ffi::String, ffi::ObjectRef> callbacks = {},
177 ffi::Map<ffi::String, ffi::ObjectRef> shared_state = {},
178 ffi::Map<ffi::String, ffi::Array<PrimExpr>> inter = {},
179 ffi::Map<ffi::String, ffi::Array<PrimExpr>> intra = {},
180 ffi::String scope_kind = "");
181
183};
184
189 public:
190 explicit TilePrimitiveCallNode(ffi::UnsafeInit tag) : op(tag) {}
191
192 TilePrimitiveCallNode(tvm::Op op, ffi::Array<ffi::Any> args,
193 ffi::Map<ffi::String, BufferVar> workspace,
194 ffi::Map<ffi::String, ffi::Any> config, ffi::Optional<ffi::String> dispatch,
196 : op(std::move(op)),
197 args(std::move(args)),
198 workspace(std::move(workspace)),
199 config(std::move(config)),
200 dispatch(std::move(dispatch)),
201 scope(std::move(scope)) {}
202
203 // tvm::Op which corresponds to the TIRX operator.
205
206 // Arguments to the operator.
207 ffi::Array<ffi::Any> args;
208
209 // Workspace (pre-allocated buffers) for the operator.
210 ffi::Map<ffi::String, BufferVar> workspace;
211
212 // Config for the operator/scheduler.
213 ffi::Map<ffi::String, ffi::Any> config;
214
215 // Optional dispatch variant name registered via @register_dispatch.
216 ffi::Optional<ffi::String> dispatch{std::nullopt};
217
218 // Cooperation scope of this call. Default thread (an unscoped call).
220
221 static void RegisterReflection() {
222 namespace refl = tvm::ffi::reflection;
223 refl::ObjectDef<TilePrimitiveCallNode>()
224 .def_ro("op", &TilePrimitiveCallNode::op)
225 .def_ro("args", &TilePrimitiveCallNode::args)
226 .def_ro("workspace", &TilePrimitiveCallNode::workspace)
227 .def_ro("config", &TilePrimitiveCallNode::config)
228 .def_ro("dispatch", &TilePrimitiveCallNode::dispatch)
229 .def_ro("scope", &TilePrimitiveCallNode::scope);
230 }
231
233};
234
239class TilePrimitiveCall : public Stmt {
240 public:
241 TVM_DLL TilePrimitiveCall(tvm::Op op, ffi::Array<ffi::Any> args,
242 ffi::Map<ffi::String, BufferVar> workspace = {},
243 ffi::Map<ffi::String, ffi::Any> config = {},
244 ffi::Optional<ffi::String> dispatch = std::nullopt,
245 ExecScope scope = ExecScope(ScopeKind::kThread));
246
247 static bool IsValidOpCallArgType(const ffi::Any& arg);
248
251};
252
258TVM_DLL const Op& cast();
259
265TVM_DLL const Op& copy();
266
273
279TVM_DLL const Op& fill();
280
286TVM_DLL const Op& gemm();
287
295
296TVM_DLL const Op& zero();
297
298TVM_DLL const Op& sqrt();
299
300TVM_DLL const Op& exp();
301
302TVM_DLL const Op& exp2();
303
304TVM_DLL const Op& add();
305
306TVM_DLL const Op& sub();
307
308TVM_DLL const Op& mul();
309
310TVM_DLL const Op& fdiv();
311
313
315
317
318TVM_DLL const Op& sum();
319
320TVM_DLL const Op& max();
321
322TVM_DLL const Op& min();
323
325
327
329
331
333
335
336TVM_DLL const Op& fma();
337
338TVM_DLL const Op& silu();
339
341
343
344} // namespace tirx
345} // namespace tvm
346
347#endif // TVM_TIRX_TILE_PRIMITIVE_H_
Managed reference class to OpNode.
Definition op.h:163
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:401
Managed reference class to TargetNode.
Definition target.h:134
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Checked zero-state view over an ordinary VarNode with BufferType.
Definition buffer.h:179
The context information of the kernel required by op dispatch.
Definition tile_primitive.h:104
bool alloc_only
Whether the dispatch context is only used for buffer allocation.
Definition tile_primitive.h:115
ffi::Map< ffi::String, ffi::ObjectRef > shared_state
Shared state that persists across dispatch calls within a single lowering pass.
Definition tile_primitive.h:119
ffi::Map< ffi::String, ffi::ObjectRef > callbacks
Callback to be handled when the operator is scheduled.
Definition tile_primitive.h:117
ExecScope exec_scope
The exec scope of the operator.
Definition tile_primitive.h:109
static void RegisterReflection()
Definition tile_primitive.h:135
ffi::Map< ffi::String, IterVar > launch_params
The kernel launch parameters.
Definition tile_primitive.h:111
ffi::String scope_kind
Scope kind string ("kernel"/"cta"/"warpgroup"/"warp"/"thread"/"cluster").
Definition tile_primitive.h:133
Target target
The target of the kernel.
Definition tile_primitive.h:107
void AddPostBufferDefStmt(BufferVar buffer, Stmt stmt)
Add a statement to be inserted after a buffer's definition.
void SharedStateSet(ffi::String key, ffi::ObjectRef value)
Set a value in the shared state cache.
ffi::Map< ffi::String, ffi::Array< PrimExpr > > inter
ExecContext inter-team view at this op site.
Definition tile_primitive.h:129
ffi::Map< Var, Range > var_range_map
A map from loop variables to their ranges.
Definition tile_primitive.h:113
ffi::Optional< ffi::ObjectRef > SharedStateGet(ffi::String key)
Get a value from the shared state cache.
ffi::Map< ffi::String, ffi::Array< PrimExpr > > intra
ExecContext intra-team view. Same encoding as inter.
Definition tile_primitive.h:131
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.DispatchContext", DispatchContextNode, ffi::Object)
void AddAllocBuffer(BufferVar buffer)
Add a buffer to be allocated in the kernel.
void AddInitStmt(Stmt stmt, bool host=false)
Add an initialization statement to be inserted.
Managed reference to DispatchContextNode.
Definition tile_primitive.h:171
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(DispatchContext, ffi::ObjectRef, DispatchContextNode)
DispatchContext(Target target, ExecScope exec_scope, ffi::Map< ffi::String, IterVar > launch_params={}, ffi::Map< Var, Range > var_range_map={}, bool alloc_only=false, ffi::Map< ffi::String, ffi::ObjectRef > callbacks={}, ffi::Map< ffi::String, ffi::ObjectRef > shared_state={}, ffi::Map< ffi::String, ffi::Array< PrimExpr > > inter={}, ffi::Map< ffi::String, ffi::Array< PrimExpr > > intra={}, ffi::String scope_kind="")
Definition exec_scope.h:228
A reified Python lambda: a list of bound variables and a body over them.
Definition tile_primitive.h:45
Array< Var > vars
The bound variables of the lambda.
Definition tile_primitive.h:48
PrimExpr pred
The lambda body over vars.
Definition tile_primitive.h:50
PrimExpr Apply(const Array< PrimExpr > &indices) const
Replace the bound variables with the given indices, returning the substituted body.
static void RegisterReflection()
Definition tile_primitive.h:55
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.LambdaExpr", LambdaExprNode, ffi::Object)
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition tile_primitive.h:62
Managed reference to LambdaExprNode.
Definition tile_primitive.h:70
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(LambdaExpr, ffi::ObjectRef, LambdaExprNode)
LambdaExpr(Array< Var > vars, PrimExpr pred)
Base node of all statements.
Definition stmt.h:43
Container of all statements.
Definition stmt.h:67
TIRX TilePrimitiveCall stmt.
Definition tile_primitive.h:188
ffi::Optional< ffi::String > dispatch
Definition tile_primitive.h:216
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.TilePrimitiveCall", TilePrimitiveCallNode, StmtNode)
TilePrimitiveCallNode(ffi::UnsafeInit tag)
Definition tile_primitive.h:190
ffi::Array< ffi::Any > args
Definition tile_primitive.h:207
tvm::Op op
Definition tile_primitive.h:204
TilePrimitiveCallNode(tvm::Op op, ffi::Array< ffi::Any > args, ffi::Map< ffi::String, BufferVar > workspace, ffi::Map< ffi::String, ffi::Any > config, ffi::Optional< ffi::String > dispatch, ExecScope scope)
Definition tile_primitive.h:192
static void RegisterReflection()
Definition tile_primitive.h:221
ffi::Map< ffi::String, BufferVar > workspace
Definition tile_primitive.h:210
ExecScope scope
Definition tile_primitive.h:219
ffi::Map< ffi::String, ffi::Any > config
Definition tile_primitive.h:213
Managed reference to TilePrimitiveCallNode.
Definition tile_primitive.h:239
TVM_DEFINE_OBJECT_REF_COW_METHOD(TilePrimitiveCallNode)
TilePrimitiveCall(tvm::Op op, ffi::Array< ffi::Any > args, ffi::Map< ffi::String, BufferVar > workspace={}, ffi::Map< ffi::String, ffi::Any > config={}, ffi::Optional< ffi::String > dispatch=std::nullopt, ExecScope scope=ExecScope(ScopeKind::kThread))
static bool IsValidOpCallArgType(const ffi::Any &arg)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TilePrimitiveCall, Stmt, TilePrimitiveCallNode)
Primitive operators(builtin intrinsics) and registry for them.
constexpr const char * kPrivateAlloc
The buffers allocated by the operator.
Definition tile_primitive.h:86
constexpr const char * kPostBufferDefStmt
Statements to be inserted after a specific buffer's definition (DeclBuffer/AllocBuffer)....
Definition tile_primitive.h:98
constexpr const char * kDeviceInitStmt
The initialization statement of the operator. which will be inserted at the beginning of the kernel.
Definition tile_primitive.h:90
constexpr const char * kHostInitStmt
The initialization statement of the operator. which will be inserted at the beginning of the kernel.
Definition tile_primitive.h:94
const Op & copy_async()
See pesudo code below:
const Op & sub()
const Op & sqrt()
const Op & minimum()
const Op & max()
const Op & compose_op()
const Op & maximum()
const Op & cast()
See pesudo code below:
const Op & mul()
const Op & fdiv()
const Op & select()
const Op & binary_chain()
const Op & zero()
const Op & reduce_negate()
const Op & gemm()
See pesudo code below:
const Op & add()
const Op & gemm_async()
See pesudo code below:
const Op & fill()
See pesudo code below:
const Op & binary_reduce()
const Op & silu()
const Op & min()
const Op & exp()
ffi::TypedFunction< void(tvm::Op, ffi::Array< ffi::ObjectRef >)> FArgSanitizer
The type of the function that sanitizes the arguments of a TIRX operator.
Definition tile_primitive.h:82
const Op & sum()
const Op & fma()
const Op & copy()
See pesudo code below:
const Op & unary_reduce()
const Op & permute_layout()
const Op & exp2()
const Op & reciprocal()
const Op & memset()
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
Compilation target object.
TIR statements.
Variables in the TIR.