MCPcopy Create free account
hub / github.com/Niko1221/Strata / session_loop

Function session_loop

engine/src/core/session.cpp:614–826  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

612}
613
614bool session_loop(const ModelGeometry& g, int64_t pos, int32_t pos_base, SessionState& s, SessionGraphs& gr,
615 PoolFn pool, HitFn hits, void* user, bool overlap, void* stream, std::string& err,
616 float* dump_layers, SessionLoopScratch* scratch) {
617 if (!gr.captured || gr.n != g.n_layers) { err = "session_loop: not captured"; return false; }
618 if (gr.parts_dev == nullptr) { err = "session_loop: the graphs were captured without a parts buffer"; return false; }
619 if (s.db == nullptr) { err = "session_loop: no doorbell; the loop has nothing to poll"; return false; }
620
621 cudaStream_t cs = (cudaStream_t) stream;
622 const int64_t k = s.k;
623 const size_t parts_bytes = (size_t) k * g.n_embd * 4;
624 ++gr.calls_total;
625
626 // ================================ THE POSITION, WHICH THIS LOOP NEVER STAGED ================================
627 //
628 // **EVERY TOKEN AFTER THE FIRST USED TO REPLAY POSITION 0.** The QSA layers read their per-token counts from
629 // `st.step` and `st.pos_dev`, which are DEVICE buffers filled by an H2D copy captured INTO each graph. A
630 // graph bakes the SOURCE POINTER, not the value - so the copy re-reads the pinned host buffers on every
631 // replay, and those hold whatever was last written there. `session_capture` left them at position 0.
632 //
633 // `layer.hpp` documents the requirement this loop was missing: "The per-token staging every QSA layer's
634 // captured H2D reads FROM. Must run before each replay: the graphs captured the SOURCE POINTER, not the
635 // value, and that is exactly why the buffers are pinned and fixed." `session_replay` calls `stage_token`;
636 // `session_loop` did not. **A single-token test cannot see a position that never advances** - which is why
637 // the whole suite passed while a 20-token prompt rotated every token at position 0.
638 stage_token(g, pos, pos_base, s);
639
640 // ---- **THE TOKEN PATH ALLOCATES NOTHING (P2.T10, review finding H3).**
641 //
642 // Everything this loop needs on the host - the pinned staging for the pool's answer, the doorbell probe
643 // event, and the host pin - lives in a `SessionLoopScratch` owned by the SESSION and passed in. It used to
644 // be created here, once per token, and `cudaFreeHost` at the end of the call IMPLICITLY SYNCHRONISES THE
645 // DEVICE, so every token finished with a device-wide sync that nothing had asked for. A comment here said
646 // "allocated once"; once per token is not once.
647 //
648 // Passing `nullptr` keeps the old behaviour so existing callers and tests are unaffected: the loop builds a
649 // local scratch and frees it on every exit path below, including the error returns.
650 SessionLoopScratch local;
651 if (scratch == nullptr) {
652 if (!local.init(parts_bytes, err)) return false;
653 scratch = &local;
654 }
655 struct LocalFree {
656 SessionLoopScratch* p;
657 ~LocalFree() { if (p != nullptr) p->free(); }
658 } local_free{scratch == &local ? &local : nullptr};
659 if (scratch->parts_bytes != parts_bytes) {
660 err = "session_loop: the scratch was sized for a different k or n_embd";
661 return false;
662 }
663
664 // HOST-side staging for the pool's answer. It is copied to `gr.parts_dev` before the next launch, on the
665 // same stream, so the ordering is the stream's and no captured node is needed for it.
666 //
667 // ---- **PINNED, BECAUSE A COPY FROM PAGEABLE MEMORY IS NOT ASYNCHRONOUS AT ALL.**
668 //
669 // `cudaMemcpyAsync` with a pageable source cannot DMA: the driver first memcpys the bytes into an internal
670 // pinned bounce buffer, SYNCHRONOUSLY, on this thread, and only then enqueues the transfer. So the "async"
671 // copy would be a blocking memcpy plus a driver round trip, on the critical path of all 48 layers. The

Callers

nothing calls this directly

Calls 15

strata_load_fenceFunction · 0.85
poolFunction · 0.85
stage_tokenFunction · 0.70
doorbell_resetFunction · 0.70
cudaMemcpyAsyncFunction · 0.50
stringFunction · 0.50
cudaGetErrorStringFunction · 0.50
nowFunction · 0.50
cudaGraphLaunchFunction · 0.50
cudaEventRecordFunction · 0.50
cudaEventQueryFunction · 0.50
to_stringFunction · 0.50

Tested by

no test coverage detected