Skip to navigation

aitune.torch.tune_strategy.mixin

View as MarkdownOpen in Claude

TuneStrategy-specific mixins.

Mixins should augment strategy configuration or tune lifecycle hooks instead of implementing _tune method.

Submodules

Package Contents

Classes

NameDescription
FindMaxBatchSizeMixinTuneStrategy mixin that finds max batch size.
PerformanceValidationMixinTuneStrategy mixin that validates each backend against a TorchEager throughput baseline.
PerformanceValidationMixinResultThroughput and speedup result for a single backend.

API

class aitune.torch.tune_strategy.mixin.FindMaxBatchSizeMixin(
args = (),
kwargs = {}
)

Bases: TuneStrategy

TuneStrategy mixin that finds max batch size.

Note: Find-max-batch-size is enabled by default and uses TorchEagerBackend as the neutral profiling backend.

_find_max_batch_size_backend_class
type[Backend] = TorchEagerBackend
aitune.torch.tune_strategy.mixin.FindMaxBatchSizeMixin._pre_tune(
module: torch.nn.Module,
name: str,
device: torch.device,
cache_dir: pathlib.Path
)

Extends tune method to find max batch size.

aitune.torch.tune_strategy.mixin.FindMaxBatchSizeMixin.enable_find_max_batch_size(
enable: bool = True

Enables or disables find max batch size.

aitune.torch.tune_strategy.mixin.FindMaxBatchSizeMixin.find_max_batch_size(
module: torch.nn.Module,
name: str,
device: torch.device,
cache_dir: pathlib.Path
)

Finds max batch size for the module.

aitune.torch.tune_strategy.mixin.FindMaxBatchSizeMixin.set_find_max_batch_size_default_backend_class(
default_backend_class: type[aitune.torch.backend.backend.Backend]

Sets default backend class for find max batch size.

class aitune.torch.tune_strategy.mixin.PerformanceValidationMixin(
args = (),
min_speedup_threshold_percent: float = 1.0,
kwargs = {}
)

Bases: TuneStrategy

TuneStrategy mixin that validates each backend against a TorchEager throughput baseline.

Unless performance validation is disabled, profiles TorchEager during _pre_tune with the strategy profiling config narrowed to the resolved batch size (from graph_spec.get_max_batch_size()) to establish a baseline. For every candidate backend, profiles with the same task-local profiling config and appends a PerformanceValidationMixinResult. In enabled mode, backends with speedup below the configured threshold are rejected. Diagnostic mode records the same results without affecting backend selection.

Note: min_speedup_threshold_percent defaults to 1.0, requiring a measurable 1% speedup in enabled mode. Performance validation is enabled by default.

_baseline_backend
Backend | None = None
_baseline_throughput
float | None = None
_performance_validation_mode
= PerformanceValidationMode.ENABLED
_resolved_batch_size
int | None = None
perf_validation_results
list[PerformanceValidationMixinResult] = []
performance_validation_mode
PerformanceValidationMode

Return the configured performance validation mode.

aitune.torch.tune_strategy.mixin.PerformanceValidationMixin._build_validate_and_check_perf(
module: torch.nn.Module,
name: str,
device: torch.device,
cache_dir: pathlib.Path,
raise_on_failure: bool = False

Build, correctness-validate, then profile and check throughput against baseline.

Returns the built backend, or None when correctness fails (raise_on_failure=False) or performance check rejects it (speedup below threshold).

aitune.torch.tune_strategy.mixin.PerformanceValidationMixin._log_baseline_selected(
) -> None

Emit an explicit message when the TorchEager baseline is the selected backend.

aitune.torch.tune_strategy.mixin.PerformanceValidationMixin._post_tune(
name: str,
)

Emits a speedup line after tuning completes.

aitune.torch.tune_strategy.mixin.PerformanceValidationMixin._pre_tune(
module: torch.nn.Module,
name: str,
device: torch.device,
cache_dir: pathlib.Path
)

Runs pre-tune setup and profiles TorchEager when performance validation is enabled.

aitune.torch.tune_strategy.mixin.PerformanceValidationMixin._profiling_config_for_batch_size(
batch_size: int,
batching: bool | None = None

Returns the strategy profiling config narrowed to one validation batch size.

aitune.torch.tune_strategy.mixin.PerformanceValidationMixin.enable_performance_validation(
mode: aitune.torch.tune_strategy.performance_validation.PerformanceValidationMode | str | bool = PerformanceValidationMode.E...

Set how eager-baseline performance data affects backend selection.

Parameters:

mode
PerformanceValidationMode | str | boolDefaults to PerformanceValidationMode.ENABLED

PerformanceValidationMode | str | bool — ENABLED applies the eager comparison as a selection gate, DIAGNOSTIC records the comparison without affecting selection, and DISABLED skips eager-baseline profiling. For compatibility, True maps to ENABLED and False maps to DISABLED.

class aitune.torch.tune_strategy.mixin.PerformanceValidationMixinResult(
backend_description: str,
throughput: float,
baseline_throughput: float,
speedup: float,
passed: bool
)
Dataclass

Throughput and speedup result for a single backend.

backend_description
str
baseline_throughput
float
passed
bool
speedup
float
throughput
float