| 4400 | #endif |
| 4401 | |
| 4402 | bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool, void* user, int32_t* out, |
| 4403 | std::string& err) { |
| 4404 | const bool measure = spec_measure_; |
| 4405 | // Clear this borrow and propagated stage borrows on every exit. |
| 4406 | struct ProposalBorrow { |
| 4407 | Verifier* verifier; |
| 4408 | bool active; |
| 4409 | ~ProposalBorrow() { if (active) verifier->set_spec_q(nullptr, 0); } |
| 4410 | } proposal_borrow{this, measure}; |
| 4411 | if (measure && (T < 1 || T > strata::kernels::kVerifyMaxT || |
| 4412 | spec_nq_ < 0 || spec_nq_ > T || (spec_nq_ > 0 && spec_q_ == nullptr))) { |
| 4413 | err = "verify: proposal row count/pointer is invalid"; return false; |
| 4414 | } |
| 4415 | if (measure && (!head_sampling_ || sampling_.greedy || sampling_.temperature <= 0.0f)) { |
| 4416 | err = "verify: proposal requires sampled head"; return false; |
| 4417 | } |
| 4418 | #if defined(STRATA_GPU_METAL) |
| 4419 | if (measure) { err = "verify: proposal unsupported on Metal"; return false; } |
| 4420 | (void) pool; |
| 4421 | (void) user; |
| 4422 | { // a sampled, penalised or biased request: the head rows are sampled on the GPU (metal/kernels/sample.metal), Philox(seed, pos0 + t) as the CUDA head |
| 4423 | const bool sampled_m = !sampling_.greedy && sampling_.temperature > 0.0f; |
| 4424 | if (head_sampling_ && (sampled_m || hist_d_ != nullptr || sampling_.logit_bias != nullptr)) { |
| 4425 | strata::kernels::SamplerParams sp_m = sampling_; |
| 4426 | sp_m.counter = (uint64_t) pos0; |
| 4427 | strata::metal_engine::window_set_sampling(metal_, &sp_m, hist_d_, hist_len_); |
| 4428 | } else { |
| 4429 | strata::metal_engine::window_set_sampling(metal_, nullptr, nullptr, 0); |
| 4430 | } |
| 4431 | } |
| 4432 | if (!strata::metal_engine::window_run(metal_, T, tokens, pos0, head_sampling_, out, err)) return false; |
| 4433 | // #879 on Metal: the CUDA path records the fact with its device row check inside run(); this branch returned |
| 4434 | // before note_nonfinite, so a window whose logits had gone non-finite streamed the sampler's token-0 "no |
| 4435 | // candidate" answer forever. The port has no device kernel; the head rows stand in unified memory after the |
| 4436 | // window and carry the same fact (metal_scan_rows, the CUDA kernel's bit test). note_nonfinite resets and |
| 4437 | // honours the guard switch; the fold adds the scan's rows after it. |
| 4438 | last_pos0_ = pos0; // (before note_nonfinite: its position of a flagged row is last_pos0_ + row, as on CUDA) |
| 4439 | note_nonfinite(T, nullptr); |
| 4440 | const uint32_t nf_scan = nan_guard_on() && last_stage() ? metal_scan_rows(metal_, T, n_vocab_) : 0u; |
| 4441 | metal_fold_nf(nf_mask_, nf_scan, T, nullptr, pos0, nf_mask_, nf_row_, nf_pos_); |
| 4442 | progress_at("decode"); // (as the CUDA / HIP path's end of run(): a finished window is progress for the serve watchdog) |
| 4443 | progress_beat(); |
| 4444 | return true; |
| 4445 | #endif |
| 4446 | using namespace strata::kernels; |
| 4447 | const OnDevice on_device(device_); |
| 4448 | last_batch_ = false; |
| 4449 | split_logits_materialized_ = false; |
| 4450 | #ifdef STRATA_WITH_LOOP |
| 4451 | // a cyclic split: which hop of the itinerary this call runs (the caller enters at the first) |
| 4452 | const int hop = (cyclic() && !bypass_loaded_) ? (cyc_entered_ ? cyc_hop_ : 0) : -1; |
| 4453 | cyc_entered_ = false; |
| 4454 | if (hop >= 0) { |
| 4455 | if (hop >= (int) cyc_hops_->size() || (*cyc_hops_)[(size_t) hop].ver != this) { |
| 4456 | err = "verify: the cyclic split's itinerary does not start on this stage"; |
| 4457 | return false; |
| 4458 | } |
| 4459 | cyc_visit_ = (*cyc_hops_)[(size_t) hop].visit; |