DepthTensor 2.1.0__tar.gz → 2.2.1__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 (26) hide show
  1. {depthtensor-2.1.0/src/DepthTensor.egg-info → depthtensor-2.2.1}/PKG-INFO +1 -1
  2. {depthtensor-2.1.0 → depthtensor-2.2.1}/pyproject.toml +1 -1
  3. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor/__init__.py +1 -1
  4. depthtensor-2.2.1/src/DepthTensor/_core/ops/creation.py +126 -0
  5. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor/tensor.py +42 -1
  6. {depthtensor-2.1.0 → depthtensor-2.2.1/src/DepthTensor.egg-info}/PKG-INFO +1 -1
  7. depthtensor-2.1.0/src/DepthTensor/_core/ops/creation.py +0 -89
  8. {depthtensor-2.1.0 → depthtensor-2.2.1}/LICENSE +0 -0
  9. {depthtensor-2.1.0 → depthtensor-2.2.1}/README.md +0 -0
  10. {depthtensor-2.1.0 → depthtensor-2.2.1}/setup.cfg +0 -0
  11. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor/_core/__init__.py +0 -0
  12. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor/_core/exceptions.py +0 -0
  13. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor/_core/ops/__init__.py +0 -0
  14. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor/_core/ops/comparison.py +0 -0
  15. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor/_core/ops/diff.py +0 -0
  16. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor/_core/ops/elementwise.py +0 -0
  17. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor/_core/ops/reduction.py +0 -0
  18. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor/_core/random/__init__.py +0 -0
  19. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor/_core/random/generator.py +0 -0
  20. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor/_core/utils.py +0 -0
  21. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor/autodiff.py +0 -0
  22. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor/typing.py +0 -0
  23. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor.egg-info/SOURCES.txt +0 -0
  24. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor.egg-info/dependency_links.txt +0 -0
  25. {depthtensor-2.1.0 → depthtensor-2.2.1}/src/DepthTensor.egg-info/requires.txt +0 -0
  26. {depthtensor-2.1.0 → depthtensor-2.2.1}/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.1.0
3
+ Version: 2.2.1
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 = "2.1.0"
7
+ version = "2.2.1"
8
8
  readme = "README.md"
9
9
  dependencies = [
10
10
  "numpy"
@@ -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.1.0"
9
+ __version__ = "2.2.1"
@@ -0,0 +1,126 @@
1
+ from typing import Optional, Literal, Union
2
+
3
+ from ...typing import (
4
+ TensorLike,
5
+ DTypeLike,
6
+ Order,
7
+ AxisLike,
8
+ OperandLike,
9
+ DeviceLike,
10
+ ShapeLike,
11
+ NDArrayLike,
12
+ )
13
+
14
+ from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
15
+
16
+ from ..utils import to_xp_array, get_device
17
+
18
+ import numpy as np
19
+
20
+ try:
21
+ import cupy as cp
22
+ except (ImportError, ModuleNotFoundError):
23
+ cp = None
24
+
25
+ ###
26
+ ###
27
+ ###
28
+
29
+
30
+ def zeros_like(
31
+ a: OperandLike,
32
+ /,
33
+ *,
34
+ device: Optional[DeviceLike] = None,
35
+ requires_grad: bool = False,
36
+ dtype: Optional[DTypeLike] = None,
37
+ order: Order = "K",
38
+ subok: bool = True,
39
+ shape: Optional[AxisLike] = None,
40
+ ) -> TensorLike:
41
+ from ...tensor import Tensor
42
+
43
+ if device is None:
44
+ device_op = get_device(a)
45
+ else:
46
+ device_op = device
47
+ a = to_xp_array(a)
48
+ if device_op == "cpu":
49
+ y = np.zeros_like(a, dtype=dtype, order=order, subok=subok, shape=shape)
50
+ else:
51
+ if cp is None:
52
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
53
+ y = cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape)
54
+ return Tensor(y, requires_grad=requires_grad)
55
+
56
+
57
+ def ones_like(
58
+ a: OperandLike,
59
+ /,
60
+ *,
61
+ device: Optional[DeviceLike] = None,
62
+ requires_grad: bool = False,
63
+ dtype: Optional[DTypeLike] = None,
64
+ order: Order = "K",
65
+ subok: bool = True,
66
+ shape: Optional[AxisLike] = None,
67
+ ) -> TensorLike:
68
+ from ...tensor import Tensor
69
+
70
+ if device is None:
71
+ device_op = get_device(a)
72
+ else:
73
+ device_op = device
74
+ a = to_xp_array(a)
75
+ if device_op == "cpu":
76
+ y = np.ones_like(a, dtype=dtype, order=order, subok=subok, shape=shape)
77
+ else:
78
+ if cp is None:
79
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
80
+ y = cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape)
81
+ return Tensor(y, requires_grad=requires_grad)
82
+
83
+
84
+ def zeros(
85
+ shape: ShapeLike,
86
+ dtype: DTypeLike = float,
87
+ order: Literal["C", "F"] = "C",
88
+ *,
89
+ device: DeviceLike = "cpu",
90
+ requires_grad: bool = False,
91
+ ) -> TensorLike:
92
+ from ...tensor import Tensor
93
+
94
+ if device == "cpu":
95
+ y = np.zeros(shape=shape, dtype=dtype, order=order)
96
+ else:
97
+ if cp is None:
98
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
99
+ y = cp.zeros(shape=shape, dtype=dtype, order=order)
100
+ return Tensor(y, requires_grad=requires_grad)
101
+
102
+
103
+ def ones(
104
+ shape: ShapeLike,
105
+ dtype: DTypeLike = float,
106
+ order: Literal["C", "F"] = "C",
107
+ *,
108
+ device: DeviceLike = "cpu",
109
+ requires_grad: bool = False,
110
+ ) -> TensorLike:
111
+ from ...tensor import Tensor
112
+
113
+ if device == "cpu":
114
+ y = np.ones(shape=shape, dtype=dtype, order=order)
115
+ else:
116
+ if cp is None:
117
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
118
+ y = cp.ones(shape=shape, dtype=dtype, order=order)
119
+ return Tensor(y, requires_grad=requires_grad)
120
+
121
+
122
+ ###
123
+ ###
124
+ ###
125
+
126
+ __all__ = ["zeros_like", "ones_like", "zeros", "ones"]
@@ -92,6 +92,12 @@ allowed_dtype_kind = "uifb"
92
92
  ###
