| 874 | namespace strata::core { |
| 875 | |
| 876 | bool session_capture_token(const WeightTable& tables, const ModelGeometry& g, SessionState& s, float* parts_dev, |
| 877 | const float* y_miss_host, size_t parts_bytes, TokenGraph& tg, std::string& err, |
| 878 | const TokenHits* hits) { |
| 879 | if (hits != nullptr && !hits->on()) { err = "session_capture_token: incomplete hit configuration"; return false; } |
| 880 | if (tg.captured) return true; |
| 881 | if (s.db == nullptr || s.db->d_flag == nullptr || s.db->d_seq == nullptr) { |
| 882 | err = "session_capture_token: the doorbell has no flag"; |
| 883 | return false; |
| 884 | } |
| 885 | if (parts_dev == nullptr || y_miss_host == nullptr || parts_bytes == 0) { |
| 886 | err = "session_capture_token: parts buffers are required"; |
| 887 | return false; |
| 888 | } |
| 889 | float* y_dev = nullptr; |
| 890 | if (cudaHostGetDevicePointer((void**) &y_dev, const_cast<float*>(y_miss_host), 0) != cudaSuccess || !y_dev) { |
| 891 | err = "session_capture_token: the parts staging is not mapped pinned memory"; |
| 892 | return false; |
| 893 | } |
| 894 | cudaStream_t cs = nullptr; |
| 895 | if (cudaStreamCreate(&cs) != cudaSuccess) { err = "session_capture_token: stream create failed"; return false; } |
| 896 | if (cudaStreamBeginCapture(cs, cudaStreamCaptureModeThreadLocal) != cudaSuccess) { |
| 897 | cudaStreamDestroy(cs); |
| 898 | err = "session_capture_token: begin capture failed"; |
| 899 | return false; |
| 900 | } |
| 901 | int64_t qsa_index = 0; |
| 902 | bool ok = true; |
| 903 | for (int64_t l = 0; l < g.n_layers && ok; ++l) { |
| 904 | gdn_point_at(g, l, s); |
| 905 | const bool qsa = is_qsa_layer(g, l); |
| 906 | QsaState& qst = qsa ? s.qsa_states[qsa_index] : s.qsa_states[0]; |
| 907 | err.clear(); |
| 908 | ok = block_layer_pre(tables, g, l, 0, 0, s.gdn, qst, s.qsa_bufs, s.moe, s.k, s.block, (void*) cs, err, s.db, |
| 909 | s.ple.ready() ? &s.ple : nullptr); |
| 910 | if (!ok) { err = "session_capture_token: pre layer " + std::to_string(l) + ": " + err; break; } |
| 911 | if (hits != nullptr) { |
| 912 | // After the ring (and the shared expert): the GPU's experts run while the CPU computes the misses. |
| 913 | strata::kernels::moe_hit_select(s.moe.ids, hits->d_res + l * hits->n_expert, (int) s.k, |
| 914 | (int) hits->n_expert, hits->d_slot, hits->d_dst, hits->d_count, (void*) cs); |
| 915 | strata::kernels::quantize_q8_0_scaled(s.block.mixed, hits->x_q8, hits->x_scale, g.n_embd, (void*) cs); |
| 916 | strata::kernels::moe_hit_grouped_s2_dev(hits->cache_base, hits->d_slot, hits->d_dst, hits->d_count, s.k, |
| 917 | hits->blob, hits->x_q8, hits->scratch, hits->hit_out, (void*) cs, |
| 918 | hits->x_scale); |
| 919 | } |
| 920 | strata::kernels::doorbell_wait(s.db->d_flag, s.db->d_seq, (void*) cs); |
| 921 | // A kernel, not a memcpy node: a copy-engine node splits the WDDM submission (measured 67 flushes/token). |
| 922 | strata::kernels::copy_from_mapped(parts_dev, y_dev, (int64_t) (parts_bytes / sizeof(float)), (void*) cs); |
| 923 | if (hits != nullptr) |
| 924 | strata::kernels::moe_hit_add(parts_dev, hits->hit_out, hits->d_dst, hits->d_count, s.k, g.n_embd, (void*) cs); |
| 925 | ok = block_layer_post(tables, g, l, s.k, s.moe, s.block, parts_dev, (void*) cs, err); |
| 926 | if (!ok) { err = "session_capture_token: post layer " + std::to_string(l) + ": " + err; break; } |
| 927 | if (qsa) ++qsa_index; |
| 928 | } |
| 929 | cudaGraph_t graph = nullptr; |
| 930 | const cudaError_t ce = cudaStreamEndCapture(cs, &graph); |
| 931 | cudaStreamDestroy(cs); |
| 932 | if (!ok) { if (graph) cudaGraphDestroy(graph); return false; } |
| 933 | if (ce != cudaSuccess) { |
nothing calls this directly
no test coverage detected