bootstrap_phase1_extraction_jobs_live.py
6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env /usr/local/miniconda3/bin/python
from __future__ import annotations
import argparse
import json
from pathlib import Path
from typing import Any
import psycopg
ROOT = Path(__file__).resolve().parents[1]
DEFAULT_OUTPUT = ROOT / 'data' / 'pgvector_eval' / 'music20' / 'phase1_extraction_jobs_report.json'
JOB_SPECS = [
{
'model_name': 'chromaprint',
'model_version': 'v1',
'feature_name': 'fingerprint_asset',
'window_sec': 5.0,
'hop_sec': 2.5,
'target_scope': 'reference_set:phase1_hot_reference_v1',
'job_status': 'pending',
'shard_key': 'phase1/reference/chromaprint/v1',
'metadata_json': {'lane': 'exact', 'phase': 'phase1', 'priority': 'p0'},
},
{
'model_name': 'mert',
'model_version': 'v1-95m',
'feature_name': 'semantic_embedding',
'window_sec': 5.0,
'hop_sec': 2.5,
'target_scope': 'reference_set:phase1_hot_reference_v1',
'job_status': 'pending',
'shard_key': 'phase1/reference/mert/v1-95m/5s_2.5s',
'metadata_json': {'lane': 'semantic', 'role': 'primary_baseline', 'phase': 'phase1'},
},
{
'model_name': 'mert',
'model_version': 'v1-95m',
'feature_name': 'semantic_embedding',
'window_sec': 10.0,
'hop_sec': 5.0,
'target_scope': 'reference_set:phase1_hot_reference_v1',
'job_status': 'pending',
'shard_key': 'phase1/reference/mert/v1-95m/10s_5s',
'metadata_json': {'lane': 'semantic', 'role': 'long_context_validation', 'phase': 'phase1'},
},
{
'model_name': 'muq',
'model_version': 'large-msd-iter',
'feature_name': 'semantic_embedding',
'window_sec': 5.0,
'hop_sec': 2.5,
'target_scope': 'reference_set:phase1_hot_reference_v1',
'job_status': 'pending',
'shard_key': 'phase1/reference/muq/large-msd-iter/5s_2.5s',
'metadata_json': {'lane': 'semantic', 'role': 'challenger', 'phase': 'phase1'},
},
{
'model_name': 'ecapa',
'model_version': 'acr-baseline-v1',
'feature_name': 'semantic_embedding',
'window_sec': 5.0,
'hop_sec': 2.5,
'target_scope': 'reference_set:phase1_hot_reference_v1',
'job_status': 'pending',
'shard_key': 'phase1/reference/ecapa/acr-baseline-v1/5s_2.5s',
'metadata_json': {'lane': 'semantic', 'role': 'historical_baseline', 'phase': 'phase1'},
},
]
def resolve_feature_set_id(conn: psycopg.Connection, job: dict[str, Any]) -> int:
row = conn.execute(
"""
SELECT fs.feature_set_id
FROM feature_set_registry fs
JOIN model_registry mr ON mr.model_id = fs.model_id
WHERE mr.model_name = %s
AND mr.model_version = %s
AND fs.feature_name = %s
AND coalesce(fs.window_sec, -1) = coalesce(%s, -1)
AND coalesce(fs.hop_sec, -1) = coalesce(%s, -1)
ORDER BY fs.feature_set_id
LIMIT 1;
""",
(
job['model_name'],
job['model_version'],
job['feature_name'],
job['window_sec'],
job['hop_sec'],
),
).fetchone()
if not row:
raise RuntimeError(
f"Feature set not found for {job['model_name']} {job['model_version']} {job['feature_name']} {job['window_sec']}/{job['hop_sec']}"
)
return int(row[0])
def ensure_job(conn: psycopg.Connection, feature_set_id: int, job: dict[str, Any]) -> tuple[int, str]:
existing = conn.execute(
"""
SELECT extraction_job_id
FROM feature_extraction_job
WHERE feature_set_id = %s
AND target_scope = %s
AND coalesce(shard_key, '') = coalesce(%s, '')
ORDER BY extraction_job_id
LIMIT 1;
""",
(feature_set_id, job['target_scope'], job['shard_key']),
).fetchone()
if existing:
conn.execute(
"""
UPDATE feature_extraction_job
SET job_status = %s,
metadata_json = %s::jsonb
WHERE extraction_job_id = %s;
""",
(job['job_status'], json.dumps(job['metadata_json']), existing[0]),
)
return int(existing[0]), 'reused'
row = conn.execute(
"""
INSERT INTO feature_extraction_job (
feature_set_id, target_scope, job_status, shard_key,
input_count, output_count, started_at, finished_at,
log_uri, metadata_json
) VALUES (
%s, %s, %s, %s,
NULL, NULL, NULL, NULL,
NULL, %s::jsonb
)
RETURNING extraction_job_id;
""",
(feature_set_id, job['target_scope'], job['job_status'], job['shard_key'], json.dumps(job['metadata_json'])),
).fetchone()
return int(row[0]), 'inserted'
def main() -> None:
ap = argparse.ArgumentParser()
ap.add_argument('--dsn', required=True)
ap.add_argument('--schema', default='acr_test')
ap.add_argument('--output', default=str(DEFAULT_OUTPUT))
args = ap.parse_args()
summary: dict[str, Any] = {
'schema': args.schema,
'dsn_redacted': 'postgres://d2:***@127.0.0.1:5432/d2',
'jobs': [],
}
with psycopg.connect(args.dsn, autocommit=True) as conn:
conn.execute(f'SET search_path TO {args.schema}, public;')
for job in JOB_SPECS:
feature_set_id = resolve_feature_set_id(conn, job)
extraction_job_id, operation = ensure_job(conn, feature_set_id, job)
summary['jobs'].append({
'extraction_job_id': extraction_job_id,
'feature_set_id': feature_set_id,
'model_name': job['model_name'],
'model_version': job['model_version'],
'feature_name': job['feature_name'],
'window_sec': job['window_sec'],
'hop_sec': job['hop_sec'],
'target_scope': job['target_scope'],
'job_status': job['job_status'],
'operation': operation,
})
summary['counts'] = {
'feature_extraction_job': int(conn.execute('SELECT count(*) FROM feature_extraction_job;').fetchone()[0]),
'pending_jobs': int(conn.execute("SELECT count(*) FROM feature_extraction_job WHERE job_status = 'pending';").fetchone()[0]),
}
out = Path(args.output)
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text(json.dumps(summary, ensure_ascii=False, indent=2), encoding='utf-8')
print(json.dumps(summary, ensure_ascii=False, indent=2))
if __name__ == '__main__':
main()