Files
OBLITERATUS/docs/analysis-profiling.md
Joseph MaglyandClaude Opus 5.5 bbb0177915 feat(analysis): fill baseline profile work counters
Baseline profiles reported unique examples, token presentations,
forwards, generated tokens and tensor bytes as null. Count them from
load completion through cleanup with pass-through observers: a forward
pre-hook on the loaded model and instance-level wrappers on generate and
activation collection, all removed on exit and after failures.

Counters are recorded per stage and totalled. A counter that cannot be
observed stays null with a reason (stages before load, calls without an
attention mask or input ids, batched generation, forwards outside
activation collection, or embedding calls that bypass the watched
model). Changes are additive within profile schema version 1; default
baseline artifacts are unchanged, profiled or not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:35:46 -05:00

203 lines
13 KiB
Markdown

# Local baseline cost profiling
`concepts baseline` remains an observational workflow: explicit pack fetch and
hash verification precede model loading, then probing, distillation, the report
validation gate, direction serialization and cleanup. Profiling adds a separate
sidecar; it does not change the direction/validation contracts, extraction
algorithm, capture policy, seed or gate verdict. With profiling off (the default),
there is no profiler, resource sampling or extra output file.
For an already available local model and concept pack:
```bash
HF_HUB_OFFLINE=1 TRANSFORMERS_OFFLINE=1 OBLITERATUS_TELEMETRY=0 \
PYTHONPATH="$PWD" timeout 180 \
python -m obliteratus \
concepts baseline /absolute/path/to/local-pack /absolute/path/to/local-model \
--output-dir /tmp/baseline-profile-run-1 --cache-dir /tmp/baseline-profile-cache \
--seed 42 --device cpu --dtype float32 --profile
```
The pack must contain local data or have all required data in its selected cache.
Profiling never supplies a downloader or an accelerator. Baseline's explicit
fetch behavior is retained. The offline flags govern supported Hugging Face
clients; they are not a general network sandbox for pack URLs. Use the reference
workload below when no local model/data are available.
The sidecar defaults to `OUTPUT_DIR/cost-profile.json`. `--profile-output PATH`
also enables profiling and selects another destination. Reserved baseline
artifact paths are rejected. The ordinary CLI result remains `mode: report`.
The API is `run_baseline(..., profile=True, profile_output=None)`. Tests can supply
a single-use `LocalCostProfile(clock=..., resources=...)` as `profile`.
## Version 1 contract and interpretation
`schema_version` is integer `1`. `encode_profile` rejects other versions and
NaN/Infinity anywhere in the document before writing bytes. Unknown measurements
are JSON `null`, never invented zeros. Report keys are additive within version 1;
a change in units, scope or existing field meaning requires a new version.
| Field | Meaning and scope |
| --- | --- |
| `workflow` | `concept_baseline` |
| `status`, `error_type` | Invocation success/failure and exception class only; gate verdict is in `validation.json` |
| `stages` | Ordered rows: `name`, `status`, `error_type`, `wall_seconds`; baseline rows add per-stage `counters` and `counter_reasons` |
| `wall_seconds` | Monotonic host wall seconds, from profiler entry through cleanup and final resource sampling |
| `cpu_seconds` | Difference of `RUSAGE_SELF` user/system CPU seconds, all threads, excluding children |
| `resources_before`, `resources_after` | Process CPU seconds and lifetime peak RSS in bytes at each boundary |
| `profiling_overhead` | Measured bookkeeping wall seconds, total overhead null, explicit exclusions |
| `runtime` | Python, OS, machine/processor when available, logical CPUs, installed package versions, tool version/commit/dirty state, offline environment requests |
| `identities` | SHA-256 of supplied model/pack identifiers; verified pack content hash and recipe hash after resolution; digest of resolved model revision when available |
| `settings` | Seed, sequence limit, extraction layer and snapshot settings; device/dtype request digests |
| `cache` | Model/data reuse null: this runner has no authoritative hit/miss telemetry |
| `counters` | Unique examples, token presentations, forwards, generated tokens, tensor bytes over the observed scope; null where not observable |
| `counter_reasons` | Per counter: null when the value is observed, otherwise a short reason string |
| `counter_scope` | Text stating what each counter measures; null when no observer was attached |
| `gpu_seconds`, `dollar_cost`, `energy_joules` | Null: not measured or inferred |
Stages are `resolve_fetch`, `setup`, `load`, `probe`, `distill`, `gate`, `save`,
`cleanup`, included only when entered. Resolve/fetch includes target construction;
load includes the existing summon operation. Stage admission belongs to its
stage. Legacy direction construction belongs to distillation. Gate may itself
serialize validation; save covers direction artifacts. Tokenization, forwards,
capture, transfers, fold refits, null comparisons and scoring remain aggregated
inside existing stages; their timings are not independently measured.
Fewer captured layers must not be interpreted as fewer forwards.
## Work counters
Profiled baseline runs fill `counters` from pass-through observers installed on
the loaded model after `load` completes and removed after `cleanup`, including
when a stage fails. A forward pre-hook on the top-level model returns no
replacement inputs, and instance-level pass-throughs around `model.generate`
and the pipeline's activation collection return the original results, so model
inputs, outputs, weights and baseline artifacts are unchanged. With profiling
off no observer exists. Token identities are reduced to in-memory digests for
distinct counting; neither tokens nor digests are written to the profile.
| Counter | Measurement |
| --- | --- |
| `forwards` | Calls of the loaded top-level model module, including generation steps |
| `token_presentations` | Sum of `attention_mask` over those calls; null for calls without a mask or during cached generation decoding |
| `unique_examples` | Distinct non-padding input token sequences (prompt rows for `generate`); truncation can merge distinct prompt texts |
| `generated_tokens` | New positions returned by single-row `model.generate` calls; zero when no `generate` call was observed in the stage; null for batched or embedding-only generation |
| `tensor_bytes` | Logical element bytes (`numel * element_size`) of activation rows and auxiliary readouts returned to the host by activation collection; storage sharing and transient device tensors are not reflected; null in a stage with forwards outside activation collection |
Each stage row carries its own `counters` and `counter_reasons`. `resolve_fetch`,
`setup` and `load` precede the observer and stay null with a reason, so top-level
totals cover load completion through cleanup, as stated in `counter_scope`. A
total is null, with the reason of the first unknown stage, when any observed
stage left that counter unknown. If input-embedding calls in a stage differ from
top-level forwards (work bypassing the observed module), every counter in that
stage is null. A loaded model that is not a Torch module gets no observer and
null counters with a reason. Reading attention masks and token rows adds host
synchronization on accelerators; this affects timings only.
On the offline reference workload the sidecar reports 16 forwards, 48 token
presentations, 8 unique examples, 0 generated tokens and 4096 tensor bytes, all
during `probe`. Profiles written before work counters were added (including the
dated 2026-11-04 baseline report) record these counters as null; that was the
contract at the time.
Wall timings are host measurements, not GPU execution time. On CPU they measure
elapsed wall time, not CPU utilization. On an accelerator they may include waits
or omit queued work because no synchronization is introduced. No accelerator
performance claims are established here. RSS is the process-lifetime high-water
mark (Linux `ru_maxrss` KiB multiplied by 1024; macOS already bytes), not a
stage peak or incremental allocation. Other platforms report RSS null when its
units are not known; absent `resource` support leaves all resource fields null.
End-to-end wall time includes setup, profiling initialization and cleanup. It
excludes profiler construction/import before invocation, final JSON encoding
and sidecar I/O, process startup and CLI parsing. Stage sums can be smaller than
the end-to-end measurement. Measured bookkeeping overhead includes initialization,
post-stage recording and final resource accounting. It excludes clock-call cost,
context dispatch and final sidecar writing, so total overhead remains null.
An unprofiled-versus-profiled repeated benchmark is needed to estimate the total
perturbation; subtracting stage sums is not that benchmark.
Failures before loading still produce a sidecar if its destination is writable.
Entered stages record failure; cleanup is attempted after pipeline construction,
including on load failure. If cleanup also fails, both failed stages remain and
the propagated cleanup exception is the top-level error (existing runner
semantics). An unwritable sidecar raises on otherwise successful runs. During a
workflow failure it emits a payload-free warning and preserves the workflow
exception. Hard termination cannot guarantee cleanup or a sidecar.
No prompts, token sequences, environment dump, exception messages/tracebacks,
URLs, credentials or raw local model paths are stored in the profile. Identifier
digests support equality checks; they are not anonymization or proof that local
weights are byte-identical. Data/recipe hashes identify inputs without including
recipe prompt suffixes. The profiler does not add model-weight hashing. Device
and dtype digests can be reproduced with `identity_digest("cpu")` and
`identity_digest("float32")`. An operator can keep a separate approved mapping.
## Reproducible offline reference workload
From a checkout with the project development environment activated:
```bash
HF_HUB_OFFLINE=1 TRANSFORMERS_OFFLINE=1 OBLITERATUS_TELEMETRY=0 \
PYTHONPATH="$PWD" timeout 180 \
python -m pytest \
tests/test_analysis_profiling.py::test_offline_reference_baseline_profile \
--basetemp="$(mktemp -d /tmp/obliteratus-profile-reference.XXXXXX)" --no-cov -q
```
The test creates a deterministic random-initialized one-layer GPT-2 and local
synthetic contrast data, supplies a fetcher returning only those local bytes,
uses one Torch CPU thread, runs the actual baseline and validates its profile.
Find `cost-profile.json` under the selected basetemp's test directory. Workload
and semantic assertions are reproducible; measured time/RSS are not expected to
be identical. Fixture/model construction occurs before the measured baseline.
This tiny underpowered fixture establishes wiring and accounting mechanics,
not pretrained-model efficacy, deployment costs, or representative bottlenecks.
The existing baseline tests separately establish unchanged weights and the
exact unprofiled artifact set. For a real cold/warm comparison, use separately
recorded operator-controlled cache conditions and distinct output directories;
this profile cannot certify cache state. Targeting-workflow profiling, actual
cold/warm benchmark evidence, detailed substage timing instrumentation, measured
power/dollars, and reuse safety studies remain outside this package. Reuse must
respect partition, normalization, layer/site, tokenizer, adapter and algorithm
identity; the profile hashes alone do not authorize reuse.
## Local implementation decision: schema registration
This persistent sidecar needs a governed registry/schema entry before it is
advertised as a registered interchange format. This bounded change keeps the
version and finite-number enforcement in `analysis/profiling.py` and records the
field meanings here, without modifying policy/config or an unowned registry.
No schema governance completion or registry validation is claimed. The local contract is owned by the analysis maintainers; its producer is
`run_baseline`, and its consumers are local inspection and the offline replay
example. This document and `encode_profile` define version 1. The compatibility
fixtures are `tests/test_analysis_profiling.py`. A registry is deferred because
this is a local sidecar rather than a registered interchange format. Checks
in `tests/test_analysis_profiling.py` establish version rejection, nonfinite
rejection, injected timing/resources, unknown values, privacy, stage/failure and
cleanup accounting, API/CLI propagation and the offline reference workload.
Checks in `tests/test_analysis_work_counters.py` establish hand-computed counter
values on the offline reference workload, null-with-reason unknowns, unchanged
baseline artifacts with profiling on, and observer removal after an injected failure.
They do not establish all O01 roadmap acceptance criteria.
## Repeated CPU baseline and overhead replay
With the development environment activated, run the following from the repository
root. The destination must be new; the example creates synthetic weights and local
data without downloading a model.
```bash
HF_HUB_OFFLINE=1 TRANSFORMERS_OFFLINE=1 OBLITERATUS_TELEMETRY=0 \
PYTHONPATH="$PWD" python examples/analysis_first_baseline.py \
--output-dir /tmp/analysis-first-baseline --repetitions 4 --child-timeout 180
```
`report.json` records fixture hashes, configuration, first-process and subsequent
workflow timings, process wall costs, stage/resource profiles and numerical output
equality. The first call is not a certified cold-cache condition: OS caches are
not flushed. Each later call reloads the local model. Plain runs precede profiled
runs and share a data cache, which may favor the profiled arm. Their timing ratio
is descriptive and includes noise; it is not an isolated estimate of profiler
overhead. CPU fixtures establish software contracts only.