NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Torchtree: flexible phylogenetic model development and inference using PyTorch

Mathieu Fourment, Matthew Macaulay, Christiaan Swanepoel, Xiang Ji, Marc A. Suchard, F. A. Matsen · DOI

Abstract

Bayesian inference has predominantly relied on the Markov chain Monte Carlo (MCMC) algorithm for many years. However, MCMC is computationally laborious, especially for complex phylogenetic models of time trees. This bottleneck has led to the search for alternatives, such as variational Bayes, which can scale better to large datasets. In this paper, we introduce torchtree, a framework written in Python that allows developers to easily implement rich phylogenetic models and algorithms using a fixed tree topology. One can either use automatic differentiation, or leverage torchtree's plug-in system to compute gradients analytically for model components for which automatic differentiation is slow. We demonstrate that the torchtree variational inference framework performs similarly to BEAST in terms of speed and approximation accuracy. Furthermore, we explore the use of the forward KL divergence as an optimizing criterion for variational inference, which can handle discontinuous and non-differentiable models. Our experiments show that inference using the forward KL divergence tends to be faster per iteration compared to the evidence lower bound (ELBO) criterion, although the ELBO-based inference may converge faster in some cases. Overall, torchtree provides a flexible and efficient framework for phylogenetic model development and inference using PyTorch.

Introduction

In the field of phylogenetics—the study of evolutionary relationships—Markov chain Monte Carlo (MCMC) has long been the gold standard for Bayesian inference. Despite its mathematical rigor, MCMC is notoriously slow and computationally demanding, particularly as genomic datasets grow exponentially. For instance, the millions of SARS-CoV-2 sequences generated during the recent pandemic have highlighted the urgent need for more scalable inference methods.

Variational Bayes (VB) presents a promising alternative by transforming the inference problem into an optimization task. However, applying VB to complex phylogenetic models is non-trivial due to discrete components and non-differentiable mathematical functions. To address these challenges, we developed torchtree, a flexible Python-based framework that leverages the PyTorch ecosystem to streamline phylogenetic model development and inference.

Methods

The torchtree framework is designed around the PyTorch library, providing several critical features for modern phylogenetic research. It utilizes Automatic Differentiation (AD) to compute gradients, significantly reducing the manual effort required for model implementation. Recognizing that AD can be computationally expensive for specific phylogenetic likelihoods, torchtree includes a plug-in system that allows users to integrate high-performance C++ kernels (such as physher or bito) for analytical gradient calculations.

Summary of phylogenetic model types and specific models supported by the torchtree framework.

As shown in Figure 1, torchtree supports a diverse array of model types, ranging from nucleotide and amino acid substitution models to complex coalescent and clock priors. To facilitate gradient-based optimization, we introduced the "skyglide" model—a continuous piecewise-linear adaptation of the traditional "skygrid" model—ensuring the objective function remains smooth.

We evaluated torchtree using two primary optimization criteria: the standard Evidence Lower Bound (ELBO) and the forward Kullback-Leibler (KL) divergence. The latter, implemented via self-normalized importance sampling, is particularly robust for non-differentiable models. Validation was conducted on two real-world viral datasets: 63 Hepatitis C Virus (HCV) sequences and 583 SARS-CoV-2 sequences, comparing performance against the MCMC-based software BEAST.

Results

Our results indicate that torchtree achieves parameter estimates—such as root heights and substitution rates—that are highly consistent with those produced by BEAST's MCMC. This demonstrates that the variational approximation is a reliable substitute for traditional methods in these contexts.

In terms of computational efficiency, the forward KL-based approach demonstrated a significant speed advantage in the HCV analysis, completing in 300 minutes compared to 930 minutes for the ELBO-based method. Furthermore, torchtree successfully captured the characteristic C→T substitution bias in SARS-CoV-2, providing strong statistical support for non-reversible evolutionary models.

Discussion and Conclusion

While torchtree represents a significant step forward in making variational inference accessible for phylogenetics, certain limitations remain. The current version requires a fixed tree topology and employs a mean-field approximation, which can lead to an underestimation of parameter variance. Future developments will focus on integrating Normalizing Flows to better model parameter correlations and extending the framework to explore tree topology space. In conclusion, torchtree provides a flexible and efficient platform for the next generation of phylogenetic model development.

Summary updated

BibTeX
@inproceedings{fourment2025torchtree,
  author = {Mathieu Fourment and Matthew Macaulay and Christiaan Swanepoel and Xiang Ji and Marc A. Suchard and F. A. Matsen},
  title = {{Torchtree: flexible phylogenetic model development and inference using PyTorch}},
  booktitle = {Systematic},
  year = {2025},
  doi = {10.1093/sysbio/syaf047},
}
Figure