93
93
 
94
94
 
95
+ def is_list_of_type(list_obj, type_obj) -> bool:
96
+ if not isinstance(list_obj, list):
97
+ return False
98
+ return all(isinstance(x, type_obj) for x in list_obj)
99
+
100
+
95
101
  class Tensor:
96
102
  data: NDArrayLike
97
103
  device: DeviceLike
@@ -100,7 +106,7 @@ class Tensor:
100
106
 
101
107
  def __init__(
102
108
  self,
103
- obj: OperandLike,
109
+ obj: Union[OperandLike, List[NDArrayLike]],
104
110
  /,
105
111
  *,
106
112
  dtype: Optional[DTypeLike] = None,
@@ -131,6 +137,8 @@ class Tensor:
131
137
  raise TypeError("Expected a numerical CuPy array.")
132
138
  elif isinstance(obj, Tensor):
133
139
  self.data = xp_array_to_device(obj.data, self.device)
140
+ elif is_list_of_type(obj, Tensor):
141
+ self.data = [xp_array_to_device(t) for t in obj] # type: ignore
134
142
  else:
135
143
  self.data = xp_array_to_device(
136
144
  to_xp_array(obj, self.device), self.device
@@ -227,6 +235,11 @@ class Tensor:
227
235
  return self.copy()
228
236
  else:
229
237
  if in_place:
238
+ if self.requires_grad:
239
+ raise RuntimeError(
240
+ "In-place operations (device switch) are forbidden on differentiable tensors."
241
+ )
242
+
230
243
  self.device = device
231
244
  self.prev = () if clear_prev else self.prev
232
245
  self.data = xp_array_to_device(self.data, device=device)
@@ -392,6 +405,10 @@ class Tensor:
392
405
  )
393
406
 
394
407
  def __iadd__(self, t: OperandLike) -> Tensor:
408
+ if self.requires_grad:
409
+ raise RuntimeError(
410
+ "In-place operations are forbidden on differentiable tensors."
411
+ )
395
412
  return add(self, t, in_place=True)
396
413
 
397
414
  def __sub__(self, t: OperandLike) -> Tensor:
@@ -405,6 +422,10 @@ class Tensor:
405
422
  )
406
423
 
407
424
  def __isub__(self, t: OperandLike) -> Tensor:
425
+ if self.requires_grad:
426
+ raise RuntimeError(
427
+ "In-place operations are forbidden on differentiable tensors."
428
+ )
408
429
  return subtract(self, t, in_place=True)
409
430
 
410
431
  def __mul__(self, t: OperandLike) -> Tensor:
@@ -418,6 +439,10 @@ class Tensor:
418
439
  )
419
440
 
420
441
  def __imul__(self, t: OperandLike) -> Tensor:
