DepthTensor 1.12.0__tar.gz → 2.0.1__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 (34) hide show
  1. {depthtensor-1.12.0/src/DepthTensor.egg-info → depthtensor-2.0.1}/PKG-INFO +1 -3
  2. {depthtensor-1.12.0 → depthtensor-2.0.1}/pyproject.toml +1 -4
  3. {depthtensor-1.12.0 → depthtensor-2.0.1}/src/DepthTensor/__init__.py +5 -3
  4. depthtensor-2.0.1/src/DepthTensor/_core/ops/comparison.py +323 -0
  5. depthtensor-2.0.1/src/DepthTensor/_core/ops/creation.py +93 -0
  6. {depthtensor-1.12.0 → depthtensor-2.0.1}/src/DepthTensor/_core/ops/diff.py +87 -39
  7. depthtensor-2.0.1/src/DepthTensor/_core/ops/elementwise.py +741 -0
  8. {depthtensor-1.12.0 → depthtensor-2.0.1}/src/DepthTensor/_core/ops/reduction.py +53 -53
  9. depthtensor-2.0.1/src/DepthTensor/_core/utils.py +228 -0
  10. depthtensor-2.0.1/src/DepthTensor/tensor.py +541 -0
  11. {depthtensor-1.12.0 → depthtensor-2.0.1}/src/DepthTensor/typing.py +54 -61
  12. {depthtensor-1.12.0 → depthtensor-2.0.1/src/DepthTensor.egg-info}/PKG-INFO +1 -3
  13. {depthtensor-1.12.0 → depthtensor-2.0.1}/src/DepthTensor.egg-info/SOURCES.txt +1 -2
  14. {depthtensor-1.12.0 → depthtensor-2.0.1}/src/DepthTensor.egg-info/requires.txt +0 -3
  15. {depthtensor-1.12.0 → depthtensor-2.0.1}/src/example/example.py +18 -4
  16. depthtensor-2.0.1/src/example/test.py +10 -0
  17. depthtensor-1.12.0/src/DepthTensor/_core/ops/comparison.py +0 -229
  18. depthtensor-1.12.0/src/DepthTensor/_core/ops/creation.py +0 -95
  19. depthtensor-1.12.0/src/DepthTensor/_core/ops/elementwise.py +0 -541
  20. depthtensor-1.12.0/src/DepthTensor/_core/utils.py +0 -154
  21. depthtensor-1.12.0/src/DepthTensor/tensor.py +0 -887
  22. depthtensor-1.12.0/src/example/test.py +0 -12
  23. depthtensor-1.12.0/tests/test_tensor.py +0 -49
  24. {depthtensor-1.12.0 → depthtensor-2.0.1}/LICENSE +0 -0
  25. {depthtensor-1.12.0 → depthtensor-2.0.1}/README.md +0 -0
  26. {depthtensor-1.12.0 → depthtensor-2.0.1}/setup.cfg +0 -0
  27. {depthtensor-1.12.0 → depthtensor-2.0.1}/src/DepthTensor/_core/__init__.py +0 -0
  28. {depthtensor-1.12.0 → depthtensor-2.0.1}/src/DepthTensor/_core/exceptions.py +0 -0
  29. {depthtensor-1.12.0 → depthtensor-2.0.1}/src/DepthTensor/_core/ops/__init__.py +0 -0
  30. {depthtensor-1.12.0 → depthtensor-2.0.1}/src/DepthTensor/_core/random/__init__.py +0 -0
  31. {depthtensor-1.12.0 → depthtensor-2.0.1}/src/DepthTensor/_core/random/generator.py +0 -0
  32. {depthtensor-1.12.0 → depthtensor-2.0.1}/src/DepthTensor/autodiff.py +0 -0
  33. {depthtensor-1.12.0 → depthtensor-2.0.1}/src/DepthTensor.egg-info/dependency_links.txt +0 -0
  34. {depthtensor-1.12.0 → depthtensor-2.0.1}/src/DepthTensor.egg-info/top_level.txt +0 -0
