cortexm 0.3.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 (120) hide show
  1. context_m.py +17 -0
  2. cortexm/__init__.py +45 -0
  3. cortexm/accel.py +403 -0
  4. cortexm/api/__init__.py +0 -0
  5. cortexm/api/chaos.py +118 -0
  6. cortexm/api/memory.py +635 -0
  7. cortexm/bench/__init__.py +0 -0
  8. cortexm/bench/abilities.py +311 -0
  9. cortexm/bench/baselines.py +89 -0
  10. cortexm/bench/beam_loader.py +317 -0
  11. cortexm/bench/generator.py +376 -0
  12. cortexm/bench/harness.py +211 -0
  13. cortexm/bench/messy.py +218 -0
  14. cortexm/bench/micro.py +251 -0
  15. cortexm/bench/ood.py +443 -0
  16. cortexm/bench/run.py +137 -0
  17. cortexm/bridge/__init__.py +0 -0
  18. cortexm/bridge/dates.py +178 -0
  19. cortexm/bridge/decoders.py +204 -0
  20. cortexm/bridge/enrich.py +255 -0
  21. cortexm/bridge/extractor.py +316 -0
  22. cortexm/bridge/fallback.py +332 -0
  23. cortexm/bridge/onnx_runtime.py +158 -0
  24. cortexm/bridge/patterns.py +760 -0
  25. cortexm/bridge/ppr.py +104 -0
  26. cortexm/bridge/prefilter.py +188 -0
  27. cortexm/bridge/query_extract.py +420 -0
  28. cortexm/bridge/reader.py +1174 -0
  29. cortexm/bridge/rerank.py +204 -0
  30. cortexm/bridge/writer.py +492 -0
  31. cortexm/cli.py +295 -0
  32. cortexm/cognition/__init__.py +53 -0
  33. cortexm/cognition/abstraction.py +192 -0
  34. cortexm/cognition/analogy.py +159 -0
  35. cortexm/cognition/engine.py +204 -0
  36. cortexm/cognition/gaps.py +365 -0
  37. cortexm/cognition/scanner.py +204 -0
  38. cortexm/config.py +375 -0
  39. cortexm/cortexm.py +8 -0
  40. cortexm/enterprise/__init__.py +0 -0
  41. cortexm/enterprise/audit.py +178 -0
  42. cortexm/enterprise/governance.py +239 -0
  43. cortexm/errors.py +35 -0
  44. cortexm/features/__init__.py +0 -0
  45. cortexm/features/git.py +204 -0
  46. cortexm/features/prefetch.py +88 -0
  47. cortexm/features/zk.py +105 -0
  48. cortexm/federation/__init__.py +39 -0
  49. cortexm/federation/crdt.py +275 -0
  50. cortexm/federation/fabric.py +109 -0
  51. cortexm/federation/hlc.py +80 -0
  52. cortexm/federation/node.py +145 -0
  53. cortexm/federation/schema_report.py +73 -0
  54. cortexm/federation/transport.py +164 -0
  55. cortexm/index/__init__.py +19 -0
  56. cortexm/index/nsg.py +386 -0
  57. cortexm/mcp/__init__.py +0 -0
  58. cortexm/mcp/server.py +985 -0
  59. cortexm/metrics.py +62 -0
  60. cortexm/migrate/__init__.py +0 -0
  61. cortexm/migrate/importers.py +192 -0
  62. cortexm/provenance/__init__.py +78 -0
  63. cortexm/provenance/agent.py +214 -0
  64. cortexm/provenance/cose.py +201 -0
  65. cortexm/provenance/scitt.py +258 -0
  66. cortexm/provenance/vc.py +250 -0
  67. cortexm/security/__init__.py +0 -0
  68. cortexm/security/crypto.py +162 -0
  69. cortexm/security/hashes.py +140 -0
  70. cortexm/security/injection.py +149 -0
  71. cortexm/security/mind.py +154 -0
  72. cortexm/security/pii.py +265 -0
  73. cortexm/security/rbac.py +169 -0
  74. cortexm/security/sandbox.py +131 -0
  75. cortexm/security/zk_hamming.py +142 -0
  76. cortexm/security/zk_sql.py +485 -0
  77. cortexm/server/__init__.py +0 -0
  78. cortexm/server/metrics.py +88 -0
  79. cortexm/server/rest.py +936 -0
  80. cortexm/server/sparql.py +984 -0
  81. cortexm/text/__init__.py +0 -0
  82. cortexm/text/dissim.py +252 -0
  83. cortexm/text/embedder.py +155 -0
  84. cortexm/text/fuzzy.py +218 -0
  85. cortexm/text/idiolect.py +253 -0
  86. cortexm/text/labse.py +374 -0
  87. cortexm/text/tokenizer.py +79 -0
  88. cortexm/trace/__init__.py +0 -0
  89. cortexm/trace/blob_arena.py +277 -0
  90. cortexm/trace/consolidate.py +337 -0
  91. cortexm/trace/contradictions.py +69 -0
  92. cortexm/trace/dedup.py +114 -0
  93. cortexm/trace/edges.py +214 -0
  94. cortexm/trace/fact.py +121 -0
  95. cortexm/trace/fade.py +245 -0
  96. cortexm/trace/lifecycle.py +112 -0
  97. cortexm/trace/rebuild.py +173 -0
  98. cortexm/trace/rules.py +171 -0
  99. cortexm/trace/store.py +680 -0
  100. cortexm/trace/structural.py +183 -0
  101. cortexm/trace/tmt.py +335 -0
  102. cortexm/util.py +148 -0
  103. cortexm/vsa/__init__.py +0 -0
  104. cortexm/vsa/attribution.py +149 -0
  105. cortexm/vsa/cleanup.py +161 -0
  106. cortexm/vsa/codecs.py +397 -0
  107. cortexm/vsa/hologram_overlay.py +139 -0
  108. cortexm/vsa/index.py +163 -0
  109. cortexm/vsa/ops.py +149 -0
  110. cortexm/vsa/palace.py +446 -0
  111. cortexm/vsa/role_vectors.py +236 -0
  112. cortexm/vsa/slb.py +78 -0
  113. cortexm/vsa/tlsh_trie.py +137 -0
  114. cortexm/vsa/working_memory.py +249 -0
  115. cortexm-0.3.0.dist-info/METADATA +482 -0
  116. cortexm-0.3.0.dist-info/RECORD +120 -0
  117. cortexm-0.3.0.dist-info/WHEEL +5 -0
  118. cortexm-0.3.0.dist-info/entry_points.txt +2 -0
  119. cortexm-0.3.0.dist-info/licenses/LICENSE +190 -0
  120. cortexm-0.3.0.dist-info/top_level.txt +2 -0
