| 80 | } |
| 81 | |
| 82 | uint64_t session_init(const ModelGeometry& g, int64_t max_cells, int64_t k, void* base, SessionState& s, |
| 83 | int64_t layer_lo, int64_t layer_hi) { |
| 84 | if (layer_hi < 0 || layer_hi > g.n_layers) layer_hi = g.n_layers; |
| 85 | if (layer_lo < 0) layer_lo = 0; |
| 86 | uint8_t* p = (uint8_t*) base; |
| 87 | uint64_t used = 0; |
| 88 | auto take = [&](uint64_t bytes) { |
| 89 | uint8_t* r = p + used; |
| 90 | used = align_up(used + bytes, SESSION_STATE_ALIGN); |
| 91 | return r; |
| 92 | }; |
| 93 | |
| 94 | s.max_cells = max_cells; |
| 95 | s.k = k; |
| 96 | s.layer_lo = layer_lo; |
| 97 | s.layer_hi = layer_hi; |
| 98 | const int64_t I = std::max<int64_t>(g.qsa_interval, 1); |
| 99 | const int64_t q_n_range = layer_hi / I - layer_lo / I; |
| 100 | #ifdef STRATA_WITH_LOOP |
| 101 | const int64_t extra_q = strata::kernels::loop_extra_qsa_slots(layer_lo, layer_hi, I); |
| 102 | const int64_t extra_g = strata::kernels::loop_extra_gdn_slots(layer_lo, layer_hi, I); |
| 103 | #endif |
| 104 | s.qsa_ord0 = layer_lo / I; |
| 105 | #ifdef STRATA_WITH_LOOP |
| 106 | s.qsa_alloc = std::max<int64_t>(q_n_range, g.n_qsa_layers() > 0 ? 1 : 0) + extra_q; |
| 107 | #else |
| 108 | s.qsa_alloc = std::max<int64_t>(q_n_range, g.n_qsa_layers() > 0 ? 1 : 0); |
| 109 | #endif |
| 110 | s.gdn_ord0 = layer_lo - layer_lo / I; |
| 111 | #ifdef STRATA_WITH_LOOP |
| 112 | s.gdn_alloc = std::max<int64_t>((layer_hi - layer_lo) - q_n_range, 0) + extra_g; |
| 113 | #else |
| 114 | s.gdn_alloc = std::max<int64_t>((layer_hi - layer_lo) - q_n_range, 0); |
| 115 | #endif |
| 116 | |
| 117 | gdn_buffers_init(g, take(gdn_buffers_bytes(g)), s.gdn); |
| 118 | s.gdn_state = (float*) take((uint64_t) s.gdn_alloc * gdn_state_floats(g) * 4); |
| 119 | |
| 120 | // the QSA states are separate allocations carved from one arena, because `QsaState` is a struct of |
| 121 | // pointers and `qsa_state_init` writes them - a contiguous array would need the arena to be laid out the |
| 122 | // same way, which is a coupling with nothing to gain. Only the range's ordinals are initialized; the |
| 123 | // rest stay value-initialized nulls. The FIRST ALLOCATED one (the session's primary, ordinal qsa_ord0) |
| 124 | // owns the RoPE table that the others - and the prefill staging identity, and the MTP drafter - borrow. |
| 125 | const uint64_t first = qsa_state_bytes(g, max_cells, true), rest = qsa_state_bytes(g, max_cells, false); |
| 126 | s.qsa_state_arena = take(g.n_qsa_layers() > 0 ? first + (uint64_t) (s.qsa_alloc - 1) * rest : 0); |
| 127 | #ifdef STRATA_WITH_LOOP |
| 128 | const int64_t qsa_arr_n = std::max<int64_t>(g.n_qsa_layers() + extra_q, s.qsa_ord0 + s.qsa_alloc); |
| 129 | s.qsa_states = new QsaState[(size_t) qsa_arr_n](); |
| 130 | #else |
| 131 | s.qsa_states = new QsaState[(size_t) g.n_qsa_layers()](); |
| 132 | #endif |
| 133 | s.qsa_buf_arena = take(qsa_buffers_bytes(g, max_cells)); |
| 134 | |
| 135 | uint8_t* qp = (uint8_t*) s.qsa_state_arena; |
| 136 | // a layer whose pinned RAM could not be had (KV streaming's host copy) is half-built: going on would have the |
| 137 | // attention read null host pointers at the first request ("illegal memory access"), so the session fails here |
| 138 | for (int64_t j = 0; j < s.qsa_alloc; ++j) |
| 139 | if (qsa_state_init(g, max_cells, qp + (j == 0 ? 0 : first + (uint64_t) (j - 1) * rest), |
no test coverage detected