1. def clones(module, N):
    2. "Produce N identical layers."
    3. return nn.ModuleList([copy.deepcopy(module) for _ in range(N)])
    1. class Encoder(nn.Module):
    2. "Core encoder is a stack of N layers"
    3. def __init__(self, layer, N):
    4. super(Encoder, self).__init__()
    5. self.layers = clones(layer, N)
    6. self.norm = LayerNorm(layer.size)
    7. def forward(self, x, mask):
    8. "Pass the input (and mask) through each layer in turn."
    9. for layer in self.layers:
    10. x = layer(x, mask)
    11. return self.norm(x)