ptwm
ptwm.ablation
PTWM ablation harness: resolved policy, bench-compress orchestration, attribution.
ptwm.ablation
PTWM ablation harness: resolved policy, bench-compress orchestration, attribution.
ptwm.ablation
python/ptwm/ablation/__init__.py:1module ptwm.ablationPTWM ablation harness: resolved policy, bench-compress orchestration, attribution.
* _resolve.py: active-trust-aware policy resolution
* _bench.py: ptwm bench compress
* _compare.py: ptwm bench compare
* _attribute.py: leave-one-out + Shapley
* _replay.py: resolved-policy round-trip
ptwm.ablation.attribute_leave_one_out
python/ptwm/ablation/_attribute.py:47def attribute_leave_one_out(input_path: str | Path, baseline_policy: str | Path | None, out_dir: str | Path, max_attributions: int = 32) -> AttributionResultO(n) re-encodes -- one per used contribution with that id ignored.
| Name | Type | Description |
|---|---|---|
| input_path | str | Path | Path to the raw binary input file to compress. |
| baseline_policy | str | Path | None | Path to a TOML policy file for the baseline run, or ``None`` to use the default empty policy. |
| out_dir | str | Path | Directory for intermediate compressed files and CSV output. |
| max_attributions = 32 | int | Maximum number of contributions to attribute. If the baseline uses more than this many contributions, only the first ``max_attributions`` are attributed. |
Returns
AttributionResult — One row per attributed contribution. ``delta_bytes`` and
``delta_pct`` are zero until the
ignore-policy path is wired in.ptwm.ablation.attribute_shapley
python/ptwm/ablation/_attribute.py:115def attribute_shapley(input_path: str | Path, baseline_policy: str | Path | None, out_dir: str | Path, max_attributions: int = 8) -> AttributionResultShapley attribution. Gated by max_attributions <= 8.
| Name | Type | Description |
|---|---|---|
| input_path | str | Path | Path to the raw binary input file to compress. |
| baseline_policy | str | Path | None | Path to a TOML policy file for the baseline run, or ``None`` to use the default empty policy. |
| out_dir | str | Path | Directory for intermediate compressed files and CSV output. |
| max_attributions = 8 | int | Maximum number of contributions allowed. Raises ``ValueError`` when the baseline uses more contributions than this limit, to avoid a 2^n * n encode blowup. |
Returns
AttributionResult — One row per contribution. ``delta_bytes`` and ``delta_pct`` are
zero until the ignore-policy path is wired in. Raises
ValueError— When the baseline references more contributions than ``max_attributions``.
ptwm.ablation.AttributionResult
python/ptwm/ablation/_attribute.py:39class AttributionResult:Result of a leave-one-out or Shapley attribution run.
ptwm.ablation.AttributionResult.__init__
python/ptwm/ablation/_attribute.py:1def __init__(self, method: str, rows: list[AttributionRow]) -> Noneptwm.ablation.AttributionResult.method
python/ptwm/ablation/_attribute.py:43method: strptwm.ablation.AttributionResult.rows
python/ptwm/ablation/_attribute.py:44rows: list[AttributionRow]ptwm.ablation.AttributionRow
python/ptwm/ablation/_attribute.py:27class AttributionRow:Per-contribution attribution row.
ptwm.ablation.AttributionRow.__init__
python/ptwm/ablation/_attribute.py:1def __init__(self, contribution_id: str, label: str, baseline_size: int, without_size: int, delta_bytes: int, delta_pct: float) -> Noneptwm.ablation.AttributionRow.baseline_size
python/ptwm/ablation/_attribute.py:33baseline_size: intptwm.ablation.AttributionRow.contribution_id
python/ptwm/ablation/_attribute.py:31contribution_id: strptwm.ablation.AttributionRow.delta_bytes
python/ptwm/ablation/_attribute.py:35delta_bytes: intptwm.ablation.AttributionRow.delta_pct
python/ptwm/ablation/_attribute.py:36delta_pct: floatptwm.ablation.AttributionRow.label
python/ptwm/ablation/_attribute.py:32label: strptwm.ablation.AttributionRow.without_size
python/ptwm/ablation/_attribute.py:34without_size: intptwm.ablation.bench_compare
python/ptwm/ablation/_compare.py:22def bench_compare(a: str | Path, b: str | Path) -> CompareResultCompare the extension tables and file sizes of two `.ptwm` files.
| Name | Type | Description |
|---|---|---|
| a | str | Path | Path to the first `.ptwm` file. |
| b | str | Path | Path to the second `.ptwm` file. |
Returns
CompareResult — A dataclass describing which contribution IDs are present only in
*a*, only in *b*, in both, and the on-disk sizes of each file.ptwm.ablation.bench_compress
python/ptwm/ablation/_bench.py:42def bench_compress(input_path: str | Path, baseline_policy: str | Path | None, variant_policies: Iterable[str | Path], metrics: list[str], trials: int, out_dir: str | Path) -> BenchResultRun the compressor under each policy and emit per-trial metrics.
Returns a :class:`BenchResult` holding every row; also writes
``<out_dir>/results.csv``, ``<out_dir>/per_tensor.csv``, and
``<out_dir>/summary.md``.
| Name | Type | Description |
|---|---|---|
| input_path | str | Path | Path to the raw binary input file. |
| baseline_policy | str | Path | None | Path to a TOML policy file for the baseline run, or ``None`` to use the default empty policy. |
| variant_policies | Iterable[str | Path] | Iterable of paths to variant policy TOML files. |
| metrics | list[str] | List of metric names to surface in the summary. v1 always captures ``ratio`` and ``encode-time``; other names are recorded in the summary for forward compatibility. |
| trials | int | Number of compress trials to execute per variant. |
| out_dir | str | Path | Directory to write ``results.csv`` and ``summary.md`` into. |
ptwm.ablation.bench_replay
python/ptwm/ablation/_replay.py:31def bench_replay(results_csv: str | Path, row: int, out_dir: str | Path | None = None) -> ReplayResultRe-run a single row from a previous ``bench compress`` results CSV.
| Name | Type | Description |
|---|---|---|
| results_csv | str | Path | Path to the ``results.csv`` produced by :func:`bench_compress`. |
| row | int | Zero-based row index to replay. |
| out_dir = None | str | Path | None | Directory for the replayed ``.ptwm`` file. Defaults to a ``replay/`` sub-directory next to the CSV. |
Returns
ReplayResult — Comparison of the expected (CSV) vs actual output sizes.ptwm.ablation.BenchResult
python/ptwm/ablation/_bench.py:35class BenchResult:Aggregated result from a bench compress run.
ptwm.ablation.BenchResult.__init__
python/ptwm/ablation/_bench.py:1def __init__(self, rows: list[BenchRow] = list()) -> Noneptwm.ablation.BenchResult.rows
python/ptwm/ablation/_bench.py:39rows: list[BenchRow] = field(default_factory=list)ptwm.ablation.BenchRow
python/ptwm/ablation/_bench.py:19class BenchRow:Single per-trial metric row.
ptwm.ablation.BenchRow.__init__
python/ptwm/ablation/_bench.py:1def __init__(self, variant: str, trial: int, input_bytes: int, output_bytes: int, ratio: float, encode_ns: int, decode_ns: int, peak_mem_bytes: int, resolved_policy_toml: str, out_file: str) -> Noneptwm.ablation.BenchRow.decode_ns
python/ptwm/ablation/_bench.py:29decode_ns: intptwm.ablation.BenchRow.encode_ns
python/ptwm/ablation/_bench.py:28encode_ns: intptwm.ablation.BenchRow.input_bytes
python/ptwm/ablation/_bench.py:25input_bytes: intptwm.ablation.BenchRow.out_file
python/ptwm/ablation/_bench.py:32out_file: strptwm.ablation.BenchRow.output_bytes
python/ptwm/ablation/_bench.py:26output_bytes: intptwm.ablation.BenchRow.peak_mem_bytes
python/ptwm/ablation/_bench.py:30peak_mem_bytes: intptwm.ablation.BenchRow.ratio
python/ptwm/ablation/_bench.py:27ratio: floatptwm.ablation.BenchRow.resolved_policy_toml
python/ptwm/ablation/_bench.py:31resolved_policy_toml: strptwm.ablation.BenchRow.trial
python/ptwm/ablation/_bench.py:24trial: intptwm.ablation.BenchRow.variant
python/ptwm/ablation/_bench.py:23variant: strptwm.ablation.CompareResult
python/ptwm/ablation/_compare.py:11class CompareResult:Result of comparing extension tables in two `.ptwm` files.
ptwm.ablation.CompareResult.__init__
python/ptwm/ablation/_compare.py:1def __init__(self, only_in_a: list[tuple[str, str]], only_in_b: list[tuple[str, str]], common: list[tuple[str, str]], size_a: int, size_b: int) -> Noneptwm.ablation.CompareResult.common
python/ptwm/ablation/_compare.py:17common: list[tuple[str, str]]ptwm.ablation.CompareResult.only_in_a
python/ptwm/ablation/_compare.py:15only_in_a: list[tuple[str, str]]ptwm.ablation.CompareResult.only_in_b
python/ptwm/ablation/_compare.py:16only_in_b: list[tuple[str, str]]ptwm.ablation.CompareResult.size_a
python/ptwm/ablation/_compare.py:18size_a: intptwm.ablation.CompareResult.size_b
python/ptwm/ablation/_compare.py:19size_b: intptwm.ablation.ReplayResult
python/ptwm/ablation/_replay.py:21class ReplayResult:ptwm.ablation.ReplayResult.__init__
python/ptwm/ablation/_replay.py:1def __init__(self, row_variant: str, row_trial: int, expected_output_bytes: int, actual_output_bytes: int, matches: bool, out_file: str) -> Noneptwm.ablation.ReplayResult.actual_output_bytes
python/ptwm/ablation/_replay.py:26actual_output_bytes: intptwm.ablation.ReplayResult.expected_output_bytes
python/ptwm/ablation/_replay.py:25expected_output_bytes: intptwm.ablation.ReplayResult.matches
python/ptwm/ablation/_replay.py:27matches: boolptwm.ablation.ReplayResult.out_file
python/ptwm/ablation/_replay.py:28out_file: strptwm.ablation.ReplayResult.row_trial
python/ptwm/ablation/_replay.py:24row_trial: intptwm.ablation.ReplayResult.row_variant
python/ptwm/ablation/_replay.py:23row_variant: strptwm.ablation.resolve_active
python/ptwm/ablation/_resolve.py:56def resolve_active(policy_path: str | os.PathLike | None = None) -> ResolvedPolicyResolve `policy_path` against the *active* trust state.
`policy_path` may be a path to a TOML policy file or ``None`` to use
the default empty policy.
Callers (`ptwm bench compress`) call this once at run start
and serialize the resulting TOML into every CSV row for
reproducibility.