"""Check the restored L4 prediction and unchanged sequential L18 gradients.""" import gc import json from pathlib import Path import sys import time import torch ROOT = Path(__file__).resolve().parents[1] LOCAL = ROOT / '.release_local' sys.path[:0] = [str(ROOT / 'src'), str(ROOT.parent / 'openpi/src'), str(ROOT.parent / 'openpi/packages/openpi-client/src')] from robottt_pi05.config import ExperimentConfig from robottt_pi05.modeling import create_adapter from robottt_pi05.openpi_backend import load_model from robottt_pi05.assets import FrozenPrefixStore, ProcessedSequenceSource from robottt_pi05.checkpoint import load_weights, load_initial from robottt_pi05.engine import generate_chunk, train_sequence from robottt_pi05.eval_io import sha def main(): torch.set_num_threads(4) torch.backends.cuda.matmul.allow_tf32 = True torch.backends.cudnn.allow_tf32 = True run = json.loads((LOCAL / 'legacy-ft-l18-learned.json').read_text()) paths = {k: Path(v) for k, v in run['paths'].items()} manifest = json.loads(paths['asset_index'].read_text()) model = load_model(source=paths['openpi_source'], checkpoint=paths['model_checkpoint'], profile=run['model_profile']) bundle = LOCAL / 'legacy-l4-handoff/bundle' bank = torch.load(paths['bank'], map_location='cpu', weights_only=True) store = FrozenPrefixStore(manifest['prefixes'], root=paths['prefix_root'], allowed_episodes=manifest['train_ids']+manifest['dev_ids']) config4 = ExperimentConfig.load(ROOT / 'configs/l4_learned_s4.json') adapter, slow = create_adapter(model, config4, inference=True) adapter.minimal_diagnostics = True trained = bundle / 'learned_inner_lr_v1/runs/LEARN/step_001024.pt' load_weights(trained, slow, config4, expected_sha256=sha(trained)) prefix = store.get(249, 0) expected = torch.load(bundle / 'reference/LEARN-P_e000249_n0/chunk_00.pt', map_location='cpu', weights_only=True) prediction, _, _ = generate_chunk(adapter, prefix, bank['evaluation_noise'][0, :, 0].cuda(), adapter.initial_state(1)) difference = float((prediction.cpu()-expected['prediction']).abs().max()) report = dict(original_L4_prediction_max_abs_difference=difference, original_L4_prediction_exact=torch.equal(prediction.cpu(), expected['prediction'])) print('L4_REFERENCE', json.dumps(report), flush=True) torch.testing.assert_close(prediction.cpu(), expected['prediction'], rtol=1e-5, atol=1e-6) del adapter, slow, prediction, prefix gc.collect(); torch.cuda.empty_cache() config = ExperimentConfig.load(paths['experiment_config']) adapter, slow = create_adapter(model, config) load_initial(paths['initial_extension'], slow, expected_sha256=run['hashes']['initial_extension']) schedule = json.loads(paths['schedule'].read_text())[0] prepared = store.sequence(schedule['episode'], schedule['source_rows']) source = ProcessedSequenceSource(manifest['episodes'], root=paths['data_root'], allowed_episodes=manifest['train_ids'], stride=10) actions = source.targets(schedule['episode'], schedule['start']).cuda() epsilon = bank['training_epsilon'][0].cuda(); times = bank['training_time'][0].cuda() gradients = {}; losses = {}; seconds = {} for name, minimal in [('audited', False), ('minimal', True)]: slow.zero_grad(set_to_none=True) adapter.minimal_diagnostics = minimal adapter.use_batched_segments = False adapter.history.clear(); adapter.call_contracts.clear() begin = time.monotonic() result = train_sequence(adapter, config, prepared, actions, epsilon, times, backward=lambda loss: loss.backward()) torch.cuda.synchronize() seconds[name] = time.monotonic()-begin losses[name] = result['loss'] gradients[name] = {k: p.grad.detach().cpu().clone() for k, p in slow.named_parameters() if p.requires_grad} del result gc.collect() exact = all(torch.equal(value, gradients['minimal'][key]) for key, value in gradients['audited'].items()) maximum = max(float((value-gradients['minimal'][key]).abs().max()) for key, value in gradients['audited'].items()) report.update(losses=losses, seconds=seconds, gradient_exact=exact, gradient_max_abs_difference=maximum, trainable_parameters=sum(p.numel() for p in slow.parameters() if p.requires_grad), sequential_timesteps=True) assert losses['audited'] == losses['minimal'] and exact, report assert all(p.grad is None for p in model.parameters()) (LOCAL / 'audit/legacy-l18-runtime-validation.json').write_text(json.dumps(report, indent=2)) print('LEGACY_L18_VALIDATED', json.dumps(report), flush=True) if __name__ == '__main__': main()