How to implement a beam search for translation?

Basically what the title says. I know what a beam search does but cannot understand how to implement it efficiently in PyTorch. I did find a couple of implementations online, but couldn’t understand how they worked. Any help would be appreciated.

If you happen to know of some way to add beam search decoding to a regular neural network, let me know of the same as well.