Some compact language model architectures keep their main token embedding small while still giving each transformer block its own per-layer embedding signal. This is done via a Per-Layer Embedding (PLE) mechanism: a separate (small) embedding table produces a vector for each layer, which is fused with a projected version of the main embedding before being consumed by each block.
Implement per_layer_embedding(input_ids, token_embed, ple_embed, proj_weight, rms_weight, d_ple, n_layers, eps=1e-6) that performs the following computation:
main = token_embed[input_ids] with shape (B, T, d_model).ple = ple_embed[input_ids] with shape (B, T, n_layers * d_ple), then multiply by sqrt(d_ple).proj = main @ proj_weight, where proj_weight has shape (d_model, n_layers * d_ple).proj along the last axis using epsilon eps, then elementwise multiply by rms_weight (shape (n_layers * d_ple,)).combined = (proj_norm + ple) * 2^(-0.5).combined to (B, T, n_layers, d_ple) and return it as a (nested) Python list.RMSNorm along the last axis is defined as x / sqrt(mean(x^2) + eps).
Inputs:
input_ids: array-like of shape (B, T) with integer token idstoken_embed: (V, d_model)ple_embed: (V, n_layers * d_ple)proj_weight: (d_model, n_layers * d_ple)rms_weight: (n_layers * d_ple,)d_ple, n_layers: intseps: float, default 1e-6Returns: nested list of shape (B, T, n_layers, d_ple).