Complete API documentation for the prefix synthesis module.
aiperf.dataset.synthesisRollingHasherConverts sequences of text blocks into globally unique hash IDs using rolling hash.
Constructor:
Parameters:
block_size (int): Number of tokens per block for hashing (default: 512)Methods:
hash_blocks(blocks: Sequence[str]) -> list[int]
blocks: Sequence of text strings representing blockshash_token_blocks(blocks: Sequence[Sequence[int]]) -> list[int]
blocks: Sequence of token blocks (each block is a sequence of token IDs)reset() -> None
_hash_to_id and _id_counter)get_stats() -> dict[str, int]
Example:
RadixTreeCompact representation of prefix patterns using a radix tree data structure.
Constructor:
Methods:
add_path(path: list[int]) -> RadixNode
path: List of edge labels (hash IDs or token counts)get_node(node_id: int) -> RadixNode | None
get_all_nodes() -> list[RadixNode]
get_stats() -> RadixTreeStats
RadixTreeStats object with ‘num_nodes’, ‘num_leaves’, ‘total_visits’, ‘max_depth’Properties:
root: RadixNode
Example:
RadixNodeA node in the radix tree representing a prefix path.
Constructor:
Attributes:
node_id: Unique node identifierlabel: Edge label (token count)visit_count: Number of times this node is visitedchildren: Dictionary of child nodes by edge labelparent: Parent node referenceMethods:
add_child(label: int, child: RadixNode) -> None
get_child(label: int) -> RadixNode | None
is_leaf() -> bool
EmpiricalSamplerSamples values from an empirical distribution learned from data.
Constructor:
Parameters:
data: List of observed values to learn distribution fromMethods:
sample() -> int | float
sample_batch(size: int) -> list[int | float]
size: Number of samples to drawget_stats() -> EmpiricalSamplerStats
EmpiricalSamplerStats object with ‘min’, ‘max’, ‘mean’, ‘median’, ‘num_unique’Example:
PrefixAnalyzerAnalyzes traces to extract ISL/OSL statistics and prefix patterns.
Constructor:
Parameters:
block_size (int): Number of tokens per block for analysis (default: 512)Methods:
analyze_file(trace_file: Path | str) -> AnalysisStats
trace_file: Path to JSONL trace fileanalyze_traces(traces: list[dict]) -> AnalysisStats
traces: List of trace dictionariesExample:
SynthesizerGenerates synthetic traces preserving prefix-sharing patterns.
Constructor:
Parameters:
params (SynthesisParams): Generation configuration (optional, uses defaults if None)Methods:
synthesize_from_file(trace_file: Path | str) -> list[dict]
trace_file: Path to input JSONL trace filesynthesize_traces(traces: list[dict]) -> list[dict]
traces: List of input trace dictionariessynthesize_grouped_traces(data: dict[str, list[dict]]) -> dict[str, list[dict]]
data: Dictionary mapping session_id to list of trace dictsget_stats() -> dict[str, Any]
Example:
AnalysisStatsStatistics extracted from trace analysis.
Fields:
total_requests: int - Total number of requests in traceunique_prefixes: int - Number of unique prefix patterns (all prefix subsequences)num_prefix_groups: int - Number of distinct shared first blocks (prefix groups)cache_hit_rate: float - Theoretical cache hit rate (0.0 to 1.0)min_isl: int - Minimum input sequence lengthmax_isl: int - Maximum input sequence lengthavg_isl: float - Average input sequence lengthmin_osl: int - Minimum output sequence lengthmax_osl: int - Maximum output sequence lengthavg_osl: float - Average output sequence lengthprefix_reuse_ratio: float - Ratio of reused prefixes (0.0 to 1.0)isl_stats: MetricStats | None - Full statistics for input sequence lengthosl_stats: MetricStats | None - Full statistics for output sequence lengthcontext_length_stats: MetricStats | None - Full statistics for context (shared prefix) lengthunique_prompt_length_stats: MetricStats | None - Full statistics for unique prompt lengthhit_rate_stats: MetricStats | None - Full statistics for per-request cache hit ratesMetricStatsStatistics for a single metric with percentiles.
Fields:
mean: float - Mean valuestd_dev: float - Standard deviationmin: float - Minimum valuep25: float - 25th percentilemedian: float - Median (50th percentile)p75: float - 75th percentilemax: float - Maximum valueSynthesisParamsParameters for synthetic trace generation.
Fields:
speedup_ratio: float = 1.0 - Timestamp scaling multiplier (ge 0.0)prefix_len_multiplier: float = 1.0 - Core prefix length multiplier (ge 0.0)prefix_root_multiplier: int = 1 - Number of independent trees to distribute traces across (ge 1)prompt_len_multiplier: float = 1.0 - Leaf prompt length multiplier (ge 0.0)max_isl: int | None = None - Maximum input sequence length filterblock_size: int = 512 - KV cache page size (ge 1)Class Methods:
from_synthesis_config(config: SynthesisConfig, block_size: int = 512) -> SynthesisParams - Create from SynthesisConfigRadixTreeStatsStatistics about radix tree structure.
Fields:
num_nodes: int - Total number of nodes in treenum_leaves: int - Number of leaf nodes (nodes with no children)total_visits: int - Number of paths added to treemax_depth: int - Maximum depth from root to leafEmpiricalSamplerStatsStatistics about learned empirical distribution.
Fields:
min: float - Minimum value in original datamax: float - Maximum value in original datamean: float - Mean of original datamedian: float - Median of original datanum_unique: int - Number of unique values in distributionaiperf.dataset.synthesis.graph_utilsGraph manipulation utilities for radix tree operations.
validate_tree(tree: RadixTree) -> bool
remove_leaves(tree: RadixTree, visit_threshold: int = 1) -> None
tree: RadixTree to prunevisit_threshold: Minimum visit count to keepmerge_unary_chains(tree: RadixTree) -> None
compute_transition_cdfs(tree: RadixTree) -> dict[int, np.ndarray]
get_tree_stats(tree: RadixTree) -> dict[str, Any]
aiperf.dataset.synthesis.rolling_hasherUtility functions for converting between texts and hash IDs.
texts_to_hashes(tokenizer: Tokenizer, texts: list[str], block_size: int = 512) -> list[list[int]]
tokenizer: Tokenizer for encoding textstexts: List of input text stringsblock_size: Number of tokens per blockhashes_to_texts(prompt_generator: PromptGenerator, hash_ids_list: list[list[int]], input_lengths: list[int], block_size: int = 512) -> list[str]
prompt_generator: PromptGenerator instance for generating text from hash_idshash_ids_list: List of hash ID sequencesinput_lengths: Target input lengths (in tokens) for each sequenceblock_size: Number of tokens per blockValueError if len(hash_ids) * block_size < input_length for any sequenceaiperf analyze-traceAnalyze a mooncake trace file for ISL/OSL distributions and cache hit rates.
Usage:
Arguments:
INPUT_FILE: Path to input mooncake trace JSONL fileOptions:
--block-size INT (default: 512) - KV cache block size--output-file PATH - Optional output path for analysis report (JSON)Example:
Synthesis parameters can be configured via CLI or programmatically.
Via CLI Options (with aiperf profile):
Synthesis is applied automatically when running aiperf profile with mooncake traces and synthesis parameters:
Via SynthesisConfig:
Direct Instantiation:
Synthesis is applied automatically during benchmark when using mooncake traces with synthesis parameters:
Synthesis is triggered automatically when any --synthesis-* parameter differs from its default value.
remove_leaves() to prune infrequent paths for memory savingsAll components use standard Python exceptions: