DepthTensor 1.1.2__tar.gz → 1.2.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.1.2/src/DepthTensor.egg-info → depthtensor-1.2.0}/PKG-INFO +1 -1
  2. {depthtensor-1.1.2 → depthtensor-1.2.0}/pyproject.toml +1 -1
  3. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor/__init__.py +1 -1
  4. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor/_core/exceptions.py +4 -0
  5. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor/_core/ops/comparison.py +72 -71
  6. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor/_core/ops/creation.py +32 -14
  7. depthtensor-1.2.0/src/DepthTensor/_core/ops/elementwise.py +362 -0
  8. depthtensor-1.2.0/src/DepthTensor/_core/ops/reduction.py +136 -0
  9. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor/_core/random/generator.py +25 -32
  10. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor/_core/utils.py +41 -4
  11. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor/tensor.py +59 -30
  12. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor/typing.py +2 -1
  13. {depthtensor-1.1.2 → depthtensor-1.2.0/src/DepthTensor.egg-info}/PKG-INFO +1 -1
  14. depthtensor-1.2.0/src/example/example.py +4 -0
  15. depthtensor-1.1.2/src/DepthTensor/_core/ops/elementwise.py +0 -356
  16. depthtensor-1.1.2/src/DepthTensor/_core/ops/reduction.py +0 -96
  17. depthtensor-1.1.2/src/example/example.py +0 -7
  18. {depthtensor-1.1.2 → depthtensor-1.2.0}/LICENSE +0 -0
  19. {depthtensor-1.1.2 → depthtensor-1.2.0}/README.md +0 -0
  20. {depthtensor-1.1.2 → depthtensor-1.2.0}/setup.cfg +0 -0
  21. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor/_core/__init__.py +0 -0
  22. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor/_core/ops/__init__.py +0 -0
  23. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor/_core/ops/diff.py +0 -0
  24. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor/_core/random/__init__.py +0 -0
  25. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor/autodiff.py +0 -0
  26. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor.egg-info/SOURCES.txt +0 -0
  27. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor.egg-info/dependency_links.txt +0 -0
  28. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor.egg-info/requires.txt +0 -0
  29. {depthtensor-1.1.2 → depthtensor-1.2.0}/src/DepthTensor.egg-info/top_level.txt +0 -0
  30. {depthtensor-1.1.2 → depthtensor-1.2.0}/tests/test_tensor.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: DepthTensor
3
- Version: 1.1.2
3
+ Version: 1.2.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.1.2"
7
+ version = "1.2.0"
8
8
  readme = "README.md"
