invarlock 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. invarlock/__init__.py +33 -0
  2. invarlock/__main__.py +10 -0
  3. invarlock/_data/runtime/profiles/ci_cpu.yaml +15 -0
  4. invarlock/_data/runtime/profiles/release.yaml +23 -0
  5. invarlock/_data/runtime/tiers.yaml +76 -0
  6. invarlock/adapters/__init__.py +102 -0
  7. invarlock/adapters/_capabilities.py +45 -0
  8. invarlock/adapters/auto.py +99 -0
  9. invarlock/adapters/base.py +530 -0
  10. invarlock/adapters/base_types.py +85 -0
  11. invarlock/adapters/hf_bert.py +852 -0
  12. invarlock/adapters/hf_gpt2.py +403 -0
  13. invarlock/adapters/hf_llama.py +485 -0
  14. invarlock/adapters/hf_mixin.py +383 -0
  15. invarlock/adapters/hf_onnx.py +112 -0
  16. invarlock/adapters/hf_t5.py +137 -0
  17. invarlock/adapters/py.typed +1 -0
  18. invarlock/assurance/__init__.py +43 -0
  19. invarlock/cli/__init__.py +8 -0
  20. invarlock/cli/__main__.py +8 -0
  21. invarlock/cli/_evidence.py +25 -0
  22. invarlock/cli/_json.py +75 -0
  23. invarlock/cli/adapter_auto.py +162 -0
  24. invarlock/cli/app.py +287 -0
  25. invarlock/cli/commands/__init__.py +26 -0
  26. invarlock/cli/commands/certify.py +403 -0
  27. invarlock/cli/commands/doctor.py +1358 -0
  28. invarlock/cli/commands/explain_gates.py +151 -0
  29. invarlock/cli/commands/export_html.py +100 -0
  30. invarlock/cli/commands/plugins.py +1331 -0
  31. invarlock/cli/commands/report.py +354 -0
  32. invarlock/cli/commands/run.py +4146 -0
  33. invarlock/cli/commands/verify.py +1040 -0
  34. invarlock/cli/config.py +396 -0
  35. invarlock/cli/constants.py +68 -0
  36. invarlock/cli/device.py +92 -0
  37. invarlock/cli/doctor_helpers.py +74 -0
  38. invarlock/cli/errors.py +6 -0
  39. invarlock/cli/overhead_utils.py +60 -0
  40. invarlock/cli/provenance.py +66 -0
  41. invarlock/cli/utils.py +41 -0
  42. invarlock/config.py +56 -0
  43. invarlock/core/__init__.py +62 -0
  44. invarlock/core/abi.py +15 -0
  45. invarlock/core/api.py +274 -0
  46. invarlock/core/auto_tuning.py +317 -0
  47. invarlock/core/bootstrap.py +226 -0
  48. invarlock/core/checkpoint.py +221 -0
  49. invarlock/core/contracts.py +73 -0
  50. invarlock/core/error_utils.py +64 -0
  51. invarlock/core/events.py +298 -0
  52. invarlock/core/exceptions.py +95 -0
  53. invarlock/core/registry.py +481 -0
  54. invarlock/core/retry.py +146 -0
  55. invarlock/core/runner.py +2041 -0
  56. invarlock/core/types.py +154 -0
  57. invarlock/edits/__init__.py +12 -0
  58. invarlock/edits/_edit_utils.py +249 -0
  59. invarlock/edits/_external_utils.py +268 -0
  60. invarlock/edits/noop.py +47 -0
  61. invarlock/edits/py.typed +1 -0
  62. invarlock/edits/quant_rtn.py +801 -0
  63. invarlock/edits/registry.py +166 -0
  64. invarlock/eval/__init__.py +23 -0
  65. invarlock/eval/bench.py +1207 -0
  66. invarlock/eval/bootstrap.py +50 -0
  67. invarlock/eval/data.py +2052 -0
  68. invarlock/eval/metrics.py +2167 -0
  69. invarlock/eval/primary_metric.py +767 -0
  70. invarlock/eval/probes/__init__.py +24 -0
  71. invarlock/eval/probes/fft.py +139 -0
  72. invarlock/eval/probes/mi.py +213 -0
  73. invarlock/eval/probes/post_attention.py +323 -0
  74. invarlock/eval/providers/base.py +67 -0
  75. invarlock/eval/providers/seq2seq.py +111 -0
  76. invarlock/eval/providers/text_lm.py +113 -0
  77. invarlock/eval/providers/vision_text.py +93 -0
  78. invarlock/eval/py.typed +1 -0
  79. invarlock/guards/__init__.py +18 -0
  80. invarlock/guards/_contracts.py +9 -0
  81. invarlock/guards/invariants.py +640 -0
  82. invarlock/guards/policies.py +805 -0
  83. invarlock/guards/py.typed +1 -0
  84. invarlock/guards/rmt.py +2097 -0
  85. invarlock/guards/spectral.py +1419 -0
  86. invarlock/guards/tier_config.py +354 -0
  87. invarlock/guards/variance.py +3298 -0
  88. invarlock/guards_ref/__init__.py +15 -0
  89. invarlock/guards_ref/rmt_ref.py +40 -0
  90. invarlock/guards_ref/spectral_ref.py +135 -0
  91. invarlock/guards_ref/variance_ref.py +60 -0
  92. invarlock/model_profile.py +353 -0
  93. invarlock/model_utils.py +221 -0
  94. invarlock/observability/__init__.py +10 -0
  95. invarlock/observability/alerting.py +535 -0
  96. invarlock/observability/core.py +546 -0
  97. invarlock/observability/exporters.py +565 -0
  98. invarlock/observability/health.py +588 -0
  99. invarlock/observability/metrics.py +457 -0
  100. invarlock/observability/py.typed +1 -0
  101. invarlock/observability/utils.py +553 -0
  102. invarlock/plugins/__init__.py +12 -0
  103. invarlock/plugins/hello_guard.py +33 -0
  104. invarlock/plugins/hf_awq_adapter.py +82 -0
  105. invarlock/plugins/hf_bnb_adapter.py +79 -0
  106. invarlock/plugins/hf_gptq_adapter.py +78 -0
  107. invarlock/plugins/py.typed +1 -0
  108. invarlock/py.typed +1 -0
  109. invarlock/reporting/__init__.py +7 -0
  110. invarlock/reporting/certificate.py +3221 -0
  111. invarlock/reporting/certificate_schema.py +244 -0
  112. invarlock/reporting/dataset_hashing.py +215 -0
  113. invarlock/reporting/guards_analysis.py +948 -0
  114. invarlock/reporting/html.py +32 -0
  115. invarlock/reporting/normalizer.py +235 -0
  116. invarlock/reporting/policy_utils.py +517 -0
  117. invarlock/reporting/primary_metric_utils.py +265 -0
  118. invarlock/reporting/render.py +1442 -0
  119. invarlock/reporting/report.py +903 -0
  120. invarlock/reporting/report_types.py +278 -0
  121. invarlock/reporting/utils.py +175 -0
  122. invarlock/reporting/validate.py +631 -0
  123. invarlock/security.py +176 -0
  124. invarlock/sparsity_utils.py +323 -0
  125. invarlock/utils/__init__.py +150 -0
  126. invarlock/utils/digest.py +45 -0
  127. invarlock-0.2.0.dist-info/METADATA +586 -0
  128. invarlock-0.2.0.dist-info/RECORD +132 -0
  129. invarlock-0.2.0.dist-info/WHEEL +5 -0
  130. invarlock-0.2.0.dist-info/entry_points.txt +20 -0
  131. invarlock-0.2.0.dist-info/licenses/LICENSE +201 -0
  132. invarlock-0.2.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,586 @@
