DepthTensor 2.5.0__tar.gz → 2.6.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 (25) hide show
  1. {depthtensor-2.5.0 → depthtensor-2.6.0}/PKG-INFO +1 -1
  2. {depthtensor-2.5.0 → depthtensor-2.6.0}/pyproject.toml +1 -1
  3. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/DepthTensor/__init__.py +1 -1
  4. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/DepthTensor/_core/ops/comparison.py +5 -2
  5. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/DepthTensor/_core/ops/elementwise.py +36 -33
  6. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/DepthTensor/_core/random/generator.py +4 -3
  7. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/DepthTensor/_core/utils.py +8 -6
  8. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/DepthTensor/tensor.py +11 -9
  9. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/depthtensor.egg-info/PKG-INFO +1 -1
  10. {depthtensor-2.5.0 → depthtensor-2.6.0}/LICENSE +0 -0
  11. {depthtensor-2.5.0 → depthtensor-2.6.0}/README.md +0 -0
  12. {depthtensor-2.5.0 → depthtensor-2.6.0}/setup.cfg +0 -0
  13. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/DepthTensor/_core/__init__.py +0 -0
  14. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/DepthTensor/_core/exceptions.py +0 -0
  15. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/DepthTensor/_core/ops/__init__.py +0 -0
  16. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/DepthTensor/_core/ops/creation.py +0 -0
  17. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/DepthTensor/_core/ops/function.py +0 -0
  18. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/DepthTensor/_core/ops/reduction.py +0 -0
  19. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/DepthTensor/_core/random/__init__.py +0 -0
  20. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/DepthTensor/autodiff.py +0 -0
  21. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/DepthTensor/typing.py +0 -0
  22. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/depthtensor.egg-info/SOURCES.txt +0 -0
  23. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/depthtensor.egg-info/dependency_links.txt +0 -0
  24. {depthtensor-2.5.0 → depthtensor-2.6.0}/src/depthtensor.egg-info/requires.txt +0 -0
  25. {depthtensor-2.5.0 → depthtensor-2.6.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.0
3
+ Version: 2.6.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.0"
7
+ version = "2.6.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.0"
9
+ __version__ = "2.6.0"
@@ -120,9 +120,12 @@ def wrapper_2in_1out(
120
120
  ) -> TensorType:
121
121
  from ...tensor import Tensor
122
122
 
123
- op_device = get_two_operand_op_device(x1, x2, device)
123
+ x1_is_tensor, x2_is_tensor = isinstance(x1, Tensor), isinstance(x2, Tensor)
124
+ op_device = get_two_operand_op_device(x1, x2, x1_is_tensor, x2_is_tensor, device)
125
+
126
+ x1 = to_tensordata(x1, device=op_device) if not x1_is_tensor else x1.data
127
+ x2 = to_tensordata(x2, device=op_device) if not x2_is_tensor else x2.data
124
128
 
125
- x1, x2 = to_tensordata(x1, device=op_device), to_tensordata(x2, device=op_device)
126
129
  if op_device == "cpu":
127
130
  y = getattr(np, func_name)(
128
131
  x1,
@@ -1,4 +1,4 @@
1
- from typing import Any, Callable
1
+ from typing import Callable
2
2
 
3
3
  from ...typing import (
4
4
  TensorType,
@@ -41,21 +41,17 @@ except (ImportError, ModuleNotFoundError):
41
41
  ###
42
42
 
43
43
 
44
- def get_requires_grad_and_prev(x1: TensorLike, x2: TensorLike):
44
+ def get_requires_grad_and_prev(
45
+ x1: TensorLike, x2: TensorLike, x1_is_tensor: bool, x2_is_tensor: bool
46
+ ):
45
47
  from ...tensor import Tensor
46
48
 
47
49
  y_requires_grad = False
48
- prev = ()
49
- if isinstance(x1, Tensor):
50
+ if x1_is_tensor:
50
51
  y_requires_grad = x1.requires_grad
51
- prev = (x1,)
52
- if isinstance(x2, Tensor):
52
+ if x2_is_tensor:
53
53
  y_requires_grad = y_requires_grad or x2.requires_grad
54
- if len(prev) == 1:
55
- prev = (x1, x2)
56
- else:
57
- prev = (x2,)
58
- return y_requires_grad, prev
54
+ return y_requires_grad
59
55
 
60
56
 
61
57
  def wrapper_2in_1out(
@@ -80,8 +76,11 @@ def wrapper_2in_1out(
80
76
 
81
77
  from ...tensor import Tensor
82
78
 
83
- op_device = get_two_operand_op_device(x1, x2, device)
84
- a1, a2 = to_tensordata(x1, op_device), to_tensordata(x2, op_device)
79
+ x1_is_tensor, x2_is_tensor = isinstance(x1, Tensor), isinstance(x2, Tensor)
80
+
81
+ op_device = get_two_operand_op_device(x1, x2, x1_is_tensor, x2_is_tensor, device)
82
+ a1 = to_tensordata(x1, op_device) if not x1_is_tensor else x1.data
83
+ a2 = to_tensordata(x2, op_device) if not x2_is_tensor else x2.data
85
84
 
86
85
  if op_device == "cpu":
87
86
  kwargs = {
@@ -100,12 +99,12 @@ def wrapper_2in_1out(
100
99
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
101
100
  y = getattr(cp, func_name)(a1, a2, out=out, dtype=dtype, casting=casting)
102
101
 
103
- if in_place and isinstance(x1, Tensor):
102
+ if in_place and x1_is_tensor:
104
103
  x1.data = y
105
104
  return x1
106
105
 
107
- requires_grad, prev = get_requires_grad_and_prev(x1, x2)
108
- return Tensor(y, prev=prev, requires_grad=requires_grad)
106
+ requires_grad = get_requires_grad_and_prev(x1, x2, x1_is_tensor, x2_is_tensor)
107
+ return Tensor(y, requires_grad=requires_grad)
109
108
 
110
109
 
111
110
  def wrapper_1in_1out(
@@ -166,33 +165,33 @@ def wrapper_diff_2in_1out(
166
165
 
167
166
  from ...tensor import Tensor
168
167
 
168
+ x1_is_tensor, x2_is_tensor = isinstance(x1, Tensor), isinstance(x2, Tensor)
169
+
169
170
  def backward() -> None:
170
171
  if y.grad is None:
171
172
  y.zero_grad()
172
173
 
173
174
  y_grad: TensorData = y.grad # type: ignore (y.grad cannot be None)
174
- device = get_two_operand_op_device(x1, x2, None)
175
+ device = get_two_operand_op_device(x1, x2, x1_is_tensor, x2_is_tensor, None)
175
176
  x1_data, x2_data = to_tensordata(x1, device=device), to_tensordata(
176
177
  x2, device=device
177
178
  )
178
179
 
179
- prev = []
180
- if isinstance(x1, Tensor) and x1.requires_grad:
180
+ if x1_is_tensor and x1.requires_grad:
181
181
  if x1.grad is None:
182
182
  x1.zero_grad()
183
- x1.grad += callback_x1(y_grad, x1.shape, device, x1_data, x2_data).astype(
184
- x1.dtype
185
- )
186
- prev.append(x1)
187
- if isinstance(x2, Tensor) and x2.requires_grad:
183
+ x1.grad += callback_x1(y_grad, x1.shape, device, x1_data, x2_data)
184
+ if x2_is_tensor and x2.requires_grad:
188
185
  if x2.grad is None:
189
186
  x2.zero_grad()
190
- x2.grad += callback_x2(y_grad, x2.shape, device, x1_data, x2_data).astype(
191
- x2.dtype
192
- )
193
- prev.append(x2)
194
- y.prev = tuple(prev)
195
-
187
+ x2.grad += callback_x2(y_grad, x2.shape, device, x1_data, x2_data)
188
+
189
+ prev = []
190
+ if x1_is_tensor and x1.requires_grad:
191
+ prev.append(x1)
192
+ if x2_is_tensor and x2.requires_grad:
193
+ prev.append(x2)
194
+ y.prev = tuple(prev)
196
195
  y.backward = backward
197
196
 
198
197
 
@@ -212,8 +211,9 @@ def wrapper_diff_1in_1out(y: TensorType, x1: TensorLike, callback_x1: Callable)
212
211
  if x1.grad is None:
213
212
  x1.zero_grad()
214
213
  x1.grad += callback_x1(result_grad, x1.shape, x1.device, _x)
215
- y.prev = (x1,)
216
214
 
215
+ if isinstance(x1, Tensor) and x1.requires_grad:
216
+ y.prev = (x1,)
217
217
  y.backward = backward
218
218
 
219
219
 
@@ -882,9 +882,12 @@ class mean_cls(Function):
882
882
  y.zero_grad()
883
883
  if x.grad is None:
884
884
  x.zero_grad()
885
- print(x)
886
885
  x.grad += callback_x1(y.grad, x.shape, y.device)
887
886
 
887
+ from ...tensor import Tensor
888
+
889
+ if isinstance(x, Tensor) and x.requires_grad:
890
+ y.prev = (x,)
888
891
  y.backward = backward
889
892
 
890
893
  def __call__(
@@ -929,7 +932,7 @@ class mean_cls(Function):
929
932
  return x
930
933
  requires_grad = x.requires_grad
931
934
 
932
- y = Tensor(y, prev=(x,), requires_grad=requires_grad)
935
+ y = Tensor(y, requires_grad=requires_grad)
933
936
  if differentiate:
934
937
  self.link(y, x, axis, keepdims)
935
938
  else:
@@ -2,7 +2,7 @@ from typing import Optional, overload, Any
2
2
 
3
3
  from numpy import random
4
4
 
5
- from ...typing import DTypeLike, TensorType, Axis, int64, Device
5
+ from ...typing import DTypeLike, TensorType, Axis, int64, Device, float32
6
6
 
7
7
  from ..exceptions import CuPyNotFound, CUPY_NOT_FOUND_MSG
8
8
 
@@ -113,16 +113,17 @@ def uniform(
113
113
  size: Axis | None = None,
114
114
  *,
115
115
  device: Device = "cpu",
116
+ dtype: DTypeLike = float32,
116
117
  requires_grad: bool = False,
117
118
  ):
118
119
  from ...tensor import Tensor
119
120
 
120
121
  if device == "cpu":
121
- y = random.uniform(low=low, high=high, size=size)
122
+ y = random.uniform(low=low, high=high, size=size).astype(dtype)
122
123
  else:
123
124
  if cp is None:
124
125
  raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
125
- y = cp.random.uniform(low=low, high=high, size=size)
126
+ y = cp.random.uniform(low=low, high=high, size=size, dtype=dtype)
126
127
  return Tensor(y, requires_grad=requires_grad)
127
128
 
128
129
 
@@ -147,19 +147,21 @@ def get_complement_device(device: Device) -> Device:
147
147
 
148
148
 
149
149
  def get_two_operand_op_device(
150
- x1: TensorLike, x2: TensorLike, device: Device | None
150
+ x1: TensorLike,
151
+ x2: TensorLike,
152
+ x1_is_tensor: bool,
153
+ x2_is_tensor: bool,
154
+ device: Device | None,
151
155
  ) -> Device:
152
156
  if device is not None:
153
157
  return device
154
158
 
155
- from ..tensor import Tensor
156
-
157
- if isinstance(x1, Tensor) and isinstance(x2, Tensor):
159
+ if x1_is_tensor and x2_is_tensor:
158
160
  if x1.device != x2.device:
159
161
  raise DeviceMismatch(DEVICE_MISMATCH_MSG)
160
162
  op_device = x1.device
161
163
  else:
162
- if isinstance(x1, Tensor):
164
+ if x1_is_tensor:
163
165
  op_device = x1.device
164
166
  if get_device(x2) != x1.device and not isinstance(
165
167
  x2,
@@ -174,7 +176,7 @@ def get_two_operand_op_device(
174
176
  raise DeviceMismatch(
175
177
  f"There is a incompatibility in datatypes between the two operands of types Tensor and {type(x2)}. Expected datatype of device: {x1.device} {"(CuPy arrays, GPU Tensors)" if x1.device == "gpu" else "(NumPy arrays, CPU Tensors, ints, floats, lists, tuples)"}, got datatype of type: {type(x2)}."
176
178
  )
177
- elif isinstance(x2, Tensor):
179
+ elif x2_is_tensor:
178
180
  op_device = x2.device
179
181
  if get_device(x1) != x2.device and not isinstance(
180
182
  x2,
@@ -112,19 +112,21 @@ class Tensor:
112
112
  self.grad = None
113
113
  self.name = name
114
114
 
115
- def zero_grad(self) -> TensorData:
115
+ def zero_grad(self) -> None:
116
116
  if not self.requires_grad:
117
117
  raise RuntimeError(
118
118
  "Attempted to zero-gradient initialize an undifferentiable tensor."
119
119
  )
120
- if self.device == "cpu":
121
- grad = np.zeros_like(self.data)
120
+ if self.grad is None:
121
+ if self.device == "cpu":
122
+ grad = np.zeros_like(self.data)
123
+ else:
124
+ if cp is None:
125
+ raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
126
+ grad = cp.zeros_like(self.data)
127
+ self.grad = grad
122
128
  else:
123
- if cp is None:
124
- raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
125
- grad = cp.zeros_like(self.data)
126
- self.grad = grad
127
- return grad
129
+ self.grad.fill(0)
128
130
 
129
131
  ###
130
132
  ###
@@ -551,7 +553,7 @@ class Tensor:
551
553
  return iter(self.data)
552
554
 
553
555
  def __repr__(self) -> str:
554
- return f"Tensor({self.data}, device={self.device}{f", name={self.name}" if self.name != "" else ""})"
556
+ return f"Tensor({self.data}, device={self.device}{f", name={self.name}" if self.name != "" else ""}, req_grad={self.requires_grad})"
555
557
 
556
558
  def __hash__(self) -> int:
557
559
  return id(self)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: depthtensor
3
- Version: 2.5.0
3
+ Version: 2.6.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
File without changes
File without changes
File without changes