442
+ if self.requires_grad:
443
+ raise RuntimeError(
444
+ "In-place operations are forbidden on differentiable tensors."
445
+ )
421
446
  return multiply(self, t, in_place=True)
422
447
 
423
448
  def __matmul__(self, t: OperandLike) -> Tensor:
@@ -431,6 +456,10 @@ class Tensor:
431
456
  )
432
457
 
433
458
  def __imatmul__(self, t: OperandLike) -> Tensor:
459
+ if self.requires_grad:
460
+ raise RuntimeError(
461
+ "In-place operations are forbidden on differentiable tensors."
462
+ )
434
463
  return matmul(self, t, in_place=True)
435
464
 
436
465
  def __truediv__(self, t: OperandLike) -> Tensor:
@@ -444,6 +473,10 @@ class Tensor:
444
473
  )
445
474
 
446
475
  def __itruediv__(self, t: OperandLike) -> Tensor:
476
+ if self.requires_grad:
477
+ raise RuntimeError(
478
+ "In-place operations are forbidden on differentiable tensors."
479
+ )
447
480
  return divide(self, t, in_place=True)
448
481
 
449
482
  def __pow__(self, t: OperandLike) -> Tensor:
@@ -452,6 +485,10 @@ class Tensor:
452
485
  )
453
486
 
454
487
  def __ipow__(self, t: OperandLike) -> Tensor:
488
+ if self.requires_grad:
489
+ raise RuntimeError(
490
+ "In-place operations are forbidden on differentiable tensors."
491
+ )
455
492
  return power(self, t, in_place=True)
456
493
 
457
494
  ###
@@ -487,6 +524,10 @@ class Tensor:
487
524
  return self.data[index]
488
525
 
489
526
  def __setitem__(self, index, value) -> Any:
527
+ if self.requires_grad:
528
+ raise RuntimeError(
529
+ "In-place operations (indexing) are forbidden on differentiable tensors."
530
+ )
490
531
  self.data[index] = value
491
532
 
492
533
  def __iter__(self) -> Iterator:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: DepthTensor
3
- Version: 2.1.0
3
+ Version: 2.2.1
4
4
  Description-Content-Type: text/markdown
5
5
  License-File: LICENSE
6
6
  Requires-Dist: numpy
@@ -1,89 +0,0 @@
1
- from typing import Optional
2
-
3
- from ...typing import TensorLike, DTypeLike, Order, AxisLike, OperandLike, DeviceLike
4
-
5
- from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
6
-
7
- from ..utils import to_xp_array, get_device
8
-
9
- import numpy as np
10
-
11
- try:
12
- import cupy as cp
13
- except (ImportError, ModuleNotFoundError):
14
- cp = None
15
-
16
- ###
17
- ###
18
- ###
19
-
20
-
21
- def zeros_like(
22
- a: OperandLike,
23
- /,
24
- *,
25
- device: Optional[DeviceLike] = None,
26
- requires_grad: bool = False,
27
- dtype: Optional[DTypeLike] = None,
28
- order: Order = "K",
29
- subok: bool = True,
30
- shape: Optional[AxisLike] = None,
31
- ) -> TensorLike:
32
- from ...tensor import Tensor
33
-
34
- if device is None:
35
- device_op = get_device(a)
36
- else:
37
- device_op = device
38
- a = to_xp_array(a)
39
- if device_op == "cpu":
40
- return Tensor(
41
- np.zeros_like(a, dtype=dtype, order=order, subok=subok, shape=shape),
42
- requires_grad=requires_grad,
43
- )
44
- else:
45
- if cp is None:
46
- raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
47
- return Tensor(
48
- cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape),
49
- requires_grad=requires_grad,
50
- )
51
-
52
-
53
- def ones_like(
54
- a: OperandLike,
55
- /,
56
- *,
57
- device: Optional[DeviceLike] = None,
58
- requires_grad: bool = False,
59
- dtype: Optional[DTypeLike] = None,
60
- order: Order = "K",
61
- subok: bool = True,
62
- shape: Optional[AxisLike] = None,
63
- ) -> TensorLike:
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_xp_array(a)
71
- if device_op == "cpu":
72
- return Tensor(
73
- np.ones_like(a, dtype=dtype, order=order, subok=subok, shape=shape),
74
- requires_grad=requires_grad,
75
- )
76
- else:
77
- if cp is None:
78
- raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
79
- return Tensor(
80
- cp.zeros_like(a, dtype=dtype, order=order, subok=None, shape=shape),
81
- requires_grad=requires_grad,
82
- )
83
-
84
-
85
- ###
86
- ###
87
- ###
88
-
89
- __all__ = ["zeros_like", "ones_like"]
File without changes
File without changes
File without changes