Put the pieces together. One stage of an H-Net takes encoder outputs , chunks them, runs a main network over the chunks, and returns one vector per input position:
class DCStage(nn.Module)
__init__(self, d_model, main): main is a callable mapping (B, M, D) to (B, M, D); store it as self.main. Create:
self.W_q, self.W_k: bias-free linear layers d_model -> d_model, both starting as the identity.self.residual: a bias-free linear layer d_model -> d_model, weights starting at zero.forward(self, x_hat): x_hat is (B, L, D). Return (z, p, b), where z is (B, L, D), p float (B, L) and b bool (B, L). In order:
p[:, 0] = 1, clamped to [0, 1]; .(B, M), where M is the largest kept count in the batch.self.main on the compacted tensor.cumsum(b) - 1 along its row.1.0 and whose gradient is that of ( where , else ).self.residual(x_hat).z must reach W_q, W_k and residual, as well as x_hat.