DepthTensor 2.0.2__tar.gz → 2.2.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.0.2/src/DepthTensor.egg-info → depthtensor-2.2.0}/PKG-INFO +1 -1
- {depthtensor-2.0.2 → depthtensor-2.2.0}/pyproject.toml +1 -1
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor/__init__.py +1 -1
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor/_core/ops/comparison.py +3 -8
- depthtensor-2.2.0/src/DepthTensor/_core/ops/creation.py +126 -0
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor/_core/ops/diff.py +73 -43
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor/_core/ops/elementwise.py +31 -37
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor/_core/ops/reduction.py +6 -4
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor/_core/random/generator.py +21 -5
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor/autodiff.py +17 -8
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor/tensor.py +18 -5
- {depthtensor-2.0.2 → depthtensor-2.2.0/src/DepthTensor.egg-info}/PKG-INFO +1 -1
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor.egg-info/SOURCES.txt +1 -3
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor.egg-info/top_level.txt +0 -1
- depthtensor-2.0.2/src/DepthTensor/_core/ops/creation.py +0 -93
- depthtensor-2.0.2/src/example/example.py +0 -8
- depthtensor-2.0.2/src/example/test.py +0 -10
- {depthtensor-2.0.2 → depthtensor-2.2.0}/LICENSE +0 -0
- {depthtensor-2.0.2 → depthtensor-2.2.0}/README.md +0 -0
- {depthtensor-2.0.2 → depthtensor-2.2.0}/setup.cfg +0 -0
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor/_core/__init__.py +0 -0
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor/_core/exceptions.py +0 -0
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor/_core/ops/__init__.py +0 -0
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor/_core/random/__init__.py +0 -0
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor/_core/utils.py +2 -2
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor/typing.py +0 -0
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor.egg-info/dependency_links.txt +0 -0
- {depthtensor-2.0.2 → depthtensor-2.2.0}/src/DepthTensor.egg-info/requires.txt +0 -0
|
@@ -75,12 +75,7 @@ def where(
|
|
|
75
75
|
if cp is None:
|
|
76
76
|
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
77
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
|
-
)
|
|
78
|
+
return tuple([Tensor(array, requires_grad=requires_grad) for array in result])
|
|
84
79
|
# * Two parameters overload
|
|
85
80
|
elif x is not None and y is not None:
|
|
86
81
|
if (
|
|
@@ -99,7 +94,7 @@ def where(
|
|
|
99
94
|
if cp is None:
|
|
100
95
|
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
101
96
|
result = cp.where(data, x_data, y_data)
|
|
102
|
-
return Tensor(result,
|
|
97
|
+
return Tensor(result, requires_grad=requires_grad)
|
|
103
98
|
else:
|
|
104
99
|
raise ValueError("Both x and y parameters must be given.")
|
|
105
100
|
|
|
@@ -143,7 +138,7 @@ def wrapper_2in_1out(
|
|
|
143
138
|
if cp is None:
|
|
144
139
|
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
145
140
|
y = getattr(cp, func_name)(x1, x2, out=out, dtype=dtype, casting=casting)
|
|
146
|
-
return Tensor(y
|
|
141
|
+
return Tensor(y)
|
|
147
142
|
|
|
148
143
|
|
|
149
144
|
def equal(
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
from typing import Optional, Literal, Union
|
|
2
|
+
|
|
3
|
+
from ...typing import (
|
|
4
|
+
TensorLike,
|
|
5
|
+
DTypeLike,
|
|
6
|
+
Order,
|
|
7
|
+
AxisLike,
|
|
8
|
+
OperandLike,
|
|
9
|
+
DeviceLike,
|
|
10
|
+
ShapeLike,
|
|
11
|
+
NDArrayLike,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
|
|
15
|
+
|
|
16
|
+
from ..utils import to_xp_array, get_device
|
|
17
|
+
|
|
18
|
+
import numpy as np
|
|
19
|
+
|
|
20
|
+
try:
|
|
21
|
+
import cupy as cp
|
|
22
|
+
except (ImportError, ModuleNotFoundError):
|
|
23
|
+
cp = None
|
|
24
|
+
|
|
25
|
+
###
|
|
26
|
+
###
|
|
27
|
+
###
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def zeros_like(
|
|
31
|
+
a: OperandLike,
|
|
32
|
+
/,
|
|
33
|
+
*,
|
|
34
|
+
device: Optional[DeviceLike] = None,
|
|
35
|
+
requires_grad: bool = False,
|
|
36
|
+
dtype: Optional[DTypeLike] = None,
|
|
37
|
+
order: Order = "K",
|
|
38
|
+
subok: bool = True,
|
|
39
|
+
shape: Optional[AxisLike] = None,
|
|
40
|
+
) -> TensorLike:
|
|
41
|
+
from ...tensor import Tensor
|
|
42
|
+
|
|
43
|
+
if device is None:
|
|
44
|
+
device_op = get_device(a)
|
|
45
|
+
else:
|
|
46
|
+
device_op = device
|
|
47
|
+
a = to_xp_array(a)
|
|
48
|
+
if device_op == "cpu":
|
|
49
|
+
y = np.zeros_like(a, dtype=dtype, order=order, subok=subok, shape=shape)
|
|
50
|
+
else:
|
|
51
|
+
if cp is None:
|
|
52
|
+
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
53
|
+
y = cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape)
|
|
54
|
+
return Tensor(y, requires_grad=requires_grad)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def ones_like(
|
|
58
|
+
a: OperandLike,
|
|
59
|
+
/,
|
|
60
|
+
*,
|
|
61
|
+
device: Optional[DeviceLike] = None,
|
|
62
|
+
requires_grad: bool = False,
|
|
63
|
+
dtype: Optional[DTypeLike] = None,
|
|
64
|
+
order: Order = "K",
|
|
65
|
+
subok: bool = True,
|
|
66
|
+
shape: Optional[AxisLike] = None,
|
|
67
|
+
) -> TensorLike:
|
|
68
|
+
from ...tensor import Tensor
|
|
69
|
+
|
|
70
|
+
if device is None:
|
|
71
|
+
device_op = get_device(a)
|
|
72
|
+
else:
|
|
73
|
+
device_op = device
|
|
74
|
+
a = to_xp_array(a)
|
|
75
|
+
if device_op == "cpu":
|
|
76
|
+
y = np.ones_like(a, dtype=dtype, order=order, subok=subok, shape=shape)
|
|
77
|
+
else:
|
|
78
|
+
if cp is None:
|
|
79
|
+
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
80
|
+
y = cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape)
|
|
81
|
+
return Tensor(y, requires_grad=requires_grad)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def zeros(
|
|
85
|
+
shape: ShapeLike,
|
|
86
|
+
dtype: DTypeLike = float,
|
|
87
|
+
order: Literal["C", "F"] = "C",
|
|
88
|
+
*,
|
|
89
|
+
device: DeviceLike = "cpu",
|
|
90
|
+
requires_grad: bool = False,
|
|
91
|
+
) -> TensorLike:
|
|
92
|
+
from ...tensor import Tensor
|
|
93
|
+
|
|
94
|
+
if device == "cpu":
|
|
95
|
+
y = np.zeros(shape=shape, dtype=dtype, order=order)
|
|
96
|
+
else:
|
|
97
|
+
if cp is None:
|
|
98
|
+
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
99
|
+
y = cp.zeros(shape=shape, dtype=dtype, order=order)
|
|
100
|
+
return Tensor(y, requires_grad=requires_grad)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def ones(
|
|
104
|
+
shape: ShapeLike,
|
|
105
|
+
dtype: DTypeLike = float,
|
|
106
|
+
order: Literal["C", "F"] = "C",
|
|
107
|
+
*,
|
|
108
|
+
device: DeviceLike = "cpu",
|
|
109
|
+
requires_grad: bool = False,
|
|
110
|
+
) -> TensorLike:
|
|
111
|
+
from ...tensor import Tensor
|
|
112
|
+
|
|
113
|
+
if device == "cpu":
|
|
114
|
+
y = np.ones(shape=shape, dtype=dtype, order=order)
|
|
115
|
+
else:
|
|
116
|
+
if cp is None:
|
|
117
|
+
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
118
|
+
y = cp.ones(shape=shape, dtype=dtype, order=order)
|
|
119
|
+
return Tensor(y, requires_grad=requires_grad)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
###
|
|
123
|
+
###
|
|
124
|
+
###
|
|
125
|
+
|
|
126
|
+
__all__ = ["zeros_like", "ones_like", "zeros", "ones"]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from typing import Callable
|
|
2
2
|
|
|
3
|
-
from ...typing import OperandLike, TensorLike
|
|
3
|
+
from ...typing import OperandLike, TensorLike, NDArrayLike
|
|
4
4
|
|
|
5
5
|
from ..utils import sum_to_shape, to_xp_array, get_two_operand_op_device
|
|
6
6
|
from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
|
|
@@ -29,13 +29,25 @@ def wrapper_2in_diff(
|
|
|
29
29
|
from ...tensor import Tensor
|
|
30
30
|
|
|
31
31
|
def backward() -> None:
|
|
32
|
+
if result.grad is None:
|
|
33
|
+
result.zeros_grad()
|
|
34
|
+
|
|
35
|
+
result_grad: NDArrayLike = result.grad
|
|
32
36
|
device = get_two_operand_op_device(x1, x2, None)
|
|
33
37
|
_x1, _x2 = to_xp_array(x1, device=device), to_xp_array(x2, device=device)
|
|
34
38
|
|
|
35
39
|
if isinstance(x1, Tensor) and x1.requires_grad:
|
|
36
|
-
x1.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
|
+
)
|
|
37
45
|
if isinstance(x2, Tensor) and x2.requires_grad:
|
|
38
|
-
x2.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
|
+
)
|
|
39
51
|
|
|
40
52
|
result.backward = backward
|
|
41
53
|
return result
|
|
@@ -49,9 +61,15 @@ def wrapper_1in_diff(
|
|
|
49
61
|
from ...tensor import Tensor
|
|
50
62
|
|
|
51
63
|
def backward() -> None:
|
|
64
|
+
if result.grad is None:
|
|
65
|
+
result.zeros_grad()
|
|
66
|
+
|
|
67
|
+
result_grad: NDArrayLike = result.grad
|
|
52
68
|
_x = to_xp_array(x)
|
|
53
69
|
if isinstance(x, Tensor) and x.requires_grad:
|
|
54
|
-
x.grad
|
|
70
|
+
if x.grad is None:
|
|
71
|
+
x.zeros_grad()
|
|
72
|
+
x.grad += callback_x1(result_grad, x.shape, x.device, _x)
|
|
55
73
|
|
|
56
74
|
result.backward = backward
|
|
57
75
|
return result
|
|
@@ -63,87 +81,95 @@ def wrapper_1in_diff(
|
|
|
63
81
|
|
|
64
82
|
|
|
65
83
|
def add_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
66
|
-
def callback_x1(shape, device, _x1, _x2):
|
|
67
|
-
return sum_to_shape(
|
|
84
|
+
def callback_x1(result_grad, shape, device, _x1, _x2):
|
|
85
|
+
return sum_to_shape(result_grad, shape, device)
|
|
68
86
|
|
|
69
|
-
def callback_x2(shape, device, _x1, _x2):
|
|
70
|
-
return sum_to_shape(
|
|
87
|
+
def callback_x2(result_grad, shape, device, _x1, _x2):
|
|
88
|
+
return sum_to_shape(result_grad, shape, device)
|
|
71
89
|
|
|
72
90
|
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
73
91
|
|
|
74
92
|
|
|
75
93
|
def subtract_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
76
|
-
def callback_x1(shape, device, _x1, _x2):
|
|
77
|
-
return sum_to_shape(result.grad, shape, device)
|
|
78
94
|
|
|
79
|
-
def
|
|
80
|
-
return sum_to_shape(
|
|
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)
|
|
81
100
|
|
|
82
101
|
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
83
102
|
|
|
84
103
|
|
|
85
104
|
def multiply_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
86
|
-
def callback_x1(shape, device, _x1, _x2):
|
|
87
|
-
return sum_to_shape(result.grad * _x2, shape, device)
|
|
88
105
|
|
|
89
|
-
def
|
|
90
|
-
return sum_to_shape(
|
|
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)
|
|
91
111
|
|
|
92
112
|
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
93
113
|
|
|
94
114
|
|
|
95
115
|
def matmul_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
96
|
-
def callback_x1(shape, device, _x1, _x2):
|
|
97
|
-
return sum_to_shape(result.grad @ _x2.swapaxes(-2, -1), shape, device)
|
|
98
116
|
|
|
99
|
-
def
|
|
100
|
-
return sum_to_shape(
|
|
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)
|
|
101
122
|
|
|
102
123
|
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
103
124
|
|
|
104
125
|
|
|
105
126
|
def divide_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
106
|
-
def callback_x1(shape, device, _x1, _x2):
|
|
107
|
-
return sum_to_shape(result.grad / _x2, shape, device)
|
|
108
127
|
|
|
109
|
-
def
|
|
110
|
-
return sum_to_shape(
|
|
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)
|
|
111
133
|
|
|
112
134
|
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
113
135
|
|
|
114
136
|
|
|
115
137
|
def power_diff(result: TensorLike, x1: OperandLike, x2: OperandLike) -> TensorLike:
|
|
116
|
-
def callback_x1(shape, device, _x1, _x2):
|
|
117
|
-
return sum_to_shape(result.grad * _x2 * _x1 ** (_x2 - 1), shape, device)
|
|
118
138
|
|
|
119
|
-
def
|
|
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):
|
|
120
143
|
if device == "cpu":
|
|
121
|
-
return sum_to_shape(
|
|
144
|
+
return sum_to_shape(result_grad * np.log(_x1) * _x1**_x2, shape, device)
|
|
122
145
|
else:
|
|
123
146
|
if cp is None:
|
|
124
147
|
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
125
|
-
return sum_to_shape(
|
|
148
|
+
return sum_to_shape(result_grad * cp.log(_x1) * _x1**_x2, shape, device)
|
|
126
149
|
|
|
127
150
|
return wrapper_2in_diff(result, x1, x2, callback_x1, callback_x2)
|
|
128
151
|
|
|
129
152
|
|
|
130
153
|
def negative_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
131
|
-
|
|
132
|
-
|
|
154
|
+
|
|
155
|
+
def callback_x1(result_grad, shape, device, _x):
|
|
156
|
+
return sum_to_shape(-result_grad, shape, device)
|
|
133
157
|
|
|
134
158
|
return wrapper_1in_diff(result, x, callback_x1)
|
|
135
159
|
|
|
136
160
|
|
|
137
161
|
def sign_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
138
|
-
|
|
139
|
-
|
|
162
|
+
|
|
163
|
+
def callback_x1(result_grad, shape, device, _x):
|
|
164
|
+
return sum_to_shape(result_grad * 0, shape, device)
|
|
140
165
|
|
|
141
166
|
return wrapper_1in_diff(result, x, callback_x1)
|
|
142
167
|
|
|
143
168
|
|
|
144
169
|
def abs_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
145
|
-
|
|
146
|
-
|
|
170
|
+
|
|
171
|
+
def callback_x1(result_grad, shape, device, _x):
|
|
172
|
+
return sum_to_shape(result_grad * result.data / _x, shape, device)
|
|
147
173
|
|
|
148
174
|
return wrapper_1in_diff(result, x, callback_x1)
|
|
149
175
|
|
|
@@ -154,29 +180,33 @@ def abs_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
|
154
180
|
|
|
155
181
|
|
|
156
182
|
def exp_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
157
|
-
|
|
158
|
-
|
|
183
|
+
|
|
184
|
+
def callback_x1(result_grad, shape, device, _x):
|
|
185
|
+
return sum_to_shape(result_grad * result.data, shape, device)
|
|
159
186
|
|
|
160
187
|
return wrapper_1in_diff(result, x, callback_x1)
|
|
161
188
|
|
|
162
189
|
|
|
163
190
|
def sqrt_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
164
|
-
|
|
165
|
-
|
|
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)
|
|
166
194
|
|
|
167
195
|
return wrapper_1in_diff(result, x, callback_x1)
|
|
168
196
|
|
|
169
197
|
|
|
170
198
|
def log_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
171
|
-
|
|
172
|
-
|
|
199
|
+
|
|
200
|
+
def callback_x1(result_grad, shape, device, _x):
|
|
201
|
+
return sum_to_shape(result_grad / _x, shape, device)
|
|
173
202
|
|
|
174
203
|
return wrapper_1in_diff(result, x, callback_x1)
|
|
175
204
|
|
|
176
205
|
|
|
177
206
|
def square_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
178
|
-
|
|
179
|
-
|
|
207
|
+
|
|
208
|
+
def callback_x1(result_grad, shape, device, _x):
|
|
209
|
+
return sum_to_shape(result_grad * 2 * _x, shape, device)
|
|
180
210
|
|
|
181
211
|
return wrapper_1in_diff(result, x, callback_x1)
|
|
182
212
|
|
|
@@ -70,7 +70,6 @@ def create_2in_1out(
|
|
|
70
70
|
from ...tensor import Tensor
|
|
71
71
|
|
|
72
72
|
op_device = get_two_operand_op_device(x1, x2, device)
|
|
73
|
-
|
|
74
73
|
a1, a2 = to_xp_array(x1, device=op_device), to_xp_array(x2, device=op_device)
|
|
75
74
|
y = op(a1, a2, device=op_device, **kwds)
|
|
76
75
|
|
|
@@ -91,7 +90,6 @@ def create_2in_1out(
|
|
|
91
90
|
else:
|
|
92
91
|
y = Tensor(
|
|
93
92
|
y,
|
|
94
|
-
device=op_device,
|
|
95
93
|
prev=prev,
|
|
96
94
|
requires_grad=y_requires_grad and requires_grad,
|
|
97
95
|
)
|
|
@@ -99,13 +97,19 @@ def create_2in_1out(
|
|
|
99
97
|
return y
|
|
100
98
|
else:
|
|
101
99
|
if y.requires_grad:
|
|
100
|
+
if y.grad is None:
|
|
101
|
+
y.zeros_grad()
|
|
102
102
|
dx1, dx2 = diff(y, a1, a2, **kwds)
|
|
103
103
|
|
|
104
104
|
def backward() -> None:
|
|
105
105
|
if isinstance(x1, Tensor) and x1.requires_grad:
|
|
106
|
-
x1.grad
|
|
106
|
+
if x1.grad is None:
|
|
107
|
+
x1.zeros_grad()
|
|
108
|
+
x1.grad += y.grad * dx1() # type: ignore
|
|
107
109
|
if isinstance(x2, Tensor) and x2.requires_grad:
|
|
108
|
-
x2.grad
|
|
110
|
+
if x2.grad is None:
|
|
111
|
+
x2.zeros_grad()
|
|
112
|
+
x2.grad += y.grad * dx2() # type: ignore
|
|
109
113
|
|
|
110
114
|
y.backward = backward
|
|
111
115
|
return y
|
|
@@ -126,13 +130,10 @@ def create_1in_1out(
|
|
|
126
130
|
) -> TensorLike:
|
|
127
131
|
from ...tensor import Tensor
|
|
128
132
|
|
|
129
|
-
if
|
|
130
|
-
device_op = x
|
|
133
|
+
if device is None:
|
|
134
|
+
device_op = get_device(x)
|
|
131
135
|
else:
|
|
132
|
-
|
|
133
|
-
device_op = get_device(x)
|
|
134
|
-
else:
|
|
135
|
-
device_op = device
|
|
136
|
+
device_op = device
|
|
136
137
|
|
|
137
138
|
a = to_xp_array(x, device=device_op)
|
|
138
139
|
y = op(a, device=device_op, **kwds)
|
|
@@ -152,7 +153,6 @@ def create_1in_1out(
|
|
|
152
153
|
else:
|
|
153
154
|
y = Tensor(
|
|
154
155
|
y,
|
|
155
|
-
device=device_op,
|
|
156
156
|
prev=(x,),
|
|
157
157
|
requires_grad=y_requires_grad and requires_grad,
|
|
158
158
|
)
|
|
@@ -160,11 +160,15 @@ def create_1in_1out(
|
|
|
160
160
|
return y
|
|
161
161
|
else:
|
|
162
162
|
if y.requires_grad:
|
|
163
|
+
if y.grad is None:
|
|
164
|
+
y.zeros_grad()
|
|
163
165
|
dx = diff(y, a, **kwds)
|
|
164
166
|
|
|
165
167
|
def backward() -> None:
|
|
166
168
|
if isinstance(x, Tensor) and x.requires_grad:
|
|
167
|
-
x.grad
|
|
169
|
+
if x.grad is None:
|
|
170
|
+
x.zeros_grad()
|
|
171
|
+
x.grad += y.grad * dx() # type: ignore
|
|
168
172
|
|
|
169
173
|
y.backward = backward
|
|
170
174
|
return y
|
|
@@ -195,27 +199,20 @@ def wrapper_2in_1out(
|
|
|
195
199
|
from ...tensor import Tensor
|
|
196
200
|
|
|
197
201
|
op_device = get_two_operand_op_device(x1, x2, device)
|
|
198
|
-
|
|
199
|
-
if device is not None:
|
|
200
|
-
op_device = device
|
|
201
202
|
a1, a2 = to_xp_array(x1, op_device), to_xp_array(x2, op_device)
|
|
202
203
|
|
|
203
204
|
if op_device == "cpu":
|
|
205
|
+
kwds = {
|
|
206
|
+
"out": out,
|
|
207
|
+
"dtype": dtype,
|
|
208
|
+
"where": where,
|
|
209
|
+
"casting": casting,
|
|
210
|
+
"order": order,
|
|
211
|
+
"subok": subok,
|
|
212
|
+
}
|
|
204
213
|
if func_name == "matmul":
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
)
|
|
208
|
-
else:
|
|
209
|
-
y = getattr(np, func_name)(
|
|
210
|
-
a1,
|
|
211
|
-
a2,
|
|
212
|
-
out=out,
|
|
213
|
-
dtype=dtype,
|
|
214
|
-
where=where,
|
|
215
|
-
casting=casting,
|
|
216
|
-
order=order,
|
|
217
|
-
subok=subok,
|
|
218
|
-
)
|
|
214
|
+
del kwds["where"]
|
|
215
|
+
y = getattr(np, func_name)(a1, a2, **kwds)
|
|
219
216
|
else:
|
|
220
217
|
if cp is None:
|
|
221
218
|
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
@@ -226,7 +223,7 @@ def wrapper_2in_1out(
|
|
|
226
223
|
return x1
|
|
227
224
|
|
|
228
225
|
requires_grad, prev = get_requires_grad_and_prev(x1, x2)
|
|
229
|
-
return Tensor(y,
|
|
226
|
+
return Tensor(y, prev=prev, requires_grad=requires_grad)
|
|
230
227
|
|
|
231
228
|
|
|
232
229
|
def wrapper_1in_1out(
|
|
@@ -245,13 +242,10 @@ def wrapper_1in_1out(
|
|
|
245
242
|
) -> TensorLike:
|
|
246
243
|
from ...tensor import Tensor
|
|
247
244
|
|
|
248
|
-
if
|
|
249
|
-
device_op =
|
|
245
|
+
if device is None:
|
|
246
|
+
device_op = get_device(device)
|
|
250
247
|
else:
|
|
251
|
-
|
|
252
|
-
device_op = get_device(device)
|
|
253
|
-
else:
|
|
254
|
-
device_op = device
|
|
248
|
+
device_op = device
|
|
255
249
|
|
|
256
250
|
a = to_xp_array(x, device=device_op)
|
|
257
251
|
if device_op == "cpu":
|
|
@@ -275,7 +269,7 @@ def wrapper_1in_1out(
|
|
|
275
269
|
x.data = y
|
|
276
270
|
return x
|
|
277
271
|
requires_grad = x.requires_grad
|
|
278
|
-
return Tensor(y,
|
|
272
|
+
return Tensor(y, prev=(x,), requires_grad=requires_grad)
|
|
279
273
|
|
|
280
274
|
|
|
281
275
|
###
|
|
@@ -66,7 +66,7 @@ def sum(
|
|
|
66
66
|
if cp is None:
|
|
67
67
|
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
68
68
|
y = cp.sum(arr, axis=axis, dtype=dtype, out=out, keepdims=keepdims)
|
|
69
|
-
return Tensor(y,
|
|
69
|
+
return Tensor(y, requires_grad=requires_grad)
|
|
70
70
|
|
|
71
71
|
|
|
72
72
|
def max(
|
|
@@ -97,7 +97,7 @@ def max(
|
|
|
97
97
|
if cp is None:
|
|
98
98
|
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
99
99
|
y = cp.max(arr, axis=axis, out=out, keepdims=keepdims)
|
|
100
|
-
return Tensor(y,
|
|
100
|
+
return Tensor(y, requires_grad=requires_grad)
|
|
101
101
|
|
|
102
102
|
|
|
103
103
|
def maximum(
|
|
@@ -133,8 +133,10 @@ def maximum(
|
|
|
133
133
|
subok=subok,
|
|
134
134
|
)
|
|
135
135
|
else:
|
|
136
|
-
|
|
137
|
-
|
|
136
|
+
if cp is None:
|
|
137
|
+
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
138
|
+
y = cp.maximum(_x1, _x2, out=out, dtype=dtype, casting=casting)
|
|
139
|
+
return Tensor(y, requires_grad=requires_grad)
|
|
138
140
|
|
|
139
141
|
|
|
140
142
|
###
|
|
@@ -6,13 +6,15 @@ from ...typing import DTypeLike, TensorLike, AxisLike, int64, DeviceLike
|
|
|
6
6
|
|
|
7
7
|
from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
import numpy as np
|
|
10
10
|
|
|
11
11
|
try:
|
|
12
12
|
import cupy as cp
|
|
13
13
|
except (ImportError, ModuleNotFoundError):
|
|
14
14
|
cp = None
|
|
15
15
|
|
|
16
|
+
_RNG = np.random.default_rng()
|
|
17
|
+
|
|
16
18
|
###
|
|
17
19
|
###
|
|
18
20
|
###
|
|
@@ -36,12 +38,19 @@ def rand(
|
|
|
36
38
|
from ...tensor import Tensor
|
|
37
39
|
|
|
38
40
|
if device == "cpu":
|
|
39
|
-
|
|
41
|
+
if dtype is None:
|
|
42
|
+
y = _RNG.random(size=d)
|
|
43
|
+
elif dtype is np.float32 or dtype is np.float64:
|
|
44
|
+
y = _RNG.random(size=d, dtype=dtype)
|
|
45
|
+
else:
|
|
46
|
+
raise RuntimeError(
|
|
47
|
+
"The 'dtype' argument must be a type either a float64 or a float32."
|
|
48
|
+
)
|
|
40
49
|
else:
|
|
41
50
|
if cp is None:
|
|
42
51
|
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
43
52
|
y = cp.random.rand(*d, dtype=dtype)
|
|
44
|
-
return Tensor(y,
|
|
53
|
+
return Tensor(y, requires_grad=requires_grad)
|
|
45
54
|
|
|
46
55
|
|
|
47
56
|
@overload
|
|
@@ -62,12 +71,19 @@ def randn(
|
|
|
62
71
|
from ...tensor import Tensor
|
|
63
72
|
|
|
64
73
|
if device == "cpu":
|
|
65
|
-
|
|
74
|
+
if dtype is None:
|
|
75
|
+
y = _RNG.standard_normal(d)
|
|
76
|
+
elif dtype is np.float32 or dtype is np.float64:
|
|
77
|
+
y = _RNG.standard_normal(d, dtype=dtype)
|
|
78
|
+
else:
|
|
79
|
+
raise RuntimeError(
|
|
80
|
+
"The 'dtype' argument must be a type either a float64 or a float32."
|
|
81
|
+
)
|
|
66
82
|
else:
|
|
67
83
|
if cp is None:
|
|
68
84
|
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
69
85
|
y = cp.random.randn(*d, dtype=dtype)
|
|
70
|
-
return Tensor(y,
|
|
86
|
+
return Tensor(y, requires_grad=requires_grad)
|
|
71
87
|
|
|
72
88
|
|
|
73
89
|
def randint(
|
|
@@ -2,16 +2,20 @@ from typing import List, Set
|
|
|
2
2
|
|
|
3
3
|
from . import Tensor
|
|
4
4
|
from ._core.exceptions import (
|
|
5
|
-
GradientComputationError,
|
|
6
|
-
|
|
5
|
+
GradientComputationError,
|
|
6
|
+
GRADIENT_COMPUTATION_ERROR,
|
|
7
|
+
CuPyNotFound,
|
|
8
|
+
CUPY_NOT_FOUND_MSG,
|
|
7
9
|
)
|
|
8
10
|
|
|
9
11
|
import numpy as np
|
|
12
|
+
|
|
10
13
|
try:
|
|
11
14
|
import cupy as cp
|
|
12
15
|
except (ModuleNotFoundError, ImportError):
|
|
13
16
|
cp = None
|
|
14
17
|
|
|
18
|
+
|
|
15
19
|
def differentiate(tensor: Tensor) -> List[Tensor]:
|
|
16
20
|
topo: List[Tensor] = []
|
|
17
21
|
visited: Set[Tensor] = set()
|
|
@@ -20,7 +24,7 @@ def differentiate(tensor: Tensor) -> List[Tensor]:
|
|
|
20
24
|
if t in visited:
|
|
21
25
|
return
|
|
22
26
|
visited.add(t)
|
|
23
|
-
|
|
27
|
+
|
|
24
28
|
for prev in t.prev:
|
|
25
29
|
build(prev)
|
|
26
30
|
topo.append(t)
|
|
@@ -28,7 +32,8 @@ def differentiate(tensor: Tensor) -> List[Tensor]:
|
|
|
28
32
|
build(tensor)
|
|
29
33
|
|
|
30
34
|
if tensor.device == "gpu":
|
|
31
|
-
if cp is None:
|
|
35
|
+
if cp is None:
|
|
36
|
+
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
32
37
|
tensor.grad = cp.ones(tensor.shape, tensor.dtype)
|
|
33
38
|
elif tensor.device == "cpu":
|
|
34
39
|
tensor.grad = np.ones(tensor.shape, tensor.dtype)
|
|
@@ -36,9 +41,13 @@ def differentiate(tensor: Tensor) -> List[Tensor]:
|
|
|
36
41
|
for t in reversed(topo):
|
|
37
42
|
if t.backward is None:
|
|
38
43
|
if not t.requires_grad:
|
|
39
|
-
raise GradientComputationError(
|
|
44
|
+
raise GradientComputationError(
|
|
45
|
+
f"Attempted to propagate backward off of a tensor ({t}), which is not differentiable."
|
|
46
|
+
)
|
|
40
47
|
if len(t.prev) > 0:
|
|
41
|
-
raise GradientComputationError(
|
|
42
|
-
|
|
48
|
+
raise GradientComputationError(
|
|
49
|
+
f"Tensor ({t})'s backward function is None."
|
|
50
|
+
)
|
|
51
|
+
continue
|
|
43
52
|
t.backward()
|
|
44
|
-
return topo
|
|
53
|
+
return topo
|
|
@@ -92,14 +92,21 @@ allowed_dtype_kind = "uifb"
|
|
|
92
92
|
###
|
|
93
93
|
|
|
94
94
|
|
|
95
|
+
def is_list_of_type(list_obj, type_obj) -> bool:
|
|
96
|
+
if not isinstance(list_obj, list):
|
|
97
|
+
return False
|
|
98
|
+
return all(isinstance(x, type_obj) for x in list_obj)
|
|
99
|
+
|
|
100
|
+
|
|
95
101
|
class Tensor:
|
|
102
|
+
data: NDArrayLike
|
|
96
103
|
device: DeviceLike
|
|
104
|
+
grad: Optional[NDArrayLike]
|
|
97
105
|
backward: Optional[Callable[[], None]]
|
|
98
|
-
data: NDArrayLike
|
|
99
106
|
|
|
100
107
|
def __init__(
|
|
101
108
|
self,
|
|
102
|
-
obj: OperandLike,
|
|
109
|
+
obj: Union[OperandLike, List[NDArrayLike]],
|
|
103
110
|
/,
|
|
104
111
|
*,
|
|
105
112
|
dtype: Optional[DTypeLike] = None,
|
|
@@ -130,6 +137,8 @@ class Tensor:
|
|
|
130
137
|
raise TypeError("Expected a numerical CuPy array.")
|
|
131
138
|
elif isinstance(obj, Tensor):
|
|
132
139
|
self.data = xp_array_to_device(obj.data, self.device)
|
|
140
|
+
elif is_list_of_type(obj, Tensor):
|
|
141
|
+
self.data = [xp_array_to_device(t) for t in obj] # type: ignore
|
|
133
142
|
else:
|
|
134
143
|
self.data = xp_array_to_device(
|
|
135
144
|
to_xp_array(obj, self.device), self.device
|
|
@@ -142,14 +151,18 @@ class Tensor:
|
|
|
142
151
|
self.data = self.data.astype(dtype)
|
|
143
152
|
self.prev = prev
|
|
144
153
|
self.requires_grad = requires_grad
|
|
154
|
+
self.backward = None
|
|
155
|
+
self.grad = None
|
|
145
156
|
|
|
157
|
+
def zeros_grad(self) -> NDArrayLike:
|
|
146
158
|
if self.device == "cpu":
|
|
147
|
-
|
|
159
|
+
grad = np.zeros_like(self.data)
|
|
148
160
|
else:
|
|
149
161
|
if cp is None:
|
|
150
162
|
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
151
|
-
|
|
152
|
-
self.
|
|
163
|
+
grad = cp.zeros_like(self.data)
|
|
164
|
+
self.grad = grad
|
|
165
|
+
return grad
|
|
153
166
|
|
|
154
167
|
###
|
|
155
168
|
###
|
|
@@ -20,6 +20,4 @@ src/DepthTensor/_core/ops/diff.py
|
|
|
20
20
|
src/DepthTensor/_core/ops/elementwise.py
|
|
21
21
|
src/DepthTensor/_core/ops/reduction.py
|
|
22
22
|
src/DepthTensor/_core/random/__init__.py
|
|
23
|
-
src/DepthTensor/_core/random/generator.py
|
|
24
|
-
src/example/example.py
|
|
25
|
-
src/example/test.py
|
|
23
|
+
src/DepthTensor/_core/random/generator.py
|
|
@@ -1,93 +0,0 @@
|
|
|
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"]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -164,11 +164,11 @@ def get_complement_device(device: DeviceLike) -> DeviceLike:
|
|
|
164
164
|
def get_two_operand_op_device(
|
|
165
165
|
x1: OperandLike, x2: OperandLike, device: Optional[DeviceLike]
|
|
166
166
|
) -> DeviceLike:
|
|
167
|
-
from ..tensor import Tensor
|
|
168
|
-
|
|
169
167
|
if device is not None:
|
|
170
168
|
return device
|
|
171
169
|
|
|
170
|
+
from ..tensor import Tensor
|
|
171
|
+
|
|
172
172
|
if isinstance(x1, Tensor) and isinstance(x2, Tensor):
|
|
173
173
|
if x1.device != x2.device:
|
|
174
174
|
raise DeviceMismatch(DEVICE_MISMATCH_MSG)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|