DepthTensor 2.2.1__tar.gz → 2.3.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.2.1/src/DepthTensor.egg-info → depthtensor-2.3.0}/PKG-INFO +1 -1
- {depthtensor-2.2.1 → depthtensor-2.3.0}/pyproject.toml +1 -1
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor/__init__.py +1 -1
- depthtensor-2.3.0/src/DepthTensor/_core/ops/diff.py +272 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor/_core/ops/elementwise.py +51 -1
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor/_core/utils.py +1 -1
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor/tensor.py +40 -8
- {depthtensor-2.2.1 → depthtensor-2.3.0/src/DepthTensor.egg-info}/PKG-INFO +1 -1
- depthtensor-2.2.1/src/DepthTensor/_core/ops/diff.py +0 -232
- {depthtensor-2.2.1 → depthtensor-2.3.0}/LICENSE +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/README.md +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/setup.cfg +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor/_core/__init__.py +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor/_core/exceptions.py +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor/_core/ops/__init__.py +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor/_core/ops/comparison.py +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor/_core/ops/creation.py +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor/_core/ops/reduction.py +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor/_core/random/__init__.py +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor/_core/random/generator.py +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor/autodiff.py +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor/typing.py +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor.egg-info/SOURCES.txt +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor.egg-info/dependency_links.txt +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor.egg-info/requires.txt +0 -0
- {depthtensor-2.2.1 → depthtensor-2.3.0}/src/DepthTensor.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
from typing import Callable
|
|
2
|
+
|
|
3
|
+
from ...typing import OperandLike, TensorLike, NDArrayLike, AxisLike
|
|
4
|
+
|
|
5
|
+
from ..utils import sum_to_shape, to_xp_array, get_two_operand_op_device
|
|
6
|
+
from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
import math
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
import cupy as cp
|
|
13
|
+
except (ImportError, ModuleNotFoundError):
|
|
14
|
+
cp = None
|
|
15
|
+
|
|
16
|
+
###
|
|
17
|
+
###
|
|
18
|
+
###
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def wrapper_2in_diff(
|
|
22
|
+
result: TensorLike,
|
|
23
|
+
x1: OperandLike,
|
|
24
|
+
x2: OperandLike,
|
|
25
|
+
callback_x1: Callable,
|
|
26
|
+
callback_x2: Callable,
|
|
27
|
+
) -> TensorLike:
|
|
28
|
+
if not result.requires_grad:
|
|
29
|
+
return result
|
|
30
|
+
from ...tensor import Tensor
|
|
31
|
+
|
|
32
|
+
def backward() -> None:
|
|
33
|
+
if result.grad is None:
|
|
34
|
+
result.zeros_grad()
|
|
35
|
+
|
|
36
|
+
result_grad: NDArrayLike = result.grad
|
|
37
|
+
device = get_two_operand_op_device(x1, x2, None)
|
|
38
|
+
x1_data, x2_data = to_xp_array(x1, device=device), to_xp_array(
|
|
39
|
+
x2, device=device
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
if isinstance(x1, Tensor) and x1.requires_grad:
|
|
43
|
+
if x1.grad is None:
|
|
44
|
+
x1.zeros_grad()
|
|
45
|
+
x1.grad += callback_x1(
|
|
46
|
+
result_grad, x1.shape, device, x1_data, x2_data
|
|
47
|
+
).astype(x1.dtype)
|
|
48
|
+
if isinstance(x2, Tensor) and x2.requires_grad:
|
|
49
|
+
if x2.grad is None:
|
|
50
|
+
x2.zeros_grad()
|
|
51
|
+
x2.grad += callback_x2(
|
|
52
|
+
result_grad, x2.shape, device, x1_data, x2_data
|
|
53
|
+
).astype(x2.dtype)
|
|
54
|
+
|
|
55
|
+
result.backward = backward
|
|
56
|
+
return result
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def wrapper_1in_diff(
|
|
60
|
+
result: TensorLike, x: OperandLike, callback_x1: Callable
|
|
61
|
+
) -> TensorLike:
|
|
62
|
+
if not result.requires_grad:
|
|
63
|
+
return result
|
|
64
|
+
from ...tensor import Tensor
|
|
65
|
+
|
|
66
|
+
def backward() -> None:
|
|
67
|
+
if result.grad is None:
|
|
68
|
+
result.zeros_grad()
|
|
69
|
+
|
|
70
|
+
result_grad: NDArrayLike = result.grad
|
|
71
|
+
_x = to_xp_array(x)
|
|
72
|
+
if isinstance(x, Tensor) and x.requires_grad:
|
|
73
|
+
if x.grad is None:
|
|
74
|
+
x.zeros_grad()
|
|
75
|
+
x.grad += callback_x1(result_grad, x.shape, x.device, _x)
|
|
76
|
+
|
|
77
|
+
result.backward = backward
|
|
78
|
+
return result
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
###
|
|
82
|
+
### Arithmetics
|
|
83
|
+
###
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def add_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
87
|
+
def callback_x1(result_grad, x1_shape, device, x1_data, x2_data):
|
|
88
|
+
return sum_to_shape(result_grad, x1_shape, device)
|
|
89
|
+
|
|
90
|
+
def callback_x2(result_grad, x2_shape, device, x1_data, x2_data):
|
|
91
|
+
return sum_to_shape(result_grad, x2_shape, device)
|
|
92
|
+
|
|
93
|
+
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def subtract_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
97
|
+
|
|
98
|
+
def callback_x1(result_grad, x1_shape, device, x1_data, x2_data):
|
|
99
|
+
return sum_to_shape(result_grad, x1_shape, device)
|
|
100
|
+
|
|
101
|
+
def callback_x2(result_grad, x2_shape, device, x1_data, x2_data):
|
|
102
|
+
return sum_to_shape(-result_grad, x2_shape, device)
|
|
103
|
+
|
|
104
|
+
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def multiply_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
108
|
+
|
|
109
|
+
def callback_x1(result_grad, x1_shape, device, x1_data, x2_data):
|
|
110
|
+
return sum_to_shape(result_grad * x2_data, x1_shape, device)
|
|
111
|
+
|
|
112
|
+
def callback_x2(result_grad, x2_shape, device, x1_data, x2_data):
|
|
113
|
+
return sum_to_shape(result_grad * x1_data, x2_shape, device)
|
|
114
|
+
|
|
115
|
+
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def matmul_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
119
|
+
|
|
120
|
+
def callback_x1(result_grad, x1_shape, device, x1_data, x2_data):
|
|
121
|
+
return sum_to_shape(result_grad @ x2_data.swapaxes(-2, -1), x1_shape, device)
|
|
122
|
+
|
|
123
|
+
def callback_x2(result_grad, x2_shape, device, x1_data, x2_data):
|
|
124
|
+
return sum_to_shape(x1_data.swapaxes(-2, -1) @ result_grad, x2_shape, device)
|
|
125
|
+
|
|
126
|
+
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def divide_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
130
|
+
|
|
131
|
+
def callback_x1(result_grad, x1_shape, device, x1_data, x2_data):
|
|
132
|
+
return sum_to_shape(result_grad / x2_data, x1_shape, device)
|
|
133
|
+
|
|
134
|
+
def callback_x2(result_grad, x2_shape, device, x1_data, x2_data):
|
|
135
|
+
return sum_to_shape(result_grad * (x1_data * -(x2_data**-2)), x2_shape, device)
|
|
136
|
+
|
|
137
|
+
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def power_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
141
|
+
|
|
142
|
+
def callback_x1(result_grad, x1_shape, device, x1_data, x2_data):
|
|
143
|
+
return sum_to_shape(
|
|
144
|
+
result_grad * x2_data * x1_data ** (x2_data - 1), x1_shape, device
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
def callback_x2(result_grad, x2_shape, device, x1_data, x2_data):
|
|
148
|
+
if device == "cpu":
|
|
149
|
+
return sum_to_shape(
|
|
150
|
+
result_grad * np.log(x1_data) * x1_data**x2_data, x2_shape, device
|
|
151
|
+
)
|
|
152
|
+
else:
|
|
153
|
+
if cp is None:
|
|
154
|
+
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
155
|
+
return sum_to_shape(
|
|
156
|
+
result_grad * cp.log(x1_data) * x1_data**x2_data, x2_shape, device
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def negative_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
163
|
+
|
|
164
|
+
def callback_x1(result_grad, x1_shape, device, x1_data):
|
|
165
|
+
return sum_to_shape(-result_grad, x1_shape, device)
|
|
166
|
+
|
|
167
|
+
return wrapper_1in_diff(result, x, callback_x1)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def sign_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
171
|
+
|
|
172
|
+
def callback_x1(result_grad, x1_shape, device, x1_data):
|
|
173
|
+
return sum_to_shape(result_grad * 0, x1_shape, device)
|
|
174
|
+
|
|
175
|
+
return wrapper_1in_diff(result, x, callback_x1)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def abs_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
179
|
+
|
|
180
|
+
def callback_x1(result_grad, x1_shape, device, x1_data):
|
|
181
|
+
return sum_to_shape(result_grad * result.data / x1_data, x1_shape, device)
|
|
182
|
+
|
|
183
|
+
return wrapper_1in_diff(result, x, callback_x1)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
###
|
|
187
|
+
### Exponents/Logarithms
|
|
188
|
+
###
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def exp_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
192
|
+
|
|
193
|
+
def callback_x1(result_grad, x1_shape, device, x1_data):
|
|
194
|
+
return sum_to_shape(result_grad * result.data, x1_shape, device)
|
|
195
|
+
|
|
196
|
+
return wrapper_1in_diff(result, x, callback_x1)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def sqrt_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
200
|
+
|
|
201
|
+
def callback_x1(result_grad, x1_shape, device, x1_data):
|
|
202
|
+
return sum_to_shape(
|
|
203
|
+
result_grad * (0.5 * result.data ** (-0.5)), x1_shape, device
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
return wrapper_1in_diff(result, x, callback_x1)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def log_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
210
|
+
|
|
211
|
+
def callback_x1(result_grad, x1_shape, device, x1_data):
|
|
212
|
+
return sum_to_shape(result_grad / x1_data, x1_shape, device)
|
|
213
|
+
|
|
214
|
+
return wrapper_1in_diff(result, x, callback_x1)
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def square_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
218
|
+
|
|
219
|
+
def callback_x1(result_grad, x1_shape, device, x1_data):
|
|
220
|
+
return sum_to_shape(result_grad * 2 * x1_data, x1_shape, device)
|
|
221
|
+
|
|
222
|
+
return wrapper_1in_diff(result, x, callback_x1)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
###
|
|
226
|
+
###
|
|
227
|
+
###
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def mean_diff(
|
|
231
|
+
result: TensorLike, x: TensorLike, axis: AxisLike, keepdims: bool
|
|
232
|
+
) -> TensorLike:
|
|
233
|
+
def callback_x1(result_grad, x1_shape, device, _x):
|
|
234
|
+
if device == "cpu":
|
|
235
|
+
xp = np
|
|
236
|
+
else:
|
|
237
|
+
if cp is None:
|
|
238
|
+
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
239
|
+
xp = cp
|
|
240
|
+
|
|
241
|
+
inp_size = math.prod(x1_shape)
|
|
242
|
+
grad_size = result_grad.size
|
|
243
|
+
N = inp_size / grad_size
|
|
244
|
+
grad = result_grad * (1.0 / N)
|
|
245
|
+
|
|
246
|
+
if not keepdims and axis is not None:
|
|
247
|
+
grad = xp.expand_dims(grad, axis)
|
|
248
|
+
return xp.broadcast_to(grad, x1_shape)
|
|
249
|
+
|
|
250
|
+
return wrapper_1in_diff(result, x, callback_x1)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
###
|
|
254
|
+
###
|
|
255
|
+
###
|
|
256
|
+
|
|
257
|
+
__all__ = [
|
|
258
|
+
"add_diff",
|
|
259
|
+
"subtract_diff",
|
|
260
|
+
"multiply_diff",
|
|
261
|
+
"matmul_diff",
|
|
262
|
+
"divide_diff",
|
|
263
|
+
"power_diff",
|
|
264
|
+
"negative_diff",
|
|
265
|
+
"sign_diff",
|
|
266
|
+
"abs_diff",
|
|
267
|
+
"exp_diff",
|
|
268
|
+
"sqrt_diff",
|
|
269
|
+
"log_diff",
|
|
270
|
+
"square_diff",
|
|
271
|
+
"mean_diff",
|
|
272
|
+
]
|
|
@@ -5,6 +5,7 @@ from ...typing import (
|
|
|
5
5
|
DTypeLike,
|
|
6
6
|
Casting,
|
|
7
7
|
Order,
|
|
8
|
+
AxisLike,
|
|
8
9
|
NDArrayLikeBool,
|
|
9
10
|
NDArrayLike,
|
|
10
11
|
OperandLike,
|
|
@@ -243,7 +244,7 @@ def wrapper_1in_1out(
|
|
|
243
244
|
from ...tensor import Tensor
|
|
244
245
|
|
|
245
246
|
if device is None:
|
|
246
|
-
device_op = get_device(
|
|
247
|
+
device_op = get_device(x)
|
|
247
248
|
else:
|
|
248
249
|
device_op = device
|
|
249
250
|
|
|
@@ -711,6 +712,54 @@ def square(
|
|
|
711
712
|
)
|
|
712
713
|
|
|
713
714
|
|
|
715
|
+
###
|
|
716
|
+
###
|
|
717
|
+
###
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
def mean(
|
|
721
|
+
x: OperandLike,
|
|
722
|
+
/,
|
|
723
|
+
axis: Optional[AxisLike] = None,
|
|
724
|
+
dtype: Optional[DTypeLike] = None,
|
|
725
|
+
out: Optional[NDArrayLike] = None,
|
|
726
|
+
keepdims: bool = False,
|
|
727
|
+
*,
|
|
728
|
+
device: Optional[DeviceLike] = None,
|
|
729
|
+
in_place: bool = False,
|
|
730
|
+
where: Union[bool, NDArrayLikeBool] = True,
|
|
731
|
+
) -> TensorLike:
|
|
732
|
+
from ...tensor import Tensor
|
|
733
|
+
|
|
734
|
+
if device is None:
|
|
735
|
+
device_op = get_device(x)
|
|
736
|
+
else:
|
|
737
|
+
device_op = device
|
|
738
|
+
|
|
739
|
+
a = to_xp_array(x, device=device_op)
|
|
740
|
+
if device_op == "cpu":
|
|
741
|
+
y = np.mean(
|
|
742
|
+
a,
|
|
743
|
+
out=out,
|
|
744
|
+
dtype=dtype,
|
|
745
|
+
where=where,
|
|
746
|
+
axis=axis,
|
|
747
|
+
keepdims=keepdims,
|
|
748
|
+
)
|
|
749
|
+
else:
|
|
750
|
+
if cp is None:
|
|
751
|
+
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
752
|
+
y = cp.mean(a, axis=axis, dtype=dtype, out=out, keepdims=keepdims)
|
|
753
|
+
|
|
754
|
+
requires_grad = False
|
|
755
|
+
if isinstance(x, Tensor):
|
|
756
|
+
if in_place:
|
|
757
|
+
x.data = y
|
|
758
|
+
return x
|
|
759
|
+
requires_grad = x.requires_grad
|
|
760
|
+
return Tensor(y, prev=(x,), requires_grad=requires_grad)
|
|
761
|
+
|
|
762
|
+
|
|
714
763
|
###
|
|
715
764
|
###
|
|
716
765
|
###
|
|
@@ -729,6 +778,7 @@ __all__ = [
|
|
|
729
778
|
"sqrt",
|
|
730
779
|
"log",
|
|
731
780
|
"square",
|
|
781
|
+
"mean",
|
|
732
782
|
"clip",
|
|
733
783
|
"create_2in_1out",
|
|
734
784
|
"create_1in_1out",
|
|
@@ -151,7 +151,7 @@ def get_device(a: OperandLike) -> DeviceLike:
|
|
|
151
151
|
elif isinstance(a, (int, float, list, tuple, bool)):
|
|
152
152
|
return "cpu"
|
|
153
153
|
else:
|
|
154
|
-
raise RuntimeError("Invalid argument type
|
|
154
|
+
raise RuntimeError(f"Invalid argument type: {type(a)}")
|
|
155
155
|
|
|
156
156
|
|
|
157
157
|
def get_complement_device(device: DeviceLike) -> DeviceLike:
|
|
@@ -26,6 +26,7 @@ from ._core import (
|
|
|
26
26
|
power,
|
|
27
27
|
clip,
|
|
28
28
|
abs,
|
|
29
|
+
mean,
|
|
29
30
|
# * diff (elementwise)
|
|
30
31
|
add_diff,
|
|
31
32
|
subtract_diff,
|
|
@@ -34,6 +35,7 @@ from ._core import (
|
|
|
34
35
|
divide_diff,
|
|
35
36
|
power_diff,
|
|
36
37
|
abs_diff,
|
|
38
|
+
mean_diff,
|
|
37
39
|
# * comparison
|
|
38
40
|
equal,
|
|
39
41
|
not_equal,
|
|
@@ -76,12 +78,13 @@ def _wrapper_2in_1out(
|
|
|
76
78
|
|
|
77
79
|
def _wrapper_1in_1out(
|
|
78
80
|
y: Tensor,
|
|
79
|
-
diff_func: Callable
|
|
81
|
+
diff_func: Callable,
|
|
80
82
|
x: OperandLike,
|
|
81
83
|
record_op: bool = True,
|
|
84
|
+
**kwargs: Any,
|
|
82
85
|
):
|
|
83
86
|
if record_op:
|
|
84
|
-
return diff_func(y, x)
|
|
87
|
+
return diff_func(y, x, **kwargs)
|
|
85
88
|
return y
|
|
86
89
|
|
|
87
90
|
|
|
@@ -311,6 +314,35 @@ class Tensor:
|
|
|
311
314
|
subok=subok,
|
|
312
315
|
)
|
|
313
316
|
|
|
317
|
+
def mean(
|
|
318
|
+
self,
|
|
319
|
+
/,
|
|
320
|
+
axis: Optional[AxisLike] = None,
|
|
321
|
+
dtype: Optional[DTypeLike] = None,
|
|
322
|
+
out: Optional[NDArrayLike] = None,
|
|
323
|
+
keepdims: bool = False,
|
|
324
|
+
*,
|
|
325
|
+
device: Optional[DeviceLike] = None,
|
|
326
|
+
in_place: bool = False,
|
|
327
|
+
where: Union[bool, NDArrayLikeBool] = True,
|
|
328
|
+
) -> Tensor:
|
|
329
|
+
return _wrapper_1in_1out(
|
|
330
|
+
mean(
|
|
331
|
+
self,
|
|
332
|
+
axis=axis,
|
|
333
|
+
dtype=dtype,
|
|
334
|
+
out=out,
|
|
335
|
+
keepdims=keepdims,
|
|
336
|
+
device=device,
|
|
337
|
+
in_place=in_place,
|
|
338
|
+
where=where,
|
|
339
|
+
),
|
|
340
|
+
mean_diff,
|
|
341
|
+
self,
|
|
342
|
+
axis=axis,
|
|
343
|
+
keepdims=keepdims,
|
|
344
|
+
)
|
|
345
|
+
|
|
314
346
|
###
|
|
315
347
|
### Reduction
|
|
316
348
|
###
|
|
@@ -407,7 +439,7 @@ class Tensor:
|
|
|
407
439
|
def __iadd__(self, t: OperandLike) -> Tensor:
|
|
408
440
|
if self.requires_grad:
|
|
409
441
|
raise RuntimeError(
|
|
410
|
-
"In-place operations are forbidden on differentiable tensors."
|
|
442
|
+
"In-place operations are (add) forbidden on differentiable tensors."
|
|
411
443
|
)
|
|
412
444
|
return add(self, t, in_place=True)
|
|
413
445
|
|
|
@@ -424,7 +456,7 @@ class Tensor:
|
|
|
424
456
|
def __isub__(self, t: OperandLike) -> Tensor:
|
|
425
457
|
if self.requires_grad:
|
|
426
458
|
raise RuntimeError(
|
|
427
|
-
"In-place operations are forbidden on differentiable tensors."
|
|
459
|
+
"In-place operations (sub) are forbidden on differentiable tensors."
|
|
428
460
|
)
|
|
429
461
|
return subtract(self, t, in_place=True)
|
|
430
462
|
|
|
@@ -441,7 +473,7 @@ class Tensor:
|
|
|
441
473
|
def __imul__(self, t: OperandLike) -> Tensor:
|
|
442
474
|
if self.requires_grad:
|
|
443
475
|
raise RuntimeError(
|
|
444
|
-
"In-place operations are forbidden on differentiable tensors."
|
|
476
|
+
"In-place operations are (mul) forbidden on differentiable tensors."
|
|
445
477
|
)
|
|
446
478
|
return multiply(self, t, in_place=True)
|
|
447
479
|
|
|
@@ -458,7 +490,7 @@ class Tensor:
|
|
|
458
490
|
def __imatmul__(self, t: OperandLike) -> Tensor:
|
|
459
491
|
if self.requires_grad:
|
|
460
492
|
raise RuntimeError(
|
|
461
|
-
"In-place operations are forbidden on differentiable tensors."
|
|
493
|
+
"In-place operations are (matmul) forbidden on differentiable tensors."
|
|
462
494
|
)
|
|
463
495
|
return matmul(self, t, in_place=True)
|
|
464
496
|
|
|
@@ -475,7 +507,7 @@ class Tensor:
|
|
|
475
507
|
def __itruediv__(self, t: OperandLike) -> Tensor:
|
|
476
508
|
if self.requires_grad:
|
|
477
509
|
raise RuntimeError(
|
|
478
|
-
"In-place operations are forbidden on differentiable tensors."
|
|
510
|
+
"In-place operations (div) are forbidden on differentiable tensors."
|
|
479
511
|
)
|
|
480
512
|
return divide(self, t, in_place=True)
|
|
481
513
|
|
|
@@ -487,7 +519,7 @@ class Tensor:
|
|
|
487
519
|
def __ipow__(self, t: OperandLike) -> Tensor:
|
|
488
520
|
if self.requires_grad:
|
|
489
521
|
raise RuntimeError(
|
|
490
|
-
"In-place operations are forbidden on differentiable tensors."
|
|
522
|
+
"In-place operations (pow) are forbidden on differentiable tensors."
|
|
491
523
|
)
|
|
492
524
|
return power(self, t, in_place=True)
|
|
493
525
|
|
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
from typing import Callable
|
|
2
|
-
|
|
3
|
-
from ...typing import OperandLike, TensorLike, NDArrayLike
|
|
4
|
-
|
|
5
|
-
from ..utils import sum_to_shape, to_xp_array, get_two_operand_op_device
|
|
6
|
-
from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
|
|
7
|
-
|
|
8
|
-
import numpy as np
|
|
9
|
-
|
|
10
|
-
try:
|
|
11
|
-
import cupy as cp
|
|
12
|
-
except (ImportError, ModuleNotFoundError):
|
|
13
|
-
cp = None
|
|
14
|
-
|
|
15
|
-
###
|
|
16
|
-
###
|
|
17
|
-
###
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def wrapper_2in_diff(
|
|
21
|
-
result: TensorLike,
|
|
22
|
-
x1: OperandLike,
|
|
23
|
-
x2: OperandLike,
|
|
24
|
-
callback_x1: Callable,
|
|
25
|
-
callback_x2: Callable,
|
|
26
|
-
) -> TensorLike:
|
|
27
|
-
if not result.requires_grad:
|
|
28
|
-
return result
|
|
29
|
-
from ...tensor import Tensor
|
|
30
|
-
|
|
31
|
-
def backward() -> None:
|
|
32
|
-
if result.grad is None:
|
|
33
|
-
result.zeros_grad()
|
|
34
|
-
|
|
35
|
-
result_grad: NDArrayLike = result.grad
|
|
36
|
-
device = get_two_operand_op_device(x1, x2, None)
|
|
37
|
-
_x1, _x2 = to_xp_array(x1, device=device), to_xp_array(x2, device=device)
|
|
38
|
-
|
|
39
|
-
if isinstance(x1, Tensor) and x1.requires_grad:
|
|
40
|
-
if x1.grad is None:
|
|
41
|
-
x1.zeros_grad()
|
|
42
|
-
x1.grad += callback_x1(result_grad, x1.shape, device, _x1, _x2).astype(
|
|
43
|
-
x1.dtype
|
|
44
|
-
)
|
|
45
|
-
if isinstance(x2, Tensor) and x2.requires_grad:
|
|
46
|
-
if x2.grad is None:
|
|
47
|
-
x2.zeros_grad()
|
|
48
|
-
x2.grad += callback_x2(result_grad, x2.shape, device, _x1, _x2).astype(
|
|
49
|
-
x2.dtype
|
|
50
|
-
)
|
|
51
|
-
|
|
52
|
-
result.backward = backward
|
|
53
|
-
return result
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
def wrapper_1in_diff(
|
|
57
|
-
result: TensorLike, x: OperandLike, callback_x1: Callable
|
|
58
|
-
) -> TensorLike:
|
|
59
|
-
if not result.requires_grad:
|
|
60
|
-
return result
|
|
61
|
-
from ...tensor import Tensor
|
|
62
|
-
|
|
63
|
-
def backward() -> None:
|
|
64
|
-
if result.grad is None:
|
|
65
|
-
result.zeros_grad()
|
|
66
|
-
|
|
67
|
-
result_grad: NDArrayLike = result.grad
|
|
68
|
-
_x = to_xp_array(x)
|
|
69
|
-
if isinstance(x, Tensor) and x.requires_grad:
|
|
70
|
-
if x.grad is None:
|
|
71
|
-
x.zeros_grad()
|
|
72
|
-
x.grad += callback_x1(result_grad, x.shape, x.device, _x)
|
|
73
|
-
|
|
74
|
-
result.backward = backward
|
|
75
|
-
return result
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
###
|
|
79
|
-
### Arithmetics
|
|
80
|
-
###
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
def add_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
84
|
-
def callback_x1(result_grad, shape, device, _x1, _x2):
|
|
85
|
-
return sum_to_shape(result_grad, shape, device)
|
|
86
|
-
|
|
87
|
-
def callback_x2(result_grad, shape, device, _x1, _x2):
|
|
88
|
-
return sum_to_shape(result_grad, shape, device)
|
|
89
|
-
|
|
90
|
-
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
def subtract_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
94
|
-
|
|
95
|
-
def callback_x1(result_grad, shape, device, _x1, _x2):
|
|
96
|
-
return sum_to_shape(result_grad, shape, device)
|
|
97
|
-
|
|
98
|
-
def callback_x2(result_grad, shape, device, _x1, _x2):
|
|
99
|
-
return sum_to_shape(-result_grad, shape, device)
|
|
100
|
-
|
|
101
|
-
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
def multiply_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
105
|
-
|
|
106
|
-
def callback_x1(result_grad, shape, device, _x1, _x2):
|
|
107
|
-
return sum_to_shape(result_grad * _x2, shape, device)
|
|
108
|
-
|
|
109
|
-
def callback_x2(result_grad, shape, device, _x1, _x2):
|
|
110
|
-
return sum_to_shape(result_grad * _x1, shape, device)
|
|
111
|
-
|
|
112
|
-
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
def matmul_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
116
|
-
|
|
117
|
-
def callback_x1(result_grad, shape, device, _x1, _x2):
|
|
118
|
-
return sum_to_shape(result_grad @ _x2.swapaxes(-2, -1), shape, device)
|
|
119
|
-
|
|
120
|
-
def callback_x2(result_grad, shape, device, _x1, _x2):
|
|
121
|
-
return sum_to_shape(_x1.swapaxes(-2, -1) @ result_grad, shape, device)
|
|
122
|
-
|
|
123
|
-
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
def divide_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
127
|
-
|
|
128
|
-
def callback_x1(result_grad, shape, device, _x1, _x2):
|
|
129
|
-
return sum_to_shape(result_grad / _x2, shape, device)
|
|
130
|
-
|
|
131
|
-
def callback_x2(result_grad, shape, device, _x1, _x2):
|
|
132
|
-
return sum_to_shape(result_grad * (_x1 * -(_x2**-2)), shape, device)
|
|
133
|
-
|
|
134
|
-
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
def power_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
138
|
-
|
|
139
|
-
def callback_x1(result_grad, shape, device, _x1, _x2):
|
|
140
|
-
return sum_to_shape(result_grad * _x2 * _x1 ** (_x2 - 1), shape, device)
|
|
141
|
-
|
|
142
|
-
def callback_x2(result_grad, shape, device, _x1, _x2):
|
|
143
|
-
if device == "cpu":
|
|
144
|
-
return sum_to_shape(result_grad * np.log(_x1) * _x1**_x2, shape, device)
|
|
145
|
-
else:
|
|
146
|
-
if cp is None:
|
|
147
|
-
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
148
|
-
return sum_to_shape(result_grad * cp.log(_x1) * _x1**_x2, shape, device)
|
|
149
|
-
|
|
150
|
-
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
def negative_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
154
|
-
|
|
155
|
-
def callback_x1(result_grad, shape, device, _x):
|
|
156
|
-
return sum_to_shape(-result_grad, shape, device)
|
|
157
|
-
|
|
158
|
-
return wrapper_1in_diff(result, x, callback_x1)
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
def sign_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
162
|
-
|
|
163
|
-
def callback_x1(result_grad, shape, device, _x):
|
|
164
|
-
return sum_to_shape(result_grad * 0, shape, device)
|
|
165
|
-
|
|
166
|
-
return wrapper_1in_diff(result, x, callback_x1)
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
def abs_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
170
|
-
|
|
171
|
-
def callback_x1(result_grad, shape, device, _x):
|
|
172
|
-
return sum_to_shape(result_grad * result.data / _x, shape, device)
|
|
173
|
-
|
|
174
|
-
return wrapper_1in_diff(result, x, callback_x1)
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
###
|
|
178
|
-
### Exponents/Logarithms
|
|
179
|
-
###
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
def exp_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
183
|
-
|
|
184
|
-
def callback_x1(result_grad, shape, device, _x):
|
|
185
|
-
return sum_to_shape(result_grad * result.data, shape, device)
|
|
186
|
-
|
|
187
|
-
return wrapper_1in_diff(result, x, callback_x1)
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
def sqrt_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
191
|
-
|
|
192
|
-
def callback_x1(result_grad, shape, device, _x):
|
|
193
|
-
return sum_to_shape(result_grad * (0.5 * result.data ** (-0.5)), shape, device)
|
|
194
|
-
|
|
195
|
-
return wrapper_1in_diff(result, x, callback_x1)
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
def log_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
199
|
-
|
|
200
|
-
def callback_x1(result_grad, shape, device, _x):
|
|
201
|
-
return sum_to_shape(result_grad / _x, shape, device)
|
|
202
|
-
|
|
203
|
-
return wrapper_1in_diff(result, x, callback_x1)
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
def square_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
207
|
-
|
|
208
|
-
def callback_x1(result_grad, shape, device, _x):
|
|
209
|
-
return sum_to_shape(result_grad * 2 * _x, shape, device)
|
|
210
|
-
|
|
211
|
-
return wrapper_1in_diff(result, x, callback_x1)
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
###
|
|
215
|
-
###
|
|
216
|
-
###
|
|
217
|
-
|
|
218
|
-
__all__ = [
|
|
219
|
-
"add_diff",
|
|
220
|
-
"subtract_diff",
|
|
221
|
-
"multiply_diff",
|
|
222
|
-
"matmul_diff",
|
|
223
|
-
"divide_diff",
|
|
224
|
-
"power_diff",
|
|
225
|
-
"negative_diff",
|
|
226
|
-
"sign_diff",
|
|
227
|
-
"abs_diff",
|
|
228
|
-
"exp_diff",
|
|
229
|
-
"sqrt_diff",
|
|
230
|
-
"log_diff",
|
|
231
|
-
"square_diff",
|
|
232
|
-
]
|
|
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
|
|
File without changes
|
|
File without changes
|