Mask embedding for calculate similarity score

I want to calculate similarity score between input pair embedding.
All input pad to max_length which is 50.
During cal,I want mask padding term.
how can I do this in easy way?
I have two mask and also embedding.

        mask_q = torch.ne(q, 0).type(self.dtype)
        mask_a = torch.ne(a, 0).type(self.dtype)

I thought maybe use product, but I don’t know how to change mask to 3d Tensor likes batch * seq * embedding.
where padding term all zero, and normal term are 1.