I think the confusion is due to the limitation of the text. Check the derivation below and I am happy to help with any more questions.
Note:- To implement in pytorch, you need to respect the dimensions (like adding squeeze and unsqueeze where necessary).
WhatsApp Image 2020-11-12 at 4.30.09 PM1280×997 46.9 KB