Preprint
Article

This version is not peer-reviewed.

XTorch: A High-Performance C++ Framework for Deep Learning Training

Submitted:

06 July 2025

Posted:

07 July 2025

You are already at the latest version

Abstract
The deep learning ecosystem is predominantly driven by high-level Python frameworks like PyTorch and TensorFlow, which offer exceptional flexibility and ease of use. However, the reliance on a Python front-end can introduce significant performance overhead, partic- ularly in data-intensive training pipelines, often necessitating multi-GPU setups to achieve acceptable training times. This paper introduces XTorch, a high-level C++ deep learning framework built atop LibTorch, designed to bridge the gap between Python’s usability and C++’s raw performance. XTorch provides a familiar API for datasets, transforms, and mod- els while eliminating Python-related bottlenecks. We demonstrate its efficacy by training a Deep Convolutional Generative Adversarial Network (DCGAN) on the CelebA dataset. Our results show that XTorch, running on a single NVIDIA RTX 3090 GPU, completes a 5-epoch training run in 219 seconds. This represents a 37% speedup over a standard PyTorch implementation which required 350 seconds using two RTX 3090 GPUs with DataParallel. This work validates that a native C++ framework can not only match but significantly outperform common multi-GPU Python setups, offering a compelling case for reducing hardware costs and accelerating research and deployment.
Keywords: 
;  ;  ;  ;  ;  ;  

1. Introduction

The last decade has seen an explosion in the adoption of deep learning, largely enabled by frameworks like PyTorch [1] and TensorFlow [2]. Their Python APIs have democratized AI development, making it accessible to a wide audience. While these frameworks perform core computations using highly optimized C++/CUDA backends, the "glue" logic—data loading, preprocessing, and the training loop itself—is typically executed in Python. This architecture, while flexible, introduces inherent performance limitations due to Python’s Global Interpreter Lock (GIL) and general interpreter overhead.
In many real-world scenarios, especially those involving large datasets and complex image augmentations, the data loading and preprocessing pipeline becomes the primary bottleneck, leaving expensive GPU hardware underutilized. The common solution is to scale horizontally, either by increasing the number of CPU workers for the data loader or by using multiple GPUs. While effective, this approach increases hardware complexity, energy consumption, and operational costs.
We argue that for high-performance applications, a framework that operates entirely within a compiled C++ environment offers a superior alternative. By removing the Python front-end, we can eliminate interpreter overhead and implement more efficient, low-level data handling mechanisms using C++ concurrency primitives.
This paper introduces XTorch, a framework designed to provide a Python-like development experience directly in C++. It offers high-level abstractions for common deep learning components, including a model zoo, data transformation pipelines, and a highly optimized data loader. Our primary contribution is to demonstrate empirically that this C++-native approach can lead to dramatic performance improvements. By benchmarking a DCGAN [3] training task on the CelebA dataset [4], we show that XTorch on a single GPU can outperform a standard dual-GPU PyTorch implementation, effectively halving the hardware requirement while simultaneously reducing training time.

2. The XTorch Framework

XTorch is designed with two core principles: performance-first and developer-friendliness. It leverages the power of LibTorch for its tensor operations and automatic differentiation engine but provides a higher-level, more expressive API for building and training models.

2.1. Architecture

The XTorch library is structured into several key modules, mirroring the familiar organization of torchvision:
  • xt::models: A collection of pre-implemented, standard neural network architectures. The DCGAN Generator and Discriminator used in our experiment are part of this module. This allows for rapid prototyping without needing to redefine common models from scratch.
  • xt::datasets: C++ classes for interfacing with popular datasets. The xt::datasets::CelebA class handles the parsing of the dataset directory and attribute files, abstracting away the file I/O boilerplate.
  • xt::transforms: A suite of data preprocessing and augmentation modules that mimic torchvision.transforms. The Compose class allows users to chain transformations like Resize, CenterCrop, and Normalize into a sequential pipeline.
  • xt::dataloaders: This is the cornerstone of XTorch’s performance. We provide an ExtendedDataLoader that is a ground-up C++ implementation of a parallel data loader.

2.2. The ExtendedDataLoader

The primary performance bottleneck in Python-based training is often the torch.utils.data.DataLoader. While it supports multi-processing via the num_workers argument, it suffers from the overhead of Inter-Process Communication (IPC) for transferring preprocessed data back to the main process.
The xt::dataloaders::ExtendedDataLoader bypasses this issue by using a C++ multi-threaded architecture.
  • Multi-threaded Prefetching: The data loader spawns a pool of C++ worker threads. Each thread independently fetches, decodes, and transforms a batch of data.
  • Shared Memory Queue: The processed tensor batches are placed into a concurrent, thread-safe queue. This avoids the costly serialization/deserialization and IPC overhead inherent in Python’s multi-processing approach.
  • Maximized GPU Saturation: The main training loop thread simply dequeues a ready batch and moves it to the target device, ensuring the GPU is fed a continuous stream of data.
