For RNNs, there’s already a batched variable-length example (on the SNLI dataset https://github.com/pytorch/examples/tree/master/snli 508); there all you have to do is sort the examples a little so that you can batch together sentences of similar lengths with minimal padding.
TreeRNNs are harder. I’ll add an example soon that does this, but the general idea for TreeRNNs is that batching is up to you as the user, and you should split and concatenate when you need to. So if you use a binary tree structure, you can represent it as a shift-reduce parser (see the SPINN paper from Bowman et al) that means you can process multiple trees in parallel by doing preprocessing like this:
input:
 tree1: ((ab)c)
 tree2: (d(ef))
preprocessed input:
        1        2        3        4        5
 tree1: SHIFT(a) SHIFT(b) REDUCE   SHIFT(c) REDUCE
 tree2: SHIFT(d) SHIFT(e) SHIFT(f) REDUCE   REDUCE

and then using advanced indexing to copy all the tokens for SHIFT at each timestep in parallel while concatenating the stack representations for batched REDUCE.
Sorry if this is confusing, I promise an example will be up soon.
I would add that PyTorch is impressively fast on TreeRNNs even without batching.