@@ -1,11 +1,9 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: DepthTensor
3
- Version: 1.12.0
3
+ Version: 2.0.1
4
4
  Description-Content-Type: text/markdown
5
5
  License-File: LICENSE
6
6
  Requires-Dist: numpy
7
- Provides-Extra: test
8
- Requires-Dist: pytest; extra == "test"
9
7
  Provides-Extra: gpu
10
8
  Requires-Dist: cupy-cuda12x; extra == "gpu"
11
9
  Dynamic: license-file
@@ -4,16 +4,13 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "DepthTensor"
7
- version = "1.12.0"
7
+ version = "2.0.1"
8
8
  readme = "README.md"
9
9
  dependencies = [
10
10
  "numpy"
11
11
  ]
12
12
 
13
13
  [project.optional-dependencies]
14
- test = [
15
- "pytest"
16
- ]
17
14
  gpu = [
18
15
  "cupy-cuda12x"
19
16
  ]
@@ -1,7 +1,9 @@
1
1
  from .tensor import Tensor
2
+ from .autodiff import differentiate
2
3
  from .typing import *
3
- from ._core import random, create_2in_1out, create_1in_1out
4
+
5
+ from ._core.ops import *
4
6
  from ._core.exceptions import *
5
- from .autodiff import differentiate
7
+ from ._core import random
6
8
 
