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

Method context

engine/src/core/dflash2.cpp:1044–1143  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1042}
1043
1044bool Dflash2Drafter::context(const float* taps, int64_t n, int64_t pos0, std::string& err) {
1045 if (!context_wait(err)) return false;
1046 const OnDevice on_device(device_);
1047 const int tap_n = (int) taps_.size() * N_;
1048 static const bool batch_on = [] {
1049 const char* e = std::getenv("STRATA_DF2_CTX_BATCH");
1050 return !(e && e[0] == '0');
1051 }();
1052 if (batch_on && n > kRows && prof_ == nullptr && ensure_ctx_batch(err)) {
1053 auto* cb = ctx_batch_;
1054 constexpr int B = CtxBatch::kB;
1055 h_meta_[18] = epoch_;
1056 std::atomic_thread_fence(std::memory_order_seq_cst);
1057 kernels::copy_i32_from_mapped(d_meta_, m_meta_, 32, cs_);
1058
1059 bool slot_pend[2] = {false, false};
1060 int64_t slot_p0[2] = {-1, -1};
1061 int slot_cnt[2] = {0, 0};
1062 auto flush_slot = [&](int s) -> bool {
1063 if (!slot_pend[s]) return true;
1064 slot_pend[s] = false;
1065 if (cudaEventSynchronize(cb->ev[s]) != cudaSuccess) {
1066 err = std::string("dflash2: ") + cudaGetErrorString(cudaGetLastError());
1067 return false;
1068 }
1069 if (log_on_) {
1070 for (int i = 0; i < slot_cnt[s]; ++i) {
1071 const int64_t pos = slot_p0[s] + i;
1072 LogChunk& ch = cur_.writable((size_t) (pos / kLogChunk));
1073 const size_t r = (size_t) (pos % kLogChunk);
1074 std::memcpy(&ch.q[r * 2560], cb->h_logq[s] + (size_t) i * 2560, 2560);
1075 std::memcpy(&ch.d[r * 80], cb->h_logd[s] + (size_t) i * 80, 80 * sizeof(float));
1076 ch.ok[r] = 1;
1077 }
1078 }
1079 return true;
1080 };
1081
1082 try {
1083 int b_idx = 0;
1084 for (int64_t r0 = 0; r0 < n; r0 += B, ++b_idx) {
1085 const int m = (int) std::min<int64_t>(B, n - r0);
1086 const bool need_kv = (r0 + m) > (n - kRing);
1087 if (!log_on_ && !need_kv) continue;
1088 const int s = b_idx & 1;
1089 if (log_on_ && !flush_slot(s)) return false;
1090
1091 kernels::df2_iota_pos(cb->pos, (int32_t) (pos0 + r0), m, cs_);
1092 kernels::df2_quant_rows(taps + (size_t) r0 * (size_t) tap_n, tap_n, m, cb->xqc, cb->xdc, cs_);
1093 gemm({"fc.weight"}, {cb->fcy}, m, cs_, cb->xqc, cb->xdc);
1094 kernels::df2_rms_quant(cb->fcy, f32("hidden_norm.weight"), nullptr, m, N_, eps_, cb->xqc, cb->xdc, cs_);
1095 if (log_on_) {
1096 cudaMemcpyAsync(cb->h_logq[s], cb->xqc, (size_t) m * N_, cudaMemcpyDeviceToHost, cs_);
1097 cudaMemcpyAsync(cb->h_logd[s], cb->xdc, (size_t) m * (N_ / 32) * sizeof(float), cudaMemcpyDeviceToHost, cs_);
1098 cudaEventRecord(cb->ev[s], cs_);
1099 slot_pend[s] = true;
1100 slot_p0[s] = pos0 + r0;
1101 slot_cnt[s] = m;

Callers 4

mainFunction · 0.45
feedFunction · 0.45
ui-flows.mjsFile · 0.45
ui-do-drive.mjsFile · 0.45

Calls 15

memcpyFunction · 0.85
df2_iota_posFunction · 0.85
df2_quant_rowsFunction · 0.85
df2_rms_quantFunction · 0.85
df2_rope_table_invFunction · 0.85
df2_kv_ringFunction · 0.85
record_contextFunction · 0.85
writableMethod · 0.80
copy_i32_from_mappedFunction · 0.50
cudaEventSynchronizeFunction · 0.50
stringFunction · 0.50
cudaGetErrorStringFunction · 0.50

Tested by 1

feedFunction · 0.36