I’m not sure, but wouldn’t torch.mm(mat, mat.t()) calculate the Gram matrix?
PS: as you can clearly see I’m not an expert in this topic, so tagging certain people might demotivate others to answer in your thread.