How to extract the output at the end of each TransformerDecoderLayer layer in the TransformerDecoderer

I am using a TransformerDecoder with 3 TransformerDecoderLayer. I was wondering if there is a way to get the output of each of the TransformerDecoderLayer?
decoder = nn.TransformerDecoder(nn.TransformerDecoderLayer(d_model=64, nhead=8), num_layers=3)
woudl it be possible to get the output of each intermediate layer (i.e. the output of first TransformerDecoderLayer, second TransformerDecoderLayer, and 3rd TransformerDecoderLayer which is equal to the output) and use it in the another model during the training?

You can use hooks:
(look for a good tutorial by googling)

Similarly, you ahve this new feature