File size: 12,561 Bytes
5ccb4fd 02f6649 5ccb4fd 02f6649 5ccb4fd 02f6649 5ccb4fd 02f6649 5ccb4fd 02f6649 5ccb4fd 02f6649 5ccb4fd 02f6649 5ccb4fd 02f6649 5ccb4fd 02f6649 5ccb4fd 02f6649 5ccb4fd 02f6649 5ccb4fd | 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | # Copyright (c) 2026 Simulacra Research Inc.
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
import functools
import time
from dataclasses import dataclass
from typing import Callable
import equinox as eqx
import jax
import jax.numpy as jnp
import numpy as np
from jax.sharding import Mesh, NamedSharding, PartitionSpec as P
from hamiltonzero.checkpoint import load_mcmc, load_model, save_model
from hamiltonzero.compiled.model import (
CompiledFinetuneWaveFunction,
compile_finetune_model,
)
from hamiltonzero.config import FineTuneConfig
from hamiltonzero.data import build_context_and_energy, load_system
from hamiltonzero.energy import vmc_energy_custom_lap_finetune
from hamiltonzero.energy.frame import compile_energy_frame
from hamiltonzero.mcmc import (
REState,
adapt_batched,
cold_samples,
init_batched_state,
run_batched,
)
from hamiltonzero.model import build_model
from hamiltonzero.optim import (
KFACBundle,
apply_finetune_kfac_step,
init_finetune_kfac_state,
learning_rate,
process_finetune_targets,
)
from hamiltonzero.router import (
batch_context,
route_context,
route_state,
select_frozen_route,
strip_router,
)
@dataclass(frozen=True, slots=True)
class FineTuneMetric:
step: int
energy: float
energy_std: float
step_walltime: float
walltime: float
@dataclass(frozen=True, slots=True)
class FineTuneResult:
model: CompiledFinetuneWaveFunction
route_perm: jax.Array
mcmc_state: REState
kfac: KFACBundle
last_metric: FineTuneMetric | None
def _adapt(state: REState, config: FineTuneConfig) -> REState:
return adapt_batched(
state,
beta_history_weight=config.mcmc.beta_history_weight,
sigma_target=config.mcmc.langevin_target_acceptance,
sigma_scale=config.mcmc.sigma_scale,
haar_target=config.mcmc.haar_target_acceptance,
)
def _burn_in(
model: CompiledFinetuneWaveFunction,
context,
state: REState,
config: FineTuneConfig,
step_mcmc,
adapt,
) -> REState:
for iteration in range(config.mcmc.burn_in):
state = step_mcmc(
state,
model,
context,
)
if iteration > 0 and iteration % config.mcmc.adapt_every == 0:
state = adapt(state)
return state
def _replicate(value, sharding: NamedSharding):
return jax.device_put(
value,
jax.tree_util.tree_map(lambda _leaf: sharding, value),
)
def _place_state(state: REState, mesh: Mesh) -> REState:
return jax.device_put(state, _state_sharding(mesh))
def _state_specs() -> REState:
walkers = P("batch")
replicated = P()
return REState(
q=walkers,
log_p=walkers,
grad_log_p=walkers,
beta=replicated,
sigma=replicated,
step=replicated,
key=walkers,
n_local_accept=walkers,
n_local=walkers,
n_swap_accept=walkers,
n_swap=walkers,
mask=replicated,
m=replicated,
n_haar_accept=walkers,
n_haar=walkers,
)
def _state_sharding(mesh: Mesh) -> REState:
replicated = NamedSharding(mesh, P())
batched = NamedSharding(mesh, P("batch"))
return REState(
q=batched,
log_p=batched,
grad_log_p=batched,
beta=replicated,
sigma=replicated,
step=replicated,
key=batched,
n_local_accept=batched,
n_local=batched,
n_swap_accept=batched,
n_swap=batched,
mask=replicated,
m=replicated,
n_haar_accept=batched,
n_haar=batched,
)
def _build_mcmc_entry(
mesh: Mesh,
state_sharding,
model_sharding,
context_sharding,
*,
replica_steps: int,
walker_chunk_size: int | None,
):
specs = _state_specs()
def local_step(state, model, context):
out = run_batched(
model,
context,
state,
n_steps=int(replica_steps),
walker_chunk_size=walker_chunk_size,
)
local_count = jnp.asarray(out.q.shape[0], dtype=jnp.int32)
global_count = jax.lax.psum(local_count, "batch")
guard = global_count.astype(out.q.dtype) * jnp.asarray(0.0, out.q.dtype)
return eqx.tree_at(lambda value: value.q, out, out.q + guard)
mapped = jax.shard_map(
local_step,
mesh=mesh,
in_specs=(specs, P(), P()),
out_specs=specs,
check_vma=False,
)
return jax.jit(
mapped,
in_shardings=(state_sharding, model_sharding, context_sharding),
out_shardings=state_sharding,
donate_argnums=(0,),
)
def _build_energy_entry(
mesh: Mesh,
model_sharding,
frame_sharding,
*,
chunk_size: int,
):
q_spec = P("batch", None, None)
output_spec = P("batch")
def local_energy(model, frame, q):
outputs = vmc_energy_custom_lap_finetune(
model,
frame,
q,
chunk_size=int(chunk_size),
)
local_count = jnp.asarray(q.shape[0], dtype=jnp.int32)
global_count = jax.lax.psum(local_count, "batch")
guard = global_count.astype(jnp.float32) * jnp.asarray(0.0, jnp.float32)
return tuple(value + guard.astype(value.dtype) for value in outputs)
mapped = jax.shard_map(
local_energy,
mesh=mesh,
in_specs=(P(), P(), q_spec),
out_specs=(output_spec,) * 4,
check_vma=False,
)
q_sharding = NamedSharding(mesh, q_spec)
output_sharding = NamedSharding(mesh, output_spec)
return jax.jit(
mapped,
in_shardings=(model_sharding, frame_sharding, q_sharding),
out_shardings=(output_sharding,) * 4,
)
def _metric(
step: int,
total,
step_started: float,
run_started: float,
) -> FineTuneMetric:
jax.block_until_ready(total)
return FineTuneMetric(
step=step,
energy=float(jax.device_get(jnp.mean(total.real))),
energy_std=float(jax.device_get(jnp.std(total.real))),
step_walltime=time.perf_counter() - step_started,
walltime=time.perf_counter() - run_started,
)
def run_finetune(
config: FineTuneConfig,
*,
metric_sink: Callable[[FineTuneMetric], None] | None = None,
) -> FineTuneResult:
key = jax.random.PRNGKey(config.seed)
key_model, key_mcmc = jax.random.split(key)
system = load_system(config.system)
context, energy_inputs = build_context_and_energy(
system,
n_max=None,
mu=config.energy.mu,
eps=config.energy.eps,
)
template = build_model(
config.model,
key_model,
n_max=int(context.mask.shape[-1]),
)
eager_model = load_model(config.checkpoint, template)
state = init_batched_state(
key_mcmc,
context,
batch_size=config.mcmc.batch_size,
n_replicas=config.mcmc.replicas,
initial_m=config.mcmc.initial_haar_sites,
initial_sigma=config.mcmc.initial_sigma,
)
if config.mcmc.reuse_mcmc is not None:
state = load_mcmc(config.mcmc.reuse_mcmc, state)
key, _route_key = jax.random.split(key)
freeze_route = eqx.filter_jit(
functools.partial(
select_frozen_route,
tau=config.route_temperature,
)
)
route_perm = freeze_route(
eager_model,
context,
)
energy_frame = compile_energy_frame(
energy_inputs,
context.mask,
context.bmask,
route_perm,
)
context = route_context(context, route_perm)
state = route_state(state, route_perm)
eager_model = strip_router(eager_model)
key, key_expand = jax.random.split(key)
compile_model = eqx.filter_jit(
functools.partial(
compile_finetune_model,
leaf_rank=config.leaf_rank,
merge_rank=config.merge_rank,
)
)
model = compile_model(
eager_model,
context,
physical_perm=route_perm,
key=key_expand,
)
del eager_model, template, _route_key
devices = tuple(jax.devices())
if config.mcmc.batch_size % len(devices):
raise ValueError(
f"batch_size={config.mcmc.batch_size} must be divisible by "
f"the {len(devices)} visible devices"
)
mesh = Mesh(np.asarray(devices, dtype=object), ("batch",))
replicated = NamedSharding(mesh, P())
state_sharding = _state_sharding(mesh)
model_sharding = jax.tree_util.tree_map(lambda _value: replicated, model)
context_sharding = jax.tree_util.tree_map(lambda _value: replicated, context)
frame_sharding = jax.tree_util.tree_map(lambda _value: replicated, energy_frame)
model = _replicate(model, replicated)
context = _replicate(context, replicated)
energy_frame = _replicate(energy_frame, replicated)
state = _place_state(state, mesh)
context_batch = _replicate(batch_context(context), replicated)
q_cold = cold_samples(state)
kfac_data = NamedSharding(mesh, P(None, "batch"))
q_kfac = jax.device_put(q_cold[None], kfac_data)
energy_seed = jax.device_put(
jnp.zeros((1, config.mcmc.batch_size), dtype=jnp.complex64),
kfac_data,
)
mcmc_entries = {}
def mcmc_entry(replica_steps: int):
entry_key = (int(replica_steps), config.mcmc.walker_chunk_size)
entry = mcmc_entries.get(entry_key)
if entry is None:
entry = _build_mcmc_entry(
mesh,
state_sharding,
model_sharding,
context_sharding,
replica_steps=entry_key[0],
walker_chunk_size=entry_key[1],
)
mcmc_entries[entry_key] = entry
return entry
adapt = jax.jit(
functools.partial(_adapt, config=config),
in_shardings=(state_sharding,),
out_shardings=state_sharding,
)
local_energy = _build_energy_entry(
mesh,
model_sharding,
frame_sharding,
chunk_size=config.energy.chunk_size,
)
target_entry = jax.jit(
functools.partial(
process_finetune_targets,
mad_width=config.kfac.mad_clip_width,
),
in_shardings=(kfac_data, replicated),
out_shardings=kfac_data,
)
kfac = init_finetune_kfac_state(
config.kfac,
model,
q_kfac,
energy_seed,
context_batch,
t=0.0,
key=jax.random.fold_in(key, 0xCAFE),
multi_device=mesh.size > 1,
)
state = _burn_in(
model,
context,
state,
config,
mcmc_entry(config.mcmc.burn_in_replica_steps),
adapt,
)
step_mcmc = mcmc_entry(config.mcmc.steps)
run_started = time.perf_counter()
last_metric = None
for step in range(config.steps):
step_started = time.perf_counter()
state = step_mcmc(
state,
model,
context,
)
if step > 0 and step % config.mcmc.adapt_every == 0:
state = adapt(state)
q_cold = cold_samples(state)
total, _exchange, _casimir, _field = local_energy(
model,
energy_frame,
q_cold,
)
target = target_entry(
total[None],
context_batch.s_norm,
)
key, key_kfac = jax.random.split(key)
model, kfac = apply_finetune_kfac_step(
kfac,
model,
jax.device_put(q_cold[None], kfac_data),
jax.device_put(target, kfac_data),
context_batch,
t=0.0,
key=key_kfac,
momentum=config.kfac.momentum,
learning_rate=learning_rate(config.kfac, step),
damping=config.kfac.damping,
)
jax.block_until_ready(model)
last_metric = _metric(step, total, step_started, run_started)
if metric_sink is not None:
metric_sink(last_metric)
save_model(
config.output,
model,
kind="compiled_finetune",
metadata={
"leaf_rank": int(config.leaf_rank),
"merge_rank": int(config.merge_rank),
"n_max": int(context.mask.shape[-1]),
},
)
return FineTuneResult(
model=model,
route_perm=route_perm,
mcmc_state=state,
kfac=kfac,
last_metric=last_metric,
)
__all__ = [
"FineTuneMetric",
"FineTuneResult",
"run_finetune",
]
|