From b1ac4a9fb87e16ab5171b31b0c5826620cf9df31 Mon Sep 17 00:00:00 2001 From: Noob <63889503+BigNoobWasTaken@users.noreply.github.com> Date: Sun, 24 Dec 2023 20:50:00 +0700 Subject: [PATCH 1/9] Add .gitattributes and .gitignore. --- .gitattributes | 63 +++++++++ .gitignore | 363 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 426 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9491a2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,363 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Oo]ut/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd \ No newline at end of file From beabaebe0a3cde5e241b76b2444b195bb5f6c75c Mon Sep 17 00:00:00 2001 From: Noob <63889503+BigNoobWasTaken@users.noreply.github.com> Date: Sun, 24 Dec 2023 20:50:02 +0700 Subject: [PATCH 2/9] Add project files. --- CPythonTensor.sln | 45 +++++ CPythonTensor/CPythonTensor.vcxproj | 158 ++++++++++++++++++ CPythonTensor/CPythonTensor.vcxproj.filters | 30 ++++ CPythonTensor/module.c | 85 ++++++++++ CPythonTensor/module.h | 12 ++ CPythonTensor/py_tensor.cpp | 23 +++ PythonTensorTesting/PyTensorArray/__init__.py | 10 ++ PythonTensorTesting/PyTensorArray/tensor.py | 0 .../PythonTensorTesting.pyproj | 60 +++++++ PythonTensorTesting/main.py | 13 ++ 10 files changed, 436 insertions(+) create mode 100644 CPythonTensor.sln create mode 100644 CPythonTensor/CPythonTensor.vcxproj create mode 100644 CPythonTensor/CPythonTensor.vcxproj.filters create mode 100644 CPythonTensor/module.c create mode 100644 CPythonTensor/module.h create mode 100644 CPythonTensor/py_tensor.cpp create mode 100644 PythonTensorTesting/PyTensorArray/__init__.py create mode 100644 PythonTensorTesting/PyTensorArray/tensor.py create mode 100644 PythonTensorTesting/PythonTensorTesting.pyproj create mode 100644 PythonTensorTesting/main.py diff --git a/CPythonTensor.sln b/CPythonTensor.sln new file mode 100644 index 0000000..1a115f2 --- /dev/null +++ b/CPythonTensor.sln @@ -0,0 +1,45 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34316.72 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CPythonTensor", "CPythonTensor\CPythonTensor.vcxproj", "{79ABB953-907D-469F-9723-42ED1915A4B0}" +EndProject +Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "PythonTensorTesting", "PythonTensorTesting\PythonTensorTesting.pyproj", "{99206FE6-CF46-4140-93B2-3F659D2CA890}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {79ABB953-907D-469F-9723-42ED1915A4B0}.Debug|Any CPU.ActiveCfg = Debug|x64 + {79ABB953-907D-469F-9723-42ED1915A4B0}.Debug|Any CPU.Build.0 = Debug|x64 + {79ABB953-907D-469F-9723-42ED1915A4B0}.Debug|x64.ActiveCfg = Debug|x64 + {79ABB953-907D-469F-9723-42ED1915A4B0}.Debug|x64.Build.0 = Debug|x64 + {79ABB953-907D-469F-9723-42ED1915A4B0}.Debug|x86.ActiveCfg = Debug|Win32 + {79ABB953-907D-469F-9723-42ED1915A4B0}.Debug|x86.Build.0 = Debug|Win32 + {79ABB953-907D-469F-9723-42ED1915A4B0}.Release|Any CPU.ActiveCfg = Release|x64 + {79ABB953-907D-469F-9723-42ED1915A4B0}.Release|Any CPU.Build.0 = Release|x64 + {79ABB953-907D-469F-9723-42ED1915A4B0}.Release|x64.ActiveCfg = Release|x64 + {79ABB953-907D-469F-9723-42ED1915A4B0}.Release|x64.Build.0 = Release|x64 + {79ABB953-907D-469F-9723-42ED1915A4B0}.Release|x86.ActiveCfg = Release|Win32 + {79ABB953-907D-469F-9723-42ED1915A4B0}.Release|x86.Build.0 = Release|Win32 + {99206FE6-CF46-4140-93B2-3F659D2CA890}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {99206FE6-CF46-4140-93B2-3F659D2CA890}.Debug|x64.ActiveCfg = Debug|Any CPU + {99206FE6-CF46-4140-93B2-3F659D2CA890}.Debug|x86.ActiveCfg = Debug|Any CPU + {99206FE6-CF46-4140-93B2-3F659D2CA890}.Release|Any CPU.ActiveCfg = Release|Any CPU + {99206FE6-CF46-4140-93B2-3F659D2CA890}.Release|x64.ActiveCfg = Release|Any CPU + {99206FE6-CF46-4140-93B2-3F659D2CA890}.Release|x86.ActiveCfg = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F504DBBF-809B-49DA-9604-93EE9EB84AB4} + EndGlobalSection +EndGlobal diff --git a/CPythonTensor/CPythonTensor.vcxproj b/CPythonTensor/CPythonTensor.vcxproj new file mode 100644 index 0000000..7e875dc --- /dev/null +++ b/CPythonTensor/CPythonTensor.vcxproj @@ -0,0 +1,158 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {79abb953-907d-469f-9723-42ed1915a4b0} + CPythonTensor + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + DynamicLibrary + v143 + Unicode + + + DynamicLibrary + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + .pyd + tensor_d + + + .pyd + tensor + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ..\..\TensorCore\TensorCore;C:\Users\Noob\AppData\Local\Programs\Python\Python311\include;%(AdditionalIncludeDirectories) + stdcpp17 + + + NotSet + true + ..\..\TensorCore\$(IntDir);C:\Users\Noob\AppData\Local\Programs\Python\Python311\libs;%(AdditionalLibraryDirectories) + python311.lib;TensorCore.lib;%(AdditionalDependencies) + + + xcopy /y /d "..\..\TensorCore\$(IntDir)TensorCore.dll" "$(OutDir)" + + + + + Level3 + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ..\..\TensorCore\TensorCore;C:\Users\Noob\AppData\Local\Programs\Python\Python311\include;%(AdditionalIncludeDirectories) + stdcpp17 + + + NotSet + true + true + true + ..\..\TensorCore\$(IntDir);C:\Users\Noob\AppData\Local\Programs\Python\Python311\libs;%(AdditionalLibraryDirectories) + python311.lib;TensorCore.lib;%(AdditionalDependencies) + + + xcopy /y /d "..\..\TensorCore\$(IntDir)TensorCore.dll" "$(OutDir)" + + + + + + + + + + + + + \ No newline at end of file diff --git a/CPythonTensor/CPythonTensor.vcxproj.filters b/CPythonTensor/CPythonTensor.vcxproj.filters new file mode 100644 index 0000000..a7950ab --- /dev/null +++ b/CPythonTensor/CPythonTensor.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff --git a/CPythonTensor/module.c b/CPythonTensor/module.c new file mode 100644 index 0000000..1e979e1 --- /dev/null +++ b/CPythonTensor/module.c @@ -0,0 +1,85 @@ +#include "module.h" +#include + +typedef struct +{ + PyObject_HEAD + void* t; +} PyTensorObject; + +static PyObject* Tensor_new(PyTypeObject* type, PyObject* args, PyObject* kwds) +{ + static char* kwlist[] = { "dtype", "shape", "data", NULL}; + PyObject *dtype, *shape; + Py_ssize_t data_ptr; + PyArg_ParseTupleAndKeywords(args, kwds, "|OOn", kwlist, &dtype, &shape, &data_ptr); + unsigned int* ca = calloc(PyTuple_GET_SIZE(shape), sizeof(unsigned int)); + for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(shape); i++) + ca[i] = _PyLong_AsInt(PyTuple_GET_ITEM(shape, i)); + PyTensorObject* self; + self = type->tp_alloc(type, 0); + if (self != NULL) { + self->t = call_tensor(PyTuple_GET_SIZE(shape), ca, data_ptr); + } + free(ca); + return self; +} + +static void Tensor_dealloc(PyTensorObject* self) +{ + delete_tensor(self->t); + Py_TYPE(self)->tp_free(self); +} + +static PyObject* +Tensor_ToString(PyTensorObject* self) +{ + return PyUnicode_FromString(to_string(self->t)); +} + +static PyMethodDef Tensor_methods[] = { + {NULL} /* Sentinel */ +}; + +static PyTypeObject TensorType = +{ + .ob_base = PyVarObject_HEAD_INIT(NULL, 0) + .tp_name = "tensor.Tensor", + .tp_doc = PyDoc_STR("Custom objects"), + .tp_basicsize = sizeof(PyTensorObject), + .tp_itemsize = 0, + .tp_flags = Py_TPFLAGS_DEFAULT, + .tp_new = Tensor_new, + .tp_dealloc = Tensor_dealloc, + .tp_methods = Tensor_methods, + .tp_str = Tensor_ToString, +}; + +static PyModuleDef tensor_module = +{ + PyModuleDef_HEAD_INIT, + "tensor", + NULL, + -1 +}; + +PyMODINIT_FUNC +PyInit_tensor() +{ + PyObject* m; + if (PyType_Ready(&TensorType) < 0) + return NULL; + + m = PyModule_Create(&tensor_module); + if (m == NULL) + return NULL; + + Py_INCREF(&TensorType); + if (PyModule_AddObject(m, "Tensor", &TensorType) < 0) { + Py_DECREF(&TensorType); + Py_DECREF(m); + return NULL; + } + + return m; +} diff --git a/CPythonTensor/module.h b/CPythonTensor/module.h new file mode 100644 index 0000000..08154d7 --- /dev/null +++ b/CPythonTensor/module.h @@ -0,0 +1,12 @@ +#pragma once + +#ifdef __cplusplus +extern "C" +{ +#endif // __cplusplus + void* call_tensor(unsigned int nd, unsigned int* dimensions, const void* data); + void delete_tensor(void* t); + const char* to_string(void* t); +#ifdef __cplusplus +} +#endif // __cplusplus \ No newline at end of file diff --git a/CPythonTensor/py_tensor.cpp b/CPythonTensor/py_tensor.cpp new file mode 100644 index 0000000..85acf81 --- /dev/null +++ b/CPythonTensor/py_tensor.cpp @@ -0,0 +1,23 @@ +#include "module.h" +#include +#include + +using namespace tensor_array::value; + +void* call_tensor(unsigned int nd, unsigned int* dimensions, const void* data) +{ + return new Tensor(TensorBase(typeid(int), std::initializer_list(dimensions, dimensions + nd), data, tensor_array::devices::DEVICE_CPU_0, tensor_array::devices::DEVICE_CPU_0)); +} + +void delete_tensor(void* t) +{ + delete t; +} + +const char* to_string(void* t) +{ + Tensor* t1 = static_cast(t); + std::ostringstream steam; + steam << *t1; + return steam.str().c_str(); +} diff --git a/PythonTensorTesting/PyTensorArray/__init__.py b/PythonTensorTesting/PyTensorArray/__init__.py new file mode 100644 index 0000000..770ec35 --- /dev/null +++ b/PythonTensorTesting/PyTensorArray/__init__.py @@ -0,0 +1,10 @@ +import numpy as np +import tensor as t + +class Tensor: + def __init__(self, arr: np.ndarray): + self.temp_tensor = t.Tensor(dtype = arr.dtype, shape = arr.shape, data = arr.ctypes.data) + + + + diff --git a/PythonTensorTesting/PyTensorArray/tensor.py b/PythonTensorTesting/PyTensorArray/tensor.py new file mode 100644 index 0000000..e69de29 diff --git a/PythonTensorTesting/PythonTensorTesting.pyproj b/PythonTensorTesting/PythonTensorTesting.pyproj new file mode 100644 index 0000000..cbd915e --- /dev/null +++ b/PythonTensorTesting/PythonTensorTesting.pyproj @@ -0,0 +1,60 @@ + + + Debug + 2.0 + 99206fe6-cf46-4140-93b2-3f659d2ca890 + . + main.py + + + . + . + PythonTensorTesting + PythonTensorTesting + False + Standard Python launcher + True + none + test*.py + . + -i + + + CondaEnv|CondaEnv|env + + + true + false + + + true + false + + + + + + + + + CPythonTensor + {79abb953-907d-469f-9723-42ed1915a4b0} + True + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/PythonTensorTesting/main.py b/PythonTensorTesting/main.py new file mode 100644 index 0000000..afc1716 --- /dev/null +++ b/PythonTensorTesting/main.py @@ -0,0 +1,13 @@ +# import os + +# os.add_dll_directory(os.path.join(os.environ['CUDA_PATH'], 'bin')) + +import PyTensorArray as py_t_arr + +import numpy as np + +if __name__ == '__main__': + t3 = np.array([[1, 2, 3], [4, 5, 6]]) + t1 = py_t_arr.Tensor(t3) + print("Hello") + print(t1.temp_tensor) From 0d1c5b243c7681dd87e9a96126a18a3c80f075af Mon Sep 17 00:00:00 2001 From: Noob <63889503+BigNoobWasTaken@users.noreply.github.com> Date: Thu, 28 Dec 2023 11:50:06 +0700 Subject: [PATCH 3/9] test --- CPythonTensor/CPythonTensor.vcxproj | 4 +-- CPythonTensor/module.c | 31 +++++++++++++------ CPythonTensor/py_tensor.cpp | 2 +- PythonTensorTesting/PyTensorArray/__init__.py | 2 +- PythonTensorTesting/main.py | 2 +- 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/CPythonTensor/CPythonTensor.vcxproj b/CPythonTensor/CPythonTensor.vcxproj index 7e875dc..40c18b3 100644 --- a/CPythonTensor/CPythonTensor.vcxproj +++ b/CPythonTensor/CPythonTensor.vcxproj @@ -110,7 +110,7 @@ true _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true - ..\..\TensorCore\TensorCore;C:\Users\Noob\AppData\Local\Programs\Python\Python311\include;%(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) stdcpp17 @@ -130,7 +130,7 @@ true NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true - ..\..\TensorCore\TensorCore;C:\Users\Noob\AppData\Local\Programs\Python\Python311\include;%(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) stdcpp17 diff --git a/CPythonTensor/module.c b/CPythonTensor/module.c index 1e979e1..7611dc1 100644 --- a/CPythonTensor/module.c +++ b/CPythonTensor/module.c @@ -1,5 +1,6 @@ #include "module.h" #include +#include typedef struct { @@ -7,21 +8,30 @@ typedef struct void* t; } PyTensorObject; -static PyObject* Tensor_new(PyTypeObject* type, PyObject* args, PyObject* kwds) +static PyObject* Tensor_new(PyTypeObject* type, PyObject* args) { - static char* kwlist[] = { "dtype", "shape", "data", NULL}; - PyObject *dtype, *shape; - Py_ssize_t data_ptr; - PyArg_ParseTupleAndKeywords(args, kwds, "|OOn", kwlist, &dtype, &shape, &data_ptr); - unsigned int* ca = calloc(PyTuple_GET_SIZE(shape), sizeof(unsigned int)); - for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(shape); i++) - ca[i] = _PyLong_AsInt(PyTuple_GET_ITEM(shape, i)); + PyObject *input_array; + PyArg_ParseTuple(args, "|O", &input_array); + + PyArrayObject* np_array = PyArray_FROM_OTF(input_array, NPY_FLOAT, NPY_ARRAY_IN_ARRAY); + if (np_array == NULL) { + return NULL; // Error handling for NumPy array conversion + } + + int ndims = PyArray_NDIM(np_array); + npy_intp* dims = PyArray_DIMS(np_array); + unsigned int* c_dims = calloc(ndims, sizeof(unsigned int)); + for (size_t i = 0; i < ndims; i++) + { + c_dims[i] = dims[i]; + } + PyTensorObject* self; self = type->tp_alloc(type, 0); if (self != NULL) { - self->t = call_tensor(PyTuple_GET_SIZE(shape), ca, data_ptr); + self->t = call_tensor(ndims, c_dims, PyArray_DATA(np_array)); } - free(ca); + free(c_dims); return self; } @@ -66,6 +76,7 @@ static PyModuleDef tensor_module = PyMODINIT_FUNC PyInit_tensor() { + import_array(); PyObject* m; if (PyType_Ready(&TensorType) < 0) return NULL; diff --git a/CPythonTensor/py_tensor.cpp b/CPythonTensor/py_tensor.cpp index 85acf81..0c7c602 100644 --- a/CPythonTensor/py_tensor.cpp +++ b/CPythonTensor/py_tensor.cpp @@ -6,7 +6,7 @@ using namespace tensor_array::value; void* call_tensor(unsigned int nd, unsigned int* dimensions, const void* data) { - return new Tensor(TensorBase(typeid(int), std::initializer_list(dimensions, dimensions + nd), data, tensor_array::devices::DEVICE_CPU_0, tensor_array::devices::DEVICE_CPU_0)); + return new Tensor(TensorBase(typeid(float), std::initializer_list(dimensions, dimensions + nd), data, tensor_array::devices::DEVICE_CPU_0)); } void delete_tensor(void* t) diff --git a/PythonTensorTesting/PyTensorArray/__init__.py b/PythonTensorTesting/PyTensorArray/__init__.py index 770ec35..caa212d 100644 --- a/PythonTensorTesting/PyTensorArray/__init__.py +++ b/PythonTensorTesting/PyTensorArray/__init__.py @@ -3,7 +3,7 @@ class Tensor: def __init__(self, arr: np.ndarray): - self.temp_tensor = t.Tensor(dtype = arr.dtype, shape = arr.shape, data = arr.ctypes.data) + self.temp_tensor = t.Tensor(arr) diff --git a/PythonTensorTesting/main.py b/PythonTensorTesting/main.py index afc1716..4137c95 100644 --- a/PythonTensorTesting/main.py +++ b/PythonTensorTesting/main.py @@ -8,6 +8,6 @@ if __name__ == '__main__': t3 = np.array([[1, 2, 3], [4, 5, 6]]) - t1 = py_t_arr.Tensor(t3) + t1 = py_t_arr.Tensor([[1, 2.5, 3], [4, 5, 6]]) print("Hello") print(t1.temp_tensor) From 3277e443825f59db33b6ae33d43b3ca1116568ea Mon Sep 17 00:00:00 2001 From: Noob <63889503+BigNoobWasTaken@users.noreply.github.com> Date: Thu, 28 Dec 2023 17:22:53 +0700 Subject: [PATCH 4/9] test --- CPythonTensor/module.c | 17 +++++++++++++---- CPythonTensor/py_tensor.cpp | 5 +++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CPythonTensor/module.c b/CPythonTensor/module.c index 7611dc1..415a18f 100644 --- a/CPythonTensor/module.c +++ b/CPythonTensor/module.c @@ -8,10 +8,10 @@ typedef struct void* t; } PyTensorObject; -static PyObject* Tensor_new(PyTypeObject* type, PyObject* args) +static int Tensor_init(PyTensorObject* self, PyObject* args) { PyObject *input_array; - PyArg_ParseTuple(args, "|O", &input_array); + PyArg_ParseTuple(args, "O", &input_array); PyArrayObject* np_array = PyArray_FROM_OTF(input_array, NPY_FLOAT, NPY_ARRAY_IN_ARRAY); if (np_array == NULL) { @@ -26,8 +26,6 @@ static PyObject* Tensor_new(PyTypeObject* type, PyObject* args) c_dims[i] = dims[i]; } - PyTensorObject* self; - self = type->tp_alloc(type, 0); if (self != NULL) { self->t = call_tensor(ndims, c_dims, PyArray_DATA(np_array)); } @@ -35,6 +33,16 @@ static PyObject* Tensor_new(PyTypeObject* type, PyObject* args) return self; } +static PyObject* Tensor_new(PyTypeObject* type, PyObject* args) +{ + PyTensorObject* self; + self = type->tp_alloc(type, 0); + if (self != NULL) { + self->t = 0; + } + return self; +} + static void Tensor_dealloc(PyTensorObject* self) { delete_tensor(self->t); @@ -59,6 +67,7 @@ static PyTypeObject TensorType = .tp_basicsize = sizeof(PyTensorObject), .tp_itemsize = 0, .tp_flags = Py_TPFLAGS_DEFAULT, + .tp_init = Tensor_init, .tp_new = Tensor_new, .tp_dealloc = Tensor_dealloc, .tp_methods = Tensor_methods, diff --git a/CPythonTensor/py_tensor.cpp b/CPythonTensor/py_tensor.cpp index 0c7c602..45960e3 100644 --- a/CPythonTensor/py_tensor.cpp +++ b/CPythonTensor/py_tensor.cpp @@ -14,6 +14,11 @@ void delete_tensor(void* t) delete t; } +void add_tensor(const void* a, const void* b) +{ + return; +} + const char* to_string(void* t) { Tensor* t1 = static_cast(t); From ca6fb69b010379c190cf469ab3af35c51a5d2a3e Mon Sep 17 00:00:00 2001 From: Noob <63889503+BigNoobWasTaken@users.noreply.github.com> Date: Thu, 28 Dec 2023 17:23:33 +0700 Subject: [PATCH 5/9] test --- CPythonTensor/module.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CPythonTensor/module.c b/CPythonTensor/module.c index 415a18f..025e3d9 100644 --- a/CPythonTensor/module.c +++ b/CPythonTensor/module.c @@ -37,9 +37,8 @@ static PyObject* Tensor_new(PyTypeObject* type, PyObject* args) { PyTensorObject* self; self = type->tp_alloc(type, 0); - if (self != NULL) { + if (self != NULL) self->t = 0; - } return self; } From 993046a8e25e4a4b9e43bdf8dbaf287ae6398283 Mon Sep 17 00:00:00 2001 From: Noob <63889503+BigNoobWasTaken@users.noreply.github.com> Date: Sat, 30 Dec 2023 13:53:18 +0700 Subject: [PATCH 6/9] test --- CPythonTensor/CPythonTensor.vcxproj | 14 ++--- CPythonTensor/CPythonTensor.vcxproj.filters | 10 +--- CPythonTensor/module.c | 16 +++++- CPythonTensor/module.h | 1 + CPythonTensor/py_tensor.cpp | 12 ++-- CPythonTensor/tensor_bind.cc | 56 +++++++++++++++++++ PythonTensorTesting/PyTensorArray/__init__.py | 16 +++++- PythonTensorTesting/main.py | 4 +- 8 files changed, 101 insertions(+), 28 deletions(-) create mode 100644 CPythonTensor/tensor_bind.cc diff --git a/CPythonTensor/CPythonTensor.vcxproj b/CPythonTensor/CPythonTensor.vcxproj index 40c18b3..1f81f92 100644 --- a/CPythonTensor/CPythonTensor.vcxproj +++ b/CPythonTensor/CPythonTensor.vcxproj @@ -18,6 +18,9 @@ x64 + + + 17.0 Win32Proj @@ -110,7 +113,7 @@ true _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true - ..\..\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) + ..\..\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) stdcpp17 @@ -130,7 +133,7 @@ true NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true - ..\..\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) + ..\..\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) stdcpp17 @@ -145,13 +148,6 @@ xcopy /y /d "..\..\TensorCore\$(IntDir)TensorCore.dll" "$(OutDir)" - - - - - - - diff --git a/CPythonTensor/CPythonTensor.vcxproj.filters b/CPythonTensor/CPythonTensor.vcxproj.filters index a7950ab..fed9ccb 100644 --- a/CPythonTensor/CPythonTensor.vcxproj.filters +++ b/CPythonTensor/CPythonTensor.vcxproj.filters @@ -15,16 +15,8 @@ - + Source Files - - Source Files - - - - - Header Files - \ No newline at end of file diff --git a/CPythonTensor/module.c b/CPythonTensor/module.c index 025e3d9..3638dfe 100644 --- a/CPythonTensor/module.c +++ b/CPythonTensor/module.c @@ -30,6 +30,7 @@ static int Tensor_init(PyTensorObject* self, PyObject* args) self->t = call_tensor(ndims, c_dims, PyArray_DATA(np_array)); } free(c_dims); + Py_DECREF(np_array); return self; } @@ -54,7 +55,18 @@ Tensor_ToString(PyTensorObject* self) return PyUnicode_FromString(to_string(self->t)); } -static PyMethodDef Tensor_methods[] = { +static PyTensorObject* Tensor_add(PyTensorObject* self, PyTensorObject* other) +{ + PyTensorObject* val; + if (self != NULL) { + self->t = add_tensor(self->t, other->t); + } + return self; +} + +static PyMethodDef Tensor_methods[] = +{ + //{"__add__", Tensor_add, METH_O, ""}, {NULL} /* Sentinel */ }; @@ -62,7 +74,7 @@ static PyTypeObject TensorType = { .ob_base = PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "tensor.Tensor", - .tp_doc = PyDoc_STR("Custom objects"), + .tp_doc = PyDoc_STR("Tensor"), .tp_basicsize = sizeof(PyTensorObject), .tp_itemsize = 0, .tp_flags = Py_TPFLAGS_DEFAULT, diff --git a/CPythonTensor/module.h b/CPythonTensor/module.h index 08154d7..fd504d0 100644 --- a/CPythonTensor/module.h +++ b/CPythonTensor/module.h @@ -7,6 +7,7 @@ extern "C" void* call_tensor(unsigned int nd, unsigned int* dimensions, const void* data); void delete_tensor(void* t); const char* to_string(void* t); + void* add_tensor(const void* a, const void* b); #ifdef __cplusplus } #endif // __cplusplus \ No newline at end of file diff --git a/CPythonTensor/py_tensor.cpp b/CPythonTensor/py_tensor.cpp index 45960e3..bc68efc 100644 --- a/CPythonTensor/py_tensor.cpp +++ b/CPythonTensor/py_tensor.cpp @@ -14,15 +14,17 @@ void delete_tensor(void* t) delete t; } -void add_tensor(const void* a, const void* b) +void* add_tensor(const void* a, const void* b) { - return; + const Tensor* t_a = static_cast(a); + const Tensor* t_b = static_cast(b); + return new Tensor(add(*t_a, *t_b)); } const char* to_string(void* t) { Tensor* t1 = static_cast(t); - std::ostringstream steam; - steam << *t1; - return steam.str().c_str(); + std::ostringstream stream; + stream << *t1; + return stream.str().c_str(); } diff --git a/CPythonTensor/tensor_bind.cc b/CPythonTensor/tensor_bind.cc new file mode 100644 index 0000000..e4dd846 --- /dev/null +++ b/CPythonTensor/tensor_bind.cc @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +using namespace tensor_array::value; + +template +TensorBase convert_numpy_to_tensor_base(pybind11::array_t py_buf) +{ + pybind11::buffer_info info = py_buf.request(); + std::vector shape_vec(info.ndim); + std::transform + ( + info.shape.cbegin(), + info.shape.cend(), + shape_vec.begin(), + [](pybind11::size_t dim) + { + return static_cast(dim); + } + ); + return TensorBase(typeid(T), shape_vec, info.ptr); +} + +std::string tensor_to_string(const Tensor t) +{ + std::ostringstream osstream; + osstream << t; + return osstream.str(); +} + +PYBIND11_MODULE(tensor, m) +{ + pybind11::class_(m, "TensorC") + .def(pybind11::init()) + .def(pybind11::init(&convert_numpy_to_tensor_base)) + .def(pybind11::self + pybind11::self) + .def(pybind11::self - pybind11::self) + .def(pybind11::self * pybind11::self) + .def(pybind11::self / pybind11::self) + .def(pybind11::self += pybind11::self) + .def(pybind11::self -= pybind11::self) + .def(pybind11::self *= pybind11::self) + .def(pybind11::self /= pybind11::self) + .def(pybind11::self == pybind11::self) + .def(pybind11::self != pybind11::self) + .def(pybind11::self >= pybind11::self) + .def(pybind11::self <= pybind11::self) + .def(pybind11::self > pybind11::self) + .def(pybind11::self < pybind11::self) + .def(+pybind11::self) + .def(-pybind11::self) + .def("__matmul__", &matmul) + .def("__repr__", &tensor_to_string); +} \ No newline at end of file diff --git a/PythonTensorTesting/PyTensorArray/__init__.py b/PythonTensorTesting/PyTensorArray/__init__.py index caa212d..9d0e44b 100644 --- a/PythonTensorTesting/PyTensorArray/__init__.py +++ b/PythonTensorTesting/PyTensorArray/__init__.py @@ -1,10 +1,22 @@ +from typing import Self import numpy as np import tensor as t class Tensor: - def __init__(self, arr: np.ndarray): - self.temp_tensor = t.Tensor(arr) + def __init__(self, arr, *args): + self.temp_tensor = t.TensorC(arr) + + def __add__(self, other) -> Self: + result = Tensor(0); + result.temp_tensor = self.temp_tensor + other.temp_tensor + return result; + def __matmul__(self, other) -> Self: + result = Tensor(0); + result.temp_tensor = self.temp_tensor @ other.temp_tensor + return result; + def __str__(self) -> str: + return self.temp_tensor.__str__() diff --git a/PythonTensorTesting/main.py b/PythonTensorTesting/main.py index 4137c95..cf3773e 100644 --- a/PythonTensorTesting/main.py +++ b/PythonTensorTesting/main.py @@ -9,5 +9,7 @@ if __name__ == '__main__': t3 = np.array([[1, 2, 3], [4, 5, 6]]) t1 = py_t_arr.Tensor([[1, 2.5, 3], [4, 5, 6]]) + t2 = py_t_arr.Tensor([[1, 2.5], [4, 5], [7, 8]]) + t3 = t1 @ t2 print("Hello") - print(t1.temp_tensor) + print(t3) From 67b9664e74c5d4a01a5d853f692857d444e52aef Mon Sep 17 00:00:00 2001 From: Noob <63889503+BigNoobWasTaken@users.noreply.github.com> Date: Sat, 30 Dec 2023 13:54:38 +0700 Subject: [PATCH 7/9] test --- CPythonTensor/module.c | 116 ----------------------------------------- CPythonTensor/module.h | 13 ----- 2 files changed, 129 deletions(-) delete mode 100644 CPythonTensor/module.c delete mode 100644 CPythonTensor/module.h diff --git a/CPythonTensor/module.c b/CPythonTensor/module.c deleted file mode 100644 index 3638dfe..0000000 --- a/CPythonTensor/module.c +++ /dev/null @@ -1,116 +0,0 @@ -#include "module.h" -#include -#include - -typedef struct -{ - PyObject_HEAD - void* t; -} PyTensorObject; - -static int Tensor_init(PyTensorObject* self, PyObject* args) -{ - PyObject *input_array; - PyArg_ParseTuple(args, "O", &input_array); - - PyArrayObject* np_array = PyArray_FROM_OTF(input_array, NPY_FLOAT, NPY_ARRAY_IN_ARRAY); - if (np_array == NULL) { - return NULL; // Error handling for NumPy array conversion - } - - int ndims = PyArray_NDIM(np_array); - npy_intp* dims = PyArray_DIMS(np_array); - unsigned int* c_dims = calloc(ndims, sizeof(unsigned int)); - for (size_t i = 0; i < ndims; i++) - { - c_dims[i] = dims[i]; - } - - if (self != NULL) { - self->t = call_tensor(ndims, c_dims, PyArray_DATA(np_array)); - } - free(c_dims); - Py_DECREF(np_array); - return self; -} - -static PyObject* Tensor_new(PyTypeObject* type, PyObject* args) -{ - PyTensorObject* self; - self = type->tp_alloc(type, 0); - if (self != NULL) - self->t = 0; - return self; -} - -static void Tensor_dealloc(PyTensorObject* self) -{ - delete_tensor(self->t); - Py_TYPE(self)->tp_free(self); -} - -static PyObject* -Tensor_ToString(PyTensorObject* self) -{ - return PyUnicode_FromString(to_string(self->t)); -} - -static PyTensorObject* Tensor_add(PyTensorObject* self, PyTensorObject* other) -{ - PyTensorObject* val; - if (self != NULL) { - self->t = add_tensor(self->t, other->t); - } - return self; -} - -static PyMethodDef Tensor_methods[] = -{ - //{"__add__", Tensor_add, METH_O, ""}, - {NULL} /* Sentinel */ -}; - -static PyTypeObject TensorType = -{ - .ob_base = PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "tensor.Tensor", - .tp_doc = PyDoc_STR("Tensor"), - .tp_basicsize = sizeof(PyTensorObject), - .tp_itemsize = 0, - .tp_flags = Py_TPFLAGS_DEFAULT, - .tp_init = Tensor_init, - .tp_new = Tensor_new, - .tp_dealloc = Tensor_dealloc, - .tp_methods = Tensor_methods, - .tp_str = Tensor_ToString, -}; - -static PyModuleDef tensor_module = -{ - PyModuleDef_HEAD_INIT, - "tensor", - NULL, - -1 -}; - -PyMODINIT_FUNC -PyInit_tensor() -{ - import_array(); - PyObject* m; - if (PyType_Ready(&TensorType) < 0) - return NULL; - - m = PyModule_Create(&tensor_module); - if (m == NULL) - return NULL; - - Py_INCREF(&TensorType); - if (PyModule_AddObject(m, "Tensor", &TensorType) < 0) { - Py_DECREF(&TensorType); - Py_DECREF(m); - return NULL; - } - - return m; -} diff --git a/CPythonTensor/module.h b/CPythonTensor/module.h deleted file mode 100644 index fd504d0..0000000 --- a/CPythonTensor/module.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" -{ -#endif // __cplusplus - void* call_tensor(unsigned int nd, unsigned int* dimensions, const void* data); - void delete_tensor(void* t); - const char* to_string(void* t); - void* add_tensor(const void* a, const void* b); -#ifdef __cplusplus -} -#endif // __cplusplus \ No newline at end of file From 78a22b1c8b152d2e5c41947a93a01e0cdeb55010 Mon Sep 17 00:00:00 2001 From: Noob <63889503+BigNoobWasTaken@users.noreply.github.com> Date: Sat, 30 Dec 2023 14:15:17 +0700 Subject: [PATCH 8/9] test --- PythonTensorTesting/PyTensorArray/__init__.py | 12 ++++++++---- PythonTensorTesting/main.py | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/PythonTensorTesting/PyTensorArray/__init__.py b/PythonTensorTesting/PyTensorArray/__init__.py index 9d0e44b..7708998 100644 --- a/PythonTensorTesting/PyTensorArray/__init__.py +++ b/PythonTensorTesting/PyTensorArray/__init__.py @@ -1,17 +1,21 @@ -from typing import Self import numpy as np import tensor as t class Tensor: - def __init__(self, arr, *args): + def __init__(self, arr): self.temp_tensor = t.TensorC(arr) - def __add__(self, other) -> Self: + def __add__(self, other): result = Tensor(0); result.temp_tensor = self.temp_tensor + other.temp_tensor return result; - def __matmul__(self, other) -> Self: + def __sub__(self, other): + result = Tensor(0); + result.temp_tensor = self.temp_tensor - other.temp_tensor + return result; + + def __matmul__(self, other): result = Tensor(0); result.temp_tensor = self.temp_tensor @ other.temp_tensor return result; diff --git a/PythonTensorTesting/main.py b/PythonTensorTesting/main.py index cf3773e..4b3823f 100644 --- a/PythonTensorTesting/main.py +++ b/PythonTensorTesting/main.py @@ -9,7 +9,7 @@ if __name__ == '__main__': t3 = np.array([[1, 2, 3], [4, 5, 6]]) t1 = py_t_arr.Tensor([[1, 2.5, 3], [4, 5, 6]]) - t2 = py_t_arr.Tensor([[1, 2.5], [4, 5], [7, 8]]) + t2 = py_t_arr.Tensor([[1, 2.5], [4, 5], [7.5, 8]]) t3 = t1 @ t2 print("Hello") print(t3) From 25a307d9deaa4c15e4971a4f77263a355cfa2b4d Mon Sep 17 00:00:00 2001 From: Noob <63889503+BigNoobWasTaken@users.noreply.github.com> Date: Fri, 5 Jan 2024 15:48:06 +0700 Subject: [PATCH 9/9] test --- CPythonTensor/CPythonTensor.vcxproj | 4 +-- CPythonTensor/tensor_bind.cc | 2 +- PythonTensorTesting/PyTensorArray/__init__.py | 27 +------------------ PythonTensorTesting/PyTensorArray/tensor.py | 11 ++++++++ PythonTensorTesting/main.py | 15 +++++------ 5 files changed, 22 insertions(+), 37 deletions(-) diff --git a/CPythonTensor/CPythonTensor.vcxproj b/CPythonTensor/CPythonTensor.vcxproj index 1f81f92..e4b0db5 100644 --- a/CPythonTensor/CPythonTensor.vcxproj +++ b/CPythonTensor/CPythonTensor.vcxproj @@ -73,11 +73,11 @@ .pyd - tensor_d + tensor_bind .pyd - tensor + tensor_bind diff --git a/CPythonTensor/tensor_bind.cc b/CPythonTensor/tensor_bind.cc index e4dd846..2ecc051 100644 --- a/CPythonTensor/tensor_bind.cc +++ b/CPythonTensor/tensor_bind.cc @@ -30,7 +30,7 @@ std::string tensor_to_string(const Tensor t) return osstream.str(); } -PYBIND11_MODULE(tensor, m) +PYBIND11_MODULE(tensor_bind, m) { pybind11::class_(m, "TensorC") .def(pybind11::init()) diff --git a/PythonTensorTesting/PyTensorArray/__init__.py b/PythonTensorTesting/PyTensorArray/__init__.py index 7708998..0e34f41 100644 --- a/PythonTensorTesting/PyTensorArray/__init__.py +++ b/PythonTensorTesting/PyTensorArray/__init__.py @@ -1,26 +1 @@ -import numpy as np -import tensor as t - -class Tensor: - def __init__(self, arr): - self.temp_tensor = t.TensorC(arr) - - def __add__(self, other): - result = Tensor(0); - result.temp_tensor = self.temp_tensor + other.temp_tensor - return result; - - def __sub__(self, other): - result = Tensor(0); - result.temp_tensor = self.temp_tensor - other.temp_tensor - return result; - - def __matmul__(self, other): - result = Tensor(0); - result.temp_tensor = self.temp_tensor @ other.temp_tensor - return result; - - def __str__(self) -> str: - return self.temp_tensor.__str__() - - +from .tensor import * \ No newline at end of file diff --git a/PythonTensorTesting/PyTensorArray/tensor.py b/PythonTensorTesting/PyTensorArray/tensor.py index e69de29..285a6a2 100644 --- a/PythonTensorTesting/PyTensorArray/tensor.py +++ b/PythonTensorTesting/PyTensorArray/tensor.py @@ -0,0 +1,11 @@ +import numpy as np +import tensor_bind as t + +def tensor_decorator(cls): + return t.TensorC + +@tensor_decorator +class Tensor: + def __init__(self): + pass + diff --git a/PythonTensorTesting/main.py b/PythonTensorTesting/main.py index 4b3823f..d57f07c 100644 --- a/PythonTensorTesting/main.py +++ b/PythonTensorTesting/main.py @@ -1,15 +1,14 @@ -# import os - -# os.add_dll_directory(os.path.join(os.environ['CUDA_PATH'], 'bin')) - import PyTensorArray as py_t_arr import numpy as np if __name__ == '__main__': - t3 = np.array([[1, 2, 3], [4, 5, 6]]) - t1 = py_t_arr.Tensor([[1, 2.5, 3], [4, 5, 6]]) - t2 = py_t_arr.Tensor([[1, 2.5], [4, 5], [7.5, 8]]) - t3 = t1 @ t2 + t4 = np.array([[1, 2, 3], [4, 5, 6]]) + t1 = py_t_arr.tensor.Tensor(t4) + t2 = py_t_arr.tensor.Tensor(t4) + t3 = t1 + t2 print("Hello") + print(t1) + print(t2) print(t3) + print(t1 == t2)