DepthTensor 2.5.1__tar.gz → 2.7.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 (33) hide show
  1. {depthtensor-2.5.1 → depthtensor-2.7.0}/PKG-INFO +1 -1
  2. {depthtensor-2.5.1 → depthtensor-2.7.0}/pyproject.toml +1 -1
  3. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/DepthTensor/__init__.py +1 -1
  4. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/DepthTensor/_core/ops/comparison.py +193 -5
  5. depthtensor-2.7.0/src/DepthTensor/_core/ops/creation.py +218 -0
  6. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/DepthTensor/_core/ops/elementwise.py +504 -25
  7. depthtensor-2.7.0/src/DepthTensor/_core/ops/reduction.py +240 -0
  8. depthtensor-2.7.0/src/DepthTensor/_core/random/generator.py +236 -0
  9. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/DepthTensor/_core/utils.py +79 -8
  10. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/DepthTensor/autodiff.py +29 -0
  11. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/DepthTensor/tensor.py +390 -18
  12. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/depthtensor.egg-info/PKG-INFO +1 -1
  13. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/depthtensor.egg-info/SOURCES.txt +6 -1
  14. depthtensor-2.7.0/tests/test_autodiff.py +148 -0
  15. depthtensor-2.7.0/tests/test_autodiff_extended.py +126 -0
  16. depthtensor-2.7.0/tests/test_ops_forward.py +82 -0
  17. depthtensor-2.7.0/tests/test_tensor_errors.py +47 -0
  18. depthtensor-2.7.0/tests/test_tensor_init.py +59 -0
  19. depthtensor-2.5.1/src/DepthTensor/_core/ops/creation.py +0 -124
  20. depthtensor-2.5.1/src/DepthTensor/_core/ops/reduction.py +0 -149
  21. depthtensor-2.5.1/src/DepthTensor/_core/random/generator.py +0 -133
  22. {depthtensor-2.5.1 → depthtensor-2.7.0}/LICENSE +0 -0
  23. {depthtensor-2.5.1 → depthtensor-2.7.0}/README.md +0 -0
  24. {depthtensor-2.5.1 → depthtensor-2.7.0}/setup.cfg +0 -0
  25. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/DepthTensor/_core/__init__.py +0 -0
  26. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/DepthTensor/_core/exceptions.py +0 -0
  27. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/DepthTensor/_core/ops/__init__.py +0 -0
  28. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/DepthTensor/_core/ops/function.py +0 -0
  29. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/DepthTensor/_core/random/__init__.py +0 -0
  30. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/DepthTensor/typing.py +0 -0
  31. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/depthtensor.egg-info/dependency_links.txt +0 -0
  32. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/depthtensor.egg-info/requires.txt +0 -0
  33. {depthtensor-2.5.1 → depthtensor-2.7.0}/src/depthtensor.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: depthtensor
3
- Version: 2.5.1
3
+ Version: 2.7.0
4
4
  Summary: A Hardware-Accelerated Tensor Computation and Autograd Engine
5
5
  Author-email: Le Hong Ha <lehahong1310@gmail.com>
6
6
  License: MIT License
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "depthtensor"
7
- version = "2.5.1"
7
+ version = "2.7.0"
8
8
  description = "A Hardware-Accelerated Tensor Computation and Autograd Engine"
9
9
  readme = "README.md"
