Skip to content

TD-MPC2

TD-MPC2 learns a model of the environment in a latent space and plans with it at every step. Its world model has no decoder: an encoder, latent dynamics, a reward predictor and an ensemble of Q-functions, trained together on short sub-trajectories from replay. To act, a sampling planner (MPPI) tries action sequences in the latent model, guided by a learned policy prior, and executes the first action of the best ones.

Paper: Hansen, Su and Wang, TD-MPC2: Scalable, Robust World Models for Continuous Control, ICLR 2024 (arXiv:2310.16828). Official code: nicklashansen/tdmpc2. Ajax reproduces the paper-era code (b67b21c, whose agent is identical to 5f6fade). The world-model design notes give the full specification, and deviations.md lists every version choice and every departure from the reference.

When to use it

TD-MPC2 is for continuous control. Planning with a learned model tends to need fewer environment steps than a model-free agent, at a much higher compute cost: it updates its model and plans at every step. World models compares it with DreamerV3.

Feature TDMPC2 TDMPC2MultiTask
Discrete actions no no
Continuous actions yes yes
Memory (memory=) no no
Environments gymnax, Brax, MuJoCo Playground trains on a dataset; evaluates on any of them
Observations flat vectors, raw (no normalisation) flat vectors of different sizes per task
Terminating episodes refused, unless episodic=True no
Several seeds in one call yes yes
Extension phases pretrain, post_update, eval_metrics, observe (which extensions run) the same but observe: its dataset holds no feature columns
Resume and checkpoints yes; a checkpoint restored by a new agent equals the run left whole yes, the same

Example

# doc: skip (about two minutes on a CPU; tests/agents/TDMPC2/test_TDMPC2.py covers this path)
from ajax import TDMPC2
from ajax.logging import LoggingConfig

# model_size=1 and a smaller planner than the paper's, for a quick CPU run.
agent = TDMPC2("Pendulum-v1", model_size=1, num_samples=64, num_elites=8,
               num_pi_trajs=4, iterations=2, seed_steps=400)
logging = LoggingConfig(log_frequency=600, backends=())
state, evaluations = agent.train(seed=0, n_timesteps=1_200, num_episode_test=2,
                                 logging_config=logging)
print(agent.resolved_gamma)  # 0.975, derived from Pendulum's 200-step episodes
print(evaluations["Eval/episodic mean reward"])

The paper's DMC protocol: action repeat 2 over 1,000 simulator steps (500 agent steps per episode), the 5M model, one environment. The paper's "1M environment steps" are 500,000 agent steps:

# doc: skip (the paper's full budget; benchmarks/world_models/paper_protocol.py runs it)
from ajax import TDMPC2

agent = TDMPC2("CheetahRun", model_size=5, action_repeat=2, episode_length=1000)
state, _ = agent.train(seed=0, n_timesteps=500_000)

Key hyperparameters

Every default is the paper's (config.yaml at 5f6fade).

