Archived
core, abi: scored steps -- rad_declare_scores, RadBatch::score_only, and /v1/systemone's model
A scored request (Request::token_scores, what /v1/systemone asks: the raw logits at a few candidate ids) does not need the half-gigabyte lm_head read the sampler would consume. A plugin whose head is the bf16 embedding table declares the final-normed hidden rows and that table (rad_declare_scores); on a step whose sampled rows are all scored rows and whose program declared both, the core sets RadBatch::score_only, the plugin issues everything but the head, and the core reads the hidden rows and the candidates' table rows back and takes the dot products in f64 on the host. RadBatch gains one trailing int32 (score_only, 360), which the pass key mixes; an older plugin never sees it set. The scheduler issues no step ahead of one that brings a scored row to max_tokens: that forward pass reads nothing and a max_tokens=1 question would pay a decode step after its prefill. Scored rows only -- every other decode keeps the step of overlap it had. /v1/systemone reports which model answered. Co-Authored-By: Pi <pi@localhost>
This commit is contained in:
1 parent
9093bf618e
commit
ed1e5caf9d
16 files changed
+214
-8
No files matched your search
+75
-6
@@ -1198,6 +1198,8 @@ int Engine::save_checkpoints() {
|
||||
* row is copied (512 KB at a 128K vocabulary), which is cheaper than one copy per id. */
|
||||
int Engine::stage_scored_rows(const StepPlan& plan, const StepOut& so) {
|
||||
score_rows_.clear();
|
||||
score_only_ = false;
|
||||
score_ids_.clear();
|
||||
if (embedding_) return RAD_OK;
|
||||
for (size_t i = 0; i < plan.e.size(); ++i) {
|
||||
const Request* r = plan.e[i].req;
|
||||
@@ -1206,16 +1208,35 @@ int Engine::stage_scored_rows(const StepPlan& plan, const StepOut& so) {
|
||||
}
|
||||
if (score_rows_.empty()) return RAD_OK;
|
||||
const int64_t n = (int64_t)score_rows_.size();
|
||||
|
||||
/* A scored step reads back the hidden rows and the candidates' table rows instead of whole
|
||||
* logits rows (BatchBuilder set the flag on the same facts). */
|
||||
const RadBatch* b0 = sched_.batch_for(0);
|
||||
score_only_ = b0 && b0->score_only;
|
||||
if (score_only_) {
|
||||
const Program& p = ranks_[0]->program;
|
||||
score_embd_ = p.buffers[p.score_hidden].decl.shape[1];
|
||||
for (const auto& sr : score_rows_)
|
||||
for (int32_t t : sr.second->tokens) score_ids_.push_back(t);
|
||||
std::sort(score_ids_.begin(), score_ids_.end());
|
||||
score_ids_.erase(std::unique(score_ids_.begin(), score_ids_.end()), score_ids_.end());
|
||||
}
|
||||
for (auto& rk : ranks_) {
|
||||
if (n <= rk->score_rows_cap) continue;
|
||||
const int64_t width = rk->sampler.config().n_vocab_local;
|
||||
/* In floats: n logits rows, or the bf16 hidden and table rows of a scored step. */
|
||||
int64_t rows = n;
|
||||
if (score_only_) {
|
||||
const int64_t need = ((n + (int64_t)score_ids_.size()) * score_embd_ + 1) / 2;
|
||||
rows = std::max(rows, (need + width - 1) / width);
|
||||
}
|
||||
if (rows <= rk->score_rows_cap) continue;
|
||||
if (ranks_.size() > 1) RAD_TRY(rad_dev_set(rk->device));
|
||||
if (rk->score_host) rad_dev_free(rk->score_host, RAD_MEM_HOST_PINNED);
|
||||
const int64_t width = rk->sampler.config().n_vocab_local;
|
||||
rk->score_host = static_cast<float*>(
|
||||
rad_dev_alloc(n * width * (int64_t)sizeof(float), RAD_MEM_HOST_PINNED));
|
||||
rk->score_rows_cap = rk->score_host ? n : 0;
|
||||
rad_dev_alloc(rows * width * (int64_t)sizeof(float), RAD_MEM_HOST_PINNED));
|
||||
rk->score_rows_cap = rk->score_host ? rows : 0;
|
||||
if (!rk->score_host) {
|
||||
RAD_ERR("scored rows: no pinned memory for %lld logits rows", (long long)n);
|
||||
RAD_ERR("scored rows: no pinned memory for %lld logits rows", (long long)rows);
|
||||
return RAD_E_NOMEM;
|
||||
}
|
||||
}
|
||||
@@ -1225,8 +1246,27 @@ int Engine::stage_scored_rows(const StepPlan& plan, const StepOut& so) {
|
||||
|
||||
int Engine::issue_scored_rows(Rank& r) {
|
||||
const int64_t width = r.sampler.config().n_vocab_local;
|
||||
const char* logits = static_cast<const char*>(r.ctx.buf_ptr(r.program.logits_buf));
|
||||
char* const logits = static_cast<char*>(r.ctx.buf_ptr(r.program.logits_buf));
|
||||
if (!logits || !r.score_host) return RAD_E_STATE;
|
||||
if (score_only_) {
|
||||
/* The plugin left the hidden rows and ran no lm_head: read back each scored row's hidden
|
||||
* state and the table rows its candidates name, then zero the logits rows the sampler is
|
||||
* about to read, so the token it draws (and the caller discards) is drawn from defined
|
||||
* values. One rank (BatchBuilder). */
|
||||
const int64_t rb = score_embd_ * (int64_t)sizeof(uint16_t);
|
||||
const char* hid = static_cast<const char*>(r.ctx.buf_ptr(r.program.score_hidden));
|
||||
const char* tab = static_cast<const char*>(r.ctx.weight_ptr(r.program.score_table));
|
||||
if (!hid || !tab) return RAD_E_STATE;
|
||||
char* host = reinterpret_cast<char*>(r.score_host);
|
||||
const int64_t n = (int64_t)score_rows_.size();
|
||||
for (int64_t k = 0; k < n; ++k)
|
||||
RAD_TRY(rad_memcpy_async(host + k * rb, hid + (int64_t)score_rows_[(size_t)k].first * rb,
|
||||
rb, r.ctx.stream()));
|
||||
for (size_t j = 0; j < score_ids_.size(); ++j)
|
||||
RAD_TRY(rad_memcpy_async(host + (n + (int64_t)j) * rb, tab + (int64_t)score_ids_[j] * rb,
|
||||
rb, r.ctx.stream()));
|
||||
return rad_memset_async(logits, 0, n * width * (int64_t)sizeof(float), r.ctx.stream());
|
||||
}
|
||||
for (size_t k = 0; k < score_rows_.size(); ++k)
|
||||
RAD_TRY(rad_memcpy_async(r.score_host + (int64_t)k * width,
|
||||
logits + (int64_t)score_rows_[k].first * width * 4,
|
||||
@@ -1234,6 +1274,18 @@ int Engine::issue_scored_rows(Rank& r) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* A scored step's logits: the dot product of a row's hidden state with each candidate's table row,
|
||||
* bf16 operands widened exactly and summed in f64, then rounded once to f32. */
|
||||
static float score_dot(const uint16_t* h, const uint16_t* w, int64_t n) {
|
||||
double acc = 0.0;
|
||||
for (int64_t i = 0; i < n; ++i) {
|
||||
const float a = __builtin_bit_cast(float, (uint32_t)h[i] << 16);
|
||||
const float b = __builtin_bit_cast(float, (uint32_t)w[i] << 16);
|
||||
acc += (double)a * (double)b;
|
||||
}
|
||||
return (float)acc;
|
||||
}
|
||||
|
||||
int Engine::collect_scored_rows() {
|
||||
if (score_rows_.empty()) return RAD_OK;
|
||||
/* collect_step's wait fences rank 0's stream; every other rank's copies are fenced here. */
|
||||
@@ -1242,6 +1294,23 @@ int Engine::collect_scored_rows() {
|
||||
RAD_TRY(rad_stream_sync(ranks_[i]->ctx.stream()));
|
||||
}
|
||||
if (ranks_.size() > 1) RAD_TRY(rad_dev_set(ranks_[0]->device));
|
||||
if (score_only_) {
|
||||
const uint16_t* host = reinterpret_cast<const uint16_t*>(ranks_[0]->score_host);
|
||||
const int64_t n = (int64_t)score_rows_.size();
|
||||
for (int64_t k = 0; k < n; ++k) {
|
||||
TokenScores& ts = *score_rows_[(size_t)k].second;
|
||||
ts.logits.assign(ts.tokens.size(), 0.0f);
|
||||
for (size_t j = 0; j < ts.tokens.size(); ++j) {
|
||||
const auto it = std::lower_bound(score_ids_.begin(), score_ids_.end(), ts.tokens[j]);
|
||||
const int64_t c = it - score_ids_.begin();
|
||||
ts.logits[j] = score_dot(host + k * score_embd_, host + (n + c) * score_embd_,
|
||||
score_embd_);
|
||||
}
|
||||
}
|
||||
score_rows_.clear();
|
||||
score_only_ = false;
|
||||
return RAD_OK;
|
||||
}
|
||||
for (size_t k = 0; k < score_rows_.size(); ++k) {
|
||||
TokenScores& ts = *score_rows_[k].second;
|
||||
ts.logits.assign(ts.tokens.size(), 0.0f);
|
||||
|
||||
Reference in new issue
Block a user