7
- __version__ = "1.12.0"
9
+ __version__ = "2.0.1"
@@ -0,0 +1,323 @@
1
+ from typing import Union, Optional, Tuple, overload
2
+
3
+ from ...typing import (
4
+ TensorLike,
5
+ DeviceLike,
6
+ NDArrayLikeBool,
7
+ Casting,
8
+ Order,
9
+ OperandLike,
10
+ )
11
+
12
+ from ..exceptions import (
13
+ CuPyNotFound,
14
+ CUPY_NOT_FOUND_MSG,
15
+ DeviceMismatch,
16
+ DEVICE_MISMATCH_MSG,
17
+ )
18
+
19
+ from ..utils import to_xp_array, get_device, get_two_operand_op_device
20
+
21
+ import numpy as np
22
+
23
+ try:
24
+ import cupy as cp
25
+ except (ImportError, ModuleNotFoundError):
26
+ cp = None
27
+
28
+ ###
29
+ ###
30
+ ###
31
+
32
+
33
+ @overload
34
+ def where(
35
+ condition: OperandLike,
36
+ /,
37
+ *,
38
+ device: DeviceLike = "cpu",
39
+ requires_grad: bool = False,
40
+ ) -> Tuple[TensorLike, ...]: ...
41
+
42
+
43
+ @overload
44
+ def where(
45
+ condition: OperandLike,
46
+ x: Optional[OperandLike],
47
+ y: Optional[OperandLike],
48
+ /,
49
+ *,
50
+ device: DeviceLike = "cpu",
51
+ requires_grad: bool = False,
52
+ ) -> TensorLike: ...
53
+
54
+
55
+ def where(
56
+ condition: OperandLike,
57
+ x: Optional[OperandLike] = None,
58
+ y: Optional[OperandLike] = None,
59
+ /,
60
+ *,
61
+ device: Optional[DeviceLike] = None,
62
+ requires_grad: bool = False,
63
+ ) -> Union[Tuple[TensorLike, ...], TensorLike]:
64
+ from ...tensor import Tensor
65
+
66
+ if device is None:
67
+ device = get_device(condition)
68
+
69
+ # * One parameter overload
70
+ if (x is None) and (y is None):
71
+ data = to_xp_array(condition, device=device)
72
+ if device == "cpu":
73
+ result = np.where(data)
74
+ else:
75
+ if cp is None:
76
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
77
+ result = cp.where(data)
78
+ return tuple(
79
+ [
80
+ Tensor(array, device=device, requires_grad=requires_grad)
81
+ for array in result
82
+ ]
83
+ )
84
+ # * Two parameters overload
85
+ elif x is not None and y is not None:
86
+ if (
87
+ not (get_device(x) == get_device(y) == device)
88
+ and not isinstance(x, (int, float, list, tuple))
89
+ and not isinstance(y, (int, float, list, tuple))
90
+ ):
91
+ raise DeviceMismatch(DEVICE_MISMATCH_MSG)
92
+
93
+ data = to_xp_array(condition, device=device)
94
+ x_data = to_xp_array(x, device=device)
95
+ y_data = to_xp_array(y, device=device)
96
+ if device == "cpu":
97
+ result = np.where(data, x_data, y_data)
98
+ else:
99
+ if cp is None:
100
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
101
+ result = cp.where(data, x_data, y_data)
102
+ return Tensor(result, device=device, requires_grad=requires_grad)
103
+ else:
104
+ raise ValueError("Both x and y parameters must be given.")
105
+
106
+
107
+ ###
108
+ ###
109
+ ###
110
+
111
+
112
+ def wrapper_2in_1out(
113
+ x1: OperandLike,
114
+ x2: OperandLike,
115
+ /,
116
+ out: Optional[NDArrayLikeBool] = None,
117
+ *,
118
+ func_name: str,
119
+ device: Optional[DeviceLike] = None,
120
+ where: Union[bool, NDArrayLikeBool] = True,
121
+ casting: Casting = "same_kind",
122
+ order: Order = "K",
123
+ dtype: None = None,
124
+ subok: bool = True,
125
+ ) -> TensorLike:
126
+ from ...tensor import Tensor
127
+
128
+ op_device = get_two_operand_op_device(x1, x2, device)
129
+
130
+ x1, x2 = to_xp_array(x1, device=op_device), to_xp_array(x2, device=op_device)
131
+ if op_device == "cpu":
132
+ y = getattr(np, func_name)(
133
+ x1,
134
+ x2,
135
+ out=out,
136
+ dtype=dtype,
137
+ where=where,
138
+ casting=casting,
139
+ order=order,
140
+ subok=subok,
141
+ )
142
+ else:
143
+ if cp is None:
144
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
145
+ y = getattr(cp, func_name)(x1, x2, out=out, dtype=dtype, casting=casting)
146
+ return Tensor(y, device=op_device)
147
+
148
+
149
+ def equal(
150
+ x1: OperandLike,
151
+ x2: OperandLike,
152
+ /,
153
+ out: Optional[NDArrayLikeBool] = None,
154
+ *,
155
+ device: Optional[DeviceLike] = None,
156
+ where: Union[bool, NDArrayLikeBool] = True,
157
+ casting: Casting = "same_kind",
158
+ order: Order = "K",
159
+ dtype: None = None,
160
+ subok: bool = True,
161
+ ) -> TensorLike:
162
+ return wrapper_2in_1out(
163
+ x1,
164
+ x2,
165
+ out=out,
166
+ func_name="equal",
167
+ device=device,
168
+ where=where,
169
+ casting=casting,
170
+ order=order,
171
+ dtype=dtype,
172
+ subok=subok,
173
+ )
174
+
175
+
176
+ def not_equal(
177
+ x1: OperandLike,
178
+ x2: OperandLike,
179
+ /,
180
+ out: Optional[NDArrayLikeBool] = None,
181
+ *,
182
+ device: Optional[DeviceLike] = None,
183
+ where: Union[bool, NDArrayLikeBool] = True,
184
+ casting: Casting = "same_kind",
185
+ order: Order = "K",
186
+ dtype: None = None,
187
+ subok: bool = True,
188
+ ) -> TensorLike:
189
+ return wrapper_2in_1out(
190
+ x1,
191
+ x2,
192
+ out=out,
193
+ func_name="not_equal",
194
+ device=device,
195
+ where=where,
196
+ casting=casting,
197
+ order=order,
198
+ dtype=dtype,
199
+ subok=subok,
200
+ )
201
+
202
+
203
+ def greater(
204
+ x1: OperandLike,
205
+ x2: OperandLike,
206
+ /,
207
+ out: Optional[NDArrayLikeBool] = None,
208
+ *,
209
+ device: Optional[DeviceLike] = None,
210
+ where: Union[bool, NDArrayLikeBool] = True,
211
+ casting: Casting = "same_kind",
212
+ order: Order = "K",
213
+ dtype: None = None,
214
+ subok: bool = True,
215
+ ) -> TensorLike:
216
+ return wrapper_2in_1out(
217
+ x1,
218
+ x2,
219
+ out=out,
220
+ func_name="greater",
221
+ device=device,
222
+ where=where,
223
+ casting=casting,
224
+ order=order,
225
+ dtype=dtype,
226
+ subok=subok,
227
+ )
228
+
229
+
230
+ def greater_equal(
231
+ x1: OperandLike,
232
+ x2: OperandLike,
233
+ /,
234
+ out: Optional[NDArrayLikeBool] = None,
235
+ *,
236
+ device: Optional[DeviceLike] = None,
237
+ where: Union[bool, NDArrayLikeBool] = True,
238
+ casting: Casting = "same_kind",
239
+ order: Order = "K",
240
+ dtype: None = None,
241
+ subok: bool = True,
242
+ ) -> TensorLike:
243
+ return wrapper_2in_1out(
244
+ x1,
245
+ x2,
246
+ out=out,
247
+ func_name="greater_equal",
248
+ device=device,
249
+ where=where,
250
+ casting=casting,
251
+ order=order,
252
+ dtype=dtype,
253
+ subok=subok,
254
+ )
255
+
256
+
257
+ def less(
258
+ x1: OperandLike,
259
+ x2: OperandLike,
260
+ /,
261
+ out: Optional[NDArrayLikeBool] = None,
262
+ *,
263
+ device: Optional[DeviceLike] = None,
264
+ where: Union[bool, NDArrayLikeBool] = True,
265
+ casting: Casting = "same_kind",
266
+ order: Order = "K",
267
+ dtype: None = None,
268
+ subok: bool = True,
269
+ ) -> TensorLike:
270
+ return wrapper_2in_1out(
271
+ x1,
272
+ x2,
273
+ out=out,
274
+ func_name="less",
275
+ device=device,
276
+ where=where,
277
+ casting=casting,
278
+ order=order,
279
+ dtype=dtype,
280
+ subok=subok,
281
+ )
282
+
283
+
284
+ def less_equal(
285
+ x1: OperandLike,
286
+ x2: OperandLike,
287
+ /,
288
+ out: Optional[NDArrayLikeBool] = None,
289
+ *,
290
+ device: Optional[DeviceLike] = None,
291
+ where: Union[bool, NDArrayLikeBool] = True,
292
+ casting: Casting = "same_kind",
293
+ order: Order = "K",
294
+ dtype: None = None,
295
+ subok: bool = True,
296
+ ) -> TensorLike:
297
+ return wrapper_2in_1out(
298
+ x1,
299
+ x2,
300
+ out=out,
301
+ func_name="less_equal",
302
+ device=device,
303
+ where=where,
304
+ casting=casting,
305
+ order=order,
306
+ dtype=dtype,
307
+ subok=subok,
308
+ )
309
+
310
+
311
+ ###
312
+ ###
313
+ ###
314
+
315
+ __all__ = [
316
+ "where",
317
+ "equal",
318
+ "not_equal",
319
+ "greater",
320
+ "greater_equal",
321
+ "less",
322
+ "less_equal",
323
+ ]
@@ -0,0 +1,93 @@
1
+ from typing import Optional
2
+
3
+ from ...typing import TensorLike, DTypeLike, Order, AxisLike, OperandLike, DeviceLike
4
+
5
+ from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
6
+
7
+ from ..utils import to_xp_array, get_device
8
+
9
+ import numpy as np
10
+
11
+ try:
12
+ import cupy as cp
13
+ except (ImportError, ModuleNotFoundError):
14
+ cp = None
15
+
16
+ ###
17
+ ###
18
+ ###
19
+
20
+
21
+ def zeros_like(
22
+ a: OperandLike,
23
+ /,
24
+ *,
25
+ device: Optional[DeviceLike] = None,
26
+ requires_grad: bool = False,
27
+ dtype: Optional[DTypeLike] = None,
28
+ order: Order = "K",
29
+ subok: bool = True,
30
+ shape: Optional[AxisLike] = None,
31
+ ) -> TensorLike:
32
+ from ...tensor import Tensor
33
+
34
+ if device is None:
35
+ device_op = get_device(a)
36
+ else:
37
+ device_op = device
38
+ a = to_xp_array(a)
39
+ if device_op == "cpu":
40
+ return Tensor(
41
+ np.zeros_like(a, dtype=dtype, order=order, subok=subok, shape=shape),
42
+ device=device_op,
43
+ requires_grad=requires_grad,
44
+ )
45
+ else:
46
+ if cp is None:
47
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
48
+ return Tensor(
49
+ cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape),
50
+ device=device_op,
51
+ requires_grad=requires_grad,
52
+ )
53
+
54
+
55
+ def ones_like(
56
+ a: OperandLike,
57
+ /,
58
+ *,
59
+ device: Optional[DeviceLike] = None,
60
+ requires_grad: bool = False,
61
+ dtype: Optional[DTypeLike] = None,
62
+ order: Order = "K",
63
+ subok: bool = True,
64
+ shape: Optional[AxisLike] = None,
65
+ ) -> TensorLike:
66
+ from ...tensor import Tensor
67
+
68
+ if device is None:
69
+ device_op = get_device(a)
70
+ else:
71
+ device_op = device
72
+ a = to_xp_array(a)
73
+ if device_op == "cpu":
74
+ return Tensor(
75
+ np.ones_like(a, dtype=dtype, order=order, subok=subok, shape=shape),
76
+ device=device_op,
77
+ requires_grad=requires_grad,
78
+ )
79
+ else:
80
+ if cp is None:
81
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
82
+ return Tensor(
83
+ cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape),
84
+ device=device_op,
85
+ requires_grad=requires_grad,
86
+ )
87
+
88
+
89
+ ###
90
+ ###
91
+ ###
92
+
93
+ __all__ = ["zeros_like", "ones_like"]
@@ -1,18 +1,12 @@
1
- from typing import (
2
- Callable
3
- )
1
+ from typing import Callable
4
2
 
