Archived
unlimitedocr: tiled/base e2e parity, per-segment tower layout, n-gram whitelist
The pass packer now keeps a view and its one-cell separator in one pass, and RadBatch carries each segment's grid host-side (enc_seg_h/enc_seg_w) so the DeepEncoder no longer reads device coordinates to pick a geometry. The tower lays out a line of n tiles as kMap rows of n*kMap features plus one newline cell, accepts an earlier chunk of a longer line, and emits the learned view_seperator for an explicit 4x4 separator rather than any square segment. The device sam_attn resamples a rel_pos table that is not 2*tile-1 rows on read (the 127-row global tables serve a 40-grid block as 79), matching libref/libavx. The n-gram ban gains the reference's optional whitelist (5th operand, OAI keys, generation_config alias) with range/dtype checks. Gates: tiled e2e stage cosines 0.9994-1.0, det/text 0.9901 with every coordinate within 1 px; two-page 0.9917 within 2 px; device selftest sam_attn (incl. 127->79) and ngram whitelist (1344 columns) 0 mismatches.
This commit is contained in:
1 parent
fad4ba7f1a
commit
52637ec186
37 files changed
+401566
-778
No files matched your search
+14
-1
@@ -119,6 +119,8 @@ int Engine::run_encodes() {
|
||||
|
||||
size_t i = 0;
|
||||
int32_t seg = 0;
|
||||
int32_t seg_grid = 0; /* the pass's view grid; a pass is one geometry (a tower refuses
|
||||
a mixed one: its position tables and grids are per geometry) */
|
||||
while (i < mine.size()) {
|
||||
std::vector<BatchBuilder::EncoderPart> parts;
|
||||
int64_t np = 0;
|
||||
@@ -126,16 +128,27 @@ int Engine::run_encodes() {
|
||||
mm::Item& it = *mine[i];
|
||||
const int32_t first = seg;
|
||||
for (; seg < it.n_seg; ++seg) {
|
||||
const int32_t g = it.seg_h(seg);
|
||||
/* The one-output-row view separator (core/mm marks it explicitly) belongs to
|
||||
* the pass of the view before it, so it neither breaks at a "grid change" nor
|
||||
* sets the pass's grid. A square segment that is a real view (a chunked line
|
||||
* of one tile) keeps the ordinary geometry rule below. */
|
||||
const bool sep = it.seg_is_sep(seg);
|
||||
const int64_t seg_p = it.patch_at(seg + 1) - it.patch_at(seg);
|
||||
if (seg_p > max_p) {
|
||||
RAD_ERR("encoder: a segment of %lld patches does not fit a pass of %lld",
|
||||
(long long)seg_p, (long long)max_p);
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
if (!sep && np > 0 && seg_grid != 0 && g != seg_grid) break;
|
||||
if (np + seg_p > max_p) break;
|
||||
np += seg_p;
|
||||
if (!sep) seg_grid = g;
|
||||
}
|
||||
if (seg == first) {
|
||||
if (seg_grid == 0) seg_grid = it.seg_h(seg); /* a lone oversized segment */
|
||||
break;
|
||||
}
|
||||
if (seg == first) break;
|
||||
parts.push_back({ &it, first, seg - first });
|
||||
if (seg == it.n_seg) { ++i; seg = 0; }
|
||||
}
|
||||
|
||||
+38
-1
@@ -38,6 +38,7 @@ SamplingParams GenSampling::onto(SamplingParams base) const {
|
||||
if (top_k >= 0) base.top_k = top_k;
|
||||
if (ngram_ban_n >= 0) base.ngram_ban_n = ngram_ban_n;
|
||||
if (ngram_window >= 0) base.ngram_window = ngram_window;
|
||||
if (!ngram_whitelist.empty()) base.ngram_whitelist = ngram_whitelist;
|
||||
return base;
|
||||
}
|
||||
|
||||
@@ -57,6 +58,8 @@ std::string GenSampling::describe(const SamplingParams& sp) {
|
||||
std::snprintf(ban, sizeof ban, ", ngram ban n %d over the whole sequence",
|
||||
sp.ngram_ban_n);
|
||||
s += ban;
|
||||
if (!sp.ngram_whitelist.empty())
|
||||
s += ", whitelist " + std::to_string(sp.ngram_whitelist.size()) + " ids";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
@@ -124,6 +127,28 @@ bool gen_sampling_parse(const std::string& json_text, const char* src, GenSampli
|
||||
const int64_t v = it->get<int64_t>();
|
||||
take_i("no_repeat_ngram_window_size", src, v < 0 ? 0 : v, 0, 131072, &out->ngram_window);
|
||||
}
|
||||
|
||||
/* The ban's whitelist, a JSON array of token ids (the reference's whitelist_token_ids).
|
||||
* Values are warned about and dropped, like every other field here: an id is a vocabulary
|
||||
* coordinate, so a negative one is not a value at all. */
|
||||
it = j.find("no_repeat_ngram_whitelist");
|
||||
if (it == j.end() || !it->is_array()) {
|
||||
if (it != j.end())
|
||||
RAD_WARN("%s states a no_repeat_ngram_whitelist that is not an array; ignoring it",
|
||||
src);
|
||||
it = j.find("ngram_whitelist");
|
||||
}
|
||||
if (it != j.end() && it->is_array()) {
|
||||
for (const auto& v : *it) {
|
||||
const int64_t id = v.is_number_integer() ? v.get<int64_t>() : -1;
|
||||
if (id < 0 || id > INT32_MAX) {
|
||||
RAD_WARN("%s states a no_repeat_ngram_whitelist entry outside [0, %d]; ignoring it",
|
||||
src, INT32_MAX);
|
||||
continue;
|
||||
}
|
||||
out->ngram_whitelist.push_back((int)id);
|
||||
}
|
||||
}
|
||||
return out->any();
|
||||
}
|
||||
|
||||
@@ -142,7 +167,8 @@ bool gen_sampling_from_kv(const std::function<const char*(const char*)>& get, Ge
|
||||
* file share one set of range checks and one reading of `do_sample`. */
|
||||
nlohmann::json j = nlohmann::json::object();
|
||||
static const char* const kFields[] = { "do_sample", "temperature", "top_p", "min_p", "top_k",
|
||||
"no_repeat_ngram_size", "no_repeat_ngram_window_size" };
|
||||
"no_repeat_ngram_size", "no_repeat_ngram_window_size",
|
||||
"no_repeat_ngram_whitelist" };
|
||||
for (const char* k : kFields) {
|
||||
const char* v = get(k);
|
||||
if (!v || !*v) continue;
|
||||
@@ -151,6 +177,17 @@ bool gen_sampling_from_kv(const std::function<const char*(const char*)>& get, Ge
|
||||
j[k] = (std::strcmp(v, "0") != 0);
|
||||
continue;
|
||||
}
|
||||
if (std::strcmp(k, "no_repeat_ngram_whitelist") == 0) {
|
||||
/* Not a number: the container's metadata is string-valued, and the whitelist is a
|
||||
* JSON array spelled as JSON text. */
|
||||
try {
|
||||
j[k] = nlohmann::json::parse(v);
|
||||
} catch (const std::exception& e) {
|
||||
RAD_WARN("the container states generation.%s as \"%s\", which is not a JSON "
|
||||
"array of ids (%s); ignoring it", k, v, e.what());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
char* end = nullptr;
|
||||
const double d = std::strtod(v, &end);
|
||||
if (end == v) {
|
||||
|
||||
+6
-1
@@ -28,10 +28,15 @@ struct GenSampling {
|
||||
* no_repeat_ngram_window_size; Unlimited-OCR ships 35 over the last 128). `ngram_window` is
|
||||
* normalized to 0, this sampler's "the whole sequence", when the config states it negative. */
|
||||
int ngram_ban_n = -1, ngram_window = -1;
|
||||
/* The ban's whitelist: token ids the ban must never ban (the reference processor's
|
||||
* `whitelist_token_ids`, removed from the banned set before the -inf writes). Transformers
|
||||
* has no field for it; a container states it as `generation.no_repeat_ngram_whitelist`, a
|
||||
* JSON array of ids. Empty means not stated. */
|
||||
std::vector<int32_t> ngram_whitelist;
|
||||
|
||||
bool any() const {
|
||||
return temp >= 0.0f || top_p >= 0.0f || min_p >= 0.0f || top_k >= 0 ||
|
||||
ngram_ban_n >= 0 || ngram_window >= 0;
|
||||
ngram_ban_n >= 0 || ngram_window >= 0 || !ngram_whitelist.empty();
|
||||
}
|
||||
|
||||
/* Overlay onto `base` field by field; unstated fields keep base's, so sources stack. */
|
||||
|
||||
@@ -970,6 +970,9 @@ int uocr_mode(const VisionConfig& c, const ImageMode& mode, bool* crop, int32_t*
|
||||
"patches (" + std::to_string(F) + ")");
|
||||
if (bs < 4 * F)
|
||||
return no("base_size " + std::to_string(bs) + " is under two output rows");
|
||||
if (bs / c.patch != 64 && bs / c.patch != 40)
|
||||
return no("the DeepEncoder serves base grids 64 and 40 (base_size 1024 or 640); "
|
||||
"base_size " + std::to_string(bs) + " is neither");
|
||||
if (mode.image_size > 0 && mode.image_size != c.tile)
|
||||
return no("crop mode cuts pictures into " + std::to_string(c.tile) + "-pixel tiles; "
|
||||
"image_size " + std::to_string(mode.image_size) + " is not one");
|
||||
@@ -980,6 +983,9 @@ int uocr_mode(const VisionConfig& c, const ImageMode& mode, bool* crop, int32_t*
|
||||
return no("image_size " + std::to_string(is) + " is not a whole number of merged patches "
|
||||
"(" + std::to_string(F) + ")");
|
||||
if (is < 4 * F) return no("image_size " + std::to_string(is) + " is under two output rows");
|
||||
if (is / c.patch != 64 && is / c.patch != 40)
|
||||
return no("the DeepEncoder serves base grids 64 and 40 (image_size 1024 or 640); "
|
||||
"image_size " + std::to_string(is) + " is neither");
|
||||
if (mode.base_size > 0 && mode.base_size != bs)
|
||||
return no("the base mode pads a page to image_size, not base_size");
|
||||
*crop = false; *base_size = bs; *image_size = is;
|
||||
@@ -1101,7 +1107,7 @@ int Processor::image_unlimitedocr(const Frame& f, const ImageMode& mode,
|
||||
* patches a row carry pixels; the rest are the newline column. */
|
||||
auto append = [&](const Frame& src, int32_t x0, int32_t y0, int32_t gh, int32_t gw,
|
||||
int32_t real_w) {
|
||||
it->segments.push_back({ gh, gw, it->n_patch });
|
||||
it->segments.push_back({ gh, gw, it->n_patch, real_w == 0 });
|
||||
it->seg_at.push_back((int32_t)seg_row);
|
||||
it->expand.insert(it->expand.end(), (size_t)(gh / M) * (size_t)(gw / M), c.tok_image);
|
||||
seg_row += (int64_t)(gh / M) * (gw / M);
|
||||
|
||||
+4
-1
@@ -229,7 +229,7 @@ struct Item {
|
||||
* thumbnail): each is `h` x `w` patches starting at patch `patch0`. Empty means every segment
|
||||
* is grid_h x grid_w. The rest of the engine reads segments only through the accessors below,
|
||||
* so both forms look alike. */
|
||||
struct Segment { int32_t h = 0, w = 0; int64_t patch0 = 0; };
|
||||
struct Segment { int32_t h = 0, w = 0; int64_t patch0 = 0; bool sep = false; };
|
||||
std::vector<Segment> segments;
|
||||
/* The model rotates this item's tokens at their token positions (LFM2-VL's 1-D rotary), not
|
||||
* on the grid the Qwen-VL family's M-RoPE lays out (RopeLayout). */
|
||||
@@ -240,6 +240,9 @@ struct Item {
|
||||
|
||||
int32_t seg_h(int32_t s) const { return segments.empty() ? grid_h : segments[(size_t)s].h; }
|
||||
int32_t seg_w(int32_t s) const { return segments.empty() ? grid_w : segments[(size_t)s].w; }
|
||||
/* The one-output-row view separator (Unlimited-OCR stages it as a merge-grid cell of zeros).
|
||||
* A square segment is not enough: a chunked line of one tile is q x q too. */
|
||||
bool seg_is_sep(int32_t s) const { return !segments.empty() && segments[(size_t)s].sep; }
|
||||
/* Segment s's first patch and first output row; s == n_seg is the end of the item. */
|
||||
int64_t patch_at(int32_t s) const {
|
||||
if (segments.empty()) return (int64_t)s * grid_h * grid_w;
|
||||
|
||||
@@ -596,6 +596,12 @@ struct SamplingParams {
|
||||
* asks for 35 and 128 (the model card's document mode uses 1024). */
|
||||
int ngram_ban_n = 0;
|
||||
int ngram_window = 0;
|
||||
/* The ids exempt from that ban: the reference processor's whitelist
|
||||
* (SlidingWindowNoRepeatNgramProcessor's `whitelist_token_ids`, whose set is removed from
|
||||
* the banned set after it is built -- modeling_unlimitedocr.py:359,379). Global token ids,
|
||||
* duplicates ignored. Empty (the default) is off: the ban is then bit-identical to the
|
||||
* no-whitelist form. */
|
||||
std::vector<int32_t> ngram_whitelist;
|
||||
};
|
||||
|
||||
/* Raw logits at chosen vocabulary ids at the prompt's last position, for a caller that answers
|
||||
|
||||
@@ -908,7 +908,7 @@ int Ctx::lane_join(int from, int to) {
|
||||
* field added to RadBatch changes its size: decide whether the key must read it, then update the
|
||||
* size here. `agen_b` and `vgen_b` are set only on generator passes, which are never recorded
|
||||
* (below). */
|
||||
static_assert(sizeof(void*) != 8 || sizeof(RadBatch) == 336,
|
||||
static_assert(sizeof(void*) != 8 || sizeof(RadBatch) == 352,
|
||||
"RadBatch changed: check that Ctx::pass_key reads every new field a pass depends on");
|
||||
static_assert(sizeof(void*) != 8 || sizeof(RadKVGroupBatch) == 72,
|
||||
"RadKVGroupBatch changed: check that Ctx::pass_key reads every new field");
|
||||
|
||||
@@ -528,7 +528,7 @@ int32_t host_argmax(const float* logits, int64_t n_vocab) {
|
||||
}
|
||||
|
||||
void host_ngram_ban(CandidateSet& s, const int32_t* history, int64_t n_history, int32_t n,
|
||||
int32_t window) {
|
||||
int32_t window, const uint32_t* whitelist, int64_t wl_words) {
|
||||
if (n <= 0 || !history || n_history < (int64_t)n) return;
|
||||
const int64_t len = n_history;
|
||||
const int64_t win = (window > 0 && window < len) ? window : len;
|
||||
@@ -547,6 +547,21 @@ void host_ngram_ban(CandidateSet& s, const int32_t* history, int64_t n_history,
|
||||
}
|
||||
if (match) banned.insert(history[idx + plen]);
|
||||
}
|
||||
|
||||
/* The reference's `banned.difference_update(self.whitelist)`, before any -inf: a token whose
|
||||
* whitelist bit is set is never banned. A bit past the plane is not set, so a token outside
|
||||
* it stays banned, as an id the reference's set does not contain. */
|
||||
if (whitelist && wl_words > 0) {
|
||||
for (auto it = banned.begin(); it != banned.end();) {
|
||||
const int64_t id = *it;
|
||||
if (id >= 0 && (id >> 5) < wl_words &&
|
||||
(whitelist[(size_t)(id >> 5)] >> ((uint32_t)id & 31u)) & 1u)
|
||||
it = banned.erase(it);
|
||||
else
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
if (banned.empty()) return;
|
||||
s.sorted = false; /* a -infinity candidate belongs after every finite one */
|
||||
for (Candidate& c : s.c)
|
||||
@@ -575,7 +590,8 @@ int32_t host_sample_into(CandidateSet& s, const SamplingParams& sp, const HostRe
|
||||
|
||||
/* The n-gram ban, after the penalties and before the greedy split: the device chain issues it
|
||||
* there, so a greedy request sees the same masked distribution. Off at ngram_ban_n 0. */
|
||||
host_ngram_ban(s, in.history, in.n_history, sp.ngram_ban_n, sp.ngram_window);
|
||||
host_ngram_ban(s, in.history, in.n_history, sp.ngram_ban_n, sp.ngram_window,
|
||||
in.ngram_whitelist, in.ngram_whitelist_words);
|
||||
|
||||
/* Greedy skips the chain: penalties -> mask -> argmax. Temperature 0 collapses every other
|
||||
* logit to -inf, so a mask applied after it could leave nothing. */
|
||||
|
||||
+11
-2
@@ -181,9 +181,14 @@ void host_xtc(CandidateSet& s, float probability, float threshold, size_t min_ke
|
||||
* SlidingWindowNoRepeatNgramProcessor): a candidate whose token would complete an `n`-gram the
|
||||
* last `window` tokens of `history` already contain gets -infinity. `n` <= 0 is off, `window` <= 0
|
||||
* is the whole history. It runs after the penalties and DRY and before every cut, so the greedy
|
||||
* path sees it too. */
|
||||
* path sees it too.
|
||||
*
|
||||
* `whitelist` is the reference's whitelist (a SET bit means exempt from the ban, the same plane
|
||||
* form as host_mask's, word w bit b covering token 32w + b): ids removed from the banned set
|
||||
* before anything is written, as `banned.difference_update(self.whitelist)` does. Null or zero
|
||||
* words is off -- the reference's empty whitelist, the default serving form. */
|
||||
void host_ngram_ban(CandidateSet& s, const int32_t* history, int64_t n_history, int32_t n,
|
||||
int32_t window);
|
||||
int32_t window, const uint32_t* whitelist = nullptr, int64_t wl_words = 0);
|
||||
|
||||
/* The grammar mask. One bit per token, set means admissible; a cleared bit sets the logit to -inf
|
||||
* instead of removing the candidate, so nothing downstream is renumbered. `n_words` is
|
||||
@@ -224,6 +229,10 @@ struct HostRefInput {
|
||||
int64_t total_context = 0; /* DRY's clamp; 0 means use n_history */
|
||||
const uint32_t* bitmask = nullptr; /* grammar mask, or null */
|
||||
int64_t n_words = 0;
|
||||
/* The n-gram ban's whitelist bitmask (a SET bit exempts a token, host_ngram_ban), null =
|
||||
* no exemptions. */
|
||||
const uint32_t* ngram_whitelist = nullptr;
|
||||
int64_t ngram_whitelist_words = 0;
|
||||
uint64_t pos = 0; /* output position: the RNG's second coordinate */
|
||||
const DryBreakers* dry_breakers = nullptr; /* null runs DRY with no breakers */
|
||||
/* The chain's candidate width (SamplerConfig::max_candidates): top-k narrows every row to
|
||||
|
||||
+43
-7
@@ -181,6 +181,9 @@ int Sampler::declare(RadBuilder* b, const SamplerConfig& cfg) {
|
||||
/* The n-gram ban's per-row {n-gram length, window}. Its own tiny plane because the params row
|
||||
* is the fixed RadSampleParams wire format every sampler op shares. */
|
||||
buf_ngram_ = decl("sampler.ngram", RAD_I32, rows, 2, RAD_BUF_PERSIST);
|
||||
/* The ban's whitelist bitmask, this rank's shard, shaped like the mask plane: a SET bit
|
||||
* exempts that column. The ban op indexes it by the sampled row, not by mask_row. */
|
||||
buf_ngram_wl_ = decl("sampler.ngram_wl", RAD_U32, rows, mask_words_, RAD_BUF_PERSIST);
|
||||
buf_mask_ = decl("sampler.mask", RAD_U32, rows, mask_words_, RAD_BUF_PERSIST);
|
||||
buf_cand_i_ = decl("sampler.cand_idx", RAD_I32, rows, ncand, RAD_BUF_TRANSIENT);
|
||||
buf_cand_v_ = decl("sampler.cand_val", RAD_F32, rows, ncand, RAD_BUF_TRANSIENT);
|
||||
@@ -200,7 +203,8 @@ int Sampler::declare(RadBuilder* b, const SamplerConfig& cfg) {
|
||||
buf_lp_g_ = decl("sampler.logprobs_gathered", RAD_F32, rows,
|
||||
lp_width() * cfg_.world_size, RAD_BUF_PERSIST);
|
||||
|
||||
if (!buf_params_ || !buf_params_late_ || !buf_hist_ || !buf_brk_ || !buf_ngram_ || !buf_mask_ ||
|
||||
if (!buf_params_ || !buf_params_late_ || !buf_hist_ || !buf_brk_ || !buf_ngram_ ||
|
||||
!buf_ngram_wl_ || !buf_mask_ ||
|
||||
!buf_cand_i_ || !buf_cand_v_ || !buf_lp_ ||
|
||||
(vocab_parallel() && (!buf_pairs_ || !buf_gather_ || !buf_lp_g_))) {
|
||||
RAD_ERR("sampler: a staging buffer failed to declare");
|
||||
@@ -325,6 +329,7 @@ int Sampler::stage_alloc() {
|
||||
hist_stage_ = (int32_t*)rad_dev_alloc(rows * cfg_.max_hist * 4, RAD_MEM_HOST_PINNED);
|
||||
brk_stage_ = (uint8_t*)rad_dev_alloc(rows * cfg_.max_hist, RAD_MEM_HOST_PINNED);
|
||||
ngram_stage_ = (int32_t*)rad_dev_alloc(rows * 2 * 4, RAD_MEM_HOST_PINNED);
|
||||
ngram_wl_stage_ = (uint32_t*)rad_dev_alloc(rows * mask_words_ * 4, RAD_MEM_HOST_PINNED);
|
||||
mask_stage_ = (uint32_t*)rad_dev_alloc(rows * mask_words_ * 4, RAD_MEM_HOST_PINNED);
|
||||
|
||||
/* Logprobs readback; only rank 0 reads it. */
|
||||
@@ -342,10 +347,10 @@ int Sampler::stage_alloc() {
|
||||
}
|
||||
|
||||
if (!params_stage_ || !greedy_stage_ || !late_stage_ || !hist_stage_ || !brk_stage_ ||
|
||||
!ngram_stage_ || !mask_stage_) {
|
||||
!ngram_stage_ || !ngram_wl_stage_ || !mask_stage_) {
|
||||
RAD_ERR("sampler: could not pin %s of staging",
|
||||
humanb(rows * (3 * (int64_t)sizeof(DeviceSampleParams) + cfg_.max_hist * 5 +
|
||||
mask_words_ * 4 + 8)).c_str());
|
||||
mask_words_ * 8 + 8)).c_str());
|
||||
stage_free();
|
||||
return RAD_E_NOMEM;
|
||||
}
|
||||
@@ -358,7 +363,8 @@ void Sampler::stage_free() {
|
||||
if (late_stage_) rad_dev_free(late_stage_, RAD_MEM_HOST_PINNED);
|
||||
if (hist_stage_) rad_dev_free(hist_stage_, RAD_MEM_HOST_PINNED);
|
||||
if (brk_stage_) rad_dev_free(brk_stage_, RAD_MEM_HOST_PINNED);
|
||||
if (ngram_stage_) rad_dev_free(ngram_stage_, RAD_MEM_HOST_PINNED);
|
||||
if (ngram_stage_) rad_dev_free(ngram_stage_, RAD_MEM_HOST_PINNED);
|
||||
if (ngram_wl_stage_) rad_dev_free(ngram_wl_stage_, RAD_MEM_HOST_PINNED);
|
||||
if (mask_stage_) rad_dev_free(mask_stage_, RAD_MEM_HOST_PINNED);
|
||||
if (lp_host_) rad_dev_free(lp_host_, RAD_MEM_HOST_PINNED);
|
||||
if (lp_ci_host_) rad_dev_free(lp_ci_host_, RAD_MEM_HOST_PINNED);
|
||||
@@ -372,7 +378,8 @@ void Sampler::stage_free() {
|
||||
cur_stage_ = nullptr;
|
||||
hist_stage_ = nullptr;
|
||||
brk_stage_ = nullptr;
|
||||
ngram_stage_ = nullptr;
|
||||
ngram_stage_ = nullptr;
|
||||
ngram_wl_stage_ = nullptr;
|
||||
mask_stage_ = nullptr;
|
||||
}
|
||||
|
||||
@@ -604,6 +611,7 @@ int Sampler::build_step(const std::vector<Request*>& reqs) {
|
||||
any_brk_ = false;
|
||||
any_late_ = false;
|
||||
any_ngram_ = false;
|
||||
any_ngram_wl_ = false;
|
||||
cur_stage_ = params_stage_;
|
||||
lp_rows_.clear();
|
||||
|
||||
@@ -724,6 +732,21 @@ int Sampler::build_step(const std::vector<Request*>& reqs) {
|
||||
ngram_stage_[2 * row] = (int32_t)sp.ngram_ban_n;
|
||||
ngram_stage_[2 * row + 1] = (int32_t)sp.ngram_window;
|
||||
|
||||
/* The ban's whitelist, cut to this rank's shard like the grammar mask (shard_mask):
|
||||
* global ids outside it exempt nothing here. Every row that runs the ban gets its
|
||||
* words freshly written -- the plane is uploaded whole with the ban, so a row must
|
||||
* never read another request's stale exemptions. */
|
||||
if (ngram) {
|
||||
uint32_t* w = ngram_wl_stage_ + row * mask_words_;
|
||||
std::memset(w, 0, (size_t)mask_words_ * 4);
|
||||
for (int32_t id : sp.ngram_whitelist) {
|
||||
const int64_t at = (int64_t)id - cfg_.vocab_off;
|
||||
if (at < 0 || at >= cfg_.n_vocab_local) continue;
|
||||
w[at >> 5] |= 1u << (uint32_t)(at & 31);
|
||||
any_ngram_wl_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* Greedy over a split vocabulary is a top-1 chain row, not an argmax: sample_argmax
|
||||
* writes a column of this rank's shard, not a token id. Top-k plus the merge produce
|
||||
* global ids, and the pick over one candidate is deterministic. A greedy row asking
|
||||
@@ -916,11 +939,14 @@ int Sampler::build_greedy(int64_t n) {
|
||||
any_brk_ = false;
|
||||
any_late_ = false;
|
||||
any_ngram_ = false;
|
||||
any_ngram_wl_ = false;
|
||||
cur_stage_ = greedy_stage_;
|
||||
/* A drafter's rows run the argmax only, so the n-gram plane is cleared: `run` reads it from
|
||||
* the staging, and a stale pair from an earlier request's build must not issue the ban over
|
||||
* history that was not staged this step. */
|
||||
* history that was not staged this step. The exemption plane goes with it: cleared for the
|
||||
* rows `run` would upload, so no build that does issue the ban reads a stale exemption. */
|
||||
std::memset(ngram_stage_, 0, (size_t)cap * 2 * sizeof(int32_t));
|
||||
std::memset(ngram_wl_stage_, 0, (size_t)n * mask_words_ * 4);
|
||||
if (greedy_ready_) return RAD_OK;
|
||||
for (int64_t i = 0; i < cap; ++i) {
|
||||
DeviceSampleParams& p = greedy_stage_[i];
|
||||
@@ -986,9 +1012,14 @@ int Sampler::run(RadCtx* c, rad_buf logits, rad_buf out_tokens) {
|
||||
if (pass_brk)
|
||||
RAD_TRY(rad_memcpy_async(rad_buf_ptr(c, buf_brk_), brk_stage_,
|
||||
n_seq_ * cfg_.max_hist, s));
|
||||
/* The ban's whitelist rides with the ban, and only a row that runs the ban reads it. */
|
||||
const bool pass_wl = any_ngram && any_ngram_wl_;
|
||||
if (any_ngram)
|
||||
RAD_TRY(rad_memcpy_async(rad_buf_ptr(c, buf_ngram_), ngram_stage_,
|
||||
n_seq_ * 2 * (int64_t)sizeof(int32_t), s));
|
||||
if (pass_wl)
|
||||
RAD_TRY(rad_memcpy_async(rad_buf_ptr(c, buf_ngram_wl_), ngram_wl_stage_,
|
||||
n_seq_ * mask_words_ * 4, s));
|
||||
if (any_mask)
|
||||
RAD_TRY(rad_memcpy_async(rad_buf_ptr(c, buf_mask_), mask_stage_,
|
||||
n_seq_ * mask_words_ * 4, s));
|
||||
@@ -998,6 +1029,7 @@ int Sampler::run(RadCtx* c, rad_buf logits, rad_buf out_tokens) {
|
||||
const RadOperand o_hist = opd_rows(buf_hist_, n_seq_);
|
||||
const RadOperand o_brk = opd_rows(buf_brk_, n_seq_);
|
||||
const RadOperand o_ngram = opd_rows(buf_ngram_, n_seq_);
|
||||
const RadOperand o_ngram_wl = opd_rows(buf_ngram_wl_, n_seq_);
|
||||
const RadOperand o_mask = opd_rows(buf_mask_, n_seq_);
|
||||
const RadOperand o_ci = opd_rows(buf_cand_i_, n_seq_);
|
||||
const RadOperand o_cv = opd_rows(buf_cand_v_, n_seq_);
|
||||
@@ -1030,7 +1062,11 @@ int Sampler::run(RadCtx* c, rad_buf logits, rad_buf out_tokens) {
|
||||
* below as well as for the chain, which is the reference processor's effect on the
|
||||
* distribution. Off (the default) the op is not issued at all, so a request without it
|
||||
* samples exactly as before. */
|
||||
if (any_ngram) RAD_TRY(issue(op_ngram_ban_, { o_logits, o_params, o_hist, o_ngram }));
|
||||
/* The whitelist operand is passed only when some staged row carries exemptions; absent
|
||||
* means nothing is exempt, the reference's empty whitelist and the bit-identical default. */
|
||||
if (any_ngram)
|
||||
RAD_TRY(issue(op_ngram_ban_, { o_logits, o_params, o_hist, o_ngram,
|
||||
pass_wl ? o_ngram_wl : opd_none() }));
|
||||
|
||||
/* Greedy rows take the argmax after penalties and the mask. In a mixed batch both the argmax
|
||||
* and the chain run over the whole batch: the argmax writes every row, then the candidate
|
||||
|
||||
+12
-2
@@ -48,7 +48,8 @@
|
||||
* sample_penalties M(range) n_vocab vocab_off? logits, params, history -> logits (inout)
|
||||
* sample_dry M(range) n_vocab vocab_off? logits, params, history, breakers?
|
||||
* -> logits (inout)
|
||||
* sample_ngram_ban M(range) n_vocab vocab_off? logits, params, history, ngram
|
||||
* sample_ngram_ban M(range) n_vocab vocab_off? logits, params, history, ngram,
|
||||
* whitelist?
|
||||
* -> logits (inout)
|
||||
* sample_temp M(range) n_vocab logits, params -> logits (inout)
|
||||
* sample_mask M(range) n_vocab logits, params, bitmask -> logits (inout)
|
||||
@@ -67,7 +68,9 @@
|
||||
* a verify. `params` is [M] DeviceSampleParams rows. `history` is [M, max_hist] int32 global
|
||||
* token ids, row m addressed by its hist_off/hist_len. `breakers` is [M, max_hist] uint8 beside
|
||||
* it, nonzero where that token is a whole DRY sequence breaker. `bitmask` is [M, mask_words]
|
||||
* uint32, addressed by the row's mask_row. `vocab_off` is this rank's first vocabulary row,
|
||||
* uint32, addressed by the row's mask_row. The ban's `whitelist` is a [M, mask_words] uint32
|
||||
* bitmask (a SET bit exempts that column from the ban) addressed by the row itself, absent when
|
||||
* no staged row carries one. `vocab_off` is this rank's first vocabulary row,
|
||||
* declared only when the vocabulary is split; the three ops that map between token ids and this
|
||||
* rank's logit columns use it. Every op has a libref implementation, which is both its fallback
|
||||
* and its oracle (spec §17).
|
||||
@@ -259,6 +262,9 @@ private:
|
||||
rad_buf buf_hist_ = RAD_NULL_HANDLE;
|
||||
rad_buf buf_brk_ = RAD_NULL_HANDLE; /* DRY's single-token breaker marks, beside history */
|
||||
rad_buf buf_ngram_ = RAD_NULL_HANDLE; /* the n-gram ban's {n, window} pair a row */
|
||||
/* The ban's whitelist bitmask, [rows, mask_words] like the grammar mask plane: a SET bit
|
||||
* exempts that column from the ban. Uploaded with the ban, only when a staged row has one. */
|
||||
rad_buf buf_ngram_wl_ = RAD_NULL_HANDLE;
|
||||
rad_buf buf_mask_ = RAD_NULL_HANDLE;
|
||||
rad_buf buf_cand_i_ = RAD_NULL_HANDLE;
|
||||
rad_buf buf_cand_v_ = RAD_NULL_HANDLE;
|
||||
@@ -283,6 +289,7 @@ private:
|
||||
int32_t* hist_stage_ = nullptr;
|
||||
uint8_t* brk_stage_ = nullptr;
|
||||
int32_t* ngram_stage_ = nullptr; /* [rows][2]: n-gram length, window */
|
||||
uint32_t* ngram_wl_stage_ = nullptr; /* [rows][mask_words] exemption bitmask */
|
||||
uint32_t* mask_stage_ = nullptr;
|
||||
|
||||
int64_t mask_words_ = 0; /* words for this rank's shard; what the mask op indexes */
|
||||
@@ -293,6 +300,9 @@ private:
|
||||
/* Whether any row this step runs the n-gram ban. Its history is the plane the penalties use,
|
||||
* so this only decides whether that plane is uploaded and the op issued. */
|
||||
bool any_ngram_ = false;
|
||||
/* Whether any banned row this step whitelists a column of this rank's shard; only then is
|
||||
* the exemption plane uploaded and passed to sample_ngram_ban. */
|
||||
bool any_ngram_wl_ = false;
|
||||
|
||||
/* The whole-vocabulary mask the grammar fills, from which this rank's shard is cut. Used
|
||||
* only when the head is sharded; see declare(). */
|
||||
|
||||
@@ -1164,6 +1164,8 @@ const RadBatch* BatchBuilder::build(const StepPlan& plan, const IKVManager& kv,
|
||||
B.enc_n_seg = 0;
|
||||
B.enc_cu = nullptr;
|
||||
B.enc_max_seg = 0;
|
||||
B.enc_seg_h = nullptr;
|
||||
B.enc_seg_w = nullptr;
|
||||
B.n_checkpoints = n_ck;
|
||||
B.checkpoint_tok = n_ck ? on_rank(base, b0, s.d_cktok) : nullptr;
|
||||
B.checkpoint_slot = n_ck ? on_rank(base, b0, s.d_ckslot) : nullptr;
|
||||
@@ -1232,6 +1234,8 @@ const RadBatch* BatchBuilder::build_encoder(const std::vector<EncoderPart>& part
|
||||
const int64_t max_segs = enc.max_patches / (enc.merge > 0 ? enc.merge : 1) + 1;
|
||||
int64_t n_patch = 0, n_seg = 0, max_seg = 0;
|
||||
enc_cu_host_[0] = 0;
|
||||
enc_seg_h_host_.clear();
|
||||
enc_seg_w_host_.clear();
|
||||
for (const EncoderPart& p : parts) {
|
||||
const mm::Item& it = *p.item;
|
||||
if (p.seg0 < 0 || p.n_seg <= 0 || p.seg0 + p.n_seg > it.n_seg) {
|
||||
@@ -1254,6 +1258,8 @@ const RadBatch* BatchBuilder::build_encoder(const std::vector<EncoderPart>& part
|
||||
for (int32_t q = p.seg0; q < p.seg0 + p.n_seg; ++q) {
|
||||
const int64_t seg_p = it.patch_at(q + 1) - it.patch_at(q);
|
||||
enc_cu_host_[n_seg + 1] = enc_cu_host_[n_seg] + (int32_t)seg_p;
|
||||
enc_seg_h_host_.push_back(it.seg_h(q));
|
||||
enc_seg_w_host_.push_back(it.seg_w(q));
|
||||
++n_seg;
|
||||
if (seg_p > max_seg) max_seg = seg_p;
|
||||
}
|
||||
@@ -1300,6 +1306,8 @@ const RadBatch* BatchBuilder::build_encoder(const std::vector<EncoderPart>& part
|
||||
B.enc_n_seg = n_seg;
|
||||
B.enc_cu = static_cast<const int32_t*>(enc_cu_dev_[r]);
|
||||
B.enc_max_seg = (int32_t)max_seg;
|
||||
B.enc_seg_h = enc_seg_h_host_.data();
|
||||
B.enc_seg_w = enc_seg_w_host_.data();
|
||||
}
|
||||
out_last_ = out_next_;
|
||||
out_next_ = (out_next_ + 1) % kOut;
|
||||
|
||||
@@ -333,6 +333,9 @@ private:
|
||||
int32_t* enc_coord_host_ = nullptr;
|
||||
int32_t* enc_cu_host_ = nullptr;
|
||||
std::vector<int32_t> enc_coord_tmp_; /* one item's coordinates; kept for its capacity */
|
||||
/* The pass's per-segment grid, host side (RadBatch::enc_seg_h/w). Filled by build_encoder and
|
||||
* valid until the next build, like the staged coordinates. */
|
||||
std::vector<int32_t> enc_seg_h_host_, enc_seg_w_host_;
|
||||
int64_t last_n_tok_ = 0;
|
||||
struct MediaBytes { int64_t mm = 0, pix = 0, crd = 0, cu = 0; };
|
||||
MediaBytes media_bytes(); /* the four per-rank device regions alloc_media reserves */
|
||||
|
||||
+29
-1
@@ -240,7 +240,7 @@ const char* kAcceptedCommon[] = {
|
||||
"dry_sequence_breakers", "samplers",
|
||||
"grammar", "json_schema", "priority", "logprobs", "ignore_eos",
|
||||
"timings_per_token", "return_progress", "session_id", "n_probs",
|
||||
"ngram_ban_n", "ngram_window",
|
||||
"ngram_ban_n", "ngram_window", "ngram_whitelist", "no_repeat_ngram_whitelist",
|
||||
};
|
||||
|
||||
const char* kAcceptedChat[] = {
|
||||
@@ -916,6 +916,34 @@ static int parse_sampling(const json& b, const OaiLimits& lim, OaiRequest* r, Ap
|
||||
}
|
||||
r->sp.ngram_window = (int)i64;
|
||||
|
||||
/* The ban's whitelist (the reference's `whitelist_token_ids`, which is removed from the
|
||||
* banned set before the -inf writes): token ids the ban must never touch. An array of
|
||||
* non-negative integers; absent, empty or null is off, the reference's empty whitelist and
|
||||
* the form the model's generation_config ships. */
|
||||
auto nwl = b.find("ngram_whitelist");
|
||||
if (nwl == b.end()) nwl = b.find("no_repeat_ngram_whitelist");
|
||||
if (nwl != b.end() && !nwl->is_null()) {
|
||||
if (!nwl->is_array()) {
|
||||
*err = err_bad("ngram_whitelist", "expected an array of integer token ids");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
std::vector<int32_t> wl;
|
||||
for (const auto& v : *nwl) {
|
||||
int32_t id = 0;
|
||||
if (!token_id(v, &id)) {
|
||||
*err = err_bad("ngram_whitelist",
|
||||
"expected token ids in [0, " + std::to_string(INT32_MAX) + "]");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (wl.size() >= (size_t)1 << 20) {
|
||||
*err = err_bad("ngram_whitelist", "at most 1048576 ids");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
wl.push_back(id);
|
||||
}
|
||||
r->sp.ngram_whitelist = std::move(wl);
|
||||
}
|
||||
|
||||
/* `images_config`: Unlimited-OCR's two picture modes. A string names one ("gundam" for a
|
||||
* single picture -- the global view plus crops --, "base" for a page of a multi-page
|
||||
* document); an object carries the reference runner's own dict. */
|
||||
|
||||
Reference in new issue
Block a user