dongik 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.
@@ -0,0 +1,32 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - 'v*'
7
+
8
+ jobs:
9
+ deploy:
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - name: Checkout code
14
+ uses: actions/checkout@v4
15
+
16
+ - name: Set up uv
17
+ uses: astral-sh/setup-uv@v5
18
+ with:
19
+ enable-cache: true
20
+
21
+ - name: Set up Python
22
+ uses: actions/setup-python@v5
23
+ with:
24
+ python-version: '3.11'
25
+
26
+ - name: Build package distributions
27
+ run: uv build
28
+
29
+ - name: Publish package to PyPI
30
+ uses: pypa/gh-action-pypi-publish@release/v1
31
+ with:
32
+ password: ${{ secrets.PYPI_API_TOKEN }}
@@ -0,0 +1,9 @@
1
+ # Python
2
+ .venv/
3
+ .ruff_cache/
4
+ __pycache__
5
+ dist/
6
+
7
+ # Editor
8
+ .vscode/
9
+ .zed/
@@ -0,0 +1,34 @@
1
+ fail_fast: false
2
+
3
+ repos:
4
+ - repo: https://github.com/pre-commit/pre-commit-hooks
5
+ rev: v5.0.0
6
+ hooks:
7
+ - id: trailing-whitespace
8
+ - id: end-of-file-fixer
9
+ - id: check-yaml
10
+ - id: check-added-large-files
11
+
12
+ - repo: https://github.com/pycqa/isort
13
+ rev: 5.12.0
14
+ hooks:
15
+ - id: isort
16
+ name: isort (python)
17
+ args:
18
+ - --profile
19
+ - black
20
+ - --filter-files
21
+
22
+ - repo: https://github.com/psf/black
23
+ rev: 24.10.0
24
+ hooks:
25
+ - id: black
26
+ args:
27
+ - --line-length=88
28
+
29
+ - repo: https://github.com/charliermarsh/ruff-pre-commit
30
+ rev: v0.7.3
31
+ hooks:
32
+ - id: ruff
33
+ args:
34
+ - --fix
@@ -0,0 +1 @@
1
+ 3.13
dongik-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Dongik Sohn
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.
dongik-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,13 @@
1
+ Metadata-Version: 2.5
2
+ Name: dongik
3
+ Version: 0.1.0
4
+ Summary: A short description of my package
5
+ Project-URL: Homepage, https://github.com/don-gik/pypi-dongik
6
+ Author-email: don-gik <224256443+don-gik@users.noreply.github.com>
7
+ License-File: LICENSE
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Programming Language :: Python :: 3
11
+ Requires-Python: >=3.8
12
+ Requires-Dist: torch>=2.5.1
13
+ Requires-Dist: torchvision>=0.20.1
dongik-0.1.0/README.md ADDED
File without changes
@@ -0,0 +1,43 @@
1
+ [project]
2
+ name = "dongik"
3
+ version = "0.1.0"
4
+ authors = [
5
+ { name = "don-gik", email = "224256443+don-gik@users.noreply.github.com" }
6
+ ]
7
+ description = "A short description of my package"
8
+ readme = "README.md"
9
+ requires-python = ">=3.8"
10
+ classifiers = [
11
+ "Programming Language :: Python :: 3",
12
+ "License :: OSI Approved :: MIT License",
13
+ "Operating System :: OS Independent",
14
+ ]
15
+ dependencies = [
16
+ "torch>=2.5.1",
17
+ "torchvision>=0.20.1",
18
+ ]
19
+
20
+ [project.urls]
21
+ Homepage = "https://github.com/don-gik/pypi-dongik"
22
+
23
+ [build-system]
24
+ requires = ["hatchling"]
25
+ build-backend = "hatchling.build"
26
+
27
+ [tool.hatch.build.targets.wheel]
28
+ packages = ["src/dongik"]
29
+
30
+ [[tool.uv.index]]
31
+ name = "pytorch-cpu"
32
+ url = "https://download.pytorch.org/whl/cpu"
33
+ explicit = true
34
+
35
+ [tool.uv.sources]
36
+ torch = { index = "pytorch-cpu" }
37
+ torchvision = { index = "pytorch-cpu" }
38
+
39
+ [dependency-groups]
40
+ dev = [
41
+ "pre-commit>=3.5.0",
42
+ "ruff>=0.16.5",
43
+ ]
@@ -0,0 +1,5 @@
1
+ # src/dongik/__init__.py
2
+
3
+ from src.dongik import data, nn, ops, optim
4
+
5
+ __all__ = ["data", "nn", "ops", "optim"]
@@ -0,0 +1,81 @@
1
+ # src/dongik/data.py
2
+
3
+ from __future__ import annotations
4
+
5
+ import random
6
+ from typing import Iterator, overload
7
+
8
+ import torch
9
+ from torchvision import datasets, transforms
10
+
11
+
12
+ class MNISTData:
13
+ def __init__(
14
+ self, samples: int = 5000, augment: bool = True, root: str = "./data"
15
+ ) -> None:
16
+ transform_list = []
17
+ if augment:
18
+ transform_list.extend(
19
+ [
20
+ transforms.RandomRotation(degrees=15),
21
+ transforms.RandomAffine(degrees=0, translate=(0.1, 0.1)),
22
+ ]
23
+ )
24
+
25
+ transform_list.append(transforms.ToTensor())
26
+
27
+ raw_ds = datasets.MNIST(
28
+ root=root,
29
+ train=True,
30
+ download=True,
31
+ transform=transforms.Compose(transform_list),
32
+ )
33
+
34
+ self._images: list[torch.Tensor] = []
35
+ self._labels: list[list[float]] = []
36
+
37
+ max_len = min(samples, len(raw_ds))
38
+ for i in range(max_len):
39
+ img_tensor, label_idx = raw_ds[i]
40
+ self._images.append(img_tensor.view(-1))
41
+
42
+ one_hot = [0.0] * 10
43
+ one_hot[int(label_idx)] = 1.0
44
+ self._labels.append(one_hot)
45
+
46
+ def __len__(self) -> int:
47
+ return len(self._labels)
48
+
49
+ @overload
50
+ def __getitem__(self, idx: int) -> tuple[list[float], list[float]]: ...
51
+ @overload
52
+ def __getitem__(self, idx: slice) -> list[tuple[list[float], list[float]]]: ...
53
+
54
+ def __getitem__(
55
+ self, idx: int | slice
56
+ ) -> tuple[list[float], list[float]] | list[tuple[list[float], list[float]]]:
57
+ # dataset[:10] Slicing support
58
+ if isinstance(idx, slice):
59
+ return [self[i] for i in range(*idx.indices(len(self)))]
60
+
61
+ x_list = self._images[idx].tolist()
62
+ y_list = self._labels[idx]
63
+ return x_list, y_list
64
+
65
+ def __iter__(self) -> Iterator[tuple[list[float], list[float]]]:
66
+ # for x, y in dataset
67
+ for i in range(len(self)):
68
+ yield self[i]
69
+
70
+ def shuffle(self) -> None:
71
+ combined = list(zip(self._images, self._labels))
72
+ random.shuffle(combined)
73
+ images_tuple, labels_tuple = zip(*combined)
74
+ self._images = list(images_tuple)
75
+ self._labels = list(labels_tuple)
76
+
77
+
78
+ def get_mnist(
79
+ samples: int = 5000, augment: bool = True, root: str = "./data"
80
+ ) -> MNISTData:
81
+ return MNISTData(samples=samples, augment=augment, root=root)
@@ -0,0 +1,129 @@
1
+ # src/dongik/nn.py
2
+
3
+ from __future__ import annotations
4
+
5
+ import abc
6
+ import math
7
+ import random
8
+ from typing import Any, Callable, overload
9
+
10
+ import torch
11
+
12
+
13
+ class Base(abc.ABC):
14
+ input_cnt: int
15
+ weight: list[float]
16
+ bias: float
17
+ d_weight: list[float]
18
+ d_bias: float
19
+ inputs: list[float]
20
+
21
+ _compiled_forward: Callable[..., Any]
22
+ _compiled_backward: Callable[..., Any]
23
+
24
+ def __init_subclass__(cls, **kwargs) -> None:
25
+ super().__init_subclass__(**kwargs)
26
+ orig_init = cls.__init__
27
+
28
+ def wrapped_init(self, *args, **kwargs):
29
+ self.input_cnt = kwargs.get(
30
+ "input_cnt", args[0] if args and isinstance(args[0], int) else 0
31
+ )
32
+ self.weight = []
33
+ self.bias = 0.0
34
+ self.d_weight = []
35
+ self.d_bias = 0.0
36
+ self.inputs = []
37
+
38
+ orig_init(self, *args, **kwargs)
39
+
40
+ # He Initialization
41
+ if self.input_cnt > 0 and not self.weight:
42
+ std = math.sqrt(2.0 / self.input_cnt)
43
+ self.weight = [random.gauss(0, std) for _ in range(self.input_cnt)]
44
+ self.bias = 0.0
45
+ self.d_weight = [0.0] * self.input_cnt
46
+ self.d_bias = 0.0
47
+
48
+ self._compiled_forward = torch.compile(self.forward, mode="default")
49
+ self._compiled_backward = torch.compile(self.backward, mode="default")
50
+
51
+ cls.__init__ = wrapped_init # type: ignore[method-assign]
52
+
53
+ def __init__(self, input_cnt: int = 0) -> None:
54
+ pass
55
+
56
+ @abc.abstractmethod
57
+ def forward(self, inputs: list[float]) -> float | list[float]:
58
+ pass
59
+
60
+ @abc.abstractmethod
61
+ def backward(self, loss: float | list[float]) -> list[float]:
62
+ pass
63
+
64
+ @overload
65
+ def __call__(self, inputs: float) -> float: ...
66
+ @overload
67
+ def __call__(self, inputs: list[float]) -> list[float]: ...
68
+ @overload
69
+ def __call__(self, inputs: Any) -> Any: ...
70
+
71
+ def __call__(self, inputs: Any) -> Any:
72
+ is_root = not isinstance(inputs, torch.Tensor)
73
+ t_inputs = torch.as_tensor(inputs, dtype=torch.float32) if is_root else inputs
74
+
75
+ res = self._compiled_forward(t_inputs)
76
+
77
+ if is_root and isinstance(res, torch.Tensor):
78
+ return res.tolist() if res.ndim > 0 else res.item()
79
+ return res
80
+
81
+ @overload
82
+ def step_backward(self, loss: float) -> float: ...
83
+ @overload
84
+ def step_backward(self, loss: list[float]) -> list[float]: ...
85
+ @overload
86
+ def step_backward(self, loss: Any) -> Any: ...
87
+
88
+ def step_backward(self, loss: Any) -> Any:
89
+ is_root = not isinstance(loss, torch.Tensor)
90
+ t_loss = torch.as_tensor(loss, dtype=torch.float32) if is_root else loss
91
+
92
+ grad = self._compiled_backward(t_loss)
93
+
94
+ if is_root and isinstance(grad, torch.Tensor):
95
+ return grad.tolist() if grad.ndim > 0 else grad.item()
96
+ return grad
97
+
98
+ def get_leaf_nodes(self, visited: set[int] | None = None) -> list[Base]:
99
+ if visited is None:
100
+ visited = set()
101
+
102
+ if id(self) in visited:
103
+ return []
104
+ visited.add(id(self))
105
+
106
+ child_nodes: list[Base] = []
107
+ for attr_value in self.__dict__.values():
108
+ child_nodes.extend(self._find_base_nodes(attr_value, visited))
109
+
110
+ if not child_nodes:
111
+ if self.weight and self.d_weight:
112
+ return [self]
113
+ return []
114
+
115
+ return child_nodes
116
+
117
+ def _find_base_nodes(self, obj: Any, visited: set[int]) -> list[Base]:
118
+ found: list[Base] = []
119
+
120
+ if isinstance(obj, Base):
121
+ found.extend(obj.get_leaf_nodes(visited))
122
+ elif isinstance(obj, (list, tuple)):
123
+ for item in obj:
124
+ found.extend(self._find_base_nodes(item, visited))
125
+ elif isinstance(obj, dict):
126
+ for item in obj.values():
127
+ found.extend(self._find_base_nodes(item, visited))
128
+
129
+ return found
@@ -0,0 +1,127 @@
1
+ # src/dongik/ops.py
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Any, overload
6
+
7
+ import torch
8
+
9
+ Number = int | float
10
+
11
+ # ==========================================
12
+ # Torch Kernel
13
+ # ==========================================
14
+
15
+
16
+ @torch.compile(mode="default")
17
+ def _fast_multiply(a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
18
+ return a * b
19
+
20
+
21
+ @torch.compile(mode="default")
22
+ def _fast_add(a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
23
+ return a + b
24
+
25
+
26
+ @torch.compile(mode="default")
27
+ def _fast_sum(a: torch.Tensor) -> torch.Tensor:
28
+ return torch.sum(a)
29
+
30
+
31
+ # ==========================================
32
+ # APIs
33
+ # ==========================================
34
+
35
+
36
+ @overload
37
+ def multiply(a: Number, b: Number) -> float: ...
38
+ @overload
39
+ def multiply(a: list[float], b: Number) -> list[float]: ...
40
+ @overload
41
+ def multiply(a: Number, b: list[float]) -> list[float]: ...
42
+ @overload
43
+ def multiply(a: list[float], b: list[float]) -> list[float]: ...
44
+ @overload
45
+ def multiply(a: Any, b: Any) -> Any: ...
46
+
47
+
48
+ def multiply(a: Any, b: Any) -> Any:
49
+ if isinstance(a, torch.Tensor) or isinstance(b, torch.Tensor):
50
+ t_a = (
51
+ a
52
+ if isinstance(a, torch.Tensor)
53
+ else torch.as_tensor(a, dtype=torch.float32)
54
+ )
55
+ t_b = (
56
+ b
57
+ if isinstance(b, torch.Tensor)
58
+ else torch.as_tensor(b, dtype=torch.float32)
59
+ )
60
+ return _fast_multiply(t_a, t_b)
61
+
62
+ if isinstance(a, (int, float)) and isinstance(b, (int, float)):
63
+ return float(a * b)
64
+
65
+ t_a = torch.as_tensor(a, dtype=torch.float32)
66
+ t_b = torch.as_tensor(b, dtype=torch.float32)
67
+ res = _fast_multiply(t_a, t_b)
68
+ return res.tolist() if res.ndim > 0 else res.item()
69
+
70
+
71
+ @overload
72
+ def add(a: Number, b: Number) -> float: ...
73
+ @overload
74
+ def add(a: list[float], b: Number) -> list[float]: ...
75
+ @overload
76
+ def add(a: Number, b: list[float]) -> list[float]: ...
77
+ @overload
78
+ def add(a: list[float], b: list[float]) -> list[float]: ...
79
+ @overload
80
+ def add(a: Any, b: Any) -> Any: ...
81
+
82
+
83
+ def add(a: Any, b: Any) -> Any:
84
+ if isinstance(a, torch.Tensor) or isinstance(b, torch.Tensor):
85
+ t_a = (
86
+ a
87
+ if isinstance(a, torch.Tensor)
88
+ else torch.as_tensor(a, dtype=torch.float32)
89
+ )
90
+ t_b = (
91
+ b
92
+ if isinstance(b, torch.Tensor)
93
+ else torch.as_tensor(b, dtype=torch.float32)
94
+ )
95
+ return _fast_add(t_a, t_b)
96
+
97
+ if isinstance(a, (int, float)) and isinstance(b, (int, float)):
98
+ return float(a + b)
99
+
100
+ t_a = torch.as_tensor(a, dtype=torch.float32)
101
+ t_b = torch.as_tensor(b, dtype=torch.float32)
102
+ res = _fast_add(t_a, t_b)
103
+ return res.tolist() if res.ndim > 0 else res.item()
104
+
105
+
106
+ @overload
107
+ def sum_all(a: Number) -> float: ...
108
+ @overload
109
+ def sum_all(a: list[float]) -> float: ...
110
+ @overload
111
+ def sum_all(a: Any) -> Any: ...
112
+
113
+
114
+ def sum_all(a: Any) -> Any:
115
+ if isinstance(a, torch.Tensor):
116
+ t_a = (
117
+ a
118
+ if isinstance(a, torch.Tensor)
119
+ else torch.as_tensor(a, dtype=torch.float32)
120
+ )
121
+ return _fast_sum(t_a)
122
+
123
+ if isinstance(a, (int, float)):
124
+ return float(a)
125
+
126
+ t_a = torch.as_tensor(a, dtype=torch.float32)
127
+ return _fast_sum(t_a).item()
@@ -0,0 +1,21 @@
1
+ # src/dongik/optim.py
2
+
3
+ from __future__ import annotations
4
+
5
+ import abc
6
+
7
+ from dongik.nn import Base
8
+
9
+
10
+ class BaseUpdater(abc.ABC):
11
+ def __init__(self, model: Base, lr: float = 0.01) -> None:
12
+ self.model = model
13
+ self.lr = lr
14
+
15
+ @abc.abstractmethod
16
+ def update_node(self, node: Base) -> None:
17
+ pass
18
+
19
+ def update(self) -> None:
20
+ for node in self.model.get_leaf_nodes():
21
+ self.update_node(node)