MTensor is a CPU-based deep learning library written in C++, inspired by PyTorch's design. It supports dynamic graphs, training, autograd, and a wide range of tensor and neural network operations.
⚠️ Beta Notice: MTensor 2.0 is tested on Windows (MSVC 17) and Linux (GCC 11+/Clang 14+). macOS support is coming soon.
- oneDNN – High-performance tensor primitives
- OpenMP – Threaded parallelism
- AVX512 – Custom fused operations for optimizers, reductions
- CImg + libpng – Image handling support
- nlohmann/json – Exporting graphs and manual tensor initialization
- vcpkg – Dependency manager
- Internal
TensorImplwith smart pointers - Tensor methods:
shape(),stride(),numel(),randn(),ones(),arange(),fill(), ... - Torch-style formatted printing
- Broadcasting support (binary ops, matmul)
- Arithmetic & Logic:
+,-,*,/,<,>,==,min,max - Elementwise:
abs,clip,exp,log,pow,relu,sigmoid, ... - Reduction:
sum,mean,min,max,mul,norm_lp_*, ... - Normalization:
batchnorm,groupnorm,layernorm,rmsnorm - Convolution:
conv1d/2d/3d,transposedConv1d/2d/3d - Pooling:
avgpool,maxpool(1d, 2d, 3d) - Memory & Views:
clone,reshape,permute,transpose,squeeze,contiguous - Matrix ops:
matmul,A @ B + C - Other:
embedding,softmax,logsoftmax,cat(),stack()
- Dynamic computation graph with
backward() - Tensor gradient helpers:
requires_grad,detach,grad, ... - Visualize autograd graph as an interactive HTML (
build_graph())
- Layers:
Linear,Conv,TransposeConv,Embedding,Dropout,Flatten,Unflatten,Sequential - Activations:
ReLU,LeakyReLU,Sigmoid,Tanh,Softmax,LogSoftmax - Losses:
MSELoss,L1Loss,CrossEntropy,KLDiv,BCEWithLogits - Optimizers (AVX512-fused):
SGD,MomentumSGD,Adam,AdamW - Initializers: Kaiming and Xavier (Uniform/Normal)
-
Datasets:
MNISTDataset(reads MNIST binary format)ImageFolderDataset(uses CImg for loading images)
-
Dataloader: For training batches
-
Image I/O:
image_to_tensor(),tensor_to_image()(via CImg)
🧪 MTensor 2.0 is tested on Windows (MSVC 17) and Linux (GCC 11+/Clang 14+). All dependencies are managed with
vcpkg.
- Visual Studio 2022 (MSVC 17)
- CMake 3.31.6+
sudo apt-get install -y build-essential ninja-build cmake libomp-devsudo dnf install -y gcc-c++ ninja-build cmake libomp-develsudo pacman -S gcc ninja cmake openmpgit clone --recurse-submodules https://github.com/your-username/MTensor.git
cd MTensor✅
vcpkgis included as a submodule underexternal/vcpkg.
If needed:
git submodule update --init --recursiveBootstrap vcpkg:
cd external/vcpkg
./bootstrap-vcpkg.bat # Windows
./bootstrap-vcpkg.sh # LinuxInstall dependencies:
./vcpkg install📦 Dependencies are defined in
vcpkg.json.
🔧 On Linux, vcpkg will automatically use the
x64-linuxtriplet.
Use the default CMake preset:
cmake --preset windows-vs2022cmake --preset linux-ninja🛠️ To change generator or toolchain, edit
CMakePresets.json.
Build in release mode:
cmake --build --preset releaseOr in debug mode:
cmake --build --preset debugBuild in release mode:
cmake --build --preset linux-releaseOr in debug mode:
cmake --build --preset linux-debug🧪 The build produces a shared library (
MTensor.dllon Windows,libMTensor.soon Linux) in thebuild/directory.
- ✅ Tensor & autograd engine
- ✅ Neural network layers and optimizers
- ✅ Dataset/dataloader system
- ✅ Linux support
- 🔜 External project integration
- 🔜 macOS support
- 🔜 Python bindings
- 🔜 Training UI (browser-based)