DepthTensor 0.0.2__tar.gz → 1.1.2__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.
Files changed (36) hide show
  1. {depthtensor-0.0.2 → depthtensor-1.1.2}/LICENSE +20 -20
  2. {depthtensor-0.0.2 → depthtensor-1.1.2}/PKG-INFO +26 -20
  3. {depthtensor-0.0.2 → depthtensor-1.1.2}/README.md +14 -8
  4. {depthtensor-0.0.2 → depthtensor-1.1.2}/pyproject.toml +21 -21
  5. {depthtensor-0.0.2 → depthtensor-1.1.2}/setup.cfg +4 -4
  6. depthtensor-1.1.2/src/DepthTensor/__init__.py +6 -0
  7. depthtensor-1.1.2/src/DepthTensor/_core/__init__.py +4 -0
  8. depthtensor-1.1.2/src/DepthTensor/_core/exceptions.py +12 -0
  9. depthtensor-1.1.2/src/DepthTensor/_core/ops/__init__.py +6 -0
  10. depthtensor-1.1.2/src/DepthTensor/_core/ops/comparison.py +225 -0
  11. depthtensor-1.1.2/src/DepthTensor/_core/ops/creation.py +77 -0
  12. depthtensor-1.1.2/src/DepthTensor/_core/ops/diff.py +113 -0
  13. depthtensor-1.1.2/src/DepthTensor/_core/ops/elementwise.py +356 -0
  14. depthtensor-1.1.2/src/DepthTensor/_core/ops/reduction.py +96 -0
  15. depthtensor-1.1.2/src/DepthTensor/_core/random/__init__.py +1 -0
  16. depthtensor-1.1.2/src/DepthTensor/_core/random/generator.py +117 -0
  17. {depthtensor-0.0.2 → depthtensor-1.1.2}/src/DepthTensor/_core/utils.py +91 -63
  18. depthtensor-1.1.2/src/DepthTensor/autodiff.py +45 -0
  19. depthtensor-1.1.2/src/DepthTensor/tensor.py +675 -0
  20. depthtensor-1.1.2/src/DepthTensor/typing.py +40 -0
  21. {depthtensor-0.0.2 → depthtensor-1.1.2}/src/DepthTensor.egg-info/PKG-INFO +26 -20
  22. depthtensor-1.1.2/src/DepthTensor.egg-info/SOURCES.txt +25 -0
  23. depthtensor-1.1.2/src/example/example.py +7 -0
  24. {depthtensor-0.0.2 → depthtensor-1.1.2}/tests/test_tensor.py +48 -48
  25. depthtensor-0.0.2/src/DepthTensor/__init__.py +0 -6
  26. depthtensor-0.0.2/src/DepthTensor/_core/__init__.py +0 -2
  27. depthtensor-0.0.2/src/DepthTensor/_core/api.py +0 -198
  28. depthtensor-0.0.2/src/DepthTensor/autodiff.py +0 -46
  29. depthtensor-0.0.2/src/DepthTensor/cpu.py +0 -133
  30. depthtensor-0.0.2/src/DepthTensor/gpu.py +0 -144
  31. depthtensor-0.0.2/src/DepthTensor/types.py +0 -25
  32. depthtensor-0.0.2/src/DepthTensor.egg-info/SOURCES.txt +0 -18
  33. depthtensor-0.0.2/src/example/example.py +0 -7
  34. {depthtensor-0.0.2 → depthtensor-1.1.2}/src/DepthTensor.egg-info/dependency_links.txt +0 -0
  35. {depthtensor-0.0.2 → depthtensor-1.1.2}/src/DepthTensor.egg-info/requires.txt +0 -0
  36. {depthtensor-0.0.2 → depthtensor-1.1.2}/src/DepthTensor.egg-info/top_level.txt +0 -0
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Lê Hồng Hà
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
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Lê Hồng Hà
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
21
  SOFTWARE.