5
- from ...typing import (
6
- OperandLike,
7
- TensorLike
8
- )
3
+ from ...typing import OperandLike, TensorLike
9
4
 
10
- from ..utils import sum_to_shape, to_xp_array
11
- from ..exceptions import (
12
- CuPyNotFound, CUPY_NOT_FOUND_MSG
13
- )
5
+ from ..utils import sum_to_shape, to_xp_array, get_two_operand_op_device
6
+ from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
14
7
 
15
8
  import numpy as np
9
+
16
10
  try:
17
11
  import cupy as cp
18
12
  except (ImportError, ModuleNotFoundError):
@@ -22,133 +16,187 @@ except (ImportError, ModuleNotFoundError):
22
16
  ###
23
17
  ###
24
18
 
25
- def wrapper_2in_diff(result: TensorLike, x1: OperandLike, x2: OperandLike, callback_x1: Callable, callback_x2: Callable) -> TensorLike:
26
- if not result.requires_grad: return result
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
27
29
  from ...tensor import Tensor
30
+
28
31
  def backward() -> None:
29
- _x1, _x2 = to_xp_array(x1), to_xp_array(x2)
32
+ device = get_two_operand_op_device(x1, x2, None)
33
+ _x1, _x2 = to_xp_array(x1, device=device), to_xp_array(x2, device=device)
34
+
30
35
  if isinstance(x1, Tensor) and x1.requires_grad:
