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.
- invarlock/__init__.py +33 -0
- invarlock/__main__.py +10 -0
- invarlock/_data/runtime/profiles/ci_cpu.yaml +15 -0
- invarlock/_data/runtime/profiles/release.yaml +23 -0
- invarlock/_data/runtime/tiers.yaml +76 -0
- invarlock/adapters/__init__.py +102 -0
- invarlock/adapters/_capabilities.py +45 -0
- invarlock/adapters/auto.py +99 -0
- invarlock/adapters/base.py +530 -0
- invarlock/adapters/base_types.py +85 -0
- invarlock/adapters/hf_bert.py +852 -0
- invarlock/adapters/hf_gpt2.py +403 -0
- invarlock/adapters/hf_llama.py +485 -0
- invarlock/adapters/hf_mixin.py +383 -0
- invarlock/adapters/hf_onnx.py +112 -0
- invarlock/adapters/hf_t5.py +137 -0
- invarlock/adapters/py.typed +1 -0
- invarlock/assurance/__init__.py +43 -0
- invarlock/cli/__init__.py +8 -0
- invarlock/cli/__main__.py +8 -0
- invarlock/cli/_evidence.py +25 -0
- invarlock/cli/_json.py +75 -0
- invarlock/cli/adapter_auto.py +162 -0
- invarlock/cli/app.py +287 -0
- invarlock/cli/commands/__init__.py +26 -0
- invarlock/cli/commands/certify.py +403 -0
- invarlock/cli/commands/doctor.py +1358 -0
- invarlock/cli/commands/explain_gates.py +151 -0
- invarlock/cli/commands/export_html.py +100 -0
- invarlock/cli/commands/plugins.py +1331 -0
- invarlock/cli/commands/report.py +354 -0
- invarlock/cli/commands/run.py +4146 -0
- invarlock/cli/commands/verify.py +1040 -0
- invarlock/cli/config.py +396 -0
- invarlock/cli/constants.py +68 -0
- invarlock/cli/device.py +92 -0
- invarlock/cli/doctor_helpers.py +74 -0
- invarlock/cli/errors.py +6 -0
- invarlock/cli/overhead_utils.py +60 -0
- invarlock/cli/provenance.py +66 -0
- invarlock/cli/utils.py +41 -0
- invarlock/config.py +56 -0
- invarlock/core/__init__.py +62 -0
- invarlock/core/abi.py +15 -0
- invarlock/core/api.py +274 -0
- invarlock/core/auto_tuning.py +317 -0
- invarlock/core/bootstrap.py +226 -0
- invarlock/core/checkpoint.py +221 -0
- invarlock/core/contracts.py +73 -0
- invarlock/core/error_utils.py +64 -0
- invarlock/core/events.py +298 -0
- invarlock/core/exceptions.py +95 -0
- invarlock/core/registry.py +481 -0
- invarlock/core/retry.py +146 -0
- invarlock/core/runner.py +2041 -0
- invarlock/core/types.py +154 -0
- invarlock/edits/__init__.py +12 -0
- invarlock/edits/_edit_utils.py +249 -0
- invarlock/edits/_external_utils.py +268 -0
- invarlock/edits/noop.py +47 -0
- invarlock/edits/py.typed +1 -0
- invarlock/edits/quant_rtn.py +801 -0
- invarlock/edits/registry.py +166 -0
- invarlock/eval/__init__.py +23 -0
- invarlock/eval/bench.py +1207 -0
- invarlock/eval/bootstrap.py +50 -0
- invarlock/eval/data.py +2052 -0
- invarlock/eval/metrics.py +2167 -0
- invarlock/eval/primary_metric.py +767 -0
- invarlock/eval/probes/__init__.py +24 -0
- invarlock/eval/probes/fft.py +139 -0
- invarlock/eval/probes/mi.py +213 -0
- invarlock/eval/probes/post_attention.py +323 -0
- invarlock/eval/providers/base.py +67 -0
- invarlock/eval/providers/seq2seq.py +111 -0
- invarlock/eval/providers/text_lm.py +113 -0
- invarlock/eval/providers/vision_text.py +93 -0
- invarlock/eval/py.typed +1 -0
- invarlock/guards/__init__.py +18 -0
- invarlock/guards/_contracts.py +9 -0
- invarlock/guards/invariants.py +640 -0
- invarlock/guards/policies.py +805 -0
- invarlock/guards/py.typed +1 -0
- invarlock/guards/rmt.py +2097 -0
- invarlock/guards/spectral.py +1419 -0
- invarlock/guards/tier_config.py +354 -0
- invarlock/guards/variance.py +3298 -0
- invarlock/guards_ref/__init__.py +15 -0
- invarlock/guards_ref/rmt_ref.py +40 -0
- invarlock/guards_ref/spectral_ref.py +135 -0
- invarlock/guards_ref/variance_ref.py +60 -0
- invarlock/model_profile.py +353 -0
- invarlock/model_utils.py +221 -0
- invarlock/observability/__init__.py +10 -0
- invarlock/observability/alerting.py +535 -0
- invarlock/observability/core.py +546 -0
- invarlock/observability/exporters.py +565 -0
- invarlock/observability/health.py +588 -0
- invarlock/observability/metrics.py +457 -0
- invarlock/observability/py.typed +1 -0
- invarlock/observability/utils.py +553 -0
- invarlock/plugins/__init__.py +12 -0
- invarlock/plugins/hello_guard.py +33 -0
- invarlock/plugins/hf_awq_adapter.py +82 -0
- invarlock/plugins/hf_bnb_adapter.py +79 -0
- invarlock/plugins/hf_gptq_adapter.py +78 -0
- invarlock/plugins/py.typed +1 -0
- invarlock/py.typed +1 -0
- invarlock/reporting/__init__.py +7 -0
- invarlock/reporting/certificate.py +3221 -0
- invarlock/reporting/certificate_schema.py +244 -0
- invarlock/reporting/dataset_hashing.py +215 -0
- invarlock/reporting/guards_analysis.py +948 -0
- invarlock/reporting/html.py +32 -0
- invarlock/reporting/normalizer.py +235 -0
- invarlock/reporting/policy_utils.py +517 -0
- invarlock/reporting/primary_metric_utils.py +265 -0
- invarlock/reporting/render.py +1442 -0
- invarlock/reporting/report.py +903 -0
- invarlock/reporting/report_types.py +278 -0
- invarlock/reporting/utils.py +175 -0
- invarlock/reporting/validate.py +631 -0
- invarlock/security.py +176 -0
- invarlock/sparsity_utils.py +323 -0
- invarlock/utils/__init__.py +150 -0
- invarlock/utils/digest.py +45 -0
- invarlock-0.2.0.dist-info/METADATA +586 -0
- invarlock-0.2.0.dist-info/RECORD +132 -0
- invarlock-0.2.0.dist-info/WHEEL +5 -0
- invarlock-0.2.0.dist-info/entry_points.txt +20 -0
- invarlock-0.2.0.dist-info/licenses/LICENSE +201 -0
- invarlock-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""
|
|
2
|
+
HuggingFace BitsAndBytes Adapter (plugin)
|
|
3
|
+
=========================================
|
|
4
|
+
|
|
5
|
+
Optional adapter for loading 4/8-bit quantized causal LMs via bitsandbytes
|
|
6
|
+
through Transformers. Requires GPU for practical use.
|
|
7
|
+
Install with the `gpu` extra on supported platforms.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
from invarlock.adapters.hf_mixin import HFAdapterMixin
|
|
15
|
+
from invarlock.core.api import ModelAdapter
|
|
16
|
+
from invarlock.core.error_utils import wrap_errors
|
|
17
|
+
from invarlock.core.exceptions import DependencyError, ModelLoadError
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class HF_BNB_Adapter(HFAdapterMixin, ModelAdapter):
|
|
21
|
+
name = "hf_bnb"
|
|
22
|
+
|
|
23
|
+
def load_model(self, model_id: str, device: str = "auto", **kwargs: Any):
|
|
24
|
+
with wrap_errors(
|
|
25
|
+
DependencyError,
|
|
26
|
+
"E203",
|
|
27
|
+
"DEPENDENCY-MISSING: transformers",
|
|
28
|
+
lambda e: {"dependency": "transformers"},
|
|
29
|
+
):
|
|
30
|
+
from transformers import AutoModelForCausalLM
|
|
31
|
+
|
|
32
|
+
# Default to 8-bit if not specified
|
|
33
|
+
load_in_8bit = bool(kwargs.pop("load_in_8bit", True))
|
|
34
|
+
load_in_4bit = bool(kwargs.pop("load_in_4bit", False))
|
|
35
|
+
|
|
36
|
+
if load_in_4bit:
|
|
37
|
+
load_in_8bit = False
|
|
38
|
+
|
|
39
|
+
with wrap_errors(
|
|
40
|
+
ModelLoadError,
|
|
41
|
+
"E201",
|
|
42
|
+
"MODEL-LOAD-FAILED: bitsandbytes/transformers",
|
|
43
|
+
lambda e: {"model_id": model_id},
|
|
44
|
+
):
|
|
45
|
+
model = AutoModelForCausalLM.from_pretrained(
|
|
46
|
+
model_id,
|
|
47
|
+
device_map="auto",
|
|
48
|
+
load_in_8bit=load_in_8bit,
|
|
49
|
+
load_in_4bit=load_in_4bit,
|
|
50
|
+
trust_remote_code=True,
|
|
51
|
+
**kwargs,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
# Let HF device map place on CUDA if available; ensure device object resolves
|
|
55
|
+
_ = self._resolve_device(device)
|
|
56
|
+
return model
|
|
57
|
+
|
|
58
|
+
def can_handle(self, model: Any) -> bool:
|
|
59
|
+
cfg = getattr(model, "config", None)
|
|
60
|
+
return hasattr(cfg, "n_layer") or hasattr(cfg, "num_hidden_layers")
|
|
61
|
+
|
|
62
|
+
def describe(self, model: Any) -> dict[str, Any]:
|
|
63
|
+
cfg = getattr(model, "config", None)
|
|
64
|
+
n_layer = int(
|
|
65
|
+
getattr(cfg, "n_layer", getattr(cfg, "num_hidden_layers", 0)) or 0
|
|
66
|
+
)
|
|
67
|
+
n_head = int(
|
|
68
|
+
getattr(cfg, "n_head", getattr(cfg, "num_attention_heads", 0)) or 0
|
|
69
|
+
)
|
|
70
|
+
heads = [n_head] * n_layer if n_layer and n_head else []
|
|
71
|
+
return {
|
|
72
|
+
"n_layer": n_layer,
|
|
73
|
+
"heads_per_layer": heads,
|
|
74
|
+
"mlp_dims": [],
|
|
75
|
+
"tying": {},
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
__all__ = ["HF_BNB_Adapter"]
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""
|
|
2
|
+
HuggingFace GPTQ Adapter (plugin)
|
|
3
|
+
=================================
|
|
4
|
+
|
|
5
|
+
Optional adapter for loading AutoGPTQ-quantized causal LMs from the Hub.
|
|
6
|
+
Requires the `auto-gptq` extra on supported platforms (typically Linux/CUDA).
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from invarlock.adapters.hf_mixin import HFAdapterMixin
|
|
14
|
+
from invarlock.core.api import ModelAdapter
|
|
15
|
+
from invarlock.core.error_utils import wrap_errors
|
|
16
|
+
from invarlock.core.exceptions import DependencyError, ModelLoadError
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class HF_GPTQ_Adapter(HFAdapterMixin, ModelAdapter):
|
|
20
|
+
name = "hf_gptq"
|
|
21
|
+
|
|
22
|
+
# ---- Lifecycle ----
|
|
23
|
+
def load_model(self, model_id: str, device: str = "auto", **kwargs: Any):
|
|
24
|
+
with wrap_errors(
|
|
25
|
+
DependencyError,
|
|
26
|
+
"E203",
|
|
27
|
+
"DEPENDENCY-MISSING: auto_gptq/transformers",
|
|
28
|
+
lambda e: {"dependency": "auto_gptq"},
|
|
29
|
+
):
|
|
30
|
+
try:
|
|
31
|
+
from auto_gptq import AutoGPTQForCausalLM
|
|
32
|
+
except Exception: # pragma: no cover - import path variations
|
|
33
|
+
from transformers import (
|
|
34
|
+
AutoModelForCausalLM as AutoGPTQForCausalLM,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
with wrap_errors(
|
|
38
|
+
ModelLoadError,
|
|
39
|
+
"E201",
|
|
40
|
+
"MODEL-LOAD-FAILED: gptq",
|
|
41
|
+
lambda e: {"model_id": model_id},
|
|
42
|
+
):
|
|
43
|
+
model = AutoGPTQForCausalLM.from_quantized(
|
|
44
|
+
model_id,
|
|
45
|
+
trust_remote_code=True,
|
|
46
|
+
inject_fused_attention=False,
|
|
47
|
+
**{k: v for k, v in kwargs.items() if k not in {"device"}},
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
return model.to(self._resolve_device(device))
|
|
51
|
+
|
|
52
|
+
# ---- Introspection ----
|
|
53
|
+
def can_handle(self, model: Any) -> bool:
|
|
54
|
+
# Heuristic: quantized causal LM typically exposes .config with hidden layers
|
|
55
|
+
cfg = getattr(model, "config", None)
|
|
56
|
+
return hasattr(cfg, "n_layer") or hasattr(cfg, "num_hidden_layers")
|
|
57
|
+
|
|
58
|
+
def describe(self, model: Any) -> dict[str, Any]:
|
|
59
|
+
cfg = getattr(model, "config", None)
|
|
60
|
+
n_layer = int(
|
|
61
|
+
getattr(cfg, "n_layer", getattr(cfg, "num_hidden_layers", 0)) or 0
|
|
62
|
+
)
|
|
63
|
+
n_head = int(
|
|
64
|
+
getattr(cfg, "n_head", getattr(cfg, "num_attention_heads", 0)) or 0
|
|
65
|
+
)
|
|
66
|
+
heads = [n_head] * n_layer if n_layer and n_head else []
|
|
67
|
+
return {
|
|
68
|
+
"n_layer": n_layer,
|
|
69
|
+
"heads_per_layer": heads,
|
|
70
|
+
"mlp_dims": [],
|
|
71
|
+
"tying": {},
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
# ---- Snapshots ----
|
|
75
|
+
# Inherit snapshot()/restore() from HFAdapterMixin
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
__all__ = ["HF_GPTQ_Adapter"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Marker file for PEP 561 type hints
|
invarlock/py.typed
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Marker file for PEP 561 type hints
|