9
9
  dependencies = [
10
10
  "numpy"
@@ -3,4 +3,4 @@ from .typing import *
3
3
  from ._core import random
4
4
  from .autodiff import differentiate
5
5
 
6
- __version__ = "1.1.2"
6
+ __version__ = "1.2.0"
@@ -1,5 +1,6 @@
1
1
  CUPY_NOT_FOUND_MSG = "Module CuPy not found or installed. Please install CuPy."
2
2
  DEVICE_MISMATCH_MSG = "There is a mismatch in device between two objects."
3
+ OPERAND_MISMATCH_MSG = "There is a mismatch in the type between two operands."
3
4
  GRADIENT_COMPUTATION_ERROR = "An error in gradient computation has occurred."
4
5
 
5
6
  class CuPyNotFound(RuntimeError):
@@ -8,5 +9,8 @@ class CuPyNotFound(RuntimeError):
8
9
  class DeviceMismatch(RuntimeError):
9
10
  ...
10
11
 
12
+ class OperandMismatch(RuntimeError):
13
+ ...
14
+
11
15
  class GradientComputationError(RuntimeError):
12
16
  ...
@@ -2,8 +2,7 @@ from typing import (
2
2
  Union,
3
3
  Optional,
4
4
  Tuple,
5
- overload,
6
- Callable
5
+ overload
7
6
  )
8
7
 
9
8
  from ...typing import (
@@ -12,14 +11,18 @@ from ...typing import (
12
11
  DeviceLike,
13
12
  ArrayLikeBool,
14
13
  Casting,
15
- Order
14
+ Order,
15
+ OperandLike
16
16
  )
17
17
 
18
18
  from ..exceptions import (
19
19
  CuPyNotFound, CUPY_NOT_FOUND_MSG,
20
- DeviceMismatch, DEVICE_MISMATCH_MSG
20
+ DeviceMismatch, DEVICE_MISMATCH_MSG,
21
+ OperandMismatch, OPERAND_MISMATCH_MSG
21
22
  )
22
23
 
24
+ from ..utils import to_xp_array
25
+
23
26
  import numpy as np
24
27
  try:
25
28
  import cupy as cp
@@ -32,39 +35,34 @@ except (ImportError, ModuleNotFoundError):
32
35
 
33
36
  @overload
34
37
  def where(
35
- condition: Union[ArrayLike, TensorLike],
36
- /
38
+ condition: OperandLike,
39
+ /,
40
+ *,
41
+ device: DeviceLike = "cpu"
37
42
  ) -> Tuple[TensorLike, ...]: ...
38
43
 
39
44
  @overload
40
45
  def where(
41
- condition: Union[ArrayLike, TensorLike],
42
- x: Optional[Union[ArrayLike, TensorLike]],
43
- y: Optional[Union[ArrayLike, TensorLike]],
46
+ condition: OperandLike,
47
+ x: Optional[OperandLike],
48
+ y: Optional[OperandLike],
44
49
  /,
45
50
  *,
46
51
  device: DeviceLike = "cpu"
47
52
  ) -> TensorLike: ...
48
53
 
49
54
  def where(
50
- condition: Union[ArrayLike, TensorLike],
51
- x: Optional[Union[ArrayLike, TensorLike]] = None,
52
- y: Optional[Union[ArrayLike, TensorLike]] = None,
55
+ condition: OperandLike,
56
+ x: Optional[OperandLike] = None,
57
+ y: Optional[OperandLike] = None,
53
58
  /,
54
59
  *,
55
60
  device: DeviceLike = "cpu"
56
61
  ) -> Union[Tuple[TensorLike, ...], TensorLike]:
57
62
  from ...tensor import Tensor
58
63
  #* One parameter overload
59
- if x is None and y is None:
60
- if isinstance(condition, TensorLike):
61
- data = condition.data
62
- else:
63
- if device == "cpu":
64
- data = np.asarray(condition)
65
- else:
66
- if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
67
- data = cp.asarray(condition)
64
+ if (x is None) and (y is None):
65
+ data = to_xp_array(condition, device=device)
68
66
  if device == "cpu":
69
67
  result = np.where(data)
70
68
  else:
@@ -72,30 +70,9 @@ def where(
72
70
  result = cp.where(data)
73
71
  return tuple([Tensor(array, device=device) for array in result])
74
72
  elif x is not None and y is not None:
75
- if isinstance(condition, TensorLike):
76
- data = condition.data
77
- else:
78
- if device == "cpu":
79
- data = np.asarray(condition)
80
- else:
81
- if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
82
- data = cp.asarray(condition)
83
- if isinstance(x, TensorLike):
84
- x_data = x.data
85
- else:
86
- if device == "cpu":
87
- x_data = np.asarray(x)
88
- else:
89
- if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
90
- x_data = cp.asarray(x)
91
- if isinstance(y, TensorLike):
92
- y_data = y.data
93
- else:
94
- if device == "cpu":
95
- y_data = np.asarray(y)
96
- else:
97
- if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
98
- y_data = cp.asarray(y)
73
+ data = to_xp_array(condition, device=device)
74
+ x_data = to_xp_array(x, device=device)
75
+ y_data = to_xp_array(y, device=device)
99
76
  if device == "cpu":
100
77
  result = np.where(data, x_data, y_data)
101
78
  else:
@@ -109,13 +86,14 @@ def where(
109
86
  ###
110
87
  ###
111
88
 
112
- def comparison_ufunc(
113
- x1: TensorLike,
114
- x2: TensorLike,
89
+ def wrapper_2in_1out(
90
+ x1: OperandLike,
91
+ x2: OperandLike,
115
92
  /,
116
93
  out: Optional[ArrayLikeBool] = None,
117
94
  *,
118
95
  func_name: str,
96
+ device: DeviceLike = "cpu",
119
97
  where: Union[bool, ArrayLikeBool] = True,
120
98
  casting: Casting = 'same_kind',
121
99
  order: Order = 'K',
@@ -123,97 +101,120 @@ def comparison_ufunc(
123
101
  subok: bool = True
124
102
  ) -> TensorLike:
125
103
  from ...tensor import Tensor
126
- if not x1.is_device(x2.device): raise DeviceMismatch(DEVICE_MISMATCH_MSG)
127
- if x1.is_cpu():
128
- y = getattr(np, func_name)(x1.data, x2.data, out=out, dtype=dtype, where=where, casting=casting, order=order, subok=subok)
104
+ if isinstance(x1, Tensor) and isinstance(x2, Tensor):
105
+ if not x1.is_device(x2.device):
106
+ raise DeviceMismatch(DEVICE_MISMATCH_MSG)
107
+ else:
108
+ is_tensor_op = True
109
+ else:
110
+ if isinstance(x1, Tensor) or isinstance(x2, Tensor):
111
+ raise OperandMismatch(OPERAND_MISMATCH_MSG)
112
+ is_tensor_op = False
113
+ #* Either tensor-tensor op or array-array op.
114
+
115
+ #* If its tensor-tensor op, device is decided by x1.device, else, its by the device argument.
116
+ if is_tensor_op and isinstance(x1, Tensor):
117
+ device_op = x1.device
118
+ else:
119
+ device_op = device
120
+
121
+ x1, x2 = to_xp_array(x1, device=device_op), to_xp_array(x2, device=device_op)
122
+ if device_op == "cpu":
123
+ y = getattr(np, func_name)(x1, x2, out=out, dtype=dtype, where=where, casting=casting, order=order, subok=subok)
129
124
  else:
130
125
  if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
131
- y = getattr(cp, func_name)(x1.data, x2.data, out=out, dtype=dtype, casting=casting)
132
- return Tensor(y, device=x1.device)
126
+ y = getattr(cp, func_name)(x1, x2, out=out, dtype=dtype, casting=casting)
127
+ return Tensor(y, device=device_op)
133
128
 
134
129
  def equal(
135
- x1: TensorLike,
136
- x2: TensorLike,
130
+ x1: OperandLike,
131
+ x2: OperandLike,
137
132
  /,
138
133
  out: Optional[ArrayLikeBool] = None,
139
134
  *,
135
+ device: DeviceLike = "cpu",
140
136
  where: Union[bool, ArrayLikeBool] = True,
141
137
  casting: Casting = 'same_kind',
142
138
  order: Order = 'K',
143
139
  dtype: None = None,
144
140
  subok: bool = True
145
141
  ) -> TensorLike:
146
- return comparison_ufunc(x1, x2, out=out, func_name="equal", where=where, casting=casting, order=order, dtype=dtype, subok=subok)
142
+ return wrapper_2in_1out(x1, x2, out=out, func_name="equal", device=device, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
147
143
 
148
144
  def not_equal(
149
- x1: TensorLike,
150
- x2: TensorLike,
145
+ x1: OperandLike,
146
+ x2: OperandLike,
151
147
  /,
152
148
  out: Optional[ArrayLikeBool] = None,
153
149
  *,
150
+ device: DeviceLike = "cpu",
154
151
  where: Union[bool, ArrayLikeBool] = True,
155
152
  casting: Casting = 'same_kind',
156
153
  order: Order = 'K',
157
154
  dtype: None = None,
158
155
  subok: bool = True
159
156
  ) -> TensorLike:
160
- return comparison_ufunc(x1, x2, out=out, func_name="not_equal", where=where, casting=casting, order=order, dtype=dtype, subok=subok)
157
+ return wrapper_2in_1out(x1, x2, out=out, func_name="not_equal", device=device, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
161
158
 
162
159
  def greater(
163
- x1: TensorLike,
164
- x2: TensorLike,
160
+ x1: OperandLike,
161
+ x2: OperandLike,
165
162
  /,
166
163
  out: Optional[ArrayLikeBool] = None,
167
164
  *,
165
+ device: DeviceLike = "cpu",
168
166
  where: Union[bool, ArrayLikeBool] = True,
169
167
  casting: Casting = 'same_kind',
170
168
  order: Order = 'K',
171
169
  dtype: None = None,
172
170
  subok: bool = True
173
171
  ) -> TensorLike:
174
- return comparison_ufunc(x1, x2, out=out, func_name="greater", where=where, casting=casting, order=order, dtype=dtype, subok=subok)
172
+ return wrapper_2in_1out(x1, x2, out=out, func_name="greater", device=device, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
175
173
 
176
174
  def greater_equal(
177
- x1: TensorLike,
178
- x2: TensorLike,
175
+ x1: OperandLike,
176
+ x2: OperandLike,
179
177
  /,
180
178
  out: Optional[ArrayLikeBool] = None,
181
179
  *,
180
+ device: DeviceLike = "cpu",
182
181
  where: Union[bool, ArrayLikeBool] = True,
183
182
  casting: Casting = 'same_kind',
184
183
  order: Order = 'K',
185
184
  dtype: None = None,
186
185
  subok: bool = True
187
186
  ) -> TensorLike:
188
- return comparison_ufunc(x1, x2, out=out, func_name="greater_equal", where=where, casting=casting, order=order, dtype=dtype, subok=subok)
187
+ return wrapper_2in_1out(x1, x2, out=out, func_name="greater_equal", device=device, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
189
188
 
190
189
  def less(
191
- x1: TensorLike,
192
- x2: TensorLike,
190
+ x1: OperandLike,
191
+ x2: OperandLike,
193
192
  /,
194
193
  out: Optional[ArrayLikeBool] = None,
195
194
  *,
195
+ device: DeviceLike = "cpu",
196
196
  where: Union[bool, ArrayLikeBool] = True,
197
197
  casting: Casting = 'same_kind',
198
198
  order: Order = 'K',
199
199
  dtype: None = None,
200
200
  subok: bool = True
201
201
  ) -> TensorLike:
202
- return comparison_ufunc(x1, x2, out=out, func_name="less", where=where, casting=casting, order=order, dtype=dtype, subok=subok)
202
+ return wrapper_2in_1out(x1, x2, out=out, func_name="less", device=device, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
203
203
 
204
204
  def less_equal(
205
- x1: TensorLike,
206
- x2: TensorLike,
205
+ x1: OperandLike,
206
+ x2: OperandLike,
207
207
  /,
208
208
  out: Optional[ArrayLikeBool] = None,
209
209
  *,
210
+ device: DeviceLike = "cpu",
210
211
  where: Union[bool, ArrayLikeBool] = True,
211
212
  casting: Casting = 'same_kind',
212
213
  order: Order = 'K',
213
214
  dtype: None = None,
214
215
  subok: bool = True
215
216
  ) -> TensorLike:
216
- return comparison_ufunc(x1, x2, out=out, func_name="less_equal", where=where, casting=casting, order=order, dtype=dtype, subok=subok)
217
+ return wrapper_2in_1out(x1, x2, out=out, func_name="less_equal", device=device, where=where, casting=casting, order=order, dtype=dtype, subok=subok)
217
218
 
218
219
  ###
219
220
  ###
@@ -3,14 +3,20 @@ from typing import (
3
3
  )
4
4
 
5
5
  from ...typing import (
6
- TensorLike, DTypeLike, Order,
7
- AxisShapeLike
6
+ TensorLike,
7
+ DTypeLike,
8
+ Order,
9
+ AxisShapeLike,
10
+ OperandLike,
11
+ DeviceLike
8
12
  )
9
13
 
10
14
  from ..exceptions import (
11
15
  CuPyNotFound, CUPY_NOT_FOUND_MSG
12
16
  )
13
17
 
18
+ from ..utils import to_xp_array
19
+
14
20
  import numpy as np
15
21
  try:
16
22
  import cupy as cp
@@ -22,49 +28,61 @@ except (ImportError, ModuleNotFoundError):
22
28
  ###
23
29
 
24
30
  def zeros_like(
25
- a: TensorLike,
31
+ a: OperandLike,
26
32
  /,
27
- *,
33
+ *,
34
+ device: DeviceLike = "cpu",
28
35
  dtype: Optional[DTypeLike] = None,
29
36
  order: Order = 'K',
30
37
  subok: bool = True,
31
38
  shape: Optional[AxisShapeLike] = None
32
39
  ) -> TensorLike:
33
40
  from ...tensor import Tensor
34
- if a.device == "cpu":
41
+ if isinstance(a, Tensor):
42
+ device_op = a.device
43
+ else:
44
+ device_op = device
45
+ a = to_xp_array(a)
46
+ if device_op == "cpu":
35
47
  return Tensor(
36
- a.zeros_like(a, dtype=dtype, order=order, subok=subok, shape=shape),
37
- device=a.device
48
+ np.zeros_like(a, dtype=dtype, order=order, subok=subok, shape=shape),
49
+ device=device_op
38
50
  )
39
51
  else:
40
52
  if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
41
53
  return Tensor(
42
54
  #! As of the time writing this, CuPy does not support subok.
43
55
  cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape),
44
- device=a.device
56
+ device=device_op
45
57
  )
46
58
 
47
59
  def ones_like(
48
- a: TensorLike,
60
+ a: OperandLike,
49
61
  /,
50
- *,
62
+ *,
63
+ device: DeviceLike = "cpu",
51
64
  dtype: Optional[DTypeLike] = None,
52
65
  order: Order = 'K',
53
66
  subok: bool = True,
54
67
  shape: Optional[AxisShapeLike] = None
55
68
  ) -> TensorLike:
56
69
  from ...tensor import Tensor
57
- if a.is_cpu():
70
+ if isinstance(a, Tensor):
71
+ device_op = a.device
72
+ else:
73
+ device_op = device
74
+ a = to_xp_array(a)
75
+ if device_op == "cpu":
58
76
  return Tensor(
59
77
  np.ones_like(a, dtype=dtype, order=order, subok=subok, shape=shape),
60
- device=a.device
78
+ device=device_op
61
79
  )
62
80
  else:
63
81
  if cp is None: raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
64
82
  return Tensor(
65
83
  #! 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
84
+ cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape),
85
+ device=device_op
68
86
  )
69
87
 
70
88
  ###