已合并
Upgrade the infrastructure to support future extensions #157
Maksim Vlasov创建于 8月11日
Upgrade the infrastructure to support future extensions #157
已合并
Maksim Vlasov创建于 8月11日
Maksim Vlasov
Maksim Vlasov
8月11日

PyAsc Infrastructure Changes

This patch introduces modifications to the existing PyAsc infrastructure required by the tile-based programming add-on. Currently, there are no changes in public API or user experience. All existing features remain fully functional, as well as unit and system tests.


1. MLIR Changes

1.1 Asc Dialect — IR

  • New operations: Broadcast, Reduction (reduce_sum/max/min/prod with ReducePattern), register-based API (~40 ops for C310: binary, unary, vec-scalar, reduce, utility), LayerNorm, Log2, get_buf/rls_buf (BufId sync for C310), data_copy_nd_dma + NdDmaParams, set_loop_mode_para/reset_loop_mode_para, trans_data_to_5hd_tensor, local_tensor_v3, local_tensor.get_phy_addr_v2.
  • New types: FixpipeParamsC310, MaskReg, RegTensor, NdDmaParams.
  • New attributes: MaskPattern, MemType, ReducePattern, DataCopyMVType.
  • New API types: LayerNormTiling, LoopModeParams.
  • New interfaces: OpWithSrcInterface (get source tensors), OpWithReusableSrcInterface (marker for safe same-op reuse), CopyToL0OpInterface, MicroAPIOpInterface (+ Binary/Unary/VecScalar variants), ReduceOpInterface, BinaryL0OpInterface, BinaryL2OpInterface.
  • Extended interfaces: OpWithDstInterface gained getDstTensors() default; UnaryOpInterface/BinaryOpInterface/VecScalarOpInterface now include OpWithSrcInterface and OpWithReusableSrcInterface; L0/L2 interfaces gained mutable operand accessors and setIsSetMask.
  • CopyDirection enum: Replaced with corresponding methods GlobalToLocal/LocalToGlobal/LocalToLocal/GlobalToGlobal.
  • Modified operations: SoftMax/SimpleSoftMax changed to VectorOp with src interface, sumTensor/maxTensor made Optional; RmsNorm renamed mnemonic to rms_norm, changed to VectorOp; Fixpipe changed to DataCopyOp with generalized dst type and C310 params support; DataCopyL0/L2 gained src interface; LocalTensorAutoOp gained position attribute; SubIndex ops gained folders (zero-index identity); ReinterpretCastOp fold enhanced to eliminate double-cast chains; MulCast ops moved to BinaryCast templates; VecScalar ops gained Subs and Divs variants; all unary/binary/vec-scalar/math base classes gained default getSrcTensors()/getDstTensors().

1.2 Asc Dialect — Passes

  • Renamed: HoistUBAllocationHoistTensorAllocation (added exclude-in-out option), InsertSyncInsertQueSync.
  • Modified: MaterializeTensor (added always-buf option), LegalizeKernelArgs (added set-ffts-addr option, conditional on architecture), DetectKernelType (now classifies as "vector"/"cube"/"mixed" instead of binary flag), InsertSync (extended with V_S/S_V event sync for get_value/set_value), InputOutputTensor (handles input-only tensors used for ubuf_gm copy).
  • All existing passes received detailed description fields in TableGen.

1.3 EmitAsc Dialect

  • New ops: init_struct (struct initialization with named fields), mask (create bit-by-bit mask pair).
  • New type: mask (2-element i64 array for high/low mask).
  • variable: Removed Pure trait; added canonicalizer support.
  • New utility: InitStructBuilder helper class.

1.4 Code Emitter (Target/AscendC)

  • New emitters for all new Asc operations (Broadcast, Reduction, LayerNorm, MicroAPI suite, BufId sync, NdDma, etc.) and arith::NegFOp.
  • CodeEmitter: Added methods for C310 types (CO2Layout, FixpipeParamsC310, RegTensor, MaskReg, NdDmaParams).
  • Fixpipe emission: Generalized dst type handling; config emitted by name instead of hardcoded constant.
  • VecScalar ops: Added Subs and Divs variants to all L0/L1/L2 emitter lists.

1.5 Build System & Tools

  • Removed C language requirement from project; added atomic profile update for GCC coverage.
  • Added LIT tests for passes and emitters that didn't have them initially.
  • ConstantOpBuilder: Widened parameter types to int64_t; added i1() helper.

