@Yunchao_Liu
you can try something like
tensor1 = torch.rand([15, 24, 4, 120])
tensor2 = torch.rand([5608, 4, 120])
cos = nn.CosineSimilarity(dim=0)

def custom_cos(cur_tensor):
    return cos(cur_tensor, tensor2)

results = map(custom_cos, tensor1.view(-1, 4, 120))
results = torch.concat(list(results)).view(15,24,4,120)
results.size()

If you have the latest version you can also use
https://pytorch.org/tutorials/prototype/vmap_recipe.html 3