lktorch 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lktorch-0.1.0/CMAKESettings.json +26 -0
- lktorch-0.1.0/CMakeLists.txt +71 -0
- lktorch-0.1.0/LICENSE.md +19 -0
- lktorch-0.1.0/MANIFEST.in +1 -0
- lktorch-0.1.0/PKG-INFO +11 -0
- lktorch-0.1.0/README.md +86 -0
- lktorch-0.1.0/auto_compile.bat +28 -0
- lktorch-0.1.0/lib/lktorch.pyd +0 -0
- lktorch-0.1.0/lib/lktorch.pyi +605 -0
- lktorch-0.1.0/lktorch.egg-info/PKG-INFO +11 -0
- lktorch-0.1.0/lktorch.egg-info/SOURCES.txt +29 -0
- lktorch-0.1.0/lktorch.egg-info/dependency_links.txt +1 -0
- lktorch-0.1.0/lktorch.egg-info/requires.txt +1 -0
- lktorch-0.1.0/lktorch.egg-info/top_level.txt +1 -0
- lktorch-0.1.0/pyproject.toml +3 -0
- lktorch-0.1.0/setup.cfg +4 -0
- lktorch-0.1.0/setup.py +52 -0
- lktorch-0.1.0/src/DebugAssist/DebugAssist.cpp +9 -0
- lktorch-0.1.0/src/Helper/GeneralMath.cpp +40 -0
- lktorch-0.1.0/src/Helper/StaticVector.cpp +209 -0
- lktorch-0.1.0/src/Tensor/BaseTensor.cpp +43 -0
- lktorch-0.1.0/src/Tensor/DebugTensor.cpp +55 -0
- lktorch-0.1.0/src/Tensor/RawTensor.cpp +555 -0
- lktorch-0.1.0/src/Tensor/SingletonTensor.cpp +20 -0
- lktorch-0.1.0/src/Tensor/TensorFunction.cpp +18 -0
- lktorch-0.1.0/src/Tensor/TensorInit.cpp +43 -0
- lktorch-0.1.0/src/Tensor/TensorMathOp.cpp +289 -0
- lktorch-0.1.0/src/Tensor/TensorOperation.cpp +270 -0
- lktorch-0.1.0/src/TensorManip/Loss.cpp +56 -0
- lktorch-0.1.0/src/TensorManip/Optimizer.cpp +159 -0
- lktorch-0.1.0/src/bindings.cpp +374 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"configurations": [
|
|
3
|
+
{
|
|
4
|
+
"name": "x64-Release",
|
|
5
|
+
"generator": "Ninja",
|
|
6
|
+
"configurationType": "Release",
|
|
7
|
+
"inheritEnvironments": [ "msvc_x64_x64" ],
|
|
8
|
+
"buildRoot": "${projectDir}\\build\\${name}",
|
|
9
|
+
"installRoot": "${projectDir}\\out\\install\\${name}",
|
|
10
|
+
"cmakeCommandArgs": "",
|
|
11
|
+
"buildCommandArgs": "",
|
|
12
|
+
"ctestCommandArgs": ""
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"name": "x64-Debug",
|
|
16
|
+
"generator": "Ninja",
|
|
17
|
+
"configurationType": "Debug",
|
|
18
|
+
"buildRoot": "${projectDir}\\build\\${name}",
|
|
19
|
+
"installRoot": "${projectDir}\\out\\install\\${name}",
|
|
20
|
+
"cmakeCommandArgs": "",
|
|
21
|
+
"buildCommandArgs": "",
|
|
22
|
+
"ctestCommandArgs": "",
|
|
23
|
+
"inheritEnvironments": [ "msvc_x64_x64" ]
|
|
24
|
+
}
|
|
25
|
+
]
|
|
26
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.16)
|
|
2
|
+
project(lktorch LANGUAGES CXX)
|
|
3
|
+
|
|
4
|
+
set(CMAKE_CXX_STANDARD 17)
|
|
5
|
+
|
|
6
|
+
option (BUILD_PYTHON "Do you wanna produce Python binding (ON) or debugging in C++ (OFF)?" OFF)
|
|
7
|
+
|
|
8
|
+
if (BUILD_PYTHON)
|
|
9
|
+
include(FetchContent)
|
|
10
|
+
FetchContent_Declare(
|
|
11
|
+
pybind11
|
|
12
|
+
GIT_REPOSITORY https://github.com/pybind/pybind11.git
|
|
13
|
+
GIT_TAG v3.0.2 # The stable version tag
|
|
14
|
+
)
|
|
15
|
+
FetchContent_MakeAvailable(pybind11)
|
|
16
|
+
endif()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# --- FORCE OUTPUT TO "build" FOLDER ---
|
|
21
|
+
|
|
22
|
+
# 1. Define the output path
|
|
23
|
+
set(OUTPUT_DIR "${CMAKE_SOURCE_DIR}/build")
|
|
24
|
+
|
|
25
|
+
# 2. Force ALL configurations (Debug, Release) to go to the same place
|
|
26
|
+
# If you don't do this, VS creates "bin/Debug/Visualizer.exe"
|
|
27
|
+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIR})
|
|
28
|
+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${OUTPUT_DIR})
|
|
29
|
+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${OUTPUT_DIR})
|
|
30
|
+
|
|
31
|
+
# 3. (Optional) Also put DLLs (.lib files) there if needed
|
|
32
|
+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_DIR})
|
|
33
|
+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${OUTPUT_DIR})
|
|
34
|
+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${OUTPUT_DIR})
|
|
35
|
+
|
|
36
|
+
#Main execcutable file
|
|
37
|
+
if (BUILD_PYTHON)
|
|
38
|
+
pybind11_add_module(lktorch "src/bindings.cpp")
|
|
39
|
+
else()
|
|
40
|
+
add_executable(lktorch "src/main.cpp")
|
|
41
|
+
endif()
|
|
42
|
+
|
|
43
|
+
target_include_directories(lktorch PRIVATE
|
|
44
|
+
${CMAKE_SOURCE_DIR}/include
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
target_sources(lktorch PRIVATE
|
|
48
|
+
"src/DebugAssist/DebugAssist.cpp"
|
|
49
|
+
"src/Helper/StaticVector.cpp" "src/Helper/GeneralMath.cpp"
|
|
50
|
+
|
|
51
|
+
"src/Tensor/BaseTensor.cpp" "src/Tensor/RawTensor.cpp" "src/Tensor/DebugTensor.cpp"
|
|
52
|
+
"src/Tensor/TensorFunction.cpp" "src/Tensor/TensorOperation.cpp" "src/Tensor/TensorInit.cpp"
|
|
53
|
+
|
|
54
|
+
"src/TensorManip/Loss.cpp" "src/TensorManip/Optimizer.cpp"
|
|
55
|
+
"src/Tensor/SingletonTensor.cpp" "src/Tensor/TensorMathOp.cpp")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
if(MSVC)
|
|
59
|
+
target_compile_options(lktorch PRIVATE /O2 /fp:fast /arch:AVX2)
|
|
60
|
+
else()
|
|
61
|
+
# For MinGW / GCC / Clang
|
|
62
|
+
target_compile_options(lktorch PRIVATE -O3 -march=native -ffast-math)
|
|
63
|
+
endif()
|
|
64
|
+
|
|
65
|
+
if(MINGW)
|
|
66
|
+
target_link_options(lktorch PRIVATE -static -static-libgcc -static-libstdc++)
|
|
67
|
+
endif()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
#Inputing the folder for accessing assets
|
|
71
|
+
target_compile_definitions(lktorch PRIVATE PROJECT_DIR="${CMAKE_SOURCE_DIR}/")
|
lktorch-0.1.0/LICENSE.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2026 Lê Kiến Thành
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
recursive-include lib *.pyd *.pyi
|
lktorch-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lktorch
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A PyTorch-like framework built from scratch
|
|
5
|
+
Requires-Python: >=3.7
|
|
6
|
+
License-File: LICENSE.md
|
|
7
|
+
Requires-Dist: numpy
|
|
8
|
+
Dynamic: license-file
|
|
9
|
+
Dynamic: requires-dist
|
|
10
|
+
Dynamic: requires-python
|
|
11
|
+
Dynamic: summary
|
lktorch-0.1.0/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
LKTorch is a Python package that provides two high-level features:
|
|
4
|
+
|
|
5
|
+
- Tensor computation (like NumPy) with no GPU acceleration!!
|
|
6
|
+
- Deep neural networks built on a tape-based autograd system
|
|
7
|
+
|
|
8
|
+
Essentially, this is a hobby project of a college student trying to clone Pytorch!
|
|
9
|
+
|
|
10
|
+
This library is compatible with numpy (converting from numpy array to lk.Tensor, and vice versa)
|
|
11
|
+
|
|
12
|
+
## More about LKTorch
|
|
13
|
+
|
|
14
|
+
LKTorch is a library that consist of the following elements:
|
|
15
|
+
|
|
16
|
+
|Components|Description|
|
|
17
|
+
|---|---|
|
|
18
|
+
|lk.Tensor|The base tensor class|
|
|
19
|
+
|lk.TensorFunction|Differentiable function that will be apply on each row of the tensor|
|
|
20
|
+
|lk.TensorMathOp|Standard , differentiable math functions for machine learning, such as reLU, log, ...|
|
|
21
|
+
|lk.TensorOperations|Other functions for tensor manipulation, like Slice, Unfold, ...|
|
|
22
|
+
|lk.Module|Compacting a complex sequence of tensor manipulation into a class|
|
|
23
|
+
|lk.Layer|Class that manipulate tensors, such as LinearLayer, Conv2D, Dropout, ...., inherited from lk.Module|
|
|
24
|
+
|lk.Optimizer|A collection of popular optimizer, such as GradientDescent, Adam, ...|
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### A tensor library
|
|
28
|
+
|
|
29
|
+
You may ask: what is a tensor, is it edible, and why is everyone talking about it in Machine Learning?
|
|
30
|
+
|
|
31
|
+
When we started programming, we learn how to manipulate variable around, from simple operations like addition, multiplication, to the more complex ones, such as GCD or exponentiation.
|
|
32
|
+
|
|
33
|
+
However, there is not much fun thing that can be solved with singular variables. So there comes the concept of an array. An array is a list of variables, therefore it is exponentially more interesting.
|
|
34
|
+
|
|
35
|
+
But array is still not enough to solve problems in programming. So we invented array of arrays, array of array of arrays of arrays. Informally, these would be called 2D and 3D array, respectively.
|
|
36
|
+
|
|
37
|
+
A tensor is just a genelizations of them. It is a multi-dimentional array. A rank-1 tensor is an array, rank-2 is a matrix, rank-0 is a variable (scalar), etc.
|
|
38
|
+
|
|
39
|
+
LKTorch offers way to manipulate tensors around. Obviously, it is much weaker than Pytorch and Numpy in this regard, because it was made by a bored college student. But I do offer a way to convert to numpy array.
|
|
40
|
+
|
|
41
|
+
### Dynamic Neural Network
|
|
42
|
+
|
|
43
|
+
*Prerequisite knowledge 1: A neural network is a structure that takes in a tensor, and spit out a tensor.*
|
|
44
|
+
|
|
45
|
+
*Prerequisite knowledge 2: Most of the time, one or more tensor will be involved in the structure of the neural network to perform linear transform like tensor addition, multiplication. However, in its untrained state, neural network usually spit out bullcrap, which means we have to use loss function to punish it.*
|
|
46
|
+
|
|
47
|
+
*Prerequisite knowledge 3: You can make the neural network learn your dataset, by calculating the gradient of the entire network with respect to the loss function. Then, you gradient descent, which means you modify the tensor value with the goal of making the loss smaller.*
|
|
48
|
+
|
|
49
|
+
Other libraries, like TensorFlow, force you to build the neural network before hand, and only then, could you start to use that to manipulate data and train it. This static behavior sometimes restrict you.
|
|
50
|
+
|
|
51
|
+
With some pointer blackmagic, LKTorch allows you to build the neural network on the fly, while still allowing you to calculate the gradient of every tensor involved in the network. In essence, you can just treat `Tensor` as another variable, where you can perform addition, multiplication, log, etc., and at the end of the day, call `.backward()`, and it will calculate the gradient of the entire ordeal for you.
|
|
52
|
+
|
|
53
|
+
This means you can literally put if statement inside your neural network training. Here is an example.
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
x = model1(x)
|
|
57
|
+
# Model 1 has a probability to spit out crap, so if it indeed spit out crap, just put the same thing into model 2
|
|
58
|
+
if (x.numpy().max() < 10):
|
|
59
|
+
x = model2(x)
|
|
60
|
+
|
|
61
|
+
# Now we can calculate the gradient of that whole thing
|
|
62
|
+
x.backward()
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Is LKTorch the only library that does this? Yes it is! This is a totally original library! There is no other library with a similar name, let alone a similar functionality.
|
|
66
|
+
|
|
67
|
+
### My Sanity First
|
|
68
|
+
|
|
69
|
+
I will ~~parody~~ copy [Pytorch README.md](https://github.com/pytorch/pytorch?tab=readme-ov-file#more-about-pytorch) on this one
|
|
70
|
+
|
|
71
|
+
LKTorch is a Python binding into a monolithic C++ framework. I tried to build it to be deeply integrated into Python, and that turned out half correct. You can kinda use it naturally like you would use NumPy / SciPy / scikit-learn etc, and you can even convert the object into numpy array! You can write your new neural network layers in Python itself. Our goal is to reinvent the wheel.
|
|
72
|
+
|
|
73
|
+
### Imperative Experiences
|
|
74
|
+
|
|
75
|
+
This is designed to be easy to use (I hope so).
|
|
76
|
+
|
|
77
|
+
### Framework Used
|
|
78
|
+
|
|
79
|
+
What?
|
|
80
|
+
|
|
81
|
+
<details>
|
|
82
|
+
<summary>Serious</summary>
|
|
83
|
+
|
|
84
|
+
In all seriousness, this was more of a learning project than an actual production code, so I didn't use any helping library aside from **pybind11** to bind my code into python.
|
|
85
|
+
</details>
|
|
86
|
+
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
@echo off
|
|
2
|
+
|
|
3
|
+
echo Step 0: Cleaning up my shit
|
|
4
|
+
del /f /q "lib\*.pyd"
|
|
5
|
+
del /f /q "lib\*.pyi"
|
|
6
|
+
|
|
7
|
+
echo Step 1: CREATING the folder
|
|
8
|
+
mkdir build
|
|
9
|
+
echo Step 2: opening the folder
|
|
10
|
+
cd build
|
|
11
|
+
echo Step 3: Cmake gaming
|
|
12
|
+
cmake -DBUILD_PYTHON=ON ..
|
|
13
|
+
echo Step 4: Cmake gaming
|
|
14
|
+
cmake --build . --config Release
|
|
15
|
+
echo Step 5: Copy the file
|
|
16
|
+
copy "*.pyd" "..\lib"
|
|
17
|
+
echo Step 6: Creating python auto-suggestion
|
|
18
|
+
cd ..\
|
|
19
|
+
cd lib
|
|
20
|
+
set PYTHONPATH=. && pybind11-stubgen lktorch
|
|
21
|
+
copy "stubs\*.pyi" "."
|
|
22
|
+
|
|
23
|
+
rd /s /q "stubs"
|
|
24
|
+
|
|
25
|
+
ren "lktorch.cp310-win_amd64.pyd" "lktorch.pyd"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
pause
|
|
Binary file
|