DepthTensor 1.8.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.
Files changed (30) hide show
  1. {depthtensor-1.8.0/src/DepthTensor.egg-info → depthtensor-1.10.0}/PKG-INFO +1 -1
  2. {depthtensor-1.8.0 → depthtensor-1.10.0}/pyproject.toml +1 -1
  3. depthtensor-1.10.0/src/DepthTensor/__init__.py +7 -0
  4. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor/_core/ops/comparison.py +8 -5
  5. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor/_core/ops/creation.py +1 -1
  6. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor/_core/ops/elementwise.py +118 -4
  7. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor/_core/utils.py +1 -1
  8. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor/tensor.py +4 -1
  9. depthtensor-1.10.0/src/DepthTensor/typing.py +126 -0
  10. {depthtensor-1.8.0 → depthtensor-1.10.0/src/DepthTensor.egg-info}/PKG-INFO +1 -1
  11. depthtensor-1.10.0/src/example/example.py +26 -0
  12. depthtensor-1.8.0/src/DepthTensor/__init__.py +0 -6
  13. depthtensor-1.8.0/src/DepthTensor/typing.py +0 -41
  14. depthtensor-1.8.0/src/example/example.py +0 -9
  15. {depthtensor-1.8.0 → depthtensor-1.10.0}/LICENSE +0 -0
  16. {depthtensor-1.8.0 → depthtensor-1.10.0}/README.md +0 -0
  17. {depthtensor-1.8.0 → depthtensor-1.10.0}/setup.cfg +0 -0
  18. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor/_core/__init__.py +0 -0
  19. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor/_core/exceptions.py +0 -0
  20. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor/_core/ops/__init__.py +0 -0
  21. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor/_core/ops/diff.py +0 -0
  22. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor/_core/ops/reduction.py +0 -0
  23. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor/_core/random/__init__.py +0 -0
  24. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor/_core/random/generator.py +0 -0
  25. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor/autodiff.py +0 -0
  26. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor.egg-info/SOURCES.txt +0 -0
  27. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor.egg-info/dependency_links.txt +0 -0
  28. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor.egg-info/requires.txt +0 -0
  29. {depthtensor-1.8.0 → depthtensor-1.10.0}/src/DepthTensor.egg-info/top_level.txt +0 -0
  30. {depthtensor-1.8.0 → depthtensor-1.10.0}/tests/test_tensor.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: DepthTensor
3
- Version: 1.8.0
3
+ Version: 1.10.0
4
4
  Description-Content-Type: text/markdown
5
5
  License-File: LICENSE
6
6
  Requires-Dist: numpy
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "DepthTensor"
7
- version = "1.8.0"
7
+ version = "1.10.0"
8
8
  readme = "README.md"