31
- x1.grad += callback_x1(x1.shape, x1.device, _x1, _x2)
36
+ x1.grad += callback_x1(x1.shape, device, _x1, _x2)
32
37
  if isinstance(x2, Tensor) and x2.requires_grad:
33
- x2.grad += callback_x2(x2.shape, x2.device, _x1, _x2)
38
+ x2.grad += callback_x2(x2.shape, device, _x1, _x2)
39
+
34
40
  result.backward = backward
35
41
  return result
36
42
 
37
- def wrapper_1in_diff(result: TensorLike, x: OperandLike, callback_x1: Callable) -> TensorLike:
38
- if not result.requires_grad: return result
43
+
44
+ def wrapper_1in_diff(
45
+ result: TensorLike, x: OperandLike, callback_x1: Callable
46
+ ) -> TensorLike:
47
+ if not result.requires_grad:
48
+ return result
39
49
  from ...tensor import Tensor
50
+
40
51
  def backward() -> None:
41
52
  _x = to_xp_array(x)
42
53
  if isinstance(x, Tensor) and x.requires_grad:
43
54
  x.grad += callback_x1(x.shape, x.device, _x)
55
+
44
56
  result.backward = backward
45
57
  return result
46
58
 
59
+
47
60
  ###
48
61
  ### Arithmetics
