Length of input, output vectors

One thing with lstm encoder/decoder model is we can have the hidden states saved from encoder and in decoder we can only set the length of the output to desired length.
For example, we can use sentence length of 16 to estimate output sentence length of 3
In the transformers, we need to start with same maximum positional encoding. And basically mask out the values on output from 3-15 to ensure we focus on 0-2. Is there another way for this?

I believe that masking is the usual technique.