I would masked_fill_ with zeros for the entries past the word vectors (if they aren’t already filled with zeros) and then sum along the whole axis. If you want an average, you can then divide by the lengths tensor suitably expand_ased.