@@ -1,20 +1,26 @@
1
- Metadata-Version: 2.4
2
- Name: DepthTensor
3
- Version: 0.0.2
4
- Description-Content-Type: text/markdown
5
- License-File: LICENSE
6
- Requires-Dist: numpy
7
- Provides-Extra: test
8
- Requires-Dist: pytest; extra == "test"
9
- Provides-Extra: gpu
10
- Requires-Dist: cupy-cuda12x; extra == "gpu"
11
- Dynamic: license-file
12
-
13
- # DepthTensor
14
- 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.
15
-
16
- # Functionalities
17
-
18
- * NumPy functionalities (N-dimensional array, broadcasting, etc.)
19
-
20
- * Auto-differentiation
1
+ Metadata-Version: 2.4
2
+ Name: DepthTensor
3
+ Version: 1.1.2
4
+ Description-Content-Type: text/markdown
5
+ License-File: LICENSE
6
+ Requires-Dist: numpy
7
+ Provides-Extra: test
8
+ Requires-Dist: pytest; extra == "test"
9
+ Provides-Extra: gpu
10
+ Requires-Dist: cupy-cuda12x; extra == "gpu"
11
+ Dynamic: license-file
12
+
13
+ # DepthTensor
14
+ 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.
15
+
16
+ # Functionalities
17
+
18
+ * NumPy functionalities (N-dimensional array, broadcasting, etc.)
19
+
20
+ * Auto-differentiation
21
+
22
+ # Installation
23
+
24
+ ```bash
25
+ pip install DepthTensor
26
+ ```
@@ -1,8 +1,14 @@
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
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
+ ```
@@ -1,22 +1,22 @@
1
- [build-system]
2
- requires = ["setuptools>=61.0.0"]
3
- build-backend = "setuptools.build_meta"
4
-
5
- [project]
6
- name = "DepthTensor"
7
- version = "0.0.2"
8
- readme = "README.md"
9
- dependencies = [
10
- "numpy"
11
- ]
12
-
13
- [project.optional-dependencies]
14
- test = [
15
- "pytest"
16
- ]
17
- gpu = [
18
- "cupy-cuda12x"
19
- ]
20
-
21
- [tool.setuptools.packages.find]
1
+ [build-system]
2
+ requires = ["setuptools>=61.0.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "DepthTensor"
7
+ version = "1.1.2"
8
+ readme = "README.md"
9
+ dependencies = [
10
+ "numpy"
11
+ ]
12
+
13
+ [project.optional-dependencies]
14
+ test = [
15
+ "pytest"
16
+ ]
17
+ gpu = [
18
+ "cupy-cuda12x"
19
+ ]
20
+
21
+ [tool.setuptools.packages.find]
22
22
  where = ["src"]
@@ -1,4 +1,4 @@
1
- [egg_info]
2
- tag_build =
3
- tag_date = 0
4
-
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,6 @@
1
+ from .tensor import Tensor
2
+ from .typing import *
3
+ from ._core import random
4
+ from .autodiff import differentiate
5
+
6
+ __version__ = "1.1.2"
@@ -0,0 +1,4 @@
1
+ from .utils import *
2
+ from .exceptions import *
3
+
4
+ from .ops import *
@@ -0,0 +1,12 @@
1
+ CUPY_NOT_FOUND_MSG = "Module CuPy not found or installed. Please install CuPy."
2
+ DEVICE_MISMATCH_MSG = "There is a mismatch in device between two objects."
3
+ GRADIENT_COMPUTATION_ERROR = "An error in gradient computation has occurred."
4
+
5
+ class CuPyNotFound(RuntimeError):
6
+ ...
7
+
8
+ class DeviceMismatch(RuntimeError):
9
+ ...
10
+
11
+ class GradientComputationError(RuntimeError):
12
+ ...
@@ -0,0 +1,6 @@
1
+ from .creation import *
2
+ from .elementwise import *
3
+ from .comparison import *
4
+ from .reduction import *
5
+
6
+ from .diff import *
@@ -0,0 +1,225 @@
1
+ from typing import (
2
+ Union,
3
+ Optional,
4
+ Tuple,
5
+ overload,
6
+ Callable
7
+ )
8
+
9
+ from ...typing import (
10
+ ArrayLike,
11
+ TensorLike,
12
+ DeviceLike,
13
+ ArrayLikeBool,
14
+ Casting,
15
+ Order
16
+ )
17
+
18
+ from ..exceptions import (
19
+ CuPyNotFound, CUPY_NOT_FOUND_MSG,
20
+ DeviceMismatch, DEVICE_MISMATCH_MSG
21
+ )
22
+
23
+ import numpy as np
24
+ try:
25
+ import cupy as cp
26
+ except (ImportError, ModuleNotFoundError):
27
+ cp = None
28
+
29
+ ###
30
+ ###
31
+ ###
32
+
33
+ @overload
34
+ def where(
35
+ condition: Union[ArrayLike, TensorLike],
36
+ /
37
+ ) -> Tuple[TensorLike, ...]: ...
38
+
39
+ @overload
40
+ def where(
41
+ condition: Union[ArrayLike, TensorLike],
42
+ x: Optional[Union[ArrayLike, TensorLike]],
43
+ y: Optional[Union[ArrayLike, TensorLike]],
44
+ /,
45
+ *,
46
+ device: DeviceLike = "cpu"
47
+ ) -> TensorLike: ...
48
+
49
+ def where(
50
+ condition: Union[ArrayLike, TensorLike],
51
+ x: Optional[Union[ArrayLike, TensorLike]] = None,
52
+ y: Optional[Union[ArrayLike, TensorLike]] = None,
53
+ /,
54
+ *,
55
+ device: DeviceLike = "cpu"
56
+ ) -> Union[Tuple[TensorLike, ...], TensorLike]:
57
+ from ...tensor import Tensor
58
+ #* One parameter overload
59
+ if x is None and y is None:
60
+ if isinstance(condition, TensorLike):
61
+ data = condition.data
62
+ else:
63
+ if device == "cpu":
64
+ data = np.asarray(condition)
65
+ else:
66
+ if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
67
+ data = cp.asarray(condition)
68
+ if device == "cpu":
69
+ result = np.where(data)
70
+ else:
71
+ if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
72
+ result = cp.where(data)
73
+ return tuple([Tensor(array, device=device) for array in result])
74
+ elif x is not None and y is not None:
75
+ if isinstance(condition, TensorLike):
76
+ data = condition.data
77
+ else:
78
+ if device == "cpu":
79
+ data = np.asarray(condition)
80
+ else:
81
+ if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
82
+ data = cp.asarray(condition)
83
+ if isinstance(x, TensorLike):
84
+ x_data = x.data
85
+ else:
86
+ if device == "cpu":
87
+ x_data = np.asarray(x)
88
+ else:
89
+ if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
90
+ x_data = cp.asarray(x)
91
+ if isinstance(y, TensorLike):
92
+ y_data = y.data
93
+ else:
94
+ if device == "cpu":
95
+ y_data = np.asarray(y)
96
+ else:
97
+ if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
98
+ y_data = cp.asarray(y)
99
+ if device == "cpu":
100
+ result = np.where(data, x_data, y_data)
101
+ else:
102
+ if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
103
+ result = cp.where(data, x_data, y_data)
104
+ return Tensor(result, device=device)
105
+ else:
106
+ raise ValueError("Both x and y parameters must be given.")
107
+
108
+ ###
109
+ ###
110
+ ###
111
+
112
+ def comparison_ufunc(
113
+ x1: TensorLike,
114
+ x2: TensorLike,
115
+ /,
116
+ out: Optional[ArrayLikeBool] = None,
117
+ *,
118
+ func_name: str,
119
+ where: Union[bool, ArrayLikeBool] = True,
120
+ casting: Casting = 'same_kind',
121
+ order: Order = 'K',
122
+ dtype: None = None,
123
+ subok: bool = True
124
+ ) -> TensorLike:
125
+ from ...tensor import Tensor
126
+ if not x1.is_device(x2.device): raise DeviceMismatch(DEVICE_MISMATCH_MSG)
127
+ if x1.is_cpu():
128
+ y = getattr(np, func_name)(x1.data, x2.data, out=out, dtype=dtype, where=where, casting=casting, order=order, subok=subok)
129
+ else:
130
+ if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
131
+ y = getattr(cp, func_name)(x1.data, x2.data, out=out, dtype=dtype, casting=casting)
132
+ return Tensor(y, device=x1.device)
133
+
134
+ def equal(
135
+ x1: TensorLike,
136
+ x2: TensorLike,
137
+ /,
138
+ out: Optional[ArrayLikeBool] = None,
139
+ *,
140
+ where: Union[bool, ArrayLikeBool] = True,
141
+ casting: Casting = 'same_kind',
142
+ order: Order = 'K',
143
+ dtype: None = None,
144
+ subok: bool = True
145
+ ) -> TensorLike:
146
+ return comparison_ufunc(x1, x2, out=out, func_name="equal", where=where, casting=casting, order=order, dtype=dtype, subok=subok)
147
+
148
+ def not_equal(
149
+ x1: TensorLike,
150
+ x2: TensorLike,
151
+ /,
152
+ out: Optional[ArrayLikeBool] = None,
153
+ *,
154
+ where: Union[bool, ArrayLikeBool] = True,
155
+ casting: Casting = 'same_kind',
156
+ order: Order = 'K',
157
+ dtype: None = None,
158
+ subok: bool = True
159
+ ) -> TensorLike:
160
+ return comparison_ufunc(x1, x2, out=out, func_name="not_equal", where=where, casting=casting, order=order, dtype=dtype, subok=subok)
161
+
162
+ def greater(
163
+ x1: TensorLike,
164
+ x2: TensorLike,
165
+ /,
166
+ out: Optional[ArrayLikeBool] = None,
167
+ *,
168
+ where: Union[bool, ArrayLikeBool] = True,
169
+ casting: Casting = 'same_kind',
170
+ order: Order = 'K',
171
+ dtype: None = None,
172
+ subok: bool = True
173
+ ) -> TensorLike:
174
+ return comparison_ufunc(x1, x2, out=out, func_name="greater", where=where, casting=casting, order=order, dtype=dtype, subok=subok)
175
+
176
+ def greater_equal(
177
+ x1: TensorLike,
178
+ x2: TensorLike,
179
+ /,
180
+ out: Optional[ArrayLikeBool] = None,
181
+ *,
182
+ where: Union[bool, ArrayLikeBool] = True,
183
+ casting: Casting = 'same_kind',
184
+ order: Order = 'K',
185
+ dtype: None = None,
186
+ subok: bool = True
187
+ ) -> TensorLike:
188
+ return comparison_ufunc(x1, x2, out=out, func_name="greater_equal", where=where, casting=casting, order=order, dtype=dtype, subok=subok)
189
+
190
+ def less(
191
+ x1: TensorLike,
192
+ x2: TensorLike,
193
+ /,
194
+ out: Optional[ArrayLikeBool] = None,
195
+ *,
196
+ where: Union[bool, ArrayLikeBool] = True,
197
+ casting: Casting = 'same_kind',
198
+ order: Order = 'K',
199
+ dtype: None = None,
200
+ subok: bool = True
201
+ ) -> TensorLike:
202
+ return comparison_ufunc(x1, x2, out=out, func_name="less", where=where, casting=casting, order=order, dtype=dtype, subok=subok)
203
+
204
+ def less_equal(
205
+ x1: TensorLike,
206
+ x2: TensorLike,
207
+ /,
208
+ out: Optional[ArrayLikeBool] = None,
209
+ *,
210
+ where: Union[bool, ArrayLikeBool] = True,
211
+ casting: Casting = 'same_kind',
212
+ order: Order = 'K',
213
+ dtype: None = None,
214
+ subok: bool = True
215
+ ) -> TensorLike:
216
+ return comparison_ufunc(x1, x2, out=out, func_name="less_equal", where=where, casting=casting, order=order, dtype=dtype, subok=subok)
217
+
218
+ ###
219
+ ###
220
+ ###
221
+
222
+ __all__ = [
223
+ 'where',
224
+ 'equal', 'not_equal', 'greater', 'greater_equal', 'less', 'less_equal'
225
+ ]
@@ -0,0 +1,77 @@
1
+ from typing import (
2
+ Optional
3
+ )
4
+
5
+ from ...typing import (
6
+ TensorLike, DTypeLike, Order,
7
+ AxisShapeLike
8
+ )
9
+
10
+ from ..exceptions import (
11
+ CuPyNotFound, CUPY_NOT_FOUND_MSG
12
+ )
13
+
14
+ import numpy as np
15
+ try:
16
+ import cupy as cp
17
+ except (ImportError, ModuleNotFoundError):
18
+ cp = None
19
+
20
+ ###
21
+ ###
22
+ ###
23
+
24
+ def zeros_like(
25
+ a: TensorLike,
26
+ /,
27
+ *,
28
+ dtype: Optional[DTypeLike] = None,
29
+ order: Order = 'K',
30
+ subok: bool = True,
31
+ shape: Optional[AxisShapeLike] = None
32
+ ) -> TensorLike:
33
+ from ...tensor import Tensor
34
+ if a.device == "cpu":
35
+ return Tensor(
36
+ a.zeros_like(a, dtype=dtype, order=order, subok=subok, shape=shape),
37
+ device=a.device
38
+ )
39
+ else:
40
+ if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
41
+ return Tensor(
42
+ #! As of the time writing this, CuPy does not support subok.
43
+ cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape),
44
+ device=a.device
45
+ )
46
+
47
+ def ones_like(
48
+ a: TensorLike,
49
+ /,
50
+ *,
51
+ dtype: Optional[DTypeLike] = None,
52
+ order: Order = 'K',
53
+ subok: bool = True,
54
+ shape: Optional[AxisShapeLike] = None
55
+ ) -> TensorLike:
56
+ from ...tensor import Tensor
57
+ if a.is_cpu():
58
+ return Tensor(
59
+ np.ones_like(a, dtype=dtype, order=order, subok=subok, shape=shape),
60
+ device=a.device
61
+ )
62
+ else:
63
+ if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
64
+ return Tensor(
65
+ #! As of the time writing this, CuPy does not support subok.
66
+ cp.ones_like(a, dtype=dtype, order=order, subok=None, shape=shape),
67
+ device=a.device
68
+ )
69
+
70
+ ###
71
+ ###
72
+ ###
73
+
74
+ __all__ = [
75
+ 'zeros_like',
76
+ 'ones_like'
77
+ ]
@@ -0,0 +1,113 @@
1
+ from ...typing import (
2
+ TensorLike
3
+ )
4
+
5
+ from ..utils import sum_to_shape
6
+
7
+ ###
8
+ ### Arithmetics
9
+ ###
10
+
11
+ def add_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
12
+ if not result.requires_grad: return result
13
+ def backward() -> None:
14
+ if x1.requires_grad:
15
+ x1.grad += sum_to_shape(result.grad, x1.shape, x1.device)
16
+ if x2.requires_grad:
17
+ x2.grad += sum_to_shape(result.grad, x2.shape, x2.device)
18
+ result.backward = backward
19
+ return result
20
+
21
+ def subtract_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
22
+ if not result.requires_grad: return result
23
+ def backward() -> None:
24
+ if x1.requires_grad:
25
+ x1.grad += sum_to_shape(result.grad, x1.shape, x1.device)
26
+ if x2.requires_grad:
27
+ x2.grad += sum_to_shape(-result.grad, x2.shape, x2.device)
28
+ result.backward = backward
29
+ return result
30
+
31
+ def multiply_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
32
+ if not result.requires_grad: return result
33
+ def backward() -> None:
34
+ if x1.requires_grad:
35
+ x1.grad += sum_to_shape(result.grad * x2.data, x1.shape, x1.device)
36
+ if x2.requires_grad:
37
+ x2.grad += sum_to_shape(result.grad * x1.data, x2.shape, x2.device)
38
+ result.backward = backward
39
+ return result
40
+
41
+ def matmul_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
42
+ if not result.requires_grad: return result
43
+ def backward() -> None:
44
+ if x1.requires_grad:
45
+ x1.grad += sum_to_shape(result.grad @ x2.data.swapaxes(-2,-1), x1.shape, x1.device)
46
+ if x2.requires_grad:
47
+ x2.grad += sum_to_shape(x1.data.swapaxes(-2, -1) @ result.grad, x2.shape, x2.device)
48
+ result.backward = backward
49
+ return result
50
+
51
+ def divide_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
52
+ if not result.requires_grad: return result
53
+ def backward() -> None:
54
+ if x1.requires_grad:
55
+ x1.grad += sum_to_shape(result.grad / x2.data, x1.shape, x1.device)
56
+ if x2.requires_grad:
57
+ x2.grad += sum_to_shape(result.grad * (x1.data * -x2.data**-2), x2.shape, x2.device)
58
+ result.backward = backward
59
+ return result
60
+
61
+ def negative_diff(result: TensorLike, x: TensorLike) -> TensorLike:
62
+ if not result.requires_grad: return result
63
+ def backward() -> None:
64
+ if x.requires_grad:
65
+ x.grad += sum_to_shape(-result.grad, x.shape, x.device)
66
+ result.backward = backward
67
+ return result
68
+
69
+ def sign_diff(result: TensorLike, x: TensorLike) -> TensorLike:
70
+ if not result.requires_grad: return result
71
+ def backward() -> None:
72
+ if x.requires_grad:
73
+ x.grad += sum_to_shape(result.grad * 0, x.shape, x.device)
74
+ result.backward = backward
75
+ return result
76
+
77
+ def abs_diff(result: TensorLike, x: TensorLike) -> TensorLike:
78
+ if not result.requires_grad: return result
79
+ def backward() -> None:
80
+ if x.requires_grad:
81
+ # TODO: Handle jump discontinuity
82
+ x.grad += sum_to_shape(result.grad * result.data / x.data, x.shape, x.device)
83
+ result.backward = backward
84
+ return result
85
+
86
+ ###
87
+ ### Exponents/Logarithms
88
+ ###
89
+
90
+ def exp_diff(result: TensorLike, x: TensorLike) -> TensorLike:
91
+ if not result.requires_grad: return result
92
+ def backward() -> None:
93
+ if x.requires_grad:
94
+ x.grad += sum_to_shape(result.grad * result.data, x.shape, x.device)
95
+ result.backward = backward
96
+ return result
97
+
98
+ def sqrt_diff(result: TensorLike, x: TensorLike) -> TensorLike:
99
+ if not result.requires_grad: return result
100
+ def backward() -> None:
101
+ if x.requires_grad:
102
+ x.grad += sum_to_shape(result.grad * (0.5*result.data**(-0.5)), x.shape, x.device)
103
+ result.backward = backward
104
+ return result
105
+
106
+ ###
107
+ ###
108
+ ###
109
+
110
+ __all__ = [
111
+ 'add_diff', 'subtract_diff', 'multiply_diff', 'matmul_diff', 'divide_diff',
112
+ 'negative_diff', 'exp_diff', 'sqrt_diff', 'sign_diff', 'abs_diff'
113
+ ]