DepthTensor 0.0.1__tar.gz → 1.0.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.0.0/PKG-INFO +26 -0
- {depthtensor-0.0.1 → depthtensor-1.0.0}/README.md +2 -2
- {depthtensor-0.0.1 → depthtensor-1.0.0}/pyproject.toml +2 -1
- depthtensor-1.0.0/src/DepthTensor/__init__.py +4 -0
- depthtensor-1.0.0/src/DepthTensor/_core/__init__.py +4 -0
- depthtensor-1.0.0/src/DepthTensor/_core/exceptions.py +8 -0
- depthtensor-1.0.0/src/DepthTensor/_core/ops/__init__.py +6 -0
- depthtensor-1.0.0/src/DepthTensor/_core/ops/comparison.py +109 -0
- depthtensor-1.0.0/src/DepthTensor/_core/ops/creation.py +77 -0
- depthtensor-1.0.0/src/DepthTensor/_core/ops/diff.py +88 -0
- depthtensor-1.0.0/src/DepthTensor/_core/ops/elementwise.py +206 -0
- depthtensor-1.0.0/src/DepthTensor/_core/ops/reduction.py +76 -0
- {depthtensor-0.0.1 → depthtensor-1.0.0}/src/DepthTensor/_core/utils.py +40 -12
- depthtensor-1.0.0/src/DepthTensor/tensor.py +449 -0
- depthtensor-1.0.0/src/DepthTensor/typing.py +40 -0
- depthtensor-1.0.0/src/DepthTensor.egg-info/PKG-INFO +26 -0
- {depthtensor-0.0.1 → depthtensor-1.0.0}/src/DepthTensor.egg-info/SOURCES.txt +9 -5
- depthtensor-1.0.0/src/example/example.py +10 -0
- {depthtensor-0.0.1 → depthtensor-1.0.0}/tests/test_tensor.py +6 -6
- depthtensor-0.0.1/PKG-INFO +0 -10
- depthtensor-0.0.1/src/DepthTensor/__init__.py +0 -6
- depthtensor-0.0.1/src/DepthTensor/_core/__init__.py +0 -2
- depthtensor-0.0.1/src/DepthTensor/_core/api.py +0 -198
- depthtensor-0.0.1/src/DepthTensor/autodiff.py +0 -46
- depthtensor-0.0.1/src/DepthTensor/cpu.py +0 -133
- depthtensor-0.0.1/src/DepthTensor/gpu.py +0 -144
- depthtensor-0.0.1/src/DepthTensor/types.py +0 -25
- depthtensor-0.0.1/src/DepthTensor.egg-info/PKG-INFO +0 -10
- depthtensor-0.0.1/src/example/example.py +0 -7
- {depthtensor-0.0.1 → depthtensor-1.0.0}/LICENSE +0 -0
- {depthtensor-0.0.1 → depthtensor-1.0.0}/setup.cfg +0 -0
- {depthtensor-0.0.1 → depthtensor-1.0.0}/src/DepthTensor.egg-info/dependency_links.txt +0 -0
- {depthtensor-0.0.1 → depthtensor-1.0.0}/src/DepthTensor.egg-info/requires.txt +0 -0
- {depthtensor-0.0.1 → depthtensor-1.0.0}/src/DepthTensor.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: DepthTensor
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Description-Content-Type: text/markdown
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Dist: numpy
|
|
7
|
+
Provides-Extra: test
|
|
8
|
+
Requires-Dist: pytest; extra == "test"
|
|
9
|
+
Provides-Extra: gpu
|
|
10
|
+
Requires-Dist: cupy-cuda12x; extra == "gpu"
|
|
11
|
+
Dynamic: license-file
|
|
12
|
+
|
|
13
|
+
# DepthTensor
|
|
14
|
+
This is a NumPy/CuPy wrapper module designed for tensor computations. DepthTensor was created to operate in tandem with another library called Depth, a deep-learning library.
|
|
15
|
+
|
|
16
|
+
# Functionalities
|
|
17
|
+
|
|
18
|
+
* NumPy functionalities (N-dimensional array, broadcasting, etc.)
|
|
19
|
+
|
|
20
|
+
* Auto-differentiation
|
|
21
|
+
|
|
22
|
+
# Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install DepthTensor
|
|
26
|
+
```
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
from typing import (
|
|
2
|
+
Union,
|
|
3
|
+
Optional,
|
|
4
|
+
Tuple,
|
|
5
|
+
overload
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
from ...typing import (
|
|
9
|
+
ArrayLike,
|
|
10
|
+
TensorLike,
|
|
11
|
+
DeviceLike
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
from ..exceptions import (
|
|
15
|
+
CuPyNotFound, CUPY_NOT_FOUND_MSG
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
import numpy as np
|
|
19
|
+
try:
|
|
20
|
+
import cupy as cp
|
|
21
|
+
except (ImportError, ModuleNotFoundError):
|
|
22
|
+
cp = None
|
|
23
|
+
|
|
24
|
+
###
|
|
25
|
+
###
|
|
26
|
+
###
|
|
27
|
+
|
|
28
|
+
@overload
|
|
29
|
+
def where(
|
|
30
|
+
condition: Union[ArrayLike, TensorLike],
|
|
31
|
+
/
|
|
32
|
+
) -> Tuple[TensorLike, ...]: ...
|
|
33
|
+
|
|
34
|
+
@overload
|
|
35
|
+
def where(
|
|
36
|
+
condition: Union[ArrayLike, TensorLike],
|
|
37
|
+
x: Optional[Union[ArrayLike, TensorLike]],
|
|
38
|
+
y: Optional[Union[ArrayLike, TensorLike]],
|
|
39
|
+
/,
|
|
40
|
+
*,
|
|
41
|
+
device: DeviceLike = "cpu"
|
|
42
|
+
) -> TensorLike: ...
|
|
43
|
+
|
|
44
|
+
def where(
|
|
45
|
+
condition: Union[ArrayLike, TensorLike],
|
|
46
|
+
x: Optional[Union[ArrayLike, TensorLike]] = None,
|
|
47
|
+
y: Optional[Union[ArrayLike, TensorLike]] = None,
|
|
48
|
+
/,
|
|
49
|
+
*,
|
|
50
|
+
device: DeviceLike = "cpu"
|
|
51
|
+
) -> Union[Tuple[TensorLike, ...], TensorLike]:
|
|
52
|
+
from ...tensor import Tensor
|
|
53
|
+
#* One parameter overload
|
|
54
|
+
if x is None and y is None:
|
|
55
|
+
if isinstance(condition, TensorLike):
|
|
56
|
+
data = condition.data
|
|
57
|
+
else:
|
|
58
|
+
if device == "cpu":
|
|
59
|
+
data = np.asarray(condition)
|
|
60
|
+
else:
|
|
61
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
62
|
+
data = cp.asarray(condition)
|
|
63
|
+
if device == "cpu":
|
|
64
|
+
result = np.where(data)
|
|
65
|
+
else:
|
|
66
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
67
|
+
result = cp.where(data)
|
|
68
|
+
return tuple([Tensor(array, device=device) for array in result])
|
|
69
|
+
elif x is not None and y is not None:
|
|
70
|
+
if isinstance(condition, TensorLike):
|
|
71
|
+
data = condition.data
|
|
72
|
+
else:
|
|
73
|
+
if device == "cpu":
|
|
74
|
+
data = np.asarray(condition)
|
|
75
|
+
else:
|
|
76
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
77
|
+
data = cp.asarray(condition)
|
|
78
|
+
if isinstance(x, TensorLike):
|
|
79
|
+
x_data = x.data
|
|
80
|
+
else:
|
|
81
|
+
if device == "cpu":
|
|
82
|
+
x_data = np.asarray(x)
|
|
83
|
+
else:
|
|
84
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
85
|
+
x_data = cp.asarray(x)
|
|
86
|
+
if isinstance(y, TensorLike):
|
|
87
|
+
y_data = y.data
|
|
88
|
+
else:
|
|
89
|
+
if device == "cpu":
|
|
90
|
+
y_data = np.asarray(y)
|
|
91
|
+
else:
|
|
92
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
93
|
+
y_data = cp.asarray(y)
|
|
94
|
+
if device == "cpu":
|
|
95
|
+
result = np.where(data, x_data, y_data)
|
|
96
|
+
else:
|
|
97
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
98
|
+
result = cp.where(data, x_data, y_data)
|
|
99
|
+
return Tensor(result, device=device)
|
|
100
|
+
else:
|
|
101
|
+
raise ValueError("Both x and y parameters must be given.")
|
|
102
|
+
|
|
103
|
+
###
|
|
104
|
+
###
|
|
105
|
+
###
|
|
106
|
+
|
|
107
|
+
__all__ = [
|
|
108
|
+
'where'
|
|
109
|
+
]
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
from typing import (
|
|
2
|
+
Optional
|
|
3
|
+
)
|
|
4
|
+
|
|
5
|
+
from ...typing import (
|
|
6
|
+
TensorLike, DTypeLike, Order,
|
|
7
|
+
AxisShapeLike
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
from ..exceptions import (
|
|
11
|
+
CuPyNotFound, CUPY_NOT_FOUND_MSG
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
import numpy as np
|
|
15
|
+
try:
|
|
16
|
+
import cupy as cp
|
|
17
|
+
except (ImportError, ModuleNotFoundError):
|
|
18
|
+
cp = None
|
|
19
|
+
|
|
20
|
+
###
|
|
21
|
+
###
|
|
22
|
+
###
|
|
23
|
+
|
|
24
|
+
def zeros_like(
|
|
25
|
+
a: TensorLike,
|
|
26
|
+
/,
|
|
27
|
+
*,
|
|
28
|
+
dtype: Optional[DTypeLike] = None,
|
|
29
|
+
order: Order = 'K',
|
|
30
|
+
subok: bool = True,
|
|
31
|
+
shape: Optional[AxisShapeLike] = None
|
|
32
|
+
) -> TensorLike:
|
|
33
|
+
from ...tensor import Tensor
|
|
34
|
+
if a.device == "cpu":
|
|
35
|
+
return Tensor(
|
|
36
|
+
a.zeros_like(a, dtype=dtype, order=order, subok=subok, shape=shape),
|
|
37
|
+
device=a.device
|
|
38
|
+
)
|
|
39
|
+
else:
|
|
40
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
41
|
+
return Tensor(
|
|
42
|
+
#! As of the time writing this, CuPy does not support subok.
|
|
43
|
+
cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape),
|
|
44
|
+
device=a.device
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
def ones_like(
|
|
48
|
+
a: TensorLike,
|
|
49
|
+
/,
|
|
50
|
+
*,
|
|
51
|
+
dtype: Optional[DTypeLike] = None,
|
|
52
|
+
order: Order = 'K',
|
|
53
|
+
subok: bool = True,
|
|
54
|
+
shape: Optional[AxisShapeLike] = None
|
|
55
|
+
) -> TensorLike:
|
|
56
|
+
from ...tensor import Tensor
|
|
57
|
+
if a.is_cpu():
|
|
58
|
+
return Tensor(
|
|
59
|
+
np.ones_like(a, dtype=dtype, order=order, subok=subok, shape=shape),
|
|
60
|
+
device=a.device
|
|
61
|
+
)
|
|
62
|
+
else:
|
|
63
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
64
|
+
return Tensor(
|
|
65
|
+
#! As of the time writing this, CuPy does not support subok.
|
|
66
|
+
cp.ones_like(a, dtype=dtype, order=order, subok=None, shape=shape),
|
|
67
|
+
device=a.device
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
###
|
|
71
|
+
###
|
|
72
|
+
###
|
|
73
|
+
|
|
74
|
+
__all__ = [
|
|
75
|
+
'zeros_like',
|
|
76
|
+
'ones_like'
|
|
77
|
+
]
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
from ...typing import (
|
|
2
|
+
TensorLike
|
|
3
|
+
)
|
|
4
|
+
|
|
5
|
+
from ..utils import sum_to_shape
|
|
6
|
+
|
|
7
|
+
###
|
|
8
|
+
### Arithmetics
|
|
9
|
+
###
|
|
10
|
+
|
|
11
|
+
def add_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
|
|
12
|
+
if not result.requires_grad: return result
|
|
13
|
+
def backward() -> None:
|
|
14
|
+
if x1.requires_grad:
|
|
15
|
+
x1.grad += sum_to_shape(result.grad, x1.shape, x1.device)
|
|
16
|
+
if x2.requires_grad:
|
|
17
|
+
x2.grad += sum_to_shape(result.grad, x2.shape, x2.device)
|
|
18
|
+
result.backward = backward
|
|
19
|
+
return result
|
|
20
|
+
|
|
21
|
+
def subtract_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
|
|
22
|
+
if not result.requires_grad: return result
|
|
23
|
+
def backward() -> None:
|
|
24
|
+
if x1.requires_grad:
|
|
25
|
+
x1.grad += sum_to_shape(result.grad, x1.shape, x1.device)
|
|
26
|
+
if x2.requires_grad:
|
|
27
|
+
x2.grad += sum_to_shape(-result.grad, x2.shape, x2.device)
|
|
28
|
+
result.backward = backward
|
|
29
|
+
return result
|
|
30
|
+
|
|
31
|
+
def multiply_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
|
|
32
|
+
if not result.requires_grad: return result
|
|
33
|
+
def backward() -> None:
|
|
34
|
+
if x1.requires_grad:
|
|
35
|
+
x1.grad += sum_to_shape(result.grad * x2.data, x1.shape, x1.device)
|
|
36
|
+
if x2.requires_grad:
|
|
37
|
+
x2.grad += sum_to_shape(result.grad * x1.data, x2.shape, x2.device)
|
|
38
|
+
result.backward = backward
|
|
39
|
+
return result
|
|
40
|
+
|
|
41
|
+
def matmul_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
|
|
42
|
+
if not result.requires_grad: return result
|
|
43
|
+
def backward() -> None:
|
|
44
|
+
if x1.requires_grad:
|
|
45
|
+
x1.grad += sum_to_shape(result.grad @ x2.data.swapaxes(-2,-1), x1.shape, x1.device)
|
|
46
|
+
if x2.requires_grad:
|
|
47
|
+
x2.grad += sum_to_shape(x1.data.swapaxes(-2, -1) @ result.grad, x2.shape, x2.device)
|
|
48
|
+
result.backward = backward
|
|
49
|
+
return result
|
|
50
|
+
|
|
51
|
+
def divide_diff(result: TensorLike, x1: TensorLike, x2: TensorLike) -> TensorLike:
|
|
52
|
+
if not result.requires_grad: return result
|
|
53
|
+
def backward() -> None:
|
|
54
|
+
if x1.requires_grad:
|
|
55
|
+
x1.grad += sum_to_shape(result.grad / x2.data, x1.shape, x1.device)
|
|
56
|
+
if x2.requires_grad:
|
|
57
|
+
x2.grad += sum_to_shape(result.grad * (x1.data * -x2.data**-2), x2.shape, x2.device)
|
|
58
|
+
result.backward = backward
|
|
59
|
+
return result
|
|
60
|
+
|
|
61
|
+
def negative_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
62
|
+
if not result.requires_grad: return result
|
|
63
|
+
def backward() -> None:
|
|
64
|
+
if x.requires_grad:
|
|
65
|
+
x.grad += sum_to_shape(-result.grad, x.shape, x.device)
|
|
66
|
+
result.backward = backward
|
|
67
|
+
return result
|
|
68
|
+
|
|
69
|
+
###
|
|
70
|
+
### Exponents/Logarithms
|
|
71
|
+
###
|
|
72
|
+
|
|
73
|
+
def exp_diff(result: TensorLike, x: TensorLike) -> TensorLike:
|
|
74
|
+
if not result.requires_grad: return result
|
|
75
|
+
def backward() -> None:
|
|
76
|
+
if x.requires_grad:
|
|
77
|
+
x.grad += sum_to_shape(result.grad * result.data, x.shape, x.device)
|
|
78
|
+
result.backward = backward
|
|
79
|
+
return result
|
|
80
|
+
|
|
81
|
+
###
|
|
82
|
+
###
|
|
83
|
+
###
|
|
84
|
+
|
|
85
|
+
__all__ = [
|
|
86
|
+
'add_diff', 'subtract_diff', 'multiply_diff', 'matmul_diff', 'divide_diff',
|
|
87
|
+
'negative_diff', 'exp_diff'
|
|
88
|
+
]
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
from typing import Optional, Union, Any
|
|
2
|
+
|
|
3
|
+
from ...typing import (
|
|
4
|
+
TensorLike, DTypeLike, Casting,
|
|
5
|
+
Order, ArrayLikeBool
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
from ..exceptions import (
|
|
9
|
+
DeviceMismatch, DEVICE_MISMATCH_MSG,
|
|
10
|
+
CuPyNotFound, CUPY_NOT_FOUND_MSG
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
try:
|
|
15
|
+
import cupy as cp
|
|
16
|
+
except (ImportError, ModuleNotFoundError):
|
|
17
|
+
cp = None
|
|
18
|
+
|
|
19
|
+
###
|
|
20
|
+
### Arithmetics
|
|
21
|
+
###
|
|
22
|
+
|
|
23
|
+
def add(
|
|
24
|
+
x1: TensorLike,
|
|
25
|
+
x2: TensorLike,
|
|
26
|
+
/,
|
|
27
|
+
out: Optional[Union[np.ndarray, Any]] = None,
|
|
28
|
+
*,
|
|
29
|
+
in_place: bool = False,
|
|
30
|
+
where: Union[ArrayLikeBool, bool] = True,
|
|
31
|
+
casting: Casting = 'same_kind',
|
|
32
|
+
order: Order = 'K',
|
|
33
|
+
dtype: Optional[DTypeLike] = None,
|
|
34
|
+
subok: bool = True
|
|
35
|
+
) -> TensorLike:
|
|
36
|
+
if not x1.is_device(x2.device): raise DeviceMismatch(DEVICE_MISMATCH_MSG)
|
|
37
|
+
from ...tensor import Tensor
|
|
38
|
+
if x1.is_cpu():
|
|
39
|
+
y = np.add(x1.data, x2.data, out=out, dtype=dtype, where=where, casting=casting, order=order, subok=subok)
|
|
40
|
+
else:
|
|
41
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
42
|
+
y = cp.add(x1.data, x2.data, out=out, dtype=dtype, casting=casting)
|
|
43
|
+
if in_place:
|
|
44
|
+
x1.data = y
|
|
45
|
+
return x1
|
|
46
|
+
return Tensor(y, device=x1.device, prev=(x1, x2), requires_grad=x1.requires_grad or x2.requires_grad)
|
|
47
|
+
|
|
48
|
+
def subtract(
|
|
49
|
+
x1: TensorLike,
|
|
50
|
+
x2: TensorLike,
|
|
51
|
+
/,
|
|
52
|
+
out: Optional[Union[np.ndarray, Any]] = None,
|
|
53
|
+
*,
|
|
54
|
+
in_place: bool = False,
|
|
55
|
+
where: Union[ArrayLikeBool, bool] = True,
|
|
56
|
+
casting: Casting = 'same_kind',
|
|
57
|
+
order: Order = 'K',
|
|
58
|
+
dtype: Optional[DTypeLike] = None,
|
|
59
|
+
subok: bool = True
|
|
60
|
+
) -> TensorLike:
|
|
61
|
+
if not x1.is_device(x2.device): raise DeviceMismatch(DEVICE_MISMATCH_MSG)
|
|
62
|
+
from ...tensor import Tensor
|
|
63
|
+
if x1.is_cpu():
|
|
64
|
+
y = np.subtract(x1.data, x2.data, out=out, dtype=dtype, where=where, casting=casting, order=order, subok=subok)
|
|
65
|
+
else:
|
|
66
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
67
|
+
y = cp.subtract(x1.data, x2.data, out=out, dtype=dtype, casting=casting)
|
|
68
|
+
if in_place:
|
|
69
|
+
x1.data = y
|
|
70
|
+
return x1
|
|
71
|
+
return Tensor(y, device=x1.device, prev=(x1, x2), requires_grad=x1.requires_grad or x2.requires_grad)
|
|
72
|
+
|
|
73
|
+
def multiply(
|
|
74
|
+
x1: TensorLike,
|
|
75
|
+
x2: TensorLike,
|
|
76
|
+
/,
|
|
77
|
+
out: Optional[Union[np.ndarray, Any]] = None,
|
|
78
|
+
*,
|
|
79
|
+
in_place: bool = False,
|
|
80
|
+
where: Union[ArrayLikeBool, bool] = True,
|
|
81
|
+
casting: Casting = 'same_kind',
|
|
82
|
+
order: Order = 'K',
|
|
83
|
+
dtype: Optional[DTypeLike] = None,
|
|
84
|
+
subok: bool = True
|
|
85
|
+
) -> TensorLike:
|
|
86
|
+
if not x1.is_device(x2.device): raise DeviceMismatch(DEVICE_MISMATCH_MSG)
|
|
87
|
+
from ...tensor import Tensor
|
|
88
|
+
if x1.is_cpu():
|
|
89
|
+
y = np.multiply(x1.data, x2.data, out=out, dtype=dtype, where=where, casting=casting, order=order, subok=subok)
|
|
90
|
+
else:
|
|
91
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
92
|
+
y = cp.multiply(x1.data, x2.data, out=out, dtype=dtype, casting=casting)
|
|
93
|
+
if in_place:
|
|
94
|
+
x1.data = y
|
|
95
|
+
return x1
|
|
96
|
+
return Tensor(y, device=x1.device, prev=(x1, x2), requires_grad=x1.requires_grad or x2.requires_grad)
|
|
97
|
+
|
|
98
|
+
def matmul(
|
|
99
|
+
x1: TensorLike,
|
|
100
|
+
x2: TensorLike,
|
|
101
|
+
/,
|
|
102
|
+
out: Optional[Union[np.ndarray, Any]] = None,
|
|
103
|
+
*,
|
|
104
|
+
in_place: bool = False,
|
|
105
|
+
casting: Casting = 'same_kind',
|
|
106
|
+
order: Order = 'K',
|
|
107
|
+
dtype: Optional[DTypeLike] = None,
|
|
108
|
+
subok: bool = True
|
|
109
|
+
) -> TensorLike:
|
|
110
|
+
if not x1.is_device(x2.device): raise DeviceMismatch(DEVICE_MISMATCH_MSG)
|
|
111
|
+
from ...tensor import Tensor
|
|
112
|
+
if x1.is_cpu():
|
|
113
|
+
y = np.matmul(x1.data, x2.data, out=out, casting=casting, order=order, dtype=dtype, subok=subok)
|
|
114
|
+
else:
|
|
115
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
116
|
+
y = cp.matmul(x1.data, x2.data, out=out, dtype=dtype, casting=casting)
|
|
117
|
+
if in_place:
|
|
118
|
+
x1.data = y
|
|
119
|
+
return x1
|
|
120
|
+
return Tensor(y, device=x1.device, prev=(x1, x2), requires_grad=x1.requires_grad or x2.requires_grad)
|
|
121
|
+
|
|
122
|
+
def divide(
|
|
123
|
+
x1: TensorLike,
|
|
124
|
+
x2: TensorLike,
|
|
125
|
+
/,
|
|
126
|
+
out: Optional[Union[np.ndarray, Any]] = None,
|
|
127
|
+
*,
|
|
128
|
+
in_place: bool = False,
|
|
129
|
+
where: Union[ArrayLikeBool, bool] = True,
|
|
130
|
+
casting: Casting = 'same_kind',
|
|
131
|
+
order: Order = 'K',
|
|
132
|
+
dtype: Optional[DTypeLike] = None,
|
|
133
|
+
subok: bool = True
|
|
134
|
+
) -> TensorLike:
|
|
135
|
+
if not x1.is_device(x2.device): raise DeviceMismatch(DEVICE_MISMATCH_MSG)
|
|
136
|
+
from ...tensor import Tensor
|
|
137
|
+
if x1.is_cpu():
|
|
138
|
+
y = np.divide(x1.data, x2.data, out=out, dtype=dtype, where=where, casting=casting, order=order, subok=subok)
|
|
139
|
+
else:
|
|
140
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
141
|
+
y = cp.divide(x1.data, x2.data, out=out, dtype=dtype, casting=casting)
|
|
142
|
+
if in_place:
|
|
143
|
+
x1.data = y
|
|
144
|
+
return x1
|
|
145
|
+
return Tensor(y, device=x1.device, prev=(x1, x2), requires_grad=x1.requires_grad or x2.requires_grad)
|
|
146
|
+
|
|
147
|
+
def negative(
|
|
148
|
+
x: TensorLike,
|
|
149
|
+
/,
|
|
150
|
+
out: Optional[Union[np.ndarray, Any]] = None,
|
|
151
|
+
*,
|
|
152
|
+
in_place: bool = False,
|
|
153
|
+
where: Union[ArrayLikeBool, bool] = True,
|
|
154
|
+
casting: Casting = 'same_kind',
|
|
155
|
+
order: Order = 'K',
|
|
156
|
+
dtype: Optional[DTypeLike] = None,
|
|
157
|
+
subok: bool = True
|
|
158
|
+
) -> TensorLike:
|
|
159
|
+
from ...tensor import Tensor
|
|
160
|
+
if x.is_cpu():
|
|
161
|
+
y = np.negative(x.data, out=out, dtype=dtype, where=where, casting=casting, order=order, subok=subok)
|
|
162
|
+
else:
|
|
163
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
164
|
+
y = cp.negative(x.data, out=out, dtype=dtype, casting=casting)
|
|
165
|
+
if in_place:
|
|
166
|
+
x.data = y
|
|
167
|
+
return x
|
|
168
|
+
return Tensor(y, device=x.device, prev=(x,), requires_grad=x.requires_grad)
|
|
169
|
+
|
|
170
|
+
###
|
|
171
|
+
### Exponents/Logarithms
|
|
172
|
+
###
|
|
173
|
+
|
|
174
|
+
def exp(
|
|
175
|
+
x: TensorLike,
|
|
176
|
+
/,
|
|
177
|
+
out: Optional[Union[np.ndarray, Any]] = None,
|
|
178
|
+
*,
|
|
179
|
+
in_place: bool = False,
|
|
180
|
+
where: Union[bool, ArrayLikeBool] = True,
|
|
181
|
+
casting: Casting = 'same_kind',
|
|
182
|
+
order: Order = 'K',
|
|
183
|
+
dtype: Optional[DTypeLike] = None,
|
|
184
|
+
subok: bool = True
|
|
185
|
+
) -> TensorLike:
|
|
186
|
+
from ...tensor import Tensor
|
|
187
|
+
if x.is_cpu():
|
|
188
|
+
y = np.exp(x.data, out=out, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
|
|
189
|
+
else:
|
|
190
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
191
|
+
y = cp.exp(x.data, out=out, casting=casting, dtype=dtype)
|
|
192
|
+
if in_place:
|
|
193
|
+
x.data = y
|
|
194
|
+
return x
|
|
195
|
+
return Tensor(y, device=x.device, prev=(x,), requires_grad=x.requires_grad)
|
|
196
|
+
|
|
197
|
+
###
|
|
198
|
+
###
|
|
199
|
+
###
|
|
200
|
+
|
|
201
|
+
__all__ = [
|
|
202
|
+
'add', 'subtract', 'multiply', 'matmul', 'divide',
|
|
203
|
+
'negative',
|
|
204
|
+
|
|
205
|
+
'exp',
|
|
206
|
+
]
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from typing import (
|
|
2
|
+
Optional,
|
|
3
|
+
Union,
|
|
4
|
+
Any
|
|
5
|
+
)
|
|
6
|
+
|
|
7
|
+
from ...typing import (
|
|
8
|
+
TensorLike,
|
|
9
|
+
ShapeLike,
|
|
10
|
+
ArrayLikeBool,
|
|
11
|
+
Casting,
|
|
12
|
+
Order,
|
|
13
|
+
DTypeLike
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
from ..exceptions import (
|
|
17
|
+
CuPyNotFound, CUPY_NOT_FOUND_MSG,
|
|
18
|
+
DeviceMismatch, DEVICE_MISMATCH_MSG
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
import numpy as np
|
|
22
|
+
try:
|
|
23
|
+
import cupy as cp
|
|
24
|
+
except (ImportError, ModuleNotFoundError):
|
|
25
|
+
cp = None
|
|
26
|
+
_NoValue = object()
|
|
27
|
+
|
|
28
|
+
###
|
|
29
|
+
###
|
|
30
|
+
###
|
|
31
|
+
|
|
32
|
+
def max(
|
|
33
|
+
a: TensorLike,
|
|
34
|
+
/,
|
|
35
|
+
*,
|
|
36
|
+
axis: Optional[ShapeLike] = None,
|
|
37
|
+
out: Optional[Union[np.ndarray, Any]] = None,
|
|
38
|
+
keepdims: bool = False,
|
|
39
|
+
initial: Any = _NoValue,
|
|
40
|
+
where: Union[bool, ArrayLikeBool] = True
|
|
41
|
+
) -> TensorLike:
|
|
42
|
+
from ...tensor import Tensor
|
|
43
|
+
if a.is_cpu():
|
|
44
|
+
y = np.max(a.data, axis=axis, out=out, keepdims=keepdims, initial=initial, where=where)
|
|
45
|
+
else:
|
|
46
|
+
if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
47
|
+
y = cp.max(a.data, axis=axis, out=out, keepdims=keepdims)
|
|
48
|
+
return Tensor(y, device=a.device)
|
|
49
|
+
|
|
50
|
+
def maximum(
|
|
51
|
+
x1: TensorLike,
|
|
52
|
+
x2: TensorLike,
|
|
53
|
+
/,
|
|
54
|
+
out: Optional[np.ndarray] = None,
|
|
55
|
+
*,
|
|
56
|
+
where: Union[bool, ArrayLikeBool] = True,
|
|
57
|
+
casting: Casting = 'same_kind',
|
|
58
|
+
order: Order = 'K',
|
|
59
|
+
dtype: Optional[DTypeLike] = None,
|
|
60
|
+
subok: bool = True
|
|
61
|
+
) -> TensorLike:
|
|
62
|
+
from ...tensor import Tensor
|
|
63
|
+
if not x1.is_device(x2.device): raise DeviceMismatch(DEVICE_MISMATCH_MSG)
|
|
64
|
+
if x1.is_cpu():
|
|
65
|
+
y = np.maximum(x1.data, x2.data, out=out, dtype=dtype, where=where, casting=casting, order=order, subok=subok)
|
|
66
|
+
else:
|
|
67
|
+
y = np.maximum(x1.data, x2.data, out=out, dtype=dtype, casting=casting)
|
|
68
|
+
return Tensor(y, device=x1.device)
|
|
69
|
+
|
|
70
|
+
###
|
|
71
|
+
###
|
|
72
|
+
###
|
|
73
|
+
|
|
74
|
+
__all__ = [
|
|
75
|
+
'max', 'maximum'
|
|
76
|
+
]
|