A possible solution is to use a graph NN library to do the heavy work:

The challenge in training Tree-LSTMs is batching — a standard technique in machine learning to accelerate optimization. However, since trees generally have different shapes by nature, parallization is non-trivial. DGL offers an alternative. Pool all the trees into one single graph then induce the message passing over them, guided by the structure of eac

e.g. from dgl: Tutorial: Tree-LSTM in DGL — DGL 0.4.3post2 documentation 6