Introduction to the Tensor Project
The Tensor project is an endeavor to develop a simple one-dimensional tensor in C, akin to similar objects in popular numerical libraries like PyTorch's torch.Tensor
and NumPy's numpy.ndarray
. This project focuses on creating a basic float tensor that allows efficient slicing and access of the data.
Core Components
The project is centered around a 1-dimensional Tensor
that is equipped to hold data efficiently. It achieves this by maintaining two crucial elements:
- Storage: This is an array-like structure that physically holds the 1-dimensional data in memory.
- View: It serves as an interface over the
Storage
, managing information about the start, end, and stride for data access. TheView
enables slicing without additional memory allocation, as it reuses theStorage
and merely updates these parameters.
Building and Running the C Code
The source code can be found in two files, tensor1d.h
and tensor1d.c
, which define and implement the 1D tensor. To compile and execute the code, use the following commands:
gcc -Wall -O3 tensor1d.c -o tensor1d
./tensor1d
Integrating with Python
One of the significant features of the Tensor project is its ability to integrate with Python, making it more versatile. By compiling the C code as a shared library, it can be accessed in Python through the cffi
library:
gcc -O3 -shared -fPIC -o libtensor1d.so tensor1d.c
The shared library, libtensor1d.so
, can then be loaded into Python and used alongside a script, such as tensor1d.py
. This allows for interactive manipulation of the tensor object using Python syntax.
Examples and Usage
Here are a few illustrative examples showcasing the tensor's capabilities in Python:
-
Create a range of values using
arange
:import tensor1d t = tensor1d.arange(20)
-
Access and modify elements:
print(t[3]) # Outputs: 3.0 t[-1] = 100.0 # Changes the last element to 100.0
-
Slicing and nested slices:
print(t[5:15:2]) # Prints: [5, 7, 9, 11, 13] print(t[5:15:2][2:7]) # Prints: [9, 11, 13]
-
Performing arithmetic operations:
t = t + 10.0 t2 = tensor1d.arange(20) t3 = t + t2 t4 = t + tensor1d.tensor([10.0])
Testing and Validation
The project includes a suite of tests using the pytest
framework to ensure the integrity and reliability of the tensor operations, located in test_tensor1d.py
. These tests can be executed with:
pytest test_tensor1d.py
Future Directions and Enhancements
The Tensor project is first and foremost a learning tool, providing insights into tensor manipulation in C and Python. However, future improvements aim to bring its functionality closer to production-grade libraries like PyTorch. Planned enhancements include:
- Enhancing test coverage.
- Extending the implementation to support 2D tensors.
- Integrating additional features like broadcasting in multi-dimensional tensors.
Related Resources
For more insight into tensors and their implementations, these resources are recommended:
Conclusion
The Tensor project offers a foundational understanding of tensors, emphasizing memory efficiency and integration with Python. While simple, it serves as a stepping stone toward more complex tensor functionalities and even deeper exploration of numerical computing frameworks. The project is open-source under the MIT License, encouraging further development and exploration.