[2026春季][T1-2-1] goog00#182
Open
goog00 wants to merge 1 commit into
Open
Conversation
…elision and stride folding Signed-off-by: sunteng <steng2009@163.com>
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.
justdoit_九齿编译优化_T1-2-1_总结报告
一、概括
在 NineToothed 现有 AOT variant 特化基础上,新增两类代码生成特化,把可靠识别的弱势场景下沉到生成阶段,生成更精简的源;不满足条件时字节级回退原路径。
Divisible tile fast path:当内层 tile 可整除对应维度时,静态消除恒为真的上界 mask;当整条 mask 折叠为
True时,在tl.load/tl.store中省去mask=/other=参数。Contiguous fast path:当某维 stride == 1 时,将
offsets * stride折叠为offsets,消除冗余指针算术。弱势场景根因:现有特化只影响 Triton 侧签名,Python 端生成源对所有 variant 共用一份——命中 divisibility 后 mask 仍写在源里、命中 contiguity 后
offset * stride仍展开,靠下游编译器折叠。二、实现
提示通道:新增
src/ninetoothed/specialization.py,定义SpecializationHints(divisible_dims, contiguous_dims),键为(source_name, dim),把 AOT variant 的divisibility_spec/contiguity_spec下沉到代码生成阶段。per-variant 重跑 codegen:
aot.py不再让所有 variant 共用一份make()的源,而是对每个 variant 用其自身 spec 调_hints_from_spec生成 hints,再跑CodeGenerator(specialization_hints=hints)产出专属源;JIT 路径与 fallback variant 用hints.empty(),行为不变。保守启用条件(宁缺毋滥):
mask 消除仅作用于恒为真的上界项(
tensor.py::offsets);额外护栏_is_divisibility_safe:算子历史必须含tile、且不含pad/_slice_dim,排除 padding/slice 误命中;dispatcher 整除检查由
shape[dim] % 16 == 0强化为shape[dim] % BLOCK_SIZE == 0;stride 折叠仅作用于
stride == 1的维度(generation.py::_generate_overall_offsets_and_mask)。正确性:mask 消除只针对整除保证下无越界的恒真项,stride 折叠只针对 stride==1 维——两者与原表达式数学等价,不改变计算语义。
变更文件:
specialization.py(新增) ·generation.py·tensor.py·aot.py·symbol.py·tests/test_specialization.py(新增) ·bench/run_specialization_bench.py(新增)。三、实验数据
环境:RTX 4090D(24GB, sm_89)/ CUDA 12.8 / driver 570.124.06 / NGC PyTorch (torch 2.6) / Triton 3.1.0 / Python 3.12。
3.1 正确性
3.2 生成代码指标(同内核内 命中 variant vs int64 fallback variant)
均超 ≥0.25 门槛。由
test_generated_source_mask_reduction_add/test_generated_source_stride_reduction_copy断言「命中 variant 严格少于 fallback」。benchmark 各命中变体所选源计数(10 场景,f32):

特化只在安全时触发:非整除仍保留 mask、非连续仍保留 stride 乘法。
3.3 运行时(baseline = hints 置空的同内核)
命中场景 1.00–1.04×;回退场景 1.00–1.03×(均 ≥1.0,无退化)。
speedup 接近 1.0 的原因:消除的是生成源层面的恒真 mask 与
stride×1,Triton/LLVM 编译期本就会折叠这些冗余;且 add/copy 为访存受限算子,运行时由带宽决定。定位为「结构改善 + 运行时零回退」。数据可复现:
pytest tests/test_specialization.py、pytest -q、python bench/run_specialization_bench.py。Honor Code.pdf