mxguru1 commited on
Commit
c30c476
·
verified ·
1 Parent(s): 002163f

Add migration 003 — kv_sensitivity_profile schema with CHECK constraints + lookup index

Browse files
vault_migration_003_kv_sensitivity_profile.sql ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -- =====================================================================
2
+ -- Sovereign Hive — Vault Migration 003
3
+ -- =====================================================================
4
+ -- Adds: kv_sensitivity_profile
5
+ --
6
+ -- Rationale: The HSAQ KV-cache profiler (kv_profiler.py) emits per-layer
7
+ -- drift measurements at each (k_bits, v_bits, quantizer) probe. These
8
+ -- feed the allocator (assignment_v2.assign_kv_bits) under a separate
9
+ -- KV-cache memory budget from weights. Caching the rows means re-paying
10
+ -- the profiling cost only when (model_hash, calibration_hash,
11
+ -- pipeline_version) changes.
12
+ --
13
+ -- Audit invariants:
14
+ -- - Every write carries originating agent_id + agent_tier + timestamp.
15
+ -- - quantizer / drift_metric / k_bits / v_bits constrained via CHECK
16
+ -- so storage layer rejects garbled rows before they hit the allocator.
17
+ -- - Schema is append-mostly; multiple options per layer all live as
18
+ -- separate rows under the same composite primary key.
19
+ --
20
+ -- Pipeline-version note:
21
+ -- pipeline_version is part of the primary key. Any change to the drift
22
+ -- metric implementation, hook semantics, or sweep contract bumps the
23
+ -- version and renders previous rows lookup-misses (silently ignored
24
+ -- rather than reused with stale semantics).
25
+ --
26
+ -- Migration policy: HUMAN-APPLIED ONLY. Do not auto-apply from agent code.
27
+ -- Assumes migration 001 created schema_migrations and migration 002 ran
28
+ -- successfully (we don't depend on 002's tables but enforce ordering).
29
+ -- =====================================================================
30
+
31
+ BEGIN;
32
+
33
+ -- ---------------------------------------------------------------------
34
+ -- kv_sensitivity_profile
35
+ -- ---------------------------------------------------------------------
36
+ CREATE TABLE IF NOT EXISTS kv_sensitivity_profile (
37
+ -- Invalidation key (matches sensitivity_profile pattern from 002)
38
+ model_hash TEXT NOT NULL,
39
+ calibration_hash TEXT NOT NULL,
40
+ pipeline_version TEXT NOT NULL,
41
+
42
+ -- Per-layer identity
43
+ layer_idx INTEGER NOT NULL,
44
+
45
+ -- Probe definition
46
+ k_bits INTEGER NOT NULL
47
+ CHECK (k_bits IN (2, 3, 4, 8, 16)),
48
+ v_bits INTEGER NOT NULL
49
+ CHECK (v_bits IN (2, 3, 4, 8, 16)),
50
+ quantizer TEXT NOT NULL
51
+ CHECK (quantizer IN ('hqq_g64', 'scaled_uniform', 'scaled_per_head', 'fp16_passthrough')),
52
+
53
+ -- Measured drift (attention output, vs full-precision baseline)
54
+ drift_attn_output REAL NOT NULL,
55
+ drift_metric TEXT NOT NULL
56
+ CHECK (drift_metric IN ('mse_normalised', 'kl_softmax')),
57
+
58
+ -- Cost accounting — kept here so the allocator never recomputes
59
+ bytes_per_kv_token REAL NOT NULL,
60
+ max_seq_len_observed INTEGER NOT NULL,
61
+
62
+ -- Per-layer architecture facts (for assemblers / cross-checks)
63
+ num_kv_heads INTEGER NOT NULL,
64
+ head_dim INTEGER NOT NULL,
65
+
66
+ -- Provenance (audit chain — matches 002)
67
+ profiled_at TEXT NOT NULL, -- ISO 8601 UTC
68
+ profiled_by_agent_id TEXT NOT NULL,
69
+ profiled_by_agent_tier INTEGER NOT NULL,
70
+ written_at TEXT NOT NULL
71
+ DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
72
+
73
+ PRIMARY KEY (model_hash, calibration_hash, pipeline_version,
74
+ layer_idx, k_bits, v_bits, quantizer)
75
+ );
76
+
77
+ -- The allocator's bulk-fetch pattern: "give me every option for every
78
+ -- layer for this (model, calibration, pipeline_version)." This index
79
+ -- covers that read.
80
+ CREATE INDEX IF NOT EXISTS idx_kv_profile_lookup
81
+ ON kv_sensitivity_profile(model_hash, calibration_hash, pipeline_version);
82
+
83
+ -- For "show me everything we've ever measured for this model" queries.
84
+ CREATE INDEX IF NOT EXISTS idx_kv_profile_model
85
+ ON kv_sensitivity_profile(model_hash, written_at DESC);
86
+
87
+
88
+ -- ---------------------------------------------------------------------
89
+ -- Record this migration
90
+ -- ---------------------------------------------------------------------
91
+ INSERT INTO schema_migrations (version, applied_at, description)
92
+ VALUES (
93
+ '003',
94
+ strftime('%Y-%m-%dT%H:%M:%fZ', 'now'),
95
+ 'Add kv_sensitivity_profile for HSAQ KV-cache profiler'
96
+ );
97
+
98
+ COMMIT;