File size: 6,277 Bytes
c6a1af0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Appendix tables of the two-video choice and the exact counts quoted in the Results (run from the repo root through
Slurm: paper/scripts/make_result_tables.slurm). Reads the same runs as make_figures.py.

Per pair: b = (gA+gB)/2, s = (gA-gB)/2 signed to the changed video. ans: right in both orders (s > |b|, chance 25%);
score: s > 0 (chance 50%); single: right in one order, averaged over the two orders (chance 50%).
Writes tables/tab_app_pooled.tex (all questions, pooled over 1-5 objects) and tables/tab_app_k.tex (scores per number
of objects), and prints the counts and the binomial thresholds."""
import collections
import json
import math
from pathlib import Path

RUNS = Path('occlusion_memory_benchmark/memory_v2/dataset_sizes/vlm_runs')
TAB = Path('paper/tables')
MODELS = [('Qwen3-VL', [('2B', 'qwen3_vl_2b'), ('8B', 'qwen3_vl_8b'), ('30B-A3B', 'qwen3_vl_30b_a3b'), ('32B', 'qwen3_vl_32b')]),
          ('InternVL3.5', [('2B', 'internvl35_2b'), ('8B', 'internvl35_8b'), ('38B', 'internvl35_38b')])]


def threshold(n, p0, alpha=0.05):
    """smallest count c with P(X >= c) < alpha for X ~ Binomial(n, p0) (one-sided test)"""
    tail = 0.0
    for c in range(n, -1, -1):
        tail += math.comb(n, c) * p0 ** c * (1 - p0) ** (n - c)
        if tail >= alpha:
            return c + 1


def load(tag):
    g = collections.defaultdict(list)
    for line in open(RUNS / f'pairs-vllm-{tag}' / 'pairs.jsonl'):
        r = json.loads(line)
        key = (int(r['layout'][1]), r['occluders'], r['path'], r['history'], r['property'], tuple(sorted(r['videos'])))
        for a in r['answers']:
            s = {c: x['sum'] for c, x in zip(a['candidates'], a['scores'])}
            g[key + (a['q'],)].append((s['Video 1'] - s['Video 2'], a['truth']))
    rows = []
    for k, d in g.items():
        assert len(d) == 2, k
        (g1, t1), (g2, t2) = d
        b = (g1 + g2) / 2; s = ((g1 if t1 == 'Video 1' else -g1) + (g2 if t2 == 'Video 1' else -g2)) / 2
        rows.append(dict(k=k[0], blocks=k[1], path=k[2], prop=k[4], q=k[6], b=b, s=s))
    return rows


def counts(rs):
    return (sum(r['s'] > abs(r['b']) for r in rs), sum(r['s'] > 0 for r in rs),
            sum((r['b'] + r['s'] > 0) + (r['s'] - r['b'] > 0) for r in rs) / 2, len(rs))


# column separators of the full-width appendix tables (about 6.5in wide at \small): label column to numbers, and
# between blocks of columns (inside a block the separation is 2\tabcolsep)
LABELSEP, BLOCKSEP = '@{\\hspace{8pt}}', '@{\\hspace{14pt}}'


def pct(c, n):
    return f'{100 * c / n:.1f}'


# questions: (label, question asked, change shown)
QS = [('colours', 'colors', 'colors'), ('places', 'places', 'places'), ('both', 'both', 'both'),
      ('any, col.', 'change', 'colors'), ('any, pl.', 'change', 'places')]


def main():
    data = {(fam, size): load(tag) for fam, ms in MODELS for size, tag in ms}
    sel = lambda rs, **kw: [r for r in rs if all(r[x] == v for x, v in kw.items())]
    for n in (24, 60, 120):
        print(f'thresholds n={n}: ans >= {threshold(n, .25)}, score/single >= {threshold(n, .5)}')
    print('\nexact counts, pooled (ans, score, single, n):')
    for (fam, size), rs in data.items():
        line = []
        for bl in ('on', 'off'):
            for lab, q, prop in QS:
                a, s, si, n = counts(sel(rs, blocks=bl, q=q, prop=prop)); line.append(f'{bl}:{lab} {a}/{s}/{si:g}/{n}')
        print(f'{fam} {size}: ' + '; '.join(line))
    print('\nhidden, per path (ans, score, n):')
    for (fam, size), rs in data.items():
        line = []
        for q in ('colors', 'places'):
            for path in ('orbit', 'translation'):
                a, s, si, n = counts(sel(rs, blocks='on', q=q, prop=q, path=path)); line.append(f'{q}-{path} {a}/{s}/{n}')
        print(f'{fam} {size}: ' + '; '.join(line))

    # pooled table: one block per block setting, cells ans / score / single
    hdr = ' & ' + ' & '.join(f'\\multicolumn{{3}}{{c}}{{{lab}}}' for lab, _, _ in QS) + ' \\\\'
    cm = ''.join(f'\\cmidrule(lr){{{2 + 3 * i}-{4 + 3 * i}}}' for i in range(len(QS)))
    sub = 'model & ' + ' & '.join(['a & s & 1'] * len(QS)) + ' \\\\'
    # full width (table*, \small, \tabcolsep=3.2pt): BLOCKSEP between the question blocks, so the blocks read apart
    out = ['\\begin{tabular}{@{}l' + LABELSEP + BLOCKSEP.join(['rrr'] * len(QS)) + '@{}}', '\\toprule', hdr, cm, sub]
    for bl, name in (('on', 'hidden (blocks on)'), ('off', 'visible (blocks off)')):
        out += ['\\midrule', f'\\multicolumn{{{1 + 3 * len(QS)}}}{{@{{}}l}}{{\\emph{{{name}}}}} \\\\']
        for (fam, size), rs in data.items():
            cells = []
            for lab, q, prop in QS:
                a, s, si, n = counts(sel(rs, blocks=bl, q=q, prop=prop)); cells += [pct(a, n), pct(s, n), pct(si, n)]
            out.append(f"{'Qwen' if fam == 'Qwen3-VL' else 'InternVL'} {size} & " + ' & '.join(cells) + ' \\\\')
    out += ['\\bottomrule', '\\end{tabular}']
    (TAB / 'tab_app_pooled.tex').write_text('\n'.join(out) + '\n')

    # scores per number of objects
    qk = [('colours', 'colors'), ('places', 'places'), ('both', 'both')]
    out = ['\\begin{tabular}{@{}l' + LABELSEP + BLOCKSEP.join(['rrrrr'] * len(qk)) + '@{}}', '\\toprule',
           ' & ' + ' & '.join(f'\\multicolumn{{5}}{{c}}{{{lab}}}' for lab, _ in qk) + ' \\\\',
           ''.join(f'\\cmidrule(lr){{{2 + 5 * i}-{6 + 5 * i}}}' for i in range(len(qk))),
           'objects & ' + ' & '.join(['1 & 2 & 3 & 4 & 5'] * len(qk)) + ' \\\\']
    for bl, name in (('on', 'hidden (blocks on)'), ('off', 'visible (blocks off)')):
        out += ['\\midrule', f'\\multicolumn{{{1 + 5 * len(qk)}}}{{@{{}}l}}{{\\emph{{{name}}}}} \\\\']
        for (fam, size), rs in data.items():
            cells = []
            for lab, q in qk:
                for k in range(1, 6):
                    a, s, si, n = counts(sel(rs, blocks=bl, q=q, prop=q, k=k)); cells.append(pct(s, n))
            out.append(f"{'Qwen' if fam == 'Qwen3-VL' else 'InternVL'} {size} & " + ' & '.join(cells) + ' \\\\')
    out += ['\\bottomrule', '\\end{tabular}']
    (TAB / 'tab_app_k.tex').write_text('\n'.join(out) + '\n')
    print('\nwrote tables/tab_app_pooled.tex, tables/tab_app_k.tex')


if __name__ == '__main__':
    main()