10
10
  authors = [
@@ -6,4 +6,4 @@ from ._core.ops import *
6
6
  from ._core.exceptions import *
7
7
  from ._core import random
8
8
 
9
- __version__ = "2.5.1"
9
+ __version__ = "2.7.0"
@@ -61,6 +61,29 @@ def where(
61
61
  device: Device | None = None,
62
62
  requires_grad: bool = False,
63
63
  ) -> Union[tuple[TensorType, ...], TensorType]:
64
+ """
65
+ Return elements chosen from `x` or `y` depending on `condition`.
66
+
67
+ Parameters
68
+ ----------
69
+ condition : TensorLike
70
+ Where True, yield `x`, otherwise yield `y`.
71
+ x : TensorLike | None, optional
72
+ Values from which to choose.
73
+ y : TensorLike | None, optional
74
+ Values from which to choose.
75
+ device : Device | None, optional
76
+ The device to place the result on.
77
+ requires_grad : bool, optional
78
+ Whether the result requires gradient computation.
79
+
80
+ Returns
81
+ -------
82
+ TensorType | tuple[TensorType, ...]
83
+ A tensor with elements from `x` where `condition` is True, and elements
84
+ from `y` elsewhere. If `x` and `y` are None, returns a tuple of
85
+ indices where `condition` is True.
86
+ """
64
87
  from ...tensor import Tensor
65
88
 
66
89
  if device is None:
@@ -75,7 +98,7 @@ def where(
75
98
  if cp is None:
76
99
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
77
100
  result = cp.where(data)
78
- return tuple([Tensor(array, requires_grad=requires_grad) for array in result])
101
+ return tuple([Tensor._fast_init(array, device=device, requires_grad=requires_grad) for array in result])
79
102
  # * Two parameters overload
80
103
  elif x is not None and y is not None:
81
104
  if (
@@ -94,7 +117,7 @@ def where(
94
117
  if cp is None:
95
118
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
96
119
  result = cp.where(data, x_data, y_data)
97
- return Tensor(result, requires_grad=requires_grad)
120
+ return Tensor._fast_init(result, device=device, requires_grad=requires_grad)
98
121
  else:
99
122
  raise ValueError("Both x and y parameters must be given.")
100
123
 
@@ -120,9 +143,12 @@ def wrapper_2in_1out(
120
143
  ) -> TensorType:
121
144
  from ...tensor import Tensor
122
145
 
123
- op_device = get_two_operand_op_device(x1, x2, device)
146
+ x1_is_tensor, x2_is_tensor = isinstance(x1, Tensor), isinstance(x2, Tensor)
147
+ op_device = get_two_operand_op_device(x1, x2, x1_is_tensor, x2_is_tensor, device)
148
+
149
+ x1 = to_tensordata(x1, device=op_device) if not x1_is_tensor else x1.data
150
+ x2 = to_tensordata(x2, device=op_device) if not x2_is_tensor else x2.data
124
151
 
125
- x1, x2 = to_tensordata(x1, device=op_device), to_tensordata(x2, device=op_device)
126
152
  if op_device == "cpu":
127
153
  y = getattr(np, func_name)(
128
154
  x1,
@@ -138,7 +164,7 @@ def wrapper_2in_1out(
138
164
  if cp is None:
139
165
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
140
166
  y = getattr(cp, func_name)(x1, x2, out=out, dtype=dtype, casting=casting)
141
- return Tensor(y)
167
+ return Tensor._fast_init(y, device=op_device)
142
168
 
143
169
 
144
170
  def equal(
@@ -154,6 +180,33 @@ def equal(
154
180
  dtype: None = None,
155
181
  subok: bool = True,
156
182
  ) -> TensorType:
183
+ """
184
+ Return (x1 == x2) element-wise.
185
+
186
+ Parameters
187
+ ----------
188
+ x1, x2 : TensorLike
189
+ Input tensors.
190
+ out : TensorDataBool | None, optional
191
+ A location into which the result is stored.
192
+ device : Device | None, optional
193
+ The device to place the result on.
194
+ where : TensorDataBool | bool, optional
195
+ This condition is broadcast over the input.
196
+ casting : Casting, optional
197
+ Controls what kind of data casting may occur.
198
+ order : Order, optional
199
+ Controls the memory layout of the result.
200
+ dtype : None, optional
201
+ Overrides the data type of the result.
202
+ subok : bool, optional
203
+ If True, then sub-classes will be passed-through.
204
+
205
+ Returns
206
+ -------
207
+ TensorType
208
+ Output array, element-wise comparison of x1 and x2.
209
+ """
157
210
  return wrapper_2in_1out(
158
211
  x1,
159
212
  x2,
@@ -181,6 +234,33 @@ def not_equal(
181
234
  dtype: None = None,
182
235
  subok: bool = True,
183
236
  ) -> TensorType:
237
+ """
238
+ Return (x1 != x2) element-wise.
239
+
240
+ Parameters
241
+ ----------
242
+ x1, x2 : TensorLike
243
+ Input tensors.
244
+ out : TensorDataBool | None, optional
245
+ A location into which the result is stored.
246
+ device : Device | None, optional
247
+ The device to place the result on.
248
+ where : TensorDataBool | bool, optional
249
+ This condition is broadcast over the input.
250
+ casting : Casting, optional
251
+ Controls what kind of data casting may occur.
252
+ order : Order, optional
253
+ Controls the memory layout of the result.
254
+ dtype : None, optional
255
+ Overrides the data type of the result.
256
+ subok : bool, optional
257
+ If True, then sub-classes will be passed-through.
258
+
259
+ Returns
260
+ -------
261
+ TensorType
262
+ Output array, element-wise comparison of x1 and x2.
263
+ """
184
264
  return wrapper_2in_1out(
185
265
  x1,
186
266
  x2,
@@ -208,6 +288,33 @@ def greater(
208
288
  dtype: None = None,
209
289
  subok: bool = True,
210
290
  ) -> TensorType:
291
+ """
292
+ Return the truth value of (x1 > x2) element-wise.
293
+
294
+ Parameters
295
+ ----------
296
+ x1, x2 : TensorLike
297
+ Input tensors.
298
+ out : TensorDataBool | None, optional
299
+ A location into which the result is stored.
300
+ device : Device | None, optional
301
+ The device to place the result on.
302
+ where : TensorDataBool | bool, optional
303
+ This condition is broadcast over the input.
304
+ casting : Casting, optional
305
+ Controls what kind of data casting may occur.
306
+ order : Order, optional
307
+ Controls the memory layout of the result.
308
+ dtype : None, optional
309
+ Overrides the data type of the result.
310
+ subok : bool, optional
311
+ If True, then sub-classes will be passed-through.
312
+
313
+ Returns
314
+ -------
315
+ TensorType
316
+ Output array, element-wise comparison of x1 and x2.
317
+ """
211
318
  return wrapper_2in_1out(
212
319
  x1,
213
320
  x2,
@@ -235,6 +342,33 @@ def greater_equal(
235
342
  dtype: None = None,
236
343
  subok: bool = True,
237
344
  ) -> TensorType:
345
+ """
346
+ Return the truth value of (x1 >= x2) element-wise.
347
+
348
+ Parameters
349
+ ----------
350
+ x1, x2 : TensorLike
351
+ Input tensors.
352
+ out : TensorDataBool | None, optional
353
+ A location into which the result is stored.
354
+ device : Device | None, optional
355
+ The device to place the result on.
356
+ where : TensorDataBool | bool, optional
357
+ This condition is broadcast over the input.
358
+ casting : Casting, optional
359
+ Controls what kind of data casting may occur.
360
+ order : Order, optional
361
+ Controls the memory layout of the result.
362
+ dtype : None, optional
363
+ Overrides the data type of the result.
364
+ subok : bool, optional
365
+ If True, then sub-classes will be passed-through.
366
+
367
+ Returns
368
+ -------
369
+ TensorType
370
+ Output array, element-wise comparison of x1 and x2.
371
+ """
238
372
  return wrapper_2in_1out(
239
373
  x1,
240
374
  x2,
@@ -262,6 +396,33 @@ def less(
262
396
  dtype: None = None,
263
397
  subok: bool = True,
264
398
  ) -> TensorType:
399
+ """
400
+ Return the truth value of (x1 < x2) element-wise.
401
+
402
+ Parameters
403
+ ----------
404
+ x1, x2 : TensorLike
405
+ Input tensors.
406
+ out : TensorDataBool | None, optional
407
+ A location into which the result is stored.
408
+ device : Device | None, optional
409
+ The device to place the result on.
410
+ where : TensorDataBool | bool, optional
411
+ This condition is broadcast over the input.
412
+ casting : Casting, optional
413
+ Controls what kind of data casting may occur.
414
+ order : Order, optional
415
+ Controls the memory layout of the result.
416
+ dtype : None, optional
417
+ Overrides the data type of the result.
418
+ subok : bool, optional
419
+ If True, then sub-classes will be passed-through.
420
+
421
+ Returns
422
+ -------
423
+ TensorType
424
+ Output array, element-wise comparison of x1 and x2.
425
+ """
265
426
  return wrapper_2in_1out(
266
427
  x1,
267
428
  x2,
@@ -289,6 +450,33 @@ def less_equal(
289
450
  dtype: None = None,
290
451
  subok: bool = True,
291
452
  ) -> TensorType:
453
+ """
454
+ Return the truth value of (x1 <= x2) element-wise.
455
+
456
+ Parameters
457
+ ----------
458
+ x1, x2 : TensorLike
459
+ Input tensors.
460
+ out : TensorDataBool | None, optional
461
+ A location into which the result is stored.
462
+ device : Device | None, optional
463
+ The device to place the result on.
464
+ where : TensorDataBool | bool, optional
465
+ This condition is broadcast over the input.
466
+ casting : Casting, optional
467
+ Controls what kind of data casting may occur.
468
+ order : Order, optional
469
+ Controls the memory layout of the result.
470
+ dtype : None, optional
471
+ Overrides the data type of the result.
472
+ subok : bool, optional
473
+ If True, then sub-classes will be passed-through.
474
+
475
+ Returns
476
+ -------
477
+ TensorType
478
+ Output array, element-wise comparison of x1 and x2.
479
+ """
292
480
  return wrapper_2in_1out(
293
481
  x1,
294
482
  x2,
@@ -0,0 +1,218 @@
1
+ from typing import Literal
2
+
3
+ from ...typing import (
4
+ TensorType,
5
+ TensorLike,
6
+ DTypeLike,
7
+ Order,
8
+ Axis,
9
+ Device,
10
+ Shape,
11
+ )
12
+
13
+ from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
14
+ from ..utils import to_tensordata, get_device
15
+
16
+ import numpy as np
17
+
18
+ try:
19
+ import cupy as cp
20
+ except (ImportError, ModuleNotFoundError):
21
+ cp = None
22
+
23
+ ###
24
+ ###
25
+ ###
26
+
27
+
28
+ def zeros_like(
29
+ a: TensorLike,
30
+ /,
31
+ *,
32
+ device: Device | None = None,
33
+ requires_grad: bool = False,
34
+ dtype: DTypeLike | None = None,
35
+ order: Order = "K",
36
+ subok: bool = True,
37
+ shape: Axis | None = None,
38
+ ) -> TensorType:
39
+ """
40
+ Return an array of zeros with the same shape and type as a given array.
41
+
42
+ Parameters
43
+ ----------
44
+ a : TensorLike
45
+ The shape and data-type of `a` define these same attributes of the returned array.
46
+ device : Device | None, optional
47
+ The device to place the result on.
48
+ requires_grad : bool, optional
49
+ Whether the result requires gradient computation.
50
+ dtype : DTypeLike | None, optional
51
+ Overrides the data type of the result.
52
+ order : Order, optional
53
+ Overrides the memory layout of the result.
54
+ subok : bool, optional
55
+ If True, then sub-classes will be passed-through.
56
+ shape : Axis | None, optional
57
+ Overrides the shape of the result.
58
+
59
+ Returns
60
+ -------
61
+ TensorType
62
+ Array of zeros with the same shape and type as `a`.
63
+ """
64
+ from ...tensor import Tensor
65
+
66
+ if device is None:
67
+ device_op = get_device(a)
68
+ else:
69
+ device_op = device
70
+ a = to_tensordata(a)
71
+ if device_op == "cpu":
72
+ y = np.zeros_like(a, dtype=dtype, order=order, subok=subok, shape=shape)
73
+ else:
74
+ if cp is None:
75
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
76
+ y = cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape)
77
+ return Tensor._fast_init(y, device=device_op, requires_grad=requires_grad)
78
+
79
+
80
+ def ones_like(
81
+ a: TensorLike,
82
+ /,
83
+ *,
84
+ device: Device | None = None,
85
+ requires_grad: bool = False,
86
+ dtype: DTypeLike | None = None,
87
+ order: Order = "K",
88
+ subok: bool = True,
89
+ shape: Axis | None = None,
90
+ ) -> TensorType:
91
+ """
92
+ Return an array of ones with the same shape and type as a given array.
93
+
94
+ Parameters
95
+ ----------
96
+ a : TensorLike
97
+ The shape and data-type of `a` define these same attributes of the returned array.
98
+ device : Device | None, optional
99
+ The device to place the result on.
100
+ requires_grad : bool, optional
101
+ Whether the result requires gradient computation.
102
+ dtype : DTypeLike | None, optional
103
+ Overrides the data type of the result.
104
+ order : Order, optional
105
+ Overrides the memory layout of the result.
106
+ subok : bool, optional
107
+ If True, then sub-classes will be passed-through.
108
+ shape : Axis | None, optional
109
+ Overrides the shape of the result.
110
+
111
+ Returns
112
+ -------
113
+ TensorType
114
+ Array of ones with the same shape and type as `a`.
115
+ """
116
+ from ...tensor import Tensor
117
+
118
+ if device is None:
119
+ device_op = get_device(a)
120
+ else:
121
+ device_op = device
122
+ a = to_tensordata(a)
123
+ if device_op == "cpu":
124
+ y = np.ones_like(a, dtype=dtype, order=order, subok=subok, shape=shape)
125
+ else:
126
+ if cp is None:
127
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
128
+ y = cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape)
129
+ return Tensor._fast_init(y, device=device_op, requires_grad=requires_grad)
130
+
131
+
132
+ def zeros(
133
+ shape: Shape,
134
+ dtype: DTypeLike = float,
135
+ order: Literal["C", "F"] = "C",
136
+ *,
137
+ device: Device = "cpu",
138
+ requires_grad: bool = False,
139
+ ) -> TensorType:
140
+ """
141
+ Return a new array of given shape and type, filled with zeros.
142
+
143
+ Parameters
144
+ ----------
145
+ shape : Shape
146
+ Shape of the new array.
147
+ dtype : DTypeLike, optional
148
+ The desired data-type for the array. Default is float.
149
+ order : {'C', 'F'}, optional
150
+ Whether to store multi-dimensional data in row-major (C-style)
151
+ or column-major (Fortran-style) order in memory.
152
+ device : Device, optional
153
+ The device to place the result on. Default is 'cpu'.
154
+ requires_grad : bool, optional
155
+ Whether the result requires gradient computation.
156
+
157
+ Returns
158
+ -------
159
+ TensorType
160
+ Array of zeros with the given shape, dtype, and order.
161
+ """
162
+ from ...tensor import Tensor
163
+
164
+ if device == "cpu":
165
+ y = np.zeros(shape=shape, dtype=dtype, order=order)
166
+ else:
167
+ if cp is None:
168
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
169
+ y = cp.zeros(shape=shape, dtype=dtype, order=order)
170
+ return Tensor._fast_init(y, device=device, requires_grad=requires_grad)
171
+
172
+
173
+ def ones(
174
+ shape: Shape,
175
+ dtype: DTypeLike = float,
176
+ order: Literal["C", "F"] = "C",
177
+ *,
178
+ device: Device = "cpu",
179
+ requires_grad: bool = False,
180
+ ) -> TensorType:
181
+ """
182
+ Return a new array of given shape and type, filled with ones.
183
+
184
+ Parameters
185
+ ----------
186
+ shape : Shape
187
+ Shape of the new array.
188
+ dtype : DTypeLike, optional
189
+ The desired data-type for the array. Default is float.
190
+ order : {'C', 'F'}, optional
191
+ Whether to store multi-dimensional data in row-major (C-style)
192
+ or column-major (Fortran-style) order in memory.
193
+ device : Device, optional
194
+ The device to place the result on. Default is 'cpu'.
195
+ requires_grad : bool, optional
196
+ Whether the result requires gradient computation.
197
+
198
+ Returns
199
+ -------
200
+ TensorType
201
+ Array of ones with the given shape, dtype, and order.
202
+ """
203
+ from ...tensor import Tensor
204
+
205
+ if device == "cpu":
206
+ y = np.ones(shape=shape, dtype=dtype, order=order)
207
+ else:
208
+ if cp is None:
209
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
210
+ y = cp.ones(shape=shape, dtype=dtype, order=order)
211
+ return Tensor._fast_init(y, device=device, requires_grad=requires_grad)
212
+
213
+
214
+ ###
215
+ ###
216
+ ###
217
+
218
+ __all__ = ["zeros_like", "ones_like", "zeros", "ones"]