Indexing spans of various length

I need to extract phrases that span different lengths from a pre-trained BERT model. For example
let’s say 2 sentences are : “Pytorch is a great library” and “Deep learning with pytorch is fun” I need to extract the vectors of “great library” and “pytorch is fun” after passing those sentences through BERT.

I’m stumped at how I can efficiently index the output of BERT if a batch of sentences is passed to it since the spans are of variable length. Is there anyway to extract these values without using a for loop.

PS: I’m aware that since BERT used Byte Pair Encoding the tokens won’t necessarily remain to be those exact words, I just wanted to simplify the example.