49
62
  ###
50
63
 
64
+
51
65
  def add_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
52
66
  def callback_x1(shape, device, _x1, _x2):
53
67
  return sum_to_shape(result.grad, shape, device)
68
+
54
69
  def callback_x2(shape, device, _x1, _x2):
55
70
  return sum_to_shape(result.grad, shape, device)
71
+
56
72
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
57
73
 
74
+
58
75
  def subtract_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
59
76
  def callback_x1(shape, device, _x1, _x2):
60
77
  return sum_to_shape(result.grad, shape, device)
78
+
61
79
  def callback_x2(shape, device, _x1, _x2):
62
80
  return sum_to_shape(-result.grad, shape, device)
81
+
63
82
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
64
83
 
84
+
65
85
  def multiply_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
66
86
  def callback_x1(shape, device, _x1, _x2):
67
87
  return sum_to_shape(result.grad * _x2, shape, device)
88
+
68
89
  def callback_x2(shape, device, _x1, _x2):
69
90
  return sum_to_shape(result.grad * _x1, shape, device)
91
+
70
92
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
71
93
 
94
+
72
95
  def matmul_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
73
96
  def callback_x1(shape, device, _x1, _x2):
74
- return sum_to_shape(result.grad @ _x2.swapaxes(-2,-1), shape, device)
97
+ return sum_to_shape(result.grad @ _x2.swapaxes(-2, -1), shape, device)
98
+
75
99
  def callback_x2(shape, device, _x1, _x2):
76
100
  return sum_to_shape(_x1.swapaxes(-2, -1) @ result.grad, shape, device)
101
+
77
102
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
78
103
 
104
+
79
105
  def divide_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
80
106
  def callback_x1(shape, device, _x1, _x2):
81
107
  return sum_to_shape(result.grad / _x2, shape, device)
108
+
82
109
  def callback_x2(shape, device, _x1, _x2):
83
- return sum_to_shape(result.grad * (_x1 * -_x2**-2), shape, device)
110
+ return sum_to_shape(result.grad * (_x1 * -(_x2**-2)), shape, device)
111
+
84
112
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
85
113
 
114
+
86
115
  def power_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
87
116
  def callback_x1(shape, device, _x1, _x2):
88
- return sum_to_shape(result.grad * _x2*_x1**(_x2-1), shape, device)
117
+ return sum_to_shape(result.grad * _x2 * _x1 ** (_x2 - 1), shape, device)
118
+
89
119
  def callback_x2(shape, device, _x1, _x2):
90
120
  if device == "cpu":
