Files
digital-drosophila/models/male-cns-lif/conditioning-pipeline-acceleration-v1/finalize.py
T

195 lines
12 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Seal bounded engineering replay evidence; never resume scientific work."""
from datetime import datetime, timezone
import hashlib
import json
from pathlib import Path
import statistics
import subprocess
ROOT = Path('/home/qishizhe/phase4ar-pipeline-acceleration-20261001')
SOURCE = Path(__file__).absolute().parent
WORKTREE = SOURCE.parents[2]
def require(value, message):
if not value:
raise ValueError(message)
def read(path):
return json.loads(path.read_text())
def sha(path):
h = hashlib.sha256()
with path.open('rb') as stream:
for block in iter(lambda: stream.read(8 << 20), b''):
h.update(block)
return h.hexdigest()
def save(path, value):
with path.open('x') as stream:
json.dump(value, stream, indent=2, sort_keys=True, allow_nan=False)
stream.write('\n')
def stats(values):
return dict(count=len(values), median=statistics.median(values), mean=statistics.mean(values),
minimum=min(values), maximum=max(values), total=sum(values))
def successful_job(label):
record = read(ROOT / 'jobs' / (label + '.exit.json'))
require(record['observed_exit'] is True and record['exit_code'] == 0
and not record.get('stop_reason') and not record['timed_out'], 'Job did not exit successfully: ' + label)
return record
def check_inventory(root, inventory):
for rel, item in inventory.items():
path = root / rel
require(path.resolve().is_relative_to(root) and not path.is_symlink(), 'Unsafe artifact path')
expected = item if isinstance(item, str) else item['sha256']
require(sha(path) == expected, 'Artifact changed: ' + str(path))
if isinstance(item, dict) and 'bytes' in item:
require(path.stat().st_size == item['bytes'], 'Artifact size changed')
def main():
require(ROOT.is_dir() and not ROOT.is_symlink(), 'New experiment root required')
require(not (ROOT / 'final-summary.json').exists(), 'Finalization must not overwrite prior results')
jobs = {name: successful_job(name) for name in
('cpu-unit-tests', 'brain-unit-tests', 'cpu-profile-01', 'brain-replay-01')}
before, after = [read(ROOT / ('integrity-' + stage + '.json')) for stage in ('before', 'after')]
require(before['status'] == after['status'] == 'PASS', 'Pre/post integrity failed')
require(after['original']['status'] == 'PASS' and after['original']['files_checked'] == 64034
and not after['original']['file_mismatches'] and not after['original']['source_mismatches'], 'Science integrity failed')
cpu = read(ROOT / 'cpu-profile/report.json')
brain = read(ROOT / 'brain-replay/summary.json')
require(cpu['complete'] and cpu['fixtures'] == 14 and cpu['all_child_exits_observed_zero']
and cpu['all_full_array_reference_comparisons_passed'] and cpu['all_original_receipts_exact']
and cpu['full_AQ_validates_actual_second_pass_E_outputs'], 'CPU regression incomplete')
require(brain['complete'] and brain['all_bitwise_exact'] and brain['fresh_trial_count'] == 14
and brain['resident_trial_count'] == 28 and brain['actual_child_processes'] == 15, 'Brain regression incomplete')
check_inventory(ROOT / 'cpu-profile', cpu['files'])
check_inventory(ROOT / 'brain-replay', brain['files'])
fresh = [read(ROOT / ('brain-replay/sessions/fresh-%02d.json' % i))['records'][0] for i in range(14)]
resident = read(ROOT / 'brain-replay/sessions/resident.json')['records']
for record in fresh + resident:
require(record['exact'] and record['input_pins_verified_before_and_after']
and record['neural_state_fresh_each_call'] and not record['generated_delta_installed'], 'Invalid replay trial')
paired = []
for first in fresh:
others = [r for r in resident if r['fixture_id'] == first['fixture_id']]
require(len(others) == 2 and {r['direction'] for r in others} == {'forward', 'reverse'}, 'Incomplete fixed ordering')
fresh_runtime = first['timings']['runtime_and_builtin_output_seconds']
warm_runtime = statistics.median(r['timings']['runtime_and_builtin_output_seconds'] for r in others)
warm_wall = statistics.median(r['trial_wall_seconds'] for r in others)
paired.append(dict(fixture_id=first['fixture_id'], fixed_index=first['fixed_index'],
first_fixture_cold_build_and_context=first['fixed_index'] == 0,
fresh_runtime_seconds=fresh_runtime, resident_runtime_seconds=warm_runtime,
runtime_ratio=fresh_runtime / warm_runtime,
fresh_trial_wall_seconds=first['trial_wall_seconds'], resident_trial_wall_seconds=warm_wall,
trial_wall_ratio=first['trial_wall_seconds'] / warm_wall,
fresh_simulation_seconds=first['timings']['original_diagnostic_simulation_seconds'],
resident_simulation_seconds=statistics.median(r['timings']['original_diagnostic_simulation_seconds'] for r in others)))
# Exclusion is by fixed initialization position, never by observed outcome.
# All 42 trials remain in correctness evidence and the full paired table.
steady = paired[1:]
brain_timing = {name: stats([p[name] for p in steady]) for name in (
'fresh_runtime_seconds', 'resident_runtime_seconds', 'runtime_ratio',
'fresh_trial_wall_seconds', 'resident_trial_wall_seconds', 'trial_wall_ratio',
'fresh_simulation_seconds', 'resident_simulation_seconds')}
cold_E = cpu['full_E']['cold_fixed_first']
warm_E = [r for r in cpu['full_E']['resident']
if r['round_index'] == 1 and r['fixture_id'] == cold_E['fixture_id']]
require(len(warm_E) == 1, 'Missing same-fixture E timing')
e_pair = dict(fixture_id=cold_E['fixture_id'],
cold_API_seconds=cold_E['original_api_seconds'], warm_API_seconds=warm_E[0]['original_api_seconds'],
API_ratio=cold_E['original_api_seconds'] / warm_E[0]['original_api_seconds'],
sample_warning='One fixed-first cold sample; not a 14-fixture matched cold E study')
commit = subprocess.check_output(['git', '-C', str(WORKTREE), 'rev-parse', 'HEAD'], text=True).strip()
git_status = subprocess.check_output(['git', '-C', str(WORKTREE), 'status', '--porcelain'], text=True)
require(not git_status, 'Candidate worktree must be committed before finalization')
result = dict(status='PASS-PERSISTENT-REPLAY-EQUIVALENCE', created_utc=datetime.now(timezone.utc).isoformat(),
source_commit=commit, registration_sha256=cpu['registration_sha256'],
original_source_commit=cpu['original_source_commit'], fixture_manifest_sha256=cpu['fixture_manifest_sha256'],
jobs=jobs, cpu_summary=cpu['summary'], full_E_same_fixture_API_comparison=e_pair,
brain_matched_pairs=paired, brain_steady_13_fixture_summary=brain_timing,
first_brain_fixture=paired[0],
full_brain_regression=dict(fresh=14, resident_forward=14, resident_reverse=14,
arrays_per_trial=10, all_bitwise_exact=True, actual_child_exits_observed_zero=15),
cpu_regression=dict(cold_U=14, resident_update_plus_independent_verification=42,
full_E_cold=1, full_E_resident=28, full_AQ_on_new_E_outputs=14, host_tests=17),
integrity=after, scientific_AR_resumed=False, training_epochs_added=0, probes_added=0,
production_runtime_promoted=False, scientific_learning_success_claimed=False,
limitations=[
'Fixed 14 real committed-prefix fixtures only; not the full scientific design.',
'Fresh means fresh interpreter, not flushed filesystem caches.',
'Brain uses unchanged original 4090 runtime; whole-brain V100 compatibility remains unestablished.',
'Original GPU tensors/graph are still allocated and uploaded each trial; no numerical loop rewrite.',
'Brain runtime timings include its original output I/O. Trial wall also includes extra audit checks.',
'Resident trial audit additionally checks the current fresh reference, making trial-wall boundaries unequal.',
'Paired steady timing excludes fixed fixture 0 cold initialization in both lanes; correctness excludes none.',
'Independent measured stages are not a combined training pipeline; no end-to-end speedup or revised full-run ETA.',
'Full independent AQ still uses a fresh subprocess and all original checks.',
'The original stopped scientific run remains at 1033 CPU-verified epochs and 138 probes.',
'Integrity covers saved Phase4AR files/sources and previous acceleration files; not a new Phase1-4AQ numerical regression.',
'Expected timeout self-test intentionally exits nonzero; it is not a scientific-run failure.'
])
save(ROOT / 'final-summary.json', result)
s = cpu['summary']
b = brain_timing
report = f'''# 常驻进程工程回放结果
状态:`PASS-PERSISTENT-REPLAY-EQUIVALENCE`。仅限这次固定回放,不代表学习成功或正式运行时升级。
## 实测
| 测量范围 | 新进程 | 常驻进程 | 说明 |
|---|---:|---:|---|
| 原始 CPU 更新 API | {s['cold_U_API_seconds']['median']:.4f} s | {s['resident_warm_U_API_seconds']['median']:.4f} s | 新进程首次 API 包含导入等开销 |
| 完整更新 API,同一固定案例 | {e_pair['cold_API_seconds']:.4f} s | {e_pair['warm_API_seconds']:.4f} s | 仅一组冷启动对照 |
| 全脑原始运行函数,13 组配对中位数 | {b['fresh_runtime_seconds']['median']:.4f} s | {b['resident_runtime_seconds']['median']:.4f} s | 含原有输出;排除固定首例的初始化 |
完整更新常驻 14 组中位耗时:{s['original_full_E_warm_roundtrip_seconds']['median']:.4f} s。
完整独立验证仍需:{s['original_full_AQ_subprocess_wall_seconds']['median']:.4f} s(14 组中位数)。
全脑运行函数同案例配对倍率中位数:{b['runtime_ratio']['median']:.3f}×,不是整轮训练加速倍率。
## 一致性与保护
- 17 项主机测试通过。
- CPU:14 次冷启动更新、42 次独立双进程更新/重算、29 次完整更新、14 次完整独立验证通过。
- 全脑:14 次新进程、正序 14 次与逆序 14 次常驻回放;所有脉冲、状态、延迟队列和请求等一致,10 个数组逐位相同。
- 64,034 个原实验文件与 16 个注册源码重新校验通过;之前 659 个加速产物和 13 个源码保持不变。
- 原 Phase4AR 仍停止于 1,033 轮 CPU 校验训练与 138 次 probe,没有新增训练或安装新权重。
## 边界与下一步
本次全脑测试使用原 RTX 4090 环境,没有证明全脑已能迁移到 V100。此前 V100 约 3.4 ms 的成绩仅属于 eligibility/update 模块。
常驻进程复用的是解释器、模块和 CUDA 上下文;每轮神经状态仍由原始函数重新分配。
当前仍保留完整验证、图和权重上传、记录写盘。下一步应在新的可审计流水线中验证整轮开销,不能把本报告的独立阶段相加当作实测训练速度。
源码提交:`{commit}`。完整逐例耗时、实际进程退出、来源校验见 `final-summary.json` 和各 lane 报告。
'''
with (ROOT / 'RESULTS.md').open('x') as stream:
stream.write(report)
files = {str(path.relative_to(ROOT)): dict(bytes=path.stat().st_size, sha256=sha(path))
for path in sorted(ROOT.rglob('*')) if path.is_file()}
source_files = {path.name: sha(path) for path in sorted(SOURCE.iterdir()) if path.is_file()}
manifest = dict(schema='conditioning-persistent-replay-artifacts-v1', source_commit=commit,
root=str(ROOT), files=files, source_files=source_files,
self_hash_policy='This manifest excludes itself and SHA256SUMS; SHA256SUMS includes the manifest.')
save(ROOT / 'artifact-manifest.json', manifest)
with (ROOT / 'SHA256SUMS').open('x') as stream:
for rel, item in files.items():
stream.write(item['sha256'] + ' ' + rel + '\n')
stream.write(sha(ROOT / 'artifact-manifest.json') + ' artifact-manifest.json\n')
print(json.dumps(dict(status=result['status'], files=len(files), source_commit=commit,
total_bytes=sum(v['bytes'] for v in files.values()), brain=brain_timing, CPU=cpu['summary']), indent=2))
if __name__ == '__main__':
main()