If I don’t want this mean calculation to take part of the autograd process, can I wrap your code with torch.no_grad()?
with torch.no_grad():
    sum_mask_embeddings = mask_embeddings.sum(dim=1) 
    for dim in range(mask.size(0)):
        average_mask_embeddings[dim] = sum_mask_embeddings[dim] / mask[dim].sum()

Does this makes sense? Do you think it can have a beneficial impact on my embeddings?