Skip to content

Commit 993046a

Browse files
test
1 parent ca6fb69 commit 993046a

File tree

8 files changed

+101
-28
lines changed

8 files changed

+101
-28
lines changed

CPythonTensor/CPythonTensor.vcxproj

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
<Platform>x64</Platform>
1919
</ProjectConfiguration>
2020
</ItemGroup>
21+
<ItemGroup>
22+
<ClCompile Include="tensor_bind.cc" />
23+
</ItemGroup>
2124
<PropertyGroup Label="Globals">
2225
<VCProjectVersion>17.0</VCProjectVersion>
2326
<Keyword>Win32Proj</Keyword>
@@ -110,7 +113,7 @@
110113
<SDLCheck>true</SDLCheck>
111114
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
112115
<ConformanceMode>true</ConformanceMode>
113-
<AdditionalIncludeDirectories>..\..\TensorCore\TensorCore;C:\Users\Noob\AppData\Local\Programs\Python\Python311\include;C:\Users\Noob\AppData\Local\Programs\Python\Python311\Lib\site-packages\numpy\core\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
116+
<AdditionalIncludeDirectories>..\..\TensorCore\TensorCore;C:\Users\Noob\AppData\Local\Programs\Python\Python311\include;C:\Users\Noob\AppData\Local\Programs\Python\Python311\Lib\site-packages\numpy\core\include;C:\Users\Noob\AppData\Local\Programs\Python\Python311\Lib\site-packages\pybind11\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
114117
<LanguageStandard>stdcpp17</LanguageStandard>
115118
</ClCompile>
116119
<Link>
@@ -130,7 +133,7 @@
130133
<SDLCheck>true</SDLCheck>
131134
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
132135
<ConformanceMode>true</ConformanceMode>
133-
<AdditionalIncludeDirectories>..\..\TensorCore\TensorCore;C:\Users\Noob\AppData\Local\Programs\Python\Python311\include;C:\Users\Noob\AppData\Local\Programs\Python\Python311\Lib\site-packages\numpy\core\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
136+
<AdditionalIncludeDirectories>..\..\TensorCore\TensorCore;C:\Users\Noob\AppData\Local\Programs\Python\Python311\include;C:\Users\Noob\AppData\Local\Programs\Python\Python311\Lib\site-packages\numpy\core\include;C:\Users\Noob\AppData\Local\Programs\Python\Python311\Lib\site-packages\pybind11\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
134137
<LanguageStandard>stdcpp17</LanguageStandard>
135138
</ClCompile>
136139
<Link>
@@ -145,13 +148,6 @@
145148
<Command>xcopy /y /d "..\..\TensorCore\$(IntDir)TensorCore.dll" "$(OutDir)"</Command>
146149
</PostBuildEvent>
147150
</ItemDefinitionGroup>
148-
<ItemGroup>
149-
<ClCompile Include="module.c" />
150-
<ClCompile Include="py_tensor.cpp" />
151-
</ItemGroup>
152-
<ItemGroup>
153-
<ClInclude Include="module.h" />
154-
</ItemGroup>
155151
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
156152
<ImportGroup Label="ExtensionTargets">
157153
</ImportGroup>

CPythonTensor/CPythonTensor.vcxproj.filters

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,8 @@
1515
</Filter>
1616
</ItemGroup>
1717
<ItemGroup>
18-
<ClCompile Include="module.c">
18+
<ClCompile Include="tensor_bind.cc">
1919
<Filter>Source Files</Filter>
2020
</ClCompile>
21-
<ClCompile Include="py_tensor.cpp">
22-
<Filter>Source Files</Filter>
23-
</ClCompile>
24-
</ItemGroup>
25-
<ItemGroup>
26-
<ClInclude Include="module.h">
27-
<Filter>Header Files</Filter>
28-
</ClInclude>
2921
</ItemGroup>
3022
</Project>

CPythonTensor/module.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ static int Tensor_init(PyTensorObject* self, PyObject* args)
3030
self->t = call_tensor(ndims, c_dims, PyArray_DATA(np_array));
3131
}
3232
free(c_dims);
33+
Py_DECREF(np_array);
3334
return self;
3435
}
3536

@@ -54,15 +55,26 @@ Tensor_ToString(PyTensorObject* self)
5455
return PyUnicode_FromString(to_string(self->t));
5556
}
5657

