DepthTensor 2.0.1__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.1/src/DepthTensor.egg-info → depthtensor-2.0.2}/PKG-INFO +1 -1
- {depthtensor-2.0.1 → depthtensor-2.0.2}/pyproject.toml +1 -1
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor/__init__.py +1 -1
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor/_core/random/generator.py +28 -30
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor/tensor.py +22 -69
- {depthtensor-2.0.1 → 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.1/src/example/example.py +0 -39
- {depthtensor-2.0.1 → depthtensor-2.0.2}/LICENSE +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/README.md +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/setup.cfg +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor/_core/__init__.py +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor/_core/exceptions.py +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor/_core/ops/__init__.py +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor/_core/ops/comparison.py +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor/_core/ops/creation.py +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor/_core/ops/diff.py +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor/_core/ops/elementwise.py +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor/_core/ops/reduction.py +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor/_core/random/__init__.py +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor/_core/utils.py +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor/autodiff.py +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor/typing.py +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor.egg-info/SOURCES.txt +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor.egg-info/dependency_links.txt +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor.egg-info/requires.txt +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/DepthTensor.egg-info/top_level.txt +0 -0
- {depthtensor-2.0.1 → depthtensor-2.0.2}/src/example/test.py +0 -0
|
@@ -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"]
|
|
@@ -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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|