DepthTensor 2.0.0__tar.gz → 2.0.2__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.0/src/DepthTensor.egg-info → depthtensor-2.0.2}/PKG-INFO +1 -1
- {depthtensor-2.0.0 → depthtensor-2.0.2}/pyproject.toml +1 -1
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor/__init__.py +1 -1
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor/_core/ops/diff.py +87 -39
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor/_core/random/generator.py +28 -30
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor/_core/utils.py +2 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor/tensor.py +22 -69
- {depthtensor-2.0.0 → depthtensor-2.0.2/src/DepthTensor.egg-info}/PKG-INFO +1 -1
- depthtensor-2.0.2/src/example/example.py +8 -0
- depthtensor-2.0.2/src/example/test.py +10 -0
- depthtensor-2.0.0/src/example/example.py +0 -39
- depthtensor-2.0.0/src/example/test.py +0 -7
- {depthtensor-2.0.0 → depthtensor-2.0.2}/LICENSE +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/README.md +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/setup.cfg +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor/_core/__init__.py +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor/_core/exceptions.py +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor/_core/ops/__init__.py +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor/_core/ops/comparison.py +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor/_core/ops/creation.py +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor/_core/ops/elementwise.py +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor/_core/ops/reduction.py +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor/_core/random/__init__.py +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor/autodiff.py +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor/typing.py +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor.egg-info/SOURCES.txt +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor.egg-info/dependency_links.txt +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor.egg-info/requires.txt +0 -0
- {depthtensor-2.0.0 → depthtensor-2.0.2}/src/DepthTensor.egg-info/top_level.txt +0 -0
|
@@ -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
|
-
|
|
26
|
-
|
|
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
|
-
|
|
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,
|
|
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,
|
|
38
|
+
x2.grad += callback_x2(x2.shape, device, _x1, _x2)
|
|
39
|
+
|
|
34
40
|
result.backward = backward
|
|
35
41
|
return result
|
|
36
42
|
|
|
37
|
-
|
|
38
|
-
|
|
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
|
|
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:
|
|
94
|
-
|
|
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
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
+
]
|
|
@@ -1,22 +1,12 @@
|
|
|
1
|
-
from typing import
|
|
2
|
-
Optional,
|
|
3
|
-
overload,
|
|
4
|
-
Any
|
|
5
|
-
)
|
|
1
|
+
from typing import Optional, overload, Any
|
|
6
2
|
|
|
7
3
|
from numpy import random
|
|
8
4
|
|
|
9
|
-
from ...typing import
|
|
10
|
-
DTypeLike,
|
|
11
|
-
TensorLike,
|
|
12
|
-
AxisLike,
|
|
13
|
-
int64,
|
|
14
|
-
DeviceLike
|
|
15
|
-
)
|
|
5
|
+
from ...typing import DTypeLike, TensorLike, AxisLike, int64, DeviceLike
|
|
16
6
|
|
|
17
|
-
from ..exceptions import
|
|
18
|
-
|
|
19
|
-
|
|
7
|
+
from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
|
|
8
|
+
|
|
9
|
+
from ..utils import get_device
|
|
20
10
|
|
|
21
11
|
try:
|
|
22
12
|
import cupy as cp
|
|
@@ -27,6 +17,7 @@ except (ImportError, ModuleNotFoundError):
|
|
|
27
17
|
###
|
|
28
18
|
###
|
|
29
19
|
|
|
20
|
+
|
|
30
21
|
@overload
|
|
31
22
|
def rand(*, device: DeviceLike = "cpu", requires_grad: bool = False) -> TensorLike: ...
|
|
32
23
|
@overload
|
|
@@ -35,7 +26,7 @@ def rand(
|
|
|
35
26
|
dtype: Optional[DTypeLike] = None,
|
|
36
27
|
device: DeviceLike = "cpu",
|
|
37
28
|
requires_grad: bool = False
|
|
38
|
-
) -> TensorLike: ...
|
|
29
|
+
) -> TensorLike: ...
|
|
39
30
|
def rand(
|
|
40
31
|
*d: int,
|
|
41
32
|
dtype: Optional[DTypeLike] = None,
|
|
@@ -43,13 +34,16 @@ def rand(
|
|
|
43
34
|
requires_grad: bool = False
|
|
44
35
|
) -> TensorLike:
|
|
45
36
|
from ...tensor import Tensor
|
|
37
|
+
|
|
46
38
|
if device == "cpu":
|
|
47
39
|
y = random.rand(*d)
|
|
48
40
|
else:
|
|
49
|
-
if cp is None:
|
|
41
|
+
if cp is None:
|
|
42
|
+
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
50
43
|
y = cp.random.rand(*d, dtype=dtype)
|
|
51
44
|
return Tensor(y, dtype=dtype, requires_grad=requires_grad)
|
|
52
|
-
|
|
45
|
+
|
|
46
|
+
|
|
53
47
|
@overload
|
|
54
48
|
def randn(*, device: DeviceLike = "cpu", requires_grad: bool = False) -> TensorLike: ...
|
|
55
49
|
@overload
|
|
@@ -58,7 +52,7 @@ def randn(
|
|
|
58
52
|
dtype: Optional[DTypeLike] = None,
|
|
59
53
|
device: DeviceLike = "cpu",
|
|
60
54
|
requires_grad: bool = False
|
|
61
|
-
) -> TensorLike: ...
|
|
55
|
+
) -> TensorLike: ...
|
|
62
56
|
def randn(
|
|
63
57
|
*d: int,
|
|
64
58
|
dtype: Optional[DTypeLike] = None,
|
|
@@ -66,29 +60,35 @@ def randn(
|
|
|
66
60
|
requires_grad: bool = False
|
|
67
61
|
) -> TensorLike:
|
|
68
62
|
from ...tensor import Tensor
|
|
63
|
+
|
|
69
64
|
if device == "cpu":
|
|
70
65
|
y = random.randn(*d)
|
|
71
66
|
else:
|
|
72
|
-
if cp is None:
|
|
67
|
+
if cp is None:
|
|
68
|
+
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
73
69
|
y = cp.random.randn(*d, dtype=dtype)
|
|
74
70
|
return Tensor(y, dtype=dtype, requires_grad=requires_grad)
|
|
75
|
-
|
|
71
|
+
|
|
72
|
+
|
|
76
73
|
def randint(
|
|
77
74
|
low: int,
|
|
78
75
|
high: Optional[int] = None,
|
|
79
76
|
size: Optional[AxisLike] = None,
|
|
80
77
|
dtype: Any = int64,
|
|
81
78
|
device: DeviceLike = "cpu",
|
|
82
|
-
requires_grad: bool = False
|
|
79
|
+
requires_grad: bool = False,
|
|
83
80
|
) -> TensorLike:
|
|
84
81
|
from ...tensor import Tensor
|
|
82
|
+
|
|
85
83
|
if device == "cpu":
|
|
86
84
|
y = random.randint(low=low, high=high, size=size, dtype=dtype)
|
|
87
85
|
else:
|
|
88
|
-
if cp is None:
|
|
86
|
+
if cp is None:
|
|
87
|
+
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
89
88
|
y = cp.random.randint(low=low, high=high, size=size, dtype=dtype)
|
|
90
89
|
return Tensor(y, requires_grad=requires_grad)
|
|
91
90
|
|
|
91
|
+
|
|
92
92
|
def uniform(
|
|
93
93
|
low: float = 0.0,
|
|
94
94
|
high: float = 1.0,
|
|
@@ -98,20 +98,18 @@ def uniform(
|
|
|
98
98
|
requires_grad: bool = False
|
|
99
99
|
):
|
|
100
100
|
from ...tensor import Tensor
|
|
101
|
+
|
|
101
102
|
if device == "cpu":
|
|
102
103
|
y = random.uniform(low=low, high=high, size=size)
|
|
103
104
|
else:
|
|
104
|
-
if cp is None:
|
|
105
|
+
if cp is None:
|
|
106
|
+
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
105
107
|
y = cp.random.uniform(low=low, high=high, size=size)
|
|
106
108
|
return Tensor(y, requires_grad=requires_grad)
|
|
107
109
|
|
|
110
|
+
|
|
108
111
|
###
|
|
109
112
|
###
|
|
110
113
|
###
|
|
111
114
|
|
|
112
|
-
__all__ = [
|
|
113
|
-
'rand',
|
|
114
|
-
'randn',
|
|
115
|
-
'randint',
|
|
116
|
-
'uniform'
|
|
117
|
-
]
|
|
115
|
+
__all__ = ["rand", "randn", "randint", "uniform"]
|
|
@@ -185,6 +185,7 @@ def get_two_operand_op_device(
|
|
|
185
185
|
tuple,
|
|
186
186
|
), # * Universal datatypes regardless of devices
|
|
187
187
|
):
|
|
188
|
+
# * This leaves x2 to be either np.array or cp.array.
|
|
188
189
|
raise DeviceMismatch(
|
|
189
190
|
f"There is a incompatibility in datatypes between the two operands of types Tensor and {type(x2)}. Expected datatype of device: {x1.device} {"(CuPy arrays, GPU Tensors)" if x1.device == "gpu" else "(NumPy arrays, CPU Tensors, ints, floats, lists, tuples)"}, got datatype of type: {type(x2)}."
|
|
190
191
|
)
|
|
@@ -199,6 +200,7 @@ def get_two_operand_op_device(
|
|
|
199
200
|
tuple,
|
|
200
201
|
), # * Universal datatypes regardless of devices
|
|
201
202
|
):
|
|
203
|
+
# * This leaves x1 to be either np.array or cp.array.
|
|
202
204
|
raise DeviceMismatch(
|
|
203
205
|
f"There is a incompatibility in datatypes between the two operands of types {type(x1)} and Tensor. Expected datatype of device: {x2.device} {"(CuPy arrays, GPU Tensors)" if x2.device == "gpu" else "(NumPy arrays, CPU Tensors, ints, floats, lists, tuples)"}, got datatype of type: {type(x1)}."
|
|
204
206
|
)
|
|
@@ -14,7 +14,6 @@ from .typing import (
|
|
|
14
14
|
)
|
|
15
15
|
|
|
16
16
|
from ._core import (
|
|
17
|
-
xp_array_to_device,
|
|
18
17
|
CuPyNotFound,
|
|
19
18
|
CUPY_NOT_FOUND_MSG,
|
|
20
19
|
# * elementwise
|
|
@@ -48,6 +47,8 @@ from ._core import (
|
|
|
48
47
|
sum,
|
|
49
48
|
)
|
|
50
49
|
|
|
50
|
+
from ._core.utils import get_device, to_xp_array, xp_array_to_device
|
|
51
|
+
|
|
51
52
|
import numpy as np
|
|
52
53
|
|
|
53
54
|
try:
|
|
@@ -84,6 +85,8 @@ def _wrapper_1in_1out(
|
|
|
84
85
|
return y
|
|
85
86
|
|
|
86
87
|
|
|
88
|
+
allowed_dtype_kind = "uifb"
|
|
89
|
+
|
|
87
90
|
###
|
|
88
91
|
###
|
|
89
92
|
###
|
|
@@ -100,7 +103,7 @@ class Tensor:
|
|
|
100
103
|
/,
|
|
101
104
|
*,
|
|
102
105
|
dtype: Optional[DTypeLike] = None,
|
|
103
|
-
device: DeviceLike =
|
|
106
|
+
device: Optional[DeviceLike] = None,
|
|
104
107
|
prev: Tuple = (),
|
|
105
108
|
requires_grad: bool = False,
|
|
106
109
|
copy: bool = True,
|
|
@@ -109,80 +112,30 @@ class Tensor:
|
|
|
109
112
|
ndmin: int = 0,
|
|
110
113
|
blocking: bool = False,
|
|
111
114
|
) -> None:
|
|
112
|
-
|
|
115
|
+
if device is None:
|
|
116
|
+
self.device = get_device(obj)
|
|
117
|
+
else:
|
|
118
|
+
self.device = device
|
|
119
|
+
|
|
113
120
|
if isinstance(obj, np.ndarray):
|
|
114
|
-
if obj.dtype.kind in
|
|
115
|
-
self.data = xp_array_to_device(obj, device)
|
|
116
|
-
self.device = "cpu"
|
|
121
|
+
if obj.dtype.kind in allowed_dtype_kind:
|
|
122
|
+
self.data = xp_array_to_device(obj, self.device)
|
|
117
123
|
else:
|
|
118
124
|
raise TypeError("Expected a numerical NumPy array.")
|
|
119
125
|
else:
|
|
120
126
|
if cp is not None and isinstance(obj, cp.ndarray):
|
|
121
|
-
if obj.dtype.kind in
|
|
122
|
-
self.data = xp_array_to_device(obj, device)
|
|
123
|
-
self.device = "gpu"
|
|
127
|
+
if obj.dtype.kind in allowed_dtype_kind: # type: ignore
|
|
128
|
+
self.data = xp_array_to_device(obj, self.device)
|
|
124
129
|
else:
|
|
125
130
|
raise TypeError("Expected a numerical CuPy array.")
|
|
131
|
+
elif isinstance(obj, Tensor):
|
|
132
|
+
self.data = xp_array_to_device(obj.data, self.device)
|
|
126
133
|
else:
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
(int, float, List, Tuple, cp.floating, cp.integer, cp.bool_),
|
|
133
|
-
):
|
|
134
|
-
self.data = cp.array(
|
|
135
|
-
obj,
|
|
136
|
-
dtype=dtype,
|
|
137
|
-
copy=copy,
|
|
138
|
-
order=order,
|
|
139
|
-
subok=subok,
|
|
140
|
-
ndmin=ndmin,
|
|
141
|
-
blocking=blocking,
|
|
142
|
-
)
|
|
143
|
-
self.device = "gpu"
|
|
144
|
-
elif isinstance(obj, Tensor):
|
|
145
|
-
self.data = cp.array(
|
|
146
|
-
obj.data,
|
|
147
|
-
dtype=dtype,
|
|
148
|
-
copy=copy,
|
|
149
|
-
order=order,
|
|
150
|
-
subok=subok,
|
|
151
|
-
ndmin=ndmin,
|
|
152
|
-
blocking=blocking,
|
|
153
|
-
)
|
|
154
|
-
self.device = "gpu"
|
|
155
|
-
else:
|
|
156
|
-
raise RuntimeError(
|
|
157
|
-
"Expected data of type: int, float, list, tuple, numpy.ndarray, cupy.ndarray."
|
|
158
|
-
)
|
|
159
|
-
else:
|
|
160
|
-
if isinstance(
|
|
161
|
-
obj, (int, float, List, Tuple, np.integer, np.floating, np.bool)
|
|
162
|
-
):
|
|
163
|
-
self.data = np.array(
|
|
164
|
-
obj,
|
|
165
|
-
dtype=dtype,
|
|
166
|
-
copy=copy,
|
|
167
|
-
order=order,
|
|
168
|
-
subok=subok,
|
|
169
|
-
ndmin=ndmin,
|
|
170
|
-
)
|
|
171
|
-
self.device = "cpu"
|
|
172
|
-
elif isinstance(obj, Tensor):
|
|
173
|
-
self.data = np.array(
|
|
174
|
-
obj.data,
|
|
175
|
-
dtype=dtype,
|
|
176
|
-
copy=copy,
|
|
177
|
-
order=order,
|
|
178
|
-
subok=subok,
|
|
179
|
-
ndmin=ndmin,
|
|
180
|
-
)
|
|
181
|
-
self.device = "cpu"
|
|
182
|
-
else:
|
|
183
|
-
raise RuntimeError(
|
|
184
|
-
"Expected data of type: int, float, list, tuple, numpy.ndarray, cupy.ndarray."
|
|
185
|
-
)
|
|
134
|
+
self.data = xp_array_to_device(
|
|
135
|
+
to_xp_array(obj, self.device), self.device
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
# TODO: Use all the parameters.
|
|
186
139
|
|
|
187
140
|
# * Convert to dtype (if provided)
|
|
188
141
|
if dtype is not None and dtype != self.data.dtype:
|
|
@@ -190,7 +143,7 @@ class Tensor:
|
|
|
190
143
|
self.prev = prev
|
|
191
144
|
self.requires_grad = requires_grad
|
|
192
145
|
|
|
193
|
-
if device == "cpu":
|
|
146
|
+
if self.device == "cpu":
|
|
194
147
|
self.grad = np.zeros_like(self.data)
|
|
195
148
|
else:
|
|
196
149
|
if cp is None:
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import DepthTensor as DTensor
|
|
2
|
-
from ..DepthTensor import (
|
|
3
|
-
Tensor,
|
|
4
|
-
differentiate,
|
|
5
|
-
create_1in_1out,
|
|
6
|
-
NDArrayLike,
|
|
7
|
-
DeviceLike,
|
|
8
|
-
CuPyNotFound,
|
|
9
|
-
CUPY_NOT_FOUND_MSG,
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
import numpy as np
|
|
13
|
-
|
|
14
|
-
try:
|
|
15
|
-
import cupy as cp
|
|
16
|
-
except:
|
|
17
|
-
cp = None
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def op(x: NDArrayLike, device: DeviceLike = "cpu", **kwds) -> NDArrayLike:
|
|
21
|
-
print(type(x))
|
|
22
|
-
return DTensor.add(x, x)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def diff(result: Tensor, x: NDArrayLike, **kwds):
|
|
26
|
-
def x_diff() -> NDArrayLike:
|
|
27
|
-
return 1
|
|
28
|
-
|
|
29
|
-
return x_diff
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
func = create_1in_1out(op, diff)
|
|
33
|
-
a = Tensor(6.0, device="gpu", requires_grad=True)
|
|
34
|
-
b = func(a, device="gpu")
|
|
35
|
-
print(b)
|
|
36
|
-
print(b.requires_grad)
|
|
37
|
-
print(b.prev)
|
|
38
|
-
differentiate(b)
|
|
39
|
-
print(a.grad)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|