Repository navigation
312 lines (277 loc) · 12.2 KB
/
Copy pathquantize.yml
File metadata and controls
312 lines (277 loc) · 12.2 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
name: Model Quantization & Validation
permissions:
contents: read
on:
push:
branches: [master]
paths:
- 'apps/desktop/public/models/**'
- 'scripts/quantize/**'
- '.github/workflows/quantize.yml'
pull_request:
paths:
- 'apps/desktop/public/models/**'
- 'scripts/quantize/**'
schedule:
# Weekly full re-validation to catch ONNX Runtime / toolchain drift
- cron: '0 6 * * 1'
workflow_dispatch:
inputs:
full_quantize:
description: 'Run full quantization (slow)'
type: boolean
default: false
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
# ---------------------------------------------------------------------------
# Fast PR gate: verify manifest schema, source hashes, existing artifact
# integrity. No Python/ORT needed — runs in Node.js.
# ---------------------------------------------------------------------------
manifest-verify:
name: Manifest verification
timeout-minutes: 10
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
- name: Validate manifest JSON schema
run: node scripts/models/validate-manifest.mjs
- name: Verify bundled model files exist
run: node scripts/models/validate-manifest.mjs --check-files
- name: Verify quantized artifact reports exist
# Two bugs kept this from ever checking anything real, and neither
# surfaced because the manifest step above always failed first:
#
# 1. It required `source_hash`. quantize_model.py has always written
# `source_sha256` (see its QuantizationReport dataclass), so the
# field it demanded did not exist on any report ever generated.
# 2. `endsWith('-report.json')` also matches `*-validation-report.json`,
# which validate_model.py writes with a completely different schema
# (MAE/PSNR/speedup metrics). Those are quality results, not
# quantization provenance, and have no weight_type to report.
run: |
node -e "
const fs = require('fs');
const path = require('path');
const quantizedDir = 'apps/desktop/public/models/quantized';
if (!fs.existsSync(quantizedDir)) {
console.log('No quantized directory — skipping report check');
process.exit(0);
}
const required = ['source_sha256', 'output_sha256', 'ort_version', 'weight_type'];
const reports = fs.readdirSync(quantizedDir)
.filter(f => f.endsWith('-report.json') && !f.endsWith('-validation-report.json'));
if (reports.length === 0) {
console.error('ERROR: no quantization reports found in', quantizedDir);
process.exit(1);
}
let errors = 0;
for (const report of reports) {
const data = JSON.parse(fs.readFileSync(path.join(quantizedDir, report), 'utf-8'));
const missing = required.filter(f => !data[f]);
if (missing.length > 0) {
console.error('ERROR:', report, 'missing provenance field(s):', missing.join(', '));
errors++;
}
}
if (errors > 0) process.exit(1);
console.log(reports.length, 'quantization reports OK');
"
# ---------------------------------------------------------------------------
# Validate quantized model quality against FP32 baseline
# Requires Python + ONNX Runtime — runs only on manifest changes or dispatch.
# ---------------------------------------------------------------------------
quality-validate:
name: Quality validation
needs: [manifest-verify]
if: |
github.event_name == 'schedule' ||
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'quantize'))
timeout-minutes: 30
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
- name: Setup Python
uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0
with:
python-version: '3.12'
cache: 'pip'
cache-dependency-path: scripts/quantize/requirements.txt
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
pip install -r scripts/quantize/requirements.txt
- name: Validate quantized models
run: |
python3 scripts/quantize/validate_model.py --all --synthetic 2>&1 | tee validation.log
if grep -q 'FAILED\|REJECTED' validation.log; then
echo "ERROR: Quality validation failed"
exit 1
fi
- name: Upload validation report
if: always()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9
with:
name: quantization-validation-report
path: apps/desktop/public/models/quantized/*-validation-report.json
# ---------------------------------------------------------------------------
# Full quantization run (manual only — slow, generates new artifacts)
# ---------------------------------------------------------------------------
full-quantize:
name: Full quantization
needs: [manifest-verify]
if: github.event_name == 'workflow_dispatch' && inputs.full_quantize
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
- name: Setup Python
uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0
with:
python-version: '3.12'
cache: 'pip'
cache-dependency-path: scripts/quantize/requirements.txt
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
pip install -r scripts/quantize/requirements.txt
- name: Verify source model hashes
run: |
node -e "
const fs = require('fs');
const crypto = require('crypto');
const manifest = JSON.parse(fs.readFileSync('apps/desktop/public/models/manifest.json', 'utf-8'));
let errors = 0;
for (const entry of manifest.models) {
if (entry.bundled && entry.sha256) {
const filePath = 'apps/desktop/public/models' + entry.localPath;
const data = fs.readFileSync(filePath);
const hash = crypto.createHash('sha256').update(data).digest('hex');
if (hash !== entry.sha256) {
console.error('ERROR: hash mismatch for', entry.id);
console.error(' expected:', entry.sha256);
console.error(' actual: ', hash);
errors++;
}
}
}
if (errors > 0) {
console.error(errors, 'hash mismatch(es)');
process.exit(1);
}
console.log('All source model hashes verified');
"
- name: Quantize all models
run: |
python3 scripts/quantize/quantize_model.py --all 2>&1 | tee quantize.log
- name: Validate quantized models
run: |
python3 scripts/quantize/validate_model.py --all --synthetic 2>&1 | tee validate.log
if grep -q 'FAILED\|REJECTED' validate.log; then
echo "ERROR: Quality validation failed for quantized models"
exit 1
fi
- name: Verify manifest consistency
run: |
node -e "
const fs = require('fs');
const manifest = JSON.parse(fs.readFileSync('apps/desktop/public/models/manifest.json', 'utf-8'));
const crypto = require('crypto');
// Check that quantized artifacts match manifest entries. Only
// in-repo (bundled) int8 entries have staged files; upstream int8
// models are runtime downloads and are never written here.
for (const entry of manifest.models) {
if (entry.precision === 'int8' && entry.bundled) {
const filePath = 'apps/desktop/public/models' + entry.localPath;
if (!fs.existsSync(filePath)) {
console.error('ERROR: quantized artifact missing:', filePath);
process.exit(1);
}
// Verify SHA-256
if (entry.sha256) {
const data = fs.readFileSync(filePath);
const hash = crypto.createHash('sha256').update(data).digest('hex');
if (hash !== entry.sha256) {
console.error('ERROR: hash mismatch for quantized', entry.id);
console.error(' expected:', entry.sha256);
console.error(' actual: ', hash);
process.exit(1);
}
}
}
}
console.log('Quantized artifacts consistent with manifest');
"
- name: Upload quantized artifacts
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9
with:
name: quantized-models
path: apps/desktop/public/models/quantized/
- name: Upload manifests and reports
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9
with:
name: quantization-reports
path: |
apps/desktop/public/models/quantized/*-report.json
apps/desktop/public/models/quantized/*-validation-report.json
# ---------------------------------------------------------------------------
# Platform-specific runtime smoke test
# ---------------------------------------------------------------------------
runtime-smoke:
name: Runtime smoke (${{ matrix.os }})
needs: [manifest-verify]
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
timeout-minutes: 15
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
- name: Setup Python
uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0
with:
python-version: '3.12'
- name: Install ONNX Runtime
run: |
python3 -m pip install onnxruntime
- name: Load each bundled model and verify graph metadata
# Explicit bash: this step's inline Python uses backslash-escaped
# quotes (entry[\"id\"]) that only survive bash's `run:` quoting.
# windows-latest defaults to pwsh, whose escaping rules are
# different and corrupt the script (`SyntaxError: '[' was never
# closed`) before Python ever sees it. Git Bash ships on all three
# hosted runner OSes, so this doesn't need per-OS branching.
shell: bash
run: |
python3 -c "
import onnxruntime as ort
import json
import os
manifest_path = 'apps/desktop/public/models/manifest.json'
with open(manifest_path) as f:
manifest = json.load(f)
for entry in manifest['models']:
if not entry.get('bundled'):
continue
model_path = os.path.join('apps/desktop/public/models', entry['localPath'])
if not os.path.exists(model_path):
print(f'SKIP (not found): {entry[\"id\"]}')
continue
try:
session = ort.InferenceSession(model_path, providers=['CPUExecutionProvider'])
meta = session.get_modelmeta()
print(f'OK: {entry[\"id\"]} — {len(session.get_inputs())} input(s), {len(session.get_outputs())} output(s)')
# Check input/output names are non-empty
for inp in session.get_inputs():
assert inp.name, f'Empty input name in {entry[\"id\"]}'
for out in session.get_outputs():
assert out.name, f'Empty output name in {entry[\"id\"]}'
except Exception as e:
print(f'FAIL: {entry[\"id\"]} — {e}')
raise
print('All bundled models loaded successfully')
"