@@ -0,0 +1,158 @@
1
+ """ONNX Runtime CPU + FP32 + LayerCast determinism seam.
2
+
3
+ arxiv research: arXiv:2506.09501 (Yuan et al., 2025) — FP32 yields
4
+ near-zero variance in token probabilities; BF16 has significant
5
+ variance; FP16 is moderate. LayerCast achieves 0% Std@Acc (perfect
6
+ reproducibility) by storing weights in BF16 for memory and casting to
7
+ FP32 in-graph before each matmul.
8
+
9
+ ONNX Runtime on CPU produces numerically identical results across runs
10
+ (non-determinism is a GPU problem — tensor parallelism, atomic
11
+ reductions, FlashAttention).
12
+
13
+ This module is a SEAM, not a built-out implementation. It documents:
14
+ * The deterministic execution contract for any future ONNX LLM
15
+ enrichment path (bridge/enrich.py)
16
+ * The config knobs that must be set on ONNX Runtime to guarantee
17
+ reproducibility
18
+ * The contract that LayerCast-instrumented ONNX models must satisfy
19
+
20
+ Production use: when an ONNX LLM is wired into bridge/enrich.py,
21
+ it MUST be loaded through this module's `deterministic_session()`
22
+ helper. This closes the μ=0 audit loop on the LLM enrichment path
23
+ too — every enriched fact becomes bit-exact reproducible.
24
+
25
+ Why this matters: the deterministic extractor (bridge/extractor.py)
26
+ already wins on cost; LayerCast is the upgrade to enable on-device LLM
27
+ enrichment WITHOUT losing reproducibility. The cost: ~30% slower than
28
+ BF16 on GPU, but at memory ingest/query extraction latency, that's
29
+ acceptable.
30
+ """
31
+
32
+ from __future__ import annotations
33
+
34
+ import os
35
+ from dataclasses import dataclass
36
+
37
+
38
+ @dataclass
39
+ class DeterministicConfig:
40
+ """Config knobs to guarantee ONNX Runtime CPU FP32 determinism."""
41
+ # providers: CPU only — GPU introduces non-determinism via tensor parallelism
42
+ providers: tuple[str, ...] = ("CPUExecutionProvider",)
43
+ # graph opt level: ORT_DISABLE_ALL = 0; optimizations can reorder ops
44
+ graph_optimization_level: int = 0
45
+ # intra-op threads: 1 — multi-thread reductions are non-associative
46
+ intra_op_num_threads: int = 1
47
+ # inter-op threads: 1
48
+ inter_op_num_threads: int = 1
49
+ # execution mode: SEQUENTIAL — parallel execution can reorder
50
+ execution_mode: int = 0 # ORT_SEQUENTIAL
51
+ # FP32 precision enforcement (LayerCast-compatible)
52
+ force_fp32: bool = True
53
+ # disable FlashAttention if available (introduces non-determinism)
54
+ disable_flash_attention: bool = True
55
+ # arena alloc: fixed for reproducibility
56
+ enable_cpu_mem_arena: bool = False
57
+ # random seed: fixed
58
+ seed: int = 0x0C0FFEE
59
+
60
+
61
+ # Documented contract for LayerCast-instrumented ONNX models:
62
+ LAYERCAST_CONTRACT = """
63
+ LayerCast ONNX model requirements (arXiv:2506.09501):
64
+
65
+ 1. Weights stored as BF16 (2 bytes each) for memory efficiency.
66
+ 2. Before each MatMul node, a Cast node converts BF16 weights to FP32.
67
+ 3. MatMul accumulates in FP32 in registers/SRAM.
68
+ 4. Result is optionally Cast back to BF16 for next layer input.
69
+ 5. Reduction trees are fixed — no non-associative reorderings.
70
+
71
+ Verification:
72
+ - Run the same input twice through the model with DeterministicConfig.
73
+ - Compare output bytes — must be byte-identical (0% Std@Acc).
74
+
75
+ Failure modes:
76
+ - If weights are stored FP32 directly: still deterministic but 2x memory.
77
+ - If weights are FP16: moderate variance (paper shows ~0.5% Std@Acc).
78
+ - If weights are BF16 without LayerCast Cast nodes: significant variance.
79
+ """
80
+
81
+
82
+ def deterministic_session(model_path: str,
83
+ config: DeterministicConfig | None = None):
84
+ """Create an ONNX Runtime InferenceSession with deterministic config.
85
+
86
+ This is the SEAM for any future LLM enrichment path. Currently a
87
+ thin wrapper that documents the contract — actual use requires
88
+ `pip install onnxruntime` and a LayerCast-instrumented model.
89
+ """
90
+ try:
91
+ import onnxruntime as ort
92
+ except ImportError as e:
93
+ raise ImportError(
94
+ "onnxruntime not installed. Install with: pip install onnxruntime. "
95
+ "LayerCast contract requires a model with explicit Cast nodes "
96
+ "before each MatMul. See LAYERCAST_CONTRACT."
97
+ ) from e
98
+
99
+ cfg = config or DeterministicConfig()
100
+ so = ort.SessionOptions()
101
+ so.graph_optimization_level = cfg.graph_optimization_level
102
+ so.intra_op_num_threads = cfg.intra_op_num_threads
103
+ so.inter_op_num_threads = cfg.inter_op_num_threads
104
+ so.execution_mode = cfg.execution_mode
105
+ so.enable_cpu_mem_arena = cfg.enable_cpu_mem_arena
106
+
107
+ sess = ort.InferenceSession(
108
+ model_path,
109
+ sess_options=so,
110
+ providers=list(cfg.providers),
111
+ )
112
+ return sess
113
+
114
+
115
+ def verify_determinism(model_path: str, sample_input: dict,
116
+ runs: int = 3) -> dict:
117
+ """Run the same input through the model N times; verify byte-identical.
118
+
119
+ Returns {identical: bool, run_count: int, variance: float}.
120
+ Production check before deploying any ONNX LLM in the enrichment path.
121
+ """
122
+ try:
123
+ import numpy as np
124
+ except ImportError:
125
+ return {"identical": False, "error": "numpy not available"}
126
+
127
+ sess = deterministic_session(model_path)
128
+ outputs = []
129
+ for _ in range(runs):
130
+ out = sess.run(None, sample_input)
131
+ # flatten all outputs into one bytes blob
132
+ flat = b"".join(arr.tobytes() for arr in out if hasattr(arr, "tobytes"))
133
+ outputs.append(flat)
134
+ identical = all(o == outputs[0] for o in outputs)
135
+ if not identical:
136
+ # compute variance
137
+ arr_lens = [len(o) for o in outputs]
138
+ # byte-level variance as fraction differing
139
+ min_len = min(arr_lens)
140
+ diffs = [sum(1 for a, b in zip(outputs[i], outputs[0]) if a != b)
141
+ for i in range(1, len(outputs))]
142
+ variance = sum(diffs) / (max(1, len(diffs)) * max(1, min_len))
143
+ else:
144
+ variance = 0.0
145
+ return {
146
+ "identical": identical,
147
+ "run_count": runs,
148
+ "variance": float(variance),
149
+ "contract": "LayerCast FP32" if identical else "non-deterministic",
150
+ }
151
+
152
+
153
+ __all__ = [
154
+ "DeterministicConfig",
155
+ "LAYERCAST_CONTRACT",
156
+ "deterministic_session",
157
+ "verify_determinism",
158
+ ]