nat.profiler.prediction_trie.trie_builder#
Classes#
A paired START/END span used for parallel sibling overlap detection. |
|
Configuration for auto-sensitivity scoring. |
|
Context for a single LLM call extracted from a trace. |
|
Accumulators for a single trie node. |
|
Builds a prediction trie from profiler execution traces. |
Module Contents#
- class _SiblingSpan#
A paired START/END span used for parallel sibling overlap detection.
- class SensitivityConfig#
Configuration for auto-sensitivity scoring.
- class LLMCallContext#
Context for a single LLM call extracted from a trace.
- class _NodeAccumulators#
Accumulators for a single trie node.
- remaining_calls: dict[int, nat.profiler.prediction_trie.metrics_accumulator.MetricsAccumulator]#
- interarrival_ms: dict[int, nat.profiler.prediction_trie.metrics_accumulator.MetricsAccumulator]#
- output_tokens: dict[int, nat.profiler.prediction_trie.metrics_accumulator.MetricsAccumulator]#
- all_remaining_calls: nat.profiler.prediction_trie.metrics_accumulator.MetricsAccumulator#
- all_interarrival_ms: nat.profiler.prediction_trie.metrics_accumulator.MetricsAccumulator#
- all_output_tokens: nat.profiler.prediction_trie.metrics_accumulator.MetricsAccumulator#
- all_sensitivity: nat.profiler.prediction_trie.metrics_accumulator.MetricsAccumulator#
- class PredictionTrieBuilder(
- sensitivity_config: SensitivityConfig | None = None,
Builds a prediction trie from profiler execution traces.
- _node_accumulators: dict[tuple[str, Ellipsis], _NodeAccumulators]#
- _sensitivity_config = None#
- _extract_llm_contexts( ) list[LLMCallContext]#
Extract LLM call contexts from a trace.
- _compute_sensitivity_scores(contexts: list[LLMCallContext]) None#
Compute composite sensitivity scores for each call in the trace.
Parallel siblings are detected via temporal overlap and assigned the same logical position so that the U-shaped position signal and fan-out signal treat them as a single workflow step rather than spreading them across sequential indices.
After computing raw weighted scores, the values are min-max normalized across all calls in the trace so the full 0–1 range is used. This ensures the most-sensitive call in a trace maps to the top of the scale and the least-sensitive call maps to the bottom.
- static _compute_logical_positions(
- contexts: list[LLMCallContext],
Assign a logical position to each call, collapsing parallel siblings.
Uses standard interval-merging: contexts are sorted by span start time, and any call whose start is before the current group’s latest end time joins the group (capturing transitive overlaps). The resulting group indices are then mapped back to the original LLM_END ordering.
All calls in a parallel group share the same logical position index, so the U-shaped position signal and fan-out signal treat them as occupying a single workflow step.
- static _build_sibling_map( ) dict[str, list[_SiblingSpan]]#
Pair START/END events by UUID, then group by parent_id.
Only considers LLM, TOOL, FUNCTION, and SPAN event types. Returns a mapping from parent_id to all completed sibling spans under that parent.
- static _compute_parallel_slack(
- llm_uuid: str,
- llm_start: float,
- llm_end: float,
- siblings: list[_SiblingSpan],
Compute the parallel slack ratio for an LLM call relative to its siblings.
slack = max(0, 1 - llm_duration / max_overlapping_sibling_duration)
Returns 0.0 when the LLM call is the longest overlapping sibling, and approaches 1.0 when a much longer sibling runs in parallel.
- _update_accumulators(ctx: LLMCallContext) None#
Update accumulators at every node along the path.
- _add_to_accumulators(
- path_key: tuple[str, Ellipsis],
- ctx: LLMCallContext,
Add context data to accumulators for a specific path.
- build() nat.profiler.prediction_trie.data_models.PredictionTrieNode#
Build the final prediction trie from accumulated data.
- _get_or_create_node(
- root: nat.profiler.prediction_trie.data_models.PredictionTrieNode,
- path_key: tuple[str, Ellipsis],
Navigate to or create a node at the given path.