This design ensures that data preprocessing occurs in parallel with GPU computation, effectively hiding the data loading latency and maximizing GPU utilization.

3. Experimental Setup

To provide a fair and compelling comparison, we configured an experiment using a widely recognized model and dataset.
  • Hardware:
    CPU: AMD Ryzen 9 5950X (16-core)
    GPU: NVIDIA RTX 3090 (24GB VRAM)
    RAM: 64GB DDR4
  • Software:
    OS: Ubuntu 20.04
    CUDA Toolkit: 11.6
    PyTorch / LibTorch: 1.12.1 (cxx11 ABI)
    Compiler: g++ 9.4.0
  • Dataset: CelebFaces Attributes (CelebA) dataset.
  • Model: Deep Convolutional Generative Adversarial Network (DCGAN).
  • Training Parameters: Epochs: 5, Batch Size: 128, Optimizer: Adam (lr=0.0002, beta1=0.5), Image Size: 64x64.
Benchmark Implementations:
  • PyTorch Baseline: The official PyTorch DCGAN example, modified to use two RTX 3090 GPUs via torch.nn.DataParallel. The DataLoader was configured with num_workers=8.
  • XTorch Implementation: The C++ code provided in Appendix A, run on a single RTX 3090 GPU. The ExtendedDataLoader was configured with num_workers=2.

4. Results and Analysis

The total time taken to complete 5 epochs of training is summarized in Table 1.
The results are unambiguous. The XTorch implementation on a single GPU was 131 seconds (37.4%) faster than the dual-GPU PyTorch baseline. This significant performance differential can be attributed to three main factors:
  • Elimination of Data Loading Bottlenecks: The primary contributor to the speedup is the efficiency of the C++ ExtendedDataLoader.
  • No Python Interpreter Overhead: The main training loop in C++ is compiled to highly efficient machine code, avoiding the accumulated overhead of a Python-based loop.
  • Inefficiency ofDataParallel: The single-GPU XTorch implementation completely avoids the scatter/gather overhead inherent in the DataParallel module.

5. Conclusions and Future Work

This paper introduced XTorch, a high-performance C++ deep learning framework. Through a direct comparison with a standard multi-GPU PyTorch setup, we have demonstrated that a C++-native approach can provide substantial performance benefits, cutting training time by over 37% while halving the required GPU hardware. The results challenge the conventional wisdom that C++ is only suitable for inference deployment. Future work will focus on expanding the XTorch library and implementing a C++-native equivalent of DistributedDataParallel for efficient multi-node training.

Appendix A. XTorch DCGAN Training Source Code

This appendix contains the complete C++ source code used for the XTorch benchmark. Preprints 166859 i001Preprints 166859 i002Preprints 166859 i003

References

  1. A. Paszke, et al. (2019). PyTorch: An Imperative Style, High-Performance Deep Learning Library. Advances in Neural Information Processing Systems 32.
  2. M. Abadi, et al. (2016). TensorFlow: Large-Scale Machine Learning on Heterogeneous Distributed Systems. arXiv preprint arXiv:1603.04467, arXiv:1603.04467.
  3. A. Radford, L. Metz, & S. Chintala. (2015). Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks. arXiv preprint arXiv:1511.06434, arXiv:1511.06434.
  4. Z. Liu, et al. (2015). Deep Learning Face Attributes in the Wild. Proceedings of the IEEE International Conference on Computer Vision.
Table 1. Benchmark Results for 5-epoch DCGAN Training on CelebA.
Table 1. Benchmark Results for 5-epoch DCGAN Training on CelebA.
Framework GPU Configuration Total Time (seconds)
PyTorch 2 x RTX 3090 (DataParallel) 350 s
XTorch 1 x RTX 3090 219 s
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content.
Copyright: This open access article is published under a Creative Commons CC BY 4.0 license, which permit the free download, distribution, and reuse, provided that the author and preprint are cited in any reuse.
Prerpints.org logo

Preprints.org is a free preprint server supported by MDPI in Basel, Switzerland.

Subscribe

Disclaimer

Terms of Use

Privacy Policy

Privacy Settings

© 2025 MDPI (Basel, Switzerland) unless otherwise stated