File size: 2,851 Bytes
f947839
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""CF-scoped checkpoint cadence, without changing the frozen SF integration."""
import json
import os
from pathlib import Path


def patched_training_source(source):
    from baselines.astrolabe.run import training_source
    text=training_source(source)
    # Disable the upstream round-based callbacks. Full VBench is scheduled by
    # actual completed optimizer updates in a separate PBS allocation.
    for condition in (
        'if i == 0 and epoch % config.eval_freq == 0 and not config.debug:',
        'if i == 0 and epoch % config.save_freq == 0 and is_main_process(rank) and not config.debug:'):
        assert text.count(condition)==1
        text=text.replace(condition,'if False:  # CF supervisor owns evaluation and checkpoint cadence')
    marker='                        global_step += 1\n'
    assert text.count(marker)==1
    cadence=int(json.loads(Path(os.environ['AFD_ASTROLABE_MANIFEST']).read_text())['baseline']['checkpoint_every_optimizer_steps'])
    text=text.replace(marker,marker+
        f'                        if global_step % {cadence} == 0:\n'
        '                            save_ckpt(config.save_dir, transformer_ddp, global_step, rank, ema,\n'
        '                                      transformer_trainable_parameters, config, optimizer, scaler)\n'
        '                            dist.barrier()\n')
    # A ready marker appears only after all files have been closed. Avoid
    # rewriting the final checkpoint while an evaluation is already loading it.
    definition='def main(_):\n'
    assert text.count(definition)==1
    wrapper='''
_afd_save_ckpt = save_ckpt
def save_ckpt(save_dir, transformer_ddp, global_step, rank, ema, parameters, config, optimizer, scaler):
    marker = os.path.join(save_dir, 'checkpoints', f'checkpoint-{global_step}', 'ready.json')
    if os.path.exists(marker):
        return
    _afd_save_ckpt(save_dir, transformer_ddp, global_step, rank, ema, parameters, config, optimizer, scaler)
    if is_main_process(rank):
        import json as afd_json
        temporary = marker + '.tmp'
        with open(temporary, 'w') as handle:
            afd_json.dump({'step':global_step, 'generator_family':'causal_forcing', 'complete':True}, handle)
        os.replace(temporary, marker)

'''
    return text.replace(definition,wrapper+definition)


def main():
    # The original worker asks run.training_source at runtime. Retain a reference
    # for the patch above to avoid recursion after installing the override.
    from baselines.astrolabe import run, worker
    original=run.training_source
    def patched(source):
        run.training_source=original
        try:
            return patched_training_source(source)
        finally:
            run.training_source=patched
    run.training_source=patched
    worker.main()


if __name__=='__main__':
    main()