DepthTensor 2.5.1__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.
- {depthtensor-2.5.1 → depthtensor-2.6.0}/PKG-INFO +1 -1
- {depthtensor-2.5.1 → depthtensor-2.6.0}/pyproject.toml +1 -1
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/DepthTensor/__init__.py +1 -1
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/DepthTensor/_core/ops/comparison.py +5 -2
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/DepthTensor/_core/ops/elementwise.py +22 -19
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/DepthTensor/_core/random/generator.py +4 -3
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/DepthTensor/_core/utils.py +8 -6
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/DepthTensor/tensor.py +11 -9
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/depthtensor.egg-info/PKG-INFO +1 -1
- {depthtensor-2.5.1 → depthtensor-2.6.0}/LICENSE +0 -0
- {depthtensor-2.5.1 → depthtensor-2.6.0}/README.md +0 -0
- {depthtensor-2.5.1 → depthtensor-2.6.0}/setup.cfg +0 -0
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/DepthTensor/_core/__init__.py +0 -0
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/DepthTensor/_core/exceptions.py +0 -0
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/DepthTensor/_core/ops/__init__.py +0 -0
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/DepthTensor/_core/ops/creation.py +0 -0
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/DepthTensor/_core/ops/function.py +0 -0
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/DepthTensor/_core/ops/reduction.py +0 -0
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/DepthTensor/_core/random/__init__.py +0 -0
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/DepthTensor/autodiff.py +0 -0
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/DepthTensor/typing.py +0 -0
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/depthtensor.egg-info/SOURCES.txt +0 -0
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/depthtensor.egg-info/dependency_links.txt +0 -0
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/depthtensor.egg-info/requires.txt +0 -0
- {depthtensor-2.5.1 → depthtensor-2.6.0}/src/depthtensor.egg-info/top_level.txt +0 -0
|
@@ -120,9 +120,12 @@ def wrapper_2in_1out(
|
|
|
120
120
|
) -> TensorType:
|
|
121
121
|
from ...tensor import Tensor
|
|
122
122
|
|
|
123
|
-
|
|
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
|
|
1
|
+
from typing import Callable
|
|
2
2
|
|
|
3
3
|
from ...typing import (
|
|
4
4
|
TensorType,
|
|
@@ -41,13 +41,15 @@ except (ImportError, ModuleNotFoundError):
|
|
|
41
41
|
###
|
|
42
42
|
|
|
43
43
|
|
|
44
|
-
def get_requires_grad_and_prev(
|
|
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
|
-
if
|
|
50
|
+
if x1_is_tensor:
|
|
49
51
|
y_requires_grad = x1.requires_grad
|
|
50
|
-
if
|
|
52
|
+
if x2_is_tensor:
|
|
51
53
|
y_requires_grad = y_requires_grad or x2.requires_grad
|
|
52
54
|
return y_requires_grad
|
|
53
55
|
|
|
@@ -74,8 +76,11 @@ def wrapper_2in_1out(
|
|
|
74
76
|
|
|
75
77
|
from ...tensor import Tensor
|
|
76
78
|
|
|
77
|
-
|
|
78
|
-
|
|
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
|
|
79
84
|
|
|
80
85
|
if op_device == "cpu":
|
|
81
86
|
kwargs = {
|
|
@@ -94,11 +99,11 @@ def wrapper_2in_1out(
|
|
|
94
99
|
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
95
100
|
y = getattr(cp, func_name)(a1, a2, out=out, dtype=dtype, casting=casting)
|
|
96
101
|
|
|
97
|
-
if in_place and
|
|
102
|
+
if in_place and x1_is_tensor:
|
|
98
103
|
x1.data = y
|
|
99
104
|
return x1
|
|
100
105
|
|
|
101
|
-
requires_grad = get_requires_grad_and_prev(x1, x2)
|
|
106
|
+
requires_grad = get_requires_grad_and_prev(x1, x2, x1_is_tensor, x2_is_tensor)
|
|
102
107
|
return Tensor(y, requires_grad=requires_grad)
|
|
103
108
|
|
|
104
109
|
|
|
@@ -160,33 +165,31 @@ def wrapper_diff_2in_1out(
|
|
|
160
165
|
|
|
161
166
|
from ...tensor import Tensor
|
|
162
167
|
|
|
168
|
+
x1_is_tensor, x2_is_tensor = isinstance(x1, Tensor), isinstance(x2, Tensor)
|
|
169
|
+
|
|
163
170
|
def backward() -> None:
|
|
164
171
|
if y.grad is None:
|
|
165
172
|
y.zero_grad()
|
|
166
173
|
|
|
167
174
|
y_grad: TensorData = y.grad # type: ignore (y.grad cannot be None)
|
|
168
|
-
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)
|
|
169
176
|
x1_data, x2_data = to_tensordata(x1, device=device), to_tensordata(
|
|
170
177
|
x2, device=device
|
|
171
178
|
)
|
|
172
179
|
|
|
173
|
-
if
|
|
180
|
+
if x1_is_tensor and x1.requires_grad:
|
|
174
181
|
if x1.grad is None:
|
|
175
182
|
x1.zero_grad()
|
|
176
|
-
x1.grad += callback_x1(y_grad, x1.shape, device, x1_data, x2_data)
|
|
177
|
-
|
|
178
|
-
)
|
|
179
|
-
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:
|
|
180
185
|
if x2.grad is None:
|
|
181
186
|
x2.zero_grad()
|
|
182
|
-
x2.grad += callback_x2(y_grad, x2.shape, device, x1_data, x2_data)
|
|
183
|
-
x2.dtype
|
|
184
|
-
)
|
|
187
|
+
x2.grad += callback_x2(y_grad, x2.shape, device, x1_data, x2_data)
|
|
185
188
|
|
|
186
189
|
prev = []
|
|
187
|
-
if
|
|
190
|
+
if x1_is_tensor and x1.requires_grad:
|
|
188
191
|
prev.append(x1)
|
|
189
|
-
if
|
|
192
|
+
if x2_is_tensor and x2.requires_grad:
|
|
190
193
|
prev.append(x2)
|
|
191
194
|
y.prev = tuple(prev)
|
|
192
195
|
y.backward = backward
|
|
@@ -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,
|
|
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
|
-
|
|
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
|
|
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
|
|
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) ->
|
|
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.
|
|
121
|
-
|
|
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
|
-
|
|
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)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|