| 225 | } |
| 226 | |
| 227 | bool session_capture(const WeightTable& tables, const ModelGeometry& g, SessionState& s, const float* parts, |
| 228 | SessionGraphs& gr, std::string& err, bool split, int64_t layer_lo, int64_t layer_hi) { |
| 229 | if (gr.captured) return true; |
| 230 | if (layer_hi < 0 || layer_hi > g.n_layers) layer_hi = g.n_layers; |
| 231 | if (layer_lo < 0) layer_lo = 0; |
| 232 | gr.execs = new cudaGraphExec_t[(size_t) g.n_layers](); |
| 233 | gr.posts = new cudaGraphExec_t[(size_t) g.n_layers](); |
| 234 | for (int64_t i = 0; i < g.n_layers; ++i) gr.posts[i] = nullptr; |
| 235 | if (split) { |
| 236 | gr.preA = new cudaGraphExec_t[(size_t) g.n_layers](); |
| 237 | gr.preB = new cudaGraphExec_t[(size_t) g.n_layers](); |
| 238 | for (int64_t i = 0; i < g.n_layers; ++i) { gr.preA[i] = nullptr; gr.preB[i] = nullptr; } |
| 239 | for (int k = 0; k < 5; ++k) { |
| 240 | gr.preP[k] = new cudaGraphExec_t[(size_t) g.n_layers](); |
| 241 | for (int64_t i = 0; i < g.n_layers; ++i) gr.preP[k][i] = nullptr; |
| 242 | } |
| 243 | } |
| 244 | gr.split_captured = split; |
| 245 | gr.n = 0; |
| 246 | |
| 247 | int64_t qsa_index = s.qsa_ord0; |
| 248 | for (int64_t l = layer_lo; l < layer_hi; ++l) { |
| 249 | gdn_point_at(g, l, s); |
| 250 | const bool qsa = is_qsa_layer(g, l); |
| 251 | QsaState& qst = qsa ? s.qsa_states[qsa_index] : s.qsa_states[s.qsa_primary()]; |
| 252 | |
| 253 | // **TWO GRAPHS PER LAYER, SPLIT AT THE ROUTER.** `pre` ends with the doorbell rung; the host then runs |
| 254 | // the CPU pool on what it published; `post` combines those experts with THIS layer's weights. Capturing |
| 255 | // them together is what the old single graph could not do, and the reason it could not is that |
| 256 | // `moe_combine` needs the pool's answer and the pool needs the router's. |
| 257 | auto capture = [&](bool post, cudaGraphExec_t* out, const char* what, int half = 0, |
| 258 | int stage_prefix = 0) -> bool { |
| 259 | cudaStream_t cs = nullptr; |
| 260 | if (cudaStreamCreate(&cs) != cudaSuccess) { |
| 261 | err = std::string("session_capture: stream create failed"); |
| 262 | return false; |
| 263 | } |
| 264 | if (cudaStreamBeginCapture(cs, cudaStreamCaptureModeThreadLocal) != cudaSuccess) { |
| 265 | err = "session_capture: begin failed at layer " + std::to_string(l); |
| 266 | return false; |
| 267 | } |
| 268 | err.clear(); |
| 269 | // R0.9: `half` is 0 for every mode except the split capture, where it selects a prefix of the |
| 270 | // stages so the mixer and the FFN-front-plus-router can be timed separately. |
| 271 | const bool ok = post |
| 272 | ? block_layer_post(tables, g, l, s.k, s.moe, s.block, parts, (void*) cs, err) |
| 273 | : block_layer_pre(tables, g, l, 0, 0, s.gdn, qst, s.qsa_bufs, s.moe, s.k, |
| 274 | s.block, (void*) cs, err, s.db, s.ple.ready() ? &s.ple : nullptr, |
| 275 | half, stage_prefix); |
| 276 | if (!ok) { |
| 277 | err = "session_capture: " + std::string(what) + " layer " + std::to_string(l) + ": " + err; |
| 278 | return false; |
| 279 | } |
| 280 | cudaGraph_t graph = nullptr; |
| 281 | const cudaError_t ce = cudaStreamEndCapture(cs, &graph); |
| 282 | cudaStreamDestroy(cs); |
| 283 | if (ce != cudaSuccess) { |
| 284 | err = "session_capture: " + std::string(what) + " layer " + std::to_string(l) + ": " + |
nothing calls this directly
no test coverage detected