AI基础架构Pass Infrastructure
• Operation Pass
o OperationPass : Op-Specific
o OperationPass : Op-Agnostic
o Dependent Dialects
o Initialization
• Analysis Management
o Querying Analyses
o Preserving Analyses
• Pass Failure
• Pass Manager
o OpPassManager
• Dynamic Pass Pipelines
• Instance Specific Pass Options
• Pass Statistics
• Pass Registration
o Pass Pipeline Registration
o Textual Pass Pipeline Specification
• Declarative Pass Specification
o Tablegen Specification
• Pass Instrumentation
o Standard Instrumentations
• Crash and Failure Reproduction
o Local Reproducer Generation
Passes represent the basic infrastructure for transformation and optimization. This document provides an overview of the pass infrastructure in MLIR and how to use it.
See MLIR specification for more information about MLIR and its core aspects, such as the IR structure and operations.
See MLIR Rewrites for a quick start on graph rewriting in MLIR. If a transformation involves pattern matching operation DAGs, this is a great place to start.
Passes代表了转换和优化的基本基础架构。本文概述了MLIR中的Passes基础结构以及如何使用。
有关MLIR 及其核心方面(如IR结构和算子)的更多信息,请参见 MLIR规范。
有关 在MLIR中进行图形重写的快速入门,请参见 MLIR Rewrites。如果转换涉及模式匹配算子作DAG,那么这是一个很好的起点。
Operation Pass
In MLIR, the main unit of abstraction and transformation is an operation . As such, the pass manager is designed to work on instances of operations at different levels of nesting. The structure of the pass manager , and the concept of nesting, is detailed further below. All passes in MLIR derive from OperationPass and adhere to the following restrictions; any noncompliance will lead to problematic behavior in multithreaded and other advanced scenarios:
• Modify any state referenced or relied upon outside the current being operated on. This includes adding or removing operations from the parent block, changing the attributes(depending on the contract of the current operation)/operands/results/successors of the current operation.
• Modify the state of another operation not nested within the current operation being operated on.
o Other threads may be operating on these operations simultaneously.
• Inspect the state of sibling operations.
o Other threads may be modifying these operations in parallel.
o Inspecting the state of ancestor/parent operations is permitted.
• Maintain mutable pass state across invocations of runOnOperation. A pass may be run on many different operations with no guarantee of execution order.
o When multithreading, a specific pass instance may not even execute on all operations within the IR. As such, a pass should not rely on running on all operations.
• Maintain any global mutable state, e.g. static variables within the source file. All mutable state should be maintained by an instance of the pass.
• Must be copy-constructible
o Multiple instances of the pass may be created by the pass manager to process operations in parallel.
When creating an operation pass, there are two different types to choose from depending on the usage scenario:
在MLIR中,抽象和转换的主要单元是一个 算子 。这样,通过管理器被设计为在不同嵌套级别上的算子实例上工作。Passes管理器的结构 和嵌套的概念将详细介绍。MLIR中的所有Passes均源自OperationPass并遵守限制;在多线程和其它高级方案中,任何不符合都会导致有问题的行为:
• 修改正在算子的电流之外引用或依赖的任何状态。这包括在父块中添加或删除算子,更改属性(取决于当前算子的协议)/当前算子的算子数/结果/后继对象。
• 修改另一个未嵌套在当前算子中的算子的状态。
o 其它线程可能同时在这些算子上运行。
• 检查同级算子的状态。
o 其它线程可能正在并行修改这些算子。
o 允许检查祖先/父项算子的状态。
• 跨调用保持可变的通过状态runOnOperation。传递可能在许多不同的算子上运行,而不能保证执行顺序。
o 在进行多线程处理时,特定的传递实例甚至可能不会在IR内的所有算子上执行。因此,传递不应该依赖于所有算子上的运行。
• 保持任何全局可变状态,例如源文件中的静态变量。所有可变状态都应通过传递实例来维护。
• 必须是可复制的
o 流程管理器可以创建流程的多个实例,以并行处理算子。
创建算子传递时,根据使用情况,有两种不同的类型可供选择:
OperationPass : Op-Specific
An op-specific operation pass operates explicitly on a given operation type. This operation type must adhere to the restrictions set by the pass manager for pass execution.
To define an op-specific operation pass, a derived class must adhere to the following:
• Inherit from the CRTP class OperationPass and provide the operation type as an additional template parameter.
• Override the virtual void runOnOperation() method.
A simple pass may look like:
在op-specific算子上传递给定类型明确的算子。此类型必须遵守pass管理器为pass执行设置的限制。
要定义特定于算子的通道,派生类必须遵守以下规定:
• 继承自CRTP类,OperationPass并提供算子类型作为附加模板参数。
• 覆盖虚拟void runOnOperation()方法。
一个简单的pass可能看起来像:
namespace {
/// Here we utilize the CRTP PassWrapper utility class to provide some
/// necessary utility hooks. This is only necessary for passes defined directly
/// in C++. Passes defined declaratively use a cleaner mechanism for providing
/// these utilities.
struct MyFunctionPass : public PassWrapper<OperationPass,
MyFunctionPass> {
void runOnOperation() override {
// Get the current FuncOp operation being operated on.
FuncOp f = getOperation();

// Walk the operations within the function.
f.walk([](Operation *inst) {....
});

}
};
} // end anonymous namespace

