File size: 6,169 Bytes
bce3b06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Generates training data from the states the model's *own* policy visits.

## Why

The model is trained only on states drawn from `gen_data.py`'s sampler, each
paired with one Kociemba solution. It is never trained on the states its own
attempts produce -- and measurement shows those are exactly where it is worst.
Tracking distance-to-solved across rounds of interactive solving, from a state 10
moves from solved:

    10 -> 16 -> 17 -> 15 -> 17 -> 17 -> 18 -> 17 -> 18

It walks away from solved and stays there. Those visited states are off the
training distribution, so the model has never been shown what to do in them.

This is textbook exposure bias, and the fix is DAgger's: run the current policy,
collect the states it actually reaches, label those with the expert (Kociemba),
and train on them alongside the original data. Behaviour cloning alone compounds
error quadratically in horizon length; adding the policy's own state
distribution brings it back to linear.

Three other explanations have already been tested and eliminated -- more
parameters, more layers at matched parameters, and interactive chunking all
changed nothing -- which is what makes this the remaining candidate rather than
one option among several.

## How

Rollouts are batched on the GPU: a batch of states is decoded together, the first
`--chunk` moves of each proposal are applied, and the resulting states are
recorded. Labelling is the slow part and runs across all cores, exactly as in
gen_data.py.

    python gen_rollout_data.py --checkpoint <hub-id-or-dir> --count 2000000 \\
        --out data/rollout.jsonl
"""
import argparse, json, random, sys, time
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent))
from gen_data import (SOLVED, apply_sequence, random_state, effective_cpus,
                      check_kociemba_is_native)
import cube_tokenizer as T


def collect_states(model, device, n_states, chunk, rounds, batch_size, rng, deep_depth,
                   shallow_frac, max_shallow):
    """Rolls the policy out and returns the states it lands in.

    The start states are excluded: those are already in the training
    distribution. Only what the policy *reaches* is new information.
    """
    import torch
    from serve import _decode

    visited = []
    while len(visited) < n_states:
        batch = []
        for _ in range(batch_size):
            depth = rng.randint(1, max_shallow) if rng.random() < shallow_frac else deep_depth
            batch.append(random_state(rng, depth))

        for _ in range(rounds):
            live = [s for s in batch if s != SOLVED]
            if not live:
                break
            with torch.no_grad():
                prompts = torch.tensor([T.encode_state(s) for s in live], device=device)
                out = prompts
                finished = torch.zeros(len(live), dtype=torch.bool, device=device)
                for _ in range(T.MAX_SOLUTION + 1):
                    nxt = model(input_ids=out).logits[:, -1, :].argmax(-1)
                    nxt = torch.where(finished, torch.full_like(nxt, T.PAD), nxt)
                    finished |= nxt == T.EOS
                    out = torch.cat([out, nxt[:, None]], dim=1)
                    if bool(finished.all()):
                        break
                proposals = [T.decode_solution(r) for r in out[:, prompts.shape[1]:].tolist()]

            nxt_batch = []
            for state, moves in zip(live, proposals):
                if not moves:
                    continue
                landed = apply_sequence(state, moves[:chunk])
                if landed != SOLVED:
                    visited.append(landed)
                    nxt_batch.append(landed)
            batch = nxt_batch
            if not batch:
                break
        print(f"  collected {len(visited)}/{n_states}", file=sys.stderr, flush=True)
    return visited[:n_states]


def _label(state):
    import kociemba
    try:
        return state, kociemba.solve(state).split()
    except Exception:
        return state, None


def main():
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument("--checkpoint", required=True)
    p.add_argument("--count", type=int, default=2_000_000)
    p.add_argument("--chunk", type=int, default=8)
    p.add_argument("--rounds", type=int, default=4)
    p.add_argument("--batch-size", type=int, default=512)
    p.add_argument("--workers", type=int, default=0)
    p.add_argument("--seed", type=int, default=7)
    p.add_argument("--deep-depth", type=int, default=200)
    p.add_argument("--shallow-frac", type=float, default=0.15)
    p.add_argument("--max-shallow", type=int, default=8)
    p.add_argument("--out", required=True)
    args = p.parse_args()

    ms = check_kociemba_is_native()
    workers = args.workers or effective_cpus()
    print(f"kociemba {ms:.1f} ms/solve | workers {workers}", file=sys.stderr)

    import torch
    from transformers import LlamaForCausalLM
    device = "cuda" if torch.cuda.is_available() else "cpu"
    model = LlamaForCausalLM.from_pretrained(args.checkpoint).to(device).eval()
    print(f"rolling out {args.checkpoint} on {device}", file=sys.stderr)

    start = time.time()
    rng = random.Random(args.seed)
    states = collect_states(model, device, args.count, args.chunk, args.rounds,
                            args.batch_size, rng, args.deep_depth, args.shallow_frac,
                            args.max_shallow)
    print(f"collected {len(states)} states in {time.time()-start:.0f}s", file=sys.stderr)

    import multiprocessing as mp
    start = time.time()
    written = 0
    with mp.Pool(workers) as pool, open(args.out, "w") as fh:
        for state, moves in pool.imap_unordered(_label, states, chunksize=64):
            if not moves:
                continue
            fh.write(json.dumps({"state": state, "solution": " ".join(moves),
                                 "depth": len(moves)}) + "\n")
            written += 1
    secs = time.time() - start
    print(f"labelled and wrote {written} pairs in {secs:.0f}s ({written/max(secs,1e-9):.0f}/s)",
          file=sys.stderr)


if __name__ == "__main__":
    main()