Yes, sorry, that shape was wrong, corrected it.
Also, now that I think of it I think you are right and one could probably calculated this
in a way that is specific to cosine similarity.
I guess what I really was interested in is if there is an abstract operation where you have two tensors and you get a result tensor by applying a function of two parameters to all pairs of values where the values are taken along some dimension of those tensors.
So to try this again more generally, lets say you have two tensors (N, D) and (M, D) and a function from (D,D) to K, I want an operation that produces a tensor (N,M,K) from applying the function to all the pairs of  D along the first axis.
This looks like such an obvious operation that I just feel it MUST exist!