37 should be the length of the longest hypothesis considered by beam search, not necessarily the best.
The shape of the weights should be (batch_size, num heads, query_len, key_len).
Since the decoder processes 1 new query token at a time this 1,
your shapes make sense.
What I’m not sure about is whether after your best hypothesis is “finalized” in beam search, it will keep being passed to the model. I suspect it will not.
You might be able to recover the attention weights of a finalized hypothesis more easily by calling
best_generation = model.generate(src_tokens)
outputs = model(src_tokens, labels=best_generation, output_attentions=True, return_dict=True)
outputs.decoder_attentions