The released H-Net code describes a model with a nested layout: ["m4", ["T1m4", ["T27"], "m4T1"], "m4"] is an encoder, an inner H-Net and a decoder, and the inner H-Net holds its own encoder, main network and decoder. Each string is a run of layer blocks: a letter (m or M for a Mamba-2 layer, t or T for a Transformer layer) followed by a count. Implement the parser, the paper's parameter arithmetic and the width changes between stages.
parse_layout(layout)
[encoder, inner, decoder], where inner is itself a layout, or a list of length 1 [main].(mamba_layers, transformer_layers) tuple per stage, outermost first. An outer stage counts its encoder and decoder layers together; the last entry counts the main network.ValueError for any other list length, or for a string that is not one or more letter-count blocks.count_params(layout, widths, vocab=0)
widths[s] is , one per stage. Raise ValueError if the lengths differ.vocab * widths[0] for a tied embedding. Return an int.width_steps(widths)
ValueError if the widths ever decrease.expand_width(x, pad) and reduce_width(x, d)
x is a tensor (..., D). expand_width appends the same trainable vector pad of shape (P,) at every position and returns (..., D + P).reduce_width returns the first d dimensions, (..., d).pad needs its shape broadcast to the leading dimensions of x, without copying the parameter by hand, so its gradient keeps flowing.