-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathdata_generation_offline.py
More file actions
467 lines (396 loc) · 14.7 KB
/
Copy pathdata_generation_offline.py
File metadata and controls
467 lines (396 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!/usr/bin/env python3
"""
Offline Hidden States Generation Pipeline
This script generates hidden states and saves them to disk for offline training.
Usage:
python data_generation_offline.py \
--model meta-llama/Llama-3.1-8B-Instruct \
--preprocessed-data sharegpt \
--output ./training_data \
--max-samples 5000
"""
import argparse
import asyncio
import logging
import os
import shutil
import sys
from pathlib import Path
from typing import Any
import openai
from datasets import load_from_disk
from safetensors import safe_open
from tqdm import tqdm
from speculators.data_generation.vllm_client import (
DEFAULT_MAX_RETRIES,
DEFAULT_REQUEST_TIMEOUT,
generate_hidden_states_async,
wait_for_lock_async,
)
from speculators.train.data import build_client_item
from speculators.train.logger import setup_root_logger
logger = logging.getLogger(__name__)
class _FailureTracker:
"""Tracks consecutive sample failures across async workers.
When the number of consecutive failures (with no successes in between)
reaches ``threshold``, the tracker signals that the run should abort.
Because asyncio is single-threaded, no locking is needed.
"""
def __init__(self, threshold: int):
self.threshold = threshold
self._consecutive = 0
def record_success(self) -> None:
self._consecutive = 0
def record_failure(self) -> bool:
"""Record a failure. Returns True when the threshold is reached."""
self._consecutive += 1
return self._consecutive >= self.threshold
def parse_args():
parser = argparse.ArgumentParser(description="Generate EAGLE training data offline")
# Model arguments
parser.add_argument(
"--model",
type=str,
default=None,
help=(
"HuggingFace model ID or local path for target model "
"(default auto select). For verification purposes only."
),
)
parser.add_argument(
"--endpoint",
type=str,
default="http://localhost:8000/v1",
help=(
"The address of the vLLM instance to use for hidden states generation "
"(default: 'http://localhost:8000/v1'). "
"Note: the vLLM instance must be configured for hidden states extraction."
),
)
# Data arguments
parser.add_argument(
"--preprocessed-data",
type=str,
default="./output",
help="Path to preprocessed dataset (dataset produced by prepare_data.py)"
" (default: ./output)",
)
parser.add_argument(
"--max-samples",
type=int,
default=None,
help="Maximum number of samples to process (default: None, process all)",
)
# Output arguments
parser.add_argument(
"--output",
type=str,
default=None,
help=(
"Directory to generated hidden states files "
"(default args.preprocessed_data / 'hidden_states')"
),
)
# Hidden states generation arguments
parser.add_argument(
"--concurrency",
type=int,
default=32,
help=(
"Number of active vLLM requests at a time. "
"Note: number of async workers set to 2*concurrency"
),
)
parser.add_argument(
"--validate-outputs",
action="store_true",
help=(
"Load generated safetensor files and check output token ids match "
"prompt tokens and hidden states seq_len matches num tokens"
),
)
parser.add_argument(
"--request-timeout",
type=float,
default=DEFAULT_REQUEST_TIMEOUT,
help=(
"Timeout in seconds for each individual vLLM request "
f"(default: {DEFAULT_REQUEST_TIMEOUT})"
),
)
parser.add_argument(
"--max-retries",
type=int,
default=DEFAULT_MAX_RETRIES,
help=(
"Maximum number of retry attempts per request on failure "
f"(default: {DEFAULT_MAX_RETRIES})"
),
)
parser.add_argument(
"--fail-on-error",
action="store_true",
help=(
"Abort when a request fails after all retries. "
"By default, failed samples are skipped."
),
)
parser.add_argument(
"--max-consecutive-errors",
type=int,
default=None,
help=(
"Abort after this many consecutive sample failures (each sample "
"already retried --max-retries times). Prevents silently churning "
"through the entire dataset when the server is down. "
"Ignored when --fail-on-error is set. "
"(default: value of --concurrency)"
),
)
return parser.parse_args()
def get_existing_hidden_state_indices(output_path: Path) -> list[int]:
"""Find existing `hs_i.safetensors` files (where i is the file index)"""
existing_file_indices_set: set[int] = set()
if not output_path.exists():
return []
for file_path in output_path.iterdir():
if file_path.name.startswith("hs_") and file_path.name.endswith(".safetensors"):
index_str = file_path.stem[3:] # Remove "hs_" prefix
try:
file_index = int(index_str)
existing_file_indices_set.add(file_index)
except ValueError:
continue
return sorted(existing_file_indices_set)
def get_indices_to_process(
num_samples: int, max_samples: int | None, existing: list[int]
) -> list[int]:
"""Determines which indices should be processed. If max_samples is None
returns all dataset indices not in existing. Otherwise gets the first
`max_samples - len(existing)` samples not already in existing.
Args:
num_samples: Total size of preprocessed dataset
max_samples: (Optional) limit for number of samples to process
existing: list of ids that have already been processed
Returns:
list of dataset indices to process
"""
if len(existing) >= num_samples:
logger.info("All samples already processed!")
return []
if max_samples and len(existing) >= max_samples:
logger.info("At least max_samples already processed!")
return []
if len(existing) > 0:
logger.info(f"Found {len(existing)} existing samples.")
existing_s = set(existing)
if max_samples is None:
return [i for i in range(num_samples) if i not in existing_s]
num_remaining = min(max_samples, num_samples) - len(existing)
to_process = []
cur = 0
while num_remaining > 0 and cur < num_samples:
if cur not in existing_s:
to_process.append(cur)
num_remaining -= 1
cur += 1
return to_process
def check_safetensors_file(path: Path, tokens: list[int]):
with safe_open(path, "pt") as f:
t_ids = f.get_tensor("token_ids").tolist()
if t_ids != tokens:
raise ValueError(
f"Token ids in {path} don't match expected token ids {tokens}"
)
hs_slice = f.get_slice("hidden_states")
hs_shape = list(hs_slice.get_shape())
if len(tokens) != hs_shape[0]:
raise ValueError(
f"Sequence length of hidden states {hs_shape[0]} in {path}"
f" doesn't match num tokens {len(tokens)}"
)
async def worker( # noqa: C901
client,
model: str,
queue: "asyncio.Queue[dict[str, Any]]",
pbar: tqdm,
vllm_semaphore: asyncio.Semaphore,
write_semaphore: asyncio.Semaphore,
hidden_states_output_dir: Path,
validate_outputs: bool,
request_timeout: float | None,
max_retries: int,
fail_on_error: bool,
skipped_indices: list[int],
cancel_event: asyncio.Event,
failure_tracker: _FailureTracker | None,
):
"""Worker that pulls items from queue and sends them to the vLLM endpoint."""
while True:
item = await queue.get()
if item is None:
queue.task_done()
return
idx = item["idx"]
# Drain remaining items quickly after cancellation
if cancel_event.is_set():
queue.task_done()
continue
target_hidden_states_path = hidden_states_output_dir / f"hs_{idx}.safetensors"
try:
async with vllm_semaphore: # Limit number of active generate calls
hidden_states_path = await generate_hidden_states_async(
client,
model,
item,
timeout=request_timeout,
max_retries=max_retries,
)
lock_path = hidden_states_path + ".lock"
if Path(lock_path).exists():
await wait_for_lock_async(lock_path)
async with write_semaphore: # Limit number of active disk writes
await asyncio.to_thread(
shutil.move, hidden_states_path, target_hidden_states_path
)
if validate_outputs:
await asyncio.to_thread(
check_safetensors_file,
target_hidden_states_path,
item["input_ids"],
)
except Exception as e:
if fail_on_error:
logger.exception(
"Fatal: sample %d aborted with --fail-on-error: %s", idx, e
)
logging.shutdown()
os._exit(1)
logger.warning("Skipping sample %d due to error: %s", idx, e)
skipped_indices.append(idx)
if failure_tracker is not None and failure_tracker.record_failure():
cancel_event.set()
raise RuntimeError(
f"Aborting: {failure_tracker.threshold} consecutive samples "
"errored out. The vLLM server may be unreachable."
) from e
else:
if failure_tracker is not None:
failure_tracker.record_success()
finally:
pbar.update(1)
queue.task_done()
async def _feed_queue(to_process, dataset, queue, cancel_event):
"""Feed dataset items into the worker queue, respecting cancellation."""
for i in to_process:
if cancel_event.is_set():
break
dataset_item = dataset[i]
client_item = build_client_item(dataset_item) | {"idx": i}
# Check cancel_event while waiting for queue space to avoid
# deadlocking when all workers have died.
while not cancel_event.is_set():
try:
queue.put_nowait(client_item)
break
except asyncio.QueueFull:
await asyncio.sleep(0.1)
async def _shutdown_workers(workers, queue, cancel_event):
"""Shut down workers and propagate the first real exception."""
logger.info("Waiting for remaining file saves to complete...")
if cancel_event.is_set():
# Workers may be dead or draining — cancel any that are
# still alive so we don't deadlock on sentinel puts.
for w in workers:
if not w.done():
w.cancel()
else:
# Normal shutdown: send sentinel values so workers exit
for _ in range(len(workers)):
await queue.put(None)
results = await asyncio.gather(*workers, return_exceptions=True)
# Propagate the first real worker exception (skip CancelledError)
for result in results:
if isinstance(result, Exception) and not isinstance(
result, asyncio.CancelledError
):
raise result
async def generate_and_save_hidden_states(args, dataset):
if args.output is None:
hidden_states_dir = Path(args.preprocessed_data) / "hidden_states"
else:
hidden_states_dir = Path(args.output)
hidden_states_dir.mkdir(parents=True, exist_ok=True)
existing_file_indices = get_existing_hidden_state_indices(hidden_states_dir)
num_samples = len(dataset)
to_process = get_indices_to_process(
num_samples, args.max_samples, existing_file_indices
)
if not to_process:
return
logger.info(f"Processing {len(to_process)} samples")
queue: asyncio.Queue = asyncio.Queue(maxsize=args.concurrency * 4)
vllm_semaphore = asyncio.Semaphore(args.concurrency)
write_semaphore = asyncio.Semaphore(args.concurrency)
skipped_indices: list[int] = []
cancel_event = asyncio.Event()
max_consec = args.max_consecutive_errors
if max_consec is None:
max_consec = args.concurrency
failure_tracker = _FailureTracker(max_consec) if not args.fail_on_error else None
async with openai.AsyncOpenAI(
base_url=args.endpoint, api_key="EMPTY", max_retries=0
) as client:
list_models = await client.models.list()
model_id = list_models.data[0].id
if args.model and args.model != model_id:
raise ValueError(
f"An explicit model name was passed ({args.model}) which doesn't match"
f" found model_id {model_id}."
"Please make sure --endpoint is set to the correct vllm instance."
)
with tqdm(total=len(to_process)) as pbar:
workers = [
asyncio.create_task(
worker(
client,
model_id,
queue,
pbar,
vllm_semaphore,
write_semaphore,
hidden_states_dir,
args.validate_outputs,
args.request_timeout,
args.max_retries,
args.fail_on_error,
skipped_indices,
cancel_event,
failure_tracker,
)
)
for _ in range(args.concurrency * 2)
]
await _feed_queue(to_process, dataset, queue, cancel_event)
await _shutdown_workers(workers, queue, cancel_event)
num_saved = len(to_process) - len(skipped_indices)
logger.info(f"Saved {num_saved} new data points to {args.output}")
if skipped_indices:
logger.warning(
f"Skipped {len(skipped_indices)} samples due to errors: {skipped_indices}"
)
def main():
args = parse_args()
setup_root_logger()
logger.info("EAGLE Offline Data Generation")
dataset = load_from_disk(args.preprocessed_data)
try:
asyncio.run(generate_and_save_hidden_states(args, dataset))
except KeyboardInterrupt:
sys.exit(130)
except Exception:
logger.exception("Data generation failed")
sys.exit(1)
logger.info("Data generation complete!")
if __name__ == "__main__":
main()