Files
OBLITERATUS/docs/analysis-profiling.md
T

163 lines
9.9 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` |
| `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: null |
| `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; they are not independently measured by this first package.
Fewer captured layers must not be interpreted as fewer forwards.
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/counter 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.
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.