DepthTensor 2.2.0__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.0/src/DepthTensor.egg-info → depthtensor-2.3.0}/PKG-INFO +1 -1
- {depthtensor-2.2.0 → depthtensor-2.3.0}/pyproject.toml +1 -1
- {depthtensor-2.2.0 → 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.0 → depthtensor-2.3.0}/src/DepthTensor/_core/ops/elementwise.py +51 -1
- {depthtensor-2.2.0 → depthtensor-2.3.0}/src/DepthTensor/_core/utils.py +1 -1
- {depthtensor-2.2.0 → depthtensor-2.3.0}/src/DepthTensor/tensor.py +67 -2
- {depthtensor-2.2.0 → depthtensor-2.3.0/src/DepthTensor.egg-info}/PKG-INFO +1 -1
- depthtensor-2.2.0/src/DepthTensor/_core/ops/diff.py +0 -232
- {depthtensor-2.2.0 → depthtensor-2.3.0}/LICENSE +0 -0
- {depthtensor-2.2.0 → depthtensor-2.3.0}/README.md +0 -0
- {depthtensor-2.2.0 → depthtensor-2.3.0}/setup.cfg +0 -0
- {depthtensor-2.2.0 → depthtensor-2.3.0}/src/DepthTensor/_core/__init__.py +0 -0
- {depthtensor-2.2.0 → depthtensor-2.3.0}/src/DepthTensor/_core/exceptions.py +0 -0
- {depthtensor-2.2.0 → depthtensor-2.3.0}/src/DepthTensor/_core/ops/__init__.py +0 -0
- {depthtensor-2.2.0 → depthtensor-2.3.0}/src/DepthTensor/_core/ops/comparison.py +0 -0
- {depthtensor-2.2.0 → depthtensor-2.3.0}/src/DepthTensor/_core/ops/creation.py +0 -0
- {depthtensor-2.2.0 → depthtensor-2.3.0}/src/DepthTensor/_core/ops/reduction.py +0 -0
- {depthtensor-2.2.0 → depthtensor-2.3.0}/src/DepthTensor/_core/random/__init__.py +0 -0
- {depthtensor-2.2.0 → depthtensor-2.3.0}/src/DepthTensor/_core/random/generator.py +0 -0
- {depthtensor-2.2.0 → depthtensor-2.3.0}/src/DepthTensor/autodiff.py +0 -0
- {depthtensor-2.2.0 → depthtensor-2.3.0}/src/DepthTensor/typing.py +0 -0
- {depthtensor-2.2.0 → depthtensor-2.3.0}/src/DepthTensor.egg-info/SOURCES.txt +0 -0
- {depthtensor-2.2.0 → depthtensor-2.3.0}/src/DepthTensor.egg-info/dependency_links.txt +0 -0
- {depthtensor-2.2.0 → depthtensor-2.3.0}/src/DepthTensor.egg-info/requires.txt +0 -0
- {depthtensor-2.2.0 → 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
|
|
|
@@ -235,6 +238,11 @@ class Tensor:
|
|
|
235
238
|
return self.copy()
|
|
236
239
|
else:
|
|
237
240
|
if in_place:
|
|
241
|
+
if self.requires_grad:
|
|
242
|
+
raise RuntimeError(
|
|
243
|
+
"In-place operations (device switch) are forbidden on differentiable tensors."
|
|
244
|
+
)
|
|
245
|
+
|
|
238
246
|
self.device = device
|
|
239
247
|
self.prev = () if clear_prev else self.prev
|
|
240
248
|
self.data = xp_array_to_device(self.data, device=device)
|
|
@@ -306,6 +314,35 @@ class Tensor:
|
|
|
306
314
|
subok=subok,
|
|
307
315
|
)
|
|
308
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
|
+
|
|
309
346
|
###
|
|
310
347
|
### Reduction
|
|
311
348
|
###
|
|
@@ -400,6 +437,10 @@ class Tensor:
|
|
|
400
437
|
)
|
|
401
438
|
|
|
402
439
|
def __iadd__(self, t: OperandLike) -> Tensor:
|
|
440
|
+
if self.requires_grad:
|
|
441
|
+
raise RuntimeError(
|
|
442
|
+
"In-place operations are (add) forbidden on differentiable tensors."
|
|
443
|
+
)
|
|
403
444
|
return add(self, t, in_place=True)
|
|
404
445
|
|
|
405
446
|
def __sub__(self, t: OperandLike) -> Tensor:
|
|
@@ -413,6 +454,10 @@ class Tensor:
|
|
|
413
454
|
)
|
|
414
455
|
|
|
415
456
|
def __isub__(self, t: OperandLike) -> Tensor:
|
|
457
|
+
if self.requires_grad:
|
|
458
|
+
raise RuntimeError(
|
|
459
|
+
"In-place operations (sub) are forbidden on differentiable tensors."
|
|
460
|
+
)
|
|
416
461
|
return subtract(self, t, in_place=True)
|
|
417
462
|
|
|
418
463
|
def __mul__(self, t: OperandLike) -> Tensor:
|
|
@@ -426,6 +471,10 @@ class Tensor:
|
|
|
426
471
|
)
|
|
427
472
|
|
|
428
473
|
def __imul__(self, t: OperandLike) -> Tensor:
|
|
474
|
+
if self.requires_grad:
|
|
475
|
+
raise RuntimeError(
|
|
476
|
+
"In-place operations are (mul) forbidden on differentiable tensors."
|
|
477
|
+
)
|
|
429
478
|
return multiply(self, t, in_place=True)
|
|
430
479
|
|
|
431
480
|
def __matmul__(self, t: OperandLike) -> Tensor:
|
|
@@ -439,6 +488,10 @@ class Tensor:
|
|
|
439
488
|
)
|
|
440
489
|
|
|
441
490
|
def __imatmul__(self, t: OperandLike) -> Tensor:
|
|
491
|
+
if self.requires_grad:
|
|
492
|
+
raise RuntimeError(
|
|
493
|
+
"In-place operations are (matmul) forbidden on differentiable tensors."
|
|
494
|
+
)
|
|
442
495
|
return matmul(self, t, in_place=True)
|
|
443
496
|
|
|
444
497
|
def __truediv__(self, t: OperandLike) -> Tensor:
|
|
@@ -452,6 +505,10 @@ class Tensor:
|
|
|
452
505
|
)
|
|
453
506
|
|
|
454
507
|
def __itruediv__(self, t: OperandLike) -> Tensor:
|
|
508
|
+
if self.requires_grad:
|
|
509
|
+
raise RuntimeError(
|
|
510
|
+
"In-place operations (div) are forbidden on differentiable tensors."
|
|
511
|
+
)
|
|
455
512
|
return divide(self, t, in_place=True)
|
|
456
513
|
|
|
457
514
|
def __pow__(self, t: OperandLike) -> Tensor:
|
|
@@ -460,6 +517,10 @@ class Tensor:
|
|
|
460
517
|
)
|
|
461
518
|
|
|
462
519
|
def __ipow__(self, t: OperandLike) -> Tensor:
|
|
520
|
+
if self.requires_grad:
|
|
521
|
+
raise RuntimeError(
|
|
522
|
+
"In-place operations (pow) are forbidden on differentiable tensors."
|
|
523
|
+
)
|
|
463
524
|
return power(self, t, in_place=True)
|
|
464
525
|
|
|
465
526
|
###
|
|
@@ -495,6 +556,10 @@ class Tensor:
|
|
|
495
556
|
return self.data[index]
|
|
496
557
|
|
|
497
558
|
def __setitem__(self, index, value) -> Any:
|
|
559
|
+
if self.requires_grad:
|
|
560
|
+
raise RuntimeError(
|
|
561
|
+
"In-place operations (indexing) are forbidden on differentiable tensors."
|
|
562
|
+
)
|
|
498
563
|
self.data[index] = value
|
|
499
564
|
|
|
500
565
|
def __iter__(self) -> Iterator:
|
|
@@ -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
|