/// Register this pass so that it can be built via from a textual pass pipeline.
/// (Pass registration is discussed more below)
void registerMyPass() {
PassRegistration(
“flag-name-to-invoke-pass-via-mlir-opt”, “Pass description here”);
}
OperationPass : Op-Agnostic
An op-agnostic pass operates on the operation type of the pass manager that it is added to. This means that passes of this type may operate on several different operation types. Passes of this type are generally written generically using operation interfaces and traits . Examples of this type of pass are Common Sub-Expression Elimination and Inlining .
To create an operation pass, a derived class must adhere to the following:
• Inherit from the CRTP class OperationPass.
• Override the virtual void runOnOperation() method.
A simple pass may look like:
op-agnostic上,被添加到管理器的算子类型进行运算。这意味着这种类型的通道可以在几种不同的算子类型上进行运算。通常使用算子接口 和 特征来写这种类型的pass 。此类传递的示例包括“ 常见子表达式消除” 和“ 内联” 。
要创建算子传递,派生类必须遵守以下内容:
• 继承自CRTP类OperationPass。
• 覆盖虚拟void runOnOperation()方法。
一个简单的pass可能看起来像:
/// Here we utilize the CRTP PassWrapper utility class to provide some
/// necessary utility hooks. This is only necessary for passes defined directly
/// in C++. Passes defined declaratively use a cleaner mechanism for providing
/// these utilities.
struct MyOperationPass : public PassWrapper<OperationPass<>, MyOperationPass> {
void runOnOperation() override {
// Get the current operation being operated on.
Operation *op = getOperation();

}
};
Dependent Dialects
Dialects must be loaded in the MLIRContext before entities from these dialects (operations, types, attributes, …) can be created. Dialects must also be loaded before starting the execution of a multi-threaded pass pipeline. To this end, a pass that may create an entity from a dialect that isn’t guaranteed to already ne loaded must express this by overriding the getDependentDialects() method and declare this list of Dialects explicitly.
必须先在MLIRContext中加载语言对话,然后才能从这些语言对话创建实体(算子,类型,属性等)。在开始执行多线程传递pass之前,还必须加载语言对话。可能无法保证已从其加载的语言对话创建实体的pass,必须通过重写getDependentDialects() 方法并明确声明此语言对话列表来表达。
Initialization
In certain situations, a Pass may contain state that is constructed dynamically, but is potentially expensive to recompute in successive runs of the Pass. One such example is when using PDL-based patterns , which are compiled into a bytecode during runtime. In these situations, a pass may override the following hook to initialize this heavy state:
• LogicalResult initialize(MLIRContext context)
This hook is executed once per run of a full pass pipeline, meaning that it does not have access to the state available during a runOnOperation call. More concretely, all necessary accesses to an MLIRContext should be driven via the provided context parameter, and methods that utilize “per-run” state such as getContext/getOperation/getAnalysis/etc. must not be used. In case of an error during initialization, the pass is expected to emit an error diagnostic and return a failure() which will abort the pass pipeline execution.
在某些情况下,通过可能包含动态构造的状态,但是在连续运行过程中重新计算可能会很费时。一个这样的例子就是使用 PDL基于 模式的模式 ,该模式在运行时被编译成字节码。在这些情况下,通过可能会覆盖以下hook,初始化此重载状态:
• LogicalResult initialize(MLIRContext context)
此hook在一次完整pass管道的每次运行中执行一次,意味着无法访问runOnOperation调用期间的可用状态。更具体地,所有必要的访问的MLIRContext应通过提供驱动context参数,利用 “per-run”状态,诸如 getContext/ getOperation/ getAnalysis/等,不得使用。如果初始化期间发生错误,则该传递将发出错误诊断并返回a failure(),中止pass管道的执行。
Analysis Management
An important concept, along with transformation passes, are analyses. These are conceptually similar to transformation passes, except that they compute information on a specific operation without modifying it. In MLIR, analyses are not passes but free-standing classes that are computed lazily on-demand and cached to avoid unnecessary recomputation. An analysis in MLIR must adhere to the following:
• Provide a valid constructor taking an Operation
.
• Must not modify the given operation.
An analysis may provide additional hooks to control various behavior:
• bool isInvalidated(const AnalysisManager::PreservedAnalyses &)
Given a preserved analysis set, the analysis returns true if it should truly be invalidated. This allows for more fine-tuned invalidation in cases where an analysis wasn’t explicitly marked preserved, but may be preserved (or invalidated) based upon other properties such as analyses sets.
一个重要的概念,以及转换过程,都是分析。这些在概念上与转换过程相似,除了在不修改计算信息的特定算子情况下。在MLIR中,分析不是pass,而是pass独立式类,这些类是按需延迟计算并缓存,以避免不必要的重新计算。MLIR中的分析必须遵循以下条件:
• 提供有效构造函数Operation

