File size: 3,666 Bytes
f74eea0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
set -euo pipefail

cd ~/LD-FPG-main/bbflow_base

echo "===== patch latent.py: add q_sample_temperature ====="

python - <<'PY'
from pathlib import Path

p = Path("bbflow/models/latent.py")
s = p.read_text()

old = '''        self.sample_coefficients = bool(getattr(latent_cfg, "sample_coefficients", False))
        self.energy_from_q = bool(getattr(latent_cfg, "energy_from_q", False))
        self.energy_from_q_full_cov = bool(getattr(latent_cfg, "energy_from_q_full_cov", False))
'''

new = '''        self.sample_coefficients = bool(getattr(latent_cfg, "sample_coefficients", False))
        self.q_sample_temperature = float(getattr(latent_cfg, "q_sample_temperature", 1.0))
        self.energy_from_q = bool(getattr(latent_cfg, "energy_from_q", False))
        self.energy_from_q_full_cov = bool(getattr(latent_cfg, "energy_from_q_full_cov", False))
'''

if old not in s:
    if "q_sample_temperature" in s:
        print("[patch] latent.py already has q_sample_temperature init")
    else:
        raise SystemExit("[ERROR] could not find init insertion point in latent.py")
else:
    s = s.replace(old, new)

old = '''        selected_mu = mu[batch, component]
        selected_scale = scale_tril[batch, component]
        eps = torch.randn_like(selected_mu)
        coeff = selected_mu + torch.einsum("bij,bj->bi", selected_scale, eps)
        return coeff.to(dtype=condition["u_global"].dtype), component
'''

new = '''        selected_mu = mu[batch, component]
        selected_scale = scale_tril[batch, component]
        temperature = float(getattr(self, "q_sample_temperature", 1.0))
        if temperature != 1.0:
            selected_scale = selected_scale * temperature
        eps = torch.randn_like(selected_mu)
        coeff = selected_mu + torch.einsum("bij,bj->bi", selected_scale, eps)
        return coeff.to(dtype=condition["u_global"].dtype), component
'''

if old not in s:
    if "q_sample_temperature" in s and "selected_scale = selected_scale * temperature" in s:
        print("[patch] latent.py already scales selected_scale by temperature")
    else:
        raise SystemExit("[ERROR] could not find sample_coefficients_from_q block in latent.py")
else:
    s = s.replace(old, new)

p.write_text(s)
print("[patch] latent.py OK")
PY

echo "===== patch reproduction/02_sample_test.sh ====="

python - <<'PY'
from pathlib import Path

p = Path("reproduction/02_sample_test.sh")
s = p.read_text()

old = '''INFERENCE_NUM_TIMESTEPS="${INFERENCE_NUM_TIMESTEPS:-20}"
'''

new = '''INFERENCE_NUM_TIMESTEPS="${INFERENCE_NUM_TIMESTEPS:-20}"
Q_SAMPLE_TEMPERATURE="${Q_SAMPLE_TEMPERATURE:-1.0}"
'''

if old not in s:
    if "Q_SAMPLE_TEMPERATURE" in s:
        print("[patch] 02_sample_test.sh already has Q_SAMPLE_TEMPERATURE env")
    else:
        raise SystemExit("[ERROR] could not find INFERENCE_NUM_TIMESTEPS block")
else:
    s = s.replace(old, new)

old = '''  "inference.interpolant.sampling.num_timesteps=${INFERENCE_NUM_TIMESTEPS}" \\
  "inference.log_times=true"
'''

new = '''  "inference.interpolant.sampling.num_timesteps=${INFERENCE_NUM_TIMESTEPS}" \\
  "+latent.q_sample_temperature=${Q_SAMPLE_TEMPERATURE}" \\
  "inference.log_times=true"
'''

if old not in s:
    if "+latent.q_sample_temperature" in s:
        print("[patch] 02_sample_test.sh already passes q temperature")
    else:
        raise SystemExit("[ERROR] could not find hydra override tail")
else:
    s = s.replace(old, new)

p.write_text(s)
print("[patch] reproduction/02_sample_test.sh OK")
PY

echo "===== verify patch ====="
grep -n "q_sample_temperature\\|Q_SAMPLE_TEMPERATURE" bbflow/models/latent.py reproduction/02_sample_test.sh