To use a pre-trained Latent Program Network (LPN), you can download artifacts from Weights & Biases (WandB). The process involves:
- Initializing a WandB run.
- Downloading the model artifact.
- Extracting the configuration from the artifact metadata.
- Instantiating the
EncoderTransformer, DecoderTransformer, and the LPN model. - Loading the trained parameters from the
state.msgpack file into a Flax TrainState.
Note: This requires omegaconf, hydra, jax, flax, and wandb to be installed.
import os
import jax
import jax.numpy as jnp
import wandb
import omegaconf
import hydra
from flax.training.train_state import TrainState
from flax.serialization import from_bytes
from lpn.src.models.lpn import LPN
from lpn.src.models.transformer import EncoderTransformer, DecoderTransformer
# 1. Download artifact
artifact_path = "TheThinker/ARC/quiet-thunder-789--checkpoint:v0"
run = wandb.init()
artifact = run.use_artifact(artifact_path, type='model')
run.finish()
cfg = omegaconf.OmegaConf.create(artifact.metadata)
artifact_dir = artifact.download()
# 2. Instantiate model components
encoder = EncoderTransformer(hydra.utils.instantiate(cfg.encoder_transformer))
decoder = DecoderTransformer(hydra.utils.instantiate(cfg.decoder_transformer))
lpn = LPN(encoder=encoder, decoder=decoder)
# 3. Initialize state for loading (requires dummy inputs matching model config)
key = jax.random.PRNGKey(0)
grids = jax.random.randint(key, (1, 3, decoder.config.max_rows, decoder.config.max_cols, 2), minval=0, maxval=decoder.config.vocab_size)
shapes = jax.random.randint(key, (1, 3, 2, 2), minval=1, maxval=min(decoder.config.max_rows, decoder.config.max_cols) + 1)
variables = lpn.init(key, grids, shapes, dropout_eval=False, prior_kl_coeff=0.0, pairwise_kl_coeff=0.0, mode="mean")
# 4. Setup optimizer and TrainState
optimizer = optax.adamw(0.0)
train_state = TrainState.create(apply_fn=lpn.apply, tx=optimizer, params=variables["params"])
# 5. Load weights
with open(os.path.join(artifact_dir, "state.msgpack"), "rb") as data_file:
byte_data = data_file.read()
loaded_state = from_bytes(train_state, byte_data)