krnl 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.
- krnl-0.1.0/LICENSE +21 -0
- krnl-0.1.0/PKG-INFO +314 -0
- krnl-0.1.0/README.md +281 -0
- krnl-0.1.0/pyproject.toml +46 -0
- krnl-0.1.0/setup.cfg +4 -0
- krnl-0.1.0/src/krnl/backends/GPU_processer.py +147 -0
- krnl-0.1.0/src/krnl/backends/device.py +40 -0
- krnl-0.1.0/src/krnl/backends/kernels.py +37 -0
- krnl-0.1.0/src/krnl/backends/tensor.py +608 -0
- krnl-0.1.0/src/krnl/nn/__init__.py +20 -0
- krnl-0.1.0/src/krnl/nn/loss.py +47 -0
- krnl-0.1.0/src/krnl/nn/model_utils.py +93 -0
- krnl-0.1.0/src/krnl/nn/optim.py +568 -0
- krnl-0.1.0/src/krnl.egg-info/PKG-INFO +314 -0
- krnl-0.1.0/src/krnl.egg-info/SOURCES.txt +21 -0
- krnl-0.1.0/src/krnl.egg-info/dependency_links.txt +1 -0
- krnl-0.1.0/src/krnl.egg-info/requires.txt +23 -0
- krnl-0.1.0/src/krnl.egg-info/top_level.txt +1 -0
- krnl-0.1.0/tests/test_muon.py +104 -0
- krnl-0.1.0/tests/test_nn.py +203 -0
- krnl-0.1.0/tests/test_opencl_backend.py +12 -0
- krnl-0.1.0/tests/test_simple_model_implementation.py +29 -0
- krnl-0.1.0/tests/test_tensor.py +22 -0
krnl-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Adhavan Yuvaraj
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
krnl-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: krnl
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A lightweight educational tensor and automatic differentiation library with CPU and OpenCL GPU support
|
|
5
|
+
Author-email: Adhavan <helloadhavan@gmaim.com>
|
|
6
|
+
Requires-Python: >=3.11
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: attrs==26.1.0
|
|
10
|
+
Requires-Dist: Automat==25.4.16
|
|
11
|
+
Requires-Dist: constantly==23.10.4
|
|
12
|
+
Requires-Dist: construct==2.10.70
|
|
13
|
+
Requires-Dist: hyperlink==21.0.0
|
|
14
|
+
Requires-Dist: idna==3.20
|
|
15
|
+
Requires-Dist: Incremental==24.11.0
|
|
16
|
+
Requires-Dist: Mako==1.4.3
|
|
17
|
+
Requires-Dist: MarkupSafe==3.0.4
|
|
18
|
+
Requires-Dist: numpy==2.5.4
|
|
19
|
+
Requires-Dist: packaging==26.3
|
|
20
|
+
Requires-Dist: platformdirs==4.13.5
|
|
21
|
+
Requires-Dist: protobuf==7.36.2
|
|
22
|
+
Requires-Dist: psutil==7.2.2
|
|
23
|
+
Requires-Dist: pyasn1==0.6.4
|
|
24
|
+
Requires-Dist: pyopencl==2026.1.4
|
|
25
|
+
Requires-Dist: pysnmp==7.1.30
|
|
26
|
+
Requires-Dist: pytools==2026.1.1
|
|
27
|
+
Requires-Dist: PyYAML==6.0.3
|
|
28
|
+
Requires-Dist: siphash24==1.9
|
|
29
|
+
Requires-Dist: Twisted==26.4.0
|
|
30
|
+
Requires-Dist: typing_extensions==4.16.0
|
|
31
|
+
Requires-Dist: zope.interface==8.7
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# krnl
|
|
35
|
+
|
|
36
|
+
`krnl` is a lightweight, educational machine learning library built in Python on top of NumPy, with experimental OpenCL support for device-aware tensor computation. The project is designed to help understand the core ideas behind modern deep learning frameworks: tensors, autograd, computational graphs, layers, losses, and optimization.
|
|
37
|
+
|
|
38
|
+
This library aims to stay small and readable rather than production-focused. It is intentionally useful for learning how neural networks and training loops work under the hood.
|
|
39
|
+
|
|
40
|
+
## Highlights
|
|
41
|
+
|
|
42
|
+
- Tensor-based autograd engine
|
|
43
|
+
- NumPy-backed CPU execution
|
|
44
|
+
- OpenCL backend experiments for GPU-style computation
|
|
45
|
+
- Basic neural network building blocks
|
|
46
|
+
- Optimizer implementations for training
|
|
47
|
+
- Common loss functions for regression and learning tasks
|
|
48
|
+
- Simple, traceable code for educational use
|
|
49
|
+
|
|
50
|
+
## Installation
|
|
51
|
+
|
|
52
|
+
Clone the repository:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
git clone https://github.com/helloadhavan/krnl.git
|
|
56
|
+
cd krnl
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Create a virtual environment:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
python -m venv .venv
|
|
63
|
+
source .venv/bin/activate
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Install dependencies:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
pip install -r requirements.txt
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Optional OpenCL setup
|
|
73
|
+
|
|
74
|
+
To use the OpenCL backend, make sure your system has a compatible OpenCL runtime and drivers installed.
|
|
75
|
+
|
|
76
|
+
## Quick start
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from backends.backends.tensor import Tensor
|
|
80
|
+
|
|
81
|
+
x = Tensor(2.0)
|
|
82
|
+
w = Tensor(3.0, requires_grad=True)
|
|
83
|
+
b = Tensor(4.0, requires_grad=True)
|
|
84
|
+
|
|
85
|
+
y = x * w + b
|
|
86
|
+
loss = (y - 10.0) ** 2
|
|
87
|
+
|
|
88
|
+
loss.backward()
|
|
89
|
+
|
|
90
|
+
print(y.data)
|
|
91
|
+
print(w.grad)
|
|
92
|
+
print(b.grad)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
This example creates a small computational graph, computes a scalar loss, and backpropagates gradients through the graph.
|
|
96
|
+
|
|
97
|
+
## Core concepts
|
|
98
|
+
|
|
99
|
+
### Tensor
|
|
100
|
+
|
|
101
|
+
The `Tensor` class is the foundation of the project. It stores numeric data, tracks computation history, and supports differentiation through custom arithmetic and autograd operations.
|
|
102
|
+
|
|
103
|
+
Key features include:
|
|
104
|
+
|
|
105
|
+
- scalar and array arithmetic
|
|
106
|
+
- forward pass tracking
|
|
107
|
+
- backward gradient propagation
|
|
108
|
+
- device awareness for CPU/GPU execution
|
|
109
|
+
- support for differentiable operations used in model training
|
|
110
|
+
|
|
111
|
+
### Device abstraction
|
|
112
|
+
|
|
113
|
+
The `Device` abstraction allows the library to reason about whether computation should happen on CPU or a GPU-like backend. This keeps the design simple while making it easier to experiment with accelerator-backed operations.
|
|
114
|
+
|
|
115
|
+
### OpenCL backend
|
|
116
|
+
|
|
117
|
+
The OpenCL path uses PyOpenCL to move arrays to an accelerator, execute simple kernels, and support basic tensor operations. It is useful for experimentation and understanding how low-level device execution fits into a training loop.
|
|
118
|
+
|
|
119
|
+
## Neural network utilities
|
|
120
|
+
|
|
121
|
+
The library includes a minimal `nn` module with building blocks for simple models.
|
|
122
|
+
|
|
123
|
+
### Module and Parameter
|
|
124
|
+
|
|
125
|
+
`Module` is the base class for neural network components. It provides a simple interface for registering trainable parameters.
|
|
126
|
+
|
|
127
|
+
`Parameter` wraps a tensor and marks it as trainable so that it participates in gradient-based optimization.
|
|
128
|
+
|
|
129
|
+
### Layers
|
|
130
|
+
|
|
131
|
+
The library includes a few basic layer types:
|
|
132
|
+
|
|
133
|
+
- `Linear`: a dense layer implementing `y = xW^T + b`
|
|
134
|
+
- `ReLU`: elementwise rectified linear activation
|
|
135
|
+
- `Sigmoid`: elementwise sigmoid activation
|
|
136
|
+
|
|
137
|
+
Example:
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
from nn.model_utils import Linear, ReLU
|
|
141
|
+
from backends.backends.tensor import Tensor
|
|
142
|
+
|
|
143
|
+
layer1 = Linear(in_features=4, out_features=8)
|
|
144
|
+
activation = ReLU()
|
|
145
|
+
|
|
146
|
+
x = Tensor([[1.0, 2.0, 3.0, 4.0]])
|
|
147
|
+
y = activation(layer1(x))
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Loss functions
|
|
151
|
+
|
|
152
|
+
The project includes a small set of regression-style loss implementations in `nn`.
|
|
153
|
+
|
|
154
|
+
### MSELoss
|
|
155
|
+
|
|
156
|
+
Mean Squared Error:
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
(predicted - target) ** 2
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
This computes the squared error across the prediction and is commonly used for regression tasks.
|
|
163
|
+
|
|
164
|
+
### MAELoss
|
|
165
|
+
|
|
166
|
+
Mean Absolute Error:
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
abs(predicted - target)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
This is useful when you want the average magnitude of the prediction error without squaring it.
|
|
173
|
+
|
|
174
|
+
### HuberLoss
|
|
175
|
+
|
|
176
|
+
A smooth piecewise loss that behaves like squared error for small residuals and absolute error for larger residuals. It is often used to reduce sensitivity to outliers.
|
|
177
|
+
|
|
178
|
+
Example:
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
from nn import MSELoss, MAELoss, HuberLoss
|
|
182
|
+
|
|
183
|
+
criterion = MSELoss()
|
|
184
|
+
loss = criterion(predictions, targets)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Optimizers
|
|
188
|
+
|
|
189
|
+
The library includes several optimization strategies in `nn`.
|
|
190
|
+
|
|
191
|
+
### SGD
|
|
192
|
+
|
|
193
|
+
Stochastic Gradient Descent updates parameters by moving them in the direction opposite the gradient:
|
|
194
|
+
|
|
195
|
+
```python
|
|
196
|
+
parameter = parameter - learning_rate * gradient
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
This is the simplest gradient-based optimizer and is useful for understanding the basics of training.
|
|
200
|
+
|
|
201
|
+
### Adam
|
|
202
|
+
|
|
203
|
+
Adam combines momentum and adaptive learning rates using first and second moment estimates. It is one of the most common modern optimizers and is useful when training neural networks more stably than plain SGD.
|
|
204
|
+
|
|
205
|
+
### Muon
|
|
206
|
+
|
|
207
|
+
Muon is a momentum-based optimizer with an orthogonalized update direction. It introduces a more structure-aware update strategy and is included as an experimental optimizer in the library.
|
|
208
|
+
|
|
209
|
+
### MuonAdam
|
|
210
|
+
|
|
211
|
+
This optimizer combines Muon-style orthogonal updates with Adam-like moment estimates, making it a hybrid approach for experimentation and learning.
|
|
212
|
+
|
|
213
|
+
Example:
|
|
214
|
+
|
|
215
|
+
```python
|
|
216
|
+
from nn.optim import SGD, Adam, Muon, MuonAdam
|
|
217
|
+
|
|
218
|
+
sgd = SGD(parameters=model_parameters, learning_rate=0.01)
|
|
219
|
+
adam = Adam(parameters=model_parameters, learning_rate=0.001)
|
|
220
|
+
muon = Muon(parameters=model_parameters, learning_rate=0.02)
|
|
221
|
+
muon_adam = MuonAdam(parameters=model_parameters, learning_rate=0.02)
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## Training workflow
|
|
225
|
+
|
|
226
|
+
A typical training loop looks like this:
|
|
227
|
+
|
|
228
|
+
```python
|
|
229
|
+
from nn import MSELoss
|
|
230
|
+
from nn.optim import SGD
|
|
231
|
+
|
|
232
|
+
model = ...
|
|
233
|
+
criterion = MSELoss()
|
|
234
|
+
optimizer = SGD(model._parameters.values(), learning_rate=0.01)
|
|
235
|
+
|
|
236
|
+
for batch_x, batch_y in data:
|
|
237
|
+
predictions = model(batch_x)
|
|
238
|
+
loss = criterion(predictions, batch_y)
|
|
239
|
+
|
|
240
|
+
loss.backward()
|
|
241
|
+
optimizer.step()
|
|
242
|
+
optimizer.zero_grad()
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
## Repository structure
|
|
246
|
+
|
|
247
|
+
```text
|
|
248
|
+
krnl/
|
|
249
|
+
├── README.md
|
|
250
|
+
├── __init__.py
|
|
251
|
+
├── requirements.txt
|
|
252
|
+
├── version.txt
|
|
253
|
+
├── src/
|
|
254
|
+
│ ├── backends/
|
|
255
|
+
│ │ ├── GPU_processer.py
|
|
256
|
+
│ │ ├── __init__.py
|
|
257
|
+
│ │ ├── device.py
|
|
258
|
+
│ │ ├── kernels.py
|
|
259
|
+
│ │ └── tensor.py
|
|
260
|
+
│ ├── nn/
|
|
261
|
+
│ │ ├── __init__.py
|
|
262
|
+
│ │ ├── loss.py
|
|
263
|
+
│ │ ├── model_utils.py
|
|
264
|
+
│ │ └── optim.py
|
|
265
|
+
│ └── tests/
|
|
266
|
+
│ ├── test_opencl_backend.py
|
|
267
|
+
│ ├── test_simple_model_implementation.py
|
|
268
|
+
│ └── test_tensor.py
|
|
269
|
+
└── .gitignore
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
## Running tests
|
|
273
|
+
|
|
274
|
+
From the project root:
|
|
275
|
+
|
|
276
|
+
```bash
|
|
277
|
+
python src/tests/test_tensor.py
|
|
278
|
+
python src/tests/test_opencl_backend.py
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
The OpenCL tests require a valid OpenCL installation and compatible device support.
|
|
282
|
+
|
|
283
|
+
## Project status
|
|
284
|
+
|
|
285
|
+
`krnl` is an educational framework for understanding the internals of deep learning systems. It is not intended to replace established libraries such as PyTorch or TensorFlow.
|
|
286
|
+
|
|
287
|
+
Current focus areas include:
|
|
288
|
+
|
|
289
|
+
- tensor arithmetic and autograd
|
|
290
|
+
- gradient-based optimization
|
|
291
|
+
- CPU execution with NumPy
|
|
292
|
+
- GPU/OpenCL experimentation
|
|
293
|
+
- small neural network primitives
|
|
294
|
+
|
|
295
|
+
## Contributing
|
|
296
|
+
|
|
297
|
+
Contributions are welcome in areas such as:
|
|
298
|
+
|
|
299
|
+
- more layer types
|
|
300
|
+
- additional loss functions
|
|
301
|
+
- more optimization algorithms
|
|
302
|
+
- clearer documentation
|
|
303
|
+
- stronger tests
|
|
304
|
+
- device and performance improvements
|
|
305
|
+
|
|
306
|
+
The project remains intentionally simple and readable so it can serve as a learning tool.
|
|
307
|
+
|
|
308
|
+
## License
|
|
309
|
+
|
|
310
|
+
This repository does not currently include a license file. If you plan to distribute or publish it publicly, consider adding a license such as MIT or Apache 2.0.
|
|
311
|
+
|
|
312
|
+
## Acknowledgements
|
|
313
|
+
|
|
314
|
+
This project is built for experimentation and education in tensor math, computational graphs, optimization, and device-aware execution.
|
krnl-0.1.0/README.md
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
# krnl
|
|
2
|
+
|
|
3
|
+
`krnl` is a lightweight, educational machine learning library built in Python on top of NumPy, with experimental OpenCL support for device-aware tensor computation. The project is designed to help understand the core ideas behind modern deep learning frameworks: tensors, autograd, computational graphs, layers, losses, and optimization.
|
|
4
|
+
|
|
5
|
+
This library aims to stay small and readable rather than production-focused. It is intentionally useful for learning how neural networks and training loops work under the hood.
|
|
6
|
+
|
|
7
|
+
## Highlights
|
|
8
|
+
|
|
9
|
+
- Tensor-based autograd engine
|
|
10
|
+
- NumPy-backed CPU execution
|
|
11
|
+
- OpenCL backend experiments for GPU-style computation
|
|
12
|
+
- Basic neural network building blocks
|
|
13
|
+
- Optimizer implementations for training
|
|
14
|
+
- Common loss functions for regression and learning tasks
|
|
15
|
+
- Simple, traceable code for educational use
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
Clone the repository:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
git clone https://github.com/helloadhavan/krnl.git
|
|
23
|
+
cd krnl
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Create a virtual environment:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
python -m venv .venv
|
|
30
|
+
source .venv/bin/activate
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Install dependencies:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install -r requirements.txt
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Optional OpenCL setup
|
|
40
|
+
|
|
41
|
+
To use the OpenCL backend, make sure your system has a compatible OpenCL runtime and drivers installed.
|
|
42
|
+
|
|
43
|
+
## Quick start
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from backends.backends.tensor import Tensor
|
|
47
|
+
|
|
48
|
+
x = Tensor(2.0)
|
|
49
|
+
w = Tensor(3.0, requires_grad=True)
|
|
50
|
+
b = Tensor(4.0, requires_grad=True)
|
|
51
|
+
|
|
52
|
+
y = x * w + b
|
|
53
|
+
loss = (y - 10.0) ** 2
|
|
54
|
+
|
|
55
|
+
loss.backward()
|
|
56
|
+
|
|
57
|
+
print(y.data)
|
|
58
|
+
print(w.grad)
|
|
59
|
+
print(b.grad)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
This example creates a small computational graph, computes a scalar loss, and backpropagates gradients through the graph.
|
|
63
|
+
|
|
64
|
+
## Core concepts
|
|
65
|
+
|
|
66
|
+
### Tensor
|
|
67
|
+
|
|
68
|
+
The `Tensor` class is the foundation of the project. It stores numeric data, tracks computation history, and supports differentiation through custom arithmetic and autograd operations.
|
|
69
|
+
|
|
70
|
+
Key features include:
|
|
71
|
+
|
|
72
|
+
- scalar and array arithmetic
|
|
73
|
+
- forward pass tracking
|
|
74
|
+
- backward gradient propagation
|
|
75
|
+
- device awareness for CPU/GPU execution
|
|
76
|
+
- support for differentiable operations used in model training
|
|
77
|
+
|
|
78
|
+
### Device abstraction
|
|
79
|
+
|
|
80
|
+
The `Device` abstraction allows the library to reason about whether computation should happen on CPU or a GPU-like backend. This keeps the design simple while making it easier to experiment with accelerator-backed operations.
|
|
81
|
+
|
|
82
|
+
### OpenCL backend
|
|
83
|
+
|
|
84
|
+
The OpenCL path uses PyOpenCL to move arrays to an accelerator, execute simple kernels, and support basic tensor operations. It is useful for experimentation and understanding how low-level device execution fits into a training loop.
|
|
85
|
+
|
|
86
|
+
## Neural network utilities
|
|
87
|
+
|
|
88
|
+
The library includes a minimal `nn` module with building blocks for simple models.
|
|
89
|
+
|
|
90
|
+
### Module and Parameter
|
|
91
|
+
|
|
92
|
+
`Module` is the base class for neural network components. It provides a simple interface for registering trainable parameters.
|
|
93
|
+
|
|
94
|
+
`Parameter` wraps a tensor and marks it as trainable so that it participates in gradient-based optimization.
|
|
95
|
+
|
|
96
|
+
### Layers
|
|
97
|
+
|
|
98
|
+
The library includes a few basic layer types:
|
|
99
|
+
|
|
100
|
+
- `Linear`: a dense layer implementing `y = xW^T + b`
|
|
101
|
+
- `ReLU`: elementwise rectified linear activation
|
|
102
|
+
- `Sigmoid`: elementwise sigmoid activation
|
|
103
|
+
|
|
104
|
+
Example:
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
from nn.model_utils import Linear, ReLU
|
|
108
|
+
from backends.backends.tensor import Tensor
|
|
109
|
+
|
|
110
|
+
layer1 = Linear(in_features=4, out_features=8)
|
|
111
|
+
activation = ReLU()
|
|
112
|
+
|
|
113
|
+
x = Tensor([[1.0, 2.0, 3.0, 4.0]])
|
|
114
|
+
y = activation(layer1(x))
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Loss functions
|
|
118
|
+
|
|
119
|
+
The project includes a small set of regression-style loss implementations in `nn`.
|
|
120
|
+
|
|
121
|
+
### MSELoss
|
|
122
|
+
|
|
123
|
+
Mean Squared Error:
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
(predicted - target) ** 2
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
This computes the squared error across the prediction and is commonly used for regression tasks.
|
|
130
|
+
|
|
131
|
+
### MAELoss
|
|
132
|
+
|
|
133
|
+
Mean Absolute Error:
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
abs(predicted - target)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
This is useful when you want the average magnitude of the prediction error without squaring it.
|
|
140
|
+
|
|
141
|
+
### HuberLoss
|
|
142
|
+
|
|
143
|
+
A smooth piecewise loss that behaves like squared error for small residuals and absolute error for larger residuals. It is often used to reduce sensitivity to outliers.
|
|
144
|
+
|
|
145
|
+
Example:
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
from nn import MSELoss, MAELoss, HuberLoss
|
|
149
|
+
|
|
150
|
+
criterion = MSELoss()
|
|
151
|
+
loss = criterion(predictions, targets)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Optimizers
|
|
155
|
+
|
|
156
|
+
The library includes several optimization strategies in `nn`.
|
|
157
|
+
|
|
158
|
+
### SGD
|
|
159
|
+
|
|
160
|
+
Stochastic Gradient Descent updates parameters by moving them in the direction opposite the gradient:
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
parameter = parameter - learning_rate * gradient
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
This is the simplest gradient-based optimizer and is useful for understanding the basics of training.
|
|
167
|
+
|
|
168
|
+
### Adam
|
|
169
|
+
|
|
170
|
+
Adam combines momentum and adaptive learning rates using first and second moment estimates. It is one of the most common modern optimizers and is useful when training neural networks more stably than plain SGD.
|
|
171
|
+
|
|
172
|
+
### Muon
|
|
173
|
+
|
|
174
|
+
Muon is a momentum-based optimizer with an orthogonalized update direction. It introduces a more structure-aware update strategy and is included as an experimental optimizer in the library.
|
|
175
|
+
|
|
176
|
+
### MuonAdam
|
|
177
|
+
|
|
178
|
+
This optimizer combines Muon-style orthogonal updates with Adam-like moment estimates, making it a hybrid approach for experimentation and learning.
|
|
179
|
+
|
|
180
|
+
Example:
|
|
181
|
+
|
|
182
|
+
```python
|
|
183
|
+
from nn.optim import SGD, Adam, Muon, MuonAdam
|
|
184
|
+
|
|
185
|
+
sgd = SGD(parameters=model_parameters, learning_rate=0.01)
|
|
186
|
+
adam = Adam(parameters=model_parameters, learning_rate=0.001)
|
|
187
|
+
muon = Muon(parameters=model_parameters, learning_rate=0.02)
|
|
188
|
+
muon_adam = MuonAdam(parameters=model_parameters, learning_rate=0.02)
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Training workflow
|
|
192
|
+
|
|
193
|
+
A typical training loop looks like this:
|
|
194
|
+
|
|
195
|
+
```python
|
|
196
|
+
from nn import MSELoss
|
|
197
|
+
from nn.optim import SGD
|
|
198
|
+
|
|
199
|
+
model = ...
|
|
200
|
+
criterion = MSELoss()
|
|
201
|
+
optimizer = SGD(model._parameters.values(), learning_rate=0.01)
|
|
202
|
+
|
|
203
|
+
for batch_x, batch_y in data:
|
|
204
|
+
predictions = model(batch_x)
|
|
205
|
+
loss = criterion(predictions, batch_y)
|
|
206
|
+
|
|
207
|
+
loss.backward()
|
|
208
|
+
optimizer.step()
|
|
209
|
+
optimizer.zero_grad()
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Repository structure
|
|
213
|
+
|
|
214
|
+
```text
|
|
215
|
+
krnl/
|
|
216
|
+
├── README.md
|
|
217
|
+
├── __init__.py
|
|
218
|
+
├── requirements.txt
|
|
219
|
+
├── version.txt
|
|
220
|
+
├── src/
|
|
221
|
+
│ ├── backends/
|
|
222
|
+
│ │ ├── GPU_processer.py
|
|
223
|
+
│ │ ├── __init__.py
|
|
224
|
+
│ │ ├── device.py
|
|
225
|
+
│ │ ├── kernels.py
|
|
226
|
+
│ │ └── tensor.py
|
|
227
|
+
│ ├── nn/
|
|
228
|
+
│ │ ├── __init__.py
|
|
229
|
+
│ │ ├── loss.py
|
|
230
|
+
│ │ ├── model_utils.py
|
|
231
|
+
│ │ └── optim.py
|
|
232
|
+
│ └── tests/
|
|
233
|
+
│ ├── test_opencl_backend.py
|
|
234
|
+
│ ├── test_simple_model_implementation.py
|
|
235
|
+
│ └── test_tensor.py
|
|
236
|
+
└── .gitignore
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## Running tests
|
|
240
|
+
|
|
241
|
+
From the project root:
|
|
242
|
+
|
|
243
|
+
```bash
|
|
244
|
+
python src/tests/test_tensor.py
|
|
245
|
+
python src/tests/test_opencl_backend.py
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
The OpenCL tests require a valid OpenCL installation and compatible device support.
|
|
249
|
+
|
|
250
|
+
## Project status
|
|
251
|
+
|
|
252
|
+
`krnl` is an educational framework for understanding the internals of deep learning systems. It is not intended to replace established libraries such as PyTorch or TensorFlow.
|
|
253
|
+
|
|
254
|
+
Current focus areas include:
|
|
255
|
+
|
|
256
|
+
- tensor arithmetic and autograd
|
|
257
|
+
- gradient-based optimization
|
|
258
|
+
- CPU execution with NumPy
|
|
259
|
+
- GPU/OpenCL experimentation
|
|
260
|
+
- small neural network primitives
|
|
261
|
+
|
|
262
|
+
## Contributing
|
|
263
|
+
|
|
264
|
+
Contributions are welcome in areas such as:
|
|
265
|
+
|
|
266
|
+
- more layer types
|
|
267
|
+
- additional loss functions
|
|
268
|
+
- more optimization algorithms
|
|
269
|
+
- clearer documentation
|
|
270
|
+
- stronger tests
|
|
271
|
+
- device and performance improvements
|
|
272
|
+
|
|
273
|
+
The project remains intentionally simple and readable so it can serve as a learning tool.
|
|
274
|
+
|
|
275
|
+
## License
|
|
276
|
+
|
|
277
|
+
This repository does not currently include a license file. If you plan to distribute or publish it publicly, consider adding a license such as MIT or Apache 2.0.
|
|
278
|
+
|
|
279
|
+
## Acknowledgements
|
|
280
|
+
|
|
281
|
+
This project is built for experimentation and education in tensor math, computational graphs, optimization, and device-aware execution.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "krnl"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A lightweight educational tensor and automatic differentiation library with CPU and OpenCL GPU support"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Adhavan", email = "helloadhavan@gmaim.com" },
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
dependencies = [
|
|
17
|
+
"attrs==26.1.0",
|
|
18
|
+
"Automat==25.4.16",
|
|
19
|
+
"constantly==23.10.4",
|
|
20
|
+
"construct==2.10.70",
|
|
21
|
+
"hyperlink==21.0.0",
|
|
22
|
+
"idna==3.20",
|
|
23
|
+
"Incremental==24.11.0",
|
|
24
|
+
"Mako==1.4.3",
|
|
25
|
+
"MarkupSafe==3.0.4",
|
|
26
|
+
"numpy==2.5.4",
|
|
27
|
+
"packaging==26.3",
|
|
28
|
+
"platformdirs==4.13.5",
|
|
29
|
+
"protobuf==7.36.2",
|
|
30
|
+
"psutil==7.2.2",
|
|
31
|
+
"pyasn1==0.6.4",
|
|
32
|
+
"pyopencl==2026.1.4",
|
|
33
|
+
"pysnmp==7.1.30",
|
|
34
|
+
"pytools==2026.1.1",
|
|
35
|
+
"PyYAML==6.0.3",
|
|
36
|
+
"siphash24==1.9",
|
|
37
|
+
"Twisted==26.4.0",
|
|
38
|
+
"typing_extensions==4.16.0",
|
|
39
|
+
"zope.interface==8.7",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[tool.setuptools]
|
|
43
|
+
package-dir = {"" = "src"}
|
|
44
|
+
|
|
45
|
+
[tool.setuptools.packages.find]
|
|
46
|
+
where = ["src"]
|
krnl-0.1.0/setup.cfg
ADDED