tvm
Loading...
Searching...
No Matches
transform.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 */
19
24#ifndef TVM_RELAX_TRANSFORM_H_
25#define TVM_RELAX_TRANSFORM_H_
26
27#include <tvm/base.h>
28#include <tvm/ffi/reflection/registry.h>
29#include <tvm/ir/transform.h>
31#include <tvm/relax/expr.h>
32#include <tvm/tirx/function.h>
33#include <tvm/tirx/index_map.h>
34
35namespace tvm {
36namespace relax {
37namespace transform {
38
45using LayoutCb = ffi::TypedFunction<ffi::Map<ffi::String, ffi::Array<ffi::String>>(Call)>;
46
56TVM_DLL Pass CreateFunctionPass(std::function<Function(Function, IRModule, PassContext)> pass_func,
57 int opt_level, ffi::String name);
58
69 std::function<DataflowBlock(DataflowBlock, IRModule, PassContext)> pass_func, int opt_level,
70 ffi::String name);
71
77TVM_DLL Pass LambdaLift();
78
85
98
105
119
142
149
156TVM_DLL Pass Normalize();
157
166
179
187TVM_DLL Pass EliminateCommonSubexpr(bool call_only = false);
188
197TVM_DLL Pass BindParams(ffi::String func_name, ffi::Map<Any, ffi::ObjectRef> params);
198
214TVM_DLL Pass BindSymbolicVars(ffi::Map<ffi::Variant<PrimVar, ffi::String>, PrimExpr> binding_map,
215 ffi::Optional<ffi::String> func_name = std::nullopt);
216
225
251TVM_DLL Pass LegalizeOps(ffi::Optional<ffi::Map<ffi::String, ffi::Function>> cmap,
252 ffi::Optional<ffi::Array<ffi::String>> skip_ops,
253 bool enable_warning = false, ffi::Optional<Target> target = std::nullopt);
254
260
272
281
308TVM_DLL Pass
309LiftTransformParams(ffi::Variant<bool, ffi::Array<ffi::String>> shared_transform = false);
310
317TVM_DLL Pass UpdateVDevice(VDevice new_vdevice, int64_t index);
318
324
330
336
345
357TVM_DLL Pass FuseOps(int fuse_opt_level = -1);
358
364class FusionPatternObj : public ffi::Object {
365 public:
366 explicit FusionPatternObj(DFPattern value) : pattern(std::move(value)) {}
367 explicit FusionPatternObj(ffi::UnsafeInit) : pattern(ffi::UnsafeInit{}) {}
368
373 ffi::String name;
374
380
385 ffi::Map<ffi::String, DFPattern> annotation_patterns;
386
394 ffi::Optional<ffi::Function> check;
395
402 ffi::Optional<ffi::Function> attrs_getter;
403
404 static void RegisterReflection() {
405 namespace refl = tvm::ffi::reflection;
406 refl::ObjectDef<FusionPatternObj>()
407 .def_ro("name", &FusionPatternObj::name)
408 .def_ro("pattern", &FusionPatternObj::pattern)
409 .def_ro("annotation_patterns", &FusionPatternObj::annotation_patterns)
410 .def_ro("check", &FusionPatternObj::check)
411 .def_ro("attrs_getter", &FusionPatternObj::attrs_getter);
412 }
413 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.transform.FusionPattern", FusionPatternObj, ffi::Object);
414};
415
416class FusionPattern : public ffi::ObjectRef {
417 public:
418 FusionPattern(ffi::String name, DFPattern pattern,
419 ffi::Map<ffi::String, DFPattern> annotation_patterns,
420 ffi::Optional<ffi::Function> check, ffi::Optional<ffi::Function> attrs_getter);
421
422 FusionPattern(ffi::String name, DFPattern pattern)
423 : FusionPattern(name, pattern, {}, std::nullopt, std::nullopt) {}
424
426};
427
431class PatternCheckContextObj : public ffi::Object {
432 public:
434 explicit PatternCheckContextObj(ffi::UnsafeInit) : matched_expr(ffi::UnsafeInit{}) {}
435
440
445 ffi::Map<ffi::String, Expr> annotated_expr;
446
451 ffi::Map<Var, Expr> matched_bindings;
452
457 ffi::Map<Var, ffi::Array<Var>> var_usages;
458
463 ffi::Map<Expr, Var> value_to_bound_var;
464
465 static void RegisterReflection() {
466 namespace refl = tvm::ffi::reflection;
467 refl::ObjectDef<PatternCheckContextObj>()
468 .def_ro("matched_expr", &PatternCheckContextObj::matched_expr)
469 .def_ro("annotated_expr", &PatternCheckContextObj::annotated_expr)
470 .def_ro("matched_bindings", &PatternCheckContextObj::matched_bindings)
471 .def_ro("var_usages", &PatternCheckContextObj::var_usages)
472 .def_ro("value_to_bound_var", &PatternCheckContextObj::value_to_bound_var);
473 }
474 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.transform.PatternCheckContext", PatternCheckContextObj,
475 ffi::Object);
476};
477
478class PatternCheckContext : public ffi::ObjectRef {
479 public:
480 PatternCheckContext(Expr matched_expr, ffi::Map<ffi::String, Expr> annotated_expr,
481 ffi::Map<Var, Expr> matched_bindings,
482 ffi::Map<Var, ffi::Array<Var>> var_usages,
483 ffi::Map<Expr, Var> value_to_bound_var);
484
487};
488
514TVM_DLL Pass Gradient(ffi::String func_name,
515 ffi::Optional<ffi::Array<Var>> require_grads = std::nullopt,
516 int target_index = 0);
517
538TVM_DLL Pass FuseOpsByPattern(const tvm::ffi::Array<FusionPattern>& patterns,
539 bool bind_constants = true, bool annotate_codegen = false,
540 const tvm::ffi::Array<ffi::String>& entry_function_names = {});
541
550
557TVM_DLL Pass FuseTIR();
558
565TVM_DLL Pass
566RunCodegen(ffi::Optional<ffi::Map<ffi::String, ffi::Map<ffi::String, ffi::Any>>> target_options,
567 ffi::Array<ffi::String> entry_functions);
568
577TVM_DLL Pass DecomposeOpsForInference(ffi::Optional<ffi::String> func_name);
578
587TVM_DLL Pass DecomposeOpsForTraining(ffi::Optional<ffi::String> func_name);
588
602 const ffi::Map<ffi::String, tirx::Function>& op_impl_map,
603 const ffi::Map<ffi::String, ffi::Array<ffi::Optional<tirx::IndexMap>>>& op_tensor_transforms);
604
612TVM_DLL Pass ConvertLayout(ffi::Map<ffi::String, ffi::Array<ffi::String>> desired_layouts,
613 LayoutCb layout_cb);
614
622TVM_DLL Pass ConvertToDataflow(int min_size = 2);
623
640TVM_DLL Pass DeadCodeElimination(ffi::Array<ffi::String> entry_functions = {});
641
652
664 DLDataType out_dtype, ffi::Optional<ffi::Array<ffi::String>> fp16_input_names = std::nullopt);
665
672
679
680} // namespace transform
681} // namespace relax
682} // namespace tvm
683
684#endif // TVM_RELAX_TRANSFORM_H_
Utilities for IR transformations.
TIR Function.
Managed reference to CallObj.
Definition expr.h:825
Managed reference to ExprObj.
Definition expr.h:101
Managed reference class to IRModuleObj.
Definition module.h:292
Typed reference/view over any Expr whose ExprObj::ty is PrimType.
Definition expr.h:187
Managed reference to VarObj.
Definition expr.h:571
Managed reference to dataflow patterns.
Definition dataflow_pattern.h:103
Definition expr.h:250
Definition expr.h:417
Managed reference to VDeviceObj.
Definition global_info.h:68
The pattern object used as the input of FuseOpsByPattern. For bindings to be fused,...
Definition transform.h:364
ffi::Optional< ffi::Function > attrs_getter
The function to get attributes for fused function.
Definition transform.h:402
ffi::Optional< ffi::Function > check
The function to determine whether the match result is accepted. This can be std::nullopt if check fun...
Definition transform.h:394
DFPattern pattern
The dataflow pattern that will be used to match expression in the DataflowBlock. All the call nodes c...
Definition transform.h:379
FusionPatternObj(ffi::UnsafeInit)
Definition transform.h:367
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.transform.FusionPattern", FusionPatternObj, ffi::Object)
FusionPatternObj(DFPattern value)
Definition transform.h:366
ffi::String name
The name of pattern. It becomes the value of the kComposite attribute of a fused function after succe...
Definition transform.h:373
static void RegisterReflection()
Definition transform.h:404
ffi::Map< ffi::String, DFPattern > annotation_patterns
The map which is used to extract important expressions from the pattern match result....
Definition transform.h:385
Definition transform.h:416
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(FusionPattern, ffi::ObjectRef, FusionPatternObj)
FusionPattern(ffi::String name, DFPattern pattern)
Definition transform.h:422
FusionPattern(ffi::String name, DFPattern pattern, ffi::Map< ffi::String, DFPattern > annotation_patterns, ffi::Optional< ffi::Function > check, ffi::Optional< ffi::Function > attrs_getter)
The input of FusionPattern::check.
Definition transform.h:431
static void RegisterReflection()
Definition transform.h:465
PatternCheckContextObj(ffi::UnsafeInit)
Definition transform.h:434
ffi::Map< Var, ffi::Array< Var > > var_usages
A map mapping variable definitions to a set of uses. It has all variables used in the function.
Definition transform.h:457
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.transform.PatternCheckContext", PatternCheckContextObj, ffi::Object)
ffi::Map< Expr, Var > value_to_bound_var
Map from value to its bound variable. It doesn't have variables after the matched expression.
Definition transform.h:463
ffi::Map< ffi::String, Expr > annotated_expr
A map which contains all expressions matched by the sub patterns in FusionPattern::annotation_pattern...
Definition transform.h:445
ffi::Map< Var, Expr > matched_bindings
Map from variable to its value. It contains variables from bindings that is being fused by FuseOpsByP...
Definition transform.h:451
PatternCheckContextObj(Expr matched_expr)
Definition transform.h:433
Expr matched_expr
The expression that's matched with the FusionPattern::pattern.
Definition transform.h:439
PatternCheckContext(Expr matched_expr, ffi::Map< ffi::String, Expr > annotated_expr, ffi::Map< Var, Expr > matched_bindings, ffi::Map< Var, ffi::Array< Var > > var_usages, ffi::Map< Expr, Var > value_to_bound_var)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(PatternCheckContext, ffi::ObjectRef, PatternCheckContextObj)
PassContext that is used to configure the pass behavior.
Definition transform.h:64
Managed reference class for PassInfoObj.
Definition transform.h:255
Definition transform.h:307
A pattern language for matching dataflow properties.
Parent compiler symbol visibility.
Defines a remapping of tensor indices.
Pass RealizeVDevice()
Propagate virtual device information.
Pass MergeCompositeFunctions()
Group one or multiple composite functions created by FuseOpsByPattern into a new function....
tvm::relax::DataflowBlock DataflowBlock
Definition transform.h:43
Pass DataflowUseInplaceCalls()
Pass that changes calls to operators that can be done in-place (generally, these are elementwise oper...
Pass FuseOpsByPattern(const tvm::ffi::Array< FusionPattern > &patterns, bool bind_constants=true, bool annotate_codegen=false, const tvm::ffi::Array< ffi::String > &entry_function_names={})
Apply pattern matching to each function in the given module, and group matched expressions into a new...
Pass CreateDataflowBlockPass(std::function< DataflowBlock(DataflowBlock, IRModule, PassContext)> pass_func, int opt_level, ffi::String name)
Create a dataflowblock pass.
Pass CallTIRRewrite()
Perform explicit tensor allocation for call_tir and call_dps_packed.
Pass ConvertLayout(ffi::Map< ffi::String, ffi::Array< ffi::String > > desired_layouts, LayoutCb layout_cb)
Layout conversion pass.
Pass ToMixedPrecision(DLDataType out_dtype, ffi::Optional< ffi::Array< ffi::String > > fp16_input_names=std::nullopt)
Automatic mixed precision pass. Currently the pass assumes the input module to be fp32 only,...
Pass Gradient(ffi::String func_name, ffi::Optional< ffi::Array< Var > > require_grads=std::nullopt, int target_index=0)
Reverse-mode automatic differentiation.
Pass Normalize()
Transform Relax IR to normal form: transform AST to A-normal form, and fill the ty of expressions.
tvm::relax::Function Function
Definition transform.h:42
Pass RemoveUnusedParameters()
Remove unused parameters to internal functions.
Pass CreateFunctionPass(std::function< Function(Function, IRModule, PassContext)> pass_func, int opt_level, ffi::String name)
Create a function pass.
Pass BindSymbolicVars(ffi::Map< ffi::Variant< PrimVar, ffi::String >, PrimExpr > binding_map, ffi::Optional< ffi::String > func_name=std::nullopt)
Bind symbolic vars to constant shape values.
Pass FuseTIR()
Fuse relax sub-function into a larger TIR function if possible. this pass works together with FuseOps...
Pass SplitLayoutRewritePreproc()
Split the layout rewrite preproc block to a separate tirx::Function.
Pass AttachGlobalSymbol()
Attach global_symbol to Relax functions and TIR Functions for codegen.
Pass FuseOps(int fuse_opt_level=-1)
This pass groups bindings in a dataflow block of Relax functions and generates a new grouped Relax fu...
Pass AnnotateTIROpPattern()
Annotate Op Pattern Kind for TIR functions, which is used in FuseOps.
Pass RemovePurityChecking()
Activate force_pure on all pure functions in the module and unwrap all pure override ops into the nor...
Pass LiftTransformParams(ffi::Variant< bool, ffi::Array< ffi::String > > shared_transform=false)
Lift transformation of the parameters of a function.
Pass NormalizeGlobalVar()
Possibly rename the GlobalVar in an IRModule to ensure these properties:
Pass LegalizeOps(ffi::Optional< ffi::Map< ffi::String, ffi::Function > > cmap, ffi::Optional< ffi::Array< ffi::String > > skip_ops, bool enable_warning=false, ffi::Optional< Target > target=std::nullopt)
Legalize high-level operator calls in Relax functions to call_tir with corresponding low-level TIR Fu...
Pass BindParams(ffi::String func_name, ffi::Map< Any, ffi::ObjectRef > params)
Bind params of function of the module to constant tensors.
Pass RemoveUnusedOutputs()
Remove unused outputs from internal functions.
Pass ExpandTupleArguments()
Expand tuple arguments to internal functions.
Pass RunCodegen(ffi::Optional< ffi::Map< ffi::String, ffi::Map< ffi::String, ffi::Any > > > target_options, ffi::Array< ffi::String > entry_functions)
Run codegen.
Pass FoldConstant()
Fold constant expressions within dataflow blocks.
Pass CanonicalizeBindings()
Simplify a Relax module by folding var bindings and match shape nodes, as well as tuple indices....
Pass RewriteDataflowReshape()
Convert all reshape-like call_tir whose corresponding binding vars are DataflowVars to relax....
Pass DecomposeOpsForTraining(ffi::Optional< ffi::String > func_name)
Decompose composite operators during training. For example, The result of batch norm (a triple) will ...
ffi::TypedFunction< ffi::Map< ffi::String, ffi::Array< ffi::String > >(Call)> LayoutCb
Definition transform.h:45
Pass AttachAttrLayoutFreeTensors()
Attach layout free tensors to the tirx::Function.
Pass LambdaLift()
Perform lambda lifting to lift functions from nested into global.
Pass RewriteCUDAGraph()
Rewrite a Relax module for executing with CUDA graph. This pass identifies the regions that can be ex...
Pass DecomposeOpsForInference(ffi::Optional< ffi::String > func_name)
Decompose composite operators during inference. For example, The result of batch norm (a triple) will...
Pass ConvertToDataflow(int min_size=2)
A pass that converts consecutive dataflow operations inside binding blocks into dataflow blocks.
Pass EliminateCommonSubexpr(bool call_only=false)
Pass UpdateVDevice(VDevice new_vdevice, int64_t index)
Update virtual device.
Pass AlterOpImpl(const ffi::Map< ffi::String, tirx::Function > &op_impl_map, const ffi::Map< ffi::String, ffi::Array< ffi::Optional< tirx::IndexMap > > > &op_tensor_transforms)
Returns a pass which replaces Functions which have matching kOperatorName attribute in op_impl_map,...
Pass StaticPlanBlockMemory()
The static memory planning pass on BindingBlock level. The pass will reuse allocated memory to its be...
Pass ToNonDataflow()
Transform all dataflow structure to non-dataflow version.
Pass SpecializeFunctionBasedOnCallSite()
This pass updates the var_tensor mapping of Functiontions from the call_tir info. Primarily used to u...
Pass DeadCodeElimination(ffi::Array< ffi::String > entry_functions={})
Dead code elimination.
TVM_TIRX_DLL Pass CreateModulePass(ffi::TypedFunction< IRModule(ffi::RValueRef< IRModule >, PassContext)> pass_func, int opt_level, ffi::String name)
Create a module pass from an owning FFI callback.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41