Skip to content

Adding an architecture

CrashDefNet is built so that a new model architecture can be tested quickly: the model plugs into the existing pipeline, and data, training, rollout, metrics and the comparison with published results work unchanged. MGN-T was added this way. It needs four small steps.

This holds for architectures that take the graph of the pipeline and predict the targets per node. Depending on how an architecture is built, other inputs, graph structures, outputs or training procedures can need changes to the pipeline as well, e.g. to the graph building, the training loop or the rollout. Such changes model the physics of the crash in more detail; inputs without a physical role are not added (design principles).

1. The model (models/<name>.py)

A torch.nn.Module with this interface:

class MyModel(nn.Module):
    def __init__(self, node_in_dim: int, mesh_edge_in_dim: int, world_edge_in_dim: int,
                 output_dim: int, **hyperparameters):
        ...

    def forward(self, data) -> torch.Tensor:
        """Normalized predictions per node, shape (N, output_dim)."""
        ...

The input dimensions come from the dataset's metadata.json and the features the config selects; the model does not need to know the feature layout. data is a (batched) PyTorch Geometric graph with

Attribute Shape Content
x (N, node_in_dim) normalized node features
edge_index, edge_attr (2, E_M), (E_M, mesh_edge_in_dim) mesh edges and their normalized features
world_edge_index, world_edge_attr (2, E_W), (E_W, world_edge_in_dim) contact edges in the variant of [model] world_edges; may be empty
mesh_pos (N, 3) undeformed positions (e.g. for positional encodings)
world_pos (N, 3) current positions, raw units
batch (N,) graph index of each node; may be missing for a single graph
u (B, global_in_dim) normalized run parameters, one row per graph; only for datasets with run parameters

For a dataset with run parameters create_model also passes global_in_dim; a model that accepts it can use them (the existing ones in their global block).

Details of the features: data and graphs. The building blocks of the existing models can be reused, e.g. make_mlp and GraphNetBlock from models/meshgraphnets.py.

2. The config (train/config.py)

  • Add the name to ARCHITECTURES.
  • Add a field per hyperparameter to TrainingConfig, prefixed with the name, with a default (the published value where there is one) and a description, e.g. my_model_latent_size: int = setting(64, "width of the latents").
  • Map the TOML keys onto the fields in TOML_KEYS, e.g. "model.my_model.latent_size": "my_model_latent_size".
  • Rewrite the configuration reference from the code: python utils/docs_reference.py adds the section [model.my_model].

The config file then gets its own section:

[model]
architecture = "my_model"

[model.my_model]
latent_size = 64

3. Building the model (train/trainer.py)

Add a branch to create_model, before the final else that builds MeshGraphNets, that passes the dimensions and the hyperparameters:

elif config.architecture == "my_model":
    from models.my_model import MyModel
    model = MyModel(**dims, latent_size=config.my_model_latent_size)

create_model is also used when a checkpoint is loaded, so the rollout finds the new model without further changes. Optionally add a one-line description of the model to the model summary at the start of Trainer.train.

4. Display name (optional)

ARCHITECTURE_NAMES sets the name shown for the architecture (default: its key): in utils/compare_paper.py and utils/check_protocol.py the short name in the figures, tables and reports, in utils/animate_rollout.py the panel title of the animation.

Testing the new architecture

# quick check on the CPU with the small dataset (architecture = "my_model" in the config)
python train/train.py --config configs/my_model.toml --epochs 1

The full comparison with MeshGraphNets follows the steps of the comparison protocol.

Everything else applies to the new model automatically: position noise, scheduled sampling and rollout training, the additional loss terms, rollout validation and model selection, early stopping, the optional residual target, the rollout metrics and the .vtu export.