Skip to content

A Powerful, Superfast Multidimensional Tensor Library for C/C++

License

muhammad-fiaz/Tensr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

14 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Tensr

A Powerful, Superfast Multidimensional Tensor Library for C/C++

Version License Last Commit Issues Pull Requests Platform Tests Release Deploy Documentation

๐Ÿ“š Documentation โ€ข ๐Ÿš€ Quick Start โ€ข ๐Ÿค Contributing

๐ŸŒŸ Features

  • High Performance: Optimized for speed with SIMD and multi-threading support
  • GPU Acceleration: Full CUDA support for GPU computing (CUDA, XPU, NPU, TPU)
  • Simple API: Clean, intuitive C and C++ interfaces
  • Comprehensive: All essential tensor operations for scientific computing
  • Production Ready: Thoroughly tested, documented, and battle-tested
  • Cross-Platform: Works on Windows, Linux, and macOS
  • Zero Dependencies: Core library has no external dependencies
  • Memory Efficient: Smart memory management with minimal overhead

๐Ÿ“ฆ Installation

Download Pre-built Binaries

Download the latest release for your platform from GitHub Releases:

  • Linux: tensr-linux-x64.tar.gz
  • Windows: tensr-windows-x64.zip
  • macOS: tensr-macos-x64.tar.gz

Extract and copy to your system:

Linux/macOS:

tar -xzf tensr-linux-x64.tar.gz
sudo cp -r lib/* /usr/local/lib/
sudo cp -r include/* /usr/local/include/

Windows: Extract the zip file and add the lib and include directories to your project paths.

Using xmake (Recommended)

For xmake users, download tensr-xmake-{version}.tar.gz from releases:

tar -xzf tensr-xmake-0.0.0.tar.gz
cd tensr-xmake-0.0.0
xmake install

Or add to your xmake.lua:

add_includedirs("/path/to/tensr/include")
add_linkdirs("/path/to/tensr/lib")
add_links("tensr")

From Source

git clone https://github.com/muhammad-fiaz/tensr.git
cd tensr
xmake build
xmake install

๐Ÿš€ Quick Start

C API

#include <tensr/tensr.h>
#include <tensr/tensr_array.h>

int main() {
    /* Create arrays from data (like np.array) */
    float data_a[] = {1, 2, 3};
    float data_b[] = {4, 5, 6};
    size_t shape[] = {3};
    
    Tensor* a = tensr_from_array(shape, 1, TENSR_FLOAT32, TENSR_CPU, data_a);
    Tensor* b = tensr_from_array(shape, 1, TENSR_FLOAT32, TENSR_CPU, data_b);
    
    /* Element-wise operations */
    Tensor* sum = tensr_add(a, b);      /* a + b */
    Tensor* product = tensr_mul(a, b);  /* a * b */
    Tensor* squared = tensr_pow(a, 2.0); /* a ** 2 */
    
    /* Print results */
    tensr_print(sum);
    tensr_print(product);
    tensr_print(squared);
    
    /* Cleanup */
    tensr_free(a);
    tensr_free(b);
    tensr_free(sum);
    tensr_free(product);
    tensr_free(squared);
    
    return 0;
}

C++ API

#include <tensr/tensr.hpp>

int main() {
    /* Create tensors */
    auto t1 = tensr::Tensor::ones({3, 3});
    auto t2 = tensr::Tensor::rand({3, 3});
    
    /* Perform operations */
    auto result = t1 + t2;
    auto sum = result.sum();
    
    /* Print result */
    result.print();
    
    return 0;
}

๐ŸŽฏ Core Operations

Tensor Creation

  • zeros() - Create tensor filled with zeros
  • ones() - Create tensor filled with ones
  • full() - Create tensor filled with a value
  • arange() - Create tensor with evenly spaced values
  • linspace() - Create tensor with linearly spaced values
  • eye() - Create identity matrix
  • rand() - Create tensor with random values
  • randn() - Create tensor with normal distribution

Arithmetic Operations

  • add(), sub(), mul(), div() - Element-wise operations
  • pow(), sqrt(), exp(), log() - Mathematical functions
  • sin(), cos(), tan() - Trigonometric functions
  • abs(), neg() - Unary operations

Linear Algebra

  • dot() - Dot product
  • matmul() - Matrix multiplication
  • inv() - Matrix inverse
  • det() - Determinant
  • svd() - Singular value decomposition
  • eig() - Eigenvalues and eigenvectors

Reduction Operations

  • sum() - Sum of elements
  • mean() - Mean of elements
  • max(), min() - Maximum and minimum
  • argmax(), argmin() - Indices of max/min

Shape Manipulation

  • reshape() - Change tensor shape
  • transpose() - Transpose dimensions
  • squeeze() - Remove single dimensions
  • expand_dims() - Add dimensions
  • concat(), stack() - Combine tensors

๐Ÿ–ฅ๏ธ GPU Support

Tensr supports multiple accelerator backends:

/* CUDA GPU */
Tensor* t = tensr_zeros(shape, 2, TENSR_FLOAT32, TENSR_CUDA);

/* Transfer between devices */
tensr_to_device(t, TENSR_CUDA, 0);

Supported devices:

  • CPU: Standard CPU execution
  • CUDA: NVIDIA GPU acceleration
  • XPU: Intel XPU support
  • NPU: Neural Processing Unit
  • TPU: Tensor Processing Unit

๐Ÿ“Š Performance

Tensr is designed for maximum performance:

  • SIMD Optimizations: Vectorized operations for CPU
  • GPU Acceleration: CUDA kernels for parallel computing
  • Memory Efficiency: Minimal allocations and smart caching
  • Multi-threading: Parallel execution for large tensors

๐Ÿงช Testing

Run the test suite:

xmake build tests
xmake run tests

All tests must pass before release.

๐Ÿ“– Documentation

Full documentation is available at https://muhammad-fiaz.github.io/Tensr/

Do's โœ…

  • Use appropriate data types for your use case
  • Free tensors when done to avoid memory leaks
  • Check return values for NULL
  • Use GPU for large-scale computations
  • Profile your code for bottlenecks

Don'ts โŒ

  • Don't mix tensors from different devices without transfer
  • Don't modify tensor data directly
  • Don't forget to synchronize after GPU operations
  • Don't use debug builds in production
  • Don't ignore compiler warnings

๐Ÿค Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

๐Ÿ“„ License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

๐Ÿ‘ค Author

Muhammad Fiaz

๐Ÿ™ Acknowledgments

Special thanks to all contributors and the open-source community.

๐Ÿ“ฎ Support


๐Ÿ› Bug Reports

Found a bug? Please open an issue on GitHub.


Star History Chart

โญ Star the repository if you find Tensr useful!