• 一定不能修改给定的算子。
分析可能会提供其它hook来控制各种行为:
• bool isInvalidated(const AnalysisManager::PreservedAnalyses &)
给定一个保留的分析集,如果该分析真无效,则分析返回true。如果没有明确标记保留分析,但可以根据其它属性(例如分析集)保留(或使之无效),则可以进行更精细的无效化。
Querying Analyses
The base OperationPass class provides utilities for querying and preserving analyses for the current operation being processed.
• OperationPass automatically provides the following utilities for querying analyses:
o getAnalysis<>
 Get an analysis for the current operation, constructing it if necessary.
o getCachedAnalysis<>
 Get an analysis for the current operation, if it already exists.
o getCachedParentAnalysis<>
 Get an analysis for a given parent operation, if it exists.
o getCachedChildAnalysis<>
 Get an analysis for a given child operation, if it exists.
o getChildAnalysis<>
 Get an analysis for a given child operation, constructing it if necessary.
Using the example passes defined above, let’s see some examples:
OperationPass基类提供用于查询和保留当前正在处理算子和分析的实用程序。
• OperationPass自动提供以下实用程序来查询分析:
o getAnalysis<>
 获得当前算子的分析,并在必要时进行构建。
o getCachedAnalysis<>
 获取当前算子的分析(如果已经存在)。
o getCachedParentAnalysis<>
 获取给定父算子的分析(如果存在)。
o getCachedChildAnalysis<>
 对给定的子算子(如果存在)进行分析。
o getChildAnalysis<>
 对给定的子算子进行分析,并在必要时进行构造。
使用上面定义的示例传递,看一些示例:
/// An interesting analysis.
struct MyOperationAnalysis {
// Compute this analysis with the provided operation.
MyOperationAnalysis(Operation *op);
};

