-
Notifications
You must be signed in to change notification settings - Fork 47
feat: add --service-tier-dist for per-request service_tier distribution #675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ajcasagrande
wants to merge
3
commits into
main
Choose a base branch
from
ajc/vip-only
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """ | ||
| Service tier distribution for OpenAI API requests. | ||
|
|
||
| Allows distributing requests across different service tiers (e.g., default, flex, priority) | ||
| with configurable probabilities. Format: ``tier:prob;tier:prob`` where probabilities are | ||
| percentages 0-100 that must sum to 100. | ||
|
|
||
| Example: | ||
| >>> from aiperf.common.models.service_tier_distribution import ServiceTierDistributionParser | ||
| >>> dist = ServiceTierDistributionParser.parse("default:50;flex:30;priority:20") | ||
| >>> tier = dist.sample() | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
|
|
||
| import numpy as np | ||
|
|
||
| from aiperf.common import random_generator as rng | ||
| from aiperf.common.aiperf_logger import AIPerfLogger | ||
|
|
||
| logger = AIPerfLogger(__name__) | ||
|
|
||
|
|
||
| def _validate_probability_sum(entries: list[ServiceTierEntry]) -> None: | ||
| """Validate that probabilities sum to approximately 100.0. | ||
|
|
||
| Args: | ||
| entries: List of ServiceTierEntry objects to validate | ||
|
|
||
| Raises: | ||
| ValueError: If probabilities don't sum to 100.0 (within floating-point tolerance) | ||
| """ | ||
| total_prob = sum(entry.probability for entry in entries) | ||
| if not np.isclose(total_prob, 100.0, rtol=1e-6, atol=1e-6): | ||
| raise ValueError( | ||
| f"Probabilities must sum to 100.0, got {total_prob:.6f}. " | ||
| f"Entries: {[str(e) for e in entries]}" | ||
| ) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ServiceTierEntry: | ||
| """Immutable representation of a service tier with probability weight.""" | ||
|
|
||
| tier: str | ||
| probability: float | ||
|
|
||
| def __post_init__(self) -> None: | ||
| """Validate tier name and probability on construction.""" | ||
| if not self.tier or not self.tier.strip(): | ||
| raise ValueError("Tier name must be non-empty") | ||
| if not 0.0 <= self.probability <= 100.0: | ||
| raise ValueError(f"Probability must be in [0,100], got {self.probability}") | ||
|
|
||
| def __str__(self) -> str: | ||
| return f"{self.tier}:{self.probability}%" | ||
|
|
||
|
|
||
| class ServiceTierDistribution: | ||
| """Manages probability distribution of service tiers for request sampling. | ||
|
|
||
| Supports efficient O(log n) sampling using binary search on cumulative | ||
| probability distribution. | ||
| """ | ||
|
|
||
| def __init__(self, entries: list[ServiceTierEntry]) -> None: | ||
| """Initialize distribution from list of service tier entries. | ||
|
|
||
| Args: | ||
| entries: List of ServiceTierEntry objects. Probabilities must sum to 100. | ||
|
|
||
| Raises: | ||
| ValueError: If entries is empty or probabilities don't sum to 100. | ||
| """ | ||
| if not entries: | ||
| raise ValueError( | ||
| "Distribution must contain at least one service tier entry" | ||
| ) | ||
|
|
||
| self._rng = rng.derive("models.service_tier.distribution") | ||
| self._entries = tuple(entries) | ||
| _validate_probability_sum(list(self._entries)) | ||
| self._cumulative_probs = self._compute_cumulative_probabilities() | ||
|
|
||
| logger.debug( | ||
| lambda: f"Created service tier distribution with {len(self._entries)} entries: {self}" | ||
| ) | ||
|
|
||
| def _compute_cumulative_probabilities(self) -> np.ndarray: | ||
| """Compute cumulative probability distribution for efficient sampling.""" | ||
| probs = [entry.probability / 100.0 for entry in self._entries] | ||
| return np.cumsum(probs, dtype=np.float64) | ||
|
|
||
| def sample(self) -> str: | ||
| """Sample a service tier according to the distribution. | ||
|
|
||
| Returns: | ||
| Service tier string value | ||
| """ | ||
| rand_val = self._rng.random() | ||
| idx = np.searchsorted(self._cumulative_probs, rand_val, side="right") | ||
| idx = min(idx, len(self._entries) - 1) | ||
| return self._entries[idx].tier | ||
|
|
||
| @property | ||
| def entries(self) -> tuple[ServiceTierEntry, ...]: | ||
| """Get immutable view of service tier entries.""" | ||
| return self._entries | ||
|
|
||
| def __str__(self) -> str: | ||
| entries_str = ";".join(str(entry) for entry in self._entries) | ||
| return f"ServiceTierDistribution[{entries_str}]" | ||
|
|
||
| def __repr__(self) -> str: | ||
| return f"ServiceTierDistribution({list(self._entries)})" | ||
|
|
||
|
|
||
| class ServiceTierDistributionParser: | ||
| """Parser for service tier distribution strings.""" | ||
|
|
||
| @classmethod | ||
| def parse(cls, dist_str: str) -> ServiceTierDistribution: | ||
| """Parse a service tier distribution string. | ||
|
|
||
| Format: ``tier:prob;tier:prob`` where probabilities are percentages 0-100. | ||
|
|
||
| Args: | ||
| dist_str: Distribution specification string (e.g., "default:50;flex:30;priority:20") | ||
|
|
||
| Returns: | ||
| ServiceTierDistribution object | ||
|
|
||
| Raises: | ||
| ValueError: If string format is invalid | ||
| """ | ||
| if not isinstance(dist_str, str) or not dist_str.strip(): | ||
| raise ValueError("Distribution string cannot be empty") | ||
|
|
||
| dist_str = dist_str.strip() | ||
| entries: list[ServiceTierEntry] = [] | ||
|
|
||
| for pair_str in dist_str.split(";"): | ||
| pair_str = pair_str.strip() | ||
| if not pair_str: | ||
| continue | ||
|
|
||
| parts = pair_str.rsplit(":", 1) | ||
| if len(parts) != 2: | ||
| raise ValueError( | ||
| f"Invalid pair format: '{pair_str}'. Expected 'tier:probability'" | ||
| ) | ||
|
|
||
| tier = parts[0].strip() | ||
| try: | ||
| probability = float(parts[1].strip()) | ||
| except ValueError as e: | ||
| raise ValueError( | ||
| f"Invalid probability value in '{pair_str}': {parts[1].strip()}" | ||
| ) from e | ||
|
|
||
| entries.append(ServiceTierEntry(tier=tier, probability=probability)) | ||
|
|
||
| if not entries: | ||
| raise ValueError("No valid entries found in distribution string") | ||
|
|
||
| return ServiceTierDistribution(entries) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding multi-turn: does anyone switch service tiers mid-conversation?
I would think not, in which case sampling per-conversation seems to make more sense than per-turn.
Thoughts?