diff --git a/npu/target/xdna/xdna_target.cpp b/npu/target/xdna/xdna_target.cpp index def6a0534..9e2e5a1f3 100644 --- a/npu/target/xdna/xdna_target.cpp +++ b/npu/target/xdna/xdna_target.cpp @@ -447,7 +447,21 @@ int64_t XdnaTarget::m16_check_gemm_rows(I8Ctx& ref, I8Ctx& fast, const float* A, void XdnaTarget::prof_add(ProfAcc* acc, int design, double pack, double quant, double wait, double rb) { - if (q4nx_lh::phprof_on() && acc == prof_dec_ && !q4nx_lh::g_in_head) { + // g_ph is what the [bs] (batch) and [ph] (decode step) lines print, and both read it. Only the DECODE + // accumulator used to be counted, so a 219-row prefill -- which launches through prof_pre_ -- reported + // "n=0 launches, host=93113 ms" for 224 launches and 93 s of device wait: the wait fell into the line's + // host= residual and the launch count stayed zero, which reads exactly like a host fallback and sent one + // lane (engine #1582) looking for a fallback that does not exist. The batched body sets g_in_batch for + // its whole duration, so accepting prof_pre_ there counts those launches and keeps a decode-only + // measurement decode-only. + // The head is NEVER counted here, and that is the second half of this gate. `g_in_batch` stays set for the whole of + // step_batch, and the batched head (run_lm_head_rows -> note_head -> prof_add(&prof_head_, ...)) runs inside that + // window without setting g_in_head -- which only the single-row head does. So accepting "any accumulator while in a + // batch" put the head's device time into g_ph as well, where [bs] computes host = layers - gsum: the same time was + // then reported twice, once in gsum and once on the epilogue line's own h_* slices, and host was understated by it + // (it can go negative) on exactly the multi-row passes this line exists for. The head keeps its own accounting. + if (q4nx_lh::phprof_on() && acc != &prof_head_ && + (acc == prof_dec_ || (q4nx_lh::g_in_batch && acc == prof_pre_)) && !q4nx_lh::g_in_head) { q4nx_lh::g_ph.g_pack += pack; q4nx_lh::g_ph.g_ql += quant; q4nx_lh::g_ph.g_wait += wait; q4nx_lh::g_ph.g_rb += rb; q4nx_lh::g_ph.launches++;