9
9
  dependencies = [
10
10
  "numpy"
@@ -0,0 +1,7 @@
1
+ from .tensor import Tensor
2
+ from .typing import *
3
+ from ._core import random, create_2in_1out, create_1in_1out
4
+ from ._core.exceptions import *
5
+ from .autodiff import differentiate
6
+
7
+ __version__ = "1.10.0"
@@ -36,7 +36,8 @@ def where(
36
36
  condition: OperandLike,
37
37
  /,
38
38
  *,
39
- device: DeviceLike = "cpu"
39
+ device: DeviceLike = "cpu",
40
+ requires_grad: bool = False
40
41
  ) -> Tuple[TensorLike, ...]: ...
41
42
 
42
43
  @overload
@@ -46,7 +47,8 @@ def where(
46
47
  y: Optional[OperandLike],
47
48
  /,
48
49
  *,
49
- device: DeviceLike = "cpu"
50
+ device: DeviceLike = "cpu",
51
+ requires_grad: bool = False
50
52
  ) -> TensorLike: ...
51
53
 
52
54
  def where(
@@ -55,7 +57,8 @@ def where(
55
57
  y: Optional[OperandLike] = None,
56
58
  /,
57
59
  *,
58
- device: DeviceLike = "cpu"
60
+ device: DeviceLike = "cpu",
61
+ requires_grad: bool = False
59
62
  ) -> Union[Tuple[TensorLike, ...], TensorLike]:
60
63
  from ...tensor import Tensor
61
64
  #* One parameter overload
@@ -66,7 +69,7 @@ def where(
66
69
  else:
67
70
  if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
68
71
  result = cp.where(data)
69
- return tuple([Tensor(array, device=device) for array in result])
72
+ return tuple([Tensor(array, device=device, requires_grad=requires_grad) for array in result])
70
73
  elif x is not None and y is not None:
71
74
  data = to_xp_array(condition, device=device)
72
75
  x_data = to_xp_array(x, device=device)
@@ -76,7 +79,7 @@ def where(
76
79
  else:
77
80
  if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
78
81
  result = cp.where(data, x_data, y_data)
79
- return Tensor(result, device=device)
82
+ return Tensor(result, device=device, requires_grad=requires_grad)
80
83
  else:
81
84
  raise ValueError("Both x and y parameters must be given.")
82
85
 
@@ -86,7 +86,7 @@ def ones_like(
86
86
  cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape),
87
87
  device=device_op, requires_grad=requires_grad
88
88
  )
89
-
89
+
90
90
  ###
91
91
  ###
92
92
  ###
@@ -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
- ### Arithmetics
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 or cp.ndarray
98
+ Convert data to numpy.ndarray/cp.ndarray.
99
99
  """
100
100
  from ..tensor import Tensor
101
101
  if isinstance(a, Tensor):
@@ -567,6 +567,7 @@ class Tensor():
567
567
  /,
568
568
  *,
569
569
  device: DeviceLike = "cpu",
570
+ requires_grad: bool = False
570
571
  ) -> Tuple[Tensor, ...]: ...
571
572
 
572
573
  @staticmethod
@@ -578,6 +579,7 @@ class Tensor():
578
579
  /,
579
580
  *,
580
581
  device: DeviceLike = "cpu",
582
+ requires_grad: bool = False
581
583
  ) -> Tensor: ...
582
584
 
583
585
  @staticmethod
@@ -588,6 +590,7 @@ class Tensor():
588
590
  /,
589
591
  *,
590
592
  device: DeviceLike = "cpu",
593
+ requires_grad: bool = False
591
594
  ) -> Union[Tuple[Tensor, ...], Tensor]:
592
595
  device = "cpu"
593
596
  if isinstance(condition, Tensor):
@@ -598,7 +601,7 @@ class Tensor():
598
601
  if y and isinstance(y, Tensor):
599
602
  if y.device != device:
600
603
  raise RuntimeError("Arguments, as tensors, must have the same device.")
601
- return where(condition, x, y, device=device)
604
+ return where(condition, x, y, device=device, requires_grad=requires_grad)
602
605
 
603
606
  @staticmethod
604
607
  def equal(
@@ -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
+ ]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: DepthTensor
3
- Version: 1.8.0
3
+ Version: 1.10.0
4
4
  Description-Content-Type: text/markdown
5
5
  License-File: LICENSE
6
6
  Requires-Dist: numpy
@@ -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,6 +0,0 @@
1
- from .tensor import Tensor
2
- from .typing import *
3
- from ._core import random
4
- from .autodiff import differentiate
5
-
6
- __version__ = "1.8.0"
@@ -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
- ]
@@ -1,9 +0,0 @@
1
- from ..DepthTensor import Tensor, differentiate, random
2
-
3
- a = Tensor([1, 2, 3], requires_grad=True)
4
- b = a**2**2 + 1
5
- print(a.requires_grad)
6
- print(b.requires_grad)
7
- differentiate(b)
8
- print(a.grad)
9
- print(b.prev)
File without changes
File without changes
File without changes