xnn.gnn.fast.megnet#

Fast path of MEGNet: the encoders, the blocks and the Set2Set readouts as compiled stages.

The reference block concatenates [v_i, v_j, e_k, u] for every bond before the first dense layer of the bond update. That layer is linear, so here its weight is split into the four blocks of the concatenation: the atom and state parts are multiplied once per atom and per structure and gathered to the bonds, and only the bond part is multiplied per bond. The atom update is split the same way (its state part once per structure). The results are the same to rounding, without the bond-sized concatenation and with a quarter of the first layer’s work on the bonds.

The Set2Set readouts write the LSTM cell out (the gate equations of torch.nn.LSTM), so that all their steps are one stage. On a GPU every stage is compiled by torch.compile(), which fuses the activations, gathers and means into few kernels. When a gradient is needed the stages are compiled with their first and second derivatives (compile_twice_differentiable()); on a CPU they run as written.

Module Attributes

AUTO_POLICY

every size, since on an A100 the compiled stages and on a CPU the split layers (as written) were faster than the reference from one 20-atom cell to one of 16384 atoms, in both precisions.

AUTO_POLICY_TRAINING

every size, on an A100 with a third to two thirds of the reference's memory.

Functions

block_function(n_dense, n_conv, residual, act)

f(e, x, u, *weights, dst, src, batch, edge_batch, per_atom, per_structure_bonds, per_structure_atoms): one MEGNet block with the first layers of the bond and atom updates split.

encode_function(n_edge, n_node, n_state, act)

f(dist, x, u, *weights, centers, gamma): the Gaussian bond expansion and the three encoders.

linear_params(mlps)

The (weight, bias) of every layer of the given MLPs, in order.

plain_mlp(mlp)

Whether every layer of mlp is a plain torch.nn.Linear with a bias (not wrapped, e.g. by LoRA).

set2set_function(steps, layers)

f(x, *weights, index, per_structure): Set2Set with the LSTM cell written out.

stage(kind, shape, act, n_inputs, ...)

The stage kind ("encode", "block", "set2set") of the given layer counts.