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.
- context_m.py +17 -0
- cortexm/__init__.py +45 -0
- cortexm/accel.py +403 -0
- cortexm/api/__init__.py +0 -0
- cortexm/api/chaos.py +118 -0
- cortexm/api/memory.py +635 -0
- cortexm/bench/__init__.py +0 -0
- cortexm/bench/abilities.py +311 -0
- cortexm/bench/baselines.py +89 -0
- cortexm/bench/beam_loader.py +317 -0
- cortexm/bench/generator.py +376 -0
- cortexm/bench/harness.py +211 -0
- cortexm/bench/messy.py +218 -0
- cortexm/bench/micro.py +251 -0
- cortexm/bench/ood.py +443 -0
- cortexm/bench/run.py +137 -0
- cortexm/bridge/__init__.py +0 -0
- cortexm/bridge/dates.py +178 -0
- cortexm/bridge/decoders.py +204 -0
- cortexm/bridge/enrich.py +255 -0
- cortexm/bridge/extractor.py +316 -0
- cortexm/bridge/fallback.py +332 -0
- cortexm/bridge/onnx_runtime.py +158 -0
- cortexm/bridge/patterns.py +760 -0
- cortexm/bridge/ppr.py +104 -0
- cortexm/bridge/prefilter.py +188 -0
- cortexm/bridge/query_extract.py +420 -0
- cortexm/bridge/reader.py +1174 -0
- cortexm/bridge/rerank.py +204 -0
- cortexm/bridge/writer.py +492 -0
- cortexm/cli.py +295 -0
- cortexm/cognition/__init__.py +53 -0
- cortexm/cognition/abstraction.py +192 -0
- cortexm/cognition/analogy.py +159 -0
- cortexm/cognition/engine.py +204 -0
- cortexm/cognition/gaps.py +365 -0
- cortexm/cognition/scanner.py +204 -0
- cortexm/config.py +375 -0
- cortexm/cortexm.py +8 -0
- cortexm/enterprise/__init__.py +0 -0
- cortexm/enterprise/audit.py +178 -0
- cortexm/enterprise/governance.py +239 -0
- cortexm/errors.py +35 -0
- cortexm/features/__init__.py +0 -0
- cortexm/features/git.py +204 -0
- cortexm/features/prefetch.py +88 -0
- cortexm/features/zk.py +105 -0
- cortexm/federation/__init__.py +39 -0
- cortexm/federation/crdt.py +275 -0
- cortexm/federation/fabric.py +109 -0
- cortexm/federation/hlc.py +80 -0
- cortexm/federation/node.py +145 -0
- cortexm/federation/schema_report.py +73 -0
- cortexm/federation/transport.py +164 -0
- cortexm/index/__init__.py +19 -0
- cortexm/index/nsg.py +386 -0
- cortexm/mcp/__init__.py +0 -0
- cortexm/mcp/server.py +985 -0
- cortexm/metrics.py +62 -0
- cortexm/migrate/__init__.py +0 -0
- cortexm/migrate/importers.py +192 -0
- cortexm/provenance/__init__.py +78 -0
- cortexm/provenance/agent.py +214 -0
- cortexm/provenance/cose.py +201 -0
- cortexm/provenance/scitt.py +258 -0
- cortexm/provenance/vc.py +250 -0
- cortexm/security/__init__.py +0 -0
- cortexm/security/crypto.py +162 -0
- cortexm/security/hashes.py +140 -0
- cortexm/security/injection.py +149 -0
- cortexm/security/mind.py +154 -0
- cortexm/security/pii.py +265 -0
- cortexm/security/rbac.py +169 -0
- cortexm/security/sandbox.py +131 -0
- cortexm/security/zk_hamming.py +142 -0
- cortexm/security/zk_sql.py +485 -0
- cortexm/server/__init__.py +0 -0
- cortexm/server/metrics.py +88 -0
- cortexm/server/rest.py +936 -0
- cortexm/server/sparql.py +984 -0
- cortexm/text/__init__.py +0 -0
- cortexm/text/dissim.py +252 -0
- cortexm/text/embedder.py +155 -0
- cortexm/text/fuzzy.py +218 -0
- cortexm/text/idiolect.py +253 -0
- cortexm/text/labse.py +374 -0
- cortexm/text/tokenizer.py +79 -0
- cortexm/trace/__init__.py +0 -0
- cortexm/trace/blob_arena.py +277 -0
- cortexm/trace/consolidate.py +337 -0
- cortexm/trace/contradictions.py +69 -0
- cortexm/trace/dedup.py +114 -0
- cortexm/trace/edges.py +214 -0
- cortexm/trace/fact.py +121 -0
- cortexm/trace/fade.py +245 -0
- cortexm/trace/lifecycle.py +112 -0
- cortexm/trace/rebuild.py +173 -0
- cortexm/trace/rules.py +171 -0
- cortexm/trace/store.py +680 -0
- cortexm/trace/structural.py +183 -0
- cortexm/trace/tmt.py +335 -0
- cortexm/util.py +148 -0
- cortexm/vsa/__init__.py +0 -0
- cortexm/vsa/attribution.py +149 -0
- cortexm/vsa/cleanup.py +161 -0
- cortexm/vsa/codecs.py +397 -0
- cortexm/vsa/hologram_overlay.py +139 -0
- cortexm/vsa/index.py +163 -0
- cortexm/vsa/ops.py +149 -0
- cortexm/vsa/palace.py +446 -0
- cortexm/vsa/role_vectors.py +236 -0
- cortexm/vsa/slb.py +78 -0
- cortexm/vsa/tlsh_trie.py +137 -0
- cortexm/vsa/working_memory.py +249 -0
- cortexm-0.3.0.dist-info/METADATA +482 -0
- cortexm-0.3.0.dist-info/RECORD +120 -0
- cortexm-0.3.0.dist-info/WHEEL +5 -0
- cortexm-0.3.0.dist-info/entry_points.txt +2 -0
- cortexm-0.3.0.dist-info/licenses/LICENSE +190 -0
- 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
|
+
]
|