Skip to content

Commit a9d00c1

Browse files
Mega Update for the tensor class.
1 parent 37b248f commit a9d00c1

File tree

17 files changed

+688
-61
lines changed

17 files changed

+688
-61
lines changed

.github/workflows/python-publish.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ jobs:
2626
uses: actions/setup-python@v3
2727
with:
2828
python-version: '3.x'
29+
- name: Run CUDA bash shell
30+
env:
31+
temp: ${{ runner.temp }}
32+
cuda: '12.9'
33+
run: |
34+
chmod +x ${{github.workspace}}/tensor-array-repo/Tensor-Array/scripts/actions/install-cuda-ubuntu.sh
35+
${{github.workspace}}/tensor-array-repo/Tensor-Array/scripts/actions/install-cuda-ubuntu.sh
36+
shell: bash
2937
- name: Install dependencies
3038
run: |
3139
python -m pip install --upgrade pip

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
include README.md LICENSE tensor-array-repo/Tensor-Array/LICENSE third_party/pybind11/LICENSE
22
graft third_party/pybind11/include
33
graft third_party/pybind11/tools
4+
graft third_party/pybind11/pybind11
45
graft tensor-array-repo/Tensor-Array/src
56
graft src
67
global-include CMakeLists.txt *.cmake Config.cmake.in

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,45 @@ This machine learning library using [Tensor-Array](https://github.com/Tensor-Arr
1010
This project is still in alpha version, we are trying to make this look like the main framework but it is easier to code.
1111

1212
## How to install Tensor-Array python version.
13+
14+
Before install this library please install [NVIDIA CUDA toolkit](https://developer.nvidia.com/cuda-toolkit) first.
15+
16+
It can not work without [NVIDIA CUDA toolkit](https://developer.nvidia.com/cuda-toolkit).
17+
18+
If you did not install [Python](https://www.python.org/) then install [Python 3](https://www.python.org/):
19+
1320
```shell
14-
$ pip install TensorArray
21+
apt-get update
22+
apt-get install python3
23+
```
24+
25+
After that go to command and install:
26+
27+
```shell
28+
pip install TensorArray
29+
```
30+
31+
## Testing with the [Tensor](https://github.com/Tensor-Array/Tensor-Array/tab=readme-ov-file#the-tensor-class) object.
32+
33+
The `Tensor` class is a storage that store value and calculate the tensor.
34+
35+
The `Tensor.calc_grad()` method can do automatic differentiation.
36+
37+
The `Tensor.get_grad()` method can get the gradient after call `Tensor.calc_grad()`.
38+
39+
```python
40+
import tensor_array.core as ta
41+
import numpy as np
42+
43+
def test_add():
44+
example_tensor_array = ta.Tensor(np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], dtype=np.int32))
45+
example_tensor_array_scalar = ta.Tensor(100)
46+
example_tensor_sum = example_tensor_array + example_tensor_array_scalar
47+
print(example_tensor_sum)
48+
example_tensor_sum.calc_grad()
49+
print(example_tensor_array.get_grad())
50+
print(example_tensor_array_scalar.get_grad())
51+
52+
test_add()
53+
1554
```

setup.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,17 @@ def main():
127127
with open(os.path.join(cwd, "README.md"), encoding="utf-8") as f:
128128
long_description = f.read()
129129

130-
packages = find_packages("")
130+
packages = find_packages()
131131

132132
print(packages)
133133

134134
setup(
135135
name = "TensorArray",
136-
version = "0.0.3",
136+
version = "0.0.4",
137137
description = "A machine learning package",
138138
long_description=long_description,
139-
authors = "TensorArray-Creators",
140-
url= "https://github.com/Tensor-Array/Tensor-Array-Python",
139+
author="TensorArray-Creators",
140+
url="https://github.com/Tensor-Array/Tensor-Array-Python",
141141
packages=packages,
142142
ext_modules=[
143143
CMakeExtension("tensor_array/core/tensor2")
@@ -146,6 +146,7 @@ def main():
146146
"Development Status :: 2 - Pre-Alpha",
147147

148148
"Programming Language :: Python :: 3",
149+
"Programming Language :: Python :: 3.8",
149150
"Programming Language :: Python :: 3.9",
150151
"Programming Language :: Python :: 3.10",
151152
"Programming Language :: Python :: 3.11",
@@ -162,6 +163,7 @@ def main():
162163
},
163164
package_dir={
164165
"": "src",
166+
"pybind11": "third_party/pybind11/pybind11",
165167
"tests": "tests"
166168
},
167169
python_requires=">=3.8",

src/tensor_array/_core/tensor_bind.cc

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ DataType tensor_type(const Tensor& self)
150150

151151
Tensor tensor_copying(const Tensor& self)
152152
{
153-
return self;
153+
return Tensor(self);
154154
}
155155

156156
Tensor py_zeros(pybind11::tuple shape_tuple, DataType dtype)
@@ -161,6 +161,14 @@ Tensor py_zeros(pybind11::tuple shape_tuple, DataType dtype)
161161
return TensorBase(warp_type(dtype), shape_vec);
162162
}
163163

164+
Tensor py_rand(pybind11::tuple shape_tuple, unsigned int seed = std::rand())
165+
{
166+
std::vector<unsigned int> shape_vec;
167+
for (auto& it: shape_tuple)
168+
shape_vec.push_back(it.cast<unsigned int>());
169+
return tensor_rand(shape_vec, seed);
170+
}
171+
164172
PYBIND11_MODULE(tensor2, m)
165173
{
166174
pybind11::enum_<DataType>(m, "DataType")
@@ -187,6 +195,13 @@ PYBIND11_MODULE(tensor2, m)
187195
pybind11::arg("dtype") = S_INT_32
188196
);
189197

198+
m.def(
199+
"rand",
200+
&py_rand,
201+
pybind11::arg("shape"),
202+
pybind11::arg("seed") = std::rand()
203+
);
204+
190205
m.def(
191206
"add",
192207
&tensor_array::value::add,
@@ -207,6 +222,13 @@ PYBIND11_MODULE(tensor2, m)
207222
pybind11::arg("value_1"),
208223
pybind11::arg("value_2")
209224
);
225+
226+
m.def(
227+
"power",
228+
&tensor_array::value::power,
229+
pybind11::arg("value_1"),
230+
pybind11::arg("value_2")
231+
);
210232

211233
m.def(
212234
"matmul",
@@ -245,17 +267,17 @@ PYBIND11_MODULE(tensor2, m)
245267
.def(+pybind11::self)
246268
.def(-pybind11::self)
247269
.def(hash(pybind11::self))
248-
.def("transpose", &tensor_array::value::Tensor::transpose)
249-
.def("calc_grad", &tensor_array::value::Tensor::calc_grad)
250-
.def("get_grad", &tensor_array::value::Tensor::get_grad)
251-
.def("sin", &tensor_array::value::Tensor::sin)
252-
.def("cos", &tensor_array::value::Tensor::cos)
253-
.def("tan", &tensor_array::value::Tensor::tan)
254-
.def("sinh", &tensor_array::value::Tensor::sinh)
255-
.def("cosh", &tensor_array::value::Tensor::cosh)
256-
.def("tanh", &tensor_array::value::Tensor::tanh)
257-
.def("log", &tensor_array::value::Tensor::log)
258-
.def("clone", &tensor_array::value::Tensor::clone)
270+
.def("transpose", &Tensor::transpose)
271+
.def("calc_grad", &Tensor::calc_grad)
272+
.def("get_grad", &Tensor::get_grad)
273+
.def("sin", &Tensor::sin)
274+
.def("cos", &Tensor::cos)
275+
.def("tan", &Tensor::tan)
276+
.def("sinh", &Tensor::sinh)
277+
.def("cosh", &Tensor::cosh)
278+
.def("tanh", &Tensor::tanh)
279+
.def("log", &Tensor::log)
280+
.def("clone", &Tensor::clone)
259281
.def("cast", &tensor_cast_1)
260282
.def("numpy", &convert_tensor_to_numpy)
261283
.def("shape", &tensor_shape)
@@ -264,8 +286,7 @@ PYBIND11_MODULE(tensor2, m)
264286
.def("__getitem__", &python_slice)
265287
.def("__getitem__", &python_tuple_slice)
266288
.def("__len__", &python_len)
267-
.def("__matmul__", &tensor_array::value::matmul)
268-
.def("__rmatmul__", &tensor_array::value::matmul)
289+
.def("__matmul__", &matmul)
269290
.def("__repr__", &tensor_to_string)
270291
.def("__copy__", &tensor_copying);
271292
}

src/tensor_array/activation.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from tensor_array.core import Tensor
22
from tensor_array.core import zeros
3+
from tensor_array.core import condition
34

4-
def relu(input):
5+
def relu(input: Tensor) -> Tensor:
56
tensor_zeros = zeros(shape = input.shape(), dtype = input.dtype())
6-
return (input > tensor_zeros).condition(input, tensor_zeros)
7+
return condition(input > tensor_zeros, input, tensor_zeros)
78

8-
def sigmoid(input):
9+
def sigmoid(input: Tensor) -> Tensor:
910
return input.sigmoid()
1011

11-
def softmax(input, dim = 0):
12-
return input
12+
def softmax(input: Tensor, dim: int = 0) -> Tensor:
13+
return input.softmax(dim)

src/tensor_array/core/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
# src/tensor_array/core/__init__.py
3+
# This module provides core functionalities for the TensorArray library, including tensor creation, random tensor generation, and data type definitions.
4+
# It includes functions to create tensors filled with zeros or random values, and defines the DataTypes enumeration for various data types supported by the library.
5+
"""
6+
17
from .tensor import Tensor
28
from .constants import *
39
from .datatypes import DataTypes

src/tensor_array/core/constants.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
1-
from .tensor2 import zeros as zerosWrapper
1+
"""
2+
# src/tensor_array/core/__init__.py
3+
# This module provides core functionalities for the TensorArray library, including tensor creation, random tensor generation, and data type definitions.
4+
# It includes functions to create tensors filled with zeros or random values, and defines the DataTypes enumeration for various data types supported by the library.
5+
"""
6+
27
from .tensor import Tensor
38
from .datatypes import DataTypes
49

5-
def zeros(shape : Tensor, dtype : DataTypes = DataTypes.S_INT_32):
6-
return zerosWrapper(shape, dtype)
10+
def zeros(shape : tuple, dtype : DataTypes = DataTypes.S_INT_32) -> Tensor:
11+
"""
12+
Creates a tensor filled with zeros.
13+
Args:
14+
shape (tuple): The shape of the tensor.
15+
dtype (DataTypes): The data type of the tensor.
16+
Returns:
17+
Tensor: A tensor filled with zeros.
18+
"""
19+
20+
from .tensor2 import zeros as zerosWrapper
21+
return zerosWrapper(shape, dtype)
22+
23+
def rand(shape : tuple, seed: int = 0) -> Tensor:
24+
"""
25+
Generates a tensor with random values.
26+
27+
Args:
28+
shape (tuple): The shape of the tensor.
29+
seed (int): The seed for random number generation.
30+
31+
Returns:
32+
Tensor: A tensor filled with random values.
33+
"""
34+
35+
from .tensor2 import rand as randWrapper
36+
return randWrapper(shape, seed)

src/tensor_array/core/datatypes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
"""
2+
# src/tensor_array/core/datatypes.py
3+
# This module defines an enumeration for various data types supported by the TensorArray library.
4+
# The DataTypes enum includes types such as BOOL, INT, FLOAT, DOUBLE, and others, which correspond to the data types used in tensors.
5+
"""
6+
17
from .tensor2 import DataType as DataTypeWrapper
28
from enum import Enum
39

410
class DataTypes(Enum):
11+
"""
12+
Enum representing various data types supported by the TensorArray library.
13+
"""
514
BOOL = DataTypeWrapper.BOOL
615
S_INT_8 = DataTypeWrapper.S_INT_8
716
S_INT_16 = DataTypeWrapper.S_INT_16

src/tensor_array/core/operator.py

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,80 @@
1+
"""
2+
# src/tensor_array/core/operator.py
3+
# This module provides various mathematical operations for tensors, including addition, division, multiplication, power, matrix multiplication, and conditional selection.
4+
# Each operation is implemented as a function that takes two tensors as input and returns a new tensor representing the result of the operation.
5+
"""
6+
17
from .tensor import Tensor
2-
from .tensor2 import add as addWrapper
3-
from .tensor2 import multiply as multiplyWrapper
4-
from .tensor2 import divide as divideWrapper
5-
from .tensor2 import matmul as matmulWrapper
6-
from .tensor2 import condition as conditionWrapper
78

8-
def add(value_1 : Tensor, value_2 : Tensor):
9+
def add(value_1 : Tensor, value_2 : Tensor) -> Tensor:
10+
"""
11+
Adds two tensors element-wise.
12+
Args:
13+
value_1 (Tensor): The first tensor.
14+
value_2 (Tensor): The second tensor.
15+
Returns:
16+
Tensor: A tensor that is the element-wise sum of value_1 and value_2
17+
"""
18+
from .tensor2 import add as addWrapper
919
return addWrapper(value_1, value_2)
1020

11-
def divide(value_1 : Tensor, value_2 : Tensor):
21+
def divide(value_1 : Tensor, value_2 : Tensor) -> Tensor:
22+
"""
23+
Divides two tensors element-wise.
24+
Args:
25+
value_1 (Tensor): The first tensor.
26+
value_2 (Tensor): The second tensor.
27+
Returns:
28+
Tensor: A tensor that is the element-wise division of value_1 by value_2
29+
"""
30+
from .tensor2 import divide as divideWrapper
31+
return divideWrapper(value_1, value_2)
32+
33+
def multiply(value_1 : Tensor, value_2 : Tensor) -> Tensor:
34+
"""
35+
Multiplies two tensors element-wise.
36+
Args:
37+
value_1 (Tensor): The first tensor.
38+
value_2 (Tensor): The second tensor.
39+
Returns:
40+
Tensor: A tensor that is the element-wise product of value_1 and value_2
41+
"""
42+
from .tensor2 import multiply as multiplyWrapper
1243
return multiplyWrapper(value_1, value_2)
1344

14-
def multiply(value_1 : Tensor, value_2 : Tensor):
15-
return divideWrapper(value_1, value_2)
45+
def power(value_1 : Tensor, value_2 : Tensor) -> Tensor:
46+
"""
47+
Raises the first tensor to the power of the second tensor element-wise.
48+
Args:
49+
value_1 (Tensor): The base tensor.
50+
value_2 (Tensor): The exponent tensor.
51+
Returns:
52+
Tensor: A tensor that is the element-wise result of value_1 raised to the power of value_2
53+
"""
54+
from .tensor2 import power as powerWrapper
55+
return powerWrapper(value_1, value_2)
1656

17-
def matmul(value_1 : Tensor, value_2 : Tensor):
57+
def matmul(value_1 : Tensor, value_2 : Tensor) -> Tensor:
58+
"""
59+
Performs matrix multiplication between two tensors.
60+
Args:
61+
value_1 (Tensor): The first tensor.
62+
value_2 (Tensor): The second tensor.
63+
Returns:
64+
Tensor: A tensor that is the result of matrix multiplication between value_1 and value_2
65+
"""
66+
from .tensor2 import matmul as matmulWrapper
1867
return matmulWrapper(value_1, value_2)
1968

20-
def condition(condition_value : Tensor, value_if_true : Tensor, value_if_false : Tensor):
69+
def condition(condition_value : Tensor, value_if_true : Tensor, value_if_false : Tensor) -> Tensor:
70+
"""
71+
Chooses between two tensors based on a condition tensor.
72+
Args:
73+
condition_value (Tensor): The condition tensor.
74+
value_if_true (Tensor): The tensor to return if the condition is true.
75+
value_if_false (Tensor): The tensor to return if the condition is false.
76+
Returns:
77+
Tensor: A tensor that is either value_if_true or value_if_false, depending on the condition.
78+
"""
79+
from .tensor2 import condition as conditionWrapper
2180
return conditionWrapper(condition_value, value_if_true, value_if_false)
22-

0 commit comments

Comments
 (0)