DepthTensor 2.6.0__tar.gz → 2.8.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.6.0 → depthtensor-2.8.0}/PKG-INFO +1 -1
  2. {depthtensor-2.6.0 → depthtensor-2.8.0}/pyproject.toml +1 -1
  3. {depthtensor-2.6.0 → depthtensor-2.8.0}/src/DepthTensor/__init__.py +1 -1
  4. {depthtensor-2.6.0 → depthtensor-2.8.0}/src/DepthTensor/_core/ops/comparison.py +199 -4
  5. depthtensor-2.8.0/src/DepthTensor/_core/ops/creation.py +218 -0
  6. {depthtensor-2.6.0 → depthtensor-2.8.0}/src/DepthTensor/_core/ops/elementwise.py +502 -11
  7. depthtensor-2.8.0/src/DepthTensor/_core/ops/reduction.py +297 -0
  8. depthtensor-2.8.0/src/DepthTensor/_core/random/generator.py +236 -0
  9. {depthtensor-2.6.0 → depthtensor-2.8.0}/src/DepthTensor/_core/utils.py +71 -2
  10. {depthtensor-2.6.0 → depthtensor-2.8.0}/src/DepthTensor/autodiff.py +29 -0
  11. depthtensor-2.8.0/src/DepthTensor/tensor.py +1301 -0
  12. {depthtensor-2.6.0 → depthtensor-2.8.0}/src/depthtensor.egg-info/PKG-INFO +1 -1
  13. {depthtensor-2.6.0 → depthtensor-2.8.0}/src/depthtensor.egg-info/SOURCES.txt +5 -1
  14. depthtensor-2.8.0/tests/test_autodiff.py +265 -0
  15. depthtensor-2.8.0/tests/test_ops_forward.py +82 -0
  16. depthtensor-2.8.0/tests/test_tensor_errors.py +47 -0
  17. depthtensor-2.8.0/tests/test_tensor_init.py +59 -0
  18. depthtensor-2.6.0/src/DepthTensor/_core/ops/creation.py +0 -124
  19. depthtensor-2.6.0/src/DepthTensor/_core/ops/reduction.py +0 -149
  20. depthtensor-2.6.0/src/DepthTensor/_core/random/generator.py +0 -134
  21. depthtensor-2.6.0/src/DepthTensor/tensor.py +0 -559
  22. {depthtensor-2.6.0 → depthtensor-2.8.0}/LICENSE +0 -0
  23. {depthtensor-2.6.0 → depthtensor-2.8.0}/README.md +0 -0
  24. {depthtensor-2.6.0 → depthtensor-2.8.0}/setup.cfg +0 -0
  25. {depthtensor-2.6.0 → depthtensor-2.8.0}/src/DepthTensor/_core/__init__.py +0 -0
  26. {depthtensor-2.6.0 → depthtensor-2.8.0}/src/DepthTensor/_core/exceptions.py +0 -0
  27. {depthtensor-2.6.0 → depthtensor-2.8.0}/src/DepthTensor/_core/ops/__init__.py +0 -0
  28. {depthtensor-2.6.0 → depthtensor-2.8.0}/src/DepthTensor/_core/ops/function.py +0 -0
  29. {depthtensor-2.6.0 → depthtensor-2.8.0}/src/DepthTensor/_core/random/__init__.py +0 -0
  30. {depthtensor-2.6.0 → depthtensor-2.8.0}/src/DepthTensor/typing.py +0 -0
  31. {depthtensor-2.6.0 → depthtensor-2.8.0}/src/depthtensor.egg-info/dependency_links.txt +0 -0
  32. {depthtensor-2.6.0 → depthtensor-2.8.0}/src/depthtensor.egg-info/requires.txt +0 -0
  33. {depthtensor-2.6.0 → depthtensor-2.8.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.6.0
3
+ Version: 2.8.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.6.0"
7
+ version = "2.8.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.6.0"
9
+ __version__ = "2.8.0"
@@ -59,8 +59,31 @@ def where(
59
59
  /,
60
60
  *,
61
61
  device: Device | None = None,
62
- requires_grad: bool = False,
62
+ requires_grad: bool | None = None,
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. If None, inferred from inputs.
77
+ requires_grad : bool | None, optional
78
+ Whether the result requires gradient computation. If None, inferred from inputs (x, y).
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:
@@ -68,6 +91,9 @@ def where(
68
91
 
69
92
  # * One parameter overload
70
93
  if (x is None) and (y is None):
94
+ if requires_grad is None:
95
+ requires_grad = False
96
+
71
97
  data = to_tensordata(condition, device=device)
72
98
  if device == "cpu":
73
99
  result = np.where(data) # type: ignore
@@ -75,9 +101,16 @@ def where(
75
101
  if cp is None:
76
102
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
77
103
  result = cp.where(data)
78
- return tuple([Tensor(array, requires_grad=requires_grad) for array in result])
104
+ return tuple([Tensor._fast_init(array, device=device, requires_grad=requires_grad) for array in result])
79
105
  # * Two parameters overload
80
106
  elif x is not None and y is not None:
107
+ if requires_grad is None:
108
+ requires_grad = False
109
+ if isinstance(x, Tensor):
110
+ requires_grad = requires_grad or x.requires_grad
111
+ if isinstance(y, Tensor):
112
+ requires_grad = requires_grad or y.requires_grad
113
+
81
114
  if (
82
115
  not (get_device(x) == get_device(y) == device)
83
116
  and not isinstance(x, (int, float, list, tuple))
@@ -94,7 +127,7 @@ def where(
94
127
  if cp is None:
95
128
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
96
129
  result = cp.where(data, x_data, y_data)
97
- return Tensor(result, requires_grad=requires_grad)
130
+ return Tensor._fast_init(result, device=device, requires_grad=requires_grad)
98
131
  else:
99
132
  raise ValueError("Both x and y parameters must be given.")
100
133
 
@@ -141,7 +174,7 @@ def wrapper_2in_1out(
141
174
  if cp is None:
142
175
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
143
176
  y = getattr(cp, func_name)(x1, x2, out=out, dtype=dtype, casting=casting)
144
- return Tensor(y)
177
+ return Tensor._fast_init(y, device=op_device)
145
178
 
146
179
 
147
180
  def equal(
@@ -157,6 +190,33 @@ def equal(
157
190
  dtype: None = None,
158
191
  subok: bool = True,
159
192
  ) -> TensorType:
193
+ """
194
+ Return (x1 == x2) element-wise.
195
+
196
+ Parameters
197
+ ----------
198
+ x1, x2 : TensorLike
199
+ Input tensors.
200
+ out : TensorDataBool | None, optional
201
+ A location into which the result is stored.
202
+ device : Device | None, optional
203
+ The device to place the result on.
204
+ where : TensorDataBool | bool, optional
205
+ This condition is broadcast over the input.
206
+ casting : Casting, optional
207
+ Controls what kind of data casting may occur.
208
+ order : Order, optional
209
+ Controls the memory layout of the result.
210
+ dtype : None, optional
211
+ Overrides the data type of the result.
212
+ subok : bool, optional
213
+ If True, then sub-classes will be passed-through.
214
+
215
+ Returns
216
+ -------
217
+ TensorType
218
+ Output array, element-wise comparison of x1 and x2.
219
+ """
160
220
  return wrapper_2in_1out(
161
221
  x1,
162
222
  x2,
@@ -184,6 +244,33 @@ def not_equal(
184
244
  dtype: None = None,
185
245
  subok: bool = True,
186
246
  ) -> TensorType:
247
+ """
248
+ Return (x1 != x2) element-wise.
249
+
250
+ Parameters
251
+ ----------
252
+ x1, x2 : TensorLike
253
+ Input tensors.
254
+ out : TensorDataBool | None, optional
255
+ A location into which the result is stored.
256
+ device : Device | None, optional
257
+ The device to place the result on.
258
+ where : TensorDataBool | bool, optional
259
+ This condition is broadcast over the input.
260
+ casting : Casting, optional
261
+ Controls what kind of data casting may occur.
262
+ order : Order, optional
263
+ Controls the memory layout of the result.
264
+ dtype : None, optional
265
+ Overrides the data type of the result.
266
+ subok : bool, optional
267
+ If True, then sub-classes will be passed-through.
268
+
269
+ Returns
270
+ -------
271
+ TensorType
272
+ Output array, element-wise comparison of x1 and x2.
273
+ """
187
274
  return wrapper_2in_1out(
188
275
  x1,
189
276
  x2,
@@ -211,6 +298,33 @@ def greater(
211
298
  dtype: None = None,
212
299
  subok: bool = True,
213
300
  ) -> TensorType:
301
+ """
302
+ Return the truth value of (x1 > x2) element-wise.
303
+
304
+ Parameters
305
+ ----------
306
+ x1, x2 : TensorLike
307
+ Input tensors.
308
+ out : TensorDataBool | None, optional
309
+ A location into which the result is stored.
310
+ device : Device | None, optional
311
+ The device to place the result on.
312
+ where : TensorDataBool | bool, optional
313
+ This condition is broadcast over the input.
314
+ casting : Casting, optional
315
+ Controls what kind of data casting may occur.
316
+ order : Order, optional
317
+ Controls the memory layout of the result.
318
+ dtype : None, optional
319
+ Overrides the data type of the result.
320
+ subok : bool, optional
321
+ If True, then sub-classes will be passed-through.
322
+
323
+ Returns
324
+ -------
325
+ TensorType
326
+ Output array, element-wise comparison of x1 and x2.
327
+ """
214
328
  return wrapper_2in_1out(
215
329
  x1,
216
330
  x2,
@@ -238,6 +352,33 @@ def greater_equal(
238
352
  dtype: None = None,
239
353
  subok: bool = True,
240
354
  ) -> TensorType:
355
+ """
356
+ Return the truth value of (x1 >= x2) element-wise.
357
+
358
+ Parameters
359
+ ----------
360
+ x1, x2 : TensorLike
361
+ Input tensors.
362
+ out : TensorDataBool | None, optional
363
+ A location into which the result is stored.
364
+ device : Device | None, optional
365
+ The device to place the result on.
366
+ where : TensorDataBool | bool, optional
367
+ This condition is broadcast over the input.
368
+ casting : Casting, optional
369
+ Controls what kind of data casting may occur.
370
+ order : Order, optional
371
+ Controls the memory layout of the result.
372
+ dtype : None, optional
373
+ Overrides the data type of the result.
374
+ subok : bool, optional
375
+ If True, then sub-classes will be passed-through.
376
+
377
+ Returns
378
+ -------
379
+ TensorType
380
+ Output array, element-wise comparison of x1 and x2.
381
+ """
241
382
  return wrapper_2in_1out(
242
383
  x1,
243
384
  x2,
@@ -265,6 +406,33 @@ def less(
265
406
  dtype: None = None,
266
407
  subok: bool = True,
267
408
  ) -> TensorType:
409
+ """
410
+ Return the truth value of (x1 < x2) element-wise.
411
+
412
+ Parameters
413
+ ----------
414
+ x1, x2 : TensorLike
415
+ Input tensors.
416
+ out : TensorDataBool | None, optional
417
+ A location into which the result is stored.
418
+ device : Device | None, optional
419
+ The device to place the result on.
420
+ where : TensorDataBool | bool, optional
421
+ This condition is broadcast over the input.
422
+ casting : Casting, optional
423
+ Controls what kind of data casting may occur.
424
+ order : Order, optional
425
+ Controls the memory layout of the result.
426
+ dtype : None, optional
427
+ Overrides the data type of the result.
428
+ subok : bool, optional
429
+ If True, then sub-classes will be passed-through.
430
+
431
+ Returns
432
+ -------
433
+ TensorType
434
+ Output array, element-wise comparison of x1 and x2.
435
+ """
268
436
  return wrapper_2in_1out(
269
437
  x1,
270
438
  x2,
@@ -292,6 +460,33 @@ def less_equal(
292
460
  dtype: None = None,
293
461
  subok: bool = True,
294
462
  ) -> TensorType:
463
+ """
464
+ Return the truth value of (x1 <= x2) element-wise.
465
+
466
+ Parameters
467
+ ----------
468
+ x1, x2 : TensorLike
469
+ Input tensors.
470
+ out : TensorDataBool | None, optional
471
+ A location into which the result is stored.
472
+ device : Device | None, optional
473
+ The device to place the result on.
474
+ where : TensorDataBool | bool, optional
475
+ This condition is broadcast over the input.
476
+ casting : Casting, optional
477
+ Controls what kind of data casting may occur.
478
+ order : Order, optional
479
+ Controls the memory layout of the result.
480
+ dtype : None, optional
481
+ Overrides the data type of the result.
482
+ subok : bool, optional
483
+ If True, then sub-classes will be passed-through.
484
+
485
+ Returns
486
+ -------
487
+ TensorType
488
+ Output array, element-wise comparison of x1 and x2.
489
+ """
295
490
  return wrapper_2in_1out(
296
491
  x1,
297
492
  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"]