2. Python Changes

2.1 Codegen (asc/codegen/)

  • FunctionVisitor:
    • Added CustomBuiltins support for injecting custom builtins into the name scope.
    • Added MatMult (@ operator) support.
    • Boolean operators (and/or/not) and IfExp: compile-time constant folding before falling back to IR.
    • visit_Call: Handles ast.Starred arguments (*args unpacking).
    • visit_For: Generalized to accept any BaseRange subclass; calls handle_op() on the range object.
    • compute_inout: Materializes initial handles outside the block; accepts init_handles for if/else merge.
    • visit_If: Else-branch inherits init handles from then-branch.
  • NameScope: builtinsdefault_builtins; constructor accepts custom_builtins; inherit() propagates them.
  • Function: Added compute_globals() for closure variable resolution; improved AST line number tracking for decorated functions; ConstExpr detection checks runtime isinstance in addition to annotations.
  • Specialization: PointerArgType.to_ir() switched to get_unranked_memref_type.

2.2 Runtime & Compiler (asc/runtime/)

  • Platform support: Added C310 architecture — CompilationArch enum (C220/C310), platform_to_arch() mapping, 12 new platforms (Ascend950PR_95xx); set_platform() accepts string values; CompilePlatform enum removed in favor of CompilationArch.
  • Compiler: Compiler sets compilation_arch, soc_version on the module before passes; added -fcce-vf-vl Bisheng flag for C310; bisheng_options type fixed to Tuple[str, ...]; opt_level default changed to int = 3; compilation pipeline extended with new pass stages.
  • JIT: Refactored compile/cache/launch into separated phases using CompilePrereqs dataclass; kernel handles cached and reused across calls (LaunchedKernel); __getitem__ returns self; compute_globals() used for closures; jit() injects CustomBuiltins(range=asc_range) by default; 0-dim torch tensors treated as scalars; LaunchOptions.core_num changed to Optional[int] = None (auto-detect). Cache hash algorithm abstracted via HashDriver protocol (default: Blake2b instead of SHA-256).
  • Launcher: expand_kernel_args handles torch 0-dim tensors as scalars; core_num auto-detected if None; DRY_RUN env var renamed to PYASC_DRY_RUN; kernel binary handles optionally cached and unregistered after use.

2.3 Language Core (asc/language/core/)

  • bfloat16 dtype: Added throughout — DataType regex/kind mapping, KnownTypes, exports, builder bindings.
  • IRValue/PlainValue/materialize_ir_value: __bool__ raises TypeError on IR values; __not__ replaced by logical_not(); added max()/min() methods; type inference refactored (bool→int1, int→int_, float→float_).
  • BaseRange/range: BaseRange extracted as base class with handle_op() hook; range inherits from it.

2.4 Pybind11 Bindings (python/src/)

  • IR.cpp: Added ir.attr submodule, bindings for more builtin types (bfloat16, math dialect), clone_shaped_type, TypedAttr, get_splat_attr, get_dict_of_int_attr, ub_block_size; registered math dialect in context.
  • OpBuilder.cpp: New PyOpBuilder(Operation*) constructor; new bindings for bfloat16 type/constants/attrs, bool/i8/i16/f16/f32/f64 attrs, i32/bool array attrs; new arith ops (MaxSI, MinSI, MaximumF, MinimumF, ShLI, ShRSI, generic ConstantOp); full math dialect op bindings.
  • Passes.cpp: New pass bindings for all new Asc passes (with parameterized options for hoist_tensor_allocation, legalize_kernel_args, materialize_tensor, reuse_ub_allocation); add_hoist_ub_allocation removed (replaced by add_hoist_tensor_allocation).

2.5 Build System (setup.py)

  • LLVM auto-download removed; LLVM must be provided via LLVM_INSTALL_PREFIX.
  • Compiler/linker configuration generalized: PYASC_SETUP_COMPILER, PYASC_SETUP_LINKER, PYASC_SETUP_CMAKE_APPEND, PYASC_SETUP_JOBS env vars; PYASC_SETUP_CLANG_LLD now sets defaults instead of hardcoding.
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 Maksim Vlasov 的贡献)
Maksim VlasovMaksim Vlasov
8月11日 创建了 pull request,commit 16e0b2c5
Maksim VlasovMaksim Vlasov
8月11日 关联了issue:Changes in common infrastructure to support tile-based PyAsc extension
atomgit-bot
atomgit-bot
8月11日 评论:

