Skip to content

[2026春季][T1-2-1] goog00#182

Open
goog00 wants to merge 1 commit into
InfiniTensor:masterfrom
goog00:2026-spring-goog00-T1-2-1
Open

[2026春季][T1-2-1] goog00#182
goog00 wants to merge 1 commit into
InfiniTensor:masterfrom
goog00:2026-spring-goog00-T1-2-1

Conversation

@goog00

@goog00 goog00 commented Jul 12, 2026

Copy link
Copy Markdown

justdoit_九齿编译优化_T1-2-1_总结报告


一、概括

在 NineToothed 现有 AOT variant 特化基础上,新增两类代码生成特化,把可靠识别的弱势场景下沉到生成阶段,生成更精简的源;不满足条件时字节级回退原路径。

  1. Divisible tile fast path:当内层 tile 可整除对应维度时,静态消除恒为真的上界 mask;当整条 mask 折叠为 True 时,在 tl.load/tl.store 中省去 mask= / other= 参数。

  2. 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 重跑 codegenaot.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 正确性

image

3.2 生成代码指标(同内核内 命中 variant vs int64 fallback variant)

image

均超 ≥0.25 门槛。由 test_generated_source_mask_reduction_add / test_generated_source_stride_reduction_copy 断言「命中 variant 严格少于 fallback」。

benchmark 各命中变体所选源计数(10 场景,f32):
image

特化只在安全时触发:非整除仍保留 mask、非连续仍保留 stride 乘法。

3.3 运行时(baseline = hints 置空的同内核)

image
  • 命中场景 1.00–1.04×;回退场景 1.00–1.03×(均 ≥1.0,无退化)。

  • speedup 接近 1.0 的原因:消除的是生成源层面的恒真 mask 与 stride×1,Triton/LLVM 编译期本就会折叠这些冗余;且 add/copy 为访存受限算子,运行时由带宽决定。定位为「结构改善 + 运行时零回退」。

数据可复现:pytest tests/test_specialization.pypytest -qpython bench/run_specialization_bench.py

实测原始输出(RTX 4090D):

image

Honor Code.pdf

…elision and stride folding

Signed-off-by: sunteng <steng2009@163.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant