DepthTensor 1.9.0__tar.gz → 1.10.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.9.0/src/DepthTensor.egg-info → depthtensor-1.10.0}/PKG-INFO +1 -1
- {depthtensor-1.9.0 → depthtensor-1.10.0}/pyproject.toml +1 -1
- depthtensor-1.10.0/src/DepthTensor/__init__.py +7 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor/_core/ops/creation.py +1 -1
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor/_core/ops/elementwise.py +118 -4
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor/_core/utils.py +1 -1
- depthtensor-1.10.0/src/DepthTensor/typing.py +126 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0/src/DepthTensor.egg-info}/PKG-INFO +1 -1
- depthtensor-1.10.0/src/example/example.py +26 -0
- depthtensor-1.9.0/src/DepthTensor/__init__.py +0 -6
- depthtensor-1.9.0/src/DepthTensor/typing.py +0 -41
- depthtensor-1.9.0/src/example/example.py +0 -9
- {depthtensor-1.9.0 → depthtensor-1.10.0}/LICENSE +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/README.md +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/setup.cfg +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor/_core/__init__.py +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor/_core/exceptions.py +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor/_core/ops/__init__.py +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor/_core/ops/comparison.py +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor/_core/ops/diff.py +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor/_core/ops/reduction.py +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor/_core/random/__init__.py +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor/_core/random/generator.py +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor/autodiff.py +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor/tensor.py +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor.egg-info/SOURCES.txt +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor.egg-info/dependency_links.txt +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor.egg-info/requires.txt +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/src/DepthTensor.egg-info/top_level.txt +0 -0
- {depthtensor-1.9.0 → depthtensor-1.10.0}/tests/test_tensor.py +0 -0
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
from typing import (
|
|
2
2
|
Optional,
|
|
3
3
|
Union,
|
|
4
|
-
Any
|
|
4
|
+
Any,
|
|
5
|
+
Callable
|
|
5
6
|
)
|
|
6
7
|
|
|
7
8
|
from ...typing import (
|
|
@@ -12,7 +13,13 @@ from ...typing import (
|
|
|
12
13
|
ArrayLikeBool,
|
|
13
14
|
ArrayLike,
|
|
14
15
|
OperandLike,
|
|
15
|
-
DeviceLike
|
|
16
|
+
DeviceLike,
|
|
17
|
+
Op_2in_1out_Protocol,
|
|
18
|
+
Diff_2in_1out_Protocol,
|
|
19
|
+
Func_2in_1out_Protocol,
|
|
20
|
+
Op_1in_1out_Protocol,
|
|
21
|
+
Diff_1in_1out_Protocol,
|
|
22
|
+
Func_1in_1out_Protocol
|
|
16
23
|
)
|
|
17
24
|
|
|
18
25
|
from ..exceptions import (
|
|
@@ -31,9 +38,110 @@ except (ImportError, ModuleNotFoundError):
|
|
|
31
38
|
cp = None
|
|
32
39
|
|
|
33
40
|
###
|
|
34
|
-
###
|
|
41
|
+
###
|
|
35
42
|
###
|
|
36
43
|
|
|
44
|
+
def create_2in_1out(
|
|
45
|
+
op: Op_2in_1out_Protocol,
|
|
46
|
+
diff: Optional[Diff_2in_1out_Protocol]
|
|
47
|
+
) -> Func_2in_1out_Protocol:
|
|
48
|
+
def func(
|
|
49
|
+
x1: OperandLike,
|
|
50
|
+
x2: OperandLike,
|
|
51
|
+
*,
|
|
52
|
+
device: DeviceLike = "cpu",
|
|
53
|
+
in_place: bool = False,
|
|
54
|
+
requires_grad: bool = True,
|
|
55
|
+
**kwds: Any
|
|
56
|
+
) -> TensorLike:
|
|
57
|
+
from ...tensor import Tensor
|
|
58
|
+
is_tensor_op = False
|
|
59
|
+
if isinstance(x1, Tensor) and isinstance(x2, Tensor):
|
|
60
|
+
if not x1.is_device(x2.device):
|
|
61
|
+
raise DeviceMismatch(DEVICE_MISMATCH_MSG)
|
|
62
|
+
else:
|
|
63
|
+
is_tensor_op = True
|
|
64
|
+
|
|
65
|
+
if is_tensor_op and isinstance(x1, Tensor):
|
|
66
|
+
device_op = x1.device
|
|
67
|
+
else:
|
|
68
|
+
device_op = device
|
|
69
|
+
|
|
70
|
+
a1, a2 = to_xp_array(x1, device=device_op), to_xp_array(x2, device=device_op)
|
|
71
|
+
y = op(a1, a2, device=device, **kwds)
|
|
72
|
+
|
|
73
|
+
if is_tensor_op and in_place and isinstance(x1, Tensor):
|
|
74
|
+
x1.data = y
|
|
75
|
+
return x1
|
|
76
|
+
|
|
77
|
+
y_requires_grad = False
|
|
78
|
+
prev = ()
|
|
79
|
+
if isinstance(x1, Tensor):
|
|
80
|
+
y_requires_grad = x1.requires_grad
|
|
81
|
+
prev = (x1,)
|
|
82
|
+
if isinstance(x2, Tensor):
|
|
83
|
+
y_requires_grad = y_requires_grad or x2.requires_grad
|
|
84
|
+
if len(prev) == 1:
|
|
85
|
+
prev = (x1, x2)
|
|
86
|
+
else:
|
|
87
|
+
prev = (x2,)
|
|
88
|
+
|
|
89
|
+
y = Tensor(y, device=device_op, prev=prev, requires_grad=y_requires_grad and requires_grad)
|
|
90
|
+
if diff is None:
|
|
91
|
+
return y
|
|
92
|
+
else:
|
|
93
|
+
if y.requires_grad:
|
|
94
|
+
dx1, dx2 = diff(y, a1, a2)
|
|
95
|
+
def backward() -> None:
|
|
96
|
+
if isinstance(x1, Tensor) and x1.requires_grad:
|
|
97
|
+
x1.grad += y.grad * dx1()
|
|
98
|
+
if isinstance(x2, Tensor) and x2.requires_grad:
|
|
99
|
+
x2.grad += y.grad * dx2()
|
|
100
|
+
y.backward = backward
|
|
101
|
+
return y
|
|
102
|
+
return func
|
|
103
|
+
|
|
104
|
+
def create_1in_1out(
|
|
105
|
+
op: Op_1in_1out_Protocol,
|
|
106
|
+
diff: Optional[Diff_1in_1out_Protocol]
|
|
107
|
+
) -> Func_1in_1out_Protocol:
|
|
108
|
+
def func(
|
|
109
|
+
x: OperandLike,
|
|
110
|
+
*,
|
|
111
|
+
device: DeviceLike = "cpu",
|
|
112
|
+
in_place: bool = False,
|
|
113
|
+
requires_grad: bool = True,
|
|
114
|
+
**kwds: Any
|
|
115
|
+
) -> TensorLike:
|
|
116
|
+
from ...tensor import Tensor
|
|
117
|
+
if isinstance(x, Tensor):
|
|
118
|
+
device_op = x.device
|
|
119
|
+
else:
|
|
120
|
+
device_op = device
|
|
121
|
+
|
|
122
|
+
a = to_xp_array(x, device=device_op)
|
|
123
|
+
y = op(a, device=device, **kwds)
|
|
124
|
+
|
|
125
|
+
y_requires_grad = False
|
|
126
|
+
if isinstance(x, Tensor):
|
|
127
|
+
if in_place:
|
|
128
|
+
x.data = y
|
|
129
|
+
return x
|
|
130
|
+
y_requires_grad = x.requires_grad
|
|
131
|
+
|
|
132
|
+
y = Tensor(y, device=device_op, prev=(x,), requires_grad=y_requires_grad and requires_grad)
|
|
133
|
+
if diff is None:
|
|
134
|
+
return y
|
|
135
|
+
else:
|
|
136
|
+
if y.requires_grad:
|
|
137
|
+
dx = diff(y, a)
|
|
138
|
+
def backward() -> None:
|
|
139
|
+
if isinstance(x, Tensor) and x.requires_grad:
|
|
140
|
+
x.grad += y.grad * dx()
|
|
141
|
+
y.backward = backward
|
|
142
|
+
return y
|
|
143
|
+
return func
|
|
144
|
+
|
|
37
145
|
def wrapper_2in_1out(
|
|
38
146
|
x1: OperandLike,
|
|
39
147
|
x2: OperandLike,
|
|
@@ -127,6 +235,10 @@ def wrapper_1in_1out(
|
|
|
127
235
|
requires_grad = x.requires_grad
|
|
128
236
|
return Tensor(y, device=device_op, prev=(x,), requires_grad=requires_grad)
|
|
129
237
|
|
|
238
|
+
###
|
|
239
|
+
### Arithmetics
|
|
240
|
+
###
|
|
241
|
+
|
|
130
242
|
def add(
|
|
131
243
|
x1: OperandLike,
|
|
132
244
|
x2: OperandLike,
|
|
@@ -387,5 +499,7 @@ __all__ = [
|
|
|
387
499
|
'sqrt',
|
|
388
500
|
'log',
|
|
389
501
|
'square',
|
|
390
|
-
'clip'
|
|
502
|
+
'clip',
|
|
503
|
+
'create_2in_1out',
|
|
504
|
+
'create_1in_1out'
|
|
391
505
|
]
|
|
@@ -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.ndarray
|
|
98
|
+
Convert data to numpy.ndarray/cp.ndarray.
|
|
99
99
|
"""
|
|
100
100
|
from ..tensor import Tensor
|
|
101
101
|
if isinstance(a, Tensor):
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
from typing import (
|
|
2
|
+
TypeAlias,
|
|
3
|
+
Union,
|
|
4
|
+
Literal,
|
|
5
|
+
Tuple,
|
|
6
|
+
TYPE_CHECKING,
|
|
7
|
+
Protocol,
|
|
8
|
+
Any,
|
|
9
|
+
Callable
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
if TYPE_CHECKING:
|
|
13
|
+
from .tensor import Tensor
|
|
14
|
+
TensorLike: TypeAlias = 'Tensor'
|
|
15
|
+
|
|
16
|
+
import numpy as np
|
|
17
|
+
import numpy.typing as npt
|
|
18
|
+
|
|
19
|
+
DeviceLike: TypeAlias = Literal["cpu", "gpu"]
|
|
20
|
+
ScalarLike: TypeAlias = Union[int, float, bool]
|
|
21
|
+
AxisLike: TypeAlias = Union[int, Tuple[int, ...]]
|
|
22
|
+
ShapeLike: TypeAlias = Tuple[int, ...]
|
|
23
|
+
AxisShapeLike: TypeAlias = Union[int, ShapeLike]
|
|
24
|
+
Order: TypeAlias = Literal['K', 'A', 'C', 'F']
|
|
25
|
+
Casting: TypeAlias = Literal['no', 'equiv', 'safe', 'same_kind', 'unsafe']
|
|
26
|
+
|
|
27
|
+
DTypeLike: TypeAlias = npt.DTypeLike
|
|
28
|
+
floating: TypeAlias = np.floating
|
|
29
|
+
float16: TypeAlias = np.float16
|
|
30
|
+
float32: TypeAlias = np.float32
|
|
31
|
+
float64: TypeAlias = np.float64
|
|
32
|
+
integer: TypeAlias = np.integer
|
|
33
|
+
int8: TypeAlias = np.int8
|
|
34
|
+
int16: TypeAlias = np.int16
|
|
35
|
+
int32: TypeAlias = np.int32
|
|
36
|
+
int64: TypeAlias = np.int64
|
|
37
|
+
double: TypeAlias = np.double
|
|
38
|
+
|
|
39
|
+
ArrayLike = Union[npt.NDArray, ScalarLike]
|
|
40
|
+
ArrayLikeBool = npt.NDArray[np.bool_]
|
|
41
|
+
OperandLike: TypeAlias = Union[ArrayLike, TensorLike]
|
|
42
|
+
|
|
43
|
+
class Func_2in_1out_Protocol(Protocol):
|
|
44
|
+
def __call__(
|
|
45
|
+
self,
|
|
46
|
+
x1: OperandLike,
|
|
47
|
+
x2: OperandLike,
|
|
48
|
+
*,
|
|
49
|
+
device: DeviceLike = "cpu",
|
|
50
|
+
requires_grad: bool = True,
|
|
51
|
+
**kwds: Any
|
|
52
|
+
) -> TensorLike: ...
|
|
53
|
+
|
|
54
|
+
class Op_2in_1out_Protocol(Protocol):
|
|
55
|
+
def __call__(
|
|
56
|
+
self,
|
|
57
|
+
x1: ArrayLike,
|
|
58
|
+
x2: ArrayLike,
|
|
59
|
+
*,
|
|
60
|
+
device: DeviceLike = "cpu",
|
|
61
|
+
**kwds: Any
|
|
62
|
+
) -> ArrayLike: ...
|
|
63
|
+
|
|
64
|
+
class Diff_2in_1out_Protocol(Protocol):
|
|
65
|
+
def __call__(
|
|
66
|
+
self,
|
|
67
|
+
result: TensorLike,
|
|
68
|
+
x1: ArrayLike,
|
|
69
|
+
x2: ArrayLike
|
|
70
|
+
) -> Tuple[Callable[[], ArrayLike], Callable[[], ArrayLike]]: ...
|
|
71
|
+
|
|
72
|
+
class Func_1in_1out_Protocol(Protocol):
|
|
73
|
+
def __call__(
|
|
74
|
+
self,
|
|
75
|
+
x: OperandLike,
|
|
76
|
+
*,
|
|
77
|
+
device: DeviceLike = "cpu",
|
|
78
|
+
requires_grad: bool = True,
|
|
79
|
+
**kwds: Any
|
|
80
|
+
) -> TensorLike: ...
|
|
81
|
+
|
|
82
|
+
class Op_1in_1out_Protocol(Protocol):
|
|
83
|
+
def __call__(
|
|
84
|
+
self,
|
|
85
|
+
x: ArrayLike,
|
|
86
|
+
*,
|
|
87
|
+
device: DeviceLike = "cpu",
|
|
88
|
+
**kwds: Any
|
|
89
|
+
) -> ArrayLike: ...
|
|
90
|
+
|
|
91
|
+
class Diff_1in_1out_Protocol(Protocol):
|
|
92
|
+
def __call__(
|
|
93
|
+
self,
|
|
94
|
+
result: TensorLike,
|
|
95
|
+
x: ArrayLike,
|
|
96
|
+
) -> Callable[[], ArrayLike]: ...
|
|
97
|
+
|
|
98
|
+
__all__ = [
|
|
99
|
+
'DTypeLike',
|
|
100
|
+
'floating',
|
|
101
|
+
'float16',
|
|
102
|
+
'float32',
|
|
103
|
+
'float64',
|
|
104
|
+
'integer',
|
|
105
|
+
'int8',
|
|
106
|
+
'int16',
|
|
107
|
+
'int16',
|
|
108
|
+
'int64',
|
|
109
|
+
'double',
|
|
110
|
+
'DeviceLike',
|
|
111
|
+
'AxisShapeLike',
|
|
112
|
+
'Order',
|
|
113
|
+
'AxisLike',
|
|
114
|
+
'ScalarLike',
|
|
115
|
+
'ArrayLike',
|
|
116
|
+
'ArrayLikeBool',
|
|
117
|
+
'ShapeLike',
|
|
118
|
+
'TensorLike',
|
|
119
|
+
'OperandLike',
|
|
120
|
+
'Op_2in_1out_Protocol',
|
|
121
|
+
'Diff_2in_1out_Protocol',
|
|
122
|
+
'Func_2in_1out_Protocol',
|
|
123
|
+
'Op_1in_1out_Protocol',
|
|
124
|
+
'Diff_1in_1out_Protocol',
|
|
125
|
+
'Func_1in_1out_Protocol'
|
|
126
|
+
]
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from ..DepthTensor import (Tensor, differentiate, random, create_1in_1out, Op_2in_1out_Protocol, Func_2in_1out_Protocol, OperandLike, Diff_2in_1out_Protocol, ArrayLike, DeviceLike, CuPyNotFound, CUPY_NOT_FOUND_MSG)
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
try:
|
|
5
|
+
import cupy as cp
|
|
6
|
+
except:
|
|
7
|
+
cp = None
|
|
8
|
+
|
|
9
|
+
def op(x: ArrayLike, device: DeviceLike = "cpu", **kwds) -> ArrayLike:
|
|
10
|
+
if device == "cpu":
|
|
11
|
+
return np.square(x)
|
|
12
|
+
else:
|
|
13
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
14
|
+
return cp.square(x)
|
|
15
|
+
|
|
16
|
+
def diff(result: Tensor, x: ArrayLike):
|
|
17
|
+
def x_diff() -> ArrayLike:
|
|
18
|
+
return 2 * x
|
|
19
|
+
return x_diff
|
|
20
|
+
|
|
21
|
+
func = create_1in_1out(op, diff)
|
|
22
|
+
a = Tensor(6.0, device="gpu", requires_grad=True)
|
|
23
|
+
b = func(a, device="gpu")
|
|
24
|
+
print(b)
|
|
25
|
+
differentiate(b)
|
|
26
|
+
print(a.grad)
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
from typing import TypeAlias, Union, Literal, Tuple, TYPE_CHECKING
|
|
2
|
-
|
|
3
|
-
if TYPE_CHECKING:
|
|
4
|
-
from .tensor import Tensor
|
|
5
|
-
TensorLike: TypeAlias = 'Tensor'
|
|
6
|
-
|
|
7
|
-
import numpy as np
|
|
8
|
-
import numpy.typing as npt
|
|
9
|
-
|
|
10
|
-
DeviceLike: TypeAlias = Literal["cpu", "gpu"]
|
|
11
|
-
ScalarLike: TypeAlias = Union[int, float, bool, complex]
|
|
12
|
-
AxisLike: TypeAlias = Union[int, Tuple[int, ...]]
|
|
13
|
-
ShapeLike: TypeAlias = Tuple[int, ...]
|
|
14
|
-
AxisShapeLike: TypeAlias = Union[int, ShapeLike]
|
|
15
|
-
Order: TypeAlias = Literal['K', 'A', 'C', 'F']
|
|
16
|
-
Casting: TypeAlias = Literal['no', 'equiv', 'safe', 'same_kind', 'unsafe']
|
|
17
|
-
|
|
18
|
-
DTypeLike: TypeAlias = npt.DTypeLike
|
|
19
|
-
floating: TypeAlias = np.floating
|
|
20
|
-
float16: TypeAlias = np.float16
|
|
21
|
-
float32: TypeAlias = np.float32
|
|
22
|
-
float64: TypeAlias = np.float64
|
|
23
|
-
integer: TypeAlias = np.integer
|
|
24
|
-
int8: TypeAlias = np.int8
|
|
25
|
-
int16: TypeAlias = np.int16
|
|
26
|
-
int32: TypeAlias = np.int32
|
|
27
|
-
int64: TypeAlias = np.int64
|
|
28
|
-
double: TypeAlias = np.double
|
|
29
|
-
|
|
30
|
-
ArrayLike = npt.ArrayLike
|
|
31
|
-
ArrayLikeBool = npt.NDArray[np.bool_]
|
|
32
|
-
OperandLike: TypeAlias = Union[ArrayLike, TensorLike]
|
|
33
|
-
|
|
34
|
-
__all__ = [
|
|
35
|
-
'DTypeLike',
|
|
36
|
-
'floating', 'float16', 'float32', 'float64',
|
|
37
|
-
'integer', 'int8', 'int16', 'int16', 'int64',
|
|
38
|
-
'double',
|
|
39
|
-
'DeviceLike', 'AxisShapeLike', 'Order', 'AxisLike', 'ScalarLike', 'ArrayLike', 'ArrayLikeBool', 'ShapeLike',
|
|
40
|
-
'TensorLike', 'OperandLike'
|
|
41
|
-
]
|
|
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
|