void MyOperationPass::runOnOperation() {
// Query MyOperationAnalysis for the current operation.
MyOperationAnalysis &myAnalysis = getAnalysis();

// Query a cached instance of MyOperationAnalysis for the current operation.
// It will not be computed if it doesn’t exist.
auto optionalAnalysis = getCachedAnalysis();
if (optionalAnalysis)

// Query a cached instance of MyOperationAnalysis for the parent operation of
// the current operation. It will not be computed if it doesn’t exist.
auto optionalAnalysis = getCachedParentAnalysis();
if (optionalAnalysis)

}
Preserving Analyses
Analyses that are constructed after being queried by a pass are cached to avoid unnecessary computation if they are requested again later. To avoid stale analyses, all analyses are assumed to be invalidated by a pass. To avoid invalidation, a pass must specifically mark analyses that are known to be preserved.
• All Pass classes automatically provide the following utilities for preserving analyses:
o markAllAnalysesPreserved
o markAnalysesPreserved<>
通过查询后构造的分析将被缓存,以避免不必要的计算(如果稍后再次请求的话)。为了避免过时的分析,假定所有分析都通过了一次pass验证就无效了。为避免无效,pass必须特别标记已知保留的分析。
• 所有Pass类都会自动提供以下用于保留分析的实用程序:
o markAllAnalysesPreserved
o markAnalysesPreserved<>
void MyOperationPass::runOnOperation() {
// Mark all analyses as preserved. This is useful if a pass can guarantee
// that no transformation was performed.
markAllAnalysesPreserved();

// Mark specific analyses as preserved. This is used if some transformation
// was performed, but some analyses were either unaffected or explicitly
// preserved.
markAnalysesPreserved<MyAnalysis, MyAnalyses…>();
}
Pass Failure
Passes in MLIR are allowed to gracefully fail. This may happen if some invariant of the pass was broken, potentially leaving the IR in some invalid state. If such a situation occurs, the pass can directly signal a failure to the pass manager via the signalPassFailure method. If a pass signaled a failure when executing, no other passes in the pipeline will execute and the top-level call to PassManager::run will return failure.
允许MLIR中的传递正常失败。如果pass的某些不变式被破坏,可能会使IR处于某种无效状态,则可能会发生这种情况。如果发生这种情况,则pass可以该signalPassFailure方法直接向pass管理器发出故障信号。如果在执行过程中pass指示失败,则流水线中将不会执行其它任何传递,并且对PassManager::run顶级调用,返回failure。
void MyOperationPass::runOnOperation() {
// Signal failure on a broken invariant.
if (some_broken_invariant)
return signalPassFailure();
}
Pass Manager
The above sections introduced the different types of passes and their invariants. This section introduces the concept of a PassManager, and how it can be used to configure and schedule a pass pipeline. There are two main classes related to pass management, the PassManager and the OpPassManager. The PassManager class acts as the top-level entry point, and contains various configurations used for the entire pass pipeline. The OpPassManager class is used to schedule passes to run at a specific level of nesting. The top-level PassManager also functions as an OpPassManager.
以上各节介绍了pass的不同类型及其不变性。本节介绍PassManager的概念,以及如何配置和计划pass管道。与pass管理相关的主要类别有两种:PassManager和OpPassManager。PassManager类作为顶层的入口点,包含用于整个pass管道的各种配置。该OpPassManager用于调度类会将以嵌套的一个特定的水平上运行。顶级 PassManager还用作OpPassManager。
OpPassManager
An OpPassManager is essentially a collection of passes to execute on an operation of a specific type. This operation type must adhere to the following requirement:
• Must be registered and marked IsolatedFromAbove .
o Passes are expected to not modify operations at or above the current operation being processed. If the operation is not isolated, it may inadvertently modify or traverse the SSA use-list of an operation it is not supposed to.
Passes can be added to a pass manager via addPass. The pass must either be an op-specific pass operating on the same operation type as OpPassManager, or an op-agnostic pass.
An OpPassManager is generally created by explicitly nesting a pipeline within another existing OpPassManager via the nest<> method. This method takes the operation type that the nested pass manager will operate on. At the top-level, a PassManager acts as an OpPassManager. Nesting in this sense, corresponds to the structural nesting within Regions of the IR.
For example, the following .mlir:
AnOpPassManager本质上是要在特定类型的算子上执行的pass的集合。算子类型必须符合以下要求:
• 必须注册并标记 IsolatedFromAbove 。
o 预期pass不会修改正在处理的当前操作或更高的操作。如果操作不是孤立的,则可能会无意间修改或遍历不应执行的操作的SSA使用列表。
可以通过将pass添加到pass管理器addPass。该pass必须是采用 op-specific与相同的算子类型进行操作OpPassManager的op-agnosticpass,或者是pass。
OpPassManager通常OpPassManager通过将该nest<>方法显式嵌套在另一个现有pass中来创建An 。此方法采用嵌套pass管理器,将对其进行操作的操作类型。在顶层,a PassManager充当OpPassManager。从这个意义上讲,嵌套对应 于 IR区域内的 结构嵌套 。
例如,以下内容.mlir:
module {
spv.module “Logical” “GLSL450” {
func @foo() {

}
}
}
Has the nesting structure of:
module
spv.module
function
Below is an example of constructing a pipeline that operates on the above structure:
// Create a top-level PassManager class. If an operation type is not
// explicitly specific, the default is the builtin module operation.
PassManager pm(ctx);
// Note: We could also create the above PassManager this way.
PassManager pm(ctx, /operationName=/“module”);

