Repository navigation
perf(jit): reduce host overhead for eager single-operator calls - #2954
Conversation
Share signature introspection through the request-local namespace snapshot while preserving per-call argument binding and annotation resolution. Refresh signatures on the next request rather than retaining them across calls. Add coverage for shared queries, default and annotation updates, custom signatures, failure cleanup and thread isolation. Validate CSA eager host latency, exact output/cache equivalence and before/after profiling.
Render exact None, bool, int and str values directly with repr, matching the existing ast.Constant unparser output. Route free-name text through the same renderer while continuing to read current bindings on every request. Keep floats, subclasses, containers and DSL symbols on the existing path. Cover literal text equivalence, fresh-value fast paths, rebinding, container mutation, custom repr and metaclass equality. Validate 833 JIT tests and exact CSA output/Top-K/cache equivalence; the B1/128K/S6 TMR eager host median decreased from 8.888 ms to 7.784 ms in the measured workload.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
3cc5c61
into
hw-native-sys:feat/kernel-mode-integration-test
问题与目的
延续 #2953 的 非aclgraph场景下host开销性能优化。已从 无优化 103.49 ms -----------> #2953 优化后 11.25 ms
当前即使 Eager 调用命中已编译 kernel,JIT 仍会在不同 helper 中重复解析同一个函数签名,并把已经取得的普通常量值先构造成 AST、再转换为源码文本。本 PR 减少这两部分 Host 准备开销。验证后耗时优化到 7.784 ms。
包含两个独立 commit,分别对应以下改动, 都是逻辑等价优化;不改变 kernel 计算、调度器或提交协议。
改动一:请求内复用函数签名
python/pypto/jit/decorator.py新增_function_signature,使用已有的@cache_in_snapshot;六处签名查询统一经由该 helper。参数绑定、默认值应用和注解求值仍按原路径执行,不缓存实参绑定结果或整个 specialization。跨请求修改默认值、注解和自定义
__signature__后会重新读取;嵌套调用共享当前快照,独立线程隔离,异常退出也清理快照。与现有请求快照语义一致。改动二:普通常量文本直接生成
python/pypto/jit/specializer.py 的 constant_source 对精确的 None/bool/int/str 直接使用 repr;free_name_source 读取当前值后复用同一转换逻辑。
原来:当前值 128 ----> ast.Constant(128) ----> ast.unparse ----> "128"
现在:当前值 128 ----> repr(128) ----> "128"
默认 AST unparser 对这些类型原本就使用 repr,因此仅省去 AST 构造和遍历。精确类型用 is 检查,避免子类或元类自定义相等比较干扰。浮点数(含 NaN/Inf、负零)、子类、容器、枚举和 DSL 符号保留原转换流程。仍读取当前绑定,不新增跨请求缓存,不缓存表达式的 eval 结果或 Python 编译对象。 所以这个优化直白简单描述一下是干了什么
• 就是把常量变成源码字符串时,省掉中间绕路。
性能验证
单卡 TMR PTO Eager,B1 / 128K 历史 / S6 / TP1,BF16 hidden、正式 C4 层 W8A8 权重,NZ2,
atomic_add=0、deterministic_level=0,合成独立物理 Cache 历史。以下为无 profiler 的 Host 调用中位数,各版每段 8 次:b63d2031→ 请求内签名复用80336c7d→ 常量文本快速生成两组为不同轮次,不将百分比直接相加。Phase 3 插桩后的另一段无 profiler 测量为 8.765 → 7.772 ms,方向一致。
轻量插桩分段如下,单位 ms、各版 8 次均值;父子边界为包含关系,不能相加:
_resolve_constexpr_bindings_get_source_hash_resolve_specialization独立 cProfile 的 3 次调用中,Phase 2 的
inspect.signature次数从 645 降至 168;cProfile 绝对耗时不作为性能结论。Phase 3 的主要收益来自常量文本处理,未优化参数特化。两阶段均保存优化前后 CANN Level1 CPU/NPU profiling。Phase 3 的 CANN Host 包络为 9.498 → 7.966 ms,设备 kernel 中位数为 483.71 → 470.84 μs,每版 8 个完整设备任务。设备仍约 0.5 ms,独立轮次差异不归因为本次核内提速。本 PR 改动位于 HBG/TMR 共用准备逻辑,但本轮只测 TMR 单算子,不将该比例外推到 HBG、ACLGraph replay 或整模型吞吐。
正确性与回归
tests/ut/jit/:833 passed;最后补强新字面量用例后定向回归:26 passed。git diff --check通过;提交前确认源码与已完成 NPU 验证的快照一致。