Jump to content

Draft:Xtorch

From Wikipedia, the free encyclopedia
This is the current revision of this page, as edited by Qwerfjkl (bot) (talk | contribs) at 15:55, 3 May 2025 (Bot: Adding AfC template (Task 15)). The present address (URL) is a permanent link to this version.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

xTorch

xTorch is an open-source C++ library that extends the functionality of PyTorch’s C++ API, known as LibTorch. Designed to enhance the usability of LibTorch for end-to-end machine learning model development, xTorch provides high-level abstractions and utilities to streamline tasks such as model definition, training, and data handling in C++. It aims to offer a development experience comparable to PyTorch’s Python API while maintaining the performance benefits of C++.

History

xTorch was developed to address limitations in LibTorch’s usability, particularly after 2019, when the C++ API’s development diverged from the Python API’s focus. While LibTorch provided robust low-level components, such as tensor operations and automatic differentiation, it lacked high-level features like prebuilt model architectures and data augmentation tools. This made tasks like building and training neural networks in C++ more complex compared to Python. xTorch emerged to fill this gap, offering a modular, user-friendly interface for C++ developers working in environments where Python is impractical, such as embedded systems or high-performance computing.

Design and architecture

xTorch is structured as a lightweight layer over LibTorch, leveraging its computational core while introducing higher-level abstractions. The library is organized into three conceptual layers:

  • LibTorch Core: Provides foundational components, including torch::Tensor, torch::autograd, and torch::nn modules for neural network primitives.
  • Extended Abstraction Layer: Includes classes like ExtendedModel and Trainer that simplify model definition and training workflows.
  • User Interface Layer: Offers intuitive APIs to reduce boilerplate code and improve developer productivity.

The library is divided into several modules:

  • Model Module: Provides high-level model classes, such as XTModule, and prebuilt architectures like ResNetExtended and XTCNN.
  • Data Module: Supports datasets like ImageFolderDataset and CSVDataset, with OpenCV-backed data augmentation.
  • Training Module: Abstracts training logic, including checkpointing and performance metrics.
  • Utilities Module: Includes tools for logging, device management, and model summaries.

Features

xTorch introduces several enhancements to LibTorch, including:

  • High-Level Model Classes: Simplified model definitions and prebuilt architectures.
  • Training Loop Abstraction: A Trainer class that automates training with support for callbacks and metrics.
  • Data Handling: Built-in support for datasets and data loaders with transformations like resizing and normalization.
  • Optimizers and Schedulers: Extended optimizers, such as AdamW and RAdam, with learning rate scheduling.
  • Model Serialization: Functions for saving models and exporting to TorchScript for deployment.
  • Utilities: Tools for logging, device management, and inference.

Usage

xTorch is designed for C++ developers working in environments requiring high performance or integration with existing C++ codebases. Common use cases include embedded systems, high-performance computing, and industrial applications requiring on-device training or edge deployment.

Example: Training a convolutional neural network

The following code demonstrates a simplified training pipeline using xTorch to train a LeNet-5 model on the MNIST dataset:

<code>#include <xtorch/xtorch.hpp>

int main() {
    auto dataset = xt::data::datasets::MNIST(
        "/path/to/data", xt::DataMode::TRAIN, true,
        {xt::data::transforms::Resize({32, 32}),
         torch::data::transforms::Normalize<>(0.5, 0.5)}
    ).map(torch::data::transforms::Stack<>());

    xt::DataLoader loader(
        std::move(dataset),
        torch::data::DataLoaderOptions().batch_size(64).drop_last(false),
        true);

    xt::models::LeNet5 model(10);
    model.to(torch::Device(torch::kCPU));
    model.train();

    torch::optim::Adam optimizer(model.parameters(), torch::optim::AdamOptions(1e-3));

    xt::Trainer trainer;
    trainer.set_optimizer(&optimizer)
           .set_max_epochs(5)
           .set_loss_fn([](auto output, auto target) {
               return torch::nll_loss(output, target);
           });

    trainer.fit(&model, loader);
    return 0;
}
</code>

Example: Inference

The following code shows how to perform inference with a pre-trained model:

<code>auto model = xt::load_model("resnet18_script.pt");
auto tensor = xt::utils::imageToTensor("input.jpg");
auto outputs = xt::utils::predict(model, tensor);
int predictedClass = xt::utils::argmax(outputs);
std::cout << "Predicted class = " << predictedClass << std::endl;
</code>

Comparison with other tools

xTorch complements LibTorch by providing features akin to those in Python-based libraries like PyTorch Lightning. The table below compares key features:

Feature LibTorch xTorch PyTorch Lightning (Python)
Training Loop Abstraction No Yes Yes
Built-in Data Augmentation No Yes Yes
Prebuilt Model Zoo Limited Yes Yes
Target Language C++ C++ Python
TorchScript Export Limited Yes Yes

Applications

xTorch is suited for:

  • C++-based Machine Learning: Enables PyTorch-like workflows without Python dependencies.
  • Embedded and Edge Devices: Supports on-device training and inference.
  • High-Performance Computing: Facilitates integration with performance-critical systems.
  • Education: Provides a platform for teaching machine learning in C++.
  • Research and Development: Encourages experimentation in C++-based deep learning.

Development status

xTorch is under active development, with releases available on GitHub. The library is not yet considered stable, and users are advised to use official release versions for production environments.

See also

References