You could use a similar approach using a mask.
I don’t know what values your padding tokens have, but you could set the mask values for all padding values to a large negative value (-Inf should work), multiply H with this mask, and apply max() on it.
Let me know, if that would work.