speculators.data_generation.preprocessing
Functions:
-
build_speculator_training_dataset–Build a speculator training dataset with render-boundary loss masks.
-
default_preprocessing_workers–Choose preprocessing workers within the shared render CPU budget.
-
load_and_preprocess_dataset–Load, tokenize, and preprocess a dataset for speculator training.
-
load_raw_dataset–Load a raw dataset from one of several source types.
BoundaryUnstableError
Bases: ValueError
The chat template is not prefix-stable at an assistant turn boundary.
build_speculator_training_dataset
build_speculator_training_dataset(
dataset: Dataset,
processor: ProcessorLike,
max_length: int = 2048,
num_proc: int = 8,
*,
render_endpoint: str | None = None,
minimum_valid_tokens: int | None = None,
) -> HFDataset
Build a speculator training dataset with render-boundary loss masks.
Both accepted representations contain responses produced by the target model. Natural-language conversations are tokenized by the vLLM /render endpoint and masked at each assistant-turn boundary, fanning out to one row per assistant turn. Rendering only converts representation; it does not generate responses or make arbitrary data on-policy. Speculator-format rows already carry input_ids and loss_mask and pass straight through.
Args: dataset: On-policy natural-language conversations, or speculator-format rows containing input_ids and loss_mask. processor: Processor, used to detect multimodal inputs and to decode. max_length: Maximum sequence length. num_proc: Number of worker processes; each renders concurrently. render_endpoint: Base URL of a vLLM server. Required unless the dataset is already in speculator format. minimum_valid_tokens: Minimum supervised tokens for a row to be kept.
Source code in speculators/data_generation/preprocessing.py
default_preprocessing_workers
Choose preprocessing workers within the shared render CPU budget.
Source code in speculators/data_generation/preprocessing.py
load_and_preprocess_dataset
load_and_preprocess_dataset(
target_model_path: str,
train_data_paths: list[str],
*,
seq_length: int,
build_dataset_num_proc: int = 8,
seed: int = 0,
max_samples: int | None = None,
token_freq_path: Path | str = "./token_freq.pt",
render_endpoint: str | None = None,
minimum_valid_tokens: int | None = None,
allow_empty_output: bool = False,
trust_remote_code: bool = False,
) -> tuple[HFDataset, ProcessorLike]
Load, tokenize, and preprocess a dataset for speculator training.
Natural-language conversations containing target-model responses are tokenized by a vLLM /render endpoint and masked at each assistant-turn boundary. Speculator-format rows pass straight through. Rendering converts representation; it does not generate or validate response provenance. Caching is handled automatically by HuggingFace datasets.
Args: target_model_path: HuggingFace model ID or local path train_data_path: Dataset name or path to JSON/JSONL file seq_length: Maximum sequence length build_dataset_num_proc: Number of processes for dataset building seed: Random seed for shuffling max_samples: Optional limit on number of samples token_freq_path: Path to save token frequency distribution cache_dir: Directory to cache HuggingFace datasets (optional) render_endpoint: Base URL of a running vLLM server (e.g. http://localhost:8000) used to render conversations. Required unless every dataset is already in speculator format. minimum_valid_tokens: Number of tokens to consider for a valid sample allow_empty_output: If True, allow returning an empty dataset instead of raising when no samples survive preprocessing. trust_remote_code: If True, allows executing code from HF Hub.
Returns: Tuple of (preprocessed_dataset, processor)
Source code in speculators/data_generation/preprocessing.py
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 | |
load_raw_dataset
Load a raw dataset from one of several source types.
Resolution order: 1. Local .json/.jsonl file. 2. Local directory: recursively load all *.json/*.jsonl files as a single dataset. 3. Named preset from DATASET_CONFIGS. 4. hf:<id>[:<subset>:<split>] for an arbitrary HuggingFace dataset.
Args: train_data_path: File path, directory path, preset name, or hf: spec.
Returns: Tuple of (raw_dataset, normalize_fn). normalize_fn is None for sources already in conversations format.
Raises: ValueError: If the source cannot be resolved or a local directory contains no .json/.jsonl files.
Source code in speculators/data_generation/preprocessing.py
usable_cpu_count
Return the CPUs available to this process, respecting affinity.