1
+ Metadata-Version: 2.4
2
+ Name: invarlock
3
+ Version: 0.2.0
4
+ Summary: Edit‑agnostic robustness certificates for weight edits (InvarLock framework)
5
+ Author-email: InvarLock Team <oss@invarlock.dev>
6
+ Maintainer-email: InvarLock Maintainers <support@invarlock.dev>
7
+ License-Expression: Apache-2.0
8
+ Project-URL: Homepage, https://github.com/invarlock/invarlock
9
+ Project-URL: Repository, https://github.com/invarlock/invarlock
10
+ Project-URL: Documentation, https://github.com/invarlock/invarlock/tree/main/docs
11
+ Project-URL: Issues, https://github.com/invarlock/invarlock/issues
12
+ Project-URL: Changelog, https://github.com/invarlock/invarlock/blob/main/CHANGELOG.md
13
+ Keywords: machine-learning,deep-learning,transformers,pytorch,llm,quantization,safety,evaluation,certification
14
+ Classifier: Development Status :: 4 - Beta
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Intended Audience :: Science/Research
17
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Operating System :: OS Independent
22
+ Classifier: Typing :: Typed
23
+ Requires-Python: >=3.12
24
+ Description-Content-Type: text/markdown
25
+ License-File: LICENSE
26
+ Requires-Dist: typer>=0.15
27
+ Requires-Dist: click>=8.1
28
+ Requires-Dist: shellingham>=1.5.0
29
+ Requires-Dist: pandas>=2.2
30
+ Requires-Dist: scikit-learn>=1.4
31
+ Requires-Dist: pydantic>=2.0
32
+ Requires-Dist: rich>=13.0
33
+ Requires-Dist: pyyaml>=6.0
34
+ Requires-Dist: psutil>=5.9
35
+ Requires-Dist: hypothesis>=6.98
36
+ Requires-Dist: typing_extensions>=4.7
37
+ Requires-Dist: jsonschema>=4.0
38
+ Provides-Extra: adapters
39
+ Requires-Dist: torch>=2.1.0; extra == "adapters"
40
+ Requires-Dist: transformers>=4.53.0; extra == "adapters"
41
+ Provides-Extra: hf
42
+ Requires-Dist: torch>=2.1.0; extra == "hf"
43
+ Requires-Dist: transformers>=4.53.0; extra == "hf"
44
+ Requires-Dist: datasets>=3.0; extra == "hf"
45
+ Requires-Dist: numpy>=1.24; extra == "hf"
46
+ Requires-Dist: huggingface_hub>=0.23; extra == "hf"
47
+ Requires-Dist: aiohttp>=3.12.14; extra == "hf"
48
+ Requires-Dist: h2>=4.3.0; extra == "hf"
49
+ Requires-Dist: pillow>=11.3.0; extra == "hf"
50
+ Provides-Extra: guards
51
+ Requires-Dist: torch>=2.1.0; extra == "guards"
52
+ Requires-Dist: numpy>=1.24; extra == "guards"
53
+ Provides-Extra: edits
54
+ Requires-Dist: torch>=2.1.0; extra == "edits"
55
+ Provides-Extra: eval
56
+ Requires-Dist: torch>=2.1.0; extra == "eval"
57
+ Requires-Dist: datasets>=3.0; extra == "eval"
58
+ Provides-Extra: gptq
59
+ Requires-Dist: torch>=2.1.0; extra == "gptq"
60
+ Requires-Dist: auto-gptq>=0.7.0; platform_system == "Linux" and extra == "gptq"
61
+ Requires-Dist: triton>=2.3.0; platform_system == "Linux" and extra == "gptq"
62
+ Requires-Dist: transformers>=4.53.0; extra == "gptq"
63
+ Provides-Extra: awq
64
+ Requires-Dist: torch>=2.1.0; extra == "awq"
65
+ Requires-Dist: autoawq>=0.2.0; platform_system == "Linux" and extra == "awq"
66
+ Requires-Dist: transformers>=4.53.0; extra == "awq"
67
+ Requires-Dist: triton>=2.3.0; platform_system == "Linux" and extra == "awq"
68
+ Provides-Extra: gpu
69
+ Requires-Dist: torch>=2.1.0; extra == "gpu"
70
+ Requires-Dist: accelerate>=0.27; extra == "gpu"
71
+ Requires-Dist: bitsandbytes>=0.41; platform_system == "Linux" and extra == "gpu"
72
+ Provides-Extra: all
73
+ Requires-Dist: torch>=2.1.0; extra == "all"
74
+ Requires-Dist: transformers>=4.53.0; extra == "all"
75
+ Requires-Dist: datasets>=3.0; extra == "all"
76
+ Requires-Dist: numpy>=1.24; extra == "all"
77
+ Requires-Dist: huggingface_hub>=0.23; extra == "all"
78
+ Requires-Dist: accelerate>=0.27; extra == "all"
79
+ Requires-Dist: bitsandbytes>=0.41; platform_system == "Linux" and extra == "all"
80
+ Requires-Dist: auto-gptq>=0.7.0; platform_system == "Linux" and extra == "all"
81
+ Requires-Dist: autoawq>=0.2.0; platform_system == "Linux" and extra == "all"
82
+ Requires-Dist: triton>=2.3.0; platform_system == "Linux" and extra == "all"
83
+ Requires-Dist: aiohttp>=3.12.14; extra == "all"
84
+ Requires-Dist: h2>=4.3.0; extra == "all"
85
+ Requires-Dist: pillow>=11.3.0; extra == "all"
86
+ Provides-Extra: onnx
87
+ Requires-Dist: optimum>=1.17.0; extra == "onnx"
88
+ Requires-Dist: onnxruntime>=1.17.0; extra == "onnx"
89
+ Provides-Extra: dev
90
+ Requires-Dist: pytest>=7.0; extra == "dev"
91
+ Requires-Dist: pytest-cov>=4.0; extra == "dev"
92
+ Requires-Dist: ruff>=0.1.0; extra == "dev"
93
+ Requires-Dist: black>=23.0; extra == "dev"
94
+ Requires-Dist: mypy>=1.0; extra == "dev"
95
+ Requires-Dist: hypothesis>=6.98; extra == "dev"
96
+ Requires-Dist: pre-commit>=3.0; extra == "dev"
97
+ Requires-Dist: mkdocs>=1.5; extra == "dev"
98
+ Requires-Dist: mkdocs-material>=9.5; extra == "dev"
99
+ Requires-Dist: mkdocs-mermaid2-plugin>=1.1; extra == "dev"
100
+ Requires-Dist: sphinx>=7.0; extra == "dev"
101
+ Requires-Dist: matplotlib>=3.7; extra == "dev"
102
+ Requires-Dist: bitsandbytes>=0.41; extra == "dev"
103
+ Requires-Dist: build>=0.10.0; extra == "dev"
104
+ Requires-Dist: twine>=4.0.0; extra == "dev"
105
+ Dynamic: license-file
106
+
107
+ # InvarLock β€” Edit‑agnostic robustness certificates for weight edits
108
+
109
+ In short: certify that weight edits (e.g., quantization) preserve quality. If
110
+ they don’t, roll back safely.
111
+
112
+ Technical: edit‑agnostic guard pipeline (invariants β†’ spectral β†’ RMT β†’
113
+ variance) producing a machine‑readable Safety Certificate.
114
+
115
+ > **Status:** 0.2.0 (pre‑1.0). Until 1.0, **minor** releases may be
116
+ > breaking. See CLI help and the CHANGELOG for updates.
117
+
118
+ [![CI](https://img.shields.io/github/actions/workflow/status/invarlock/invarlock/ci.yml?branch=main&logo=github&label=CI)](https://github.com/invarlock/invarlock/actions/workflows/ci.yml)
119
+ [![PyPI](https://badge.fury.io/py/invarlock.svg)](https://pypi.org/project/invarlock/)
120
+ [![Docs](https://img.shields.io/badge/docs-quickstart-blue.svg)](https://github.com/invarlock/invarlock/blob/main/docs/user-guide/quickstart.md)
121
+ [![License: Apache-2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](LICENSE)
122
+ [![Python 3.12+](https://img.shields.io/badge/python-3.12+-blue.svg)](https://www.python.org/downloads/release/python-3120/)
123
+ ---
124
+
125
+ For guidance on where to ask questions, how to report bugs, and what to expect in terms of response times, see
126
+ [SUPPORT.md](https://github.com/invarlock/invarlock/blob/main/SUPPORT.md).
127
+
128
+ ## πŸš€ Quick start (no repo clone)
129
+
130
+ [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/invarlock/invarlock/blob/main/notebooks/invarlock_quickstart_cpu.ipynb)
131
+
132
+ ```bash
133
+ # Install with HF adapters
134
+ pip install "invarlock[hf]"
135
+
136
+ # Fast dev self‑cert on GPT‑2 small (tiny‑relax; downloads require explicit network)
137
+ INVARLOCK_ALLOW_NETWORK=1 INVARLOCK_DEDUP_TEXTS=1 INVARLOCK_TINY_RELAX=1 \
138
+ invarlock certify \
139
+ --baseline gpt2 \
140
+ --subject gpt2 \
141
+ --adapter auto \
142
+ --profile dev
143
+ ```
144
+
145
+ This produces `reports/.../evaluation.cert.json` with paired metrics
146
+ (ppl/accuracy), structural deltas, spectral/RMT stats, variance‑estimator
147
+ provenance, seeds/hashes, pairing metrics, and a policy digest.
148
+
149
+ > **Calibration note:** tier thresholds and window sizes are piloted on GPT‑2 small,
150
+ > BERT base, and TinyLLaMA (see `docs/assurance/09-tier-v1-calibration.md`). For
151
+ > calibrated Balanced/Conservative certs, use the preset‑based CI/Release examples
152
+ > below. `INVARLOCK_TINY_RELAX` dev runs relax sample‑size floors and are intended
153
+ > only for small smoke tests (not release evidence).
154
+
155
+ > Need presets or matrix scripts? Clone this repo and see Presets & Demos below.
156
+
157
+ ---
158
+
159
+ ## πŸ“š Docs & Guides
160
+
161
+ - Quickstart: <https://github.com/invarlock/invarlock/blob/main/docs/user-guide/quickstart.md>
162
+ - Compare & Certify (BYOE): <https://github.com/invarlock/invarlock/blob/main/docs/user-guide/compare-and-certify.md>
163
+ - Reading a Certificate: <https://github.com/invarlock/invarlock/blob/main/docs/user-guide/reading-certificate.md>
164
+ - CLI reference: <https://github.com/invarlock/invarlock/blob/main/docs/reference/cli.md>
165
+
166
+ Quick examples (repo presets, CPU; repo clone required for preset paths):
167
+
168
+ ```bash
169
+ # Install with HF adapters
170
+ pip install "invarlock[hf]"
171
+
172
+ # Preflight a config (JSON diagnostics)
173
+ invarlock doctor --config configs/tasks/causal_lm/ci_cpu.yaml --json
174
+
175
+ # Calibrated GPT‑2 small (recommended starting point; repo preset)
176
+ INVARLOCK_ALLOW_NETWORK=1 INVARLOCK_DEDUP_TEXTS=1 \
177
+ invarlock certify \
178
+ --baseline gpt2 \
179
+ --subject gpt2 \
180
+ --adapter auto \
181
+ --profile release \
182
+ --preset configs/tasks/causal_lm/release_auto.yaml
183
+
184
+ # Tiny causal LM smoke (out‑of‑calibration, dev‑only)
185
+ INVARLOCK_ALLOW_NETWORK=1 \
186
+ invarlock certify \
187
+ --baseline hf:sshleifer/tiny-gpt2 \
188
+ --subject hf:sshleifer/tiny-gpt2 \
189
+ --profile dev
190
+ ```
191
+
192
+ Notes:
193
+
194
+ - Presets and scripts live in this repo (`configs/`, `scripts/`) and are not
195
+ shipped in wheels. Use flag‑only `certify` when installing from PyPI, or clone
196
+ this repo to use presets and the matrix script.
197
+ - `python -m invarlock` works the same as `invarlock`.
198
+ - InvarLock runs offline by default; enable network per command with `INVARLOCK_ALLOW_NETWORK=1` when fetching.
199
+
200
+ ---
201
+
202
+ ## πŸ”§ Installation
203
+
204
+ ```bash
205
+ # Core + HF adapter
206
+ pip install "invarlock[hf]"
207
+
208
+ # GPU extras (CUDA wheels if available)
209
+ pip install "invarlock[gpu]"
210
+
211
+ # Optional edit backends
212
+ pip install "invarlock[awq,gptq]" # AWQ/GPTQ PTQ stacks
213
+ pip install "invarlock[dev]" # dev tooling (ruff, pytest, mkdocs)
214
+ ```
215
+
216
+ > Minimal core installs with `pip install invarlock`. The OSS core is edit‑agnostic
217
+ > (BYOE): supply baseline and subject checkpoints and run Compare & Certify. A
218
+ > small built‑in edit, `quant_rtn`, is provided for CI/quickstart demos only;
219
+ > optional extras (e.g., `gptq`, `awq`, `gpu`) are loaders/runtimes, not edit
220
+ > pipelines. Core installs do not pull in torch/transformers; those are only
221
+ > installed when you opt into extras such as `"invarlock[hf]"` or
222
+ > `"invarlock[adapters]"`.
223
+
224
+ Run either entry point:
225
+
226
+ ```bash
227
+ invarlock --help
228
+ python -m invarlock --help
229
+ ```
230
+
231
+ Common error (missing torch on adapter-based commands):
232
+
233
+ ```text
234
+ ❌ Torch is required for this command.
235
+ Install extras with: pip install "invarlock[hf]" or "invarlock[adapters]".
236
+ ```
237
+
238
+ If you see this, install an appropriate extra (for example, `pip install "invarlock[hf]"`)
239
+ before running `invarlock run` or `invarlock certify` with HF adapters.
240
+
241
+ ### Network Access
242
+
243
+ - Outbound network is disabled by default for safety. Enable it explicitly (per
244
+ command) when you need to download models or datasets:
245
+
246
+ ```bash
247
+ INVARLOCK_ALLOW_NETWORK=1 invarlock certify \
248
+ --baseline gpt2 \
249
+ --subject gpt2 \
250
+ --adapter auto \
251
+ --profile ci \
252
+ --preset configs/tasks/causal_lm/ci_cpu.yaml
253
+ ```
254
+
255
+ - Offline/air‑gapped usage: pre‑download to a cache, then run with network
256
+ disabled. You can enforce offline reads with `HF_DATASETS_OFFLINE=1` (and
257
+ optionally set `HF_HOME`/`HF_DATASETS_CACHE` to your cache location).
258
+
259
+ See the CLI reference and datasets guide for details:
260
+
261
+ - <https://github.com/invarlock/invarlock/blob/main/docs/reference/cli.md>
262
+ - <https://github.com/invarlock/invarlock/blob/main/docs/reference/datasets.md>
263
+
264
+ ### Install via pipx (isolated)
265
+
266
+ ```bash
267
+ # Ensure pipx uses Python 3.12+
268
+ pipx install --python python3.12 "invarlock[hf]" # Python 3.12+ recommended
269
+
270
+ # With GPU extras (if supported on your platform)
271
+ pipx install --python python3.12 "invarlock[hf,gpu]"
272
+ ```
273
+
274
+ ### Conda environment recipe
275
+
276
+ ```bash
277
+ conda create -n invarlock python=3.12 -y
278
+ conda activate invarlock
279
+
280
+ # Core + HF stack
281
+ pip install "invarlock[hf]"
282
+
283
+ # Optional extras
284
+ # pip install "invarlock[gpu]"
285
+ # pip install "invarlock[awq,gptq]"
286
+ ```
287
+
288
+ ---
289
+
290
+ ## πŸ’» Support Matrix
291
+
292
+ | Platform | Status | Notes |
293
+ | ---------------------- | --------------- | ------------------------------------------ |
294
+ | Python 3.12+ | βœ… Required | |
295
+ | Linux | βœ… Full | Primary dev target |
296
+ | macOS (Intel/M-series) | βœ… Full | MPS supported (default on AppleΒ Silicon) |
297
+ | Windows | ❌ Not supported | Use WSL2 or a Linux container if required |
298
+ | CUDA | βœ… Recommended | For larger models |
299
+ | CPU | βœ… Fallback | Slower but functional |
300
+
301
+ **Device selection:** CUDA β†’ MPS β†’ CPU (auto). Override with torch env if
302
+ needed (e.g., `CUDA_VISIBLE_DEVICES`).
303
+
304
+ ---
305
+
306
+ ## 🧱 What InvarLock Provides
307
+
308
+ - **Runner** (torch-agnostic core): `prepare β†’ preview β†’ apply β†’ guards β†’ evaluate β†’ report/rollback`
309
+
310
+ - **Built-in edit**:
311
+ - `quant_rtn` (INT8 RTN, per‑channel, clamp/group size)
312
+
313
+ - **Guards** (policy-tiered; β€œGuardChain” = ordered guard pipeline):
314
+
315
+ 1. **Invariants** (pre/post: shapes/finite/tying)
316
+ 2. **Spectral** (per-family z-caps; monitor or gate per tier)
317
+ 3. **RMT** (Ξ΅-band on outliers; monitor or gate per tier)
318
+ 4. **Variance (VE)** (predictive paired Ξ”logNLL gate; tiered sidedness)
319
+
320
+ - **Safety Certificate (schema v1, PM‑only)**: Primary Metric (ppl or
321
+ accuracy) with paired statistics, structural deltas, spectral/RMT stats, VE
322
+ provenance, seeds/hashes, pairing metrics, and **policy digest**. Canonical
323
+ artifact: `reports/.../evaluation.cert.json`.
324
+
325
+ **Scope (what InvarLock does / does not do):**
326
+
327
+ - InvarLock certifies **regression risk from weight edits** (e.g., quantization or
328
+ pruning) relative to a fixed baseline under a specific configuration.
329
+ - It focuses on **paired primary metrics** (ppl/accuracy) plus structural and
330
+ guard telemetry (invariants, spectral, RMT, variance) for those edits.
331
+ - It **does not** claim to solve content‑safety problems (toxicity, bias,
332
+ jailbreaks) or alignment in general, and it does not certify arbitrary
333
+ training changes or new datasets.
334
+ - It is calibrated and tested on Linux/macOS environments using the HF/PyTorch
335
+ stack described in the docs; native Windows is not supported.
336
+ - For the detailed assurance case and threat model, see
337
+ `docs/assurance/00-safety-case.md` and `docs/security/threat-model.md`.
338
+
339
+ Minimal excerpt (redacted):
340
+
341
+ ```json
342
+ {
343
+ "schema_version": "v1",
344
+ "run_id": "...",
345
+ "validation": {
346
+ "primary_metric_acceptable": true,
347
+ "guard_overhead_acceptable": true
348
+ },
349
+ "primary_metric": {
350
+ "kind": "ppl_causal",
351
+ "preview": 12.3,
352
+ "final": 12.1,
353
+ "ratio_vs_baseline": 0.98,
354
+ "display_ci": [0.97, 0.99]
355
+ },
356
+ "structure": {"layers_modified": 0, "params_changed": 0},
357
+ "spectral": {"caps_applied": 0},
358
+ "rmt": {"stable": true},
359
+ "auto": {"tier": "balanced"}
360
+ }
361
+ ```
362
+
363
+ ---
364
+
365
+ ## πŸ›‘οΈ Guard Order & Balanced Defaults
366
+
367
+ **Canonical order**: `["invariants", "spectral", "rmt", "variance", "invariants"]`
368
+
369
+ **Balanced profile (example)**
370
+
371
+ ```yaml
372
+ guards:
373
+ spectral:
374
+ mode: monitor
375
+ sigma_quantile: 0.95
376
+ deadband: 0.10
377
+ scope: all
378
+ max_caps: 5
379
+ max_spectral_norm: null # disable absolute clamp; rely on calibrated ΞΊ_f
380
+ multiple_testing: { method: bh, alpha: 0.05, m: 4 }
381
+ family_caps: { ffn: 2.5, attn: 2.8, embed: 3.0, other: 3.0 } # z-caps (FPR-derived)
382
+ rmt:
383
+ mode: monitor
384
+ epsilon_by_family: { ffn: 0.10, attn: 0.08, embed: 0.12, other: 0.12 }
385
+ variance:
386
+ tap: "post mlp.c_proj (pre-residual)"
387
+ targets: "edited_modules_only"
388
+ discovery:
389
+ deadband: 0.02
390
+ min_abs_adjust: 0.012
391
+ max_scale_step: 0.03
392
+ gating:
393
+ sided: "one-sided" # improvement-only
394
+ min_effect_lognll: 9e-4 # pilot-derived power threshold
395
+ ```
396
+
397
+ > **Conservative** raises z-caps/Ξ΅/deadband/min-effect and uses **two-sided** VE; **Aggressive** relaxes accordingly.
398
+
399
+ ---
400
+
401
+ > πŸ” For development and CI commands (pytest, mkdocs, generators), see CONTRIBUTING.md.
402
+
403
+ ---
404
+
405
+ ## βœ‚οΈ Edits & Plugins
406
+
407
+ - **Quant RTN** (built‑in): INT8 RTN, per‑channel, group size, percentile clamp
408
+ - **Compare & Certify (BYOE, recommended)**: Bring your baseline + subject checkpoints and certify with InvarLock
409
+ - **Plugins (optional)**: Adapters and guards via entry points. Adapters extend
410
+ model loading/inference (e.g., GPTQ/AWQ formats); plugins do not add edit
411
+ algorithms beyond RTN. List components with:
412
+
413
+ ```bash
414
+ invarlock plugins --help # summary
415
+ invarlock plugins guards # guard plugins
416
+ invarlock plugins edits # edit plugins
417
+ invarlock plugins adapters # adapters and backend hints
418
+ ```
419
+
420
+ ---
421
+
422
+ ## πŸ” Certification Criteria (balanced profile)
423
+
424
+ Key checks enforced by balanced policy (summary):
425
+
426
+ - **Pairing invariants**: preview = final counts; `match=1.00`, `overlap=0.00` (fail-fast in CI/Release)
427
+ - **PM ratio gate** (ppl or accuracy): upper CI ≀ **1.10**
428
+ - **Drift**: 0.95–1.05 (paired log-space)
429
+ - **Spectral/RMT**: within tier FPR/Ξ΅ band
430
+ - **Catastrophe rollback**: automatic revert if PPL > **2.0Γ—**
431
+ - **Guard overhead**: a bare-vs-guarded comparison records `validation.guard_overhead_acceptable=true` when ≀ 1β€―% PPL overhead
432
+
433
+
434
+ ---
435
+
436
+ ## 🧾 Minimal Config (balanced GPT-2, CI profile)
437
+
438
+ ```yaml
439
+ model:
440
+ id: "<set-your-model-id>" # e.g., gpt2
441
+ adapter: "hf_gpt2"
442
+ device: "cpu"
443
+ dataset:
444
+ provider: "wikitext2"
445
+ split: "validation"
446
+ seq_len: 512
447
+ stride: 512
448
+ preview_n: 64
449
+ final_n: 64
450
+ seed: 42
451
+ edit:
452
+ # Optional: built-in quant demo. Omit for Compare & Certify/BYOE.
453
+ name: quant_rtn
454
+ plan:
455
+ bitwidth: 8
456
+ per_channel: true
457
+ scope: attn
458
+ eval:
459
+ metric:
460
+ kind: ppl_causal
461
+ loss:
462
+ type: causal
463
+ guards:
464
+ order: [invariants, spectral, rmt, variance, invariants]
465
+ spectral: { mode: monitor }
466
+ rmt: { mode: monitor }
467
+ variance:
468
+ tap: "post mlp.c_proj (pre-residual)"
469
+ targets: "edited_modules_only"
470
+ discovery: { deadband: 0.02, min_abs_adjust: 0.012, max_scale_step: 0.03 }
471
+ gating: { sided: one-sided, min_effect_lognll: 9e-4 }
472
+ auto:
473
+ enabled: true
474
+ tier: balanced
475
+ probes: 0
476
+ output:
477
+ dir: runs
478
+ save_model: false
479
+ save_report: true
480
+ ```
481
+
482
+ ---
483
+
484
+ ## 🩺 Doctor (preflight)
485
+
486
+ Run preflight checks before a run to catch misconfigurations early:
487
+
488
+ ```bash
489
+ invarlock doctor --config configs/tasks/causal_lm/ci_cpu.yaml --json
490
+ ```
491
+
492
+ Text mode emits lines prefixed with `ERROR:`, `WARNING:`, or `NOTE:` and stable
493
+ codes like `[INVARLOCK:D001]`. JSON mode includes `summary`, `policy`,
494
+ `findings[]`, `resolution`, and `format_version`.
495
+
496
+ ---
497
+
498
+ ## πŸ—οΈ Source Layout (Single Distribution)
499
+
500
+ ```text
501
+ invarlock/
502
+ β”œβ”€ src/
503
+ β”‚ β”œβ”€ invarlock/ # core + unified namespace
504
+ β”‚ β”‚ β”œβ”€ core/ # runner, registry, contracts, events, ABI
505
+ β”‚ β”‚ β”œβ”€ cli/ # console app + command wrappers (unified import path)
506
+ β”‚ β”‚ β”œβ”€ adapters/ # adapter wrappers (HF GPT‑2/BERT/LLaMA)
507
+ β”‚ β”‚ β”œβ”€ edits/ # quant_rtn
508
+ β”‚ β”‚ β”œβ”€ guards/ # invariants, spectral, rmt, variance
509
+ β”‚ β”‚ β”œβ”€ eval/ # evaluation metrics and helpers
510
+ β”‚ β”‚ β”œβ”€ reporting/ # report assembly, certificate generation/validation
511
+ β”‚ β”‚ β”œβ”€ assurance/ # assurance surface aggregating cert helpers
512
+ β”‚ β”‚ β”œβ”€ plugins/ # built-in example plugins
513
+ β”‚ β”‚ └─ observability/ # monitoring/metrics/tracing wrappers
514
+ β”œβ”€ configs/ # presets (repo‑only; clone to use)
515
+ β”œβ”€ docs/ # user guides, reference, assurance notes
516
+ β”œβ”€ scripts/ # automation / QA helpers
517
+ └─ tests/ # unit/integration/property tests
518
+
519
+ Note: The package exposes a single import namespace (`invarlock.*`). Presets/scripts are repo resources and not packaged in wheels.
520
+ ```
521
+
522
+ ---
523
+
524
+ ## πŸ“š Documentation
525
+
526
+ - User Guide: <https://github.com/invarlock/invarlock/blob/main/docs/user-guide/getting-started.md>
527
+ - Quickstart: <https://github.com/invarlock/invarlock/blob/main/docs/user-guide/quickstart.md>
528
+ - Compare & Certify (BYOE): <https://github.com/invarlock/invarlock/blob/main/docs/user-guide/compare-and-certify.md>
529
+ - Reading a Certificate: <https://github.com/invarlock/invarlock/blob/main/docs/user-guide/reading-certificate.md>
530
+ - Assurance (proof notes): <https://github.com/invarlock/invarlock/tree/main/docs/assurance>
531
+ - eval math, spectral FPR, RMT Ξ΅, VE gate power, determinism
532
+ - Config Schema: <https://github.com/invarlock/invarlock/blob/main/docs/reference/config-schema.md>
533
+ - Guard Reference: <https://github.com/invarlock/invarlock/blob/main/docs/reference/guards.md>
534
+
535
+ ---
536
+
537
+ ## ⚑ Quick CPU Demos (dev)
538
+
539
+ For tiny, CPU‑only demos that produce readable PASS banners in dev, enable
540
+ tiny‑relax and run the matrix script (repo clone required). This mode relaxes
541
+ primary‑metric token floors and is intended for smoke testing only (not release
542
+ evidence):
543
+
544
+ ```bash
545
+ export INVARLOCK_TINY_RELAX=1 INVARLOCK_ALLOW_NETWORK=1 INVARLOCK_DEDUP_TEXTS=1 \
546
+ TRANSFORMERS_NO_TORCHVISION=1 TOKENIZERS_PARALLELISM=false
547
+ RUN=1 NET=1 bash scripts/run_tiny_all_matrix.sh
548
+ ```
549
+
550
+ Add `INCLUDE_MEASURED_CLS=1` to include a measured classification step (requires warmed HF caches/network).
551
+
552
+ ---
553
+
554
+ ## πŸ§ͺ Determinism & Provenance
555
+
556
+ - Seeds: `{python, numpy, torch}` recorded in certs
557
+ - Dataset/tokenizer hashes recorded
558
+ - Paired non-overlapping windows (fail-fast if counts mismatch or pairing < 1.0)
559
+ - Cert math checks: `ppl_ratio.point == exp(mean Ξ”logNLL)` and CI from the **same** paired Ξ” array
560
+
561
+ ---
562
+
563
+ ## 🀝 Contributing
564
+
565
+ ```bash
566
+ make dev-install # editable + dev tools (pytest, ruff, mypy, mkdocs, etc.)
567
+ make test # run tests
568
+ make lint # ruff + mypy
569
+ make format # ruff format/fix
570
+ make docs # build docs (mkdocs)
571
+ make verify # tests, lint, format, markdownlint
572
+ ```
573
+
574
+ Please see `CONTRIBUTING.md` for guidelines and `Makefile` for more targets.
575
+
576
+ ---
577
+
578
+ ## πŸ“„ License
579
+
580
+ Apache-2.0 β€” see `LICENSE`.
581
+
582
+ ---
583
+
584
+ ### Notes
585
+
586
+ - PPL levels depend on `seq_len` (e.g., 768-token windows typically reduce PPL vs shorter contexts).