Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Code Structure

Chunwei edited this page Jul 27, 2020 · 5 revisions

本页面介绍代码章节。

目录介绍

image-20200727104859959
  • cinn 包含了核心大部分代码
  • cmake 为 CMAKE 的封装及第三方库的 CMAKE 配置
  • docs 预计为文档页面
  • python 为 python 封装
  • tests cinn 整体相关的测试,目前包含 test01test02 ,主要包含了向量、矩阵乘的 C 源码生成的性能测试

进入 cinn 之后

image-20200727104929488
  • backends 为各种硬件后端的封装,包括 CodeGenJIT
  • common 为 IR 层次的复用函数方法
  • hlir 包含了 IR 以上的组网封装,包括 primitive, Op, Model 等等
  • ir 包含了最底层 IR 相关的实现
  • lang 包含了 CINN DSL 的相关代码
  • optim 包含底层 IR 的各类改写及优化
  • poly 包含了 polyhedral compilation 相关封装
  • pybind 包含了 pyton 相关封装(TODO 整理到 python 下)
  • python 包含了 python 相关封装 (TODO 整理到 上层 python 下)
  • runtime 包含了执行时的一些封装
  • utils 包含了各类通用的工具方法

概念介绍

以下介绍 CINN 相关的一部分概念

  • CINN IR,类似 + - * %,跟 LLVM IR 有一定对应关系,是 CINN 的最底层表示
  • CINN DSL,CINN 底层用于表示计算的一套专有语法,比如 auto C = Compute({10, 20}, [](Expr i) { return A(i) + 1.f; })
    • 目前有 C++ 和 Python 两种支持
  • Primitive,基础原语,表示计算的最基本单元,比如 Dot, Broadcast 等等;根据不同后端,Primitive 可能会进一步 lower 成 基础 CINN IR
  • HLIR(Hight Level IR) ,包含了 CINN IR 上层所有的组网的模块封装
  • PE (Peirmitive Emitter) ,目录在 cinn/hlir/pe ,包含了一些计算的 helper function,比如 matmul(... 等等

CINN 代码生成流程

st=>start: CINN DSL code
lower=>operation: Lower
ir=>start: CINN IR
op=>operation: Optimize
opt_code=>start: Optimized IR
codegen=>operation: CodeGen
jit=>operation: JIT
comp=>end: compiled code

st->lower->ir->op->opt_code->codegen->jit->comp

重要模块

Tensor

CINN 中计算的定义是一个组织 SSA 的过程,Tensor 在其中就是 SSA 节点,其特性是

  • 名字不同的 Tensor 严格表示不同的计算过程,反之,名字相同表示相同的过程

Tensor 一般由 Compute 来创建,比如

Expr M(100), N(200);
Placeholder<float> A("A", {M, N});
Placeholder<float> B("B", {M, N});

Tensor C = Compute({M,N}, [=](Expr i, Expr j) { return A(i,j) + B(i,j); });

Compute

Compute 有多种使用方法,比较重要的方法声明

ir::Tensor Compute(const std::vector<Expr> &domain,
                   compute_handler_t fn,
                   const std::string &name             = "",
                   const std::vector<Var> &reduce_axis = {},
                   const std::vector<Expr> &shape      = {});

有关 reduce axis 的使用方法可以参考 tests/test02_matmul_main.cc .

Lower, LowerImpl

输入前端传入的 DSL 定义,返回底层的 CINN IR。

中间包含的步骤:

  1. polyhedral 表示 经过 AstGen 生成 AST
  2. AST 扩展为 CINN IR
  3. 一系列的后处理,针对 polyhedral 不方便处理的部分做修改
  4. 一系列的优化处理

CodeGen

Clone this wiki locally