| 119 | } |
| 120 | |
| 121 | std::vector<BatchDecoder::Emitted> BatchDecoder::window(const std::vector<BatchLane*>& lanes, const std::vector<int>& limit, |
| 122 | const std::function<void(int, int)>& emit) { |
| 123 | const auto t0 = std::chrono::steady_clock::now(); |
| 124 | const int n = (int) lanes.size(); |
| 125 | if (n < 1 || (int) limit.size() != n) throw std::runtime_error("batch decoder: one lane or more, a limit each"); |
| 126 | std::vector<int> ids, counts, rows, r0; |
| 127 | for (int i = 0; i < n; ++i) { |
| 128 | const BatchLane& L = *lanes[(size_t) i]; |
| 129 | if (L.ctx.empty() || limit[(size_t) i] < 1) throw std::runtime_error("batch decoder: a lane without its next token or room for one"); |
| 130 | if (L.qs.size() != L.drafts.size()) throw std::runtime_error("batch decoder: a lane's drafts without their distributions"); |
| 131 | ids.push_back(L.lane); |
| 132 | counts.push_back(L.rows()); |
| 133 | r0.push_back((int) rows.size()); |
| 134 | rows.push_back(L.ctx.back()); |
| 135 | rows.insert(rows.end(), L.drafts.begin(), L.drafts.end()); |
| 136 | } |
| 137 | const int T = (int) rows.size(); |
| 138 | const auto tv = std::chrono::steady_clock::now(); |
| 139 | const std::vector<int> o = e_.verify_rows(ids, counts, rows); |
| 140 | last_verify_ms = ms_since(tv); |
| 141 | last_rows = T; |
| 142 | if (e_.last_capture_ms > 0.0) capture_ms_ = 0.8 * capture_ms_ + 0.2 * e_.last_capture_ms; |
| 143 | bool sample = false, pen = false; |
| 144 | for (const BatchLane* L : lanes) { |
| 145 | sample = sample || L->s.temperature > 0.f; |
| 146 | pen = pen || penalizes(L->s); |
| 147 | } |
| 148 | std::vector<std::vector<std::pair<float, int>>> cand; |
| 149 | if (sample) { |
| 150 | // each row's penalties as the lane alone would apply them: its window, then its rows before this one |
| 151 | Engine::Penalties P; |
| 152 | if (pen) |
| 153 | for (int i = 0; i < n; ++i) { |
| 154 | const BatchLane& L = *lanes[(size_t) i]; |
| 155 | const bool p = penalizes(L.s); |
| 156 | std::vector<int> seq; |
| 157 | if (p) seq = tail(L.ctx, L.s.pen_last_n); |
| 158 | for (int j = 0; j < counts[(size_t) i]; ++j) { |
| 159 | if (p && j > 0) seq.push_back(rows[(size_t) (r0[(size_t) i] + j)]); |
| 160 | P.rows.push_back(p ? penalty_row(seq, L.s) : Engine::Penalties::Row{}); |
| 161 | } |
| 162 | } |
| 163 | cand = e_.top_tokens(T, P); |
| 164 | } |
| 165 | std::vector<int> keep((size_t) n), going; |
| 166 | std::vector<Emitted> out((size_t) n); |
| 167 | std::vector<std::vector<int>> got((size_t) n); |
| 168 | for (int i = 0; i < n; ++i) { |
| 169 | BatchLane& L = *lanes[(size_t) i]; |
| 170 | const int k = counts[(size_t) i], b = r0[(size_t) i]; |
| 171 | std::vector<int> oi(o.begin() + b, o.begin() + b + k); |
| 172 | if (L.shadow >= 0) { // no drafts this window: the draft layer's unverified first draft, scored as it would have stood |
| 173 | double stand; |
| 174 | if (L.s.temperature <= 0.f) { |
| 175 | stand = L.shadow == oi[0] ? 1.0 : 0.0; |
| 176 | } else { |
| 177 | const Dist pd = make_dist(cand[(size_t) b], L.s); |
| 178 | stand = L.shadow_q.ids.empty() ? pd.prob(L.shadow) : std::min(1.0, pd.prob(L.shadow) / L.shadow_q.prob(L.shadow)); |