$ show NVIDIA/TorchFort
TorchFort is a library that bridges PyTorch with Fortran and C/C++ programs, primarily targeting HPC/scientific computing applications. It provides a C API that allows users to load PyTorch models (via TorchScript or built-in MLP), train them, and perform inference directly on user-provided arrays without data copying. The library supports supervised learning as well as reinforcement learning (RL) with several state-of-the-art algorithms: DDPG, TD3, SAC, and PPO. It includes comprehensive RL infrastructure such as replay buffers, rollout buffers, online running normalizers, and distributed training via MPI. Integration with Weights & Biases is available for experiment tracking. Architecturally, TorchFort consists of a thin C API layer, a set of C++ implementations using LibTorch, and Fortran bindings generated via ISO_C_BINDING. The core C++ code is organized into modules for model management, training, distributed communication, CUDA utilities, and RL subsystems. Configuration is externalized via YAML files, and the library is designed to be used as a static or shared library linked into user applications. Notable components include a prioritized replay buffer with sum-tree implementation (replay_buffer.h), an online running normalizer using Welford's algorithm with MPI synchronization (running_normalizer.h), and a tensor list abstraction for handling multiple typed arrays (tensor_list.h). The codebase demonstrates good engineering practices with extensive test coverage, error handling via return codes, and support for both CPU and GPU execution. While primarily an application/library for integrating PyTorch into HPC codes, the repository also contains research-quality RL algorithm implementations and data preprocessing components, giving it a hybrid character.
The LLM identified 4 files worth highlighting for reuse:
Implements an online per-feature normalizer using Welford's parallel algorithm for mean and variance computation, with Chan's algorithm for MPI-distributed synchronization. Supports state, reward, and return normalization with a scale_only mode to preserve sign of normalized values. The component is self-contained (depends only on LibTorch tensors and MPI) and can be extracted for any streaming or distributed statistics use case.
Provides both UniformReplayBuffer and PrioritizedReplayBuffer using a sum-tree data structure for prioritized experience replay as described by Schaul et al. Includes a min-tree for correct importance-sampling weight normalization and supports n-step returns. The implementation is self-contained, templated for different data types, and can be reused in any RL codebase needing efficient prioritized replay.
Manages a list of tensors from user-provided pointers, wrapping external memory buffers into PyTorch tensors without copying. Handles shape, device, stride, and data type metadata. This abstraction is essential for zero-copy integration with Fortran/C arrays and can be reused in any project that needs to convert external array pointers into LibTorch tensors.
Defines a ModelPack struct that bundles a model, optimizer, learning rate scheduler, model state, communicator, gradient accumulation steps, and max gradient norm. This pattern centralizes all training-related objects into a single composable unit, used consistently across supervised and RL training paths. It demonstrates clean encapsulation and can be extracted for any LibTorch-based training loop.