[WIP]solve fp4 -> bf16 layout - #1443
Open
Likai-19 wants to merge 1 commit into
Open
Conversation
Contributor
|
Should add unit test / lit test? |
Contributor
|
Some remaining problems https://github.com/learning-chip/TileKernels-vmi/pull/38#issuecomment-5514398503 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
1. 背景与问题
FP4 cast_back VMI kernel(512×2048, e2m1 packed UE8M0, out bf16)在 PTOAS lowering 后精度错误:bit_mismatch 约 47%,maxabs 约 3.0。
VPTO 中 FP4 数据装载为:
UNPK_B8的语义是contiguous, lane_stride = 2:紧凑流元素x[i]落在物理寄存器 lane2*i,即有效数据位于 P0 和 P2 两个 mod-4 lane 分区(P1/P3 是 padding)。但
vmi-to-vpto的OneToNVMIExtFOpPattern对 sourceBits==8 的 packed4 转换按P0, P1顺序取 part:P1 是 padding,P2 的有效数据被漏掉,正好产生约一半错误。
2. 根因
两个独立的 layout 问题叠加:
ls(2)的 packed4 布局,vldslower 成单条UNPK_B8,后续vcvt必须跨 P0/P2 取数;而OneToNVMIExtFOpPattern的 part 选择逻辑按连续P0..P3取,不感知 source 的lane_stride,导致错取 P1。vsldb + vselr;旧 SF 表布局下vsldbbase 只有 16B 对齐,触发ACL_ERROR_RT_VECTOR_CORE_EXCEPTION (507035)。3. 改动内容
3.1
lib/PTO/Transforms/VMILayoutSupportTables.inc在
kPreferredCastLayoutPatterns中新增一行 preferred cast layout:{bits<8>(), bits<32>(), 128, ls(4), c()},效果:8bit source → 32bit result、128 lane 的 FP4 vload 优先以
lane_stride = 4的 contiguous 布局 lower,生成两条UNPK4,每个物理寄存器有效数据全部落在 P0,vcvt只取 P0 即可。3.2
lib/PTO/Transforms/VMILayoutAssignment.cpp两处改动:
getPreferredGroupBroadcastLoadLayout():当 E2B direct fact 的 result layout 是 deinterleaved,而 contiguous 形式物理 arity > 1 且存在 generic 支持时,返回 contiguous 优先(genericgroup_slots -> contiguous),避免后续vldsx2/vintlv式 deinterleave materialization。VMILoadOp的自然 layout 约束:小 element count(小于lanesPerPart且整除)的 vload 设为contiguous(lane_stride),补齐 SIMT 侧小粒度 load 的 layout 推导。3.3
lib/PTO/Transforms/VMIToVPTO.cpp两处改动:
OneToNVMIGroupBroadcastLoadOpPattern:在canUseDirectE2B路径上增加保护——当 result layout 为 contiguous 且chunks_per_part != 1时,一个 E2B packet 实际填不满一个物理 part,无法复用单个 packet,此时canUseDirectE2B = false,走 generic fallback,不再 hard fail 或生成错误指令。OneToNVMIExtFOpPattern:sourceBits==16 / sourceBits==8 两条 packed 转换路径,part factor 从硬编码2/4改为由resultTypes.size() / sourceParts.size()推导,并增加边界校验(factor 不合法时直接notifyMatchFailure),避免在异常 result 拆分下静默生成错误 part 序列。4. 生成的 VPTO 效果
4.1 FP4 值转换路径(核心修复)
修复前(
FP4_修复前_原始问题_UNPK_B8_P0P1_scratch_47pct.vpto):P1是 padding,P2有效数据丢失。修复后(
FP4_修复后_final_UNPK4_P0_SF32Bslot_half16_bit_exact.vpto):两条
UNPK4,每个物理寄存器有效数据全在 P0,vcvt只取 P0;数据位序与 CCE 参考实现一致。4.2 scale 消费路径
修复前(直接 E2B + deinterleave 物化路径):
修复后(generic contiguous 路径):
不再出现
vdintlv/vintlv的 deinterleave 物化,scale 消费以 contiguous 形式完成。5. 验证
ptoas --pto-backend=vpto --emit-vpto:FP4 vcvt 段生成两条UNPK4+ 两个vcvt {part = "P0"},与预期一致。maxabs = 0.0,bit_mismatch = 0/1048576,与参考实现 bit-exact。