transform.h 14.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/*!
 * \file tvm/relay/transform.h
22
 * \brief Relay specific transformation passes.
23 24 25 26
 */
#ifndef TVM_RELAY_TRANSFORM_H_
#define TVM_RELAY_TRANSFORM_H_

27
#include <tvm/relay/attrs/transform.h>
28
#include <tvm/ir/transform.h>
29
#include <tvm/relay/expr.h>
30
#include <tvm/relay/function.h>
31
#include <tvm/relay/op_attr_types.h>
32 33
#include <tvm/relay/op.h>

34 35 36 37 38 39
#include <string>

namespace tvm {
namespace relay {
namespace transform {

40 41 42 43 44 45 46
using Pass = tvm::transform::Pass;
using PassNode = tvm::transform::PassNode;
using PassInfo = tvm::transform::PassInfo;
using PassInfoNode = tvm::transform::PassInfoNode;
using PassContext = tvm::transform::PassContext;
using PassContextNode = tvm::transform::PassContextNode;
using Sequential = tvm::transform::Sequential;
47 48 49 50 51 52 53 54 55 56 57

/*
 * \brief Create a function pass.
 *
 * \param pass_func The packed function that contains the optimization.
 * \param opt_level The optimization level of the function pass.
 * \param name The name of the function pass.
 * \param required The list of the passes that the function pass is dependent on.
 *
 * \return The created function pass.
 */
58
TVM_DLL Pass CreateFunctionPass(const runtime::TypedPackedFunc<
59
                                Function(Function, IRModule, PassContext)>& pass_func,
60 61
                                int opt_level,
                                const std::string& name,
62
                                const tvm::Array<tvm::PrimExpr>& required);
63 64 65 66 67 68 69 70 71 72 73

/*! \brief Remove expressions which does not effect the program result.
 *
 * It will remove let bindings which are not referenced,
 * and inline let bindings that are only used once.
 *
 * For example, this pass should turn `let a = 1 in 2` into `2`,
 * as the value of the expression does not depend on a.
 *
 * As another example, `let a = 1 in a` will be optimized into 1.
 *
雾雨魔理沙 committed
74 75
 * \param inline_once whether or not to inline binding used one.
 *
76 77
 * \return the pass.
 */
雾雨魔理沙 committed
78
TVM_DLL Pass DeadCodeElimination(bool inline_once = false);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

/*!
 * \brief Fold constant expressions.
 *
 * \return The pass.
 */
TVM_DLL Pass FoldConstant();

/*!
 * \brief Fuse operations into expr into seperate functions.
 *
 * \param fuse_opt_level Optimization level. If it is -1 it will be inferred from pass context.
 *
 * \return The pass.
 */
TVM_DLL Pass FuseOps(int fuse_opt_level = -1);

/*!
 * \brief Rewrite the annotated program.
 *
 * \param fallback_device The fallback device which is the default device for
 *                        operators without annotation.
 *
 * \return The pass.
 */
TVM_DLL Pass RewriteAnnotatedOps(int fallback_device);

/*!
 * \brief turn a dataflow graph into Administrative Normal Form, or A-Normal Form (ANF).
 *
 * It will turn an expression that is in a graph form (with sharing implicit),
 * to an expression with explicit sharing (A-Normal Form).
 *
 * The scope of the root expression is the global scope.
 *
 * The scope of any non root expression is the least common ancestor of all it's scope.
 *
 * Values are ordered by post-DFS order in each scope.
 *
 * \return The pass.
 */
TVM_DLL Pass ToANormalForm();

/*!
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
 * \brief Turn an expression into continuation passing style(CPS).
 *
 * CPS mean that every function will, instead of returning the result directly,
 * be passed down an extra function (called the continuation) as argument,
 * and pass the result to the continuation instead.
 *
 * Thus, every function call has to be passed an extra argument
 * that represent the rest of the computation (Hence the name of continuation).
 *
 * Similarly, all other compute will be wrapped and call the continuation as well.
 *
 * \return the pass.
 */
TVM_DLL Pass ToCPS();

/*!
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
 * \brief Remove let binding and directly share via pointer instead.
 *
 * It will remove all let binding,
 * and turn all of the variable bound by let into direct pointer reference.
 *
 * \return the expression in graph normal form.
 */
TVM_DLL Pass ToGraphNormalForm();

/*!
 * \brief Aggressive constant propagation/constant folding/inlining.
 *
 * It will do as much computation in compile time as possible.
 * It has two benefit: remove runtime overhead, and allow more optimization (typically fusion).
 * As a side effect, code size will explode.
 *
 * \return the optimized expression.
 */
TVM_DLL Pass PartialEval();
158

159 160 161 162 163 164 165 166 167
/*!
 * \brief Simplify certain operators during inference. For example, batch norm
 * will be unpacked into a number of simplified operators.
 *
 * \return The Pass.
 */
TVM_DLL Pass SimplifyInference();

/*!
168 169 170 171 172 173 174
 * \brief Replaces non linear activation functions with their fast but approximate counterparts.
 *
 * \return The Pass.
 */
TVM_DLL Pass FastMath();

/*!
175 176 177 178 179 180
 * \brief Infer the type of an expression.
 *
 * The result of type checking is a new expression with unambigous
 * type information filled in, as well as it's checked type field
 * populated with the result type.
 *
181
 * \return The pass.
182 183 184 185 186 187 188 189 190 191 192 193
 */
TVM_DLL Pass InferType();

/*!
 * \brief Search and eliminate common subexpression. For example, if there are
 * two expressions evaluated to an identical value, a single variable is created
 * and these two expressions are replaced by this variable.
 *
 * \param fskip The callback argument that allows to skip certain expressions.
 *
 * \return The pass.
 */
194
TVM_DLL Pass EliminateCommonSubexpr(runtime::PackedFunc fskip = nullptr);
195 196 197 198 199 200 201 202 203 204 205 206 207

/*!
 * \brief Combine parallel 2d convolutions into a single convolution if the
 * number of branches of this conv2d operator is not less than
 * `min_num_branch`.
 *
 * \param min_num_branches The minimun number of branches.
 *
 * \return The pass.
 */
TVM_DLL Pass CombineParallelConv2D(uint64_t min_num_branches = 3);

/*!
208 209 210 211 212 213 214 215 216 217 218
 * \brief Combine parallel dense ops into a single batch_matmul if the
 * number of branches of this dense operator is not less than
 * `min_num_branch`.
 *
 * \param min_num_branches The minimun number of branches.
 *
 * \return The pass.
 */
TVM_DLL Pass CombineParallelDense(uint64_t min_num_branches = 3);

/*!
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
 * \brief Backward fold axis scaling into weights of conv/dense operators.
 *
 * \return The pass.
 */
TVM_DLL Pass BackwardFoldScaleAxis();

/*!
 * \brief Forward fold axis scaling into weights of conv/dense operators.
 *
 * \return The pass.
 */
TVM_DLL Pass ForwardFoldScaleAxis();

/*!
 * \brief A sequential pass that executes ForwardFoldScaleAxis and
 * BackwardFoldScaleAxis passes.
 *
 * \return The pass.
 */
TVM_DLL Pass FoldScaleAxis();

/*!
 * \brief Canonicalize some operators to the simplified operators. For example,
 * bias_add can be canonicalized to expand_dims and broadcast_add.
 *
 * \return The pass.
 */
TVM_DLL Pass CanonicalizeOps();

/*!
 * \brief Alternate the layouts of operators or replace primitive operators
 * with other expressions.
 *
 * \return The pass.
 */
TVM_DLL Pass AlterOpLayout();

256
/*!
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
 * \brief Given a dest layout, this pass transforms the expr such that most of the ops input data
 * layout is changed to the dest layout. In ideal situation, there are only 2 layout transforms, one
 * at the start and one at the end.
 *
 * This pass is not a part of relay.build and is expected to be called between framework-relay
 * parser and relay.build call. This is very helpful for hardware backends that support/prefer only
 * type of data layout.
 *
 * RFC - https://discuss.tvm.ai/t/layout-conversion-pass/4009
 *
 * This pass uses most of the AlterOpLayout and InferCorrectLayout infrastructure. We can define new
 * layouts for conv2d ops for now. Most of the other operators try to adapt to their input layout
 * using the InferCorrectLayout infrastructure.
 *
 * \param desired_layout The desired layout.
 * \return The pass.
 */
TVM_DLL Pass ConvertLayout(const std::string& desired_layout);

/*!
277
 * \brief Legalizes an expr with another expression.
278 279 280 281 282
 * \param legalize_map_attr_name The Op's attr name which corresponds to the legalize rule function.
 * One can collect and isolate similar type of legalize transformations using this param. For
 * example, transformations that only apply to Dialects can be isolated into a FTVMDialectLegalize
 * string. This pass calls only those transformations that have been registered using the supplied
 * legalize_map_attr_name.
283 284 285
 *
 * \return The pass.
 */
286
TVM_DLL Pass Legalize(const std::string& legalize_map_attr_name = "FTVMLegalize");
287 288

/*!
289 290 291 292 293 294
 * \brief Canonicalize cast expressions to make operator fusion more efficient.
 *
 * \return The pass.
 */
TVM_DLL Pass CanonicalizeCast();

295
/*!
296
 * \brief Add abstraction over a constructor or global variable bound to a function.
297 298
 *
 * For example: `square` is transformed to
299
 * `fn (%x: int32) -> int32 { square(x) }`.
300 301 302 303
 *
 * See https://en.wikipedia.org/wiki/Lambda_calculus#%CE%B7-conversion
 * for more details.
 *
304 305 306
 * \param expand_constructor Whether to expand constructors.
 * \param expand_global_var Whether to expand global variables.
 *
307 308
 * \return The pass.
 */
309
TVM_DLL Pass EtaExpand(bool expand_constructor, bool expand_global_var);
310

311 312 313
/*!
 * \brief Print the IR for a module to help debugging.
 *
314 315
 * \param show_meta_data The flag to control if meta data needs to be printed.
 *
316 317
 * \return the pass.
 */
318
TVM_DLL Pass PrintIR(bool show_meta_data = true);
319

320 321 322 323 324 325 326 327
/*!
 * \brief Partition a Relay program into regions that can be executed on
 * different backends.
 *
 * \return The pass.
 */
TVM_DLL Pass PartitionGraph();

328 329 330 331 332 333 334 335
/*!
 * \brief Inline the global functions marked as `inline` in a given Relay
 * IRModule.
 *
 * \return The pass.
 */
TVM_DLL Pass Inline();

336 337 338 339 340 341 342 343 344 345
/*!
 * \brief Remove the unused functions in the Relay IRModule.
 *
 * \param entry_functions The entry functions used to search the functions that
 *        are being used.
 *
 * \return The pass.
 */
TVM_DLL Pass RemoveUnusedFunctions(Array<tvm::PrimExpr> entry_functions);

Zhi committed
346 347
}  // namespace transform

348
/*!
Zhi committed
349 350
 * \brief Bind the free variables to a Relay expression. This is a helper
 * function usually called by other pass functions to help optimizations.
351
 *
Zhi committed
352 353 354
 * \param expr The input expression.
 * \param binds The variable to expression map that will be used to help the
 *        binding.
355
 *
Zhi committed
356
 * \return The updated expression.
357
 */
Zhi committed
358 359 360 361 362 363 364 365 366 367 368 369 370
TVM_DLL Expr Bind(const Expr& expr, const tvm::Map<Var, Expr>& binds);

/*!
 * \brief Infer the type of a function as if it is mapped to var in the mod.
 *
 * \param f the function.
 * \param mod The module used for referencing global functions.
 * \param var The global variable corresponding to the function.
 *
 * \return A type checked Function with its checked_type field populated.
 * \note this function mutates mod and is not thread-safe.
 */
TVM_DLL Function InferType(const Function& f,
371
                           const IRModule& mod,
Zhi committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
                           const GlobalVar& var);

/*!
 * \brief Apply rewrite rules to rewrite the expr in post DFS order. This
 * function is used as a helper function to rewrtie an expression in a pass.
 *
 * \param expr The expression.
 * \param rewrite_map_attr_name The Op's attr name which corresponds to the rewrite
 *                              rule function.
 * \param fcontext Additional callback to provide context argument for each call node.
 * \param fmulti_ref_trigger Transformation function to be called when
 *                           an Expr consumed by multiple callers.
 * \return The rewritten expression.
 */
TVM_DLL Expr ForwardRewrite(const Expr& expr,
                            const std::string& rewrite_map_attr_name,
388
                            std::function<ObjectRef(const Call&)> fcontext = nullptr,
Zhi committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
                            std::function<Expr(const Expr&)> fmulti_ref_trigger = nullptr);

/*!
 * \brief Apply rewrite rules to rewrite the expr in post DFS order. This
 * function is used as a helper function to rewrtie an expression in a pass.
 *
 * \param expr The expression.
 * \param rewrite_func The rewrite func that will apply to all operators.
 * \param fcontext Additional callback to provide context argument for each call node.
 * \param fmulti_ref_trigger Transformation function to be called when
 *                           an Expr consumed by multiple callers.
 *
 * \return The rewritten expression.
 */
TVM_DLL Expr ForwardRewrite(const Expr& expr,
                            const FForwardRewrite& rewrite_func,
405
                            std::function<ObjectRef(const Call&)> fcontext = nullptr,
Zhi committed
406
                            std::function<Expr(const Expr&)> fmulti_ref_trigger = nullptr);
407

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
/*!
 * \brief Rewrite the annotated program.
 *
 * \param expr The expression.
 * \param fallback_device The fallback device which is the default device for
 *                        operators without annotation.
 *
 * \return The updated program.
 */
TVM_DLL Expr RewriteAnnotatedOps(const Expr& expr, int fallback_device);

/*!
 * \brief Turn an expression into continuation passing style(CPS).
 *
 * CPS mean that every function will, instead of returning the result directly,
 * be passed down an extra function (called the continuation) as argument,
 * and pass the result to the continuation instead.
 *
 * Thus, every function call has to be passed an extra argument
 * that represent the rest of the computation (Hence the name of continuation).
 *
 * Similarly, all other compute will be wrapped and call the continuation as well.
 *
 * \param f the function.
 * \param mod the module.
 *
 * \return the converted Function.
 */
436
TVM_DLL Function ToCPS(const Function& f, const IRModule& mod);
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458

/*!
 * \brief Remove the continuation argument of a CPS function.
 *
 * Note that this only transform the type back into un-CPS form
 * when there is no higher order input/output.
 *
 * \param f the function.
 *
 * \return the converted Function.
 */
TVM_DLL Function UnCPS(const Function& f);

/*!
 * \brief Deduplicate the bound variables and type variables in the expression.
 *
 * \param e the expression.
 *
 * \return the deduplicated expression.
 */
TVM_DLL Expr DeDup(const Expr& e);

459 460 461 462
}  // namespace relay
}  // namespace tvm

#endif  // TVM_RELAY_TRANSFORM_H_