// Add a pass on the top-level module operation.
pm.addPass(std::make_unique());

// Nest a pass manager that operates on spirv.module operations nested
// directly under the top-level module.
OpPassManager &nestedModulePM = pm.nestspirv::ModuleOp();
nestedModulePM.addPass(std::make_unique());

// Nest a pass manager that operates on functions within the nested SPIRV
// module.
OpPassManager &nestedFunctionPM = nestedModulePM.nest();
nestedFunctionPM.addPass(std::make_unique());

// Run the pass manager on the top-level module.
ModuleOp m = …;
if (failed(pm.run(m)))
… // One of the passes signaled a failure.
The above pass manager contains the following pipeline structure:
OpPassManager
MyModulePass
OpPassManagerspirv::ModuleOp
MySPIRVModulePass
OpPassManager
MyFunctionPass
These pipelines are then run over a single operation at a time. This means that, for example, given a series of consecutive passes on FuncOp, it will execute all on the first function, then all on the second function, etc. until the entire program has been run through the passes. This provides several benefits:
• This improves the cache behavior of the compiler, because it is only touching a single function at a time, instead of traversing the entire program.
• This improves multi-threading performance by reducing the number of jobs that need to be scheduled, as well as increasing the efficiency of each job. An entire function pipeline can be run on each function asynchronously.
Dynamic Pass Pipelines
In some situations it may be useful to run a pass pipeline within another pass, to allow configuring or filtering based on some invariants of the current operation being operated on. For example, the Inliner Pass may want to run intraprocedural simplification passes while it is inlining to produce a better cost model, and provide more optimal inlining. To enable this, passes may run an arbitrary OpPassManager on the current operation being operated on or any operation nested within the current operation via the LogicalResult Pass::runPipeline(OpPassManager &, Operation *) method. This method returns whether the dynamic pipeline succeeded or failed, similarly to the result of the top-level PassManager::run method. A simple example is shown below:
void MyModulePass::runOnOperation() {
ModuleOp module = getOperation();
if (hasSomeSpecificProperty(module)) {
OpPassManager dynamicPM(“module”);
…; // Build the dynamic pipeline.
if (failed(runPipeline(dynamicPM, module)))
return signalPassFailure();
}
}
Note: though above the dynamic pipeline was constructed within the runOnOperation method, this is not necessary and pipelines should be cached when possible as the OpPassManager class can be safely copy constructed.
The mechanism described in this section should be used whenever a pass pipeline should run in a nested fashion, i.e. when the nested pipeline cannot be scheduled statically along with the rest of the main pass pipeline. More specifically, a PassManager should generally never need to be constructed within a Pass. Using runPipeline also ensures that all analyses, instrumentations , and other pass manager related components are integrated with the dynamic pipeline being executed.
Instance Specific Pass Options
MLIR provides a builtin mechanism for passes to specify options that configure its behavior. These options are parsed at pass construction time independently for each instance of the pass. Options are defined using the Option<> and ListOption<> classes, and follow the LLVM command line flag definition rules. See below for a few examples:
struct MyPass … {
/// Make sure that we have a valid default constructor and copy constructor to
/// ensure that the options are initialized properly.
MyPass() = default;
MyPass(const MyPass& pass) {}

/// Any parameters after the description are forwarded to llvm:

AI基础架构Pass Infrastructure相关推荐

  1. 中国的“Databricks”们:打造AI基础架构,我们是认真的

    AI落地最大的驱动因素是基础架构的升级. 近年来,大数据分析.AI等领域一直备受关注,常有引人关注的融资事件发生.美国数据科学公司Databricks刚刚在今年8月底完成了16亿美元H轮融资,其最新估 ...

  2. 九章云极DataCanvas完成C轮融资:定义标准化AI基础架构未来

    近日,九章云极DataCanvas宣布完成C轮融资,由尚珹资本.赛富投资基金领投,君紫投资.领沨资本等投资机构跟投,融资金额3亿元,致远资本担任独家财务顾问.本轮融资备受一线投资机构认可,资方长期关注 ...

