import os p = os.path.expanduser("~/work/numerics-orig/source/src/core/verify.cpp") s = open(p).read() old = r''' VDBG("window done\n"); ++windows;''' assert s.count(old) == 1 new = r''' if (const char* dir = std::getenv("STRATA_NUM_RESID_DIR")) { // numerics bring-up: final R of every window row static int seq = 0; const size_t rn = (size_t) T * (size_t) g.hc * (size_t) g.n_embd; std::vector h(rn); cudaStreamSynchronize(cs_); if (cudaMemcpy(h.data(), R_, rn * 4, cudaMemcpyDeviceToHost) == cudaSuccess) { char path[512]; std::snprintf(path, sizeof path, "%s/r_win%05d_pos%lld.f32", dir, seq, (long long) pos0); if (std::FILE* f = std::fopen(path, "wb")) { std::fwrite(h.data(), 4, h.size(), f); std::fclose(f); } std::fprintf(stderr, "NUMRESID seq=%d pos0=%lld T=%d tokens=", seq, (long long) pos0, T); for (int t = 0; t < T; ++t) std::fprintf(stderr, "%d%s", tokens[t], t + 1 < T ? "," : "\n"); } ++seq; } ''' s = s.replace(old, new + old) open(p, "w").write(s) print("patched")