57-
static PyMethodDef Tensor_methods[] = {
58+
static PyTensorObject* Tensor_add(PyTensorObject* self, PyTensorObject* other)
59+
{
60+
PyTensorObject* val;
61+
if (self != NULL) {
62+
self->t = add_tensor(self->t, other->t);
63+
}
64+
return self;
65+
}
66+
67+
static PyMethodDef Tensor_methods[] =
68+
{
69+
//{"__add__", Tensor_add, METH_O, ""},
5870
{NULL} /* Sentinel */
5971
};
6072

6173
static PyTypeObject TensorType =
6274
{
6375
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
6476
.tp_name = "tensor.Tensor",
65-
.tp_doc = PyDoc_STR("Custom objects"),
77+
.tp_doc = PyDoc_STR("Tensor"),
6678
.tp_basicsize = sizeof(PyTensorObject),
6779
.tp_itemsize = 0,
6880
.tp_flags = Py_TPFLAGS_DEFAULT,

CPythonTensor/module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extern "C"
77
void* call_tensor(unsigned int nd, unsigned int* dimensions, const void* data);
88
void delete_tensor(void* t);
99
const char* to_string(void* t);
10+
void* add_tensor(const void* a, const void* b);
1011
#ifdef __cplusplus
1112
}
1213
#endif // __cplusplus

CPythonTensor/py_tensor.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ void delete_tensor(void* t)
1414
delete t;
1515
}
1616

17-
void add_tensor(const void* a, const void* b)
17+
void* add_tensor(const void* a, const void* b)
1818
{
19-
return;
19+
const Tensor* t_a = static_cast<const Tensor*>(a);
20+
const Tensor* t_b = static_cast<const Tensor*>(b);
21+
return new Tensor(add(*t_a, *t_b));
2022
}
2123

2224
const char* to_string(void* t)
2325
{
2426
Tensor* t1 = static_cast<Tensor*>(t);
25-
std::ostringstream steam;
26-
steam << *t1;
27-
return steam.str().c_str();
27+
std::ostringstream stream;
28+
stream << *t1;
29+
return stream.str().c_str();
2830
}

CPythonTensor/tensor_bind.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <tensor.hh>
2+
#include <pybind11/pybind11.h>
3+
#include <pybind11/numpy.h>
4+
#include <pybind11/operators.h>
5+
6+
using namespace tensor_array::value;
7+
8+
template <typename T>
9+
TensorBase convert_numpy_to_tensor_base(pybind11::array_t<T> py_buf)
10+
{
11+
pybind11::buffer_info info = py_buf.request();
12+
std::vector<unsigned int> shape_vec(info.ndim);
13+
std::transform
14+
(
15+
info.shape.cbegin(),
16+
info.shape.cend(),
17+
shape_vec.begin(),
18+
[](pybind11::size_t dim)
19+
{
20+
return static_cast<unsigned int>(dim);
21+
}
22+
);
23+
return TensorBase(typeid(T), shape_vec, info.ptr);
24+
}
25+
26+
std::string tensor_to_string(const Tensor t)
27+
{
28+
std::ostringstream osstream;
29+
osstream << t;
30+
return osstream.str();
31+
}
32+
33+
PYBIND11_MODULE(tensor, m)
34+
{
35+
pybind11::class_<Tensor>(m, "TensorC")
36+
.def(pybind11::init())
37+
.def(pybind11::init(&convert_numpy_to_tensor_base<float>))
38+
.def(pybind11::self + pybind11::self)
39+
.def(pybind11::self - pybind11::self)
40+
.def(pybind11::self * pybind11::self)
41+
.def(pybind11::self / pybind11::self)
42+
.def(pybind11::self += pybind11::self)
43+
.def(pybind11::self -= pybind11::self)
44+
.def(pybind11::self *= pybind11::self)
45+
.def(pybind11::self /= pybind11::self)
46+
.def(pybind11::self == pybind11::self)
47+
.def(pybind11::self != pybind11::self)
48+
.def(pybind11::self >= pybind11::self)
49+
.def(pybind11::self <= pybind11::self)
50+
.def(pybind11::self > pybind11::self)
51+
.def(pybind11::self < pybind11::self)
52+
.def(+pybind11::self)
53+
.def(-pybind11::self)
54+
.def("__matmul__", &matmul)
55+
.def("__repr__", &tensor_to_string);
56+
}
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
from typing import Self
12
import numpy as np
23
import tensor as t
34

45
class Tensor:
5-
def __init__(self, arr: np.ndarray):
6-
self.temp_tensor = t.Tensor(arr)
6+
def __init__(self, arr, *args):
7+
self.temp_tensor = t.TensorC(arr)
8+
9+
def __add__(self, other) -> Self:
10+
result = Tensor(0);
11+
result.temp_tensor = self.temp_tensor + other.temp_tensor
12+
return result;
713

14+
def __matmul__(self, other) -> Self:
15+
result = Tensor(0);
16+
result.temp_tensor = self.temp_tensor @ other.temp_tensor
17+
return result;
818

19+
def __str__(self) -> str:
20+
return self.temp_tensor.__str__()
921

1022

PythonTensorTesting/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
if __name__ == '__main__':
1010
t3 = np.array([[1, 2, 3], [4, 5, 6]])
1111
t1 = py_t_arr.Tensor([[1, 2.5, 3], [4, 5, 6]])
12+
t2 = py_t_arr.Tensor([[1, 2.5], [4, 5], [7, 8]])
13+
t3 = t1 @ t2
1214
print("Hello")
13-
print(t1.temp_tensor)
15+
print(t3)

0 commit comments

Comments
 (0)