Argument Default What it does
model_size 5 preset of 1, 5, 19, 48 or 317 M parameters; fills enc_dim, mlp_dim, latent_dim, num_enc_layers, num_q
n_envs 1 environments stepped in lockstep, one update per environment step
horizon 3 length of the training sub-trajectories and of the plans
batch_size 256 sub-trajectories per update
buffer_size 1_000_000 environment steps kept in replay
seed_steps None random steps before training; None is max(1000, 5 T), T the episode length
updates_per_step 1.0 updates per environment step after the seed phase
learning_rate 3e-4 a float or a schedule; the encoder trains at learning_rate * enc_lr_scale (0.3)
gamma None None derives it from the episode length T: (T/5 - 1) / (T/5), clipped to [0.95, 0.995]
iterations, num_samples, num_elites 6, 512, 64 MPPI iterations, sampled plans and kept plans
num_pi_trajs 24 plans proposed by the policy prior
consistency_coef, reward_coef, value_coef 20, 0.1, 0.1 world-model loss weights
rho 0.5 per-step decay of the loss weights along the horizon
entropy_coef 1e-4 policy prior's entropy weight
tau 0.01 target Q update rate
episodic False accept terminating tasks (the latest code's episodic mode)
episode_length, action_repeat 1000, 1 simulator steps per episode and per agent step, on Brax and Playground (Environments)

Presets

TDMPC2.preset("MuJoCo", env_id) sets discount_max=0.99 and rho=0.7, which the latest official code sets for every task of its gym module: Walker2d, HalfCheetah, BipedalWalker and LunarLander (eece801, tdmpc2/envs/mujoco.py:57-58; Presets). With 1000-step episodes the derived gamma becomes 0.99 instead of 0.995, and the loss weights decay more slowly along the horizon, whose last step is the only one a termination can fall on. The preset applies to Gymnasium's MuJoCo tasks in general, Hopper included, which the reference does not list. A task that terminates also needs episodic=True, as in TDMPC2.preset("MuJoCo", "mujoco:Hopper-v5", episodic=True).

Differences from the reference

Each is in deviations.md, under its number.

  • Terminations (T10). The paper-era agent bootstraps through every episode end, so a terminating task would train on wrong targets. Ajax counts terminations and raises a ValueError after train if there were any. episodic=True grafts the latest official code's episodic mode (eece801) onto the paper-era agent instead: a termination head and variable-length episodes in replay (T29).
  • Evaluation (T9) runs on a rebuilt environment, each evaluation from fresh initial states (the same ones when a seed is rerun); the reference reuses the training environment.
  • Replay size (T26). The replay holds min(buffer_size, total) steps, total being the run's, rounded up to whole rounds of one episode per environment where the reference rounds down to whole episodes. Each train call sizes it again for the steps the run will have taken, in the episodic mode as well (T29).
  • Several environments (T7). n_envs > 1 plans for each environment separately and takes n_envs updates per step; the reference uses one.
  • The DMC tasks run on MuJoCo Playground's MJX ports, whose physics differ from dm_control's (E18), so comparisons with the published curves are approximate.

Notes

  • What n_timesteps counts. Agent steps (environment steps after action repeat); logs also carry env_frames. With episodic=True it counts rows: environment steps plus one per finished episode.
  • Evaluation (with a logging_config): every log_frequency environment steps, num_episode_test episodes of the planner in evaluation mode. The paper uses log_frequency=50_000 with 10 episodes; LoggingConfig evaluates every 10,000 steps by default. Evaluating every 1,000 steps makes a 500-step-episode run spend about twice as long evaluating as training.
  • Replay memory. The ring holds R = ceil(capacity / (T * n_envs)) + 1 rounds of one episode per environment, R * n_envs * (T + 1) * (obs_dim + A + 1) * 4 bytes per seed: at full capacity, 124 MB on DMC Walker (observations 24, actions 6, T = 500). agent.replay_capacity and agent.replay_bytes_per_seed give the figures after train (also in the run config).
  • Recompilation. The architecture (model_size and the widths), simnorm_dim, num_bins, vmax, horizon, batch_size, buffer_size, seed_steps and the planner's sizes fix the compiled program; changing one recompiles. The other hyperparameters are plain floats, and learning_rate may be a schedule.
  • Actions. The planner's actions lie in [-1, 1]; Ajax maps them onto the environment's bounds, as the reference scales its actions.

Multi-task: TDMPC2MultiTask

The paper's multi-task models are trained offline, on the pooled replay of single-task agents. TDMPC2MultiTask does the same: a learnable task embedding conditions the model, observations are zero-padded to the largest task's size, the unused action dimensions are masked, and each task keeps the discount of its own episode length. Training does no planning, and n_timesteps counts updates. Build the dataset from single-task runs with export_episodes (one run's replay), concatenate_episodes (several seeds of a task) and pool_tasks; save_dataset and load_dataset store it as an .npz file.

# doc: skip (about five minutes on a CPU; tests/agents/TDMPC2/test_TDMPC2MultiTask.py covers this path)
from ajax import TDMPC2, TDMPC2MultiTask
from ajax.agents.TDMPC2.dataset import export_episodes, pool_tasks
from ajax.logging import LoggingConfig

small = dict(model_size=1, num_samples=64, num_elites=8, num_pi_trajs=4, iterations=2)
tasks = ["Reacher-misc", "PointRobot-misc"]  # 8 and 6 observations, 2 actions, T = 100

episodes = []
for task in tasks:  # one single-task run per task; its replay is the task's data
    agent = TDMPC2(task, seed_steps=500, **small)
    state, _ = agent.train(seed=0, n_timesteps=1_000)
    episodes.append(export_episodes(agent, state, seed_index=0, name=task))
dataset = pool_tasks(episodes)  # every task's episodes must have the same length

multi = TDMPC2MultiTask(dataset, eval_envs=tasks, batch_size=256, **small)
logging = LoggingConfig(log_frequency=500, backends=())
state, history = multi.train(seed=0, n_timesteps=1_000, num_episode_test=2,
                             logging_config=logging)
print(history["Eval/Reacher-misc/episodic mean reward"])
print(history["Eval/episodic mean reward"])  # the mean over tasks

It also logs Eval/normalized score, the mean over tasks of return / 10 (the paper's normalisation of DMC returns). multi.evaluate(state, num_episodes=10) runs the same evaluation outside training, on a state train returned, and gives the same keys, one value per seed; without eval_envs it raises.

The defaults are the single-task agent's, except batch_size=1024 and task_dim=96, the paper's (T14). The evaluation follows the offline trainer's protocol (T16). Ajax's datasets come from its own single-task runs, not the paper's mt30 and mt80 data, so they validate the mechanisms rather than the paper's numbers (T17).

Results

On a CPU, with model_size=1 and every other setting at the paper's default, two seeds: Pendulum reaches -143 and -124 after 6,000 steps (a random policy scores about -1,200), and Playground's CartpoleBalance (action repeat 2) reaches 810 and 854 after 10,000 steps, still improving (PERFORMANCE_REPORT.md). The GPU validation against the published curves is specified in VALIDATION.md and has not been run yet. The probes (tests/probing) train TD-MPC2 briefly on tiny environments whose answers need its model: a payoff past its planning horizon, and twin tasks for the multi-task agent.

API

ajax.agents.TDMPC2.TDMPC2.TDMPC2 dataclass

TDMPC2(
    env_id,
    *,
    n_envs=1,
    env_params=None,
    weight_decay=0.0,
    episode_length=1000,
    action_repeat=1,
    extensions=(),
    model_size=5,
    enc_dim=None,
    mlp_dim=None,
    latent_dim=None,
    num_enc_layers=None,
    num_q=None,
    simnorm_dim=8,
    num_bins=101,
    vmax=10.0,
    dropout=0.01,
    horizon=3,
    rho=0.5,
    consistency_coef=20.0,
    reward_coef=0.1,
    value_coef=0.1,
    learning_rate=0.0003,
    enc_lr_scale=0.3,
    grad_clip_norm=20.0,
    pi_eps=1e-05,
    tau=0.01,
    entropy_coef=0.0001,
    log_std_min=-10.0,
    log_std_max=2.0,
    batch_size=256,
    buffer_size=1000000,
    seed_steps=None,
    updates_per_step=1.0,
    gamma=None,
    discount_denom=5,
    discount_min=0.95,
    discount_max=0.995,
    iterations=6,
    num_samples=512,
    num_elites=64,
    num_pi_trajs=24,
    min_std=0.05,
    max_std=2.0,
    temperature=0.5,
    episodic=False,
    termination_coef=1.0,
)

Bases: OwnReplayAgent

TD-MPC2 for continuous control, single task, online (the module's tick).

Every hyperparameter is the one of the same name in nicklashansen/tdmpc2@5f6fade:tdmpc2/config.yaml (lr is learning_rate), with its paper default; setup builds model_config (TDMPC2Config) from them, the widths left None taking the model_size preset's (tdmpc2_spec 1.20). learning_rate is a float or a schedule, the encoder's learning_rate * enc_lr_scale (5f6fade:tdmpc2/tdmpc2.py:21-28); weight_decay is Ajax's (0 keeps the reference's plain Adam). Left None, two derive from the episode length T in agent steps (ajax.environments.utils.agent_episode_length), into resolved_gamma and resolved_seed_steps: gamma, clip((T/denom - 1) / (T/denom), discount_min, discount_max) (tdmpc2_spec 2.20, tdmpc2.py:36-49), and seed_steps, max(1000, 5 T) (4.3, envs/init.py:81).

The replay (buffer, the b67b21c episode ring) keeps min(buffer_size, total) env steps, total being the run's when a train call ends (the reference's clamp to the run length, deviation T26): every call sizes it (prepare_replay), moving a resumed run's episodes into it, so a run trained in chunks replays what the uninterrupted run does.

Continuous actions only. The env must be fixed-length and non-terminating (all DMC tasks): a termination raises ValueError after train (deviation T10). episodic=True takes terminating tasks instead (the latest code's episodic mode, nicklashansen/tdmpc2@eece801; deviation T29): a termination head, 1 - terminated in the targets and the planner, a replay of variable-length episodes and n_timesteps counting rows (env steps plus one per finished episode). Observations and rewards are the env's raw values, as in the reference.

Extensions: pretrain, post_update (after every update, step = env steps), eval_metrics (with init_state) and observe (the observation features, which the row collector and the evaluation append and the encoder reads).

set_compute_dtype

set_compute_dtype(dtype)

The world model's and the policy prior's layers compute in dtype (TDMPC2Config.compute_dtype).

run_capabilities

run_capabilities()

The shared run's capabilities, with warmup_steps: whatever seed_steps, its seed phase acts uniformly for at least one episode (both schedules' seed tick is at least the episode length), where the shared rule reads learning_starts.

prepare_replay

prepare_replay(start_tick, end_tick, agent_state)

The replay of a call ending at tick end_tick, for the env steps (rows in the episodic mode) the run has taken by then, and the state it resumes from, its carried episodes moved into that replay (adopt). Records the replay's capacity and bytes, also in the run config.

init

init(key, context)

Fresh networks and optimizers (core.create_update_state), a zero planner carry, reset envs (every env's first row is is_first) and an empty replay.

collect

collect(agent_state, tick, context)

Act and store one row per env on the absolute, unbatched tick; returns the state and the tick, which locates the replay's newest rows for the tick's updates. The episodic mode collects in the dynamic reset mode, counting rows.

count_updates

count_updates(agent_state, tick, context)

The schedule's updates of the tick. The episodic mode's follow the envs the tick stepped (those not held: the collector sets is_first exactly after a held tick) and wait for a sampleable episode.

update

update(agent_state, tick, context)

The reference's agent.update(buffer) after the tick's rows: a fresh batch and fresh noise, then core.update (tdmpc2_spec section 2), its logged quantities kept on update_metrics. The loop folds post_update after it.

evaluation_metrics

evaluation_metrics(
    agent_state, key, context, num_episode_test
)

Mean return and length of num_episode_test episodes of the planner in eval_mode (no final exploration noise, everything else stochastic: online_trainer.py:27-48, tdmpc2_spec 4.21) on a rebuilt env, its carry from zeros, apart from the training one (deviation T9). The key is fresh at each evaluation (ajax.log.evaluation_keys), so each starts from new initial states, as the reference's resets of its env do.

policy_parts

policy_parts()

As its evaluation acts: the planner in eval_mode, its carry from zeros.

report

report(agent_state, aux)

The house keys, env_frames and the last update's quantities.

steps_before

steps_before(tick)

The env steps, summed over envs, of the ticks before tick.

check_run

check_run(agent_state, end_tick)

Refuse a paper-era run whose env terminated (deviation T10): the paper-era TD-MPC2 bootstraps through every episode end, so a terminating task would train on wrong targets; and an episodic run that never updated.

make_planner_policy

make_planner_policy(agent_state, gamma, eval_mode)

plan_for_envs with the agent's current parameters and the gamma its caller passes bound: the row collector's policy and the evaluation's.

ajax.agents.TDMPC2.TDMPC2MultiTask.TDMPC2MultiTask

TDMPC2MultiTask(
    dataset,
    eval_envs=None,
    model_size=5,
    enc_dim=None,
    mlp_dim=None,
    latent_dim=None,
    num_enc_layers=None,
    num_q=None,
    task_dim=PAPER_TASK_DIM,
    simnorm_dim=8,
    num_bins=101,
    vmax=10.0,
    dropout=0.01,
    horizon=3,
    rho=0.5,
    consistency_coef=20.0,
    reward_coef=0.1,
    value_coef=0.1,
    learning_rate=0.0003,
    enc_lr_scale=0.3,
    grad_clip_norm=20.0,
    pi_eps=1e-05,
    weight_decay=0.0,
    tau=0.01,
    entropy_coef=0.0001,
    log_std_min=-10.0,
    log_std_max=2.0,
    batch_size=1024,
    discount_denom=5,
    discount_min=0.95,
    discount_max=0.995,
    iterations=6,
    num_samples=512,
    num_elites=64,
    num_pi_trajs=24,
    min_std=0.05,
    max_std=2.0,
    temperature=0.5,
    episode_length=1000,
    action_repeat=1,
    extensions=(),
)

Multi-task TD-MPC2, trained offline on a pooled dataset (paper era).

The reference's multi-task path is offline only (train.py:52; tdmpc2_spec 3.25, 4.1): the updates of the single-task agent, conditioned on each slice's task (:func:ajax.agents.TDMPC2.multitask.update: a learnable task embedding with max-norm 1, zero-padded observations, prefix action masks, per-task discounts), on batches of slices drawn uniformly over the dataset's pooled episodes, no planning during training. n_timesteps counts updates (the reference's iterations; paper: 10M at batch 1024, spec 4.B). The paper protocol is batch_size=1024 (the default here) and task_dim=96 for every model size (deviation T14).

The dataset (:class:~ajax.agents.TDMPC2.dataset.MultiTaskDataset, e.g. :func:~ajax.agents.TDMPC2.dataset.pool_tasks of single-task runs' exports) fixes the tasks: task i has the dataset's obs_dims[i], action_dims[i] and episode length T_i, whose discount is the reference's heuristic of T_i (discount_denom, discount_min, discount_max; spec 2.20). The model sees the padded observation and action spaces; the planner's iteration rule uses the padded action dim (spec 3.1). The dataset is one array set on the device, shared by every seed: the constructor places it there once (whatever built it: host NumPy arrays are transferred then, not at every chunk) and :meth:train passes it to its jitted, seed-vmapped program as an unbatched argument, so the seeds read the same copy (dataset.nbytes) and the program does not embed it.

Logging (with a logging_config): every log_frequency updates, the last update's quantities (Train/..., timestep = updates done), the extensions' eval_metrics and, with eval_envs, an evaluation: a host loop over the tasks runs num_episode_test episodes of each in its own env (eval_envs[i], built with episode_length simulator steps and action_repeat; its dims and agent-step episode length must be task i's), the planner in eval_mode with t0 on each episode's first step: the offline trainer's protocol (offline_trainer.py:22-39; deviation T16: evaluate.py's exploration noise is not reproduced). Logged per task Eval/<name>/episodic mean reward and length, their mean over tasks Eval/episodic mean reward and the normalised score Eval/normalized score, the mean over tasks of return / 10 (the paper's DMC normalisation, meaningful for DMC returns in [0, 1000]; spec 4.20). The first evaluation follows the first log_frequency updates (the reference's follows its first update). Each evaluation starts its episodes from fresh initial states. The embedding renorm of a look-up at evaluation is not persisted (deviation T15).

Static hyperparameters, as :class:~ajax.agents.TDMPC2.TDMPC2.TDMPC2's: the architecture (model_size preset and explicit widths), task_dim, simnorm_dim, num_bins, vmax, horizon, batch_size and the planner sizes. learning_rate accepts a float or a schedule (the task embedding is trained by the world-model optimizer at learning_rate, the encoder at learning_rate * enc_lr_scale).

Extensions: pretrain, post_update (folded after every update, step = updates done) and eval_metrics (with init_state), the phases of the single-task agent but observe (its dataset holds no feature columns); the others are rejected at construction.

Parameters:

Name Type Description Default
dataset MultiTaskDataset

the pooled multi-task dataset (it fixes the tasks).

required
eval_envs Optional[Sequence[str | EnvType]]

one env id or prebuilt gymnax env per task, in task-id order, for evaluation; None trains without evaluating.

None
task_dim int

task-embedding width (paper: 96; deviation T14).

PAPER_TASK_DIM
episode_length int

simulator steps per episode of the brax / playground evaluation envs.

1000
action_repeat int

simulator steps per agent step of the evaluation envs (the paper's DMC protocol: 2).

1
extensions Sequence[Extension]

composable research features (see :mod:ajax.extensions).

()

Every other argument is the hyperparameter of the same name of :class:~ajax.agents.TDMPC2.TDMPC2.TDMPC2 (5f6fade's config.yaml), with the paper's multi-task batch size 1024.

resume_update_offset

resume_update_offset(initial_state)

The updates a resumed run has done, equal across seeds.

evaluate

evaluate(state, num_episodes=10)

One evaluation of every task (host loop), per seed.

state has a leading seed axis (as :meth:train returns it). Returns {key: [n_seeds]}: per task Eval/<name>/episodic mean reward and Eval/<name>/mean episodic length, their means over tasks and Eval/normalized score (class docstring). It does not advance state.n_logs: :meth:train does after logging, so the next evaluation draws fresh keys (:func:ajax.log.evaluation_keys).

policy

policy(agent_state, seed_index=0)

Not yet: its planner acts for a task, which a policy does not take.

predict

predict(agent_state, observation, **kwargs)

Not yet, as policy.

train

train(
    seed=42,
    n_timesteps=int(1000000.0),
    num_episode_test=10,
    logging_config=None,
    on_ids_ready=None,
    initial_state=None,
    total_timesteps=None,
)

Run n_timesteps more updates for every seed (vmapped).

A fresh run initialises the networks (and folds the extensions' init_state and pretrain); initial_state (a state this agent's train returned, or a checkpoint restored into the n_timesteps=0 skeleton) continues a run; its buffers are donated to the first chunk (as ActorCritic.train donates them). The updates run as jitted scans; with a logging_config whose log_frequency (updates) is set, the scans stop every log_frequency updates of the run (an absolute cadence: a resumed run keeps it) to log the training metrics and, with eval_envs, to evaluate every task (class docstring). total_timesteps is the whole run's updates, the extensions' total_steps (None: this call's end), as :meth:ActorCritic.train's.

Returns (state, history): the state with a leading seed axis and, when it logged, {key: [n_seeds, n_logs]} (training and evaluation metrics), else None.

ajax.agents.TDMPC2.dataset.export_episodes

export_episodes(
    agent,
    state,
    seed_index=None,
    *,
    name="task",
    allow_evicted=False,
)

The committed episodes of a single-task TD-MPC2 run, oldest first.

Reads state.buffer_state (the episode ring of agent's run, :mod:ajax.agents.TDMPC2.buffer) after the ticks its collector has run: every complete episode the ring holds, in the order collected (rounds in order, env order within a round; the episode in progress is not exported). Its rows are the ring's obs-aligned rows, unchanged.

Parameters:

Name Type Description Default
agent TDMPC2

the :class:~ajax.agents.TDMPC2.TDMPC2.TDMPC2 that trained state (its n_envs and episode length).

required
state Any

the run's state, as agent.train returned it; with a leading seed axis, seed_index picks the seed.

required
seed_index Optional[int]

the seed to export, required for a seed-batched state.

None
allow_evicted bool

export what the ring still holds when it wrapped and evicted older episodes. By default this raises: a dataset of the full training history (DESIGN.md ยง7) needs buffer_size at least the run length.

False

Raises:

Type Description
ValueError

on a missing or extraneous seed_index, when no episode is complete, or when episodes were evicted (unless allow_evicted).

ajax.agents.TDMPC2.dataset.concatenate_episodes

concatenate_episodes(episodes)

The episodes of several runs of one task (e.g. seeds), in order.

They must agree on the rows, dims, episode length and name.

ajax.agents.TDMPC2.dataset.pool_tasks

pool_tasks(tasks)

The multi-task dataset of tasks, task i being tasks[i].

Observations and actions are zero-padded at the end to the largest dims (MultitaskWrapper, spec 1.23, 4.16); episodes keep their order, task after task. Every task's episodes must have the same number of rows (cutting longer episodes into segments, as the mt80 data probably did, spec 4.18, is not done here). Names must be unique. The arrays are placed on the default device once.

ajax.agents.TDMPC2.dataset.save_dataset

save_dataset(dataset, path)

Write dataset to path (an uncompressed NumPy .npz).

The file holds the four arrays under their field names and meta, a JSON string with the format name and version and the per-task metadata (obs dims, action dims, episode lengths, names): plain arrays and text, readable without pickle (:func:load_dataset). The dataset is checked first (:meth:MultiTaskDataset.check) and the file is written to a temporary name and renamed, so an interrupted save leaves no partial file under path.

ajax.agents.TDMPC2.dataset.load_dataset

load_dataset(path)

The dataset :func:save_dataset wrote to path, checked.

The arrays stay host NumPy arrays (TDMPC2MultiTask places the dataset on the device once). Raises ValueError on another format or version, or when the data do not follow the schema.