Skip to content

Commit 283ac9c

Browse files
committed
refactor: inline sampled mtp helpers
1 parent 0209e00 commit 283ac9c

1 file changed

Lines changed: 20 additions & 44 deletions

File tree

examples/batch-processing/server.py

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,20 +1983,6 @@ class SampledDraftState:
19831983
n_predict: int
19841984
embedding: np.ndarray
19851985

1986-
def _target_row_count(
1987-
self,
1988-
updates: Sequence["MTPDraftProvider.SampledBatchUpdate"],
1989-
/,
1990-
) -> int:
1991-
return max(
1992-
(
1993-
max(update.row_indices) + 1
1994-
for update in updates
1995-
if update.row_indices
1996-
),
1997-
default=0,
1998-
)
1999-
20001986
def _build_sampled_batch_plan(
20011987
self,
20021988
updates: Sequence["MTPDraftProvider.SampledBatchUpdate"],
@@ -2140,29 +2126,6 @@ def _decode_sampled_pending_rows(
21402126

21412127
return sampled_outputs
21422128

2143-
def _mark_sampled_outputs_ready(
2144-
self,
2145-
updates: Sequence["MTPDraftProvider.SampledBatchUpdate"],
2146-
sampled_outputs: Sequence["MTPDraftProvider.SampledOutput"],
2147-
h_tgt_rows: np.ndarray,
2148-
/,
2149-
) -> Dict[int, int]:
2150-
cleanup_keep_len_by_seq: Dict[int, int] = {}
2151-
for output in sampled_outputs:
2152-
update = updates[output.update_index]
2153-
sample_index = update.sample_index
2154-
sample_source_row = update.row_indices[sample_index]
2155-
self.pending_h[output.seq_id] = h_tgt_rows[sample_source_row]
2156-
self.ready[output.seq_id] = True
2157-
self.ready_pos[output.seq_id] = output.ready_pos
2158-
cleanup_keep_len_by_seq[output.seq_id] = output.keep_len
2159-
2160-
return cleanup_keep_len_by_seq
2161-
2162-
def _truncate_sampled_outputs(self, cleanup_keep_len_by_seq: Dict[int, int]) -> None:
2163-
for seq_id, keep_len in cleanup_keep_len_by_seq.items():
2164-
self._truncate_memory(seq_id, keep_len)
2165-
21662129
def _start_sampled_draft_states(
21672130
self,
21682131
updates: Sequence["MTPDraftProvider.SampledBatchUpdate"],
@@ -2284,7 +2247,14 @@ def process_sampled_batch(
22842247
h_tgt = llama_cpp_ext.llama_get_embeddings_pre_norm(self.target_ctx)
22852248
if not h_tgt:
22862249
raise RuntimeError("missing target pre-norm embeddings for MTP")
2287-
n_target_rows = self._target_row_count(updates)
2250+
n_target_rows = max(
2251+
(
2252+
max(update.row_indices) + 1
2253+
for update in updates
2254+
if update.row_indices
2255+
),
2256+
default=0,
2257+
)
22882258
if n_target_rows <= 0:
22892259
return results
22902260
h_tgt_rows = np.ctypeslib.as_array(h_tgt, shape=(n_target_rows, self.n_embd))
@@ -2301,12 +2271,18 @@ def process_sampled_batch(
23012271

23022272
self._decode_sampled_context_rows(plan.context_rows, h_tgt_rows)
23032273
sampled_outputs = self._decode_sampled_pending_rows(plan, h_tgt_rows)
2304-
cleanup_keep_len_by_seq = self._mark_sampled_outputs_ready(
2305-
updates,
2306-
sampled_outputs,
2307-
h_tgt_rows,
2308-
)
2309-
self._truncate_sampled_outputs(cleanup_keep_len_by_seq)
2274+
cleanup_keep_len_by_seq: Dict[int, int] = {}
2275+
for output in sampled_outputs:
2276+
update = updates[output.update_index]
2277+
sample_index = update.sample_index
2278+
sample_source_row = update.row_indices[sample_index]
2279+
self.pending_h[output.seq_id] = h_tgt_rows[sample_source_row]
2280+
self.ready[output.seq_id] = True
2281+
self.ready_pos[output.seq_id] = output.ready_pos
2282+
cleanup_keep_len_by_seq[output.seq_id] = output.keep_len
2283+
2284+
for seq_id, keep_len in cleanup_keep_len_by_seq.items():
2285+
self._truncate_memory(seq_id, keep_len)
23102286

23112287
if sampled_outputs:
23122288
active = self._start_sampled_draft_states(

0 commit comments

Comments
 (0)