  3. 活动预告 | 2023 QCon 全球软件开发大会 - AI 基础架构论坛

    QCon 全球软件开发大会是由极客邦科技旗下 InfoQ 中国主办的综合性技术盛会,2023 QCon 会议北京站即将举行. 第四范式技术副总裁.OpenMLDB 项目发起人郑曌作为出品人在本次会议上 ...

  4. 金融业加速智能化,解析360金融AI基础架构和应用

    传统金融信贷业务中,催收.客服及电销人员占比超过 60%,人员素质参差不齐的现状造成了管理成本过高的问题,由此衍生的客户体验差,也成为困扰金融业的一大通病. 8 月 15 日,在 360金融 AI 媒 ...

  5. Pony.ai 的基础架构挑战与实践

    导读:本次分享将从以下几个方面介绍-- Pony.ai 基础架构做什么 车载系统 仿真平台 数据基础架构 其他基础架构 -- 01 Pony.ai 基础架构 首先给大家介绍一下 Pony.ai 的基础 ...

  6. 重新定义 AI 服务器架构

    作者 | 琥珀 出品 | AI科技大本营(公众号ID:rgznai100) 得益于迅速增长的计算能力.海量数据,以及神经网络前所未有的突破,AI 变得无处不在,也成为未来十年最具颠覆性的技术.根据 G ...

  7. 超融合基础架构超融合一体机

    一.什么是超融合基础架构? 超融合基础架构(Hyper-Converged Infrastructure,简称"HCI")常被简称为超融合架构,它是指在同一套单元设备(x86服务器 ...

  8. Liferay研究之廿九:Liferay5.2基础架构变动

    前几天Liferay正式发布了5.2, 抽空Down下来研究了一天,感觉还是有不少变化的,很多底层的东西都发生了变化.因为现在重点关注于MDD的研究,所以这次研究重点在基础架构(liferay inf ...

  9. Liferay研究之廿九:Liferay5.2基础架构变动收藏

    前几天Liferay正式发布了5.2, 抽空Down下来研究了一天,感觉还是有不少变化的,很多底层的东西都发生了变化.因为现在重点关注于MDD的研究,所以这次研究重点在基础架构 (liferay in ...

最新文章

  1. 学习《Linux设备模型浅析之设备篇》笔记(三)
  2. 菜鸟学Java(十九)——WEB项目测试好帮手,Maven+Jetty
  3. Java线程详解(11)-线程池
  4. python后端开发工程师做什么-如何面试Python后端工程师?
  5. 紫书 例题8-10 UVa 714 (二分答案)
  6. Spring Cloud教程–使用Spring Cloud Bus自动刷新配置更改
  7. 工业交换机为何要老化测试
  8. 与 python 交互
  9. 【转】类与类之间的常见关系,uml图表示
  10. 什么是Redis的VM机制
  11. windows pip命令不见了_Python中Pygame以及pip的下载与安装
  12. python 分词字典的词性_NLP注2“自定义词性与词典实现”,笔记,字典,的
  13. ES部分查询方法,elasticsearch查询方法
  14. emoji表情 mysql转移,mysql中emoji表情存储
  15. 视频演示-Snapper快捷优秀的音频预览播放器演示
  16. vs2019下载不了的解决办法
  17. 2022最新圣诞节代码:圣诞树
  18. 视频伪原创片头片尾 视频合并会改变md5
  19. 在JS数组特定索引处指定位置插入或修改元素的技巧
  20. 日常中文短句翻译英文有哪些好方法?

热门文章

  1. 2022-2028年中国农用塑料薄膜行业市场研究及前瞻分析报告
  2. Python+OpenCV 图像处理系列(9)—— 图像的翻转和缩放插值
  3. c/c++ 如何输入带空格的字符串
  4. tf.nn.embedding_lookup()的用法
  5. 使用ONNX将模型转移至Caffe2和移动端
  6. 处理器解决物联网和人工智能的融合
  7. 微调BERT:序列级和令牌级应用程序
  8. adb.exe: more than one device/emulator
  9. android.util.AndroidRuntimeException: requestFeature() must be called before adding content
  10. 上一篇的js处理失真数据存在问题换了种方法