The released H-Net code initializes three things on purpose, beyond the usual normal draw: residual-branch outputs shrink with the total number of residual blocks on the path, router projections start as the identity, and the skip projection starts at zero. Implement this plan on real PyTorch modules.
residual_counts(heights)
heights[s] is the number of residual blocks at stage s, outermost first (encoder plus decoder blocks at outer stages, main network blocks at the last).int. Each stage counts itself plus all stages outside it, so [8, 10, 26] gives [8, 18, 44].init_stage(stage, n_residuals, std=0.02)
torch.nn.Linear inside stage (found with named_modules()), draw its weight from a zero-mean normal. Use standard deviation std / sqrt(n_residuals) when the module's name contains out_proj or fc2, and std otherwise.weight has an attribute _no_reinit set to True. Leave biases alone. Return None.init_dynamic_chunking(w_q, w_k, residual_proj)
w_q and w_k are square bias-free nn.Linear modules; residual_proj is an nn.Linear._no_reinit = True, so a later init_stage keeps them. Leave the bias of residual_proj as it is. Return None.torch.no_grad() lets you copy values into a parameter directly.