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,146 @@
1
+ """
2
+ InvarLock Retry Controller
3
+ =====================
4
+
5
+ Manages retry logic for automated certification workflows with:
6
+ - Attempt budgets (max 3 attempts default)
7
+ - Time budgets (optional timeout)
8
+ - Parameter adjustment strategies per edit type
9
+ - Certificate-driven retry decisions
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import time
15
+ from typing import Any
16
+
17
+ __all__ = ["RetryController", "adjust_edit_params"]
18
+
19
+
20
+ class RetryController:
21
+ """
22
+ Controls retry logic for certificate-driven automation.
23
+
24
+ Features:
25
+ - Attempt budget enforcement (default 3 max)
26
+ - Optional timeout enforcement
27
+ - Edit parameter adjustment between attempts
28
+ - Logging of retry decisions
29
+ """
30
+
31
+ def __init__(
32
+ self, max_attempts: int = 3, timeout: int | None = None, verbose: bool = False
33
+ ):
34
+ """
35
+ Initialize retry controller.
36
+
37
+ Args:
38
+ max_attempts: Maximum retry attempts (default 3)
39
+ timeout: Optional timeout in seconds
40
+ verbose: Enable verbose logging
41
+ """
42
+ self.max_attempts = max_attempts
43
+ self.timeout = timeout
44
+ self.verbose = verbose
45
+ self.start_time = time.time()
46
+ self.attempt_history: list[dict[str, Any]] = []
47
+
48
+ def should_retry(self, certificate_passed: bool) -> bool:
49
+ """
50
+ Determine if retry should be attempted.
51
+
52
+ Args:
53
+ certificate_passed: Whether certificate validation passed
54
+
55
+ Returns:
56
+ True if retry should be attempted, False otherwise
57
+ """
58
+ # If certificate passed, no retry needed
59
+ if certificate_passed:
60
+ return False
61
+
62
+ # Check attempt budget (attempt count equals history length)
63
+ if len(self.attempt_history) >= self.max_attempts:
64
+ if self.verbose:
65
+ print(f"Exhausted {self.max_attempts} attempts, stopping retry")
66
+ return False
67
+
68
+ # Check timeout budget
69
+ if self.timeout is not None:
70
+ elapsed = time.time() - self.start_time
71
+ if elapsed > self.timeout:
72
+ if self.verbose:
73
+ print(
74
+ f"Timeout {self.timeout}s exceeded ({elapsed:.1f}s), stopping retry"
75
+ )
76
+ return False
77
+
78
+ # Retry is allowed - increment attempt counter for next check
79
+ return True
80
+
81
+ def record_attempt(
82
+ self,
83
+ attempt_num: int,
84
+ certificate_result: dict[str, Any],
85
+ edit_params: dict[str, Any],
86
+ ) -> None:
87
+ """Record details of an attempt for tracking."""
88
+ certificate_result = certificate_result or {}
89
+ edit_params = edit_params or {}
90
+
91
+ self.attempt_history.append(
92
+ {
93
+ "attempt": attempt_num,
94
+ "timestamp": time.time(),
95
+ "certificate_passed": certificate_result.get("passed", False),
96
+ "edit_params": edit_params.copy(),
97
+ "failures": certificate_result.get("failures", []),
98
+ "validation": certificate_result.get("validation", {}),
99
+ }
100
+ )
101
+
102
+ def get_attempt_summary(self) -> dict[str, Any]:
103
+ """Get summary of all retry attempts."""
104
+ return {
105
+ "total_attempts": len(self.attempt_history),
106
+ "max_attempts": self.max_attempts,
107
+ "timeout": self.timeout,
108
+ "elapsed_time": time.time() - self.start_time,
109
+ "attempts": self.attempt_history,
110
+ }
111
+
112
+
113
+ def adjust_edit_params(
114
+ edit_name: str,
115
+ edit_params: dict[str, Any],
116
+ attempt: int,
117
+ certificate_result: dict[str, Any] | None = None,
118
+ ) -> dict[str, Any]:
119
+ """
120
+ Adjust edit parameters for retry attempt based on edit type and failure mode.
121
+
122
+ Strategies:
123
+ - Quant: Add/increase clamp_ratio for stability
124
+
125
+ Args:
126
+ edit_name: Name of the edit operation
127
+ edit_params: Current edit parameters
128
+ attempt: Attempt number (1-indexed)
129
+ certificate_result: Optional certificate result for failure analysis
130
+
131
+ Returns:
132
+ Adjusted parameters for next attempt
133
+ """
134
+ adjusted = edit_params.copy()
135
+
136
+ # Quantization adjustments
137
+ if "quant" in edit_name.lower():
138
+ # Add clamp_ratio for stability
139
+ if "clamp_ratio" not in adjusted:
140
+ adjusted["clamp_ratio"] = 0.01
141
+ print(" Quant retry adjustment: added clamp_ratio=0.01")
142
+ else:
143
+ # Could increase existing clamp_ratio if needed
144
+ pass
145
+
146
+ return adjusted