DepthTensor 2.3.0__tar.gz → 2.4.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.
- {depthtensor-2.3.0 → depthtensor-2.4.0}/LICENSE +1 -1
- depthtensor-2.4.0/PKG-INFO +155 -0
- depthtensor-2.4.0/README.md +115 -0
- depthtensor-2.4.0/pyproject.toml +35 -0
- {depthtensor-2.3.0 → depthtensor-2.4.0}/src/DepthTensor/__init__.py +1 -1
- depthtensor-2.4.0/src/DepthTensor/autodiff.py +75 -0
- {depthtensor-2.3.0 → depthtensor-2.4.0}/src/DepthTensor/tensor.py +4 -0
- depthtensor-2.4.0/src/depthtensor.egg-info/PKG-INFO +155 -0
- {depthtensor-2.3.0/src/DepthTensor.egg-info → depthtensor-2.4.0/src/depthtensor.egg-info}/SOURCES.txt +6 -6
- {depthtensor-2.3.0/src/DepthTensor.egg-info → depthtensor-2.4.0/src/depthtensor.egg-info}/requires.txt +1 -1
- depthtensor-2.3.0/PKG-INFO +0 -24
- depthtensor-2.3.0/README.md +0 -14
- depthtensor-2.3.0/pyproject.toml +0 -19
- depthtensor-2.3.0/src/DepthTensor/autodiff.py +0 -53
- depthtensor-2.3.0/src/DepthTensor.egg-info/PKG-INFO +0 -24
- {depthtensor-2.3.0 → depthtensor-2.4.0}/setup.cfg +0 -0
- {depthtensor-2.3.0 → depthtensor-2.4.0}/src/DepthTensor/_core/__init__.py +0 -0
- {depthtensor-2.3.0 → depthtensor-2.4.0}/src/DepthTensor/_core/exceptions.py +0 -0
- {depthtensor-2.3.0 → depthtensor-2.4.0}/src/DepthTensor/_core/ops/__init__.py +0 -0
- {depthtensor-2.3.0 → depthtensor-2.4.0}/src/DepthTensor/_core/ops/comparison.py +0 -0
- {depthtensor-2.3.0 → depthtensor-2.4.0}/src/DepthTensor/_core/ops/creation.py +0 -0
- {depthtensor-2.3.0 → depthtensor-2.4.0}/src/DepthTensor/_core/ops/diff.py +0 -0
- {depthtensor-2.3.0 → depthtensor-2.4.0}/src/DepthTensor/_core/ops/elementwise.py +0 -0
- {depthtensor-2.3.0 → depthtensor-2.4.0}/src/DepthTensor/_core/ops/reduction.py +0 -0
- {depthtensor-2.3.0 → depthtensor-2.4.0}/src/DepthTensor/_core/random/__init__.py +0 -0
- {depthtensor-2.3.0 → depthtensor-2.4.0}/src/DepthTensor/_core/random/generator.py +0 -0
- {depthtensor-2.3.0 → depthtensor-2.4.0}/src/DepthTensor/_core/utils.py +0 -0
- {depthtensor-2.3.0 → depthtensor-2.4.0}/src/DepthTensor/typing.py +0 -0
- {depthtensor-2.3.0/src/DepthTensor.egg-info → depthtensor-2.4.0/src/depthtensor.egg-info}/dependency_links.txt +0 -0
- {depthtensor-2.3.0/src/DepthTensor.egg-info → depthtensor-2.4.0/src/depthtensor.egg-info}/top_level.txt +0 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: depthtensor
|
|
3
|
+
Version: 2.4.0
|
|
4
|
+
Summary: A Hardware-Accelerated Tensor Computation and Autograd Engine
|
|
5
|
+
Author-email: Le Hong Ha <lehahong1310@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 Le Hong Ha
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
Project-URL: Homepage, https://github.com/l-h-ha/DepthTensor
|
|
28
|
+
Project-URL: Bug Tracker, https://github.com/l-h-ha/DepthTensor/issues
|
|
29
|
+
Classifier: Programming Language :: Python :: 3
|
|
30
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
31
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
32
|
+
Classifier: Operating System :: OS Independent
|
|
33
|
+
Requires-Python: >=3.8
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
License-File: LICENSE
|
|
36
|
+
Requires-Dist: numpy
|
|
37
|
+
Provides-Extra: gpu
|
|
38
|
+
Requires-Dist: cupy; extra == "gpu"
|
|
39
|
+
Dynamic: license-file
|
|
40
|
+
|
|
41
|
+
# DepthTensor: A Hardware-Accelerated Tensor Computation and Autograd Engine
|
|
42
|
+
|
|
43
|
+
**DepthTensor** is a light-weight, high-performance library for reverse-mode automatic differentiation (AD). It is useful in building the mathematical foundation of deep learning frameworks, with the use of a `Tensor` object which dynamically builds computational graphs and computes gradients using **Vector-Jacobian Products (VJP)**, generalized for tensors of abritrary rank.
|
|
44
|
+
|
|
45
|
+
> **Note**: This is the core autograd and tensor computation engine. For the full deep learning framework, refer to [DepthML](https://github.com/l-h-ha/DepthML).
|
|
46
|
+
|
|
47
|
+
## 1. Mathematical Foundation
|
|
48
|
+
|
|
49
|
+
The goal is to compute the gradient of scalar field $L$ with respect to an input tensor $X$, denoted as the adjoint $\bar{X}$.
|
|
50
|
+
|
|
51
|
+
### 1.1. Generalized VJPs via Tensor Contractions
|
|
52
|
+
|
|
53
|
+
Let $X$ be a tensor of shape $\mathcal{I} = (i_1, \dots, i_n)$ and $Y = f(X)$ be a tensor of shape $\mathcal{J} = (j_1, \dots, j_m)$.
|
|
54
|
+
|
|
55
|
+
Mathematically, the Jacobian is the tensor of rank $n + m$ which contains all the partial derivatives $\frac{\partial Y_{\mathcal{J}}}{\partial X_{\mathcal{I}}}$. DepthTensor does not compute this object, but, rather, it computes the contraction of the incoming adjoint $\bar{Y}$ with the local derivative:
|
|
56
|
+
|
|
57
|
+
$$
|
|
58
|
+
\bar{X}_{i_1 \dots i_n} = \sum_{j_1 \dots j_m} \bar{Y}_{j_1 \dots j_m} \frac{\partial Y_{j_1 \dots j_m}}{\partial X_{i_1 \dots i_n}}
|
|
59
|
+
$$
|
|
60
|
+
|
|
61
|
+
If X and Y are scalars, indices vanish, and this reduces to:
|
|
62
|
+
|
|
63
|
+
$$
|
|
64
|
+
\bar{x} = \bar{y} \cdot f'(x)
|
|
65
|
+
$$
|
|
66
|
+
|
|
67
|
+
If X and Y are matrices, this reduces to:
|
|
68
|
+
|
|
69
|
+
$$
|
|
70
|
+
\bar{X} = \bar{Y} W^{T}
|
|
71
|
+
$$
|
|
72
|
+
|
|
73
|
+
### 1.2. Graph Topology and Gradient Accumulation
|
|
74
|
+
|
|
75
|
+
Gradients are accumulated using a Depth-First Search (DFS) topological sort, which ensures that for any node $X$ with multiple gradient flow streams ${Y^{(1)}, \dots, Y^{k}}$, the gradient sum is the sum of contractions:
|
|
76
|
+
|
|
77
|
+
$$
|
|
78
|
+
\bar{X} = \sum_{k} \mathrm{VJP}(Y^{(k)}, X)
|
|
79
|
+
$$
|
|
80
|
+
|
|
81
|
+
All gradients are aggregated from all gradient downstreams.
|
|
82
|
+
|
|
83
|
+
## 2. Architecture and System Design
|
|
84
|
+
|
|
85
|
+
### 2.1. The differentiable Primitive (`Tensor`)
|
|
86
|
+
|
|
87
|
+
The `Tensor` class acts like a node in the Directed Acyclic Graph (DAG).
|
|
88
|
+
|
|
89
|
+
Operations are automatically dispatched to backend computers, which consists of `numpy` (CPU) and `cupy` (GPU).
|
|
90
|
+
|
|
91
|
+
Computational graphs are built dynamically at runtime.
|
|
92
|
+
|
|
93
|
+
## 3. Empirical Validation
|
|
94
|
+
|
|
95
|
+
We will verify the tensor engine by minimizing the Rosenbrock function, which is a non-convex optimization benchmark:
|
|
96
|
+
|
|
97
|
+
$$
|
|
98
|
+
f(x, y) = (a - x)^2 + b(y - x^2)^2
|
|
99
|
+
$$
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
import depthtensor as dt
|
|
103
|
+
|
|
104
|
+
# Initialize tensors
|
|
105
|
+
x = dt.Tensor([1.2], device="gpu", requires_grad=True)
|
|
106
|
+
y = dt.Tensor([1.2], device="gpu", requires_grad=True)
|
|
107
|
+
|
|
108
|
+
a, b = dt.Tensor([1], device="gpu"), dt.Tensor(
|
|
109
|
+
[100], device="gpu"
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
# 2. Optimization Loop
|
|
113
|
+
lr = 0.001
|
|
114
|
+
for i in range(500):
|
|
115
|
+
# Rosenbrock: f(x,y) = (a-x)^2 + b(y-x^2)^2
|
|
116
|
+
loss = (a - x) ** 2 + b * (y - x**2) ** 2
|
|
117
|
+
|
|
118
|
+
# Backpward pass
|
|
119
|
+
dt.differentiate(loss)
|
|
120
|
+
|
|
121
|
+
# Gradient Descent
|
|
122
|
+
x.data -= lr * x.grad # type: ignore
|
|
123
|
+
y.data -= lr * y.grad # type: ignore
|
|
124
|
+
|
|
125
|
+
# Zero grads
|
|
126
|
+
x.zeros_grad()
|
|
127
|
+
y.zeros_grad()
|
|
128
|
+
|
|
129
|
+
if i % 10 == 0:
|
|
130
|
+
print(loss.item())
|
|
131
|
+
|
|
132
|
+
print(f"Converged: ({x.data}, {y.data})")
|
|
133
|
+
# Target: (1.0, 1.0)
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## 4. Installation
|
|
137
|
+
|
|
138
|
+
Requirements:
|
|
139
|
+
- `numpy`
|
|
140
|
+
- `cupy` (optional: for NVIDIA GPU acceleration)
|
|
141
|
+
|
|
142
|
+
### 4.1. Bash
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
git clone https://github.com/YourUsername/DepthTensor.git
|
|
146
|
+
pip install -e .
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### 4.2. Pip
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
pip install depthtensor
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Author: Le Hong Ha
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# DepthTensor: A Hardware-Accelerated Tensor Computation and Autograd Engine
|
|
2
|
+
|
|
3
|
+
**DepthTensor** is a light-weight, high-performance library for reverse-mode automatic differentiation (AD). It is useful in building the mathematical foundation of deep learning frameworks, with the use of a `Tensor` object which dynamically builds computational graphs and computes gradients using **Vector-Jacobian Products (VJP)**, generalized for tensors of abritrary rank.
|
|
4
|
+
|
|
5
|
+
> **Note**: This is the core autograd and tensor computation engine. For the full deep learning framework, refer to [DepthML](https://github.com/l-h-ha/DepthML).
|
|
6
|
+
|
|
7
|
+
## 1. Mathematical Foundation
|
|
8
|
+
|
|
9
|
+
The goal is to compute the gradient of scalar field $L$ with respect to an input tensor $X$, denoted as the adjoint $\bar{X}$.
|
|
10
|
+
|
|
11
|
+
### 1.1. Generalized VJPs via Tensor Contractions
|
|
12
|
+
|
|
13
|
+
Let $X$ be a tensor of shape $\mathcal{I} = (i_1, \dots, i_n)$ and $Y = f(X)$ be a tensor of shape $\mathcal{J} = (j_1, \dots, j_m)$.
|
|
14
|
+
|
|
15
|
+
Mathematically, the Jacobian is the tensor of rank $n + m$ which contains all the partial derivatives $\frac{\partial Y_{\mathcal{J}}}{\partial X_{\mathcal{I}}}$. DepthTensor does not compute this object, but, rather, it computes the contraction of the incoming adjoint $\bar{Y}$ with the local derivative:
|
|
16
|
+
|
|
17
|
+
$$
|
|
18
|
+
\bar{X}_{i_1 \dots i_n} = \sum_{j_1 \dots j_m} \bar{Y}_{j_1 \dots j_m} \frac{\partial Y_{j_1 \dots j_m}}{\partial X_{i_1 \dots i_n}}
|
|
19
|
+
$$
|
|
20
|
+
|
|
21
|
+
If X and Y are scalars, indices vanish, and this reduces to:
|
|
22
|
+
|
|
23
|
+
$$
|
|
24
|
+
\bar{x} = \bar{y} \cdot f'(x)
|
|
25
|
+
$$
|
|
26
|
+
|
|
27
|
+
If X and Y are matrices, this reduces to:
|
|
28
|
+
|
|
29
|
+
$$
|
|
30
|
+
\bar{X} = \bar{Y} W^{T}
|
|
31
|
+
$$
|
|
32
|
+
|
|
33
|
+
### 1.2. Graph Topology and Gradient Accumulation
|
|
34
|
+
|
|
35
|
+
Gradients are accumulated using a Depth-First Search (DFS) topological sort, which ensures that for any node $X$ with multiple gradient flow streams ${Y^{(1)}, \dots, Y^{k}}$, the gradient sum is the sum of contractions:
|
|
36
|
+
|
|
37
|
+
$$
|
|
38
|
+
\bar{X} = \sum_{k} \mathrm{VJP}(Y^{(k)}, X)
|
|
39
|
+
$$
|
|
40
|
+
|
|
41
|
+
All gradients are aggregated from all gradient downstreams.
|
|
42
|
+
|
|
43
|
+
## 2. Architecture and System Design
|
|
44
|
+
|
|
45
|
+
### 2.1. The differentiable Primitive (`Tensor`)
|
|
46
|
+
|
|
47
|
+
The `Tensor` class acts like a node in the Directed Acyclic Graph (DAG).
|
|
48
|
+
|
|
49
|
+
Operations are automatically dispatched to backend computers, which consists of `numpy` (CPU) and `cupy` (GPU).
|
|
50
|
+
|
|
51
|
+
Computational graphs are built dynamically at runtime.
|
|
52
|
+
|
|
53
|
+
## 3. Empirical Validation
|
|
54
|
+
|
|
55
|
+
We will verify the tensor engine by minimizing the Rosenbrock function, which is a non-convex optimization benchmark:
|
|
56
|
+
|
|
57
|
+
$$
|
|
58
|
+
f(x, y) = (a - x)^2 + b(y - x^2)^2
|
|
59
|
+
$$
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import depthtensor as dt
|
|
63
|
+
|
|
64
|
+
# Initialize tensors
|
|
65
|
+
x = dt.Tensor([1.2], device="gpu", requires_grad=True)
|
|
66
|
+
y = dt.Tensor([1.2], device="gpu", requires_grad=True)
|
|
67
|
+
|
|
68
|
+
a, b = dt.Tensor([1], device="gpu"), dt.Tensor(
|
|
69
|
+
[100], device="gpu"
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
# 2. Optimization Loop
|
|
73
|
+
lr = 0.001
|
|
74
|
+
for i in range(500):
|
|
75
|
+
# Rosenbrock: f(x,y) = (a-x)^2 + b(y-x^2)^2
|
|
76
|
+
loss = (a - x) ** 2 + b * (y - x**2) ** 2
|
|
77
|
+
|
|
78
|
+
# Backpward pass
|
|
79
|
+
dt.differentiate(loss)
|
|
80
|
+
|
|
81
|
+
# Gradient Descent
|
|
82
|
+
x.data -= lr * x.grad # type: ignore
|
|
83
|
+
y.data -= lr * y.grad # type: ignore
|
|
84
|
+
|
|
85
|
+
# Zero grads
|
|
86
|
+
x.zeros_grad()
|
|
87
|
+
y.zeros_grad()
|
|
88
|
+
|
|
89
|
+
if i % 10 == 0:
|
|
90
|
+
print(loss.item())
|
|
91
|
+
|
|
92
|
+
print(f"Converged: ({x.data}, {y.data})")
|
|
93
|
+
# Target: (1.0, 1.0)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## 4. Installation
|
|
97
|
+
|
|
98
|
+
Requirements:
|
|
99
|
+
- `numpy`
|
|
100
|
+
- `cupy` (optional: for NVIDIA GPU acceleration)
|
|
101
|
+
|
|
102
|
+
### 4.1. Bash
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
git clone https://github.com/YourUsername/DepthTensor.git
|
|
106
|
+
pip install -e .
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 4.2. Pip
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
pip install depthtensor
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Author: Le Hong Ha
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "depthtensor"
|
|
7
|
+
version = "2.4.0"
|
|
8
|
+
description = "A Hardware-Accelerated Tensor Computation and Autograd Engine"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [
|
|
11
|
+
{ name = "Le Hong Ha", email = "lehahong1310@gmail.com" }
|
|
12
|
+
]
|
|
13
|
+
license = { file = "LICENSE" }
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence", # Fixed
|
|
17
|
+
"License :: OSI Approved :: MIT License", # Fixed
|
|
18
|
+
"Operating System :: OS Independent"
|
|
19
|
+
]
|
|
20
|
+
requires-python = ">=3.8"
|
|
21
|
+
dependencies = [
|
|
22
|
+
"numpy"
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.optional-dependencies]
|
|
26
|
+
gpu = [
|
|
27
|
+
"cupy"
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
"Homepage" = "https://github.com/l-h-ha/DepthTensor"
|
|
32
|
+
"Bug Tracker" = "https://github.com/l-h-ha/DepthTensor/issues"
|
|
33
|
+
|
|
34
|
+
[tool.setuptools.packages.find]
|
|
35
|
+
where = ["src"]
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
from typing import List, Set
|
|
2
|
+
|
|
3
|
+
from . import Tensor
|
|
4
|
+
from .typing import NDArrayLike
|
|
5
|
+
from ._core.exceptions import (
|
|
6
|
+
GradientComputationError,
|
|
7
|
+
GRADIENT_COMPUTATION_ERROR,
|
|
8
|
+
CuPyNotFound,
|
|
9
|
+
CUPY_NOT_FOUND_MSG,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
import numpy as np
|
|
13
|
+
|
|
14
|
+
try:
|
|
15
|
+
import cupy as cp
|
|
16
|
+
except (ModuleNotFoundError, ImportError):
|
|
17
|
+
cp = None
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _grad_check(grad: NDArrayLike, tensor: Tensor) -> None:
|
|
21
|
+
if cp is not None and (tensor.device == "gpu" and not isinstance(grad, cp.ndarray)):
|
|
22
|
+
raise RuntimeError(f"Expected gradient to be a cupy.ndarray, got: {type(grad)}")
|
|
23
|
+
if tensor.device == "cpu" and not isinstance(grad, np.ndarray):
|
|
24
|
+
raise RuntimeError(
|
|
25
|
+
f"Expected gradient to be a numpy.ndarray, got: {type(grad)}"
|
|
26
|
+
)
|
|
27
|
+
if grad.shape != tensor.shape:
|
|
28
|
+
raise RuntimeError(
|
|
29
|
+
f"Gradient's shape is incompatible with tensor's. Expected {tensor.shape}, got {grad.shape}"
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def differentiate(tensor: Tensor, grad: NDArrayLike | None = None) -> List[Tensor]:
|
|
34
|
+
topo: List[Tensor] = []
|
|
35
|
+
visited: Set[Tensor] = set()
|
|
36
|
+
|
|
37
|
+
def build(t: Tensor):
|
|
38
|
+
if t in visited:
|
|
39
|
+
return
|
|
40
|
+
visited.add(t)
|
|
41
|
+
|
|
42
|
+
for prev in t.prev:
|
|
43
|
+
if isinstance(prev, Tensor):
|
|
44
|
+
build(prev)
|
|
45
|
+
topo.append(t)
|
|
46
|
+
|
|
47
|
+
build(tensor)
|
|
48
|
+
|
|
49
|
+
if tensor.grad is None:
|
|
50
|
+
if grad is not None:
|
|
51
|
+
_grad_check(grad, tensor)
|
|
52
|
+
tensor.grad = grad
|
|
53
|
+
else:
|
|
54
|
+
if tensor.device == "gpu":
|
|
55
|
+
if cp is None:
|
|
56
|
+
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
57
|
+
tensor.grad = cp.ones(tensor.shape, tensor.dtype)
|
|
58
|
+
elif tensor.device == "cpu":
|
|
59
|
+
tensor.grad = np.ones(tensor.shape, tensor.dtype)
|
|
60
|
+
else:
|
|
61
|
+
if grad is not None:
|
|
62
|
+
_grad_check(grad, tensor)
|
|
63
|
+
tensor.grad += grad
|
|
64
|
+
|
|
65
|
+
for t in reversed(topo):
|
|
66
|
+
if t.backward is None:
|
|
67
|
+
if not t.requires_grad:
|
|
68
|
+
continue
|
|
69
|
+
if len(t.prev) > 0:
|
|
70
|
+
raise GradientComputationError(
|
|
71
|
+
f"Tensor ({t})'s backward function is None."
|
|
72
|
+
)
|
|
73
|
+
continue
|
|
74
|
+
t.backward()
|
|
75
|
+
return topo
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: depthtensor
|
|
3
|
+
Version: 2.4.0
|
|
4
|
+
Summary: A Hardware-Accelerated Tensor Computation and Autograd Engine
|
|
5
|
+
Author-email: Le Hong Ha <lehahong1310@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 Le Hong Ha
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
Project-URL: Homepage, https://github.com/l-h-ha/DepthTensor
|
|
28
|
+
Project-URL: Bug Tracker, https://github.com/l-h-ha/DepthTensor/issues
|
|
29
|
+
Classifier: Programming Language :: Python :: 3
|
|
30
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
31
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
32
|
+
Classifier: Operating System :: OS Independent
|
|
33
|
+
Requires-Python: >=3.8
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
License-File: LICENSE
|
|
36
|
+
Requires-Dist: numpy
|
|
37
|
+
Provides-Extra: gpu
|
|
38
|
+
Requires-Dist: cupy; extra == "gpu"
|
|
39
|
+
Dynamic: license-file
|
|
40
|
+
|
|
41
|
+
# DepthTensor: A Hardware-Accelerated Tensor Computation and Autograd Engine
|
|
42
|
+
|
|
43
|
+
**DepthTensor** is a light-weight, high-performance library for reverse-mode automatic differentiation (AD). It is useful in building the mathematical foundation of deep learning frameworks, with the use of a `Tensor` object which dynamically builds computational graphs and computes gradients using **Vector-Jacobian Products (VJP)**, generalized for tensors of abritrary rank.
|
|
44
|
+
|
|
45
|
+
> **Note**: This is the core autograd and tensor computation engine. For the full deep learning framework, refer to [DepthML](https://github.com/l-h-ha/DepthML).
|
|
46
|
+
|
|
47
|
+
## 1. Mathematical Foundation
|
|
48
|
+
|
|
49
|
+
The goal is to compute the gradient of scalar field $L$ with respect to an input tensor $X$, denoted as the adjoint $\bar{X}$.
|
|
50
|
+
|
|
51
|
+
### 1.1. Generalized VJPs via Tensor Contractions
|
|
52
|
+
|
|
53
|
+
Let $X$ be a tensor of shape $\mathcal{I} = (i_1, \dots, i_n)$ and $Y = f(X)$ be a tensor of shape $\mathcal{J} = (j_1, \dots, j_m)$.
|
|
54
|
+
|
|
55
|
+
Mathematically, the Jacobian is the tensor of rank $n + m$ which contains all the partial derivatives $\frac{\partial Y_{\mathcal{J}}}{\partial X_{\mathcal{I}}}$. DepthTensor does not compute this object, but, rather, it computes the contraction of the incoming adjoint $\bar{Y}$ with the local derivative:
|
|
56
|
+
|
|
57
|
+
$$
|
|
58
|
+
\bar{X}_{i_1 \dots i_n} = \sum_{j_1 \dots j_m} \bar{Y}_{j_1 \dots j_m} \frac{\partial Y_{j_1 \dots j_m}}{\partial X_{i_1 \dots i_n}}
|
|
59
|
+
$$
|
|
60
|
+
|
|
61
|
+
If X and Y are scalars, indices vanish, and this reduces to:
|
|
62
|
+
|
|
63
|
+
$$
|
|
64
|
+
\bar{x} = \bar{y} \cdot f'(x)
|
|
65
|
+
$$
|
|
66
|
+
|
|
67
|
+
If X and Y are matrices, this reduces to:
|
|
68
|
+
|
|
69
|
+
$$
|
|
70
|
+
\bar{X} = \bar{Y} W^{T}
|
|
71
|
+
$$
|
|
72
|
+
|
|
73
|
+
### 1.2. Graph Topology and Gradient Accumulation
|
|
74
|
+
|
|
75
|
+
Gradients are accumulated using a Depth-First Search (DFS) topological sort, which ensures that for any node $X$ with multiple gradient flow streams ${Y^{(1)}, \dots, Y^{k}}$, the gradient sum is the sum of contractions:
|
|
76
|
+
|
|
77
|
+
$$
|
|
78
|
+
\bar{X} = \sum_{k} \mathrm{VJP}(Y^{(k)}, X)
|
|
79
|
+
$$
|
|
80
|
+
|
|
81
|
+
All gradients are aggregated from all gradient downstreams.
|
|
82
|
+
|
|
83
|
+
## 2. Architecture and System Design
|
|
84
|
+
|
|
85
|
+
### 2.1. The differentiable Primitive (`Tensor`)
|
|
86
|
+
|
|
87
|
+
The `Tensor` class acts like a node in the Directed Acyclic Graph (DAG).
|
|
88
|
+
|
|
89
|
+
Operations are automatically dispatched to backend computers, which consists of `numpy` (CPU) and `cupy` (GPU).
|
|
90
|
+
|
|
91
|
+
Computational graphs are built dynamically at runtime.
|
|
92
|
+
|
|
93
|
+
## 3. Empirical Validation
|
|
94
|
+
|
|
95
|
+
We will verify the tensor engine by minimizing the Rosenbrock function, which is a non-convex optimization benchmark:
|
|
96
|
+
|
|
97
|
+
$$
|
|
98
|
+
f(x, y) = (a - x)^2 + b(y - x^2)^2
|
|
99
|
+
$$
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
import depthtensor as dt
|
|
103
|
+
|
|
104
|
+
# Initialize tensors
|
|
105
|
+
x = dt.Tensor([1.2], device="gpu", requires_grad=True)
|
|
106
|
+
y = dt.Tensor([1.2], device="gpu", requires_grad=True)
|
|
107
|
+
|
|
108
|
+
a, b = dt.Tensor([1], device="gpu"), dt.Tensor(
|
|
109
|
+
[100], device="gpu"
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
# 2. Optimization Loop
|
|
113
|
+
lr = 0.001
|
|
114
|
+
for i in range(500):
|
|
115
|
+
# Rosenbrock: f(x,y) = (a-x)^2 + b(y-x^2)^2
|
|
116
|
+
loss = (a - x) ** 2 + b * (y - x**2) ** 2
|
|
117
|
+
|
|
118
|
+
# Backpward pass
|
|
119
|
+
dt.differentiate(loss)
|
|
120
|
+
|
|
121
|
+
# Gradient Descent
|
|
122
|
+
x.data -= lr * x.grad # type: ignore
|
|
123
|
+
y.data -= lr * y.grad # type: ignore
|
|
124
|
+
|
|
125
|
+
# Zero grads
|
|
126
|
+
x.zeros_grad()
|
|
127
|
+
y.zeros_grad()
|
|
128
|
+
|
|
129
|
+
if i % 10 == 0:
|
|
130
|
+
print(loss.item())
|
|
131
|
+
|
|
132
|
+
print(f"Converged: ({x.data}, {y.data})")
|
|
133
|
+
# Target: (1.0, 1.0)
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## 4. Installation
|
|
137
|
+
|
|
138
|
+
Requirements:
|
|
139
|
+
- `numpy`
|
|
140
|
+
- `cupy` (optional: for NVIDIA GPU acceleration)
|
|
141
|
+
|
|
142
|
+
### 4.1. Bash
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
git clone https://github.com/YourUsername/DepthTensor.git
|
|
146
|
+
pip install -e .
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### 4.2. Pip
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
pip install depthtensor
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Author: Le Hong Ha
|
|
@@ -5,11 +5,6 @@ src/DepthTensor/__init__.py
|
|
|
5
5
|
src/DepthTensor/autodiff.py
|
|
6
6
|
src/DepthTensor/tensor.py
|
|
7
7
|
src/DepthTensor/typing.py
|
|
8
|
-
src/DepthTensor.egg-info/PKG-INFO
|
|
9
|
-
src/DepthTensor.egg-info/SOURCES.txt
|
|
10
|
-
src/DepthTensor.egg-info/dependency_links.txt
|
|
11
|
-
src/DepthTensor.egg-info/requires.txt
|
|
12
|
-
src/DepthTensor.egg-info/top_level.txt
|
|
13
8
|
src/DepthTensor/_core/__init__.py
|
|
14
9
|
src/DepthTensor/_core/exceptions.py
|
|
15
10
|
src/DepthTensor/_core/utils.py
|
|
@@ -20,4 +15,9 @@ src/DepthTensor/_core/ops/diff.py
|
|
|
20
15
|
src/DepthTensor/_core/ops/elementwise.py
|
|
21
16
|
src/DepthTensor/_core/ops/reduction.py
|
|
22
17
|
src/DepthTensor/_core/random/__init__.py
|
|
23
|
-
src/DepthTensor/_core/random/generator.py
|
|
18
|
+
src/DepthTensor/_core/random/generator.py
|
|
19
|
+
src/depthtensor.egg-info/PKG-INFO
|
|
20
|
+
src/depthtensor.egg-info/SOURCES.txt
|
|
21
|
+
src/depthtensor.egg-info/dependency_links.txt
|
|
22
|
+
src/depthtensor.egg-info/requires.txt
|
|
23
|
+
src/depthtensor.egg-info/top_level.txt
|
depthtensor-2.3.0/PKG-INFO
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: DepthTensor
|
|
3
|
-
Version: 2.3.0
|
|
4
|
-
Description-Content-Type: text/markdown
|
|
5
|
-
License-File: LICENSE
|
|
6
|
-
Requires-Dist: numpy
|
|
7
|
-
Provides-Extra: gpu
|
|
8
|
-
Requires-Dist: cupy-cuda12x; extra == "gpu"
|
|
9
|
-
Dynamic: license-file
|
|
10
|
-
|
|
11
|
-
# DepthTensor
|
|
12
|
-
This is a NumPy/CuPy wrapper module designed for tensor computations. DepthTensor was created to operate in tandem with another library called Depth, a deep-learning library.
|
|
13
|
-
|
|
14
|
-
# Functionalities
|
|
15
|
-
|
|
16
|
-
* NumPy functionalities (N-dimensional array, broadcasting, etc.)
|
|
17
|
-
|
|
18
|
-
* Auto-differentiation
|
|
19
|
-
|
|
20
|
-
# Installation
|
|
21
|
-
|
|
22
|
-
```bash
|
|
23
|
-
pip install DepthTensor
|
|
24
|
-
```
|
depthtensor-2.3.0/README.md
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
# DepthTensor
|
|
2
|
-
This is a NumPy/CuPy wrapper module designed for tensor computations. DepthTensor was created to operate in tandem with another library called Depth, a deep-learning library.
|
|
3
|
-
|
|
4
|
-
# Functionalities
|
|
5
|
-
|
|
6
|
-
* NumPy functionalities (N-dimensional array, broadcasting, etc.)
|
|
7
|
-
|
|
8
|
-
* Auto-differentiation
|
|
9
|
-
|
|
10
|
-
# Installation
|
|
11
|
-
|
|
12
|
-
```bash
|
|
13
|
-
pip install DepthTensor
|
|
14
|
-
```
|
depthtensor-2.3.0/pyproject.toml
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
[build-system]
|
|
2
|
-
requires = ["setuptools>=61.0.0"]
|
|
3
|
-
build-backend = "setuptools.build_meta"
|
|
4
|
-
|
|
5
|
-
[project]
|
|
6
|
-
name = "DepthTensor"
|
|
7
|
-
version = "2.3.0"
|
|
8
|
-
readme = "README.md"
|
|
9
|
-
dependencies = [
|
|
10
|
-
"numpy"
|
|
11
|
-
]
|
|
12
|
-
|
|
13
|
-
[project.optional-dependencies]
|
|
14
|
-
gpu = [
|
|
15
|
-
"cupy-cuda12x"
|
|
16
|
-
]
|
|
17
|
-
|
|
18
|
-
[tool.setuptools.packages.find]
|
|
19
|
-
where = ["src"]
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
from typing import List, Set
|
|
2
|
-
|
|
3
|
-
from . import Tensor
|
|
4
|
-
from ._core.exceptions import (
|
|
5
|
-
GradientComputationError,
|
|
6
|
-
GRADIENT_COMPUTATION_ERROR,
|
|
7
|
-
CuPyNotFound,
|
|
8
|
-
CUPY_NOT_FOUND_MSG,
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
import numpy as np
|
|
12
|
-
|
|
13
|
-
try:
|
|
14
|
-
import cupy as cp
|
|
15
|
-
except (ModuleNotFoundError, ImportError):
|
|
16
|
-
cp = None
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
def differentiate(tensor: Tensor) -> List[Tensor]:
|
|
20
|
-
topo: List[Tensor] = []
|
|
21
|
-
visited: Set[Tensor] = set()
|
|
22
|
-
|
|
23
|
-
def build(t: Tensor):
|
|
24
|
-
if t in visited:
|
|
25
|
-
return
|
|
26
|
-
visited.add(t)
|
|
27
|
-
|
|
28
|
-
for prev in t.prev:
|
|
29
|
-
build(prev)
|
|
30
|
-
topo.append(t)
|
|
31
|
-
|
|
32
|
-
build(tensor)
|
|
33
|
-
|
|
34
|
-
if tensor.device == "gpu":
|
|
35
|
-
if cp is None:
|
|
36
|
-
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
37
|
-
tensor.grad = cp.ones(tensor.shape, tensor.dtype)
|
|
38
|
-
elif tensor.device == "cpu":
|
|
39
|
-
tensor.grad = np.ones(tensor.shape, tensor.dtype)
|
|
40
|
-
|
|
41
|
-
for t in reversed(topo):
|
|
42
|
-
if t.backward is None:
|
|
43
|
-
if not t.requires_grad:
|
|
44
|
-
raise GradientComputationError(
|
|
45
|
-
f"Attempted to propagate backward off of a tensor ({t}), which is not differentiable."
|
|
46
|
-
)
|
|
47
|
-
if len(t.prev) > 0:
|
|
48
|
-
raise GradientComputationError(
|
|
49
|
-
f"Tensor ({t})'s backward function is None."
|
|
50
|
-
)
|
|
51
|
-
continue
|
|
52
|
-
t.backward()
|
|
53
|
-
return topo
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: DepthTensor
|
|
3
|
-
Version: 2.3.0
|
|
4
|
-
Description-Content-Type: text/markdown
|
|
5
|
-
License-File: LICENSE
|
|
6
|
-
Requires-Dist: numpy
|
|
7
|
-
Provides-Extra: gpu
|
|
8
|
-
Requires-Dist: cupy-cuda12x; extra == "gpu"
|
|
9
|
-
Dynamic: license-file
|
|
10
|
-
|
|
11
|
-
# DepthTensor
|
|
12
|
-
This is a NumPy/CuPy wrapper module designed for tensor computations. DepthTensor was created to operate in tandem with another library called Depth, a deep-learning library.
|
|
13
|
-
|
|
14
|
-
# Functionalities
|
|
15
|
-
|
|
16
|
-
* NumPy functionalities (N-dimensional array, broadcasting, etc.)
|
|
17
|
-
|
|
18
|
-
* Auto-differentiation
|
|
19
|
-
|
|
20
|
-
# Installation
|
|
21
|
-
|
|
22
|
-
```bash
|
|
23
|
-
pip install DepthTensor
|
|
24
|
-
```
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|