solvers.CompressionModel
solvers.CompressionModel()Analyzes model compression trade-offs (Accuracy vs. Efficiency).
This model simulates the ‘Compression Tax’ — the accuracy degradation that occurs when reducing model size via quantization or pruning, balanced against the gains in memory footprint and inference latency.
Literature Source: 1. Han et al. (2015), “Deep Compression: Compressing Deep Neural Networks with Pruning, Trained Quantization and Huffman Coding.” 2. Gholami et al. (2021), “A Survey of Quantization Methods for Efficient Neural Network Inference.” 3. Blalock et al. (2020), “What is the State of Neural Network Pruning?”
Methods
| Name | Description |
|---|---|
| candidate | Apply declared size, quality, speed, and support constraints to a candidate. |
| solve | Estimate physical compression footprint, Roofline speedup, and supplied quality delta. |
| sweep | Evaluate compression configurations and identify nondominated candidates. |
candidate
solvers.CompressionModel.candidate(
model,
hardware,
*,
label=None,
method='quantization',
target_bitwidth=8,
sparsity=0.0,
sparsity_type='unstructured',
size_limit=None,
max_accuracy_drop=None,
min_speedup=None,
require_hardware_support=False,
baseline_precision='fp32',
group_size=None,
scale_bits=16,
accuracy_delta=0.0,
accuracy_curve=None,
)Apply declared size, quality, speed, and support constraints to a candidate.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model | Workload | Workload specification supplying parameter count and a lowered graph. | required |
| hardware | HardwareNode | Device specification supplying memory bandwidth and precision compute rates. | required |
| label | str | Display name; defaults to a name derived from the compression configuration. | None |
| method | str | Compression branch, quantization or pruning; other names are no-ops. |
'quantization' |
| target_bitwidth | int | Positive target bits per weight for quantization; default eight. | 8 |
| sparsity | float | Removed-weight fraction in [0, 1] for pruning; default zero. | 0.0 |
| sparsity_type | str | Pruning pattern: unstructured, structured, or N:M; common 2:4 spellings normalize to n_m. Default unstructured. |
'unstructured' |
| size_limit | Quantity | Maximum compressed checkpoint size, convertible to bytes. | None |
| max_accuracy_drop | float | Maximum accuracy drop as a fraction, not percent. A gain never violates it. | None |
| min_speedup | float | Minimum dimensionless modeled speed ratio relative to baseline precision. | None |
| require_hardware_support | bool | Require the registry’s precision/sparsity support heuristic; default False. | False |
| baseline_precision | str | Uncompressed precision key; default fp32. All ratios use this baseline. | 'fp32' |
| group_size | int | Positive quantization block size sharing one scale metadata element. | None |
| scale_bits | int | Bitwidth of each quantization group’s scale metadata when group_size is set. |
16 |
| accuracy_delta | float | Caller-supplied empirical task quality change passed to solve. |
0.0 |
| accuracy_curve | AccuracyCurve | Measured accuracy curve passed to solve. The max_accuracy_drop gate then tests the curve’s value, and source_trace names the curve. |
None |
Returns
| Name | Type | Description |
|---|---|---|
| CompressionCandidate | Sizes in GB, speed/storage ratios, supplied quality delta, support status, and constraint violations. Feasible means these requested gates pass; it does not certify runtime memory residency or deployment performance. |
See Also
CompressionModel.solve : Compression estimates and their assumptions.
Notes
Equation: feasible = all(requested limits pass), where size <= size_limit, max(0, -quality_delta) <= max_accuracy_drop, speedup >= min_speedup, and optional hardware support. Missing limits impose no condition.
Assumptions: the estimates returned by solve apply unchanged. Hardware support is inferred from registry keys and pattern names, not from a kernel execution. Constraint checks use the same baseline for every metric.
Excludes: validation of task accuracy, sparse-kernel availability, workspace, and the quality of an actually compressed checkpoint.
References
Derivation: candidate admission is the conjunction of user-supplied scalar bounds on the compression model outputs; the first failed check is reported as binding while all violations remain in the result.
Examples
>>> from mlsysim import Models, Hardware, Q_
>>> result = CompressionModel().candidate(Models.Language.Llama3_8B,
... Hardware.Cloud.H100, target_bitwidth=8, size_limit=Q_("1 byte"))
>>> result.feasible
False
>>> result.binding_constraint
'model_size'solve
solvers.CompressionModel.solve(
model,
hardware,
method='quantization',
target_bitwidth=8,
sparsity=0.0,
sparsity_type='unstructured',
baseline_precision='fp32',
group_size=None,
scale_bits=16,
accuracy_delta=0.0,
accuracy_curve=None,
)Estimate physical compression footprint, Roofline speedup, and supplied quality delta.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model | Workload | The model to be compressed. | required |
| hardware | HardwareNode | The target execution hardware. | required |
| method | str | Compression method ('quantization', 'pruning', or 'distillation'). |
'quantization' |
| target_bitwidth | int | Positive quantized bits per weight (e.g., 8 for INT8/FP8, 4 for INT4). | 8 |
| sparsity | float | Removed-weight fraction in [0, 1] for pruning. |
0.0 |
| sparsity_type | str | Sparsity pattern: 'unstructured' (storage savings only), 'structured' (storage and compute savings), or 'n_m' (Ampere+ 2:4 hardware sparsity, 2x speedup at 50% sparsity). |
'unstructured' |
| baseline_precision | str | Uncompressed precision resolved through core.units.PRECISION_MAP (e.g., 'fp32' or 'fp16'). |
'fp32' |
| group_size | int | Positive quantization block size g sharing one scale metadata element. When None, scale metadata overhead is omitted (per-tensor / channel limit). |
None |
| scale_bits | int | Positive bitwidth b_scale of each quantization group’s scale metadata when group_size is set. |
16 |
| accuracy_delta | float | Caller-supplied empirical task quality change (e.g., -0.01 for a 1% drop). Task accuracy depends on model, calibration data, and evaluation benchmark rather than bitwidth or sparsity alone, so no synthetic accuracy curve is invented when omitted. |
0.0 |
| accuracy_curve | AccuracyCurve | A measured accuracy curve for this model and method. When given, estimated_accuracy_delta is read from the curve at target_bitwidth (a bitwidth curve, with quantization) or at sparsity (a sparsity curve, with pruning), and the result’s constraint_trace names the curve. Cannot be combined with a non-zero accuracy_delta. |
None |
Returns
| Name | Type | Description |
|---|---|---|
| CompressionResult | Original and compressed sizes in GB, compression ratio, memory savings percentage, Roofline inference speedup, and the supplied accuracy_delta (or curve-derived delta). |
Raises
| Name | Type | Description |
|---|---|---|
| TypeError | If accuracy_curve is not an AccuracyCurve. |
|
| ValueError | If accuracy_curve has the wrong axis for method, if method is neither quantization nor pruning, if the requested level is outside the curve’s measured range, or if both a non-zero accuracy_delta and accuracy_curve are passed. |
See Also
mlsysim.physics.performance.calc_bottleneck : Weight-only Roofline regime.
Notes
Equation: for quantization, effective bits per weight are b_eff = target_bitwidth + (scale_bits / group_size if group_size else 0) and compression_ratio = baseline_bits / b_eff. For pruning, compression_ratio = 1 / (1 - sparsity) (capped at 100x at sparsity == 1). compressed_bytes = original_bytes / compression_ratio.
Assumptions: baseline weight-only Roofline regime determines whether quantization speedup follows bandwidth savings (compression_ratio) or low-precision Tensor Core peak ratios; structured and 2:4 n_m pruning convert sparsity into compute speedup while unstructured pruning saves storage only. estimated_accuracy_delta echoes accuracy_delta directly unless accuracy_curve is supplied, in which case the curve is taken to describe this model, method, and task (the solver cannot verify workload identity).
Excludes: sparse index metadata (CSR/bitmask overhead), dequantization kernel overhead, activation quantization, and unmeasured task-accuracy prediction.
References
Derivation: bit-packing storage divides element count by effective bits per weight including scale_bits / group_size block scales; Roofline speedup compares weight-bandwidth savings against precision FLOP/s ceilings.
Examples
>>> from mlsysim import Models, Hardware
>>> result = CompressionModel().solve(Models.Language.Llama3_8B,
... Hardware.Cloud.H100, target_bitwidth=8, baseline_precision="fp16")
>>> result.compression_ratio == 2
Truesweep
solvers.CompressionModel.sweep(
model,
hardware,
candidate_configs,
*,
size_limit=None,
max_accuracy_drop=None,
min_speedup=None,
require_hardware_support=False,
objective='min_size_max_speed_preserve_quality',
baseline_precision='fp32',
)Evaluate compression configurations and identify nondominated candidates.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model | Workload | Common workload evaluated for every candidate. | required |
| hardware | HardwareNode | Common device profile evaluated for every candidate. | required |
| candidate_configs | list of dict | Candidate keyword dictionaries accepted by candidate, such as method, target_bitwidth, sparsity, sparsity_type, accuracy_curve, and label. Do not repeat the shared constraints or baseline passed separately here. |
required |
| size_limit | Quantity | Maximum checkpoint size for each candidate, convertible to bytes. | None |
| max_accuracy_drop | float | Maximum accuracy drop as a fraction, not percent. A gain never violates it. | None |
| min_speedup | float | Minimum modeled speed ratio relative to the common baseline. | None |
| require_hardware_support | bool | Require the candidate support heuristic; default False. | False |
| objective | str | Descriptive result label only; changing it does not change the selection algorithm. Default min_size_max_speed_preserve_quality. |
'min_size_max_speed_preserve_quality' |
| baseline_precision | str | Common uncompressed precision key; default fp32. | 'fp32' |
Returns
| Name | Type | Description |
|---|---|---|
| CompressionSweepResult | Candidate records, frontier/dominated labels, and a selected label among feasible frontier candidates, or None when none exist. Sizes are in GB; quality and speed comparisons are dimensionless modeled quantities. |
See Also
CompressionModel.candidate : Constraint checks and support heuristics.
Notes
Equation: A dominates B if A has no larger size, no lower speed, and no lower quality (1 + accuracy_delta), with at least one strict improvement. Among feasible frontier entries, selection minimizes the tuple (max(0, -accuracy_delta), size/max(speedup, 1e-9)).
Assumptions: each candidate uses the same workload, hardware, and baseline. Dominance is evaluated across all candidates, including infeasible ones; feasibility is applied afterward when choosing the returned best label.
Excludes: global optimization outside the supplied candidate list, measured accuracy/performance, uncertainty intervals, and runtime memory feasibility.
References
Derivation: Pareto dominance is the componentwise ordering of storage cost, speed benefit, and assumed quality. The lexicographic tie-break is an explicit implementation policy, not a learned or universally optimal preference.
Examples
>>> from mlsysim import Models, Hardware
>>> result = CompressionModel().sweep(Models.Language.Llama3_8B,
... Hardware.Cloud.H100, [{"target_bitwidth": 8, "label": "int8"}])
>>> result.frontier_labels
['int8']