91
- return sum_to_shape(result.grad * np.log(_x1)*_x1**_x2, shape, device)
121
+ return sum_to_shape(result.grad * np.log(_x1) * _x1**_x2, shape, device)
92
122
  else:
93
- if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
94
- return sum_to_shape(result.grad * cp.log(_x1)*_x1**_x2, shape, device)
123
+ if cp is None:
124
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
125
+ return sum_to_shape(result.grad * cp.log(_x1) * _x1**_x2, shape, device)
126
+
95
127
  return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
96
128
 
129
+
97
130
  def negative_diff(result: TensorLike, x: TensorLike) -> TensorLike:
98
131
  def callback_x1(shape, device, _x):
99
132
  return sum_to_shape(-result.data, shape, device)
133
+
100
134
  return wrapper_1in_diff(result, x, callback_x1)
101
135
 
136
+
102
137
  def sign_diff(result: TensorLike, x: TensorLike) -> TensorLike:
103
138
  def callback_x1(shape, device, _x):
104
139
  return sum_to_shape(result.grad * 0, shape, device)
140
+
105
141
  return wrapper_1in_diff(result, x, callback_x1)
106
142
 
143
+
107
144
  def abs_diff(result: TensorLike, x: TensorLike) -> TensorLike:
108
145
  def callback_x1(shape, device, _x):
109
146
  return sum_to_shape(result.grad * result.data / _x, shape, device)
147
+
110
148
  return wrapper_1in_diff(result, x, callback_x1)
111
149
 
150
+
112
151
  ###
113
152
  ### Exponents/Logarithms
114
153
  ###
115
154
 
155
+
116
156
  def exp_diff(result: TensorLike, x: TensorLike) -> TensorLike:
117
157
  def callback_x1(shape, device, _x):
118
158
  return sum_to_shape(result.grad * result.data, shape, device)
159
+
119
160
  return wrapper_1in_diff(result, x, callback_x1)
120
161
 
162
+
121
163
  def sqrt_diff(result: TensorLike, x: TensorLike) -> TensorLike:
122
164
  def callback_x1(shape, device, _x):
123
- return sum_to_shape(result.grad * (0.5*result.data**(-0.5)), shape, device)
165
+ return sum_to_shape(result.grad * (0.5 * result.data ** (-0.5)), shape, device)
166
+
124
167
  return wrapper_1in_diff(result, x, callback_x1)
125
168
 
169
+
126
170
  def log_diff(result: TensorLike, x: TensorLike) -> TensorLike:
127
171
  def callback_x1(shape, device, _x):
128
172
  return sum_to_shape(result.grad / _x, shape, device)
173
+
129
174
  return wrapper_1in_diff(result, x, callback_x1)
130
175
 
176
+
131
177
  def square_diff(result: TensorLike, x: TensorLike) -> TensorLike:
132
178
  def callback_x1(shape, device, _x):
133
179
  return sum_to_shape(result.grad * 2 * _x, shape, device)
180
+
134
181
  return wrapper_1in_diff(result, x, callback_x1)
135
182
 
183
+
136
184
  ###
137
185
  ###
138
186
  ###
139
187
 
140
188
  __all__ = [
141
- 'add_diff',
142
- 'subtract_diff',
143
- 'multiply_diff',
144
- 'matmul_diff',
145
- 'divide_diff',
146
- 'power_diff',
147
- 'negative_diff',
148
- 'sign_diff',
149
- 'abs_diff',
150
- 'exp_diff',
151
- 'sqrt_diff',
152
- 'log_diff',
153
- 'square_diff'
154
- ]
189
+ "add_diff",
190
+ "subtract_diff",
191
+ "multiply_diff",
192
+ "matmul_diff",
193
+ "divide_diff",
194
+ "power_diff",
195
+ "negative_diff",
196
+ "sign_diff",
197
+ "abs_diff",
198
+ "exp_diff",
199
+ "sqrt_diff",
200
+ "log_diff",
201
+ "square_diff",
202
+ ]