DepthTensor 1.6.0__tar.gz → 1.8.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-1.6.0/src/DepthTensor.egg-info → depthtensor-1.8.0}/PKG-INFO +1 -1
- {depthtensor-1.6.0 → depthtensor-1.8.0}/pyproject.toml +1 -1
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor/__init__.py +1 -1
- depthtensor-1.8.0/src/DepthTensor/_core/ops/diff.py +151 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor/_core/ops/elementwise.py +16 -8
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor/_core/random/generator.py +18 -12
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor/_core/utils.py +13 -3
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor/autodiff.py +1 -1
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor/tensor.py +2 -1
- {depthtensor-1.6.0 → depthtensor-1.8.0/src/DepthTensor.egg-info}/PKG-INFO +1 -1
- depthtensor-1.8.0/src/example/example.py +9 -0
- depthtensor-1.6.0/src/DepthTensor/_core/ops/diff.py +0 -164
- depthtensor-1.6.0/src/example/example.py +0 -5
- {depthtensor-1.6.0 → depthtensor-1.8.0}/LICENSE +0 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/README.md +0 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/setup.cfg +0 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor/_core/__init__.py +0 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor/_core/exceptions.py +0 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor/_core/ops/__init__.py +0 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor/_core/ops/comparison.py +0 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor/_core/ops/creation.py +0 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor/_core/ops/reduction.py +0 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor/_core/random/__init__.py +0 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor/typing.py +0 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor.egg-info/SOURCES.txt +0 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor.egg-info/dependency_links.txt +0 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor.egg-info/requires.txt +0 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/src/DepthTensor.egg-info/top_level.txt +0 -0
- {depthtensor-1.6.0 → depthtensor-1.8.0}/tests/test_tensor.py +0 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
from typing import (
|
|
2
|
+
Callable
|
|
3
|
+
)
|
|
4
|
+
|
|
5
|
+
from ...typing import (
|
|
6
|
+
OperandLike,
|
|
7
|
+
TensorLike
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
from ..utils import sum_to_shape, to_xp_array
|
|
11
|
+
from ..exceptions import (
|
|
12
|
+
CuPyNotFound, CUPY_NOT_FOUND_MSG
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
import numpy as np
|
|
16
|
+
try:
|
|
17
|
+
import cupy as cp
|
|
18
|
+
except (ImportError, ModuleNotFoundError):
|
|
19
|
+
cp = None
|
|
20
|
+
|
|
21
|
+
###
|
|
22
|
+
### Arithmetics
|
|
23
|
+
###
|
|
24
|
+
|
|
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
|
|
27
|
+
from ...tensor import Tensor
|
|
28
|
+
def backward() -> None:
|
|
29
|
+
_x1, _x2 = to_xp_array(x1), to_xp_array(x2)
|
|
30
|
+
if isinstance(x1, Tensor) and x1.requires_grad:
|
|
31
|
+
x1.grad += callback_x1(x1.shape, x1.device, _x1, _x2)
|
|
32
|
+
if isinstance(x2, Tensor) and x2.requires_grad:
|
|
33
|
+
x2.grad += callback_x2(x2.shape, x2.device, _x1, _x2)
|
|
34
|
+
result.backward = backward
|
|
35
|
+
return result
|
|
36
|
+
|
|
37
|
+
def wrapper_1in_diff(result: TensorLike, x: OperandLike, callback_x1: Callable) -> TensorLike:
|
|
38
|
+
if not result.requires_grad: return result
|
|
39
|
+
from ...tensor import Tensor
|
|
40
|
+
def backward() -> None:
|
|
41
|
+
_x = to_xp_array(x)
|
|
42
|
+
if isinstance(x, Tensor) and x.requires_grad:
|
|
43
|
+
x.grad += callback_x1(x.shape, x.device, _x)
|
|
44
|
+
result.backward = backward
|
|
45
|
+
return result
|
|
46
|
+
|
|
47
|
+
def add_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
48
|
+
def callback_x1(shape, device, _x1, _x2):
|
|
49
|
+
return sum_to_shape(result.grad, shape, device)
|
|
50
|
+
def callback_x2(shape, device, _x1, _x2):
|
|
51
|
+
return sum_to_shape(result.grad, shape, device)
|
|
52
|
+
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
53
|
+
|
|
54
|
+
def subtract_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
55
|
+
def callback_x1(shape, device, _x1, _x2):
|
|
56
|
+
return sum_to_shape(result.grad, shape, device)
|
|
57
|
+
def callback_x2(shape, device, _x1, _x2):
|
|
58
|
+
return sum_to_shape(-result.grad, shape, device)
|
|
59
|
+
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
60
|
+
|
|
61
|
+
def multiply_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
62
|
+
def callback_x1(shape, device, _x1, _x2):
|
|
63
|
+
return sum_to_shape(result.grad * _x2, shape, device)
|
|
64
|
+
def callback_x2(shape, device, _x1, _x2):
|
|
65
|
+
return sum_to_shape(result.grad * _x1, shape, device)
|
|
66
|
+
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
67
|
+
|
|
68
|
+
def matmul_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
69
|
+
def callback_x1(shape, device, _x1, _x2):
|
|
70
|
+
return sum_to_shape(result.grad @ _x2.swapaxes(-2,-1), shape, device)
|
|
71
|
+
def callback_x2(shape, device, _x1, _x2):
|
|
72
|
+
return sum_to_shape(_x1.swapaxes(-2, -1) @ result.grad, shape, device)
|
|
73
|
+
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
74
|
+
|
|
75
|
+
def divide_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
76
|
+
def callback_x1(shape, device, _x1, _x2):
|
|
77
|
+
return sum_to_shape(result.grad / _x2, shape, device)
|
|
78
|
+
def callback_x2(shape, device, _x1, _x2):
|
|
79
|
+
return sum_to_shape(result.grad * (_x1 * -_x2**-2), shape, device)
|
|
80
|
+
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
81
|
+
|
|
82
|
+
def power_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
83
|
+
def callback_x1(shape, device, _x1, _x2):
|
|
84
|
+
return sum_to_shape(result.grad * _x2*_x1**(_x2-1), shape, device)
|
|
85
|
+
def callback_x2(shape, device, _x1, _x2):
|
|
86
|
+
if device == "cpu":
|
|
87
|
+
return sum_to_shape(result.grad * np.log(_x1)*_x1**_x2, shape, device)
|
|
88
|
+
else:
|
|
89
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
90
|
+
return sum_to_shape(result.grad * cp.log(_x1)*_x1**_x2, shape, device)
|
|
91
|
+
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
92
|
+
|
|
93
|
+
def negative_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
94
|
+
def callback_x1(shape, device, _x):
|
|
95
|
+
return sum_to_shape(-result.data, shape, device)
|
|
96
|
+
return wrapper_1in_diff(result, x, callback_x1)
|
|
97
|
+
|
|
98
|
+
def sign_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
99
|
+
def callback_x1(shape, device, _x):
|
|
100
|
+
return sum_to_shape(result.grad * 0, shape, device)
|
|
101
|
+
return wrapper_1in_diff(result, x, callback_x1)
|
|
102
|
+
|
|
103
|
+
def abs_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
104
|
+
def callback_x1(shape, device, _x):
|
|
105
|
+
return sum_to_shape(result.grad * result.data / _x, shape, device)
|
|
106
|
+
return wrapper_1in_diff(result, x, callback_x1)
|
|
107
|
+
|
|
108
|
+
###
|
|
109
|
+
### Exponents/Logarithms
|
|
110
|
+
###
|
|
111
|
+
|
|
112
|
+
def exp_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
113
|
+
def callback_x1(shape, device, _x):
|
|
114
|
+
return sum_to_shape(result.grad * result.data, shape, device)
|
|
115
|
+
return wrapper_1in_diff(result, x, callback_x1)
|
|
116
|
+
|
|
117
|
+
def sqrt_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
118
|
+
def callback_x1(shape, device, _x):
|
|
119
|
+
return sum_to_shape(result.grad * (0.5*result.data**(-0.5)), shape, device)
|
|
120
|
+
return wrapper_1in_diff(result, x, callback_x1)
|
|
121
|
+
|
|
122
|
+
def log_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
123
|
+
def callback_x1(shape, device, _x):
|
|
124
|
+
return sum_to_shape(result.grad / _x, shape, device)
|
|
125
|
+
return wrapper_1in_diff(result, x, callback_x1)
|
|
126
|
+
|
|
127
|
+
def square_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
128
|
+
def callback_x1(shape, device, _x):
|
|
129
|
+
return sum_to_shape(result.grad * 2 * _x, shape, device)
|
|
130
|
+
return wrapper_1in_diff(result, x, callback_x1)
|
|
131
|
+
|
|
132
|
+
###
|
|
133
|
+
###
|
|
134
|
+
###
|
|
135
|
+
|
|
136
|
+
__all__ = [
|
|
137
|
+
'add_diff',
|
|
138
|
+
'subtract_diff',
|
|
139
|
+
'multiply_diff',
|
|
140
|
+
'matmul_diff',
|
|
141
|
+
'divide_diff',
|
|
142
|
+
'power_diff',
|
|
143
|
+
'negative_diff',
|
|
144
|
+
'sign_diff',
|
|
145
|
+
'abs_diff',
|
|
146
|
+
|
|
147
|
+
'exp_diff',
|
|
148
|
+
'sqrt_diff',
|
|
149
|
+
'log_diff',
|
|
150
|
+
'square_diff'
|
|
151
|
+
]
|
|
@@ -78,11 +78,19 @@ def wrapper_2in_1out(
|
|
|
78
78
|
x1.data = y
|
|
79
79
|
return x1
|
|
80
80
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
requires_grad =
|
|
85
|
-
|
|
81
|
+
requires_grad = False
|
|
82
|
+
prev = ()
|
|
83
|
+
if isinstance(x1, Tensor):
|
|
84
|
+
requires_grad = x1.requires_grad
|
|
85
|
+
prev = (x1,)
|
|
86
|
+
if isinstance(x2, Tensor):
|
|
87
|
+
requires_grad = requires_grad or x2.requires_grad
|
|
88
|
+
if len(prev) == 1:
|
|
89
|
+
prev = (x1, x2)
|
|
90
|
+
else:
|
|
91
|
+
prev = (x2,)
|
|
92
|
+
|
|
93
|
+
return Tensor(y, device=device_op, prev=prev, requires_grad=requires_grad)
|
|
86
94
|
|
|
87
95
|
def wrapper_1in_1out(
|
|
88
96
|
x: OperandLike,
|
|
@@ -111,13 +119,12 @@ def wrapper_1in_1out(
|
|
|
111
119
|
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
112
120
|
y = getattr(cp, func_name)(a, out=out, dtype=dtype, casting=casting)
|
|
113
121
|
|
|
122
|
+
requires_grad = False
|
|
114
123
|
if isinstance(x, Tensor):
|
|
115
124
|
if in_place:
|
|
116
125
|
x.data = y
|
|
117
126
|
return x
|
|
118
127
|
requires_grad = x.requires_grad
|
|
119
|
-
else:
|
|
120
|
-
requires_grad = False
|
|
121
128
|
return Tensor(y, device=device_op, prev=(x,), requires_grad=requires_grad)
|
|
122
129
|
|
|
123
130
|
def add(
|
|
@@ -268,6 +275,7 @@ def clip(
|
|
|
268
275
|
/,
|
|
269
276
|
out: Optional[ArrayLike] = None,
|
|
270
277
|
*,
|
|
278
|
+
requires_grad: bool = False,
|
|
271
279
|
device: DeviceLike = "cpu",
|
|
272
280
|
where: Union[bool, ArrayLikeBool] = True,
|
|
273
281
|
casting: Casting = 'same_kind',
|
|
@@ -295,7 +303,7 @@ def clip(
|
|
|
295
303
|
else:
|
|
296
304
|
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
297
305
|
y = cp.clip(arr_a, arr_min, arr_max, out=out)
|
|
298
|
-
return Tensor(y)
|
|
306
|
+
return Tensor(y, requires_grad=requires_grad)
|
|
299
307
|
|
|
300
308
|
###
|
|
301
309
|
### Exponents/Logarithms
|
|
@@ -28,17 +28,19 @@ except (ImportError, ModuleNotFoundError):
|
|
|
28
28
|
###
|
|
29
29
|
|
|
30
30
|
@overload
|
|
31
|
-
def rand(*, device: DeviceLike = "cpu") -> TensorLike: ...
|
|
31
|
+
def rand(*, device: DeviceLike = "cpu", requires_grad: bool = False) -> TensorLike: ...
|
|
32
32
|
@overload
|
|
33
33
|
def rand(
|
|
34
34
|
*d: int,
|
|
35
35
|
dtype: Optional[DTypeLike] = None,
|
|
36
|
-
device: DeviceLike = "cpu"
|
|
36
|
+
device: DeviceLike = "cpu",
|
|
37
|
+
requires_grad: bool = False
|
|
37
38
|
) -> TensorLike: ...
|
|
38
39
|
def rand(
|
|
39
40
|
*d: int,
|
|
40
41
|
dtype: Optional[DTypeLike] = None,
|
|
41
|
-
device: DeviceLike = "cpu"
|
|
42
|
+
device: DeviceLike = "cpu",
|
|
43
|
+
requires_grad: bool = False
|
|
42
44
|
) -> TensorLike:
|
|
43
45
|
from ...tensor import Tensor
|
|
44
46
|
if device == "cpu":
|
|
@@ -46,20 +48,22 @@ def rand(
|
|
|
46
48
|
else:
|
|
47
49
|
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
48
50
|
y = cp.random.rand(*d, dtype=dtype)
|
|
49
|
-
return Tensor(y, dtype=dtype)
|
|
51
|
+
return Tensor(y, dtype=dtype, requires_grad=requires_grad)
|
|
50
52
|
|
|
51
53
|
@overload
|
|
52
|
-
def randn(*, device: DeviceLike = "cpu") -> TensorLike: ...
|
|
54
|
+
def randn(*, device: DeviceLike = "cpu", requires_grad: bool = False) -> TensorLike: ...
|
|
53
55
|
@overload
|
|
54
56
|
def randn(
|
|
55
57
|
*d: int,
|
|
56
58
|
dtype: Optional[DTypeLike] = None,
|
|
57
|
-
device: DeviceLike = "cpu"
|
|
59
|
+
device: DeviceLike = "cpu",
|
|
60
|
+
requires_grad: bool = False
|
|
58
61
|
) -> TensorLike: ...
|
|
59
62
|
def randn(
|
|
60
63
|
*d: int,
|
|
61
64
|
dtype: Optional[DTypeLike] = None,
|
|
62
|
-
device: DeviceLike = "cpu"
|
|
65
|
+
device: DeviceLike = "cpu",
|
|
66
|
+
requires_grad: bool = False
|
|
63
67
|
) -> TensorLike:
|
|
64
68
|
from ...tensor import Tensor
|
|
65
69
|
if device == "cpu":
|
|
@@ -67,14 +71,15 @@ def randn(
|
|
|
67
71
|
else:
|
|
68
72
|
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
69
73
|
y = cp.random.randn(*d, dtype=dtype)
|
|
70
|
-
return Tensor(y, dtype=dtype)
|
|
74
|
+
return Tensor(y, dtype=dtype, requires_grad=requires_grad)
|
|
71
75
|
|
|
72
76
|
def randint(
|
|
73
77
|
low: int,
|
|
74
78
|
high: Optional[int] = None,
|
|
75
79
|
size: Optional[AxisLike] = None,
|
|
76
80
|
dtype: Any = int64,
|
|
77
|
-
device: DeviceLike = "cpu"
|
|
81
|
+
device: DeviceLike = "cpu",
|
|
82
|
+
requires_grad: bool = False
|
|
78
83
|
) -> TensorLike:
|
|
79
84
|
from ...tensor import Tensor
|
|
80
85
|
if device == "cpu":
|
|
@@ -82,14 +87,15 @@ def randint(
|
|
|
82
87
|
else:
|
|
83
88
|
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
84
89
|
y = cp.random.randint(low=low, high=high, size=size, dtype=dtype)
|
|
85
|
-
return Tensor(y)
|
|
90
|
+
return Tensor(y, requires_grad=requires_grad)
|
|
86
91
|
|
|
87
92
|
def uniform(
|
|
88
93
|
low: float = 0.0,
|
|
89
94
|
high: float = 1.0,
|
|
90
95
|
size: Optional[AxisLike] = None,
|
|
91
96
|
*,
|
|
92
|
-
device: DeviceLike = "cpu"
|
|
97
|
+
device: DeviceLike = "cpu",
|
|
98
|
+
requires_grad: bool = False
|
|
93
99
|
):
|
|
94
100
|
from ...tensor import Tensor
|
|
95
101
|
if device == "cpu":
|
|
@@ -97,7 +103,7 @@ def uniform(
|
|
|
97
103
|
else:
|
|
98
104
|
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
99
105
|
y = cp.random.uniform(low=low, high=high, size=size)
|
|
100
|
-
return Tensor(y)
|
|
106
|
+
return Tensor(y, requires_grad=requires_grad)
|
|
101
107
|
|
|
102
108
|
###
|
|
103
109
|
###
|
|
@@ -95,7 +95,7 @@ def sum_to_shape(result: Any, target_shape: Tuple, device: DeviceLike) -> Any:
|
|
|
95
95
|
|
|
96
96
|
def to_xp_array(a: Union[ArrayLike, TensorLike], device: Optional[DeviceLike] = None) -> ArrayLike:
|
|
97
97
|
"""
|
|
98
|
-
Convert data to numpy.
|
|
98
|
+
Convert data to numpy.ndarray or cp.ndarray
|
|
99
99
|
"""
|
|
100
100
|
from ..tensor import Tensor
|
|
101
101
|
if isinstance(a, Tensor):
|
|
@@ -107,15 +107,25 @@ def to_xp_array(a: Union[ArrayLike, TensorLike], device: Optional[DeviceLike] =
|
|
|
107
107
|
if device == "cpu":
|
|
108
108
|
return y
|
|
109
109
|
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
110
|
-
return cp.
|
|
110
|
+
return cp.array(y)
|
|
111
111
|
else:
|
|
112
112
|
if cp is not None and isinstance(y, cp.ndarray):
|
|
113
113
|
if device == "gpu":
|
|
114
114
|
return y
|
|
115
115
|
return cp.asnumpy(y)
|
|
116
116
|
else:
|
|
117
|
+
if device == "cpu":
|
|
118
|
+
return np.array(y)
|
|
119
|
+
else:
|
|
120
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
121
|
+
return cp.array(y)
|
|
122
|
+
else:
|
|
123
|
+
if isinstance(y, np.ndarray):
|
|
124
|
+
return y
|
|
125
|
+
else:
|
|
126
|
+
if cp is not None and isinstance(y, cp.ndarray):
|
|
117
127
|
return y
|
|
118
|
-
|
|
128
|
+
return np.array(y)
|
|
119
129
|
|
|
120
130
|
###
|
|
121
131
|
###
|
|
@@ -416,6 +416,7 @@ class Tensor():
|
|
|
416
416
|
/,
|
|
417
417
|
out: Optional[ArrayLike] = None,
|
|
418
418
|
*,
|
|
419
|
+
requires_grad: bool = False,
|
|
419
420
|
device: DeviceLike = "cpu",
|
|
420
421
|
where: Union[bool, ArrayLikeBool] = True,
|
|
421
422
|
casting: Casting = 'same_kind',
|
|
@@ -423,7 +424,7 @@ class Tensor():
|
|
|
423
424
|
dtype: Optional[DTypeLike] = None,
|
|
424
425
|
subok: bool = True
|
|
425
426
|
) -> Tensor:
|
|
426
|
-
return clip(a, a_min, a_max, out=out, device=device, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
|
|
427
|
+
return clip(a, a_min, a_max, out=out, requires_grad=requires_grad, device=device, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
|
|
427
428
|
|
|
428
429
|
@staticmethod
|
|
429
430
|
def sign(
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
from ...typing import (
|
|
2
|
-
TensorLike
|
|
3
|
-
)
|
|
4
|
-
|
|
5
|
-
from ..utils import sum_to_shape
|
|
6
|
-
from ..exceptions import (
|
|
7
|
-
CuPyNotFound, CUPY_NOT_FOUND_MSG
|
|
8
|
-
)
|
|
9
|
-
|
|
10
|
-
import numpy as np
|
|
11
|
-
try:
|
|
12
|
-
import cupy as cp
|
|
13
|
-
except (ImportError, ModuleNotFoundError):
|
|
14
|
-
cp = None
|
|
15
|
-
|
|
16
|
-
###
|
|
17
|
-
### Arithmetics
|
|
18
|
-
###
|
|
19
|
-
|
|
20
|
-
def add_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
|
|
21
|
-
if not result.requires_grad: return result
|
|
22
|
-
def backward() -> None:
|
|
23
|
-
if x1.requires_grad:
|
|
24
|
-
x1.grad += sum_to_shape(result.grad, x1.shape, x1.device)
|
|
25
|
-
if x2.requires_grad:
|
|
26
|
-
x2.grad += sum_to_shape(result.grad, x2.shape, x2.device)
|
|
27
|
-
result.backward = backward
|
|
28
|
-
return result
|
|
29
|
-
|
|
30
|
-
def subtract_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
|
|
31
|
-
if not result.requires_grad: return result
|
|
32
|
-
def backward() -> None:
|
|
33
|
-
if x1.requires_grad:
|
|
34
|
-
x1.grad += sum_to_shape(result.grad, x1.shape, x1.device)
|
|
35
|
-
if x2.requires_grad:
|
|
36
|
-
x2.grad += sum_to_shape(-result.grad, x2.shape, x2.device)
|
|
37
|
-
result.backward = backward
|
|
38
|
-
return result
|
|
39
|
-
|
|
40
|
-
def multiply_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
|
|
41
|
-
if not result.requires_grad: return result
|
|
42
|
-
def backward() -> None:
|
|
43
|
-
if x1.requires_grad:
|
|
44
|
-
x1.grad += sum_to_shape(result.grad * x2.data, x1.shape, x1.device)
|
|
45
|
-
if x2.requires_grad:
|
|
46
|
-
x2.grad += sum_to_shape(result.grad * x1.data, x2.shape, x2.device)
|
|
47
|
-
result.backward = backward
|
|
48
|
-
return result
|
|
49
|
-
|
|
50
|
-
def matmul_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
|
|
51
|
-
if not result.requires_grad: return result
|
|
52
|
-
def backward() -> None:
|
|
53
|
-
if x1.requires_grad:
|
|
54
|
-
x1.grad += sum_to_shape(result.grad @ x2.data.swapaxes(-2,-1), x1.shape, x1.device)
|
|
55
|
-
if x2.requires_grad:
|
|
56
|
-
x2.grad += sum_to_shape(x1.data.swapaxes(-2, -1) @ result.grad, x2.shape, x2.device)
|
|
57
|
-
result.backward = backward
|
|
58
|
-
return result
|
|
59
|
-
|
|
60
|
-
def divide_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
|
|
61
|
-
if not result.requires_grad: return result
|
|
62
|
-
def backward() -> None:
|
|
63
|
-
if x1.requires_grad:
|
|
64
|
-
x1.grad += sum_to_shape(result.grad / x2.data, x1.shape, x1.device)
|
|
65
|
-
if x2.requires_grad:
|
|
66
|
-
x2.grad += sum_to_shape(result.grad * (x1.data * -x2.data**-2), x2.shape, x2.device)
|
|
67
|
-
result.backward = backward
|
|
68
|
-
return result
|
|
69
|
-
|
|
70
|
-
def power_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
|
|
71
|
-
if not result.requires_grad: return result
|
|
72
|
-
def backward() -> None:
|
|
73
|
-
if x1.requires_grad:
|
|
74
|
-
x1.grad += sum_to_shape(result.grad * x2.data*x1.data**(x2.data-1), x1.shape, x1.device)
|
|
75
|
-
if x2.requires_grad:
|
|
76
|
-
if x2.device == "cpu":
|
|
77
|
-
x2.grad += sum_to_shape(result.grad * np.log(x1.data)*x1.data**x2.data, x2.shape, x2.device)
|
|
78
|
-
else:
|
|
79
|
-
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
80
|
-
x2.grad += sum_to_shape(result.grad * cp.log(x1.data)*x1.data**x2.data, x2.shape, x2.device)
|
|
81
|
-
result.backward = backward
|
|
82
|
-
return result
|
|
83
|
-
|
|
84
|
-
def negative_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
85
|
-
if not result.requires_grad: return result
|
|
86
|
-
def backward() -> None:
|
|
87
|
-
if x.requires_grad:
|
|
88
|
-
x.grad += sum_to_shape(-result.grad, x.shape, x.device)
|
|
89
|
-
result.backward = backward
|
|
90
|
-
return result
|
|
91
|
-
|
|
92
|
-
def sign_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
93
|
-
if not result.requires_grad: return result
|
|
94
|
-
def backward() -> None:
|
|
95
|
-
if x.requires_grad:
|
|
96
|
-
x.grad += sum_to_shape(result.grad * 0, x.shape, x.device)
|
|
97
|
-
result.backward = backward
|
|
98
|
-
return result
|
|
99
|
-
|
|
100
|
-
def abs_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
101
|
-
if not result.requires_grad: return result
|
|
102
|
-
def backward() -> None:
|
|
103
|
-
if x.requires_grad:
|
|
104
|
-
# TODO: Handle jump discontinuity
|
|
105
|
-
x.grad += sum_to_shape(result.grad * result.data / x.data, x.shape, x.device)
|
|
106
|
-
result.backward = backward
|
|
107
|
-
return result
|
|
108
|
-
|
|
109
|
-
###
|
|
110
|
-
### Exponents/Logarithms
|
|
111
|
-
###
|
|
112
|
-
|
|
113
|
-
def exp_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
114
|
-
if not result.requires_grad: return result
|
|
115
|
-
def backward() -> None:
|
|
116
|
-
if x.requires_grad:
|
|
117
|
-
x.grad += sum_to_shape(result.grad * result.data, x.shape, x.device)
|
|
118
|
-
result.backward = backward
|
|
119
|
-
return result
|
|
120
|
-
|
|
121
|
-
def sqrt_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
122
|
-
if not result.requires_grad: return result
|
|
123
|
-
def backward() -> None:
|
|
124
|
-
if x.requires_grad:
|
|
125
|
-
x.grad += sum_to_shape(result.grad * (0.5*result.data**(-0.5)), x.shape, x.device)
|
|
126
|
-
result.backward = backward
|
|
127
|
-
return result
|
|
128
|
-
|
|
129
|
-
def log_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
130
|
-
if not result.requires_grad: return result
|
|
131
|
-
def backward() -> None:
|
|
132
|
-
if x.requires_grad:
|
|
133
|
-
x.grad += sum_to_shape(result.grad / x.data, x.shape, x.device)
|
|
134
|
-
result.backward = backward
|
|
135
|
-
return result
|
|
136
|
-
|
|
137
|
-
def square_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
138
|
-
if not result.requires_grad: return result
|
|
139
|
-
def backward() -> None:
|
|
140
|
-
if x.requires_grad:
|
|
141
|
-
x.grad += sum_to_shape(result.grad * 2, x.shape, x.device)
|
|
142
|
-
result.backward = backward
|
|
143
|
-
return result
|
|
144
|
-
|
|
145
|
-
###
|
|
146
|
-
###
|
|
147
|
-
###
|
|
148
|
-
|
|
149
|
-
__all__ = [
|
|
150
|
-
'add_diff',
|
|
151
|
-
'subtract_diff',
|
|
152
|
-
'multiply_diff',
|
|
153
|
-
'matmul_diff',
|
|
154
|
-
'divide_diff',
|
|
155
|
-
'power_diff',
|
|
156
|
-
'negative_diff',
|
|
157
|
-
'sign_diff',
|
|
158
|
-
'abs_diff',
|
|
159
|
-
|
|
160
|
-
'exp_diff',
|
|
161
|
-
'sqrt_diff',
|
|
162
|
-
'log_diff',
|
|
163
|
-
'square_diff'
|
|
164
|
-
]
|
|
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
|