NForge
Tensor library
Loading...
Searching...
No Matches
NForge

Build Docs Benchmarks

A C++ tensor library meant to feel like NumPy or PyTorch idk.

Features

  • N-dimensional tensors
  • Tensor views and strided views
  • NumPy-style broadcasting
  • Reductions (sum, mean, min, max, prod, norm)
  • Matrix multiplication with batched support
  • CPU and CUDA backends

Quick start

Code example

#include <nforge/nforge.h>
int main() {
size_t n = 4, m = 5;
Tensor a({n, m}); // 2D tensor of zeros
for (size_t i = 0; i < n; i++) {
a[i] = Tensor({m}, (float)i); // fill each row with its index
}
a[0][3] = Tensor(3.14f);
a.print();
}
Definition tensor.h:18

Output

====================
Tensor[CPU], Data:
0 0 0 3.14 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
Shape: { 4 5 }
====================

See INTRO.md for more examples.

Installation

Using CMake and FetchContent with your project.

include(FetchContent)
FetchContent_Declare(
NForge
GIT_REPOSITORY https://github.com/Marbjo07/NForge.git
GIT_TAG main
)
FetchContent_MakeAvailable(NForge)
# Link to your target
target_link_libraries(${PROJECT_NAME} PRIVATE NForge)

Requirments

  • C++17 compiler
  • CMake 3.16+
  • Optional: CUDA Toolkit

Documentation

Documentation is built using Doxygen and Github Actions, then hosted on Github Pages on the branch gh-pages.
See marbjo07.github.io/NForge/docs

To generate documentation locally, ensure Doxygen is installed, then from project root:

doxygen Doxyfile

Build from source

mkdir build && cd build
cmake ..
cmake --build .

CUDA support

cmake .. -DNFORGE_ENABLE_CUDA=on
cmake --build .

Requires the CUDA Toolkit. MSBuild is recommended on Windows.

Build options

  • NFORGE_ENABLE_CUDA, by default off
  • NFORGE_BUILD_BENCHMARKS, by default off
  • NFORGE_BUILD_TESTS, by default off

Tests

Unit and integration tests use Catch2 and live under tests/.

Run after building:

ctest --progress

Make sure CMake was configured with NFORGE_BUILD_TESTS.

Benchmarks

Benchmarks run on merge with main branch. A performance regression blocks merging.

Current benchmarks are the examples from physics scenarios with default parameters and some microbenchmarks for various hotpath functions. The results are published to marbjo07.github.io/NForge/dev/bench/

Code Formatting

NForge uses clang-format to enforce consistent formatting. Formatting is checked automatically on all pull requests and must pass before merging.

Pre-commit Hook

The easiest way to avoid formatting failures is to install the pre-commit hook, which checks formatting automatically on every commit:

# from project root
pip install pre-commit
python -m pre_commit install

After installation, any commit that fails formatting will be blocked and fixed in-place. Stage the changes and commit again to proceed.

Or run clang-format directly through your editor or IDE.