DepthTensor 2.4.0__py3-none-any.whl → 2.4.2__py3-none-any.whl
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/__init__.py +1 -1
- DepthTensor/_core/ops/comparison.py +67 -67
- DepthTensor/_core/ops/creation.py +24 -26
- DepthTensor/_core/ops/diff.py +63 -48
- DepthTensor/_core/ops/elementwise.py +160 -152
- DepthTensor/_core/ops/reduction.py +32 -32
- DepthTensor/_core/random/generator.py +28 -26
- DepthTensor/_core/utils.py +31 -46
- DepthTensor/autodiff.py +5 -7
- DepthTensor/tensor.py +87 -99
- DepthTensor/typing.py +52 -78
- {depthtensor-2.4.0.dist-info → depthtensor-2.4.2.dist-info}/METADATA +5 -5
- depthtensor-2.4.2.dist-info/RECORD +20 -0
- depthtensor-2.4.0.dist-info/RECORD +0 -20
- {depthtensor-2.4.0.dist-info → depthtensor-2.4.2.dist-info}/WHEEL +0 -0
- {depthtensor-2.4.0.dist-info → depthtensor-2.4.2.dist-info}/licenses/LICENSE +0 -0
- {depthtensor-2.4.0.dist-info → depthtensor-2.4.2.dist-info}/top_level.txt +0 -0
DepthTensor/__init__.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
from typing import
|
|
1
|
+
from typing import overload, Union, Tuple
|
|
2
2
|
|
|
3
3
|
from ...typing import (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
TensorType,
|
|
5
|
+
Device,
|
|
6
|
+
TensorDataBool,
|
|
7
7
|
Casting,
|
|
8
8
|
Order,
|
|
9
|
-
|
|
9
|
+
TensorLike,
|
|
10
10
|
)
|
|
11
11
|
|
|
12
12
|
from ..exceptions import (
|
|
@@ -16,7 +16,7 @@ from ..exceptions import (
|
|
|
16
16
|
DEVICE_MISMATCH_MSG,
|
|
17
17
|
)
|
|
18
18
|
|
|
19
|
-
from ..utils import
|
|
19
|
+
from ..utils import to_tensordata, get_device, get_two_operand_op_device
|
|
20
20
|
|
|
21
21
|
import numpy as np
|
|
22
22
|
|
|
@@ -32,35 +32,35 @@ except (ImportError, ModuleNotFoundError):
|
|
|
32
32
|
|
|
33
33
|
@overload
|
|
34
34
|
def where(
|
|
35
|
-
condition:
|
|
35
|
+
condition: TensorLike,
|
|
36
36
|
/,
|
|
37
37
|
*,
|
|
38
|
-
device:
|
|
38
|
+
device: Device = "cpu",
|
|
39
39
|
requires_grad: bool = False,
|
|
40
|
-
) -> Tuple[
|
|
40
|
+
) -> Tuple[TensorType, ...]: ...
|
|
41
41
|
|
|
42
42
|
|
|
43
43
|
@overload
|
|
44
44
|
def where(
|
|
45
|
-
condition:
|
|
46
|
-
x:
|
|
47
|
-
y:
|
|
45
|
+
condition: TensorLike,
|
|
46
|
+
x: TensorLike | None,
|
|
47
|
+
y: TensorLike | None,
|
|
48
48
|
/,
|
|
49
49
|
*,
|
|
50
|
-
device:
|
|
50
|
+
device: Device = "cpu",
|
|
51
51
|
requires_grad: bool = False,
|
|
52
|
-
) ->
|
|
52
|
+
) -> TensorType: ...
|
|
53
53
|
|
|
54
54
|
|
|
55
55
|
def where(
|
|
56
|
-
condition:
|
|
57
|
-
x:
|
|
58
|
-
y:
|
|
56
|
+
condition: TensorLike,
|
|
57
|
+
x: TensorLike | None = None,
|
|
58
|
+
y: TensorLike | None = None,
|
|
59
59
|
/,
|
|
60
60
|
*,
|
|
61
|
-
device:
|
|
61
|
+
device: Device | None = None,
|
|
62
62
|
requires_grad: bool = False,
|
|
63
|
-
) -> Union[
|
|
63
|
+
) -> Union[tuple[TensorType, ...], TensorType]:
|
|
64
64
|
from ...tensor import Tensor
|
|
65
65
|
|
|
66
66
|
if device is None:
|
|
@@ -68,9 +68,9 @@ def where(
|
|
|
68
68
|
|
|
69
69
|
# * One parameter overload
|
|
70
70
|
if (x is None) and (y is None):
|
|
71
|
-
data =
|
|
71
|
+
data = to_tensordata(condition, device=device)
|
|
72
72
|
if device == "cpu":
|
|
73
|
-
result = np.where(data)
|
|
73
|
+
result = np.where(data) # type: ignore
|
|
74
74
|
else:
|
|
75
75
|
if cp is None:
|
|
76
76
|
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
@@ -85,9 +85,9 @@ def where(
|
|
|
85
85
|
):
|
|
86
86
|
raise DeviceMismatch(DEVICE_MISMATCH_MSG)
|
|
87
87
|
|
|
88
|
-
data =
|
|
89
|
-
x_data =
|
|
90
|
-
y_data =
|
|
88
|
+
data = to_tensordata(condition, device=device)
|
|
89
|
+
x_data = to_tensordata(x, device=device)
|
|
90
|
+
y_data = to_tensordata(y, device=device)
|
|
91
91
|
if device == "cpu":
|
|
92
92
|
result = np.where(data, x_data, y_data)
|
|
93
93
|
else:
|
|
@@ -105,24 +105,24 @@ def where(
|
|
|
105
105
|
|
|
106
106
|
|
|
107
107
|
def wrapper_2in_1out(
|
|
108
|
-
x1:
|
|
109
|
-
x2:
|
|
108
|
+
x1: TensorLike,
|
|
109
|
+
x2: TensorLike,
|
|
110
110
|
/,
|
|
111
|
-
out:
|
|
111
|
+
out: TensorDataBool | None = None,
|
|
112
112
|
*,
|
|
113
113
|
func_name: str,
|
|
114
|
-
device:
|
|
115
|
-
where:
|
|
114
|
+
device: Device | None = None,
|
|
115
|
+
where: TensorDataBool | bool = True,
|
|
116
116
|
casting: Casting = "same_kind",
|
|
117
117
|
order: Order = "K",
|
|
118
118
|
dtype: None = None,
|
|
119
119
|
subok: bool = True,
|
|
120
|
-
) ->
|
|
120
|
+
) -> TensorType:
|
|
121
121
|
from ...tensor import Tensor
|
|
122
122
|
|
|
123
123
|
op_device = get_two_operand_op_device(x1, x2, device)
|
|
124
124
|
|
|
125
|
-
x1, x2 =
|
|
125
|
+
x1, x2 = to_tensordata(x1, device=op_device), to_tensordata(x2, device=op_device)
|
|
126
126
|
if op_device == "cpu":
|
|
127
127
|
y = getattr(np, func_name)(
|
|
128
128
|
x1,
|
|
@@ -142,18 +142,18 @@ def wrapper_2in_1out(
|
|
|
142
142
|
|
|
143
143
|
|
|
144
144
|
def equal(
|
|
145
|
-
x1:
|
|
146
|
-
x2:
|
|
145
|
+
x1: TensorLike,
|
|
146
|
+
x2: TensorLike,
|
|
147
147
|
/,
|
|
148
|
-
out:
|
|
148
|
+
out: TensorDataBool | None = None,
|
|
149
149
|
*,
|
|
150
|
-
device:
|
|
151
|
-
where:
|
|
150
|
+
device: Device | None = None,
|
|
151
|
+
where: TensorDataBool | bool = True,
|
|
152
152
|
casting: Casting = "same_kind",
|
|
153
153
|
order: Order = "K",
|
|
154
154
|
dtype: None = None,
|
|
155
155
|
subok: bool = True,
|
|
156
|
-
) ->
|
|
156
|
+
) -> TensorType:
|
|
157
157
|
return wrapper_2in_1out(
|
|
158
158
|
x1,
|
|
159
159
|
x2,
|
|
@@ -169,18 +169,18 @@ def equal(
|
|
|
169
169
|
|
|
170
170
|
|
|
171
171
|
def not_equal(
|
|
172
|
-
x1:
|
|
173
|
-
x2:
|
|
172
|
+
x1: TensorLike,
|
|
173
|
+
x2: TensorLike,
|
|
174
174
|
/,
|
|
175
|
-
out:
|
|
175
|
+
out: TensorDataBool | None = None,
|
|
176
176
|
*,
|
|
177
|
-
device:
|
|
178
|
-
where:
|
|
177
|
+
device: Device | None = None,
|
|
178
|
+
where: TensorDataBool | bool = True,
|
|
179
179
|
casting: Casting = "same_kind",
|
|
180
180
|
order: Order = "K",
|
|
181
181
|
dtype: None = None,
|
|
182
182
|
subok: bool = True,
|
|
183
|
-
) ->
|
|
183
|
+
) -> TensorType:
|
|
184
184
|
return wrapper_2in_1out(
|
|
185
185
|
x1,
|
|
186
186
|
x2,
|
|
@@ -196,18 +196,18 @@ def not_equal(
|
|
|
196
196
|
|
|
197
197
|
|
|
198
198
|
def greater(
|
|
199
|
-
x1:
|
|
200
|
-
x2:
|
|
199
|
+
x1: TensorLike,
|
|
200
|
+
x2: TensorLike,
|
|
201
201
|
/,
|
|
202
|
-
out:
|
|
202
|
+
out: TensorDataBool | None = None,
|
|
203
203
|
*,
|
|
204
|
-
device:
|
|
205
|
-
where:
|
|
204
|
+
device: Device | None = None,
|
|
205
|
+
where: TensorDataBool | bool = True,
|
|
206
206
|
casting: Casting = "same_kind",
|
|
207
207
|
order: Order = "K",
|
|
208
208
|
dtype: None = None,
|
|
209
209
|
subok: bool = True,
|
|
210
|
-
) ->
|
|
210
|
+
) -> TensorType:
|
|
211
211
|
return wrapper_2in_1out(
|
|
212
212
|
x1,
|
|
213
213
|
x2,
|
|
@@ -223,18 +223,18 @@ def greater(
|
|
|
223
223
|
|
|
224
224
|
|
|
225
225
|
def greater_equal(
|
|
226
|
-
x1:
|
|
227
|
-
x2:
|
|
226
|
+
x1: TensorLike,
|
|
227
|
+
x2: TensorLike,
|
|
228
228
|
/,
|
|
229
|
-
out:
|
|
229
|
+
out: TensorDataBool | None = None,
|
|
230
230
|
*,
|
|
231
|
-
device:
|
|
232
|
-
where:
|
|
231
|
+
device: Device | None = None,
|
|
232
|
+
where: TensorDataBool | bool = True,
|
|
233
233
|
casting: Casting = "same_kind",
|
|
234
234
|
order: Order = "K",
|
|
235
235
|
dtype: None = None,
|
|
236
236
|
subok: bool = True,
|
|
237
|
-
) ->
|
|
237
|
+
) -> TensorType:
|
|
238
238
|
return wrapper_2in_1out(
|
|
239
239
|
x1,
|
|
240
240
|
x2,
|
|
@@ -250,18 +250,18 @@ def greater_equal(
|
|
|
250
250
|
|
|
251
251
|
|
|
252
252
|
def less(
|
|
253
|
-
x1:
|
|
254
|
-
x2:
|
|
253
|
+
x1: TensorLike,
|
|
254
|
+
x2: TensorLike,
|
|
255
255
|
/,
|
|
256
|
-
out:
|
|
256
|
+
out: TensorDataBool | None = None,
|
|
257
257
|
*,
|
|
258
|
-
device:
|
|
259
|
-
where:
|
|
258
|
+
device: Device | None = None,
|
|
259
|
+
where: TensorDataBool | bool = True,
|
|
260
260
|
casting: Casting = "same_kind",
|
|
261
261
|
order: Order = "K",
|
|
262
262
|
dtype: None = None,
|
|
263
263
|
subok: bool = True,
|
|
264
|
-
) ->
|
|
264
|
+
) -> TensorType:
|
|
265
265
|
return wrapper_2in_1out(
|
|
266
266
|
x1,
|
|
267
267
|
x2,
|
|
@@ -277,18 +277,18 @@ def less(
|
|
|
277
277
|
|
|
278
278
|
|
|
279
279
|
def less_equal(
|
|
280
|
-
x1:
|
|
281
|
-
x2:
|
|
280
|
+
x1: TensorLike,
|
|
281
|
+
x2: TensorLike,
|
|
282
282
|
/,
|
|
283
|
-
out:
|
|
283
|
+
out: TensorDataBool | None = None,
|
|
284
284
|
*,
|
|
285
|
-
device:
|
|
286
|
-
where:
|
|
285
|
+
device: Device | None = None,
|
|
286
|
+
where: TensorDataBool | bool = True,
|
|
287
287
|
casting: Casting = "same_kind",
|
|
288
288
|
order: Order = "K",
|
|
289
289
|
dtype: None = None,
|
|
290
290
|
subok: bool = True,
|
|
291
|
-
) ->
|
|
291
|
+
) -> TensorType:
|
|
292
292
|
return wrapper_2in_1out(
|
|
293
293
|
x1,
|
|
294
294
|
x2,
|
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
from typing import
|
|
1
|
+
from typing import Literal
|
|
2
2
|
|
|
3
3
|
from ...typing import (
|
|
4
|
+
TensorType,
|
|
4
5
|
TensorLike,
|
|
5
6
|
DTypeLike,
|
|
6
7
|
Order,
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
ShapeLike,
|
|
11
|
-
NDArrayLike,
|
|
8
|
+
Axis,
|
|
9
|
+
Device,
|
|
10
|
+
Shape,
|
|
12
11
|
)
|
|
13
12
|
|
|
14
13
|
from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
|
|
15
|
-
|
|
16
|
-
from ..utils import to_xp_array, get_device
|
|
14
|
+
from ..utils import to_tensordata, get_device
|
|
17
15
|
|
|
18
16
|
import numpy as np
|
|
19
17
|
|
|
@@ -28,23 +26,23 @@ except (ImportError, ModuleNotFoundError):
|
|
|
28
26
|
|
|
29
27
|
|
|
30
28
|
def zeros_like(
|
|
31
|
-
a:
|
|
29
|
+
a: TensorLike,
|
|
32
30
|
/,
|
|
33
31
|
*,
|
|
34
|
-
device:
|
|
32
|
+
device: Device | None = None,
|
|
35
33
|
requires_grad: bool = False,
|
|
36
|
-
dtype:
|
|
34
|
+
dtype: DTypeLike | None = None,
|
|
37
35
|
order: Order = "K",
|
|
38
36
|
subok: bool = True,
|
|
39
|
-
shape:
|
|
40
|
-
) ->
|
|
37
|
+
shape: Axis | None = None,
|
|
38
|
+
) -> TensorType:
|
|
41
39
|
from ...tensor import Tensor
|
|
42
40
|
|
|
43
41
|
if device is None:
|
|
44
42
|
device_op = get_device(a)
|
|
45
43
|
else:
|
|
46
44
|
device_op = device
|
|
47
|
-
a =
|
|
45
|
+
a = to_tensordata(a)
|
|
48
46
|
if device_op == "cpu":
|
|
49
47
|
y = np.zeros_like(a, dtype=dtype, order=order, subok=subok, shape=shape)
|
|
50
48
|
else:
|
|
@@ -55,23 +53,23 @@ def zeros_like(
|
|
|
55
53
|
|
|
56
54
|
|
|
57
55
|
def ones_like(
|
|
58
|
-
a:
|
|
56
|
+
a: TensorLike,
|
|
59
57
|
/,
|
|
60
58
|
*,
|
|
61
|
-
device:
|
|
59
|
+
device: Device | None = None,
|
|
62
60
|
requires_grad: bool = False,
|
|
63
|
-
dtype:
|
|
61
|
+
dtype: DTypeLike | None = None,
|
|
64
62
|
order: Order = "K",
|
|
65
63
|
subok: bool = True,
|
|
66
|
-
shape:
|
|
67
|
-
) ->
|
|
64
|
+
shape: Axis | None = None,
|
|
65
|
+
) -> TensorType:
|
|
68
66
|
from ...tensor import Tensor
|
|
69
67
|
|
|
70
68
|
if device is None:
|
|
71
69
|
device_op = get_device(a)
|
|
72
70
|
else:
|
|
73
71
|
device_op = device
|
|
74
|
-
a =
|
|
72
|
+
a = to_tensordata(a)
|
|
75
73
|
if device_op == "cpu":
|
|
76
74
|
y = np.ones_like(a, dtype=dtype, order=order, subok=subok, shape=shape)
|
|
77
75
|
else:
|
|
@@ -82,13 +80,13 @@ def ones_like(
|
|
|
82
80
|
|
|
83
81
|
|
|
84
82
|
def zeros(
|
|
85
|
-
shape:
|
|
83
|
+
shape: Shape,
|
|
86
84
|
dtype: DTypeLike = float,
|
|
87
85
|
order: Literal["C", "F"] = "C",
|
|
88
86
|
*,
|
|
89
|
-
device:
|
|
87
|
+
device: Device = "cpu",
|
|
90
88
|
requires_grad: bool = False,
|
|
91
|
-
) ->
|
|
89
|
+
) -> TensorType:
|
|
92
90
|
from ...tensor import Tensor
|
|
93
91
|
|
|
94
92
|
if device == "cpu":
|
|
@@ -101,13 +99,13 @@ def zeros(
|
|
|
101
99
|
|
|
102
100
|
|
|
103
101
|
def ones(
|
|
104
|
-
shape:
|
|
102
|
+
shape: Shape,
|
|
105
103
|
dtype: DTypeLike = float,
|
|
106
104
|
order: Literal["C", "F"] = "C",
|
|
107
105
|
*,
|
|
108
|
-
device:
|
|
106
|
+
device: Device = "cpu",
|
|
109
107
|
requires_grad: bool = False,
|
|
110
|
-
) ->
|
|
108
|
+
) -> TensorType:
|
|
111
109
|
from ...tensor import Tensor
|
|
112
110
|
|
|
113
111
|
if device == "cpu":
|
DepthTensor/_core/ops/diff.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
from typing import Callable
|
|
2
2
|
|
|
3
|
-
from ...typing import
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
from ...typing import TensorLike, TensorData, Axis, TensorType
|
|
4
|
+
from ..utils import (
|
|
5
|
+
unbroadcast_tensordata_to_shape,
|
|
6
|
+
to_tensordata,
|
|
7
|
+
get_two_operand_op_device,
|
|
8
|
+
)
|
|
6
9
|
from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
|
|
7
10
|
|
|
8
11
|
import numpy as np
|
|
@@ -19,12 +22,12 @@ except (ImportError, ModuleNotFoundError):
|
|
|
19
22
|
|
|
20
23
|
|
|
21
24
|
def wrapper_2in_diff(
|
|
22
|
-
result:
|
|
23
|
-
x1:
|
|
24
|
-
x2:
|
|
25
|
+
result: TensorType,
|
|
26
|
+
x1: TensorLike,
|
|
27
|
+
x2: TensorLike,
|
|
25
28
|
callback_x1: Callable,
|
|
26
29
|
callback_x2: Callable,
|
|
27
|
-
) ->
|
|
30
|
+
) -> TensorType:
|
|
28
31
|
if not result.requires_grad:
|
|
29
32
|
return result
|
|
30
33
|
from ...tensor import Tensor
|
|
@@ -33,9 +36,9 @@ def wrapper_2in_diff(
|
|
|
33
36
|
if result.grad is None:
|
|
34
37
|
result.zeros_grad()
|
|
35
38
|
|
|
36
|
-
result_grad:
|
|
39
|
+
result_grad: TensorData = result.grad # type: ignore (result.grad cannot be None)
|
|
37
40
|
device = get_two_operand_op_device(x1, x2, None)
|
|
38
|
-
x1_data, x2_data =
|
|
41
|
+
x1_data, x2_data = to_tensordata(x1, device=device), to_tensordata(
|
|
39
42
|
x2, device=device
|
|
40
43
|
)
|
|
41
44
|
|
|
@@ -57,8 +60,8 @@ def wrapper_2in_diff(
|
|
|
57
60
|
|
|
58
61
|
|
|
59
62
|
def wrapper_1in_diff(
|
|
60
|
-
result:
|
|
61
|
-
) ->
|
|
63
|
+
result: TensorType, x: TensorLike, callback_x1: Callable
|
|
64
|
+
) -> TensorType:
|
|
62
65
|
if not result.requires_grad:
|
|
63
66
|
return result
|
|
64
67
|
from ...tensor import Tensor
|
|
@@ -67,8 +70,8 @@ def wrapper_1in_diff(
|
|
|
67
70
|
if result.grad is None:
|
|
68
71
|
result.zeros_grad()
|
|
69
72
|
|
|
70
|
-
result_grad:
|
|
71
|
-
_x =
|
|
73
|
+
result_grad: TensorData = result.grad # type: ignore
|
|
74
|
+
_x = to_tensordata(x)
|
|
72
75
|
if isinstance(x, Tensor) and x.requires_grad:
|
|
73
76
|
if x.grad is None:
|
|
74
77
|
x.zeros_grad()
|
|
@@ -83,102 +86,110 @@ def wrapper_1in_diff(
|
|
|
83
86
|
###
|
|
84
87
|
|
|
85
88
|
|
|
86
|
-
def add_diff(result:
|
|
89
|
+
def add_diff(result: TensorType, x1: TensorLike, x2: TensorLike) -> TensorType:
|
|
87
90
|
def callback_x1(result_grad, x1_shape, device, x1_data, x2_data):
|
|
88
|
-
return
|
|
91
|
+
return unbroadcast_tensordata_to_shape(result_grad, x1_shape, device)
|
|
89
92
|
|
|
90
93
|
def callback_x2(result_grad, x2_shape, device, x1_data, x2_data):
|
|
91
|
-
return
|
|
94
|
+
return unbroadcast_tensordata_to_shape(result_grad, x2_shape, device)
|
|
92
95
|
|
|
93
96
|
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
94
97
|
|
|
95
98
|
|
|
96
|
-
def subtract_diff(result:
|
|
99
|
+
def subtract_diff(result: TensorType, x1: TensorLike, x2: TensorLike) -> TensorType:
|
|
97
100
|
|
|
98
101
|
def callback_x1(result_grad, x1_shape, device, x1_data, x2_data):
|
|
99
|
-
return
|
|
102
|
+
return unbroadcast_tensordata_to_shape(result_grad, x1_shape, device)
|
|
100
103
|
|
|
101
104
|
def callback_x2(result_grad, x2_shape, device, x1_data, x2_data):
|
|
102
|
-
return
|
|
105
|
+
return unbroadcast_tensordata_to_shape(-result_grad, x2_shape, device)
|
|
103
106
|
|
|
104
107
|
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
105
108
|
|
|
106
109
|
|
|
107
|
-
def multiply_diff(result:
|
|
110
|
+
def multiply_diff(result: TensorType, x1: TensorLike, x2: TensorLike) -> TensorType:
|
|
108
111
|
|
|
109
112
|
def callback_x1(result_grad, x1_shape, device, x1_data, x2_data):
|
|
110
|
-
return
|
|
113
|
+
return unbroadcast_tensordata_to_shape(result_grad * x2_data, x1_shape, device)
|
|
111
114
|
|
|
112
115
|
def callback_x2(result_grad, x2_shape, device, x1_data, x2_data):
|
|
113
|
-
return
|
|
116
|
+
return unbroadcast_tensordata_to_shape(result_grad * x1_data, x2_shape, device)
|
|
114
117
|
|
|
115
118
|
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
116
119
|
|
|
117
120
|
|
|
118
|
-
def matmul_diff(result:
|
|
121
|
+
def matmul_diff(result: TensorType, x1: TensorLike, x2: TensorLike) -> TensorType:
|
|
119
122
|
|
|
120
123
|
def callback_x1(result_grad, x1_shape, device, x1_data, x2_data):
|
|
121
|
-
return
|
|
124
|
+
return unbroadcast_tensordata_to_shape(
|
|
125
|
+
result_grad @ x2_data.swapaxes(-2, -1), x1_shape, device
|
|
126
|
+
)
|
|
122
127
|
|
|
123
128
|
def callback_x2(result_grad, x2_shape, device, x1_data, x2_data):
|
|
124
|
-
return
|
|
129
|
+
return unbroadcast_tensordata_to_shape(
|
|
130
|
+
x1_data.swapaxes(-2, -1) @ result_grad, x2_shape, device
|
|
131
|
+
)
|
|
125
132
|
|
|
126
133
|
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
127
134
|
|
|
128
135
|
|
|
129
|
-
def divide_diff(result:
|
|
136
|
+
def divide_diff(result: TensorType, x1: TensorLike, x2: TensorLike) -> TensorType:
|
|
130
137
|
|
|
131
138
|
def callback_x1(result_grad, x1_shape, device, x1_data, x2_data):
|
|
132
|
-
return
|
|
139
|
+
return unbroadcast_tensordata_to_shape(result_grad / x2_data, x1_shape, device)
|
|
133
140
|
|
|
134
141
|
def callback_x2(result_grad, x2_shape, device, x1_data, x2_data):
|
|
135
|
-
return
|
|
142
|
+
return unbroadcast_tensordata_to_shape(
|
|
143
|
+
result_grad * (x1_data * -(x2_data**-2)), x2_shape, device
|
|
144
|
+
)
|
|
136
145
|
|
|
137
146
|
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
138
147
|
|
|
139
148
|
|
|
140
|
-
def power_diff(result:
|
|
149
|
+
def power_diff(result: TensorType, x1: TensorLike, x2: TensorLike) -> TensorType:
|
|
141
150
|
|
|
142
151
|
def callback_x1(result_grad, x1_shape, device, x1_data, x2_data):
|
|
143
|
-
return
|
|
152
|
+
return unbroadcast_tensordata_to_shape(
|
|
144
153
|
result_grad * x2_data * x1_data ** (x2_data - 1), x1_shape, device
|
|
145
154
|
)
|
|
146
155
|
|
|
147
156
|
def callback_x2(result_grad, x2_shape, device, x1_data, x2_data):
|
|
148
157
|
if device == "cpu":
|
|
149
|
-
return
|
|
158
|
+
return unbroadcast_tensordata_to_shape(
|
|
150
159
|
result_grad * np.log(x1_data) * x1_data**x2_data, x2_shape, device
|
|
151
160
|
)
|
|
152
161
|
else:
|
|
153
162
|
if cp is None:
|
|
154
163
|
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
155
|
-
return
|
|
164
|
+
return unbroadcast_tensordata_to_shape(
|
|
156
165
|
result_grad * cp.log(x1_data) * x1_data**x2_data, x2_shape, device
|
|
157
166
|
)
|
|
158
167
|
|
|
159
168
|
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
160
169
|
|
|
161
170
|
|
|
162
|
-
def negative_diff(result:
|
|
171
|
+
def negative_diff(result: TensorType, x: TensorType) -> TensorType:
|
|
163
172
|
|
|
164
173
|
def callback_x1(result_grad, x1_shape, device, x1_data):
|
|
165
|
-
return
|
|
174
|
+
return unbroadcast_tensordata_to_shape(-result_grad, x1_shape, device)
|
|
166
175
|
|
|
167
176
|
return wrapper_1in_diff(result, x, callback_x1)
|
|
168
177
|
|
|
169
178
|
|
|
170
|
-
def sign_diff(result:
|
|
179
|
+
def sign_diff(result: TensorType, x: TensorType) -> TensorType:
|
|
171
180
|
|
|
172
181
|
def callback_x1(result_grad, x1_shape, device, x1_data):
|
|
173
|
-
return
|
|
182
|
+
return unbroadcast_tensordata_to_shape(result_grad * 0, x1_shape, device)
|
|
174
183
|
|
|
175
184
|
return wrapper_1in_diff(result, x, callback_x1)
|
|
176
185
|
|
|
177
186
|
|
|
178
|
-
def abs_diff(result:
|
|
187
|
+
def abs_diff(result: TensorType, x: TensorType) -> TensorType:
|
|
179
188
|
|
|
180
189
|
def callback_x1(result_grad, x1_shape, device, x1_data):
|
|
181
|
-
return
|
|
190
|
+
return unbroadcast_tensordata_to_shape(
|
|
191
|
+
result_grad * result.data / x1_data, x1_shape, device
|
|
192
|
+
)
|
|
182
193
|
|
|
183
194
|
return wrapper_1in_diff(result, x, callback_x1)
|
|
184
195
|
|
|
@@ -188,36 +199,40 @@ def abs_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
|
188
199
|
###
|
|
189
200
|
|
|
190
201
|
|
|
191
|
-
def exp_diff(result:
|
|
202
|
+
def exp_diff(result: TensorType, x: TensorType) -> TensorType:
|
|
192
203
|
|
|
193
204
|
def callback_x1(result_grad, x1_shape, device, x1_data):
|
|
194
|
-
return
|
|
205
|
+
return unbroadcast_tensordata_to_shape(
|
|
206
|
+
result_grad * result.data, x1_shape, device
|
|
207
|
+
)
|
|
195
208
|
|
|
196
209
|
return wrapper_1in_diff(result, x, callback_x1)
|
|
197
210
|
|
|
198
211
|
|
|
199
|
-
def sqrt_diff(result:
|
|
212
|
+
def sqrt_diff(result: TensorType, x: TensorType) -> TensorType:
|
|
200
213
|
|
|
201
214
|
def callback_x1(result_grad, x1_shape, device, x1_data):
|
|
202
|
-
return
|
|
215
|
+
return unbroadcast_tensordata_to_shape(
|
|
203
216
|
result_grad * (0.5 * result.data ** (-0.5)), x1_shape, device
|
|
204
217
|
)
|
|
205
218
|
|
|
206
219
|
return wrapper_1in_diff(result, x, callback_x1)
|
|
207
220
|
|
|
208
221
|
|
|
209
|
-
def log_diff(result:
|
|
222
|
+
def log_diff(result: TensorType, x: TensorType) -> TensorType:
|
|
210
223
|
|
|
211
224
|
def callback_x1(result_grad, x1_shape, device, x1_data):
|
|
212
|
-
return
|
|
225
|
+
return unbroadcast_tensordata_to_shape(result_grad / x1_data, x1_shape, device)
|
|
213
226
|
|
|
214
227
|
return wrapper_1in_diff(result, x, callback_x1)
|
|
215
228
|
|
|
216
229
|
|
|
217
|
-
def square_diff(result:
|
|
230
|
+
def square_diff(result: TensorType, x: TensorType) -> TensorType:
|
|
218
231
|
|
|
219
232
|
def callback_x1(result_grad, x1_shape, device, x1_data):
|
|
220
|
-
return
|
|
233
|
+
return unbroadcast_tensordata_to_shape(
|
|
234
|
+
result_grad * 2 * x1_data, x1_shape, device
|
|
235
|
+
)
|
|
221
236
|
|
|
222
237
|
return wrapper_1in_diff(result, x, callback_x1)
|
|
223
238
|
|
|
@@ -228,8 +243,8 @@ def square_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
|
228
243
|
|
|
229
244
|
|
|
230
245
|
def mean_diff(
|
|
231
|
-
result:
|
|
232
|
-
) ->
|
|
246
|
+
result: TensorType, x: TensorType, axis: Axis, keepdims: bool
|
|
247
|
+
) -> TensorType:
|
|
233
248
|
def callback_x1(result_grad, x1_shape, device, _x):
|
|
234
249
|
if device == "cpu":
|
|
235
250
|
xp = np
|