A solution along the same logic of the answer given by @googlebot, but using PyTorch functions:

C = torch.sum(torch.mul(A, B)) - torch.mul(A, B)

This should give you what you want! Assumption here is that A and B are 1D tensors, of course.