变更摘要

本次 PR 对 PyAsc 基础设施进行了大规模升级,主要涉及:引入 AscendC 寄存器 API 操作族(RegOpBinaryRegOpUnaryRegOp 等)及其对应的 IR 定义与 C++ 代码生成;新增 BroadcastOpLayerNormOpReduceOp 族等高阶算子;对接口体系进行重构,新增 OpWithSrcInterfaceOpWithReusableSrcInterfaceCopyToL0OpInterface 等接口,并将 CopyDirection 从枚举改为结构体属性;同时将 HoistUBAllocation 重命名为 HoistTensorAllocation 并增加 excludeInOut 选项,MaterializeTensorPass 增加 alwaysBuf 选项。Python 侧新增 bfloat16 数据类型、性能分析模块 lib/profilingKernelMeta/LaunchedKernel 内核元数据类,并添加了 C310 架构的编译支持。

主要改动

  • 新增寄存器 API 操作体系:在 include/ascir/Dialect/Asc/IR/Basic/OpReg.td 中新增了 AddRegOpMulRegOpAbsRegOpExpRegOpDataCopyLoadOpDataCopyStoreOpUpdateMaskOpRegTensorOpCreateMaskOpSelectRegOp 等 30+ 个寄存器级操作定义,并在 Base.td 中引入了 RegOpBinaryRegOpUnaryRegOpVecScalarRegOp 基类模板和对应的 BinaryRegOpInterfaceUnaryRegOpInterfaceVecScalarRegOpInterface 接口

  • 新增高阶算子与类型:新增 BroadcastOpBroadcast.td)、LayerNormOpNormalization.td)、ReduceOp 族(Reduction.td,含 ReduceProdOpReduceMinOpReduceMaxOpReduceSumOp),并增加了 LayerNormTilingLayerNormSeparateTilingLayerNormParaLoopModeParamsDn2NzParams 等 API 类型;向量标量运算新增 Divs/Subs 操作(OpVecBinaryScalar.td

  • 接口体系重构:新增 OpWithSrcInterfaceOpWithReusableSrcInterface 接口,使 UnaryOpInterfaceBinaryOpInterfaceVecScalarOpInterface 等统一继承 OpWithSrcInterface 并实现 getSrcTensors() 方法;新增 CopyToL0OpInterface 抽象 L0 拷贝语义;CopyDirectionI32EnumAttr 改为 AscendC_CopyDirectionAttr 结构体属性(含 src/dst 两个 TPosition 字段)

  • Pass 管线调整与架构支持HoistUBAllocation 重命名为 HoistTensorAllocation,增加 excludeInOut 选项以区分输入输出张量;MaterializeTensorPass 增加 alwaysBuf 选项;LegalizeKernelArgsPass 增加 setFftsAddr 选项;DetectKernelTypePass 重构为按 VectorOp/MmadOp 检测 kernel 类型并写入 asc.kernel_type 属性;新增 CompilationArch 枚举支持 C220/C310 架构,编译器增加 vf_vec_len 向量寄存器长度配置

  • Python 运行时扩展与新增教程:新增 bfloat16 数据类型支持(dtype.pyOpBuilder.cppget_bf16_type 等);新增 lib/profiling 性能分析模块(ProfilerMsprofInterfaceProfilingResult);新增 KernelMetaCompiledKernelLaunchedKernel 数据类用于内核生命周期管理;缓存哈希从 sha256 切换为 blake2b;新增三个 Ascend C 教程文件(01-vector-add.py02-fused-softmax.py03-matrix-multiplication.py

likedislike
不准确?
atomgit-bot
atomgit-bot
8月11日 评论:

🤖 AI Code Review

⚠️ 审查未能完成

本次代码审查未能完成,可能是临时性故障。

你可以在评论区输入 /ai review 重新发起审查。若多次失败,请联系管理员并附上下方追踪 ID。

追踪 ID: task-177966-run-170670

likedislike
不准确?
CANN-robotCANN-robot成员
8月11日 添加了label:cann-cla/yes
此处折叠了386条消息 查看更多
CANN-robotCANN-robot成员
14 天前 添加了label:ci-pipeline-running
CANN-robotCANN-robot成员
14 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
14 天前 添加了label:ci-pipeline-passed
CANN-robotCANN-robot成员
14 天前 关闭了关联的issue
CANN-robotCANN-robot成员
14 天前 合入了pull request