tvm
Loading...
Searching...
No Matches
analysis.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_ANALYSIS_H_
25#define TVM_RELAX_ANALYSIS_H_
26
27#include <tvm/ir/module.h>
28#include <tvm/relax/expr.h>
30#include <tvm/relax/type.h>
31#include <tvm/s_tir/stmt.h>
32#include <tvm/sym/analyzer.h>
33#include <tvm/tirx/function.h>
34#include <tvm/tirx/index_map.h>
35
36#include <functional>
37#include <set>
38#include <utility>
39
40namespace tvm {
41namespace relax {
42//-----------------------------------
43// Shape expression analysis
44//----------------------------------
57TVM_DLL bool CanProveShapeEqual(const ffi::Array<PrimExpr>& lhs, const ffi::Array<PrimExpr>& rhs,
58 const sym::Analyzer& ana);
59
71TVM_DLL bool CanProveShapeEqual(const Expr& lhs, const Expr& rhs, const sym::Analyzer& ana);
72
73//-----------------------------------
74// Foundational Type analysis
75//-----------------------------------
81TVM_DLL Type GetStaticType(const Type& info);
82
88TVM_DLL Type TypeFromStaticType(const Type& type);
89
99TVM_DLL Type DeriveCallRetType(const FuncType& finfo, const Call& call, const BlockBuilder& ctx);
108TVM_DLL Type DeriveCallRetType(const FuncType& finfo, const Call& call, const BlockBuilder& ctx,
109 const sym::Analyzer& ana);
110
164 const Type& info, std::function<ffi::Optional<Expr>(const Var& var)> f_var_map = nullptr);
174TVM_DLL Type EraseToWellDefined(const Type& info,
175 std::function<ffi::Optional<Expr>(const Var& var)> f_var_map,
176 const sym::Analyzer& ana);
177
187TVM_DLL Type EraseToWellDefined(const Type& info, ffi::Map<Var, Expr> var_map);
197TVM_DLL Type EraseToWellDefined(const Type& info, ffi::Map<Var, Expr> var_map,
198 const sym::Analyzer& ana);
199
213enum class BaseCheckResult {
217 kFailL0 = 0,
223 kFailL1 = 1,
242 kFailL2 = 2,
244 kPass = 3
245};
246
258TVM_DLL BaseCheckResult TypeBaseCheck(const Type& base, const Type& derived);
268TVM_DLL BaseCheckResult TypeBaseCheck(const Type& base, const Type& derived,
269 const sym::Analyzer& ana);
270
278TVM_DLL bool IsBaseOf(const Type& base, const Type& derived);
286TVM_DLL bool IsBaseOf(const Type& base, const Type& derived, const sym::Analyzer& ana);
287
308TVM_DLL PrimExpr TypeBaseCheckPrecondition(const Type& base, const Type& derived);
309
317TVM_DLL Type TypeLCA(const Type& lhs, const Type& rhs);
325TVM_DLL Type TypeLCA(const Type& lhs, const Type& rhs, const sym::Analyzer& ana);
326
333TVM_DLL ffi::Array<tvm::Var> TIRVarsInType(const Type& ty);
334
347TVM_DLL ffi::Array<tvm::Var> DefinableTIRVarsInType(const Type& ty);
348
360TVM_DLL ffi::Array<PrimExpr> CollectNonNegativeExpressions(const Type& ty);
361
368TVM_DLL ffi::Array<tvm::Var> DefinedSymbolicVars(const Expr& expr);
369
376TVM_DLL ffi::Array<tvm::Var> FreeSymbolicVars(const Expr& expr);
377//-----------------------------------
378// General IR analysis
379//-----------------------------------
390TVM_DLL tvm::ffi::Array<Var> BoundVars(const Expr& expr);
391
402TVM_DLL tvm::ffi::Array<Var> FreeVars(const Expr& expr);
403
411TVM_DLL tvm::ffi::Array<Var> AllVars(const Expr& expr);
412
423TVM_DLL tvm::ffi::Array<GlobalVar> AllGlobalVars(const Expr& expr);
424
448TVM_DLL tvm::ffi::Array<tvm::ffi::Array<GlobalVar>> DetectRecursion(const IRModule& m);
449
456TVM_DLL ffi::Map<Var, Expr> AnalyzeVar2Value(const IRModule& m);
457
464TVM_DLL ffi::Map<Var, Expr> AnalyzeVar2Value(const Expr& expr);
465
472TVM_DLL ffi::Map<Var, Expr> AnalyzeVar2Value(const DataflowBlock& dfb);
473
480TVM_DLL ffi::Map<ffi::String, ffi::Array<Binding>> NameToBinding(const Function& fn);
481
488TVM_DLL ffi::Map<Var, ffi::Array<Var>> DataflowBlockUseDef(const DataflowBlock& dfb);
489
501std::pair<ffi::Map<Var, ffi::Array<Var>>, ffi::Array<Var>> FunctionUseDef(const Expr& expr);
502
506 /* \brief A map from variables to the bound expression.
507 *
508 * This is equivalent to the output of AnalyzeVar2Value
509 */
510 ffi::Map<Var, Expr> bound_values;
511
512 /* \brief The map from variables to downstream usages of the variable
513 *
514 * This is equivalent to the first output of FunctionUseDef.
515 */
516 ffi::Map<Var, ffi::Array<Var>> downstream_usage;
517
518 /* \brief A list of variables produced as output
519 *
520 * This is equivalent to the second output of FunctionUseDef
521 */
522 ffi::Array<Var> outputs;
523};
524
536
546TVM_DLL std::set<const VarNode*> GetUsedVars(const Expr& expr);
547
557
568
582TVM_DLL bool HasReshapePattern(const tirx::Function& func);
583
596TVM_DLL ffi::Optional<Expr> FindImpureCall(
597 const Expr& expr, const ffi::Optional<Expr>& own_name = ffi::Optional<Expr>(std::nullopt));
598
611 const Expr& expr, const ffi::Optional<Expr>& own_name = ffi::Optional<Expr>(std::nullopt));
612
626TVM_DLL void WellFormed(ffi::Variant<IRModule, Function> obj, bool check_ty = true);
627
638TVM_DLL bool CheckWellFormed(ffi::Variant<IRModule, Function> obj, bool check_ty = true);
639
650TVM_DLL ffi::Map<s_tir::SBlock, ffi::Map<ffi::ObjectRef, tirx::IndexMap>> SuggestLayoutTransforms(
651 const Function& fn, ffi::Array<tirx::IndexMap> write_tensor_transformations);
652
653/* \brief Collect variables whose value can be computed at compile-time
654 *
655 * If a function has the `kNumInput` attribute, then the first
656 * `kNumInput` parameters are provided at run-time, while all
657 * remaining parameters may be known at compile-time. This utility
658 * collects all variable bindings that only depend, directly or
659 * indirectly, on the parameters known at compile-time.
660 *
661 * \param func The relax::Function to analyze
662 *
663 * \return The set of variables that can be computed at compile-time,
664 * in order of their occurrence within the function.
665 */
666TVM_DLL ffi::Array<Var> ComputableAtCompileTime(const Function& func);
667
668} // namespace relax
669} // namespace tvm
670
671#endif // TVM_RELAX_ANALYSIS_H_
Algebra expression simplifications.
Managed reference to CallNode.
Definition expr.h:972
Managed reference to ExprNode.
Definition base_expr.h:359
Managed reference class to IRModuleNode.
Definition module.h:255
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:447
Managed reference to TypeNode.
Definition base_expr.h:80
Managed reference to VarNode.
Definition expr.h:714
Definition block_builder.h:257
Definition expr.h:248
Managed reference to FuncTypeNode.
Definition type.h:292
Definition expr.h:415
Managed reference to AnalyzerObj.
Definition analyzer.h:916
Managed reference to FunctionNode.
Definition function.h:105
Defines a remapping of tensor indices.
IRModule that holds the functions and type definitions.
tvm::ffi::Array< tvm::ffi::Array< GlobalVar > > DetectRecursion(const IRModule &m)
Find all sets of recursive or mutually recursive functions in the module.
BaseCheckResult
Fine grained result of base check.
Definition analysis.h:213
@ kPass
LSet is superset of RSet.
@ kFailL2
WLSet is not superset of RSet because of mismatch in value information.
@ kFailL1
LSet is not superset of RSet by only looking at static information.
@ kFailL0
The two value sets have no intersection at all: Interset(LSet, RSet) = empty.
ffi::Array< Var > ComputableAtCompileTime(const Function &func)
bool CanProveShapeEqual(const ffi::Array< PrimExpr > &lhs, const ffi::Array< PrimExpr > &rhs, const sym::Analyzer &ana)
Can prove the two symbolic shape arrays equals to each other.
Type GetStaticType(const Type &info)
Get the corresponding static type from a given type.
bool ContainsImpureCall(const Expr &expr, const ffi::Optional< Expr > &own_name=ffi::Optional< Expr >(std::nullopt))
Check if the given expression (likely a function body) contains any impure calls.
tvm::ffi::Array< GlobalVar > AllGlobalVars(const Expr &expr)
Get all global variables from expression expr.
ffi::Array< tvm::Var > TIRVarsInType(const Type &ty)
Get the TIR variables that appear in the input type. The returned list is deduplicated - each TIR var...
OpPatternKind AnalyzeOpPatternKind(const tirx::Function &func)
Annotate Op Pattern Kind for tirx::Function, which is used in relax FuseOps.
ffi::Array< tvm::Var > FreeSymbolicVars(const Expr &expr)
Get the TIR variables that are used but not defined in the input function. The returned list is dedup...
ffi::Map< Var, Expr > AnalyzeVar2Value(const IRModule &m)
Analyze var -> value mapping from VarBindings.
ffi::Array< tvm::Var > DefinedSymbolicVars(const Expr &expr)
Get the TIR variables that defined in the input function. The returned list is deduplicated - each TI...
Expr RemoveAllUnused(Expr expr)
Remove unused statements inside DataflowBlocks.
BaseCheckResult TypeBaseCheck(const Type &base, const Type &derived)
Run a base check to see if base subsumes derived.
bool CheckWellFormed(ffi::Variant< IRModule, Function > obj, bool check_ty=true)
Return whether an IRModule or Function is well-formed.
Type EraseToWellDefined(const Type &info, std::function< ffi::Optional< Expr >(const Var &var)> f_var_map=nullptr)
Erase the info to a corresponding more coarse grained type that is still well-defined(with all the va...
ffi::Array< tvm::Var > DefinableTIRVarsInType(const Type &ty)
Get the TIR variables that appear in the input type.
ffi::Optional< Expr > FindImpureCall(const Expr &expr, const ffi::Optional< Expr > &own_name=ffi::Optional< Expr >(std::nullopt))
Check if the given expression (likely a function body) contains any impure calls.
std::set< const VarNode * > GetUsedVars(const Expr &expr)
Get the used variables in an expression.
std::pair< ffi::Map< Var, ffi::Array< Var > >, ffi::Array< Var > > FunctionUseDef(const Expr &expr)
Get the use-def chain of variables inside a function.
bool HasReshapePattern(const tirx::Function &func)
Check if the given tirx::Function is essentially doing a reshape operation. The reshape operation als...
PrimExpr TypeBaseCheckPrecondition(const Type &base, const Type &derived)
Return the condition for which base is a superset of derived.
ffi::Map< ffi::String, ffi::Array< Binding > > NameToBinding(const Function &fn)
Return a mapping from variable name to its Bindings.
Type TypeFromStaticType(const Type &type)
Get the corresponding type from static type.
bool IsBaseOf(const Type &base, const Type &derived)
Check the relation of two type to see if one subsumes another one.
ffi::Map< s_tir::SBlock, ffi::Map< ffi::ObjectRef, tirx::IndexMap > > SuggestLayoutTransforms(const Function &fn, ffi::Array< tirx::IndexMap > write_tensor_transformations)
Using the layout transforms on the outputs, suggest layout transformation on the blocks and tensors f...
void WellFormed(ffi::Variant< IRModule, Function > obj, bool check_ty=true)
Check if an IRModule or Function is well-formed.
tvm::ffi::Array< Var > BoundVars(const Expr &expr)
Get all bound variables from expression expr.
tvm::ffi::Array< Var > FreeVars(const Expr &expr)
Get free type parameters from expression expr.
tvm::ffi::Array< Var > AllVars(const Expr &expr)
Get all variables from expression expr.
OpPatternKind
Definition op_attr_types.h:35
Type TypeLCA(const Type &lhs, const Type &rhs)
Unify the two type to their least common ancestor.
Type DeriveCallRetType(const FuncType &finfo, const Call &call, const BlockBuilder &ctx)
ffi::Array< PrimExpr > CollectNonNegativeExpressions(const Type &ty)
Collect expressions whose usage requires them to be non-negative.
ffi::Map< Var, ffi::Array< Var > > DataflowBlockUseDef(const DataflowBlock &dfb)
Get the use-def chain of variables inside a dataflow block.
VarUsageInfo CollectVarUsage(const Expr &expr)
Collect variable bindings and usage.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
Data structures that can appear in operator attributes.
Relax types, including the richer dependent Relax type nodes.
S-TIR (Schedulable TIR) statements and attributes.
A utility struct returned by CollectVarUsage.
Definition analysis.h:505
ffi::Array< Var > outputs
Definition analysis.h:522
ffi::Map< Var, ffi::Array< Var > > downstream_usage
Definition analysis.h:516
ffi::Map< Var, Expr > bound_values
Definition analysis.h:510
TIR Function.